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

2
main.v
View File

@@ -32,6 +32,6 @@ fn main() {
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 {
expr Expr
type string
exprs []Expr
types []string
}
struct FnCall {
@@ -408,9 +408,20 @@ fn (mut p Parser) parse_call(name string) FnCall {
fn (mut p Parser) parse_print() PrintExpr {
p.expect(.lparen)
mut exprs := []Expr{}
mut types := []string{}
for p.peek().type != .rparen {
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)
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 {