Compare commits

..

3 Commits

Author SHA1 Message Date
uan
db1b4b88c0 - 2026-02-06 16:02:40 +01:00
uan
dee3a5dfed better prints 2026-02-06 11:38:40 +01:00
uan
5602765b67 multiple expr per print 2026-02-06 11:15:44 +01:00
4 changed files with 48 additions and 31 deletions

View File

@@ -80,7 +80,7 @@ fn (mut g Generator) gen_class_print_func(stmt ClassDecl) {
} }
g.out.writeln('for(int i=0; i<indent; i++) printf(" ");') g.out.writeln('for(int i=0; i<indent; i++) printf(" ");')
g.out.writeln('printf("}\\n");') g.out.writeln('printf("}\");')
g.out.writeln('}') g.out.writeln('}')
} }
@@ -169,26 +169,32 @@ fn (mut g Generator) gen_expr(expr Expr) {
g.out.write_string(')') g.out.write_string(')')
} }
PrintExpr { PrintExpr {
label := g.get_print_label(expr.expr) mut i := 0;
if g.symbols.lookup_class(expr.type) != none { for i < expr.exprs.len {
class_name := mangle_struct(expr.type) inner_expr := expr.exprs[i];
if label != "" { expr_type := expr.types[i];
g.out.write_string('printf("${label}: ");') label := g.get_print_label(inner_expr);
} if g.symbols.lookup_class(expr_type) != none {
class_name := mangle_struct(expr_type)
g.out.write_string('print_${class_name}(') g.out.write_string('print_${class_name}(')
g.gen_expr(expr.expr) g.gen_expr(inner_expr)
g.out.write_string(', 0)') g.out.write_string(', 0);')
} else { } else {
g.out.write_string('printf(\"') g.out.write_string('printf(\"')
if label != "" { dump(label);
g.out.write_string('${label}: ') format := get_type_format(expr_type)
g.out.write_string('%${format}')
g.out.write_string('\", ')
g.gen_expr(inner_expr)
g.out.write_string(');')
} }
format := get_type_format(expr.type) i++;
g.out.write_string('%${format}\\n\", ') if i < expr.exprs.len {
g.gen_expr(expr.expr) g.out.write_string('printf(\" \");')
g.out.write_string(')')
} }
} }
g.out.write_string('printf(\"\\n\");\n')
}
TypeCast { TypeCast {
c_type := g.mangle_if_class(g.get_c_type(expr.type)) c_type := g.mangle_if_class(g.get_c_type(expr.type))
g.out.write_string('((${c_type})') g.out.write_string('((${c_type})')

2
main.v
View File

@@ -32,6 +32,6 @@ fn main() {
out_c := generator.gen_c(statements) out_c := generator.gen_c(statements)
compile(out_c, 'exec', true, 'clang') compile(out_c, 'exec', true, 'gcc')
} }

BIN
one

Binary file not shown.

View File

@@ -183,8 +183,8 @@ struct ParenExpr {
} }
struct PrintExpr { struct PrintExpr {
expr Expr exprs []Expr
type string types []string
} }
struct FnCall { struct FnCall {
@@ -408,9 +408,20 @@ fn (mut p Parser) parse_call(name string) FnCall {
fn (mut p Parser) parse_print() PrintExpr { fn (mut p Parser) parse_print() PrintExpr {
p.expect(.lparen) p.expect(.lparen)
mut exprs := []Expr{}
mut types := []string{}
for p.peek().type != .rparen {
expr := p.parse_expr(.lowest) expr := p.parse_expr(.lowest)
exprs << expr
types << p.get_expr_type(expr)
if p.peek().type == .comma {
p.next()
} else {
break
}
}
p.expect(.rparen) p.expect(.rparen)
return PrintExpr{expr: expr, type: p.get_expr_type(expr)} return PrintExpr{exprs: exprs, types: types}
} }
fn (mut p Parser) parse_binary(left Expr, op string, prec Precedence) BinaryExpr { fn (mut p Parser) parse_binary(left Expr, op string, prec Precedence) BinaryExpr {