removed class checks in favor of statement?

This commit is contained in:
Jeremy Ashkenas
2009-12-17 21:10:49 -05:00
parent e3c667d49d
commit 3ffbf541df
2 changed files with 28 additions and 4 deletions

View File

@@ -85,13 +85,17 @@ finally
try all_hell_breaks_loose() catch error print(error) finally clean_up(). try all_hell_breaks_loose() catch error print(error) finally clean_up().
# While loops. # While loops, break and continue.
while demand > supply while demand > supply
sell() sell()
restock(). restock().
while supply > demand then buy(). while supply > demand then buy().
while true
break if broken
continue if continuing.
# Unary operators. # Unary operators.
!!true !!true

View File

@@ -36,6 +36,10 @@ class Nodes < Node
"(function(){\n#{compile(TAB, Scope.new)}\n})();" "(function(){\n#{compile(TAB, Scope.new)}\n})();"
end end
def statement?
true
end
# Fancy to handle pushing down returns recursively to the final lines of # Fancy to handle pushing down returns recursively to the final lines of
# inner statements (to make expressions out of them). # inner statements (to make expressions out of them).
def compile(indent='', scope=nil, opts={}) 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, # Literals are static values that have a Ruby representation, eg.: a string, a number,
# true, false, nil, etc. # true, false, nil, etc.
class LiteralNode < Node class LiteralNode < Node
STATEMENTS = ['break', 'continue']
def initialize(value) def initialize(value)
@value = value @value = value
end end
def statement?
STATEMENTS.include?(@value.to_s)
end
def compile(indent, scope, opts={}) def compile(indent, scope, opts={})
@value.to_s @value.to_s
end end
@@ -71,6 +81,10 @@ class ReturnNode < Node
@expression = expression @expression = expression
end end
def statement?
true
end
def custom_return? def custom_return?
true true
end end
@@ -183,6 +197,10 @@ class AssignNode < Node
true true
end end
def statement?
true
end
def compile(indent, scope, opts={}) def compile(indent, scope, opts={})
name = @variable.compile(indent, scope) if @variable.respond_to?(:compile) name = @variable.compile(indent, scope) if @variable.respond_to?(:compile)
last = @variable.respond_to?(:last) ? @variable.last : name last = @variable.respond_to?(:last) ? @variable.last : name
@@ -370,6 +388,10 @@ class ThrowNode < Node
@expression = expression @expression = expression
end end
def statement?
true
end
def compile(indent, scope, opts={}) def compile(indent, scope, opts={})
"throw #{@expression.compile(indent, scope)}" "throw #{@expression.compile(indent, scope)}"
end end
@@ -390,8 +412,6 @@ end
# "if-else" control structure. Look at this node if you want to implement other control # "if-else" control structure. Look at this node if you want to implement other control
# structures like while, for, loop, etc. # structures like while, for, loop, etc.
class IfNode < Node class IfNode < Node
FORCE_STATEMENT = [Nodes, ReturnNode, AssignNode, IfNode, ForNode, ThrowNode, WhileNode]
def initialize(condition, body, else_body=nil, tag=nil) def initialize(condition, body, else_body=nil, tag=nil)
@condition = condition @condition = condition
@body = body && body.flatten @body = body && body.flatten
@@ -423,7 +443,7 @@ class IfNode < Node
end end
def statement? 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 end
def line_ending def line_ending