From 854de4e7e02efb126fbc74cbf602dfacea510489 Mon Sep 17 00:00:00 2001 From: Delyan Angelov Date: Mon, 16 Sep 2019 00:41:12 +0300 Subject: [PATCH] repl: add more stats and tests; compiler: tcc fixes --- compiler/cflags.v | 3 ++ compiler/cheaders.v | 12 ++++---- compiler/gen_c.v | 2 +- compiler/tests/repl/README.md | 13 +++++++- .../tests/repl/chained_fields.correct.repl | 16 ++++++++++ compiler/tests/repl/empty_struct.repl | 4 +++ compiler/tests/repl/immutable_len_fields.repl | 30 +++++++++++++++++++ compiler/tests/repl/repl_test.v | 9 +++++- 8 files changed, 79 insertions(+), 10 deletions(-) create mode 100644 compiler/tests/repl/chained_fields.correct.repl create mode 100644 compiler/tests/repl/empty_struct.repl create mode 100644 compiler/tests/repl/immutable_len_fields.repl diff --git a/compiler/cflags.v b/compiler/cflags.v index b56bddfa12..2edb6a2ee9 100644 --- a/compiler/cflags.v +++ b/compiler/cflags.v @@ -30,6 +30,9 @@ fn (v V) get_os_cflags() []CFlag { // format flag fn (cf &CFlag) format() string { mut value := cf.value + if cf.name == '-l' && value.len>0 { + return '${cf.name}${value}'.trim_space() + } // convert to absolute path if cf.name == '-I' || cf.name == '-L' || value.ends_with('.o') { value = '"'+os.realpath(value)+'"' diff --git a/compiler/cheaders.v b/compiler/cheaders.v index 5da192b96f..0705dc4bbc 100644 --- a/compiler/cheaders.v +++ b/compiler/cheaders.v @@ -38,12 +38,8 @@ CommonCHeaders = ' #include // os__wait uses wait on nix #endif - #define EMPTY_STRUCT_DECLARATION -#ifdef _MSC_VER -#define EMPTY_STRUCT_DECLARATION int:0 -#endif - +#define EMPTY_STRUCT_INITIALIZATION #define OPTION_CAST(x) (x) #ifdef _WIN32 @@ -65,9 +61,11 @@ CommonCHeaders = ' // MSVC cannot parse some things properly #undef EMPTY_STRUCT_DECLARATION -#define EMPTY_STRUCT_DECLARATION void *____dummy_variable - +#undef EMPTY_STRUCT_INITIALIZATION #undef OPTION_CAST + +#define EMPTY_STRUCT_DECLARATION int ____dummy_variable +#define EMPTY_STRUCT_INITIALIZATION 0 #define OPTION_CAST(x) #endif diff --git a/compiler/gen_c.v b/compiler/gen_c.v index 49235c0610..a41c876ec9 100644 --- a/compiler/gen_c.v +++ b/compiler/gen_c.v @@ -330,7 +330,7 @@ fn (p mut Parser) gen_struct_init(typ string, t Type) bool { else { p.gen('($typ) {') if t.fields.len == 1 && t.fields[0].name == '' && t.fields[0].typ.starts_with('EMPTY_STRUCT_DECLARATION') { - p.gen(' 0 /* v empty struct initialization */ ') + p.gen(' EMPTY_STRUCT_INITIALIZATION ') } } } diff --git a/compiler/tests/repl/README.md b/compiler/tests/repl/README.md index 6120577428..095015306e 100644 --- a/compiler/tests/repl/README.md +++ b/compiler/tests/repl/README.md @@ -5,10 +5,21 @@ - Write the input to be given to REPL - Add `===output===` - Write the output expected + +### Notes +Keep in mind, that the way V repl works for now, every non empty line +would cause a new recompilation of the entire repl content that was +collected so far. + +*Longer REPL files would cause measurably* +*longer recompilation/testing times.* + +Also, longer repl files would be slower to debug when they fail, +*It is better to have several smaller files vs one huge REPL file.* ### Example : ``` a := 1 println(a) ===output=== -1 \ No newline at end of file +1 diff --git a/compiler/tests/repl/chained_fields.correct.repl b/compiler/tests/repl/chained_fields.correct.repl new file mode 100644 index 0000000000..81597218c6 --- /dev/null +++ b/compiler/tests/repl/chained_fields.correct.repl @@ -0,0 +1,16 @@ +struct A { mut: v int } struct B { a A } struct C { mut: b B } struct D { mut: c C } struct E { mut: v []int } struct F { e []E } + +mut b := B{} b = B{A{2}} +println('b is: ' + b.a.v.str()) + +mut c := C{} c.b = B{} + +mut d := D{} d.c.b = B{} + +f := F{[E{[10,20,30]},E{[100,200,300,400]}]} +println('f.e[0].v.len: ${f.e[0].v.len}') +println('f.e[1].v.len: ${f.e[1].v.len}') +===output=== +b is: 2 +f.e[0].v.len: 3 +f.e[1].v.len: 4 diff --git a/compiler/tests/repl/empty_struct.repl b/compiler/tests/repl/empty_struct.repl new file mode 100644 index 0000000000..ee27285fcf --- /dev/null +++ b/compiler/tests/repl/empty_struct.repl @@ -0,0 +1,4 @@ +struct Empty{} ee := Empty{} +println('OK') +===output=== +OK diff --git a/compiler/tests/repl/immutable_len_fields.repl b/compiler/tests/repl/immutable_len_fields.repl new file mode 100644 index 0000000000..f7bcd20e06 --- /dev/null +++ b/compiler/tests/repl/immutable_len_fields.repl @@ -0,0 +1,30 @@ +mut s := 'hello world' +s.len = 0 // Error (field len immutable) + +mut a := []string +a.len = 0 // Error (field len immutable) + +mut ints := []int +ints.len = 0 // Error (field len immutable) + +println('BYE') +===output=== +.vrepl_temp.v:2:5: cannot modify immutable field `len` (type `string`) +declare the field with `mut:` +struct string { + mut: + len int +} +.vrepl_temp.v:3:5: cannot modify immutable field `len` (type `array`) +declare the field with `mut:` +struct array { + mut: + len int +} +.vrepl_temp.v:4:8: cannot modify immutable field `len` (type `array`) +declare the field with `mut:` +struct array { + mut: + len int +} +BYE diff --git a/compiler/tests/repl/repl_test.v b/compiler/tests/repl/repl_test.v index e2aa8588b2..802e3c00d4 100644 --- a/compiler/tests/repl/repl_test.v +++ b/compiler/tests/repl/repl_test.v @@ -20,17 +20,24 @@ fn test_the_v_compiler_can_be_invoked() { fn test_all_v_repl_files() { options := runner.new_options() - global_start_time := runner.now() + global_start_time := runner.now() + mut total_tests := 0 + mut ok_tests := 0 + mut failed_tests := 0 for file in options.files { + total_tests++ sticks := runner.now() fres := runner.run_repl_file(options.wd, options.vexec, file) or { + failed_tests++ assert false eprintln( runner.tdiff_in_ms(err, sticks) ) continue } assert true println( runner.tdiff_in_ms(fres, sticks) ) + ok_tests++ } println( runner.tdiff_in_ms('<=== total time spent running REPL files', global_start_time) ) + println( ' ok, failed, total : ${ok_tests:5d}, ${failed_tests:5d}, ${total_tests:5d}' ) }