Class: Panda::Expression
Overview
The base class for binary Panda expression nodes.
Expression objects have an operator (as a symbol) and left & right operands. Expression classes define a hash of possible operators where the key is the operator and the value is a ‘wordy’ alias for the operator which can be nil.
There is currently no unary expression class because I have not yet figured out a way to implement one in a way that would be useful with this library (for now you’ll just have to apply a bit of Demorgan to get around the absence of a unary negation operator).
Direct Known Subclasses
Constant Summary collapse
- @@inspector =
Expressers::Inspector
Instance Attribute Summary collapse
-
#left ⇒ Object
readonly
Returns the value of attribute left.
-
#operator ⇒ Object
readonly
Returns the value of attribute operator.
-
#right ⇒ Object
readonly
Returns the value of attribute right.
Class Method Summary collapse
-
.has_operator?(op) ⇒ Boolean
Returns
trueif the class uses the given operator. -
.inspector=(inspector) ⇒ Object
Sets the default expresser class for all Panda AST node’s
inspectmethod. -
.operator_alias_for(op) ⇒ Object
Returns the wordy alias of the operator or
nilif it does not have one or if the receiver class does not use the given operator. -
.operators ⇒ Object
Returns the receiver class’s hash of possible operators where the key is the operator and the value is the ‘wordy’ alias name of the operator, or
nil.
Instance Method Summary collapse
-
#has_operator?(op) ⇒ Boolean
Returns
trueif the receiver’s class uses the given operator. -
#initialize(left, operator, right) ⇒ Expression
constructor
Accepts a left operand, an operator as a symbol, and a right operand.
-
#inspect ⇒ Object
Overwrites
inspectto use theexpressclass method of the expresser class assigned to@@inspectorif one exists. -
#operator_alias ⇒ Object
Returns the wordy alias name of the receiver’s operator or
nilif it does not have one.
Methods inherited from Node
Constructor Details
#initialize(left, operator, right) ⇒ Expression
Accepts a left operand, an operator as a symbol, and a right operand. If operator is not one of the possible operators of the invoking Epression class then a Panda::SyntaxError exception is raised.
51 52 53 54 55 56 |
# File 'lib/panda/expressions.rb', line 51 def initialize(left, operator, right) unless has_operator? operator raise SyntaxError, "bad operator :#{operator} for #{self.class}" end @left, @operator, @right = left, operator, right end |
Instance Attribute Details
#left ⇒ Object (readonly)
Returns the value of attribute left.
45 46 47 |
# File 'lib/panda/expressions.rb', line 45 def left @left end |
#operator ⇒ Object (readonly)
Returns the value of attribute operator.
45 46 47 |
# File 'lib/panda/expressions.rb', line 45 def operator @operator end |
#right ⇒ Object (readonly)
Returns the value of attribute right.
45 46 47 |
# File 'lib/panda/expressions.rb', line 45 def right @right end |
Class Method Details
.has_operator?(op) ⇒ Boolean
Returns true if the class uses the given operator.
68 69 70 |
# File 'lib/panda/expressions.rb', line 68 def self.has_operator?(op) @operators.has_key? op end |
.inspector=(inspector) ⇒ Object
Sets the default expresser class for all Panda AST node’s inspect method. If nil is passed then ruby’s default inspect method is used instead.
82 83 84 |
# File 'lib/panda/expressions.rb', line 82 def self.inspector=(inspector) @@inspector = inspector end |
.operator_alias_for(op) ⇒ Object
Returns the wordy alias of the operator or nil if it does not have one or if the receiver class does not use the given operator.
75 76 77 |
# File 'lib/panda/expressions.rb', line 75 def self.operator_alias_for(op) @operators[op.to_sym] end |
.operators ⇒ Object
Returns the receiver class’s hash of possible operators where the key is the operator and the value is the ‘wordy’ alias name of the operator, or nil.
62 63 64 |
# File 'lib/panda/expressions.rb', line 62 def self.operators @operators end |
Instance Method Details
#has_operator?(op) ⇒ Boolean
Returns true if the receiver’s class uses the given operator.
88 89 90 |
# File 'lib/panda/expressions.rb', line 88 def has_operator?(op) self.class.has_operator? op end |
#inspect ⇒ Object
Overwrites inspect to use the express class method of the expresser class assigned to @@inspector if one exists.
The default expresser is Panda::Expressers::Inspector.
104 105 106 |
# File 'lib/panda/expressions.rb', line 104 def inspect @@inspector && @@inspector.express(self) || super end |
#operator_alias ⇒ Object
Returns the wordy alias name of the receiver’s operator or nil if it does not have one.
95 96 97 |
# File 'lib/panda/expressions.rb', line 95 def operator_alias self.class.operator_alias_for @operator end |