parser/cgen: default struct field values

pull/4264/head
Alexander Medvednikov 2020-04-06 14:45:28 +02:00
parent f099f90f50
commit 35fbac8d56
8 changed files with 23 additions and 18 deletions

View File

@ -23,7 +23,7 @@ module strconv
union Float64u {
mut:
f f64
u u64 = u64(0)
u u64
}
/**********************************************************************
@ -165,9 +165,9 @@ fn is_exp(x byte) bool {
// The structure is filled by parser, then given to converter.
pub struct PrepNumber {
pub mut:
negative bool=false // 0 if positive number, 1 if negative
exponent int=0 // power of 10 exponent
mantissa u64=u64(0) // integer mantissa
negative bool // 0 if positive number, 1 if negative
exponent int // power of 10 exponent
mantissa u64 // integer mantissa
}
/**********************************************************************
*

View File

@ -22,14 +22,14 @@ module ftoa
// dec32 is a floating decimal type representing m * 10^e.
struct Dec32 {
mut:
m u32 = u32(0)
m u32 = 0
e int = 0
}
// support union for convert f32 to u32
union Uf32 {
mut:
f f32 = f32(0)
f f32 = 0
u u32
}

View File

@ -28,14 +28,14 @@ mut:
// dec64 is a floating decimal type representing m * 10^e.
struct Dec64 {
mut:
m u64 = u64(0)
m u64 = 0
e int = 0
}
// support union for convert f64 to u64
union Uf64 {
mut:
f f64 = f64(0)
f f64 = 0
u u64
}

View File

@ -8,7 +8,7 @@ mut:
buf []byte
pub mut:
len int
initial_size int=1
initial_size int = 1
}
pub fn new_builder(initial_size int) Builder {

View File

@ -107,7 +107,7 @@ pub:
name string
pos token.Position
comment Comment
default_expr Expr
default_expr string // token literal //Expr
mut:
typ table.Type
}

View File

@ -1900,7 +1900,7 @@ fn (g mut Gen) struct_init(it ast.StructInit) {
continue
}
field_name := c_name(field.name)
zero := g.type_default(field.typ)
zero := if field.default_val != '' { field.default_val } else { g.type_default(field.typ) }
g.writeln('\t.$field_name = $zero,') // zer0')
}
}

View File

@ -1540,11 +1540,13 @@ fn (p mut Parser) struct_decl() ast.StructDecl {
println('XXXX' + s.str())
}
*/
mut default_expr := ast.Expr{}
mut default_expr := '' // ast.Expr{}
if p.tok.kind == .assign {
// Default value
p.next()
default_expr = p.expr(0)
default_expr = p.tok.lit
p.expr(0)
//default_expr = p.expr(0)
}
if p.tok.kind == .comment {
comment = p.comment()
@ -1554,10 +1556,12 @@ fn (p mut Parser) struct_decl() ast.StructDecl {
pos: field_pos
typ: typ
comment: comment
default_expr: default_expr
}
fields << table.Field{
name: field_name
typ: typ
default_val: default_expr
}
// println('struct field $ti.name $field_name')
}

View File

@ -557,6 +557,7 @@ pub:
name string
mut:
typ Type
default_val string
}
pub struct Array {