lib/gloo/expr/expression.rb in gloo-0.6.1 vs lib/gloo/expr/expression.rb in gloo-0.7.0
- old
+ new
@@ -6,20 +6,32 @@
module Gloo
module Expr
class Expression
+ # ---------------------------------------------------------------------
+ # Constructor
+ # ---------------------------------------------------------------------
+
+ #
# Create the expression from a list of tokens.
+ #
def initialize( tokens )
@tokens = tokens
@symbols = []
@left = nil
@right = nil
@op = nil
end
+ # ---------------------------------------------------------------------
+ # Evaluate Expression
+ # ---------------------------------------------------------------------
+
+ #
# Evaluate the expression and return the value.
+ #
def evaluate
identify_tokens
@symbols.each do |sym|
if sym.is_a? Gloo::Core::Op
@@ -37,44 +49,54 @@
return resolve_ref @left if @left.is_a? Gloo::Core::Pn
return @left
end
+ # ---------------------------------------------------------------------
+ # Private functions
+ # ---------------------------------------------------------------------
+
+ private
+
+ #
# Perform the operation.
+ #
def perform_op
@op ||= Gloo::Core::Op.default_op
l = evaluate_sym @left
r = evaluate_sym @right
@left = @op.perform l, r
@right = nil
@op = nil
end
+ #
# Evaluate the symbol and get a simple value.
+ #
def evaluate_sym( sym )
return sym.value if sym.is_a? Gloo::Core::Literal
return resolve_ref sym if sym.is_a? Gloo::Core::Pn
return sym
end
+ #
# resolve an object reference and get the value.
+ #
def resolve_ref( ref )
return ref.src if ref.named_color?
ob = ref.resolve
return ob.value if ob
end
+ #
# Identify each token in the list.
+ #
def identify_tokens
@tokens.each do |o|
@symbols << identify_token( o )
end
-
- # @symbols.each do |o|
- # puts o.class.name
- # end
end
#
# Identify the tokens and create appropriate symbols.
#