Made parse_fun_call() work.

This commit is contained in:
Goren Barak 2023-11-26 10:17:09 -05:00
parent 1733535322
commit 4d4ab24382
2 changed files with 31 additions and 10 deletions

View file

@ -45,8 +45,9 @@ fn main() {
// println!("{}", fc);
let parsed = "hello(hi)";
let mut lexer = Token::lexer("let a = b;");
println!("{:?}", lex_str("let a = b;"));
println!("{:?}", parse_var_declaration(lexer));
let mut lexer = Token::lexer(parsed);
println!("{:?}", lex_str(parsed));
println!("{:?}", parse_fun_call(lexer));
}

View file

@ -17,7 +17,6 @@ pub fn parse_var_declaration(mut tokens: Lexer<Token>) -> Option<Expr> {
println!("{:?}", name);
if unwrap!(tokens) == Equal {
let temp_token = unwrap!(tokens);
if let Number(n) = temp_token {
let value = Value::Number(n);
println!("{:?}", value);
@ -32,13 +31,20 @@ pub fn parse_var_declaration(mut tokens: Lexer<Token>) -> Option<Expr> {
tok
}
pub fn parse_value(token: (Token, &str)) -> Option<Value<'static>> {
let mut value = None;
pub fn parse_value<'a>(token: (Option<Result<Token, ()>>, &'a str)) -> Option<Value<'a>> {
if let Some(Ok(tt)) = token.0 {
let mut value = None;
if let Number(n) = token.0 {
value = Some(Value::Number(n));
} else if token.0 == Identifier {
value = Some(Value::Var(VarReference { name: token.1 }));
if let Number(n) = tt {
value = Some(Value::Number(n));
} else if tt == Identifier {
value = Some(Value::Var(VarReference { name: token.1 }));
}
value
} else {
return None;
}
}
@ -46,4 +52,18 @@ pub fn parse_fun_call(mut tokens: Lexer<Token>) -> Option<Expr> {
// Is it an Ident? → Is it a LeftParen? → Is it a value (I should really make a function to parse that) or is it a RightParen?
// ↓ ↓
// If it's a value, push that to `params`. Otherwise, params will just be a `Vec::new()`.
let mut tok = None;
if unwrap!(tokens) == Identifier {
let name = tokens.slice();
if unwrap!(tokens) == LeftParen {
let mut params = Vec::new();
while let Some(value) = parse_value((tokens.next(), tokens.slice())) {
params.push(value);
}
tok = Some(Expr::FunCall(FunCall {name, params: params.clone()}));
}
}
tok
}