Skip to content

Commit

Permalink
Only allow negating booleans.
Browse files Browse the repository at this point in the history
  • Loading branch information
gdotdesign committed Jun 3, 2018
1 parent f328468 commit 6e483e7
Show file tree
Hide file tree
Showing 5 changed files with 36 additions and 3 deletions.
4 changes: 2 additions & 2 deletions spec/formatters/negated_expression
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
module Test {
fun test : Bool {
!!!!!"fun" && !!!![1] && !1
!!!!!true && !!!!false && !false
}
}
--------------------------------------------------------------------------------
module Test {
fun test : Bool {
!"fun" && !![1] && !1
!true && !!false && !false
}
}
11 changes: 11 additions & 0 deletions spec/type_checking/negated_expression
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
module Test {
fun test : Bool {
!false
}
}
--------------------------------------------------------NegatedExpressionNotBool
module Test {
fun test : Bool {
!"asd"
}
}
1 change: 1 addition & 0 deletions src/formatters/negated_expression.cr
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ module Mint
def format(node : Ast::NegatedExpression) : String
expression =
format node.expression

"#{node.negations}#{expression}"
end
end
Expand Down
11 changes: 11 additions & 0 deletions src/messages/negated_expression_not_bool.cr
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
message NegatedExpressionNotBool do
title "Type Error"

block do
text "A negated expressions expression must evaluate to bool."
end

was_expecting_type expected, got

snippet node
end
12 changes: 11 additions & 1 deletion src/type_checkers/negated_expression.cr
Original file line number Diff line number Diff line change
@@ -1,7 +1,17 @@
module Mint
class TypeChecker
type_error NegatedExpressionNotBool

def check(node : Ast::NegatedExpression) : Type
BOOL
expression =
check node.expression

raise NegatedExpressionNotBool, {
"got" => expression,
"expected" => BOOL,
} unless Comparer.compare(BOOL, expression)

expression
end
end
end

0 comments on commit 6e483e7

Please sign in to comment.