Clean up ast structure

This commit is contained in:
elkowar 2021-07-31 13:59:02 +02:00
parent 24c9fee204
commit dd482987cd
No known key found for this signature in database
GPG key ID: E321AD71B1D1F27F
8 changed files with 9 additions and 17 deletions

View file

@ -17,6 +17,7 @@ pub enum AstType {
Array, Array,
Keyword, Keyword,
Symbol, Symbol,
// TODO this does no longer correspond to an actual literal ast type as that's replaced with SimplExpr
Literal, Literal,
SimplExpr, SimplExpr,
Comment, Comment,
@ -36,7 +37,6 @@ pub enum Ast {
Array(Span, Vec<Ast>), Array(Span, Vec<Ast>),
Keyword(Span, String), Keyword(Span, String),
Symbol(Span, String), Symbol(Span, String),
Literal(Span, DynVal),
SimplExpr(Span, SimplExpr), SimplExpr(Span, SimplExpr),
Comment(Span), Comment(Span),
} }
@ -60,8 +60,6 @@ macro_rules! as_func {
} }
impl Ast { impl Ast {
as_func!(AstType::Literal, as_literal as_literal_ref<DynVal> = Ast::Literal(_, x) => x);
as_func!(AstType::Symbol, as_symbol as_symbol_ref<String> = Ast::Symbol(_, x) => x); as_func!(AstType::Symbol, as_symbol as_symbol_ref<String> = Ast::Symbol(_, x) => x);
as_func!(AstType::Keyword, as_keyword as_keyword_ref<String> = Ast::Keyword(_, x) => x); as_func!(AstType::Keyword, as_keyword as_keyword_ref<String> = Ast::Keyword(_, x) => x);
@ -74,7 +72,6 @@ impl Ast {
Ast::Array(..) => AstType::Array, Ast::Array(..) => AstType::Array,
Ast::Keyword(..) => AstType::Keyword, Ast::Keyword(..) => AstType::Keyword,
Ast::Symbol(..) => AstType::Symbol, Ast::Symbol(..) => AstType::Symbol,
Ast::Literal(..) => AstType::Literal,
Ast::SimplExpr(..) => AstType::SimplExpr, Ast::SimplExpr(..) => AstType::SimplExpr,
Ast::Comment(_) => AstType::Comment, Ast::Comment(_) => AstType::Comment,
} }
@ -86,7 +83,6 @@ impl Ast {
Ast::Array(span, _) => *span, Ast::Array(span, _) => *span,
Ast::Keyword(span, _) => *span, Ast::Keyword(span, _) => *span,
Ast::Symbol(span, _) => *span, Ast::Symbol(span, _) => *span,
Ast::Literal(span, _) => *span,
Ast::SimplExpr(span, _) => *span, Ast::SimplExpr(span, _) => *span,
Ast::Comment(span) => *span, Ast::Comment(span) => *span,
} }
@ -97,7 +93,6 @@ impl Ast {
// TODO do I do this? // TODO do I do this?
// Ast::Array(span, elements) => todo!() // Ast::Array(span, elements) => todo!()
Ast::Symbol(span, x) => Ok(SimplExpr::VarRef(span, VarName(x))), Ast::Symbol(span, x) => Ok(SimplExpr::VarRef(span, VarName(x))),
Ast::Literal(span, x) => Ok(SimplExpr::Literal(span, x)),
Ast::SimplExpr(span, x) => Ok(x), Ast::SimplExpr(span, x) => Ok(x),
_ => Err(AstError::WrongExprType(self.span(), AstType::IntoPrimitive, self.expr_type())), _ => Err(AstError::WrongExprType(self.span(), AstType::IntoPrimitive, self.expr_type())),
} }
@ -118,7 +113,7 @@ impl std::fmt::Display for Ast {
Array(_, x) => write!(f, "({})", x.iter().map(|e| format!("{}", e)).join(" ")), Array(_, x) => write!(f, "({})", x.iter().map(|e| format!("{}", e)).join(" ")),
Keyword(_, x) => write!(f, ":{}", x), Keyword(_, x) => write!(f, ":{}", x),
Symbol(_, x) => write!(f, "{}", x), Symbol(_, x) => write!(f, "{}", x),
Literal(_, x) => write!(f, "\"{}\"", x), SimplExpr(_, simplexpr::SimplExpr::Literal(_, value)) => write!(f, "\"{}\"", value),
SimplExpr(_, x) => write!(f, "{{{}}}", x), SimplExpr(_, x) => write!(f, "{{{}}}", x),
Comment(_) => write!(f, ""), Comment(_) => write!(f, ""),
} }

View file

