Browse Source

Refactor function call parsing

github-actions
Garrit Franke 3 years ago
parent
commit
7b081ab974
  1. 4
      examples/hello_world.sb
  2. 5
      examples_out/out.js
  3. 23
      src/parser/mod.rs

4
examples/hello_world.sb

@ -1,3 +1,3 @@
fn main() { fn main(n) {
return 2 * 5 / 3; n * 2;
} }

5
examples_out/out.js

@ -1,4 +1,3 @@
function main() { function main(n) {
return 2 * 5 / 3 n * 2}
}
console.log(main()) console.log(main())

23
src/parser/mod.rs

@ -202,7 +202,7 @@ impl Parser {
TokenKind::Identifier(_) => { TokenKind::Identifier(_) => {
let ident = self.match_identifier()?; let ident = self.match_identifier()?;
if let Ok(_) = self.peek_token(TokenKind::BraceOpen) { 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)) Ok(Statement::Exp(state))
} else { } else {
let state = Statement::Exp(Expression::Variable(ident.into())); let state = Statement::Exp(Expression::Variable(ident.into()));
@ -218,15 +218,16 @@ impl Parser {
/// Parses a function call from tokens. /// 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. /// 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<Expression, String> { /// If no function name is provided, the next token will be fetched
let ident_token = self.prev().ok_or_else(|| "Expected function identifier")?; fn parse_function_call(&mut self, func_name: Option<String>) -> Result<Expression, String> {
let name = match &ident_token.kind { let name = match func_name {
TokenKind::Identifier(name) => name, Some(name) => name,
_ => { None => {
panic!(self.make_error(TokenKind::Identifier("Function name".into()), ident_token)) self.next()
.ok_or_else(|| "Expected function identifier")?
.raw
} }
} };
.to_string();
self.match_token(TokenKind::BraceOpen)?; self.match_token(TokenKind::BraceOpen)?;
@ -284,7 +285,7 @@ impl Parser {
let next = self.peek().ok_or_else(|| "Token expected")?; let next = self.peek().ok_or_else(|| "Token expected")?;
let state = match &next.kind { let state = match &next.kind {
TokenKind::BraceOpen => { 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) { match BinOp::try_from(self.peek().ok_or("Could not peek token")?.kind) {
Ok(_) => self.parse_bin_op(Some(func_call))?, Ok(_) => self.parse_bin_op(Some(func_call))?,
Err(_) => func_call, Err(_) => func_call,
@ -292,7 +293,7 @@ impl Parser {
} }
_ => match BinOp::try_from(self.peek().ok_or("Could not peek token")?.kind) { _ => match BinOp::try_from(self.peek().ok_or("Could not peek token")?.kind) {
Ok(_) => self.parse_bin_op(None)?, Ok(_) => self.parse_bin_op(None)?,
Err(_) => Expression::Str(val), Err(_) => Expression::Variable(val),
}, },
}; };
Ok(state) Ok(state)

Loading…
Cancel
Save