-
This commit is contained in:
15
generator.v
15
generator.v
@@ -8,8 +8,8 @@ mut:
|
||||
out strings.Builder
|
||||
}
|
||||
|
||||
fn mangle_var(name string, scope_depth int) string {
|
||||
return 'v${name}${scope_depth.str()}'
|
||||
fn mangle_var(name string) string {
|
||||
return 'v${name}'
|
||||
}
|
||||
|
||||
fn (mut g Generator) get_c_type(typ string) string {
|
||||
@@ -21,7 +21,10 @@ 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} ${mangle_var(stmt.name, stmt.scope_depth)} = ')
|
||||
if stmt.const {
|
||||
g.out.write_string('const ')
|
||||
}
|
||||
g.out.write_string('${c_type} ${mangle_var(stmt.name)} = ')
|
||||
g.gen_expr(stmt.value)
|
||||
g.out.writeln(';')
|
||||
}
|
||||
@@ -55,7 +58,7 @@ fn (mut g Generator) gen_stmt(stmt Stmt) {
|
||||
}
|
||||
Param {
|
||||
c_type := g.get_c_type(stmt.type)
|
||||
g.out.write_string('${c_type} ${mangle_var(stmt.name, 2)}')
|
||||
g.out.write_string('${c_type} ${mangle_var(stmt.name)}')
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -72,7 +75,7 @@ fn (mut g Generator) gen_expr(expr Expr) {
|
||||
g.out.write_string(expr.val.str())
|
||||
}
|
||||
Variable {
|
||||
g.out.write_string(mangle_var(expr.name, expr.scope_depth))
|
||||
g.out.write_string(mangle_var(expr.name))
|
||||
}
|
||||
UnaryExpr {
|
||||
g.out.write_string('${expr.ident}${expr.op}')
|
||||
@@ -121,7 +124,7 @@ fn (mut g Generator) gen_expr(expr Expr) {
|
||||
|
||||
fn (mut g Generator) gen_c(program []Stmt) string {
|
||||
g.out.writeln('#include <stdio.h>')
|
||||
g.out.writeln('#include <stdbool.h>') // For your 'bool' types [cite: 4]
|
||||
g.out.writeln('#include <stdbool.h>')
|
||||
for stmt in program {
|
||||
g.gen_stmt(stmt)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user