better types in C

This commit is contained in:
uan
2026-02-04 22:42:39 +01:00
parent a964c86967
commit 12c925ebcd

View File

@@ -13,8 +13,11 @@ fn mangle_var(name string) string {
} }
fn (mut g Generator) get_c_type(typ string) string { fn (mut g Generator) get_c_type(typ string) string {
c_type := if typ == 'real' { 'double' } else { typ } return match typ {
return c_type 'real' {'float'}
'int' {'int32_t'}
else {typ}
}
} }
fn (mut g Generator) gen_stmt(stmt Stmt) { fn (mut g Generator) gen_stmt(stmt Stmt) {
@@ -100,8 +103,9 @@ fn (mut g Generator) gen_expr(expr Expr) {
} }
TypeCast { TypeCast {
c_type := g.get_c_type(expr.type) 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.gen_expr(expr.expr)
g.out.write_string(')')
} }
ParenExpr { ParenExpr {
g.out.write_string('(') 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 { fn (mut g Generator) gen_c(program []Stmt) string {
g.out.writeln('#include <stdio.h>') g.out.writeln('#include <stdio.h>')
g.out.writeln('#include <stdbool.h>') g.out.writeln('#include <stdbool.h>')
g.out.writeln('#include <stdint.h>')
for stmt in program { for stmt in program {
g.gen_stmt(stmt) g.gen_stmt(stmt)
} }