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