@ -39,19 +39,17 @@ macro_rules! return_or_put_back {
impl<I: Iterator<Item = Ast>> AstIterator<I> { impl<I: Iterator<Item = Ast>> AstIterator<I> {
return_or_put_back!(expect_symbol, AstType::Symbol, (Span, String) = Ast::Symbol(span, x) => (span, x)); return_or_put_back!(expect_symbol, AstType::Symbol, (Span, String) = Ast::Symbol(span, x) => (span, x));
// return_or_put_back!(expect_literal, AstType::Literal, (Span, DynVal) = Ast::Literal(span, x) => (span, x));
return_or_put_back!(expect_list, AstType::List, (Span, Vec<Ast>) = Ast::List(span, x) => (span, x)); return_or_put_back!(expect_list, AstType::List, (Span, Vec<Ast>) = Ast::List(span, x) => (span, x));
return_or_put_back!(expect_array, AstType::Array, (Span, Vec<Ast>) = Ast::Array(span, x) => (span, x)); return_or_put_back!(expect_array, AstType::Array, (Span, Vec<Ast>) = Ast::Array(span, x) => (span, x));
pub fn expect_literal(&mut self) -> AstResult<(Span, DynVal)> { pub fn expect_literal(&mut self) -> AstResult<(Span, DynVal)> {
// TODO add some others
match self.expect_any()? { match self.expect_any()? {
// Ast::List(_, _) => todo!(), // Ast::List(_, _) => todo!(),
// Ast::Array(_, _) => todo!(), // Ast::Array(_, _) => todo!(),
// Ast::Keyword(_, _) => todo!(), // Ast::Keyword(_, _) => todo!(),
// Ast::Symbol(_, _) => todo!(), // Ast::Symbol(_, _) => todo!(),
// Ast::Literal(_, _) => todo!(),
Ast::SimplExpr(span, expr) => Ok((span, dbg!(expr.eval_no_vars()).map_err(|e| AstError::SimplExpr(e.into()))?)), Ast::SimplExpr(span, expr) => Ok((span, dbg!(expr.eval_no_vars()).map_err(|e| AstError::SimplExpr(e.into()))?)),
other => { other => {
let span = other.span(); let span = other.span();

View file

@ -24,7 +24,7 @@ impl FromAst for Ast {
impl FromAst for String { impl FromAst for String {
fn from_ast(e: Ast) -> AstResult<Self> { fn from_ast(e: Ast) -> AstResult<Self> {
Ok(e.as_literal()?.as_string().unwrap()) Ok(e.as_simplexpr()?.eval_no_vars().map_err(simplexpr::error::Error::Eval)?.to_string())
} }
} }
@ -51,7 +51,6 @@ impl FromAst for SimplExpr {
fn from_ast(e: Ast) -> AstResult<Self> { fn from_ast(e: Ast) -> AstResult<Self> {
match e { match e {
Ast::Symbol(span, x) => Ok(SimplExpr::VarRef(span.into(), VarName(x))), Ast::Symbol(span, x) => Ok(SimplExpr::VarRef(span.into(), VarName(x))),
Ast::Literal(span, x) => Ok(SimplExpr::Literal(span.into(), x)),
Ast::SimplExpr(span, x) => Ok(x), Ast::SimplExpr(span, x) => Ok(x),
_ => Err(AstError::NotAValue(e.span(), e.expr_type())), _ => Err(AstError::NotAValue(e.span(), e.expr_type())),
} }

View file

@ -36,7 +36,7 @@ pub Ast: Ast = {
<l:@L> <expr:SimplExpr> <r:@R> => Ast::SimplExpr(Span(l, r, file_id), expr), <l:@L> <expr:SimplExpr> <r:@R> => Ast::SimplExpr(Span(l, r, file_id), expr),
<x:Keyword> => x, <x:Keyword> => x,
<x:Symbol> => x, <x:Symbol> => x,
<l:@L> <x:Literal> <r:@R> => Ast::Literal(Span(l, r, file_id), x.into()), <l:@L> <x:Literal> <r:@R> => Ast::SimplExpr(Span(l, r, file_id), SimplExpr::literal(Span(l, r, file_id), x.into())),
<l:@L> "comment" <r:@R> => Ast::Comment(Span(l, r, file_id)), <l:@L> "comment" <r:@R> => Ast::Comment(Span(l, r, file_id)),
}; };

View file

@ -4,5 +4,5 @@ expression: "p.parse(0, Lexer::new(0, r#\"(test \"hi\")\"#.to_string()))"
--- ---
Ok( Ok(
(test {"hi"}), (test "hi"),
) )

View file

@ -4,5 +4,5 @@ expression: "p.parse(0, Lexer::new(0, r#\"(test \"h\\\"i\")\"#.to_string()))"
--- ---
Ok( Ok(
(test {"h"i"}), (test "h"i"),
) )

View file

@ -4,5 +4,5 @@ expression: "p.parse(0, Lexer::new(0, r#\"(test \" hi \")\"#.to_string()))"
--- ---
Ok( Ok(
(test {" hi "}), (test " hi "),
) )

View file

@ -4,5 +4,5 @@ expression: "p.parse(0, Lexer::new(0, \"\\\"h\\\\\\\"i\\\"\".to_string()))"
--- ---
Ok( Ok(
{"h"i"}, "h"i",
) )