Redone original parser

= implemented original features
This commit is contained in:
Erin 2021-04-27 11:49:07 +02:00 committed by ondra05
parent d2160a3a4a
commit 39a8bf6a54
3 changed files with 13 additions and 15 deletions

View file

@ -1,2 +1,4 @@
var a = 3; functio test () {
var b = 4; var a = 3;
var b = 4;
}

View file

@ -12,10 +12,3 @@ pub enum ErrorKind {
EndOfTokenStream, EndOfTokenStream,
InvalidIdentifier, InvalidIdentifier,
} }
impl Error {
pub fn panic(&self, span: &str) {
println!("{:?} occured at {:?}", self.kind, self.position);
println!(" {}", &span);
}
}

View file

@ -74,7 +74,7 @@ impl<'a> Parser<'a> {
Some(Token::Semicolon) => None, Some(Token::Semicolon) => None,
Some(Token::Assignment) => { Some(Token::Assignment) => {
let value = self.lexer.next(); let value = self.lexer.next();
let value = self.parse_expr(value)?; // TODO: Shouldn't be limited to boolean (pattern match?) let value = self.parse_expr(value)?;
self.require(Token::Semicolon)?; self.require(Token::Semicolon)?;
Some(Box::new(value)) Some(Box::new(value))
} }
@ -95,12 +95,15 @@ impl<'a> Parser<'a> {
fn function_declaration(&mut self) -> Result<Expr, Error> { fn function_declaration(&mut self) -> Result<Expr, Error> {
let iden = self.require_iden()?; let iden = self.require_iden()?;
self.require(Token::LeftParenthesis)?; self.require(Token::LeftParenthesis)?;
// TODO: Arguments
self.require(Token::RightParenthesis)?; self.require(Token::RightParenthesis)?;
self.require(Token::LeftBrace)?; self.require(Token::LeftBrace)?;
let expr = self.lexer.next(); // Parse function body
let expr = self.parse_expr(expr); let mut body = Vec::new();
let body = vec![expr?]; while let Some(token) = self.lexer.next() {
if token == Token::RightBrace { break }
body.push(self.parse_expr(Some(token))?);
}
Ok(Expr::FunctionDeclaration { iden, body }) Ok(Expr::FunctionDeclaration { iden, body })
} }