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

@ -1,6 +1,6 @@
/**********************************************************************
*
* f32 to string
* f32 to string
*
* Copyright (c) 2019-2020 Dario Deledda. All rights reserved.
* Use of this source code is governed by an MIT license
@ -9,11 +9,11 @@
* This file contains the f64 to string functions
*
* These functions are based on the work of:
* Publication:PLDI 2018: Proceedings of the 39th ACM SIGPLAN
* Conference on Programming Language Design and ImplementationJune 2018
* Publication:PLDI 2018: Proceedings of the 39th ACM SIGPLAN
* Conference on Programming Language Design and ImplementationJune 2018
* Pages 270282 https://doi.org/10.1145/3192366.3192369
*
* inspired by the Go version here:
* inspired by the Go version here:
* https://github.com/cespare/ryu/tree/ba56a33f39e3bbbfa409095d0f9ae168a595feea
*
**********************************************************************/
@ -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
}
@ -107,7 +107,7 @@ fn (d Dec64) get_string_64(neg bool, i_n_digit int) string {
mut x := 0
for x < (out_len-disp-1) {
buf[y - x] = `0` + byte(out%10)
out /= 10
out /= 10
i++
x++
}

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 {