start parse simple expr
This commit is contained in:
parent
26924a17ae
commit
ebbc4be8f6
2 changed files with 32 additions and 4 deletions
|
|
@ -1,11 +1,28 @@
|
|||
use super::types::*;
|
||||
use crate::ast;
|
||||
|
||||
fn parse_expr(tokens: &mut Vec<LocatedToken>) -> Result<ast::Expr, Error> {
|
||||
todo!()
|
||||
fn parse_simple_expr(tokens: &mut Vec<LocatedToken>) -> Result<ast::Expr, Error> {
|
||||
let start = tokens.last()?;
|
||||
match start {
|
||||
Token::OpenParen => {
|
||||
tokens.pop();
|
||||
let expr = parse_expr(tokens)?;
|
||||
if Some(p) = tokens.pop_if(|t| t.token == Token::CloseParen) {
|
||||
Ok(expr)
|
||||
} else {
|
||||
Err(Error::UnexpectedToken { expected: Token::CloseParen, got: start})
|
||||
}
|
||||
|
||||
}
|
||||
Token::Number(n) => {
|
||||
tokens.pop();
|
||||
Ok(ast::Expr::Value(ast::Value::Number(n)))
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
enum Error {
|
||||
UnexpectedToken{expected: Token, got: LocatedToken},
|
||||
UnexpectedToken(LocatedToken),
|
||||
UnexpectedEndOfInput,
|
||||
}
|
||||
|
|
@ -26,10 +43,10 @@ fn parse_definition(tokens: &mut Vec<LocatedToken>) -> Result<ast::Definition, E
|
|||
expr,
|
||||
})
|
||||
} else {
|
||||
Err(Error::UnexpectedToken(start))
|
||||
Err(Error::UnexpectedToken{expected: Token::Equals, got: start})
|
||||
}
|
||||
} else {
|
||||
Err(Error::UnexpectedToken(start))
|
||||
Err(Error::UnexpectedToken{expected: Token::Equals, got: start})
|
||||
}
|
||||
} else {
|
||||
Err(Error::UnexpectedEndOfInput)
|
||||
|
|
|
|||
|
|
@ -26,3 +26,14 @@ pub enum Token {
|
|||
String(String),
|
||||
Identifier(String),
|
||||
}
|
||||
|
||||
pub struct Tokens(Vec<LocatedToken>);
|
||||
|
||||
impl Tokens {
|
||||
fn peek(&mut self) -> Option<LocatedToken> {
|
||||
self.last()
|
||||
}
|
||||
fn next(&mut self) -> Option<LocatedToken> {
|
||||
self.pop()
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue