module main import strings import os struct Generator { mut: symbols SymbolTable out strings.Builder } fn mangle_var(name string) string { return 'one_var_${name}' } fn mangle_func(name string) string { if name == 'main' {return 'main'} return 'one_func_${name}' } fn mangle_struct(name string) string { return 'one_class_${name}_' } fn get_type_format(type string) string { return match type { 'int', 'bool' {'d'} 'real' {'f'} 'string' {'s'} else {panic("invalid type to print")} } } fn (mut g Generator) get_print_label(expr Expr) string { return match expr { Variable { expr.name } MemberAccess { "${g.get_print_label(expr.from)}.${expr.member}" } else { "" } } } fn (mut g Generator) get_c_type(typ string) string { return match typ { 'real' {'float'} 'int' {'int32_t'} 'string' {'char*'} else {typ} } } fn (mut g Generator) mangle_if_class(name string) string { if g.symbols.lookup_class(name) != none { return 'struct ${mangle_struct(name)}' } return name } // thank google gemini for this one, I genuinely did not have the mental strength to // think this at 9pm fn (mut g Generator) gen_class_print_func(stmt ClassDecl) { struct_name := mangle_struct(stmt.name) g.out.writeln('void print_${struct_name}(struct ${struct_name} s, int indent) {') g.out.writeln('printf("${stmt.name} {\\n");') for member in stmt.members { g.out.writeln('for(int i=0; i') g.out.writeln('#include ') g.out.writeln('#include ') //g.out.writeln('typedef struct __one_string_builtin__ {\nchar* string;\nint len;\n} string;') for stmt in program { g.gen_stmt(stmt) } return g.out.str() } 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 } 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: ${cmd}') result := os.execute(cmd) if result.exit_code != 0 { eprintln('${compiler} Compilation Failed:') eprintln(result.output) } else { println('Compilation successful! Binary created: $output_name') if !keep_c { os.rm(c_file) or { } } } }