Class: Panda::Expression

Inherits:
Node
  • Object
show all
Defined in:
lib/panda/expressions.rb

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

Boolean, Comparison

Constant Summary collapse

@@inspector =
Expressers::Inspector

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Node

define_operator_builders_for

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

#leftObject (readonly)

Returns the value of attribute left.



45
46
47
# File 'lib/panda/expressions.rb', line 45

def left
  @left
end

#operatorObject (readonly)

Returns the value of attribute operator.



45
46
47
# File 'lib/panda/expressions.rb', line 45

def operator
  @operator
end

#rightObject (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.

Returns:



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

.operatorsObject

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.

Returns:



88
89
90
# File 'lib/panda/expressions.rb', line 88

def has_operator?(op)
  self.class.has_operator? op
end

#inspectObject

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_aliasObject

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