From 5ae010fef48cc2bf83a0d366d2a1cfa74ecce278 Mon Sep 17 00:00:00 2001 From: JJ Date: Thu, 13 Apr 2023 00:20:06 -0700 Subject: major cleanups: extend Type, refactor Term, and switch to String errs --- src/parser.rs | 28 +++++++++++----------------- 1 file changed, 11 insertions(+), 17 deletions(-) (limited to 'src/parser.rs') diff --git a/src/parser.rs b/src/parser.rs index 6e6a262..8eac52d 100644 --- a/src/parser.rs +++ b/src/parser.rs @@ -6,7 +6,7 @@ pub fn parse(input: &str) -> Expression { Ok(expr) => return expr, Err(e) => println!("invalid expression! {:?}", e) } - return Expression::Constant { term: Term { val: 0, kind: Type::Empty } }; + return Expression::Constant { term: Term::Unit() }; } /// Parses a Nim-like language into an AST. @@ -35,15 +35,12 @@ pub fn parse_lambda(input: &str) -> Result Expression = p:"-"? c:['0'..='9']+ { - let value = c.iter().collect::().parse::().unwrap(); + let value = c.iter().collect::().parse::().unwrap(); Expression::Constant { - term: Term { - val: if let Some(_) = p { - value.wrapping_neg() - } else { - value - }, - kind: Type::Empty + term: if let Some(_) = p { + Term::Integer(-1 * isize::try_from(value).unwrap()) + } else { + Term::Natural(value) } } } @@ -236,15 +233,12 @@ pub fn parse_lang(input: &str) -> Result, peg::error::ParseError } // constants rule cons() -> Expression = p:"-"? c:['0'..='9']+ { - let value = c.iter().collect::().parse::().unwrap(); + let value = c.iter().collect::().parse::().unwrap(); Expression::Constant { - term: Term { - val: if let Some(_) = p { - value.wrapping_neg() - } else { - value - }, - kind: Type::Empty // fixme + term: if let Some(_) = p { + Term::Integer(-1 * isize::try_from(value).unwrap()) + } else { + Term::Natural(value) } } } -- cgit v1.2.3-70-g09d2