You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
61 lines
1.8 KiB
61 lines
1.8 KiB
use sexp::{Atom, Sexp};
|
|
|
|
use crate::asm::error::Error;
|
|
|
|
pub fn expect_list(expr: Option<Sexp>, allow_empty: bool) -> Result<Vec<Sexp>, Error> {
|
|
if let Some(expr) = expr {
|
|
match &expr {
|
|
Sexp::Atom(_) => {
|
|
return Err(Error::ParseIn("Expected a list".into(), expr));
|
|
}
|
|
Sexp::List(list) => {
|
|
if !allow_empty && list.is_empty() {
|
|
return Err(Error::ParseIn("Routine: Empty list".into(), expr));
|
|
}
|
|
|
|
if let Sexp::List(list) = expr {
|
|
return Ok(list);
|
|
} else {
|
|
unreachable!();
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
Err(Error::Parse("Expected a list, got nothing".into()))
|
|
}
|
|
|
|
pub fn expect_atom(expr: Option<Sexp>) -> Result<Atom, Error> {
|
|
if let Some(expr) = expr {
|
|
match &expr {
|
|
Sexp::Atom(_atom) => {
|
|
if let Sexp::Atom(a) = expr {
|
|
return Ok(a);
|
|
} else {
|
|
unreachable!();
|
|
}
|
|
}
|
|
Sexp::List(_) => {
|
|
return Err(Error::ParseIn("Expected atom got list".into(), expr));
|
|
}
|
|
}
|
|
}
|
|
|
|
Err(Error::Parse("Expected atom, got nothing".into()))
|
|
}
|
|
|
|
pub fn expect_string_atom(expr: Option<Sexp>) -> Result<String, Error> {
|
|
match expect_atom(expr) {
|
|
Ok(Atom::S(s)) => Ok(s),
|
|
Ok(atom) => Err(Error::ParseIn("Expected string atom".into(), Sexp::Atom(atom))),
|
|
Err(e) => Err(e),
|
|
}
|
|
}
|
|
|
|
// pub fn expect_int_atom(expr: Option<Sexp>) -> Result<i64, Error> {
|
|
// match expect_atom(expr) {
|
|
// Ok(Atom::I(v)) => Ok(v),
|
|
// Ok(atom) => Err(Error::ParseIn("Expected int atom".into(), Sexp::Atom(atom))),
|
|
// Err(e) => Err(e),
|
|
// }
|
|
// }
|
|
|