From 831be437408f732bb634e56d2013dbfdd412e086 Mon Sep 17 00:00:00 2001 From: Alexander Medvednikov Date: Sat, 28 Mar 2020 14:38:16 +0100 Subject: [PATCH] checker: check ! operator --- vlib/v/ast/ast.v | 19 ++++--------------- vlib/v/checker/checker.v | 3 +++ vlib/v/parser/parser.v | 17 ++++++++++------- 3 files changed, 17 insertions(+), 22 deletions(-) diff --git a/vlib/v/ast/ast.v b/vlib/v/ast/ast.v index c868dc599f..b447463116 100644 --- a/vlib/v/ast/ast.v +++ b/vlib/v/ast/ast.v @@ -319,18 +319,6 @@ mut: right_type table.Type } -/* -// renamed to PrefixExpr -pub struct UnaryExpr { -pub: -// tok_kind token.Kind -// op BinaryOp - op token.Kind - left Expr -} -*/ - - pub struct PostfixExpr { pub: op token.Kind @@ -342,6 +330,7 @@ pub struct PrefixExpr { pub: op token.Kind right Expr + pos token.Position } pub struct IndexExpr { @@ -528,9 +517,9 @@ pub struct DeferStmt { pub: stmts []Stmt mut: - // TODO: handle this differently - // v1 excludes non current os ifdefs so - // the defer's never get added in the first place +// TODO: handle this differently +// v1 excludes non current os ifdefs so +// the defer's never get added in the first place ifdef string } diff --git a/vlib/v/checker/checker.v b/vlib/v/checker/checker.v index be56d462e5..e7f79c2675 100644 --- a/vlib/v/checker/checker.v +++ b/vlib/v/checker/checker.v @@ -746,6 +746,9 @@ pub fn (c mut Checker) expr(node ast.Expr) table.Type { if it.op == .mul && table.type_is_ptr(right_type) { return table.type_deref(right_type) } + if it.op == .not && right_type != table.bool_type_idx { + c.error('! operator can only be used with bool types', it.pos) + } return right_type } ast.None { diff --git a/vlib/v/parser/parser.v b/vlib/v/parser/parser.v index 9bb7362c68..62bef84851 100644 --- a/vlib/v/parser/parser.v +++ b/vlib/v/parser/parser.v @@ -52,7 +52,7 @@ pub fn parse_stmt(text string, table &table.Table, scope &ast.Scope) ast.Stmt { pref: &pref.Preferences{} scope: scope // scope: &ast.Scope{start_pos: 0, parent: 0} - + } p.init_parse_fns() p.read_first_token() @@ -70,13 +70,14 @@ pub fn parse_file(path string, table &table.Table, comments_mode scanner.Comment scanner: scanner.new_scanner_file(path, comments_mode) table: table file_name: path - pref: pref //&pref.Preferences{} + pref: pref // &pref.Preferences{} + scope: &ast.Scope{ start_pos: 0 parent: 0 } // comments_mode: comments_mode - + } p.read_first_token() // p.scope = &ast.Scope{start_pos: p.tok.position(), parent: 0} @@ -687,7 +688,7 @@ pub fn (p mut Parser) name_expr() ast.Expr { p.expr_mod = '' return ast.EnumVal{ enum_name: enum_name // lp.prepend_mod(enum_name) - + val: val pos: p.tok.position() mod: mod @@ -777,7 +778,7 @@ pub fn (p mut Parser) expr(precedence int) ast.Expr { node = ast.SizeOf{ typ: sizeof_type // type_name: type_name - + } } .key_typeof { @@ -904,6 +905,7 @@ pub fn (p mut Parser) expr(precedence int) ast.Expr { } fn (p mut Parser) prefix_expr() ast.PrefixExpr { + pos := p.tok.position() op := p.tok.kind if op == .amp { p.is_amp = true @@ -914,6 +916,7 @@ fn (p mut Parser) prefix_expr() ast.PrefixExpr { return ast.PrefixExpr{ op: op right: right + pos: pos } } @@ -1045,7 +1048,7 @@ fn (p mut Parser) infix_expr(left ast.Expr) ast.Expr { left: left right: right // right_type: typ - + op: op pos: pos } @@ -1450,7 +1453,7 @@ fn (p mut Parser) const_decl() ast.ConstDecl { fields << ast.Field{ name: name // typ: typ - + } exprs << expr // TODO: once consts are fixed reg here & update in checker