Croissant Runtime
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.
 
 
crsn/crsn/src/asm/parse/sexp_expect.rs

61 lines
1.8 KiB

use sexp::{Atom, Sexp};
use crate::asm::error::CrsnError;
pub fn expect_list(expr: Option<Sexp>, allow_empty: bool) -> Result<Vec<Sexp>, CrsnError> {
if let Some(expr) = expr {
match &expr {
Sexp::Atom(_) => {
return Err(CrsnError::ParseIn("Expected a list".into(), expr));
}
Sexp::List(list) => {
if !allow_empty && list.is_empty() {
return Err(CrsnError::ParseIn("Routine: Empty list".into(), expr));
}
if let Sexp::List(list) = expr {
return Ok(list);
} else {
unreachable!();
}
}
}
}
Err(CrsnError::Parse("Expected a list, got nothing".into()))
}
pub fn expect_atom(expr: Option<Sexp>) -> Result<Atom, CrsnError> {
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(CrsnError::ParseIn("Expected atom got list".into(), expr));
}
}
}
Err(CrsnError::Parse("Expected atom, got nothing".into()))
}
pub fn expect_string_atom(expr: Option<Sexp>) -> Result<String, CrsnError> {
match expect_atom(expr) {
Ok(Atom::S(s)) => Ok(s),
Ok(atom) => Err(CrsnError::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),
// }
// }