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 super::types::*;
|
||||||
use crate::ast;
|
use crate::ast;
|
||||||
|
|
||||||
fn parse_expr(tokens: &mut Vec<LocatedToken>) -> Result<ast::Expr, Error> {
|
fn parse_simple_expr(tokens: &mut Vec<LocatedToken>) -> Result<ast::Expr, Error> {
|
||||||
todo!()
|
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 {
|
enum Error {
|
||||||
|
UnexpectedToken{expected: Token, got: LocatedToken},
|
||||||
UnexpectedToken(LocatedToken),
|
UnexpectedToken(LocatedToken),
|
||||||
UnexpectedEndOfInput,
|
UnexpectedEndOfInput,
|
||||||
}
|
}
|
||||||
|
|
@ -26,10 +43,10 @@ fn parse_definition(tokens: &mut Vec<LocatedToken>) -> Result<ast::Definition, E
|
||||||
expr,
|
expr,
|
||||||
})
|
})
|
||||||
} else {
|
} else {
|
||||||
Err(Error::UnexpectedToken(start))
|
Err(Error::UnexpectedToken{expected: Token::Equals, got: start})
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
Err(Error::UnexpectedToken(start))
|
Err(Error::UnexpectedToken{expected: Token::Equals, got: start})
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
Err(Error::UnexpectedEndOfInput)
|
Err(Error::UnexpectedEndOfInput)
|
||||||
|
|
|
||||||
|
|
@ -26,3 +26,14 @@ pub enum Token {
|
||||||
String(String),
|
String(String),
|
||||||
Identifier(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