Skip to content

Commit

Permalink
got rid of instance_variable_get calls
Browse files Browse the repository at this point in the history
  • Loading branch information
juliend2 committed Apr 9, 2011
1 parent b3b5364 commit 4469d6b
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 23 deletions.
13 changes: 12 additions & 1 deletion nodes.rb
Expand Up @@ -2,6 +2,7 @@ class Awesome
end end
# Collection of nodes each one representing an expression. # Collection of nodes each one representing an expression.
class Nodes < Awesome class Nodes < Awesome
attr_accessor :nodes
def initialize(nodes) def initialize(nodes)
@nodes = nodes @nodes = nodes
end end
Expand All @@ -25,6 +26,7 @@ def eval(context)
# Literals are static values that have a Ruby representation, # Literals are static values that have a Ruby representation,
# eg.: a string, a number, true, false, nil, etc. # eg.: a string, a number, true, false, nil, etc.
class LiteralNode < Awesome class LiteralNode < Awesome
attr_accessor :value
def initialize(value) def initialize(value)
@value = value @value = value
end end
Expand All @@ -48,6 +50,7 @@ def eval(context)
end end


class VarNode < Awesome class VarNode < Awesome
attr_accessor :name
def initialize(name) def initialize(name)
@name = name @name = name
end end
Expand All @@ -67,6 +70,7 @@ def eval(context)
# receiver.method(argument1, argument2) # receiver.method(argument1, argument2)
# #
class CallNode < Awesome class CallNode < Awesome
attr_accessor :receiver, :method, :arguments, :is_end
def initialize(receiver, method, arguments=[], is_end=false) def initialize(receiver, method, arguments=[], is_end=false)
@receiver = receiver @receiver = receiver
@method = method @method = method
Expand Down Expand Up @@ -99,6 +103,7 @@ def eval(context)
end end


class ArrayNode < Awesome class ArrayNode < Awesome
attr_accessor :values
def initialize(arguments=[]) def initialize(arguments=[])
@values = arguments @values = arguments
end end
Expand All @@ -110,6 +115,7 @@ def end(context)


# Retreiving the value of a constant. # Retreiving the value of a constant.
class GetConstantNode < Awesome class GetConstantNode < Awesome
attr_accessor :name
def initialize(name) def initialize(name)
@name = name @name = name
end end
Expand All @@ -121,6 +127,7 @@ def eval(context)


# Setting the value of a constant. # Setting the value of a constant.
class SetConstantNode < Awesome class SetConstantNode < Awesome
attr_accessor :name, :value
def initialize(name, value) def initialize(name, value)
@name = name @name = name
@value = value @value = value
Expand All @@ -133,6 +140,7 @@ def eval(context)


# Setting the value of a local variable. # Setting the value of a local variable.
class SetLocalNode < Awesome class SetLocalNode < Awesome
attr_accessor :name, :value, :is_end
def initialize(name, value, is_end) def initialize(name, value, is_end)
@name = name @name = name
@value = value @value = value
Expand All @@ -146,6 +154,7 @@ def eval(context)


# Method definition. # Method definition.
class DefNode < Awesome class DefNode < Awesome
attr_accessor :name, :params, :body
def initialize(name, params, body) def initialize(name, params, body)
@name = name @name = name
@params = params @params = params
Expand All @@ -159,6 +168,7 @@ def eval(context)


# Class definition. # Class definition.
class ClassNode < Awesome class ClassNode < Awesome
attr_accessor :name, :body
def initialize(name, body) def initialize(name, body)
@name = name @name = name
@body = body @body = body
Expand All @@ -180,6 +190,7 @@ def eval(context)
# Look at this node if you want to implement other # Look at this node if you want to implement other
# control structures like while, for, loop, etc. # control structures like while, for, loop, etc.
class IfNode < Awesome class IfNode < Awesome
attr_accessor :condition, :body, :else_body
def initialize(condition, body, else_body=nil) def initialize(condition, body, else_body=nil)
@condition = condition @condition = condition
@body = body @body = body
Expand All @@ -195,4 +206,4 @@ def eval(context)
@else_body.eval(context) @else_body.eval(context)
end end
end end
end end
34 changes: 12 additions & 22 deletions phlower.rb
Expand Up @@ -177,7 +177,7 @@ def node(objet)
end end


if objet.instance_of?(ArrayNode) if objet.instance_of?(ArrayNode)
arraynode(objet.instance_variable_get(:@values)) do |txt| arraynode(objet.values) do |txt|
if txt.is_a?(Awesome) if txt.is_a?(Awesome)
node(txt) node(txt)
elsif(txt.instance_of?(Array)) elsif(txt.instance_of?(Array))
Expand All @@ -189,7 +189,7 @@ def node(objet)
end end


if objet.instance_of?(GetConstantNode) if objet.instance_of?(GetConstantNode)
getconstantnode(objet.instance_variable_get(:@name)) do |txt| getconstantnode(objet.name) do |txt|
if txt.is_a?(Awesome) if txt.is_a?(Awesome)
node(txt) node(txt)
elsif(txt.instance_of?(Array)) elsif(txt.instance_of?(Array))
Expand All @@ -202,9 +202,7 @@ def node(objet)


