From 29564ed63dda02979e8ec6cf27b069419412b8d1 Mon Sep 17 00:00:00 2001 From: Alexander Medvednikov Date: Thu, 9 Jan 2020 01:39:47 +0100 Subject: [PATCH] interface fix --- examples/tetris/tetris.v | 20 +++++------ vlib/compiler/aparser.v | 5 ++- vlib/compiler/enum.v | 1 + vlib/compiler/expression.v | 9 +++++ vlib/v/parser/parser.v | 4 +-- vlib/v/table/table.v | 5 ++- vlib/v/types/types.v | 70 ++++++++++++++++++-------------------- 7 files changed, 61 insertions(+), 53 deletions(-) diff --git a/examples/tetris/tetris.v b/examples/tetris/tetris.v index 61b3f8e580..49a571fa44 100644 --- a/examples/tetris/tetris.v +++ b/examples/tetris/tetris.v @@ -143,23 +143,21 @@ fn main() { window_title: 'V Tetris' window_user_ptr: game }) - ft: 0 + ft: freetype.new_context(gg.Cfg{ + width: WinWidth + height: WinHeight + use_ortho: true + font_size: 18 + scale: 2 + window_user_ptr: 0 + }) } game.gg.window.set_user_ptr(game) // TODO remove this when `window_user_ptr:` works game.init_game() game.gg.window.onkeydown(key_down) go game.run() // Run the game loop in a new thread gg.clear(BackgroundColor) - // Try to load font - game.ft = freetype.new_context(gg.Cfg{ - width: WinWidth - height: WinHeight - use_ortho: true - font_size: 18 - scale: 2 - window_user_ptr: 0 - }) - game.font_loaded = (game.ft != 0 ) + game.font_loaded = game.ft != 0 for { gg.clear(BackgroundColor) game.draw_scene() diff --git a/vlib/compiler/aparser.v b/vlib/compiler/aparser.v index 4cd3f9de9e..a21af38ab3 100644 --- a/vlib/compiler/aparser.v +++ b/vlib/compiler/aparser.v @@ -2112,6 +2112,9 @@ fn (p mut Parser) dot(str_typ_ string, method_ph int) string { // Is the next token `=`, `+=` etc? (Are we modifying the field?) next := p.peek() modifying := next.is_assign() || next == .inc || next == .dec || (field.typ.starts_with('array_') && next == .left_shift) + if modifying { + p.expected_type = field.typ + } if !p.builtin_mod && !p.pref.translated && modifying && p.has_immutable_field { f := p.first_immutable_field p.error_with_token_index('cannot modify immutable field `$f.name` (type `$f.parent_fn`)\n' + 'declare the field with `mut:` @@ -2705,7 +2708,7 @@ fn (p mut Parser) array_init() string { // vals.len == 0 { if exp_array { type_expected := p.expected_type[6..].replace('ptr_', '&') - p.error('no need to specify the full array type here, use `[]` instead of `[]$type_expected`') + p.warn('no need to specify the full array type here, use `[]` instead of `[]$type_expected`') } typ = p.get_type() } diff --git a/vlib/compiler/enum.v b/vlib/compiler/enum.v index bd421adf68..ef9fa5d85e 100644 --- a/vlib/compiler/enum.v +++ b/vlib/compiler/enum.v @@ -73,6 +73,7 @@ fn (p mut Parser) enum_decl(no_name bool) { } if p.tok == .comma { p.next() + p.fremove_last() } p.fgen_nl() val++ diff --git a/vlib/compiler/expression.v b/vlib/compiler/expression.v index a081cc29ce..8712205cb1 100644 --- a/vlib/compiler/expression.v +++ b/vlib/compiler/expression.v @@ -50,6 +50,15 @@ fn (p mut Parser) bool_expression() string { if p.inside_return_expr && p.expected_type.contains('_MulRet_') { //is_ret { // return a,b hack TODO expected = p.expected_type } + // `window.widget = button`, widget is an interface + if expected != typ && expected.ends_with('er') && expected.contains('I') { + tt := typ.replace('*', '_ptr') + p.cgen.set_placeholder(start_ph, + '($expected) { ._interface_idx = _${expected}_${tt}_index, ._object = ' ) + p.gen('}') + //p.satisfies_interface(expected, typ, true) + } + // e.g. `return BinaryExpr{}` in a function expecting `Expr` if expected != typ && expected in p.table.sum_types { // TODO perf //p.warn('SUM CAST exp=$expected typ=$typ p.exp=$p.expected_type') T := p.table.find_type(typ) diff --git a/vlib/v/parser/parser.v b/vlib/v/parser/parser.v index 552d92647f..e00b14b550 100644 --- a/vlib/v/parser/parser.v +++ b/vlib/v/parser/parser.v @@ -425,7 +425,7 @@ fn (p mut Parser) dot_expr(left ast.Expr, ti types.TypeIdent) (ast.Expr,types.Ty field_name := p.check_name() println('# $ti.name $ti.idx - $field_name') if ti.kind != .void { - println('#### void type in dot_expr - field: $field_name') + p.warn('#### void type in dot_expr - field: $field_name') } struc := p.table.types[ti.idx] as types.Struct // Method call @@ -445,7 +445,7 @@ fn (p mut Parser) dot_expr(left ast.Expr, ti types.TypeIdent) (ast.Expr,types.Ty return node,types.int_ti } if !p.table.struct_has_field(struc, field_name) { - // t := + // t := p.error('type `$struc.name` has no field `$field_name`') } /* diff --git a/vlib/v/table/table.v b/vlib/v/table/table.v index 3f80d5612f..b2ded05e8e 100644 --- a/vlib/v/table/table.v +++ b/vlib/v/table/table.v @@ -180,10 +180,9 @@ pub fn (t mut Table) register_method(ti types.TypeIdent, new_fn Fn) bool { } t.types[ti.idx] = struc */ + println('register method `$new_fn.name` struct=$ti.name ') - println('##### $ti.idx - $t.methods.len') - t.methods[ti.idx] << new_fn return true } @@ -246,7 +245,7 @@ pub fn (t mut Table) register_type(typ types.Type, name string, idx int) { } t.type_idxs[name] = idx t.types << typ - t.methods << []Fn + t.methods << []Fn // TODO [] breaks V } pub fn (t mut Table) register_struct(typ types.Struct) int { diff --git a/vlib/v/types/types.v b/vlib/v/types/types.v index aa16a54a2a..894acaf79c 100644 --- a/vlib/v/types/types.v +++ b/vlib/v/types/types.v @@ -25,35 +25,35 @@ pub const ( pub enum Kind { placeholder - void, - voidptr, - charptr, - byteptr, - const_, - enum_, - struct_, - int, - i8, - i16, - i64, - byte, - u16, - u32, - u64, - f32, - f64, - string, - char, - bool, - array, - array_fixed, - map, - multi_return, + void + voidptr + charptr + byteptr + const_ + enum_ + struct_ + int + i8 + i16 + i64 + byte + u16 + u32 + u64 + f32 + f64 + string + char + bool + array + array_fixed + map + multi_return variadic } -pub type Type = Placeholder | Primitive | Const | Enum | Struct | Int | Float | - String | Bool | Array | ArrayFixed | Map | MultiReturn | Variadic +pub type Type = Placeholder | Primitive | Const | Enum | Struct | Int | Float | +String | Bool | Array | ArrayFixed | Map | MultiReturn | Variadic pub struct TypeIdent { pub: @@ -228,7 +228,6 @@ pub: name string // kind Kind } - // Void | Voidptr | Charptr | Byteptr pub struct Primitive { pub: @@ -266,7 +265,7 @@ pub: pub struct Int { pub: - idx int + idx int bit_size u32 is_unsigned bool } @@ -335,27 +334,26 @@ pub: pub fn (t Primitive) str() string { s := match t.kind { - .void { + .void{ 'void' } - .voidptr { + .voidptr{ 'voidptr' } - .charptr { + .charptr{ 'charptr' } - .byteptr { + .byteptr{ 'byteptr' } - .char { + .char{ 'char' } - .byte { + .byte{ 'byte' } else { - 'unknown' - } + 'unknown'} } return s }