From b290efa394a931b962d46ecd48152f4cdf6ce2fa Mon Sep 17 00:00:00 2001 From: Alexander Medvednikov Date: Thu, 19 Mar 2020 12:15:39 +0100 Subject: [PATCH] ast: TypeOf --- vlib/v/ast/ast.v | 7 ++++++- vlib/v/checker/checker.v | 3 +++ vlib/v/gen/cgen.v | 3 +++ vlib/v/gen/jsgen.v | 12 +++++++----- vlib/v/parser/parser.v | 9 +++++++++ vlib/v/token/token.v | 4 ++-- 6 files changed, 30 insertions(+), 8 deletions(-) diff --git a/vlib/v/ast/ast.v b/vlib/v/ast/ast.v index 3366f593bb..d1df59a823 100644 --- a/vlib/v/ast/ast.v +++ b/vlib/v/ast/ast.v @@ -14,7 +14,7 @@ pub type Expr = InfixExpr | IfExpr | StringLiteral | IntegerLiteral | CharLitera FloatLiteral | Ident | CallExpr | BoolLiteral | StructInit | ArrayInit | SelectorExpr | PostfixExpr | AssignExpr | PrefixExpr | MethodCallExpr | IndexExpr | RangeExpr | MatchExpr | CastExpr | EnumVal | Assoc | SizeOf | None | MapInit | IfGuardExpr | ParExpr | OrExpr | -ConcatExpr | Type | AsCast +ConcatExpr | Type | AsCast | TypeOf pub type Stmt = GlobalDecl | FnDecl | Return | Module | Import | ExprStmt | ForStmt | StructDecl | ForCStmt | ForInStmt | CompIf | ConstDecl | Attr | BranchStmt | @@ -608,6 +608,11 @@ pub: type_name string } +pub struct TypeOf { +pub: + expr Expr +} + pub struct LineComment { pub: text string diff --git a/vlib/v/checker/checker.v b/vlib/v/checker/checker.v index 3f9acd3bbb..737843b129 100644 --- a/vlib/v/checker/checker.v +++ b/vlib/v/checker/checker.v @@ -741,6 +741,9 @@ pub fn (c mut Checker) expr(node ast.Expr) table.Type { ast.Type { return it.typ } + ast.TypeOf { + return table.string_type + } /* ast.UnaryExpr { c.expr(it.left) diff --git a/vlib/v/gen/cgen.v b/vlib/v/gen/cgen.v index c7429fe425..d4561c5ca4 100644 --- a/vlib/v/gen/cgen.v +++ b/vlib/v/gen/cgen.v @@ -887,6 +887,9 @@ fn (g mut Gen) expr(node ast.Expr) { g.write('_type_idx_') g.write(g.typ(it.typ)) } + ast.TypeOf { + g.write('tos3("TYPEOF_TODO")') + } else { // #printf("node=%d\n", node.typ); println(term.red('cgen.expr(): bad node ' + typeof(node))) diff --git a/vlib/v/gen/jsgen.v b/vlib/v/gen/jsgen.v index 795a51afb8..fc3ea73394 100644 --- a/vlib/v/gen/jsgen.v +++ b/vlib/v/gen/jsgen.v @@ -58,11 +58,10 @@ fn (g mut JsGen) stmt(node ast.Stmt) { g.writeln(';') } ast.AssignStmt { - if it.left.len > it.right.len { - // TODO: multi return - } + if it.left.len > it.right.len {} + // TODO: multi return else { - for i,ident in it.left { + for i, ident in it.left { var_info := ident.var_info() var_type_sym := g.table.get_type_symbol(var_info.typ) val := it.right[i] @@ -90,6 +89,8 @@ fn (g mut JsGen) stmt(node ast.Stmt) { } ast.ExprStmt { g.expr(it.expr) + } + /* match it.expr { // no ; after an if expression ast.IfExpr {} @@ -97,7 +98,8 @@ fn (g mut JsGen) stmt(node ast.Stmt) { g.writeln(';') } } - } + */ + else { verror('jsgen.stmt(): bad node') } diff --git a/vlib/v/parser/parser.v b/vlib/v/parser/parser.v index 5b7cb6f08a..2b4a0f3905 100644 --- a/vlib/v/parser/parser.v +++ b/vlib/v/parser/parser.v @@ -765,6 +765,15 @@ pub fn (p mut Parser) expr(precedence int) ast.Expr { } } + .key_typeof { + p.next() + p.check(.lpar) + expr := p.expr(0) + p.check(.rpar) + node = ast.TypeOf{ + expr: expr + } + } // Map `{"age": 20}` or `{ x | foo:bar, a:10 }` .lcbr { p.next() diff --git a/vlib/v/token/token.v b/vlib/v/token/token.v index 4aaf99a2fb..a6de64dda0 100644 --- a/vlib/v/token/token.v +++ b/vlib/v/token/token.v @@ -114,7 +114,7 @@ pub enum Kind { key_switch key_true key_type - // typeof + key_typeof key_orelse key_union key_pub @@ -229,7 +229,7 @@ fn build_token_str() []string { s[Kind.key_import] = 'import' s[Kind.key_embed] = 'embed' s[Kind.key_unsafe] = 'unsafe' - // Kinds[key_typeof] = 'typeof' + s[Kind.key_typeof] = 'typeof' s[Kind.key_enum] = 'enum' s[Kind.key_interface] = 'interface' s[Kind.key_pub] = 'pub'