1
0
mirror of https://github.com/vlang/v.git synced 2023-08-10 21:13:21 +03:00

parser/cgen: default struct field values

This commit is contained in:
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 { union Float64u {
mut: mut:
f f64 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. // The structure is filled by parser, then given to converter.
pub struct PrepNumber { pub struct PrepNumber {
pub mut: pub mut:
negative bool=false // 0 if positive number, 1 if negative negative bool // 0 if positive number, 1 if negative
exponent int=0 // power of 10 exponent exponent int // power of 10 exponent
mantissa u64=u64(0) // integer mantissa mantissa u64 // integer mantissa
} }
/********************************************************************** /**********************************************************************
* *

View File

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

View File

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

View File

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

View File

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

View File

@ -1900,7 +1900,7 @@ fn (g mut Gen) struct_init(it ast.StructInit) {
continue continue
} }
field_name := c_name(field.name) 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') 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()) println('XXXX' + s.str())
} }
*/ */
mut default_expr := ast.Expr{} mut default_expr := '' // ast.Expr{}
if p.tok.kind == .assign { if p.tok.kind == .assign {
// Default value // Default value
p.next() 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 { if p.tok.kind == .comment {
comment = p.comment() comment = p.comment()
@ -1554,10 +1556,12 @@ fn (p mut Parser) struct_decl() ast.StructDecl {
pos: field_pos pos: field_pos
typ: typ typ: typ
comment: comment comment: comment
default_expr: default_expr
} }
fields << table.Field{ fields << table.Field{
name: field_name name: field_name
typ: typ typ: typ
default_val: default_expr
} }
// println('struct field $ti.name $field_name') // println('struct field $ti.name $field_name')
} }

View File

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