Skip to content

Commit

Permalink
Compiler: replace binary property of If with two booleans, and mo…
Browse files Browse the repository at this point in the history
…ved to semantic code
Ary Borenszweig committed Jul 22, 2016

Verified

This commit was created on GitHub.com and signed with GitHub’s verified signature. The key has expired.
1 parent 969c103 commit d63ebf8
Showing 5 changed files with 22 additions and 11 deletions.
15 changes: 15 additions & 0 deletions src/compiler/crystal/semantic/ast.cr
Original file line number Diff line number Diff line change
@@ -858,6 +858,21 @@ module Crystal
end
end

class If
# This is set to `true` for an `If` that was created from an `&&` expression.
property? and = false

# This is set to `true` for an `If` that was created from an `||` expression.
property? or = false

def clone_without_location
a_if = previous_def
a_if.and = and?
a_if.or = or?
a_if
end
end

class TupleLiteral
property! program : Program

2 changes: 1 addition & 1 deletion src/compiler/crystal/semantic/cleanup_transformer.cr
Original file line number Diff line number Diff line change
@@ -567,7 +567,7 @@ module Crystal

reset_last_status

if node.binary == :and
if node.and?
@last_is_truthy = cond_is_truthy && then_is_truthy
@last_is_falsey = cond_is_falsey || then_is_falsey
end
4 changes: 2 additions & 2 deletions src/compiler/crystal/semantic/literal_expander.cr
Original file line number Diff line number Diff line change
@@ -226,7 +226,7 @@ module Crystal
temp_var = new_temp_var
If.new(Assign.new(temp_var.clone, left), node.right, temp_var.clone)
end
new_node.binary = :and
new_node.and = true
new_node.location = node.location
new_node
end
@@ -263,7 +263,7 @@ module Crystal
temp_var = new_temp_var
If.new(Assign.new(temp_var.clone, left), temp_var.clone, node.right)
end
new_node.binary = :or
new_node.or = true
new_node.location = node.location
new_node
end
7 changes: 3 additions & 4 deletions src/compiler/crystal/semantic/main_visitor.cr
Original file line number Diff line number Diff line change
@@ -1605,11 +1605,10 @@ module Crystal
merge_if_vars node, cond_vars, then_vars, else_vars, then_unreachable, else_unreachable

if needs_type_filters?
case node.binary
when :and
if node.and?
@type_filters = TypeFilters.and(cond_type_filters, then_type_filters, else_type_filters)
# TODO: or type filters
# when :or
# elsif node.or?
# node.type_filters = or_type_filters(node.then.type_filters, node.else.type_filters)
end
end
@@ -1846,7 +1845,7 @@ module Crystal
when And
return get_while_cond_assign_target(node.left)
when If
if node.binary == :and
if node.and?
return get_while_cond_assign_target(node.cond)
end
when Call
5 changes: 1 addition & 4 deletions src/compiler/crystal/syntax/ast.cr
Original file line number Diff line number Diff line change
@@ -578,7 +578,6 @@ module Crystal
property cond : ASTNode
property then : ASTNode
property else : ASTNode
property binary : Symbol?

def initialize(@cond, a_then = nil, a_else = nil)
@then = Expressions.from a_then
@@ -592,9 +591,7 @@ module Crystal
end

def clone_without_location
a_if = If.new(@cond.clone, @then.clone, @else.clone)
a_if.binary = binary
a_if
If.new(@cond.clone, @then.clone, @else.clone)
end

def_equals_and_hash @cond, @then, @else

0 comments on commit d63ebf8

Please sign in to comment.