vfmt: lots of fixes; cmd/v: -keepc;

pull/4322/head
Alexander Medvednikov 2020-04-10 00:30:43 +02:00
parent 32550c6d69
commit 9b2511133d
4 changed files with 66 additions and 37 deletions

View File

@ -130,6 +130,7 @@ fn parse_args(args []string) (&pref.Preferences, string) {
'-obfuscate' { res.obfuscate = true } '-obfuscate' { res.obfuscate = true }
'-translated' { res.translated = true } '-translated' { res.translated = true }
'-showcc' { res.show_cc = true } '-showcc' { res.show_cc = true }
'-keepc' { res.is_keep_c = true }
//'-x64' { res.translated = true } //'-x64' { res.translated = true }
'-os' { '-os' {
//TODO Remove `tmp` variable when it doesn't error out in C. //TODO Remove `tmp` variable when it doesn't error out in C.

View File

@ -402,6 +402,9 @@ fn (f mut Fmt) struct_decl(node ast.StructDecl) {
f.write('\t$field.name ') f.write('\t$field.name ')
f.write(strings.repeat(` `, max - field.name.len)) f.write(strings.repeat(` `, max - field.name.len))
f.write(f.type_to_str(field.typ)) f.write(f.type_to_str(field.typ))
if field.default_expr != '' {
f.write(' = $field.default_expr')
}
// f.write('// $field.pos.line_nr') // f.write('// $field.pos.line_nr')
if field.comment.text != '' && field.comment.pos.line_nr == field.pos.line_nr { if field.comment.text != '' && field.comment.pos.line_nr == field.pos.line_nr {
// Same line comment // Same line comment
@ -605,13 +608,13 @@ fn (f mut Fmt) expr(node ast.Expr) {
f.write(it.field) f.write(it.field)
} }
ast.SizeOf { ast.SizeOf {
f.writeln('sizeof(') f.write('sizeof(')
if it.type_name != '' { if it.type_name != '' {
f.writeln(it.type_name) f.write(it.type_name)
} else { } else {
f.writeln(f.type_to_str(it.typ)) f.write(f.type_to_str(it.typ))
} }
f.writeln(')') f.write(')')
} }
ast.StringLiteral { ast.StringLiteral {
if it.val.contains("'") { if it.val.contains("'") {
@ -641,10 +644,23 @@ fn (f mut Fmt) expr(node ast.Expr) {
} }
ast.StructInit { ast.StructInit {
type_sym := f.table.get_type_symbol(it.typ) type_sym := f.table.get_type_symbol(it.typ)
name := short_module(type_sym.name).replace(f.cur_mod + '.', '') // TODO f.type_to_str? mut 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 name == 'void' {
if it.fields.len == 0 { name = ''
}
if it.fields.len == 0 && it.exprs.len == 0 {
// `Foo{}` on one line if there are no fields
f.write('$name{}') 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 { } else {
f.writeln('$name{') f.writeln('$name{')
f.indent++ f.indent++

View File

@ -21,9 +21,9 @@ fn (p mut Parser) hash() ast.HashStmt {
// #flag linux -lm // #flag linux -lm
mut flag := val[5..] mut flag := val[5..]
// expand `@VROOT` to its absolute path // expand `@VROOT` to its absolute path
/*
if flag.contains('@VROOT') { 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 { if vmod_file_location.vmod_file.len == 0 {
// There was no actual v.mod file found. // There was no actual v.mod file found.
p.error_with_token_index('To use @VROOT, you need' + 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) p.cur_tok_index() - 1)
} }
flag = flag.replace('@VROOT', vmod_file_location.vmod_folder ) 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'] { for deprecated in ['@VMOD', '@VMODULE', '@VPATH', '@VLIB_PATH'] {
if flag.contains(deprecated) { if flag.contains(deprecated) {
p.error('${deprecated} had been deprecated, use @VROOT instead.') p.error('${deprecated} had been deprecated, use @VROOT instead.')

View File

@ -23,10 +23,11 @@ struct User {
} }
struct Foo { struct Foo {
@type string typ string
} }
struct Empty {} struct Empty {
}
// We need to make sure that this compiles with all the reserved names. // We need to make sure that this compiles with all the reserved names.
struct ReservedKeywords { struct ReservedKeywords {
@ -35,7 +36,7 @@ struct ReservedKeywords {
unix int unix int
error int error int
malloc int malloc int
calloc int calloc int
free int free int
panic int panic int
auto int auto int
@ -61,10 +62,13 @@ fn test_empty_struct() {
d := &Empty{} d := &Empty{}
d2 := Empty{} d2 := Empty{}
println('&empty:') println('&empty:')
println(d) // != voidptr(0) println(d) // != voidptr(0)
println('empty:') println('empty:')
println(d2) // empty struct print println(d2) // empty struct print
println(sizeof(Empty)) // == 0 println(sizeof(
Empty
)
) // == 0
} }
fn test_struct_levels() { fn test_struct_levels() {
@ -98,23 +102,22 @@ fn test_struct_levels() {
} }
fn test_struct_str() { fn test_struct_str() {
u := User{ u := User{'Bob',30}
'Bob',30} println(u) // make sure the struct is printable
println(u) // make sure the struct is printable
// assert u.str() == '{name:"Bob", age:30}' // QTODO // assert u.str() == '{name:"Bob", age:30}' // QTODO
} }
fn test_at() { fn test_at() {
foo := Foo{ foo := Foo{
@type: 'test' typ: 'test' // QTODO @type
//type: 'test'
} }
println(foo.@type) println(foo.typ)
} }
fn test_reserved_keywords() { fn test_reserved_keywords() {
// Make sure we can initialize them correctly using full syntax. // Make sure we can initialize them correctly using full syntax.
rk_holder := ReservedKeywords{ 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}
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 // Test a few as it'll take too long to test all. If it's initialized
// correctly, other fields are also probably valid. // correctly, other fields are also probably valid.
assert rk_holder.unix == 5 assert rk_holder.unix == 5
@ -126,7 +129,7 @@ fn test_reserved_keywords() {
// Make sure partial initialization works too. // Make sure partial initialization works too.
assert rk_holder2.inline == 9 assert rk_holder2.inline == 9
assert rk_holder2.volatile == 11 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 { struct User2 {
@ -142,15 +145,14 @@ fn test_mutable_fields() {
struct Def { struct Def {
a int a int
b int=7 b int = 7
} }
fn test_default_vals() { fn test_default_vals() {
d := Def{} d := Def{}
assert d.a == 0 assert d.a == 0
assert d.b == 7 assert d.b == 7
d2 := Def{ d2 := Def{10,20}
10,20}
assert d2.a == 10 assert d2.a == 10
assert d2.b == 20 assert d2.b == 20
} }
@ -161,7 +163,7 @@ fn test_assoc_with_vars() {
} }
merged := { merged := {
def2 | def2 |
a:42 a: 42
} }
assert merged.a == 42 assert merged.a == 42
assert merged.b == 7 assert merged.b == 7
@ -184,8 +186,7 @@ fn test_assoc_with_constants() {
again := { const_def | b: 22 } again := { const_def | b: 22 }
assert again.a == 100 assert again.a == 100
assert again.b == 22 assert again.b == 22
*/ */
} }
struct AttrTest { struct AttrTest {
@ -197,13 +198,11 @@ pub:
d int // public immmutable (readonly) d int // public immmutable (readonly)
pub mut: pub mut:
e int // public, but mutable only in parent module e int // public, but mutable only in parent module
__global:
f int // public and mutable both inside and outside parent module f int // public and mutable both inside and outside parent module
} }
fn fooo() { fn fooo() {
a := AttrTest{ a := AttrTest{1,2,3,4,5,6}
1,2,3,4,5,6}
} }
/* /*
@ -228,16 +227,16 @@ fn test_fixed_field() {
//} //}
} }
*/ */
struct Config { struct Config {
n int n int
def int=10 def int=10
} }
fn foo_config(c Config) {} fn foo_config(c Config) {
}
fn foo2(u User) {} fn foo2(u User) {
}
fn test_config() { fn test_config() {
foo_config({ foo_config({
@ -249,3 +248,15 @@ fn test_config() {
name: 'Peter' name: 'Peter'
}) })
} }
struct City {
name string
population int
}
struct Country {
capital City
}
fn test_levels() {
}