From 3ffbf541dfdbd96b223e04c77222a5ee90838352 Mon Sep 17 00:00:00 2001 From: Jeremy Ashkenas Date: Thu, 17 Dec 2009 21:10:49 -0500 Subject: [PATCH] removed class checks in favor of statement? --- examples/code.cs | 6 +++++- lib/coffee_script/nodes.rb | 26 +++++++++++++++++++++++--- 2 files changed, 28 insertions(+), 4 deletions(-) diff --git a/examples/code.cs b/examples/code.cs index 39f301bc..29737c45 100644 --- a/examples/code.cs +++ b/examples/code.cs @@ -85,13 +85,17 @@ finally try all_hell_breaks_loose() catch error print(error) finally clean_up(). -# While loops. +# While loops, break and continue. while demand > supply sell() restock(). while supply > demand then buy(). +while true + break if broken + continue if continuing. + # Unary operators. !!true diff --git a/lib/coffee_script/nodes.rb b/lib/coffee_script/nodes.rb index 61532d47..d2eb2eaa 100644 --- a/lib/coffee_script/nodes.rb +++ b/lib/coffee_script/nodes.rb @@ -36,6 +36,10 @@ class Nodes < Node "(function(){\n#{compile(TAB, Scope.new)}\n})();" end + def statement? + true + end + # Fancy to handle pushing down returns recursively to the final lines of # inner statements (to make expressions out of them). def compile(indent='', scope=nil, opts={}) @@ -57,10 +61,16 @@ end # Literals are static values that have a Ruby representation, eg.: a string, a number, # true, false, nil, etc. class LiteralNode < Node + STATEMENTS = ['break', 'continue'] + def initialize(value) @value = value end + def statement? + STATEMENTS.include?(@value.to_s) + end + def compile(indent, scope, opts={}) @value.to_s end @@ -71,6 +81,10 @@ class ReturnNode < Node @expression = expression end + def statement? + true + end + def custom_return? true end @@ -183,6 +197,10 @@ class AssignNode < Node true end + def statement? + true + end + def compile(indent, scope, opts={}) name = @variable.compile(indent, scope) if @variable.respond_to?(:compile) last = @variable.respond_to?(:last) ? @variable.last : name @@ -370,6 +388,10 @@ class ThrowNode < Node @expression = expression end + def statement? + true + end + def compile(indent, scope, opts={}) "throw #{@expression.compile(indent, scope)}" end @@ -390,8 +412,6 @@ end # "if-else" control structure. Look at this node if you want to implement other control # structures like while, for, loop, etc. class IfNode < Node - FORCE_STATEMENT = [Nodes, ReturnNode, AssignNode, IfNode, ForNode, ThrowNode, WhileNode] - def initialize(condition, body, else_body=nil, tag=nil) @condition = condition @body = body && body.flatten @@ -423,7 +443,7 @@ class IfNode < Node end def statement? - @is_statement ||= (FORCE_STATEMENT.include?(@body.class) || FORCE_STATEMENT.include?(@else_body.class)) + @is_statement ||= (@body.statement? || (@else_body && @else_body.statement?)) end def line_ending