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::lex::tok::*;
use crate::parse::ast::*;
use crate::parse::parse::*;
use logos::Logos;
pub mod parse;
@ -43,4 +45,8 @@ fn main() {
// 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;

View file

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