diff --git a/examples/test.lua b/examples/test.lua index d8a5243..e1e42d4 100644 --- a/examples/test.lua +++ b/examples/test.lua @@ -1,7 +1,7 @@ function max (a, b) local m = a - -- if b > a then - -- m = b - -- end + if a then + local m = b + end return m end \ No newline at end of file diff --git a/src/ast.rs b/src/ast.rs index d8c238c..b420cc4 100644 --- a/src/ast.rs +++ b/src/ast.rs @@ -203,6 +203,7 @@ impl Parse for Block { pub enum Statement { Assignment(Option, Node, Node), Return(Node), + If(Node, Block), } impl Parse for Statement { @@ -211,6 +212,13 @@ impl Parse for Statement { if peeked == Some(Token::Keyword(Keyword::Return)) { stream.next(); Ok(Statement::Return(stream.parse()?)) + } else if peeked == Some(Token::Keyword(Keyword::If)) { + stream.next(); // Consume if + let cond = stream.parse()?; + stream.expect(Token::Keyword(Keyword::Then))?; + let then = stream.parse()?; + stream.expect(Token::Keyword(Keyword::End))?; + Ok(Self::If(cond, then)) } else if peeked == Some(Token::Keyword(Keyword::Local)) { stream.next(); let name = stream.parse()?; diff --git a/src/main.rs b/src/main.rs index ef76162..364fbea 100644 --- a/src/main.rs +++ b/src/main.rs @@ -18,6 +18,8 @@ fn main() { let tokens = tokenize(TEST).unwrap(); let mut stream = TokenStream::from(&file_path, &tokens); + dbg!(&tokens); + let mut functions = Vec::new(); while stream.peek() != Some(Token::Eof) { functions.push(stream.parse::().unwrap()); diff --git a/src/token_stream/lexer.rs b/src/token_stream/lexer.rs index dae9cc0..4dfc4cc 100644 --- a/src/token_stream/lexer.rs +++ b/src/token_stream/lexer.rs @@ -13,6 +13,8 @@ pub enum Keyword { End, Local, Return, + If, + Then, } impl Keyword { @@ -22,6 +24,8 @@ impl Keyword { "end" => Keyword::End, "local" => Keyword::Local, "return" => Keyword::Return, + "if" => Keyword::If, + "then" => Keyword::Then, _ => None?, }) } @@ -34,6 +38,8 @@ impl ToString for Keyword { Keyword::End => "end", Keyword::Local => "local", Keyword::Return => "return", + Keyword::If => "if", + Keyword::Then => "then", } .to_string() }