function calls

This commit is contained in:
uan
2026-02-04 21:39:14 +01:00
parent ce51292f7c
commit ab5559d206
5 changed files with 35 additions and 10 deletions

View File

@@ -91,7 +91,7 @@ fn (mut s SymbolTable) is_in_global_scope() bool {
// ------------------------------------------- Expressions
type Expr = VoidExpr | UnaryExpr | BinaryExpr | IntegerLiteral | RealLiteral | BoolLiteral | Variable | TypeExpr | Function | TypeCast | ParenExpr | PrintExpr
type Expr = VoidExpr | UnaryExpr | BinaryExpr | IntegerLiteral | RealLiteral | BoolLiteral | Variable | TypeExpr | Function | TypeCast | ParenExpr | PrintExpr | FnCall
struct VoidExpr {}
@@ -120,6 +120,7 @@ struct BoolLiteral {
struct Variable {
name string
scope_depth int
}
struct TypeExpr {
@@ -144,6 +145,10 @@ struct PrintExpr {
type string
}
struct FnCall {
name string
}
// ------------------------------------------- Statements
type Stmt = VarDecl | ExprStmt | ReturnStmt | Block | FuncDecl
@@ -152,6 +157,7 @@ struct VarDecl {
name string
type string
value Expr
scope_depth int
}
struct FuncDecl {
@@ -253,10 +259,16 @@ fn (mut p Parser) parse_expr(prec Precedence) Expr {
}
fn (mut p Parser) parse_ident(ident string) Expr {
return match p.peek().type {
.increment, .decrement {UnaryExpr {ident: ident, op: p.next().text}}
else {Variable{ident}}
expr := match p.peek().type {
.increment, .decrement {Expr(UnaryExpr {ident: ident, op: p.next().text})}
.lparen {
p.expect(.lparen)
p.expect(.rparen)
FnCall{name: ident}
}
else {Variable{ident, p.symbols.variable_scopes.len}}
}
return expr
}
fn (mut p Parser) parse_print() PrintExpr {
@@ -333,6 +345,10 @@ fn (mut p Parser) get_expr_type(expr Expr) string {
return info.type
}
TypeCast {expr.type}
FnCall {
fninfo := p.symbols.lookup_func(expr.name) or {parse_error("Tried to call undefined function ${expr.name}")}
fninfo.type
}
else {"Tried getting type of unexpected Expr"}
}
@@ -407,6 +423,7 @@ fn (mut p Parser) parse_var_decl() VarDecl {
name: name_tok.text
value: val
type: type_tok.text
scope_depth: p.symbols.variable_scopes.len
}
}
@@ -463,7 +480,7 @@ fn (mut p Parser) parse_return_stmt() ReturnStmt {
.integer {IntegerLiteral{token.text.int()}}
.real {RealLiteral{token.text.f32()}}
.boolean {BoolLiteral{token.text == 'true'}}
.identifier {Variable{token.text}}
.identifier {Variable{token.text, p.symbols.variable_scopes.len}}
.semicolon {VoidExpr{}}
else {parse_error("Unexpected Token")}
}