Class: Keisan::AST::Node

Inherits:
Object
  • Object
show all
Defined in:
lib/keisan/ast/node.rb

Direct Known Subclasses

Block, Cell, Hash, Literal, Parent

Instance Method Summary collapse

Instance Method Details

#!Object



134
135
136
# File 'lib/keisan/ast/node.rb', line 134

def !
  UnaryLogicalNot.new(self)
end

#%(other) ⇒ Object



128
129
130
131
132
# File 'lib/keisan/ast/node.rb', line 128

def %(other)
  Modulo.new(
    [self, other.to_node]
  )
end

#&(other) ⇒ Object



154
155
156
# File 'lib/keisan/ast/node.rb', line 154

def &(other)
  BitwiseAnd.new([self, other.to_node])
end

#*(other) ⇒ Object



116
117
118
119
120
# File 'lib/keisan/ast/node.rb', line 116

def *(other)
  Times.new(
    [self, other.to_node]
  )
end

#**(other) ⇒ Object



150
151
152
# File 'lib/keisan/ast/node.rb', line 150

def **(other)
  Exponent.new([self, other.to_node])
end

#+(other) ⇒ Object



104
105
106
107
108
# File 'lib/keisan/ast/node.rb', line 104

def +(other)
  Plus.new(
    [self, other.to_node]
  )
end

#+@Object



142
143
144
# File 'lib/keisan/ast/node.rb', line 142

def +@
  self
end

#-(other) ⇒ Object



110
111
112
113
114
# File 'lib/keisan/ast/node.rb', line 110

def -(other)
  Plus.new(
    [self, UnaryMinus.new(other.to_node)]
  )
end

#-@Object



146
147
148
# File 'lib/keisan/ast/node.rb', line 146

def -@
  UnaryMinus.new(self)
end

#/(other) ⇒ Object



122
123
124
125
126
# File 'lib/keisan/ast/node.rb', line 122

def /(other)
  Times.new(
    [self, UnaryInverse.new(other.to_node)]
  )
end

#<(other) ⇒ Object



182
183
184
# File 'lib/keisan/ast/node.rb', line 182

def <(other)
  LogicalLessThan.new([self, other.to_node])
end

#<<(other) ⇒ Object



166
167
168
# File 'lib/keisan/ast/node.rb', line 166

def <<(other)
  BitwiseLeftShift.new([self, other.to_node])
end

#<=(other) ⇒ Object



186
187
188
# File 'lib/keisan/ast/node.rb', line 186

def <=(other)
  LogicalLessThanOrEqualTo.new([self, other.to_node])
end

#>(other) ⇒ Object



174
175
176
# File 'lib/keisan/ast/node.rb', line 174

def >(other)
  LogicalGreaterThan.new([self, other.to_node])
end

#>=(other) ⇒ Object



178
179
180
# File 'lib/keisan/ast/node.rb', line 178

def >=(other)
  LogicalGreaterThanOrEqualTo.new([self, other.to_node])
end

#>>(other) ⇒ Object



170
171
172
# File 'lib/keisan/ast/node.rb', line 170

def >>(other)
  BitwiseRightShift.new([self, other.to_node])
end

#^(other) ⇒ Object



158
159
160
# File 'lib/keisan/ast/node.rb', line 158

def ^(other)
  BitwiseXor.new([self, other.to_node])
end

#and(other) ⇒ Object



198
199
200
# File 'lib/keisan/ast/node.rb', line 198

def and(other)
  LogicalAnd.new([self, other.to_node])
end

#coerce(other) ⇒ Object



83
84
85
# File 'lib/keisan/ast/node.rb', line 83

def coerce(other)
  [other.to_node, self]
end

#contains_a?(klass) ⇒ Boolean

Returns:



48
49
50
51
52
53
54
55
56
57
58
59
60
61
# File 'lib/keisan/ast/node.rb', line 48

def contains_a?(klass)
  case klass
  when Array
    klass.any? do |k|
      traverse do |node|
        node.is_a?(k)
      end
    end
  else
    traverse do |node|
      node.is_a?(klass)
    end
  end
end

#deep_dupObject



20
21
22
# File 'lib/keisan/ast/node.rb', line 20

def deep_dup
  dup
end

#differentiate(variable, context = nil) ⇒ Object



67
68
69
# File 'lib/keisan/ast/node.rb', line 67

