This commit is contained in:
uan
2026-02-05 13:18:40 +01:00
parent 12c925ebcd
commit 0d5cc35b5a
5 changed files with 161 additions and 26 deletions

View File

@@ -9,7 +9,21 @@ mut:
}
fn mangle_var(name string) string {
return 'v${name}'
return 'one_v${name}'
}
fn mangle_func(name string) string {
if name == 'main' {return 'main'}
return 'one_f${name}'
}
fn mangle_struct(name string) string {
return 'one_s${name}_'
}
fn mangle_if_struct_type(name string) string {
if name[0..6] != 'struct' {return name}
return 'struct ${mangle_struct(name[7..])}'
}
fn (mut g Generator) get_c_type(typ string) string {
@@ -23,7 +37,7 @@ fn (mut g Generator) get_c_type(typ string) string {
fn (mut g Generator) gen_stmt(stmt Stmt) {
match stmt {
VarDecl {
c_type := g.get_c_type(stmt.type)
c_type := mangle_if_struct_type(g.get_c_type(stmt.type))
if stmt.const {
g.out.write_string('const ')
}
@@ -49,7 +63,7 @@ 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} ${mangle_func(stmt.name)}(')
for param in stmt.params {
g.gen_stmt(param)
if param != stmt.params[stmt.params.len-1]{
@@ -63,6 +77,14 @@ fn (mut g Generator) gen_stmt(stmt Stmt) {
c_type := g.get_c_type(stmt.type)
g.out.write_string('${c_type} ${mangle_var(stmt.name)}')
}
StructDecl {
g.out.writeln('struct ${mangle_struct(stmt.name)} {')
for member in stmt.members {
g.gen_expr(member)
g.out.writeln(';')
}
g.out.writeln('};')
}
}
}
@@ -113,7 +135,7 @@ fn (mut g Generator) gen_expr(expr Expr) {
g.out.write_string(')')
}
FnCall {
g.out.write_string('${expr.name}(')
g.out.write_string('${mangle_func(expr.name)}(')
for arg in expr.args {
g.gen_expr(arg)
if arg != expr.args[expr.args.len-1]{
@@ -122,6 +144,20 @@ fn (mut g Generator) gen_expr(expr Expr) {
}
g.out.write_string(')')
}
StructMember {
c_type := g.get_c_type(expr.type)
g.out.write_string('${c_type} ${expr.name}')
}
StructInstantiation {
g.out.write_string('(struct ${mangle_struct(expr.name)}){')
for m_expr in expr.member_values {
g.gen_expr(m_expr)
if m_expr != expr.member_values[expr.member_values.len - 1] {
g.out.write_string(', ')
}
}
g.out.write_string('}')
}
else {panic("Unimplemented expression")}
}
}
@@ -138,21 +174,25 @@ fn (mut g Generator) gen_c(program []Stmt) string {
}
fn compile_with_clang(c_code string, output_name string, keep_c bool) {
fn compile(c_code string, output_name string, keep_c bool, compiler string) {
c_file := 'middle_c.c'
os.write_file(c_file, c_code) or {
eprintln('Failed to write C file: $err')
return
}
clang_cmd := 'clang ${c_file} -o ${output_name} -O2'
cmd := match compiler {
'clang' {'clang ${c_file} -o ${output_name} -O2'}
'gcc' {'gcc ${c_file} -o ${output_name} -O2'}
else {panic("Invalid compiler")}
}
println('Executing: ${clang_cmd}')
println('Executing: ${cmd}')
result := os.execute(clang_cmd)
result := os.execute(cmd)
if result.exit_code != 0 {
eprintln('Clang Compilation Failed:')
eprintln('${compiler} Compilation Failed:')
eprintln(result.output)
} else {
println('Compilation successful! Binary created: $output_name')