strings handled as built-in struct, and added immutable keyword
This commit is contained in:
21
generator.v
21
generator.v
@@ -24,6 +24,7 @@ fn mangle_struct(name string) string {
|
||||
|
||||
|
||||
fn get_type_format(type string) string {
|
||||
dump(type)
|
||||
return match type {
|
||||
'int', 'bool' {'d'}
|
||||
'real' {'f'}
|
||||
@@ -46,7 +47,7 @@ fn (mut g Generator) get_c_type(typ string) string {
|
||||
return match typ {
|
||||
'real' {'float'}
|
||||
'int' {'int32_t'}
|
||||
'string' {'char*'}
|
||||
'string' {'OneString'}
|
||||
else {typ}
|
||||
}
|
||||
}
|
||||
@@ -70,12 +71,16 @@ fn (mut g Generator) gen_class_print_func(stmt ClassDecl) {
|
||||
g.out.writeln('for(int i=0; i<indent + 1; i++) printf(" ");')
|
||||
g.out.write_string('printf("${member.name}: ");')
|
||||
|
||||
if g.symbols.lookup_class(member.type) != none {
|
||||
if member.type != 'string' && g.symbols.lookup_class(member.type) != none {
|
||||
inner_struct_name := mangle_struct(member.type)
|
||||
g.out.writeln('print_${inner_struct_name}(s.${member.name}, indent + 1);')
|
||||
} else {
|
||||
format := get_type_format(member.type)
|
||||
g.out.writeln('printf("%${format}\\n", s.${member.name});')
|
||||
g.out.write_string('printf("%${format}\\n", s.${member.name}')
|
||||
if member.type == 'string' {
|
||||
g.out.write_string('.string')
|
||||
}
|
||||
g.out.writeln(');')
|
||||
}
|
||||
}
|
||||
|
||||
@@ -168,7 +173,7 @@ fn (mut g Generator) gen_expr(expr Expr) {
|
||||
g.out.write_string(expr.val.str())
|
||||
}
|
||||
StringLiteral {
|
||||
g.out.write_string('\"${expr.val}\"')
|
||||
g.out.write_string('(OneString){\"${expr.val}\", ${expr.val.len}}')
|
||||
}
|
||||
Variable {
|
||||
g.out.write_string(mangle_var(expr.name))
|
||||
@@ -188,16 +193,20 @@ fn (mut g Generator) gen_expr(expr Expr) {
|
||||
for i < expr.exprs.len {
|
||||
inner_expr := expr.exprs[i];
|
||||
expr_type := expr.types[i];
|
||||
if g.symbols.lookup_class(expr_type) != none {
|
||||
if expr_type != 'string' && g.symbols.lookup_class(expr_type) != none {
|
||||
class_name := mangle_struct(expr_type)
|
||||
g.out.write_string('print_${class_name}(')
|
||||
g.gen_expr(inner_expr)
|
||||
g.out.write_string(', 0);')
|
||||
} else {
|
||||
g.out.write_string('printf(\"')
|
||||
format := get_type_format(expr_type)
|
||||
g.out.write_string('%${format}')
|
||||
g.out.write_string('\", ')
|
||||
g.gen_expr(inner_expr)
|
||||
if expr_type == 'string' {
|
||||
g.out.write_string('.string')
|
||||
}
|
||||
g.out.write_string(');')
|
||||
}
|
||||
i++;
|
||||
@@ -254,7 +263,7 @@ 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>')
|
||||
//g.out.writeln('typedef struct __one_string_builtin__ {\nchar* string;\nint len;\n} string;')
|
||||
g.out.writeln('typedef struct {\nchar* string;\nint len;\n} OneString;')
|
||||
for stmt in program {
|
||||
g.gen_stmt(stmt)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user