Allowed parsing of variable defenitions to AST.

This commit is contained in:
Goren Barak 2023-11-25 18:15:48 -05:00
parent 86e5a5a5ca
commit c285d14f2e
3 changed files with 30 additions and 24 deletions

View file

@ -5,6 +5,8 @@ pub mod codegen;
use crate::codegen::fasm::*; use crate::codegen::fasm::*;
use crate::lex::tok::*; use crate::lex::tok::*;
use crate::parse::ast::*; use crate::parse::ast::*;
use crate::parse::parse::*;
use logos::Logos;
pub mod parse; pub mod parse;
@ -43,4 +45,8 @@ fn main() {
// println!("{}", fc); // println!("{}", fc);
let mut lexer = Token::lexer("let a = b;");
println!("{:?}", lex_str("let a = b;"));
println!("{:?}", parse_var_declaration(lexer));
} }

View file

@ -1 +1,2 @@
pub mod parse;
pub mod ast; pub mod ast;

View file

@ -2,33 +2,32 @@ use super::ast::*;
use crate::lex::tok::*; use crate::lex::tok::*;
use logos::Lexer; use logos::Lexer;
fn parse_var_declaration(tokens: Lexer) -> Option<Expr> { macro_rules! unwrap {
let tok = None; ($var:expr) => {
match tokens.next() { $var.next().unwrap().unwrap()
Let => { }
match tokens.next() { }
Identifier => {
pub fn parse_var_declaration(mut tokens: Lexer<Token>) -> Option<Expr> {
let mut tok = None;
if unwrap!(tokens) == Let {
if unwrap!(tokens) == Identifier {
let name = tokens.slice(); let name = tokens.slice();
println!("{:?}", name); println!("{:?}", name);
match tokens.next() { if unwrap!(tokens) == Equal {
Equal => { let temp_token = unwrap!(tokens);
match tokens.next() {
Number(n) => { if let Number(n) = temp_token {
let value = Value::Number(n); let value = Value::Number(n);
println!("{:?}", value); println!("{:?}", value);
return Expr::VarDefenition {name, value}; tok = Some(Expr::VarDefinition(VarDefinition {name, value}));
}, } else if temp_token == Identifier {
Identifier => {
let value = Value::Var(VarReference { name: tokens.slice() }); let value = Value::Var(VarReference { name: tokens.slice() });
return Expr::Vardefenition {name, value}; tok = Some(Expr::VarDefinition(VarDefinition {name, value}));
} }
} }
} }
} }
} tok
}
}
_ => None
}
} }