Class: Keisan::AST::Operator
Constant Summary
collapse
- ARITY_PRIORITY_ASSOCIATIVITY =
NOTE: operators with same priority must have same associativity
{
"u!": [1, 100, :right], "u~": [1, 100, :right], "u+": [1, 100, :right], "**": [2, 95, :right], "u-": [1, 90, :right], "*": [2, 85, :left], "%": [2, 85, :left], "+": [2, 80, :left], "<<": [2, 75, :left], ">>": [2, 75, :left], "&": [2, 70, :left], "^": [2, 65, :left], "|": [2, 65, :left], ">": [2, 60, :left], ">=": [2, 60, :left], "<": [2, 60, :left], "<=": [2, 60, :left], "==": [2, 55, :none], "!=": [2, 55, :none], "&&": [2, 50, :left], "||": [2, 45, :left], "=": [2, 40, :right] }.freeze
- ARITIES =
Hash[ARITY_PRIORITY_ASSOCIATIVITY.map {|sym, ary| [sym, ary[0]]}].freeze
- PRIORITIES =
Hash[ARITY_PRIORITY_ASSOCIATIVITY.map {|sym, ary| [sym, ary[1]]}].freeze
- ASSOCIATIVITIES =
Hash[ARITY_PRIORITY_ASSOCIATIVITY.map {|sym, ary| [sym, ary[2]]}].freeze
- ASSOCIATIVITY_OF_PRIORITY =
ARITY_PRIORITY_ASSOCIATIVITY.inject({}) do |h, (symbol,arity_priority_associativity)|
h[arity_priority_associativity[1]] = arity_priority_associativity[2]
h
end.freeze
Instance Attribute Summary
Attributes inherited from Parent
#children
Class Method Summary
collapse
Instance Method Summary
collapse
Methods inherited from Parent
#==, #deep_dup, #evaluate, #freeze, #replace, #simplify, #traverse, #unbound_functions, #unbound_variables
Methods inherited from Node
#!, #%, #&, #*, #**, #+, #+@, #-, #-@, #/, #<, #<<, #<=, #>, #>=, #>>, #^, #and, #coerce, #contains_a?, #deep_dup, #differentiate, #differentiated, #equal, #evaluate, #evaluated, #false?, #not_equal, #or, #replace, #replaced, #simplified, #simplify, #to_cell, #to_node, #traverse, #true?, #unbound_functions, #unbound_variables, #well_defined?, #|, #~
Constructor Details
#initialize(children = [], parsing_operators = []) ⇒ Operator
Returns a new instance of Operator.
41
42
43
44
45
46
47
48
49
50
|
# File 'lib/keisan/ast/operator.rb', line 41
def initialize(children = [], parsing_operators = [])
unless parsing_operators.empty? || children.count == parsing_operators.count + 1
raise Exceptions::ASTError.new("Mismatch of children and operators")
end
children = Array(children)
super(children)
@parsing_operators = parsing_operators
end
|
Class Method Details
.arity ⇒ Object
68
69
70
|
# File 'lib/keisan/ast/operator.rb', line 68
def self.arity
ARITIES[symbol]
end
|
.associativity ⇒ Object
84
85
86
|
# File 'lib/keisan/ast/operator.rb', line 84
def self.associativity
ASSOCIATIVITIES[symbol]
end
|
.associativity_of_priority(priority) ⇒ Object
60
61
62
|
# File 'lib/keisan/ast/operator.rb', line 60
def self.associativity_of_priority(priority)
ASSOCIATIVITY_OF_PRIORITY[priority]
end
|
.priority ⇒ Object
76
77
78
|
# File 'lib/keisan/ast/operator.rb', line 76
def self.priority
PRIORITIES[symbol]
end
|
Instance Method Details
#arity ⇒ Object
64
65
66
|
# File 'lib/keisan/ast/operator.rb', line 64
def arity
self.class.arity
end
|
#associativity ⇒ Object
80
81
82
|
# File 'lib/keisan/ast/operator.rb', line 80
def associativity
self.class.associativity
end
|
#evaluate_assignments(context = nil) ⇒ Object
52
53
54
55
56
57
58
|
# File 'lib/keisan/ast/operator.rb', line 52
def evaluate_assignments(context = nil)
context ||= Context.new
@children = children.map do |child|
child.evaluate_assignments(context)
end
self
end
|
#priority ⇒ Object
72
73
74
|
# File 'lib/keisan/ast/operator.rb', line 72
def priority
self.class.priority
end
|
#symbol ⇒ Object
88
89
90
|
# File 'lib/keisan/ast/operator.rb', line 88
def symbol
self.class.symbol
end
|
#to_s ⇒ Object
113
114
115
116
117
118
119
120
121
122
|
# File 'lib/keisan/ast/operator.rb', line 113
def to_s
children.map do |child|
case child
when Operator
"(#{child.to_s})"
else
"#{child.to_s}"
end
end.join(symbol.to_s)
end
|
#value(context = nil) ⇒ Object
100
101
102
103
104
105
106
107
108
109
110
111
|
# File 'lib/keisan/ast/operator.rb', line 100
def value(context = nil)
args = children
args = args.reverse if associativity == :right
args.inject(blank_value) do |result, child|
if associativity == :left
result.send(symbol, child.value(context))
else
child.value(context).send(symbol, result)
end
end
end
|