Parse multiple statements
- parse() now parses a Vec<Statement> that can represent basically a full program - Now multiple lines / statements are parsed together
This commit is contained in:
parent
41b7247ffd
commit
6bd58a4ecb
@ -4,7 +4,9 @@ use plang2_lib::*;
|
|||||||
fn main() {
|
fn main() {
|
||||||
|
|
||||||
let code = r#"
|
let code = r#"
|
||||||
a = 54 * sqrt(9);
|
a = 54 * 3;
|
||||||
|
b = 5;
|
||||||
|
print("{}", a + b);
|
||||||
"#;
|
"#;
|
||||||
|
|
||||||
let mut lexer = Lexer::new(code);
|
let mut lexer = Lexer::new(code);
|
||||||
|
|||||||
@ -13,7 +13,7 @@ pub struct Parser {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/*
|
/*
|
||||||
# GRAMMAR
|
# GRAMMAR
|
||||||
## expressions
|
## expressions
|
||||||
|
|
||||||
ident = IDENT
|
ident = IDENT
|
||||||
@ -48,8 +48,21 @@ impl Parser {
|
|||||||
self.tokens.advance()
|
self.tokens.advance()
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn parse(&mut self) -> PRes<Statement> {
|
pub fn parse(&mut self) -> PRes<Vec<Statement>> {
|
||||||
self.parse_statement()
|
let mut prog = Vec::new();
|
||||||
|
|
||||||
|
while let Some(tok) = self.curr() {
|
||||||
|
match tok {
|
||||||
|
// Skip empty statements like duplicate or redundant semicolons
|
||||||
|
Token::Semicolon => {
|
||||||
|
self.advance();
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
_ => prog.push(self.parse_statement()?),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
Ok(prog)
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn parse_statement(&mut self) -> PRes<Statement> {
|
pub fn parse_statement(&mut self) -> PRes<Statement> {
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user