Skip to content

Commit

Permalink
Added support for the ternary if/else form
Browse files Browse the repository at this point in the history
  • Loading branch information
vidarh committed May 6, 2009
1 parent 203250e commit 675bd03
Showing 1 changed file with 15 additions and 2 deletions.
17 changes: 15 additions & 2 deletions compiler.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ class Compiler
# a special calling convention
@@keywords = Set[
:do, :class, :defun, :if, :lambda,
:assign, :while, :index, :let, :case
:assign, :while, :index, :let, :case, :ternif
]


Expand Down Expand Up @@ -173,14 +173,27 @@ def compile_if(scope, cond, if_arm, else_arm = nil)
l_else_arm = @e.get_local
l_end_if_arm = @e.get_local
@e.jmp_on_false(l_else_arm)
compile_eval_arg(scope, if_arm)
compile_eval_arg(scope, if_arm)
@e.jmp(l_end_if_arm) if else_arm
@e.local(l_else_arm)
compile_eval_arg(scope, else_arm) if else_arm
@e.local(l_end_if_arm) if else_arm
return [:subexpr]
end

# Compiles the ternary if form (cond ? then : else)
# It may be better to transform this into the normal
# if form in the tree.
def compile_ternif(scope, cond, alt)
if alt.is_a?(Array) && alt[0] == :ternalt
if_arm = alt[1]
else_arm = alt[2]
else
if_arm = alt
end
compile_if(scope,cond,if_arm,else_arm)
end

def compile_case(scope, *args)
# error(":case not implemented yet", scope, [:case]+args)
# FIXME:
Expand Down

0 comments on commit 675bd03

Please sign in to comment.