cgen: printing pointers

pull/4313/head
Daniel Däschle 2020-04-09 12:29:29 +02:00 committed by GitHub
parent 806f86e4e2
commit 3fbf91a044
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
7 changed files with 58 additions and 41 deletions

View File

@ -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 typ := node.args[0].typ
mut styp := g.typ(typ) mut styp := g.typ(typ)
sym := g.table.get_type_symbol(typ) sym := g.table.get_type_symbol(typ)
@ -2560,45 +2560,46 @@ fn (g mut Gen) fn_call(node ast.CallExpr) {
g.write('string $tmp = ${styp}_str(') g.write('string $tmp = ${styp}_str(')
g.expr(node.args[0].expr) g.expr(node.args[0].expr)
g.writeln('); ${print_method}($tmp); string_free($tmp); //MEM2 $styp') 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 expr := node.args[0].expr
is_var := match expr { is_var := match expr {
ast.SelectorExpr { ast.SelectorExpr { true }
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))` // `println(int_str(10))`
// sym := g.table.get_type_symbol(node.args[0].typ) // sym := g.table.get_type_symbol(node.args[0].typ)
if table.type_is_ptr(typ) { if table.type_is_ptr(typ) {
// ptr_str() for pointers // ptr_str() for pointers
styp = 'ptr' // styp = 'ptr'
} }
g.write('${print_method}(${styp}_str(') if sym.kind == .enum_ {
if table.type_is_ptr(typ) { if is_var {
// dereference g.write('${print_method}(${styp}_str(')
// g.write('*') } else {
} // when no var, print string directly
g.expr(node.args[0].expr) g.write('${print_method}(tos3("')
if sym.kind ==.struct_ && styp != 'ptr' && !sym.has_method('str') { }
g.write(', 0') // trailing 0 is initial struct indent count 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.expr(expr)
if sym.kind ==.struct_ && styp != 'ptr' && !sym.has_method('str') {
g.write(', 0') // trailing 0 is initial struct indent count
}
} }
g.write('))') g.write('))')
} }

View File

@ -12,7 +12,6 @@ fn test_all() {
files := os.ls(dir) or { files := os.ls(dir) or {
panic(err) panic(err)
} }
println(files)
tests := files.filter(it.ends_with('.vv')) tests := files.filter(it.ends_with('.vv'))
if tests.len == 0 { if tests.len == 0 {
println('no compiler tests found') println('no compiler tests found')
@ -25,7 +24,6 @@ fn test_all() {
os.cp(path, program) or { os.cp(path, program) or {
panic(err) panic(err)
} }
os.rm('exe')
x := os.exec('$vexe -o exe -cflags "-w" -cg $program') or { x := os.exec('$vexe -o exe -cflags "-w" -cg $program') or {
panic(err) panic(err)
} }
@ -34,23 +32,25 @@ fn test_all() {
println('nope') println('nope')
panic(err) panic(err)
} }
os.rm('./exe')
// println('============') // println('============')
// println(res.output) // println(res.output)
// println('============') // println('============')
mut expected := os.read_file(program.replace('.v', '') + '.out') or { mut expected := os.read_file(program.replace('.v', '') + '.out') or {
panic(err) panic(err)
} }
expected = expected.trim_space() expected = expected.trim_space().trim('\n').replace('\r\n', '\n')
found := res.output.trim_space() found := res.output.trim_space().trim('\n').replace('\r\n', '\n')
if expected != found { if expected != found {
println(term.red('FAIL')) println(term.red('FAIL'))
println(x.output.limit(30)) // println(x.output.limit(30))
println('============') println('============')
println('expected:') println('expected:')
println(expected) println(expected)
println('\nfound:')
println(found)
println('============') println('============')
println('found:')
println(found)
println('============\n')
total_errors++ total_errors++
} }
else { else {

View File

@ -4,3 +4,4 @@ green
green green
interp: green interp: green
interp: green interp: green
orange

View File

@ -11,6 +11,10 @@ struct A{
color Color color Color
} }
fn (c &Color) test() {
println(c)
}
fn main() { fn main() {
col := Color.green col := Color.green
a := A{color: col} a := A{color: col}
@ -21,4 +25,5 @@ fn main() {
println(a.color) println(a.color)
println('interp: ${col}') println('interp: ${col}')
println('interp: ${a.color}') println('interp: ${a.color}')
orange.test()
} }

View File

@ -5,3 +5,7 @@ A {
name: '' name: ''
} }
} }
B {
pass: false
name: ''
}

View File

@ -5,6 +5,10 @@ struct B {
name string name string
} }
fn (b &B) print() {
println(b)
}
struct A { struct A {
test bool test bool
b B b B
@ -13,4 +17,6 @@ struct A {
fn main() { fn main() {
a := A{} a := A{}
println(a) println(a)
b := B{}
b.print()
} }