lib/liquidscript/compiler/icr/expressions.rb in liquidscript-0.4.1 vs lib/liquidscript/compiler/icr/expressions.rb in liquidscript-0.5.0

- old
+ new

@@ -6,26 +6,35 @@ # Compiles an expression. This is primarily used in a general # context, such that anything can be returned. # # @return [ICR::Code] def compile_expression - expect :if, :unless, :class, :module, :_ => :vexpression + expect :if, :unless, :class, :module, :loop, :for, + :while, :action, :try, :_ => :vexpression end # Compiles an expression that returns a value. # # @return [ICR::Code] def compile_vexpression out = expect :number, :identifier, :istring, :lparen, :sstring, :operator, :keyword, :unop, + :regex, :newline, :istring_begin, - :lbrack => :object, - :lbrace => :array, - :arrow => :function + :iheredoc_begin, + :lbrace => :object, + :lbrack => :array, + :arrow => :function, + [ + :heredoc_ref, :iheredoc_ref + ] => :href, + [ + :heredoc, :iheredoc + ] => :heredoc if peek? :binop compile_binop(out) elsif peek? :prop compile_property(out) @@ -118,13 +127,13 @@ define_method(:"compile_#{key}") do shift key shift :lparen conditional = compile_vexpression shift :rparen - shift :lbrack + shift :lbrace - body = collect_compiles(:expression, :rbrack) + body = collect_compiles(:expression, :rbrace) if peek?(:elsif, :else) code key, conditional, body, expect(:elsif, :else) else code key, conditional, body @@ -135,22 +144,59 @@ def compile_unless shift :unless shift :lparen conditional = compile_vexpression shift :rparen - shift :lbrack + shift :lbrace - body = collect_compiles(:expression, :rbrack) + body = collect_compiles(:expression, :rbrace) code :unless, conditional, body end def compile_else shift :else - shift :lbrack + shift :lbrace - body = collect_compiles(:expression, :rbrack) + body = collect_compiles(:expression, :rbrace) code :else, body + end + + def compile_try + shift :try + shift :lbrace + try_body = collect_compiles(:expression, :rbrace) + + next_part = if peek?(:catch) + _compile_catch + elsif peek?(:finally) + _compile_finally + end + + code :try, try_body, next_part + end + + def _compile_catch + shift :catch + shift :lparen + var = shift :identifier + shift :rparen + shift :lbrace + catch_body = collect_compiles(:expression, :rbrace) + + next_part = if peek?(:finally) + _compile_finally + end + + code :catch, var, catch_body, next_part + end + + def _compile_finally + shift :finally + shift :lbrace + finally_body = collect_compiles(:expression, :rbrace) + + code :finally, finally_body end end end end