cgen: printing pointers
parent
806f86e4e2
commit
3fbf91a044
|
@ -2545,7 +2545,7 @@ fn (g mut Gen) fn_call(node ast.CallExpr) {
|
|||
}
|
||||
}
|
||||
*/
|
||||
if is_print && node.args[0].typ != table.string_type_idx {
|
||||
if is_print && node.args[0].typ != table.string_type {
|
||||
typ := node.args[0].typ
|
||||
mut styp := g.typ(typ)
|
||||
sym := g.table.get_type_symbol(typ)
|
||||
|
@ -2560,46 +2560,47 @@ fn (g mut Gen) fn_call(node ast.CallExpr) {
|
|||
g.write('string $tmp = ${styp}_str(')
|
||||
g.expr(node.args[0].expr)
|
||||
g.writeln('); ${print_method}($tmp); string_free($tmp); //MEM2 $styp')
|
||||
} else if sym.kind == .enum_ {
|
||||
} else {
|
||||
// println(var) or println println(str.var)
|
||||
expr := node.args[0].expr
|
||||
is_var := match expr {
|
||||
ast.SelectorExpr {
|
||||
true
|
||||
ast.SelectorExpr { true }
|
||||
ast.Ident { true }
|
||||
else { false }
|
||||
}
|
||||
ast.Ident {
|
||||
true
|
||||
}
|
||||
else {
|
||||
false
|
||||
}
|
||||
}
|
||||
g.write(if is_var {
|
||||
'${print_method}(${styp}_str('
|
||||
} else {
|
||||
'${print_method}(tos3("'
|
||||
})
|
||||
g.enum_expr(expr)
|
||||
g.write(if is_var {
|
||||
'))'
|
||||
} else {
|
||||
'"))'
|
||||
})
|
||||
} else {
|
||||
// `println(int_str(10))`
|
||||
// sym := g.table.get_type_symbol(node.args[0].typ)
|
||||
if table.type_is_ptr(typ) {
|
||||
// ptr_str() for pointers
|
||||
styp = 'ptr'
|
||||
// styp = 'ptr'
|
||||
}
|
||||
if sym.kind == .enum_ {
|
||||
if is_var {
|
||||
g.write('${print_method}(${styp}_str(')
|
||||
} else {
|
||||
// when no var, print string directly
|
||||
g.write('${print_method}(tos3("')
|
||||
}
|
||||
if table.type_is_ptr(typ) {
|
||||
// dereference
|
||||
g.write('*')
|
||||
}
|
||||
g.enum_expr(expr)
|
||||
if !is_var {
|
||||
// end of string
|
||||
g.write('"')
|
||||
}
|
||||
} else {
|
||||
g.write('${print_method}(${styp}_str(')
|
||||
if table.type_is_ptr(typ) {
|
||||
// dereference
|
||||
// g.write('*')
|
||||
g.write('*')
|
||||
}
|
||||
g.expr(node.args[0].expr)
|
||||
g.expr(expr)
|
||||
if sym.kind ==.struct_ && styp != 'ptr' && !sym.has_method('str') {
|
||||
g.write(', 0') // trailing 0 is initial struct indent count
|
||||
}
|
||||
}
|
||||
g.write('))')
|
||||
}
|
||||
} else {
|
||||
|
|
|
@ -12,7 +12,6 @@ fn test_all() {
|
|||
files := os.ls(dir) or {
|
||||
panic(err)
|
||||
}
|
||||
println(files)
|
||||
tests := files.filter(it.ends_with('.vv'))
|
||||
if tests.len == 0 {
|
||||
println('no compiler tests found')
|
||||
|
@ -25,7 +24,6 @@ fn test_all() {
|
|||
os.cp(path, program) or {
|
||||
panic(err)
|
||||
}
|
||||
os.rm('exe')
|
||||
x := os.exec('$vexe -o exe -cflags "-w" -cg $program') or {
|
||||
panic(err)
|
||||
}
|
||||
|
@ -34,23 +32,25 @@ fn test_all() {
|
|||
println('nope')
|
||||
panic(err)
|
||||
}
|
||||
os.rm('./exe')
|
||||
// println('============')
|
||||
// println(res.output)
|
||||
// println('============')
|
||||
mut expected := os.read_file(program.replace('.v', '') + '.out') or {
|
||||
panic(err)
|
||||
}
|
||||
expected = expected.trim_space()
|
||||
found := res.output.trim_space()
|
||||
expected = expected.trim_space().trim('\n').replace('\r\n', '\n')
|
||||
found := res.output.trim_space().trim('\n').replace('\r\n', '\n')
|
||||
if expected != found {
|
||||
println(term.red('FAIL'))
|
||||
println(x.output.limit(30))
|
||||
// println(x.output.limit(30))
|
||||
println('============')
|
||||
println('expected:')
|
||||
println(expected)
|
||||
println('\nfound:')
|
||||
println(found)
|
||||
println('============')
|
||||
println('found:')
|
||||
println(found)
|
||||
println('============\n')
|
||||
total_errors++
|
||||
}
|
||||
else {
|
||||
|
|
|
@ -4,3 +4,4 @@ green
|
|||
green
|
||||
interp: green
|
||||
interp: green
|
||||
orange
|
|
@ -11,6 +11,10 @@ struct A{
|
|||
color Color
|
||||
}
|
||||
|
||||
fn (c &Color) test() {
|
||||
println(c)
|
||||
}
|
||||
|
||||
fn main() {
|
||||
col := Color.green
|
||||
a := A{color: col}
|
||||
|
@ -21,4 +25,5 @@ fn main() {
|
|||
println(a.color)
|
||||
println('interp: ${col}')
|
||||
println('interp: ${a.color}')
|
||||
orange.test()
|
||||
}
|
||||
|
|
|
@ -5,3 +5,7 @@ A {
|
|||
name: ''
|
||||
}
|
||||
}
|
||||
B {
|
||||
pass: false
|
||||
name: ''
|
||||
}
|
|
@ -5,6 +5,10 @@ struct B {
|
|||
name string
|
||||
}
|
||||
|
||||
fn (b &B) print() {
|
||||
println(b)
|
||||
}
|
||||
|
||||
struct A {
|
||||
test bool
|
||||
b B
|
||||
|
@ -13,4 +17,6 @@ struct A {
|
|||
fn main() {
|
||||
a := A{}
|
||||
println(a)
|
||||
b := B{}
|
||||
b.print()
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue