From 24b40be54815f982698a0b788c38fb74a834868a Mon Sep 17 00:00:00 2001 From: Alexander Medvednikov Date: Sun, 24 Nov 2019 13:53:59 +0300 Subject: [PATCH] default struct vals --- vlib/clipboard/clipboard_linux.v | 3 ++- vlib/compiler/struct.v | 11 +++++++++++ vlib/compiler/table.v | 7 +++++++ vlib/os/os.v | 2 +- 4 files changed, 21 insertions(+), 2 deletions(-) diff --git a/vlib/clipboard/clipboard_linux.v b/vlib/clipboard/clipboard_linux.v index d56f5ec57d..46a77b288c 100644 --- a/vlib/clipboard/clipboard_linux.v +++ b/vlib/clipboard/clipboard_linux.v @@ -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) diff --git a/vlib/compiler/struct.v b/vlib/compiler/struct.v index 3d03c6ba90..4c1afd14a2 100644 --- a/vlib/compiler/struct.v +++ b/vlib/compiler/struct.v @@ -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 { diff --git a/vlib/compiler/table.v b/vlib/compiler/table.v index e38fe3e74d..5d41bc146d 100644 --- a/vlib/compiler/table.v +++ b/vlib/compiler/table.v @@ -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 diff --git a/vlib/os/os.v b/vlib/os/os.v index 0b81f2512c..2e12419f6f 100644 --- a/vlib/os/os.v +++ b/vlib/os/os.v @@ -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))