Class: Postsvg::Source::OperandStack
- Inherits:
-
Object
- Object
- Postsvg::Source::OperandStack
- Defined in:
- lib/postsvg/source/operand_stack.rb
Overview
Parse-time operand stack. Wraps an Array with type-aware pops.
Instance Method Summary collapse
- #empty? ⇒ Boolean
-
#initialize ⇒ OperandStack
constructor
A new instance of OperandStack.
- #length ⇒ Object
- #peek(offset = 0) ⇒ Object
- #pop ⇒ Object
- #pop_number(default_on_empty: nil) ⇒ Object
- #push(value) ⇒ Object (also: #<<)
- #to_a ⇒ Object
Constructor Details
#initialize ⇒ OperandStack
Returns a new instance of OperandStack.
7 8 9 |
# File 'lib/postsvg/source/operand_stack.rb', line 7 def initialize @items = [] end |
Instance Method Details
#empty? ⇒ Boolean
46 47 48 |
# File 'lib/postsvg/source/operand_stack.rb', line 46 def empty? @items.empty? end |
#length ⇒ Object
42 43 44 |
# File 'lib/postsvg/source/operand_stack.rb', line 42 def length @items.length end |
#peek(offset = 0) ⇒ Object
38 39 40 |
# File 'lib/postsvg/source/operand_stack.rb', line 38 def peek(offset = 0) @items[-1 - offset] end |
#pop ⇒ Object
17 18 19 20 21 |
# File 'lib/postsvg/source/operand_stack.rb', line 17 def pop return Model::Computed.new(operator_keyword: "(missing)") if @items.empty? @items.pop end |
#pop_number(default_on_empty: nil) ⇒ Object
23 24 25 26 27 28 29 30 31 32 33 34 35 36 |
# File 'lib/postsvg/source/operand_stack.rb', line 23 def pop_number(default_on_empty: nil) return default_on_empty if @items.empty? && !default_on_empty.nil? value = pop return value.to_f if value.is_a?(Model::Literals::Number) return value.to_f if value.is_a?(Numeric) # Parse-time fallback: a Name (e.g. /x defined via def) or a # Computed sentinel (e.g. result of an arithmetic op) cannot # be evaluated to a number yet. Return 0 so the operator # instance can be built; the visitor will re-resolve at # runtime if the program is executed. 0.0 end |
#push(value) ⇒ Object Also known as: <<
11 12 13 14 |
# File 'lib/postsvg/source/operand_stack.rb', line 11 def push(value) @items.push(value) self end |
#to_a ⇒ Object
50 51 52 |
# File 'lib/postsvg/source/operand_stack.rb', line 50 def to_a @items.dup end |