Skip to content

Commit 1d75cc4

Browse files
committed
feat: function calls
1 parent 713a2d4 commit 1d75cc4

File tree

3 files changed

+38
-3
lines changed

3 files changed

+38
-3
lines changed

packages/engine/src/parser/ast.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ pub enum Expression {
1515
Range(Box<(WithCursor<Literal>, WithCursor<Literal>, bool)>),
1616
ShellCommand(Box<ShellCommand>),
1717
Identifier(Box<Identifier>),
18-
FunctionCall(Box<(Identifier, Vec<Expression>)>),
18+
FunctionCall(Box<(Identifier, Vec<WithCursor<Expression>>)>),
1919
If(Box<(WithCursor<Expression>, WithCursor<Block>, Option<Else>)>),
2020
Match(Box<(Expression, Vec<(Expression, Expression)>)>),
2121
}

packages/engine/src/parser/mod.rs

Lines changed: 36 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -445,7 +445,42 @@ impl<'a> Parser<'a> {
445445
}
446446

447447
fn func_invoke(&mut self) -> ParserResult<Option<WithCursor<Expression>>> {
448-
self.literals() // TODO
448+
let_expr!(mut expr = self.literals()?);
449+
450+
if let Expression::Identifier(identifier) = &expr.value {
451+
if self.next_if_eq(&&LexerTokenKind::LParen).is_some() {
452+
let mut args = vec![];
453+
let mut end = self.cursor;
454+
455+
while let Some(token) = self.peek() {
456+
dbg!(token);
457+
458+
if self.next_if_eq(&&LexerTokenKind::RParen).is_some() {
459+
end = self.cursor;
460+
break;
461+
}
462+
463+
if self.next_if_eq(&&LexerTokenKind::Comma).is_some() {
464+
continue;
465+
}
466+
467+
let_expr!(arg = self.expression()?);
468+
469+
args.push(arg);
470+
}
471+
472+
expr = WithCursor::create_with(
473+
expr.start,
474+
end,
475+
Expression::FunctionCall(Box::from((
476+
identifier.to_string(),
477+
args
478+
)))
479+
)
480+
}
481+
}
482+
483+
Ok(Some(expr))
449484
}
450485

451486
fn literals(&mut self) -> ParserResult<Option<WithCursor<Expression>>> {

script.tsh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
var test = ((5 + 5) + 5)
1+
test(5, 10, test(5, 51))

0 commit comments

Comments
 (0)