From 94ac0863bc38cdc0576a3b0fe325ad9dd7bfe6fa Mon Sep 17 00:00:00 2001 From: Tristan Smith Date: Fri, 20 Sep 2024 01:40:17 -0400 Subject: [PATCH] expanded parser --- examples-output.txt | 55 ++++++++++++++++++++++++++++++++++++++++++++ examples.fddl | 18 +++++++++++++++ hello-world.fddl | 3 --- hello.fddl | 5 ---- src/parser/parser.rs | 20 +++++++++++++++- 5 files changed, 92 insertions(+), 9 deletions(-) create mode 100644 examples-output.txt create mode 100644 examples.fddl delete mode 100644 hello-world.fddl delete mode 100644 hello.fddl diff --git a/examples-output.txt b/examples-output.txt new file mode 100644 index 0000000..4be1e5f --- /dev/null +++ b/examples-output.txt @@ -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 diff --git a/examples.fddl b/examples.fddl new file mode 100644 index 0000000..07a94c4 --- /dev/null +++ b/examples.fddl @@ -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; + } +} diff --git a/hello-world.fddl b/hello-world.fddl deleted file mode 100644 index c4431d2..0000000 --- a/hello-world.fddl +++ /dev/null @@ -1,3 +0,0 @@ -func main() { - print("hello, world in fddl"); -} diff --git a/hello.fddl b/hello.fddl deleted file mode 100644 index 37b2f46..0000000 --- a/hello.fddl +++ /dev/null @@ -1,5 +0,0 @@ -!test - -func main() { - print("Hello World"); -} diff --git a/src/parser/parser.rs b/src/parser/parser.rs index e2baa9e..fe33702 100644 --- a/src/parser/parser.rs +++ b/src/parser/parser.rs @@ -8,10 +8,28 @@ pub struct Parser { impl Parser { fn parse_statement(&mut self) -> Option { 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 { + let value = self.parse.expression(); + if self.match_token(Token::Semicolon) { + Some(Statement::PrintStatement(value?)) + } else { + None + } + } pub fn new(tokens: Vec) -> Self { Parser {