boolean-type from comparisons
This commit is contained in:
26
parser.v
26
parser.v
@@ -213,11 +213,9 @@ fn (mut p Parser) parse_expr() Expr {
|
||||
mut left := p.parse_primary()
|
||||
|
||||
match p.peek().type {
|
||||
.plus {return p.parse_binary(left, '+')}
|
||||
.minus {return p.parse_binary(left, '-')}
|
||||
.star {return p.parse_binary(left, '*')}
|
||||
.slash {return p.parse_binary(left, '/')}
|
||||
.equals {return p.parse_binary(left, '=')}
|
||||
.plus, .minus, .star, .slash, .equals, .eq_eq, .not_eq, .less_eq, .greater_eq {
|
||||
return p.parse_binary(left, p.peek().text)
|
||||
}
|
||||
else {return left}
|
||||
}
|
||||
}
|
||||
@@ -226,8 +224,8 @@ fn (mut p Parser) parse_binary(left Expr, op string) BinaryExpr {
|
||||
p.next()
|
||||
right := p.parse_expr()
|
||||
binary_expr := BinaryExpr{left, op, right}
|
||||
if !p.is_op_valid_for_type(p.get_expr_type(binary_expr), op) {
|
||||
parse_error("Illegal operation ${op} for type ${p.get_expr_type(binary_expr)}")
|
||||
if !p.is_op_valid_for_type(p.get_expr_type(left), op) {
|
||||
parse_error("Illegal operation ${op} for type ${p.get_expr_type(left)}")
|
||||
}
|
||||
return binary_expr
|
||||
}
|
||||
@@ -267,7 +265,11 @@ fn (mut p Parser) get_expr_type(expr Expr) string {
|
||||
if left_t != right_t {
|
||||
parse_error ('Type mismatch in expression: ${left_t} and ${right_t}')
|
||||
}
|
||||
left_t
|
||||
if expr.op in ['<=', '==', '>=', '!='] {
|
||||
'bool'
|
||||
} else {
|
||||
left_t
|
||||
}
|
||||
}
|
||||
Variable {
|
||||
p.dump_stmt()
|
||||
@@ -284,12 +286,14 @@ fn (mut p Parser) get_expr_type(expr Expr) string {
|
||||
}
|
||||
|
||||
fn (mut p Parser) is_op_valid_for_type(type string, op string) bool {
|
||||
legal_ops := match type {
|
||||
'int' {['+', '-', '*', '/', '<', '>', '=']}
|
||||
'real' {['+', '-', '*', '/', '<', '>', '=']}
|
||||
global := ['=', '==', '!=']
|
||||
mut legal_ops := match type {
|
||||
'int' {['+', '-', '*', '/', '<', '>', '<=', '>=',]}
|
||||
'real' {['+', '-', '*', '/', '<', '>', '<=', '>=']}
|
||||
'bool' {['=']}
|
||||
else {[]}
|
||||
}
|
||||
legal_ops << global
|
||||
return op in legal_ops
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user