From 9b2511133dcfde71ac1b67f0e1f7ac4a7b129e5e Mon Sep 17 00:00:00 2001 From: Alexander Medvednikov Date: Fri, 10 Apr 2020 00:30:43 +0200 Subject: [PATCH] vfmt: lots of fixes; cmd/v: -keepc; --- cmd/v/v.v | 1 + vlib/v/fmt/fmt.v | 30 ++++++++++++++---- vlib/v/parser/comptime.v | 7 ++-- vlib/v/tests/struct_test.v | 65 ++++++++++++++++++++++---------------- 4 files changed, 66 insertions(+), 37 deletions(-) diff --git a/cmd/v/v.v b/cmd/v/v.v index 6e2c23fb00..a3e6d8ac2b 100644 --- a/cmd/v/v.v +++ b/cmd/v/v.v @@ -130,6 +130,7 @@ fn parse_args(args []string) (&pref.Preferences, string) { '-obfuscate' { res.obfuscate = true } '-translated' { res.translated = true } '-showcc' { res.show_cc = true } + '-keepc' { res.is_keep_c = true } //'-x64' { res.translated = true } '-os' { //TODO Remove `tmp` variable when it doesn't error out in C. diff --git a/vlib/v/fmt/fmt.v b/vlib/v/fmt/fmt.v index 0d1e0aa250..a55c03c301 100644 --- a/vlib/v/fmt/fmt.v +++ b/vlib/v/fmt/fmt.v @@ -402,6 +402,9 @@ fn (f mut Fmt) struct_decl(node ast.StructDecl) { f.write('\t$field.name ') f.write(strings.repeat(` `, max - field.name.len)) f.write(f.type_to_str(field.typ)) + if field.default_expr != '' { + f.write(' = $field.default_expr') + } // f.write('// $field.pos.line_nr') if field.comment.text != '' && field.comment.pos.line_nr == field.pos.line_nr { // Same line comment @@ -605,13 +608,13 @@ fn (f mut Fmt) expr(node ast.Expr) { f.write(it.field) } ast.SizeOf { - f.writeln('sizeof(') + f.write('sizeof(') if it.type_name != '' { - f.writeln(it.type_name) + f.write(it.type_name) } else { - f.writeln(f.type_to_str(it.typ)) + f.write(f.type_to_str(it.typ)) } - f.writeln(')') + f.write(')') } ast.StringLiteral { if it.val.contains("'") { @@ -641,10 +644,23 @@ fn (f mut Fmt) expr(node ast.Expr) { } ast.StructInit { type_sym := f.table.get_type_symbol(it.typ) - name := short_module(type_sym.name).replace(f.cur_mod + '.', '') // TODO f.type_to_str? - // `Foo{}` on one line if there are no fields - if it.fields.len == 0 { + mut name := short_module(type_sym.name).replace(f.cur_mod + '.', '') // TODO f.type_to_str? + if name == 'void' { + name = '' + } + if it.fields.len == 0 && it.exprs.len == 0 { + // `Foo{}` on one line if there are no fields f.write('$name{}') + } else if it.fields.len == 0 { + // `Foo{1,2,3}` (short syntax ) + f.write('{') + for i, expr in it.exprs { + f.expr(expr) + if i < it.exprs.len - 1 { + f.write(', ') + } + } + f.write('}') } else { f.writeln('$name{') f.indent++ diff --git a/vlib/v/parser/comptime.v b/vlib/v/parser/comptime.v index c7fde23ee1..1824af3a39 100644 --- a/vlib/v/parser/comptime.v +++ b/vlib/v/parser/comptime.v @@ -21,9 +21,9 @@ fn (p mut Parser) hash() ast.HashStmt { // #flag linux -lm mut flag := val[5..] // expand `@VROOT` to its absolute path - /* if flag.contains('@VROOT') { - vmod_file_location := p.v.mod_file_cacher.get( p.file_path_dir ) + /* + vmod_file_location := p.v.mod_file_cacher.get( p.file_path_dir ) if vmod_file_location.vmod_file.len == 0 { // There was no actual v.mod file found. p.error_with_token_index('To use @VROOT, you need' + @@ -32,8 +32,9 @@ fn (p mut Parser) hash() ast.HashStmt { p.cur_tok_index() - 1) } flag = flag.replace('@VROOT', vmod_file_location.vmod_folder ) -} */ + flag = flag.replace('@VROOT', '/Users/alex/code/v/') + } for deprecated in ['@VMOD', '@VMODULE', '@VPATH', '@VLIB_PATH'] { if flag.contains(deprecated) { p.error('${deprecated} had been deprecated, use @VROOT instead.') diff --git a/vlib/v/tests/struct_test.v b/vlib/v/tests/struct_test.v index 8ce1d08ee1..dd75858da7 100644 --- a/vlib/v/tests/struct_test.v +++ b/vlib/v/tests/struct_test.v @@ -23,10 +23,11 @@ struct User { } struct Foo { - @type string + typ string } -struct Empty {} +struct Empty { +} // We need to make sure that this compiles with all the reserved names. struct ReservedKeywords { @@ -35,7 +36,7 @@ struct ReservedKeywords { unix int error int malloc int - calloc int + calloc int free int panic int auto int @@ -61,10 +62,13 @@ fn test_empty_struct() { d := &Empty{} d2 := Empty{} println('&empty:') - println(d) // != voidptr(0) + println(d) // != voidptr(0) println('empty:') - println(d2) // empty struct print - println(sizeof(Empty)) // == 0 + println(d2) // empty struct print + println(sizeof( + Empty + ) + ) // == 0 } fn test_struct_levels() { @@ -98,23 +102,22 @@ fn test_struct_levels() { } fn test_struct_str() { - u := User{ - 'Bob',30} - println(u) // make sure the struct is printable +u := User{'Bob',30} + println(u) // make sure the struct is printable // assert u.str() == '{name:"Bob", age:30}' // QTODO } fn test_at() { foo := Foo{ - @type: 'test' + typ: 'test' // QTODO @type + //type: 'test' } - println(foo.@type) + println(foo.typ) } fn test_reserved_keywords() { // Make sure we can initialize them correctly using full syntax. - rk_holder := ReservedKeywords{ - 0,0,5,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,3} + rk_holder := ReservedKeywords{0,0,5,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,3} // Test a few as it'll take too long to test all. If it's initialized // correctly, other fields are also probably valid. assert rk_holder.unix == 5 @@ -126,7 +129,7 @@ fn test_reserved_keywords() { // Make sure partial initialization works too. assert rk_holder2.inline == 9 assert rk_holder2.volatile == 11 - assert rk_holder2.while == 0 // Zero value as not specified. + assert rk_holder2.while == 0 // Zero value as not specified. } struct User2 { @@ -142,15 +145,14 @@ fn test_mutable_fields() { struct Def { a int - b int=7 + b int = 7 } fn test_default_vals() { d := Def{} assert d.a == 0 assert d.b == 7 - d2 := Def{ - 10,20} + d2 := Def{10,20} assert d2.a == 10 assert d2.b == 20 } @@ -161,7 +163,7 @@ fn test_assoc_with_vars() { } merged := { def2 | - a:42 + a: 42 } assert merged.a == 42 assert merged.b == 7 @@ -184,8 +186,7 @@ fn test_assoc_with_constants() { again := { const_def | b: 22 } assert again.a == 100 assert again.b == 22 - */ - +*/ } struct AttrTest { @@ -197,13 +198,11 @@ pub: d int // public immmutable (readonly) pub mut: e int // public, but mutable only in parent module -__global: f int // public and mutable both inside and outside parent module } fn fooo() { - a := AttrTest{ - 1,2,3,4,5,6} + a := AttrTest{1,2,3,4,5,6} } /* @@ -228,16 +227,16 @@ fn test_fixed_field() { //} } */ - - struct Config { n int def int=10 } -fn foo_config(c Config) {} +fn foo_config(c Config) { +} -fn foo2(u User) {} +fn foo2(u User) { +} fn test_config() { foo_config({ @@ -249,3 +248,15 @@ fn test_config() { name: 'Peter' }) } + +struct City { + name string + population int +} + +struct Country { + capital City +} + +fn test_levels() { +}