forked from AbleScript/ablescript
Redone original parser
= implemented original features
This commit is contained in:
parent
138b9efadc
commit
2a0b2952d4
|
@ -1,2 +1,4 @@
|
||||||
var a = 3;
|
functio test () {
|
||||||
var b = 4;
|
var a = 3;
|
||||||
|
var b = 4;
|
||||||
|
}
|
|
@ -11,11 +11,4 @@ pub enum ErrorKind {
|
||||||
SyntaxError(String),
|
SyntaxError(String),
|
||||||
EndOfTokenStream,
|
EndOfTokenStream,
|
||||||
InvalidIdentifier,
|
InvalidIdentifier,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Error {
|
|
||||||
pub fn panic(&self, span: &str) {
|
|
||||||
println!("{:?} occured at {:?}", self.kind, self.position);
|
|
||||||
println!(" {}", &span);
|
|
||||||
}
|
|
||||||
}
|
|
|
@ -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 })
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue