default struct vals

pull/2873/head
Alexander Medvednikov 2019-11-24 13:53:59 +03:00
parent 7cc21be7de
commit 24b40be548
4 changed files with 21 additions and 2 deletions

View File

@ -35,6 +35,7 @@ fn C.DefaultScreen() int
fn C.RootWindow() voidptr
fn C.BlackPixel() voidptr
fn C.WhitePixel() voidptr
fn C.XFree()
struct C.XSelectionRequestEvent{
mut:
@ -329,7 +330,7 @@ fn read_property(d &Display, w Window, p Atom) Property {
ret := byteptr(0)
mut read_bytes := 1024
for {
if(ret != 0){
if ret != 0 {
C.XFree(ret)
}
XGetWindowProperty(d, w, p, 0, read_bytes, 0, C.AnyPropertyType, &actual_type, &actual_format, &nitems, &bytes_after, &ret)

View File

@ -131,7 +131,9 @@ fn (p mut Parser) struct_decl() {
}
mut did_gen_something := false
mut i := -1
for p.tok != .rcbr {
i++
if p.tok == .key_pub {
if is_pub_field {
p.error('structs can only have one `pub:`, all public fields have to be grouped')
@ -205,6 +207,15 @@ fn (p mut Parser) struct_decl() {
if is_atomic {
p.next()
}
// `a int = 4`
if p.tok == .assign {
p.next()
def_val_type, expr := p.tmp_expr()
if def_val_type != field_type {
p.error('expected `$field_type` but got `$def_val_type`')
}
p.table.add_default_val(i, typ.name, expr)
}
// [ATTR]
mut attr := ''
if p.tok == .lsbr {

View File

@ -105,6 +105,7 @@ mut:
is_c bool // `C.FILE`
enum_vals []string
gen_types []string
default_vals []string // `struct Foo { bar int = 2 }`
// This field is used for types that are not defined yet but are known to exist.
// It allows having things like `fn (f Foo) bar()` before `Foo` is defined.
// This information is needed in the first pass.
@ -441,6 +442,12 @@ fn (table mut Table) add_field(type_name, field_name, field_type string, is_mut
table.typesmap[type_name] = t
}
fn (table mut Table) add_default_val(idx int, type_name, val_expr string) {
mut t := table.typesmap[type_name]
t.default_vals[idx] = val_expr
table.typesmap[type_name] = t
}
fn (t &Type) has_field(name string) bool {
_ = t.find_field(name) or { return false }
return true

View File

@ -621,7 +621,7 @@ pub fn get_raw_line() string {
max_line_chars := 256
buf := &byte(malloc(max_line_chars*2))
if is_atty(0) > 0 {
h_input := C.GetStdHandle(C.STD_INPUT_HANDLE)
h_input := C.GetStdHandle(STD_INPUT_HANDLE)
mut nr_chars := u32(0)
C.ReadConsole(h_input, buf, max_line_chars * 2, voidptr(&nr_chars), 0)
return string_from_wide2(&u16(buf), int(nr_chars))