Class: Postscript::Source::OperandStack

Inherits:
Object
  • Object
show all
Defined in:
lib/postscript/source/operand_stack.rb

Overview

Parse-time operand stack. Wraps an Array with type-aware pops.

Instance Method Summary collapse

Constructor Details

#initializeOperandStack

Returns a new instance of OperandStack.



7
8
9
# File 'lib/postscript/source/operand_stack.rb', line 7

def initialize
  @items = []
end

Instance Method Details

#empty?Boolean

Returns:

  • (Boolean)


46
47
48
# File 'lib/postscript/source/operand_stack.rb', line 46

def empty?
  @items.empty?
end

#lengthObject



42
43
44
# File 'lib/postscript/source/operand_stack.rb', line 42

def length
  @items.length
end

#peek(offset = 0) ⇒ Object



38
39
40
# File 'lib/postscript/source/operand_stack.rb', line 38

def peek(offset = 0)
  @items[-1 - offset]
end

#popObject



17
18
19
20
21
# File 'lib/postscript/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/postscript/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/postscript/source/operand_stack.rb', line 11

def push(value)
  @items.push(value)
  self
end

#to_aObject



50
51
52
# File 'lib/postscript/source/operand_stack.rb', line 50

def to_a
  @items.dup
end