diff options
author | JJ | 2023-04-13 07:20:06 +0000 |
---|---|---|
committer | JJ | 2023-04-13 07:21:28 +0000 |
commit | 5ae010fef48cc2bf83a0d366d2a1cfa74ecce278 (patch) | |
tree | 2241dcec8d38d16e2314e94b4dec0ed61e20d922 /src/parser.rs | |
parent | 188631f3bb263700c34d578af5968ab80e699485 (diff) |
major cleanups: extend Type, refactor Term, and switch to String errs
Diffstat (limited to 'src/parser.rs')
-rw-r--r-- | src/parser.rs | 28 |
1 files changed, 11 insertions, 17 deletions
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, peg::error::ParseError<pe } rule cons() -> Expression = p:"-"? c:['0'..='9']+ { - let value = c.iter().collect::<String>().parse::<Value>().unwrap(); + let value = c.iter().collect::<String>().parse::<usize>().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<Vec<Expression>, peg::error::ParseError } // constants rule cons() -> Expression = p:"-"? c:['0'..='9']+ { - let value = c.iter().collect::<String>().parse::<Value>().unwrap(); + let value = c.iter().collect::<String>().parse::<usize>().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) } } } |