From f57a651e3bbd0afc6c2385b7771e911ef7d7a162 Mon Sep 17 00:00:00 2001 From: Joe Conigliaro Date: Mon, 2 Mar 2020 20:53:38 +1100 Subject: [PATCH] v2: match expr fixes & tmp typeof skip --- vlib/v/ast/ast.v | 8 +++++++- vlib/v/checker/checker.v | 14 +++++++++++++- vlib/v/parser/parser.v | 17 +++++++++++------ 3 files changed, 31 insertions(+), 8 deletions(-) diff --git a/vlib/v/ast/ast.v b/vlib/v/ast/ast.v index be8a813429..0140dadc10 100644 --- a/vlib/v/ast/ast.v +++ b/vlib/v/ast/ast.v @@ -12,7 +12,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 +ConcatExpr | TypeName pub type Stmt = VarDecl | GlobalDecl | FnDecl | Return | Module | Import | ExprStmt | ForStmt | StructDecl | ForCStmt | ForInStmt | CompIf | ConstDecl | Attr | BranchStmt | @@ -27,6 +27,12 @@ pub struct StructType { pub struct ArrayType {} +pub struct TypeName { +pub: + name string + typ table.Type +} + // | IncDecStmt k // Stand-alone expression in a statement list. pub struct ExprStmt { diff --git a/vlib/v/checker/checker.v b/vlib/v/checker/checker.v index f4ac07ed73..fefed75801 100644 --- a/vlib/v/checker/checker.v +++ b/vlib/v/checker/checker.v @@ -175,6 +175,10 @@ fn (c mut Checker) check_assign_expr(assign_expr ast.AssignExpr) { pub fn (c mut Checker) call_expr(call_expr ast.CallExpr) table.Type { fn_name := call_expr.name + // TODO: impl typeof properly (probably not going to be a fn call) + if fn_name == 'typeof' { + return table.string_type + } mut found := false // start hack: until v1 is fixed and c definitions are added for these if fn_name == 'C.calloc' { @@ -553,6 +557,9 @@ pub fn (c mut Checker) expr(node ast.Expr) table.Type { ast.StructInit { return c.check_struct_init(it) } + ast.TypeName { + return it.typ + } /* ast.UnaryExpr { c.expr(it.left) @@ -656,7 +663,12 @@ pub fn (c mut Checker) match_expr(node mut ast.MatchExpr) table.Type { if i < node.match_exprs.len { match_expr := node.match_exprs[i] c.expected_type = t - c.expr(match_expr) + typ := c.expr(match_expr) + typ_sym := c.table.get_type_symbol(typ) + // TODO: + if typ_sym.kind == .sum_type { + + } } c.stmts(block.stmts) // If the last statement is an expression, return its type diff --git a/vlib/v/parser/parser.v b/vlib/v/parser/parser.v index a109376ea7..37dfe5c870 100644 --- a/vlib/v/parser/parser.v +++ b/vlib/v/parser/parser.v @@ -1732,16 +1732,21 @@ fn (p mut Parser) match_expr() ast.Expr { mut match_exprs := []ast.Expr // mut return_type := table.void_type for { + p.open_scope() // Sum type match if p.tok.kind == .name && (p.tok.lit[0].is_capital() || p.peek_tok.kind == .dot) { // if sym.kind == .sum_type { // p.warn('is sum') - // p.parse_type() - p.check_name() - if p.tok.kind == .dot { - p.check(.dot) - p.check_name() + typ := p.parse_type() + typ_sym := p.table.get_type_symbol(typ) + match_exprs << ast.TypeName{ + name: typ_sym.name + typ: typ } + p.scope.register_var(ast.VarDecl{ + name: 'it' + typ: typ + }) } else { // Expression match @@ -1778,7 +1783,7 @@ fn (p mut Parser) match_expr() ast.Expr { } } */ - + p.close_scope() if p.tok.kind == .rcbr { break }