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
mut styp := g.typ(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.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.Ident {
true
}
else {
false
}
ast.SelectorExpr { true }
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'
}
g.write('${print_method}(${styp}_str(')
if table.type_is_ptr(typ) {
// dereference
// g.write('*')
}
g.expr(node.args[0].expr)
if sym.kind ==.struct_ && styp != 'ptr' && !sym.has_method('str') {
g.write(', 0') // trailing 0 is initial struct indent count
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.expr(expr)
if sym.kind ==.struct_ && styp != 'ptr' && !sym.has_method('str') {
g.write(', 0') // trailing 0 is initial struct indent count
}
}
g.write('))')
}

View File

@ -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 {

View File

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

View File

@ -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()
}

View File

@ -4,4 +4,8 @@ A {
pass: false
name: ''
}
}
B {
pass: false
name: ''
}

View File

@ -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()
}

View File

@ -1,4 +1,4 @@
CHANGELOG.md
CODE_OF_CONDUCT.md
CONTRIBUTING.md
README.md
README.md