diff --git a/examples/hello_world.sb b/examples/hello_world.sb index e6cc36c..486984c 100644 --- a/examples/hello_world.sb +++ b/examples/hello_world.sb @@ -1,3 +1,3 @@ -fn main() { - return 2 * 5 / 3; +fn main(n) { + n * 2; } \ No newline at end of file diff --git a/examples_out/out.js b/examples_out/out.js index 41b2667..25e652e 100644 --- a/examples_out/out.js +++ b/examples_out/out.js @@ -1,4 +1,3 @@ -function main() { -return 2 * 5 / 3 -} +function main(n) { +n * 2} console.log(main()) \ No newline at end of file diff --git a/src/parser/mod.rs b/src/parser/mod.rs index 8d6fa77..1670119 100644 --- a/src/parser/mod.rs +++ b/src/parser/mod.rs @@ -202,7 +202,7 @@ impl Parser { TokenKind::Identifier(_) => { let ident = self.match_identifier()?; if let Ok(_) = self.peek_token(TokenKind::BraceOpen) { - let state = self.parse_function_call()?; + let state = self.parse_function_call(Some(ident))?; Ok(Statement::Exp(state)) } else { let state = Statement::Exp(Expression::Variable(ident.into())); @@ -218,15 +218,16 @@ impl Parser { /// Parses a function call from tokens. /// The name of the function needs to be passed here, because we have already passed it with our cursor. - fn parse_function_call(&mut self) -> Result { - let ident_token = self.prev().ok_or_else(|| "Expected function identifier")?; - let name = match &ident_token.kind { - TokenKind::Identifier(name) => name, - _ => { - panic!(self.make_error(TokenKind::Identifier("Function name".into()), ident_token)) + /// If no function name is provided, the next token will be fetched + fn parse_function_call(&mut self, func_name: Option) -> Result { + let name = match func_name { + Some(name) => name, + None => { + self.next() + .ok_or_else(|| "Expected function identifier")? + .raw } - } - .to_string(); + }; self.match_token(TokenKind::BraceOpen)?; @@ -284,7 +285,7 @@ impl Parser { let next = self.peek().ok_or_else(|| "Token expected")?; let state = match &next.kind { TokenKind::BraceOpen => { - let func_call = self.parse_function_call()?; + let func_call = self.parse_function_call(Some(val))?; match BinOp::try_from(self.peek().ok_or("Could not peek token")?.kind) { Ok(_) => self.parse_bin_op(Some(func_call))?, Err(_) => func_call, @@ -292,7 +293,7 @@ impl Parser { } _ => match BinOp::try_from(self.peek().ok_or("Could not peek token")?.kind) { Ok(_) => self.parse_bin_op(None)?, - Err(_) => Expression::Str(val), + Err(_) => Expression::Variable(val), }, }; Ok(state)