From 9861b24bc397462d24d5a5063eb4fcd4deadca90 Mon Sep 17 00:00:00 2001 From: Alexander Medvednikov Date: Tue, 7 Jan 2020 16:06:37 +0100 Subject: [PATCH] struct field check + enable v2 again --- vlib/compiler/main.v | 24 ++++-------------------- vlib/v/gen/cgen.v | 2 +- vlib/v/parser/parser.v | 19 +++++++++++++++++-- vlib/v/types/types.v | 8 ++++++++ 4 files changed, 30 insertions(+), 23 deletions(-) diff --git a/vlib/compiler/main.v b/vlib/compiler/main.v index 6a9ee2ffed..26b75c1f26 100644 --- a/vlib/compiler/main.v +++ b/vlib/compiler/main.v @@ -8,12 +8,10 @@ import ( os.cmdline strings filepath - //compiler.x64 - // v.gen.x64 - //v.types - // v.table - // v.parser - // v.gen + v.gen.x64 + v.table + v.parser + v.gen time ) @@ -382,8 +380,6 @@ pub fn (v mut V) compile() { v.cc() } -pub fn (v &V) compile2() {} -/* pub fn (v mut V) compile2() { if os.user_os() != 'windows' && v.pref.ccompiler == 'msvc' { verror('Cannot build with msvc on ${os.user_os()}') @@ -418,10 +414,7 @@ pub fn (v mut V) compile2() { v.cc() } -*/ -pub fn (v &V) compile_x64() {} -/* pub fn (v mut V) compile_x64() { $if !linux { println('v -x64 can only generate Linux binaries for now') @@ -436,16 +429,7 @@ pub fn (v mut V) compile_x64() { println('PARSE: ${time.ticks() - ticks}ms') x64.gen(files, v.out_name) println('x64 GEN: ${time.ticks() - ticks}ms') - /* - for f in v.files { - v.parse(f, .decl) - } - for f in v.files { - v.parse(f, .main) - } - */ } -*/ fn (v mut V) generate_init() { $if js { diff --git a/vlib/v/gen/cgen.v b/vlib/v/gen/cgen.v index 1c3b9c3bfe..d3186a35b9 100644 --- a/vlib/v/gen/cgen.v +++ b/vlib/v/gen/cgen.v @@ -4,7 +4,7 @@ import ( strings v.ast v.table - v.types + // v.types term ) diff --git a/vlib/v/parser/parser.v b/vlib/v/parser/parser.v index 5911e46784..a97821ef27 100644 --- a/vlib/v/parser/parser.v +++ b/vlib/v/parser/parser.v @@ -269,6 +269,10 @@ pub fn (p mut Parser) name_expr() (ast.Expr,types.TypeIdent) { p.next() p.check(.dot) } + else if p.tok.lit in ['strings'] { + p.next() + p.check(.dot) + } // fn call if p.peek_tok.kind == .lpar { x,ti2 := p.call_expr() // TODO `node,typ :=` should work @@ -363,7 +367,7 @@ pub fn (p mut Parser) expr(precedence int) (ast.Expr,types.TypeIdent) { node = p.assign_expr(node) } else if p.tok.kind == .dot { - node,ti = p.dot_expr(node) + node,ti = p.dot_expr(node, ti) } else if p.tok.kind == .lsbr { node,ti = p.index_expr(node) @@ -416,9 +420,20 @@ fn (p mut Parser) index_expr(left ast.Expr) (ast.Expr,types.TypeIdent) { return node,ti } -fn (p mut Parser) dot_expr(left ast.Expr) (ast.Expr,types.TypeIdent) { +fn (p mut Parser) dot_expr(left ast.Expr, ti types.TypeIdent) (ast.Expr,types.TypeIdent) { p.next() field_name := p.check_name() + typ := p.table.types[ti.idx] as types.Struct + mut ok := false + for field in typ.fields { + if field.name == field_name { + ok = true + break + } + } + if !ok { + p.error('type `$typ.name` has no field or method `$field_name`') + } // Method call if p.tok.kind == .lpar { p.next() diff --git a/vlib/v/types/types.v b/vlib/v/types/types.v index 273fa861a8..189b77e63b 100644 --- a/vlib/v/types/types.v +++ b/vlib/v/types/types.v @@ -227,6 +227,7 @@ pub: idx int name string fields []Field + // methods } pub struct Field { @@ -365,6 +366,13 @@ pub fn (t Variadic) str() string { return 'variadic_$t.ti.kind.str()' } +/* +pub fn (s &Struct) has_field(name string) bool { + +} +*/ + + pub const ( void_type = Void{} voidptr_type = Voidptr{}