# if true: # if true:
if objet.instance_of?(IfNode) if objet.instance_of?(IfNode)
ifnode(objet.instance_variable_get(:@condition), ifnode(objet.condition, objet.body, objet.else_body) do |txt|
objet.instance_variable_get(:@body),
objet.instance_variable_get(:@else_body)) do |txt|
if txt.is_a?(Awesome) if txt.is_a?(Awesome)
node(txt) node(txt)
elsif(txt.instance_of?(Array)) elsif(txt.instance_of?(Array))
Expand All @@ -217,8 +215,7 @@ def node(objet)


# class Name: # class Name:
if objet.instance_of?(ClassNode) if objet.instance_of?(ClassNode)
classnode(objet.instance_variable_get(:@name), classnode(objet.name, objet.body) do |txt|
objet.instance_variable_get(:@body)) do |txt|
if txt.is_a?(Awesome) if txt.is_a?(Awesome)
node(txt) node(txt)
elsif(txt.instance_of?(Array)) elsif(txt.instance_of?(Array))
Expand All @@ -231,9 +228,7 @@ def node(objet)


# def methode: # def methode:
if objet.instance_of?(DefNode) if objet.instance_of?(DefNode)
defnode(objet.instance_variable_get(:@name), defnode(objet.name, objet.params, objet.body) do |txt|
objet.instance_variable_get(:@params),
objet.instance_variable_get(:@body)) do |txt|
if txt.is_a?(Awesome) if txt.is_a?(Awesome)
node(txt) node(txt)
elsif(txt.instance_of?(Array)) elsif(txt.instance_of?(Array))
Expand All @@ -246,10 +241,7 @@ def node(objet)


# objet.method(args) # objet.method(args)
if objet.instance_of?(CallNode) if objet.instance_of?(CallNode)
callnode(objet.instance_variable_get(:@method), callnode(objet.method, objet.arguments, objet.receiver, objet.is_end) do |txt, type|
objet.instance_variable_get(:@arguments),
objet.instance_variable_get(:@receiver),
objet.instance_variable_get(:@is_end)) do |txt, type|
if txt.is_a?(Awesome) if txt.is_a?(Awesome)
node(txt) node(txt)
elsif(txt.instance_of?(Array)) elsif(txt.instance_of?(Array))
Expand All @@ -262,7 +254,7 @@ def node(objet)


# variable # variable
if objet.instance_of?(VarNode) if objet.instance_of?(VarNode)
varnode(objet.instance_variable_get(:@name)) do |txt| varnode(objet.name) do |txt|
if txt.is_a?(Awesome) if txt.is_a?(Awesome)
node(txt) node(txt)
elsif(txt.instance_of?(Array)) elsif(txt.instance_of?(Array))
Expand All @@ -275,9 +267,7 @@ def node(objet)


# var = value # var = value
if objet.instance_of?(SetLocalNode) if objet.instance_of?(SetLocalNode)
setlocalnode(objet.instance_variable_get(:@name), setlocalnode(objet.name, objet.value, objet.is_end) do |txt|
objet.instance_variable_get(:@value),
objet.instance_variable_get(:@is_end)) do |txt|
if txt.is_a?(Awesome) if txt.is_a?(Awesome)
node(txt) node(txt)
elsif(txt.instance_of?(Array)) elsif(txt.instance_of?(Array))
Expand All @@ -289,7 +279,7 @@ def node(objet)
end end


if objet.instance_of?(Nodes) if objet.instance_of?(Nodes)
nodenode(objet.instance_variable_get(:@nodes)) do |txt| nodenode(objet.nodes) do |txt|
if txt.is_a?(Awesome) if txt.is_a?(Awesome)
node(txt) node(txt)
elsif(txt.instance_of?(Array)) elsif(txt.instance_of?(Array))
Expand All @@ -301,7 +291,7 @@ def node(objet)
end end


if objet.instance_of?(LiteralNode) if objet.instance_of?(LiteralNode)
literalnode(objet.instance_variable_get(:@value)) do |txt| literalnode(objet.value) do |txt|
if txt.is_a?(Awesome) if txt.is_a?(Awesome)
node(txt) node(txt)
elsif(txt.instance_of?(Array)) elsif(txt.instance_of?(Array))
Expand Down Expand Up @@ -334,7 +324,7 @@ def initialize(inputfile, outputfile=false, isstring=false)
# output # output
returned = '' returned = ''
if objects.instance_of?(Nodes) if objects.instance_of?(Nodes)
objarray = objects.instance_variable_get(:@nodes) objarray = objects.nodes
objarray.each do |object| objarray.each do |object|
returned << node(object) unless node(object).nil? returned << node(object) unless node(object).nil?
end end
Expand All @@ -355,7 +345,7 @@ def initialize(inputfile, outputfile=false, isstring=false)
# output # output
@c << "<?php\n\n" @c << "<?php\n\n"
if objects.instance_of?(Nodes) if objects.instance_of?(Nodes)
objarray = objects.instance_variable_get(:@nodes) objarray = objects.nodes
objarray.each do |object| objarray.each do |object|
@c << node(object) unless node(object).nil? @c << node(object) unless node(object).nil?
end end
Expand Down

0 comments on commit 4469d6b

Please sign in to comment.