From 3fbf91a0442043527e62a3426fc0e6195ca1eb05 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20D=C3=A4schle?= Date: Thu, 9 Apr 2020 12:29:29 +0200 Subject: [PATCH] cgen: printing pointers --- vlib/v/gen/cgen.v | 65 ++++++++++++++------------- vlib/v/tests/inout/compiler_test.v | 14 +++--- vlib/v/tests/inout/enum_print.out | 3 +- vlib/v/tests/inout/enum_print.vv | 5 +++ vlib/v/tests/inout/nested_structs.out | 4 ++ vlib/v/tests/inout/nested_structs.vv | 6 +++ vlib/v/tests/inout/os.out | 2 +- 7 files changed, 58 insertions(+), 41 deletions(-) diff --git a/vlib/v/gen/cgen.v b/vlib/v/gen/cgen.v index d66edfebf6..f7bd22cdd2 100644 --- a/vlib/v/gen/cgen.v +++ b/vlib/v/gen/cgen.v @@ -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('))') } diff --git a/vlib/v/tests/inout/compiler_test.v b/vlib/v/tests/inout/compiler_test.v index 8643370881..bc0f4fada0 100644 --- a/vlib/v/tests/inout/compiler_test.v +++ b/vlib/v/tests/inout/compiler_test.v @@ -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 { diff --git a/vlib/v/tests/inout/enum_print.out b/vlib/v/tests/inout/enum_print.out index 362ee21355..440b18edf6 100644 --- a/vlib/v/tests/inout/enum_print.out +++ b/vlib/v/tests/inout/enum_print.out @@ -3,4 +3,5 @@ yellow green green interp: green -interp: green \ No newline at end of file +interp: green +orange \ No newline at end of file diff --git a/vlib/v/tests/inout/enum_print.vv b/vlib/v/tests/inout/enum_print.vv index 77bb301350..a68470d0b2 100644 --- a/vlib/v/tests/inout/enum_print.vv +++ b/vlib/v/tests/inout/enum_print.vv @@ -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() } diff --git a/vlib/v/tests/inout/nested_structs.out b/vlib/v/tests/inout/nested_structs.out index b1b44ad24e..1ed5766aa7 100644 --- a/vlib/v/tests/inout/nested_structs.out +++ b/vlib/v/tests/inout/nested_structs.out @@ -4,4 +4,8 @@ A { pass: false name: '' } +} +B { + pass: false + name: '' } \ No newline at end of file diff --git a/vlib/v/tests/inout/nested_structs.vv b/vlib/v/tests/inout/nested_structs.vv index 26ed593289..970351b410 100644 --- a/vlib/v/tests/inout/nested_structs.vv +++ b/vlib/v/tests/inout/nested_structs.vv @@ -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() } diff --git a/vlib/v/tests/inout/os.out b/vlib/v/tests/inout/os.out index d9ba4c4a5f..bd656fcf15 100644 --- a/vlib/v/tests/inout/os.out +++ b/vlib/v/tests/inout/os.out @@ -1,4 +1,4 @@ CHANGELOG.md CODE_OF_CONDUCT.md CONTRIBUTING.md -README.md \ No newline at end of file +README.md