checker: check ! operator

pull/4146/head
Alexander Medvednikov 2020-03-28 14:38:16 +01:00
parent 718819eb7a
commit 831be43740
3 changed files with 17 additions and 22 deletions

View File

@ -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 {

View File

@ -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 {

View File

@ -71,6 +71,7 @@ pub fn parse_file(path string, table &table.Table, comments_mode scanner.Comment
table: table
file_name: path
pref: pref // &pref.Preferences{}
scope: &ast.Scope{
start_pos: 0
parent: 0
@ -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
}
}