Skip to content

Commit

Permalink
parse call expr & builtin call expr
Browse files Browse the repository at this point in the history
  • Loading branch information
StunxFS committed Dec 26, 2024
1 parent 0b56d3c commit ea3f3ec
Show file tree
Hide file tree
Showing 4 changed files with 68 additions and 2 deletions.
16 changes: 16 additions & 0 deletions compiler/ast/Expr.v
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ pub type Expr = EmptyExpr
| StringLiteral
| LoopControl
| ReturnExpr
| BuiltinCallExpr
| CallExpr
| IfExpr
| MatchExpr
| AssignExpr
Expand Down Expand Up @@ -84,6 +86,20 @@ pub:
pos FilePos
}

pub struct BuiltinCallExpr {
pub:
name string
args []Expr
pos FilePos
}

pub struct CallExpr {
pub:
left Expr
args []Expr
pos FilePos
}

pub struct BlockExpr {
pub:
stmts []Stmt
Expand Down
40 changes: 39 additions & 1 deletion compiler/parser/expr.v
Original file line number Diff line number Diff line change
Expand Up @@ -187,7 +187,27 @@ fn (mut p Parser) parse_primary_expr() ast.Expr {
expr = p.parse_literal()
}
.ident {
if p.next_tok.kind == .char {
if p.next_tok.kind == .bang {
// builtin call expr
mut pos := p.tok.pos
name := p.parse_ident()
p.expect(.bang)
p.expect(.lparen)
mut args := []ast.Expr{}
for {
args << p.parse_expr()
if !p.accept(.comma) || p.should_abort() {
break
}
}
pos += p.tok.pos
p.expect(.rparen)
expr = ast.BuiltinCallExpr{
name: name
args: args
pos: pos
}
} else if p.next_tok.kind == .char {
if p.tok.lit == 'b' {
expr = p.parse_char_literal()
} else {
Expand Down Expand Up @@ -227,6 +247,24 @@ fn (mut p Parser) parse_primary_expr() ast.Expr {

for true {
match true {
p.accept(.lparen) {
// call expr
mut pos := p.prev_tok.pos
mut args := []ast.Expr{}
for {
args << p.parse_expr()
if !p.accept(.comma) || p.should_abort() {
break
}
}
pos += p.tok.pos
p.expect(.rparen)
expr = ast.CallExpr{
left: expr
args: args
pos: pos
}
}
p.tok.kind.is_assign() {
op := match p.tok.kind {
.assign { ast.AssignOp.assign }
Expand Down
12 changes: 12 additions & 0 deletions tests/valid/call_expr.ri
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
fn main() {
let x = abc();
assert!(x == 1124);
}

fn add(a: int, b: int) int {
return a + b;
}

fn abc() int {
return add(100, 1024);
}
2 changes: 1 addition & 1 deletion tests/valid/return_expr.ri
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
fn main() {
let x = abc;
let x = abc();
}

fn abc() int {
Expand Down

0 comments on commit ea3f3ec

Please sign in to comment.