Files
one/generator.v
2026-02-05 19:40:59 +01:00

205 lines
4.5 KiB
V

module main
import strings
import os
struct Generator {
mut:
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 mangle_if_struct_type(name string) string {
if name.len < 7 || name[0..6] != 'struct' {return name}
return 'struct ${mangle_struct(name[7..])}'
}
fn (mut g Generator) get_c_type(typ string) string {
return match typ {
'real' {'float'}
'int' {'int32_t'}
else {typ}
}
}
fn (mut g Generator) gen_stmt(stmt Stmt) {
match stmt {
VarDecl {
c_type := mangle_if_struct_type(g.get_c_type(stmt.type))
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(';')
}
ExprStmt {
g.gen_expr(stmt.expr)
g.out.writeln(';')
}
ReturnStmt {
g.out.write_string('return ')
g.gen_expr(stmt.expr)
g.out.writeln(';')
}
Block {
g.out.writeln('{')
for inner_stmt in stmt.stmts {
g.gen_stmt(inner_stmt)
}
g.out.writeln('}')
}
FuncDecl {
dump(stmt.ret_type)
c_type := mangle_if_struct_type(g.get_c_type(stmt.ret_type))
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]{
g.out.write_string(', ')
}
}
g.out.write_string(')')
g.gen_stmt(stmt.block)
}
Param {
c_type := mangle_if_struct_type(g.get_c_type(stmt.type))
g.out.write_string('${c_type} ${mangle_var(stmt.name)}')
}
ClassDecl {
g.out.writeln('struct ${mangle_struct(stmt.name)} {')
for member in stmt.members {
g.gen_expr(member)
g.out.writeln(';')
}
g.out.writeln('};')
}
}
}
fn (mut g Generator) gen_expr(expr Expr) {
match expr {
RealLiteral {
g.out.write_string(expr.val.str())
}
IntegerLiteral {
g.out.write_string(expr.val.str())
}
BoolLiteral {
g.out.write_string(expr.val.str())
}
Variable {
g.out.write_string(mangle_var(expr.name))
}
UnaryExpr {
g.out.write_string('${expr.ident}${expr.op}')
}
BinaryExpr {
g.out.write_string('(')
g.gen_expr(expr.left)
g.out.write_string(' ${expr.op} ')
g.gen_expr(expr.right)
g.out.write_string(')')
}
PrintExpr {
g.out.write_string('printf(\"%')
format := match expr.type {
'int', 'bool' {'d'}
'real' {'lf'}
else {panic("Tried printing illegal type")}
}
g.out.write_string('${format}\\n\", ')
g.gen_expr(expr.expr)
g.out.write_string(')')
}
TypeCast {
c_type := mangle_if_struct_type(g.get_c_type(expr.type))
g.out.write_string('((${c_type})')
g.gen_expr(expr.expr)
g.out.write_string(')')
}
ParenExpr {
g.out.write_string('(')
g.gen_expr(expr.expr)
g.out.write_string(')')
}
FnCall {
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]{
g.out.write_string(', ')
}
}
g.out.write_string(')')
}
ClassMember {
c_type := mangle_if_struct_type(g.get_c_type(expr.type))
g.out.write_string('${c_type} ${expr.name}')
}
ClassInstantiation {
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")}
}
}
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)
}
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 { }
}
}
}