better types in C
This commit is contained in:
11
generator.v
11
generator.v
@@ -13,8 +13,11 @@ fn mangle_var(name string) string {
|
||||
}
|
||||
|
||||
fn (mut g Generator) get_c_type(typ string) string {
|
||||
c_type := if typ == 'real' { 'double' } else { typ }
|
||||
return c_type
|
||||
return match typ {
|
||||
'real' {'float'}
|
||||
'int' {'int32_t'}
|
||||
else {typ}
|
||||
}
|
||||
}
|
||||
|
||||
fn (mut g Generator) gen_stmt(stmt Stmt) {
|
||||
@@ -100,8 +103,9 @@ fn (mut g Generator) gen_expr(expr Expr) {
|
||||
}
|
||||
TypeCast {
|
||||
c_type := g.get_c_type(expr.type)
|
||||
g.out.write_string('(${c_type})')
|
||||
g.out.write_string('((${c_type})')
|
||||
g.gen_expr(expr.expr)
|
||||
g.out.write_string(')')
|
||||
}
|
||||
ParenExpr {
|
||||
g.out.write_string('(')
|
||||
@@ -125,6 +129,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>')
|
||||
g.out.writeln('#include <stdint.h>')
|
||||
for stmt in program {
|
||||
g.gen_stmt(stmt)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user