referencing and methods

This commit is contained in:
uan
2026-02-07 19:14:48 +01:00
parent 005828cec2
commit a3b978a09d
5 changed files with 269 additions and 39 deletions

View File

@@ -14,18 +14,24 @@ fn mangle_var(name string) string {
return 'one_var_${name}'
}
fn mangle_func(name string) string {
fn mangle_func(name string, class_name ?string) string {
if name == 'main' {return 'main'}
return 'one_func_${name}'
mut mangled_name := 'one_func_${name}'
if class_name != none {
mangled_name += '_${class_name}'
}
return mangled_name
}
fn mangle_struct(name string) string {
return 'one_class_${name}_'
return 'one_class_${name}'
}
fn get_type_format(type string) string {
dump(type)
if type.contains('*') {
return 'p'
}
return match type {
'int', 'bool' {'d'}
'real' {'f'}
@@ -45,17 +51,21 @@ fn (mut g Generator) get_print_label(expr Expr) string {
}
fn (mut g Generator) get_c_type(typ string) string {
return match typ {
astkcount := typ.count('*')
clean := typ.replace('*', '')
return match clean {
'real' {'float'}
'int' {'int32_t'}
'string' {'OneString'}
else {typ}
}
else {clean}
} + '*'.repeat(astkcount)
}
fn (mut g Generator) mangle_if_class(name string) string {
if g.symbols.lookup_class(name) != none {
return 'struct ${mangle_struct(name)}'
astkcount := name.count('*')
noptr := name.replace('*', '')
if g.symbols.lookup_class(noptr) != none {
return 'struct ${mangle_struct(noptr)}' + '*'.repeat(astkcount)
}
return name
}
@@ -119,7 +129,7 @@ fn (mut g Generator) gen_stmt(stmt Stmt) {
}
FuncDecl {
c_type := g.mangle_if_class(g.get_c_type(stmt.ret_type))
g.out.write_string('${c_type} ${mangle_func(stmt.name)}(')
g.out.write_string('${c_type} ${mangle_func(stmt.name, stmt.class_name)}(')
for param in stmt.params {
g.gen_stmt(param)
if param != stmt.params[stmt.params.len-1]{
@@ -170,6 +180,16 @@ fn (mut g Generator) gen_stmt(stmt Stmt) {
fn (mut g Generator) gen_expr(expr Expr) {
match expr {
RefExpr {
g.out.write_string('(&')
g.gen_expr(expr.expr)
g.out.write_string(')')
}
DerefExpr {
g.out.write_string('(*')
g.gen_expr(expr.expr)
g.out.write_string(')')
}
RealLiteral {
g.out.write_string(expr.val.str())
}
@@ -243,7 +263,22 @@ fn (mut g Generator) gen_expr(expr Expr) {
g.out.write_string(')')
}
FnCall {
g.out.write_string('${mangle_func(expr.name)}(')
g.out.write_string('${mangle_func(expr.name, none)}(')
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(')')
}
MethodCall {
g.out.write_string('${mangle_func(expr.method, expr.from_type)}(')
g.out.write_string('&')
g.gen_expr(expr.from)
if expr.args.len > 0 {
g.out.write_string(', ')
}
for arg in expr.args {
g.gen_expr(arg)
if arg != expr.args[expr.args.len-1]{
@@ -268,7 +303,12 @@ fn (mut g Generator) gen_expr(expr Expr) {
}
MemberAccess {
g.gen_expr(expr.from)
g.out.write_string('.${expr.member}')
match expr.from_type.count('*') {
0 {g.out.write_string('.')}
1 {g.out.write_string('->')}
else {panic("Tried accessing member from an object of type ${expr.from_type}")}
}
g.out.write_string('${expr.member}')
}
else {panic("Unimplemented expression")}
}