expanded parser

This commit is contained in:
Tristan Smith 2024-09-20 01:40:17 -04:00
parent c08f292bd8
commit 94ac0863bc
5 changed files with 92 additions and 9 deletions

55
examples-output.txt Normal file
View file

@ -0,0 +1,55 @@
fddl examples.fddl
Func
Identifier("main")
LeftParen
RightParen
LeftBrace
Comment(" print statement")
Print
LeftParen
StringLiteral("hello, world in fddl")
RightParen
Semicolon
Comment(" variable declaration")
Let
Identifier("y")
Equal
Number(5.0)
Semicolon
Comment(" if statement")
If
LeftParen
Identifier("x")
Greater
Number(10.0)
RightParen
LeftBrace
Print
LeftParen
StringLiteral("x is greater than 10")
RightParen
Semicolon
RightBrace
Comment(" while loop")
While
LeftParen
Identifier("x")
Less
Number(100.0)
RightParen
LeftBrace
Print
LeftParen
Identifier("x")
RightParen
Semicolon
Let
Identifier("x")
Equal
Identifier("x")
Plus
Number(1.0)
Semicolon
RightBrace
RightBrace
EOF

18
examples.fddl Normal file
View file

@ -0,0 +1,18 @@
func main() {
// print statement
print("hello, world in fddl");
// variable declaration
let y = 5;
// if statement
if (x > 10) {
print("x is greater than 10");
}
// while loop
while (x < 100) {
print(x);
let x = x + 1;
}
}

View file

@ -1,3 +0,0 @@
func main() {
print("hello, world in fddl");
}

View file

@ -1,5 +0,0 @@
!test
func main() {
print("Hello World");
}

View file

@ -9,9 +9,27 @@ impl Parser {
fn parse_statement(&mut self) -> Option<Statement> { fn parse_statement(&mut self) -> Option<Statement> {
if self.match_token(Token::Print) { if self.match_token(Token::Print) {
self.parse_print_statement() self.parse_print_statement()
} else if self.match_token(Token::Let) {
self.parse_variable_declaration()
} else if self.match_token(Token::If) {
self.parse_if_statement()
} else if self.match_token(Token::While) {
self.parse_while_statement()
} else if self.match_token(Token::For) {
self.parse_for_statement()
} else {
self.parse_expression_statement()
} }
} }
fn parse_print_statement(&mut self) -> Option<Statement> {
let value = self.parse.expression();
if self.match_token(Token::Semicolon) {
Some(Statement::PrintStatement(value?))
} else {
None
}
}
pub fn new(tokens: Vec<Token>) -> Self { pub fn new(tokens: Vec<Token>) -> Self {
Parser { Parser {