Skip to content
Browse files

Add support for conditionals

  • Loading branch information...
1 parent db801d9 commit 1e6a30e09c39a58c3d54fe5cacb1af644f7a2f41 @wycats wycats committed
Showing with 60 additions and 4 deletions.
  1. +1 −0 .gitignore
  2. +1 −1 lib/sparta/ast_visitor.rb
  3. +24 −3 lib/sparta/compilers/compiler.rb
  4. +34 −0 spec/conditional_spec.rb
View
1 .gitignore
@@ -15,3 +15,4 @@ spec/reports
test/tmp
test/version_tmp
tmp
+.rbx
View
2 lib/sparta/ast_visitor.rb
@@ -349,7 +349,7 @@ def visit_IfNode(o)
with_padding("If") do
with_padding("@conditions") { o.conditions.accept(self) }
with_padding("@value") { o.value.accept(self) }
- with_padding("@else") { o.else.accept(self) }
+ with_padding("@else") { o.else.accept(self) } if o.else
end
end
View
27 lib/sparta/compilers/compiler.rb
@@ -360,8 +360,31 @@ def visit_TypeOfNode(o)
end
end
+ def visit_IfNode(o)
+ set_line(o)
+
+ els = g.new_label
+ fin = g.new_label
+
+ o.conditions.accept(self)
+ to_boolean
+
+ g.gif els
+ o.value.accept(self)
+ g.goto fin
+ els.set!
+ o.else.accept(self) if o.else
+ fin.set!
+ end
+
protected
+ def to_boolean
+ g.push_const :Utils
+ g.swap
+ g.send :ToBoolean, 1
+ end
+
def short_circuit(o, short_circuit_if_true)
fin = g.new_label
@@ -369,9 +392,7 @@ def short_circuit(o, short_circuit_if_true)
o.left.accept(self)
g.dup_top
- g.push_const :Utils
- g.swap
- g.send :ToBoolean, 1
+ to_boolean
if short_circuit_if_true
g.git fin
View
34 spec/conditional_spec.rb
@@ -0,0 +1,34 @@
+require "spec_helper"
+
+describe "if" do
+ it "should not execute the conditional branch if the condition is false" do
+ e("var x = false, y = false; if(x) { y = true; } y").should == false
+ e("var x = 0, y = false; if(x) { y = true; } y").should == false
+ e("var x = '', y = false; if(x) { y = true; } y").should == false
+ e("var x = null, y = false; if(x) { y = true; } y").should == false
+ e("var x, y = false; if(x) { y = true; } y").should == false
+ end
+
+ it "should execute the conditional branch if the condition is true" do
+ e("var x = true, y = false; if(x) { y = true; } y").should == true
+ e("var x = 1, y = false; if(x) { y = true; } y").should == true
+ e("var x = 'hi', y = false; if(x) { y = true; } y").should == true
+ e("var x = {}, y = false; if(x) { y = true; } y").should == true
+ end
+
+ it "should execute the else branch if the condition is false" do
+ e("var x = false, y = false; if(x) { y = 'if'; } else { y = 'else'; } y").should == 'else'
+ e("var x = 0, y = false; if(x) { y = 'if'; } else { y = 'else'; } y").should == 'else'
+ e("var x = '', y = false; if(x) { y = 'if'; } else { y = 'else'; } y").should == 'else'
+ e("var x = null, y = false; if(x) { y = 'if'; } else { y = 'else'; } y").should == 'else'
+ e("var x, y = false; if(x) { y = 'if'; } else { y = 'else'; } y").should == 'else'
+ end
+
+ it "should execute the if branch if the condition is true" do
+ e("var x = true, y = false; if(x) { y = 'if'; } else { y = 'else'; } y").should == 'if'
+ e("var x = 1, y = false; if(x) { y = 'if'; } else { y = 'else'; } y").should == 'if'
+ e("var x = 'hi', y = false; if(x) { y = 'if'; } else { y = 'else'; } y").should == 'if'
+ e("var x = {}, y = false; if(x) { y = 'if'; } else { y = 'else'; } y").should == 'if'
+ end
+end
+

0 comments on commit 1e6a30e

Please sign in to comment.
Something went wrong with that request. Please try again.