def differentiate(variable, context = nil)
  raise Exceptions::NonDifferentiableError.new
end

#differentiated(variable, context = nil) ⇒ Object



71
72
73
# File 'lib/keisan/ast/node.rb', line 71

def differentiated(variable, context = nil)
  deep_dup.differentiate(variable, context)
end

#equal(other) ⇒ Object



190
191
192
# File 'lib/keisan/ast/node.rb', line 190

def equal(other)
  LogicalEqual.new([self, other.to_node])
end

#evaluate(context = nil) ⇒ Object



36
37
38
# File 'lib/keisan/ast/node.rb', line 36

def evaluate(context = nil)
  value(context)
end

#evaluate_assignments(context = nil) ⇒ Object



63
64
65
# File 'lib/keisan/ast/node.rb', line 63

def evaluate_assignments(context = nil)
  self
end

#evaluated(context = nil) ⇒ Object



32
33
34
# File 'lib/keisan/ast/node.rb', line 32

def evaluated(context = nil)
  deep_dup.evaluate(context)
end

#false?Boolean

Returns:



100
101
102
# File 'lib/keisan/ast/node.rb', line 100

def false?
  !true?
end

#is_constant?Boolean

Returns:



206
207
208
# File 'lib/keisan/ast/node.rb', line 206

def is_constant?
  false
end

#not_equal(other) ⇒ Object



194
195
196
# File 'lib/keisan/ast/node.rb', line 194

def not_equal(other)
  LogicalNotEqual.new([self, other.to_node])
end

#or(other) ⇒ Object



202
203
204
# File 'lib/keisan/ast/node.rb', line 202

def or(other)
  LogicalOr.new([self, other.to_node])
end

#replace(variable, replacement) ⇒ Object



75
76
77
# File 'lib/keisan/ast/node.rb', line 75

def replace(variable, replacement)
  self
end

#replaced(variable, replacement) ⇒ Object



79
80
81
# File 'lib/keisan/ast/node.rb', line 79

def replaced(variable, replacement)
  deep_dup.replace(variable, replacement)
end

#simplified(context = nil) ⇒ Object



24
25
26
# File 'lib/keisan/ast/node.rb', line 24

def simplified(context = nil)
  deep_dup.simplify(context)
end

#simplify(context = nil) ⇒ Object



28
29
30
# File 'lib/keisan/ast/node.rb', line 28

def simplify(context = nil)
  self
end

#to_cellObject



91
92
93
# File 'lib/keisan/ast/node.rb', line 91

def to_cell
  AST::Cell.new(self)
end

#to_nodeObject



87
88
89
# File 'lib/keisan/ast/node.rb', line 87

def to_node
  self
end

#traverse(&block) ⇒ Object

Takes a block, and does a DFS down the AST, evaluating the received block at each node, passing in the node as the single argument. If the block returns a truthy value at any point, the DFS ends and the return value is percolated up the tree.



44
45
46
# File 'lib/keisan/ast/node.rb', line 44

def traverse(&block)
  block.call(self)
end

#true?Boolean

Will only return False for AST::Boolean(false) and AST::Null

Returns:



96
97
98
# File 'lib/keisan/ast/node.rb', line 96

def true?
  true
end

#unbound_functions(context = nil) ⇒ Object



12
13
14
# File 'lib/keisan/ast/node.rb', line 12

def unbound_functions(context = nil)
  Set.new
end

#unbound_variables(context = nil) ⇒ Object



8
9
10
# File 'lib/keisan/ast/node.rb', line 8

def unbound_variables(context = nil)
  Set.new
end

#value(context = nil) ⇒ Object



4
5
6
# File 'lib/keisan/ast/node.rb', line 4

def value(context = nil)
  raise Exceptions::NotImplementedError.new
end

#well_defined?(context = nil) ⇒ Boolean

Returns:



16
17
18
# File 'lib/keisan/ast/node.rb', line 16

def well_defined?(context = nil)
  unbound_variables(context).empty? && unbound_functions(context).empty?
end

#|(other) ⇒ Object



162
163
164
# File 'lib/keisan/ast/node.rb', line 162

def |(other)
  BitwiseOr.new([self, other.to_node])
end

#~Object



138
139
140
# File 'lib/keisan/ast/node.rb', line 138

def ~
  UnaryBitwiseNot.new(self)
end