Function arguments

This commit is contained in:
uan
2026-02-04 22:21:18 +01:00
parent ab5559d206
commit 6c9679ff57
2 changed files with 84 additions and 29 deletions

View File

@@ -8,6 +8,10 @@ mut:
out strings.Builder
}
fn mangle_var(name string, scope_depth int) string {
return 'v${name}${scope_depth.str()}'
}
fn (mut g Generator) get_c_type(typ string) string {
c_type := if typ == 'real' { 'double' } else { typ }
return c_type
@@ -17,7 +21,7 @@ fn (mut g Generator) gen_stmt(stmt Stmt) {
match stmt {
VarDecl {
c_type := g.get_c_type(stmt.type)
g.out.write_string('${c_type} v${stmt.name}${stmt.scope_depth.str()} = ')
g.out.write_string('${c_type} ${mangle_var(stmt.name, stmt.scope_depth)} = ')
g.gen_expr(stmt.value)
g.out.writeln(';')
}
@@ -39,9 +43,20 @@ fn (mut g Generator) gen_stmt(stmt Stmt) {
}
FuncDecl {
c_type := g.get_c_type(stmt.ret_type)
g.out.write_string('${c_type} ${stmt.name}() ')
g.out.write_string('${c_type} ${stmt.name}(')
for param in stmt.params {
g.gen_stmt(param)
if param != stmt.params[stmt.params.len-1]{
g.out.write_string(', ')
}
}
g.out.write_string(')')
g.gen_stmt(stmt.block)
}
Param {
c_type := g.get_c_type(stmt.type)
g.out.write_string('${c_type} ${mangle_var(stmt.name, 2)}')
}
}
}
@@ -57,7 +72,7 @@ fn (mut g Generator) gen_expr(expr Expr) {
g.out.write_string(expr.val.str())
}
Variable {
g.out.write_string('v${expr.name}${expr.scope_depth.str()}')
g.out.write_string(mangle_var(expr.name, expr.scope_depth))
}
UnaryExpr {
g.out.write_string('${expr.ident}${expr.op}')
@@ -91,7 +106,14 @@ fn (mut g Generator) gen_expr(expr Expr) {
g.out.write_string(')')
}
FnCall {
g.out.write_string('${expr.name}()')
g.out.write_string('${expr.name}(')
for arg in expr.args {
g.gen_expr(arg)
if arg != expr.args[expr.args.len-1]{
g.out.write_string(', ')
}
}
g.out.write_string(')')
}
else {panic("Unimplemented expression")}
}