Class: RMasm::Stacker

Inherits:
Object show all
Defined in:
lib/rmasm/stacker.rb

Overview

Utility class to implement stack values. This class is used by subclass like Align, Data.default…etc. It provides a simple mechanism to hold a stack of value, with an initial default value that can be updated but cannot be removed from the stack

Direct Known Subclasses

Align, BinIOMode, DataDefault

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(default_value, *args) ⇒ Stacker

Initialize the stack with a default initial value.



34
35
36
37
38
39
40
41
42
43
44
# File 'lib/rmasm/stacker.rb', line 34

def initialize(default_value, *args)
  if args.empty?
    @stack = [default_value]
  else
    @stack = []
    args.reverse.each {|arg| push arg}
    if @stack.empty?
      @stack = [default_value]
    end
  end
end

Instance Attribute Details

#stackObject (readonly)

Returns the value of attribute stack.



31
32
33
# File 'lib/rmasm/stacker.rb', line 31

def stack
  @stack
end

Instance Method Details

#pop(*args) ⇒ Object

pop the last alignment.



77
78
79
80
81
82
83
84
85
86
87
88
89
# File 'lib/rmasm/stacker.rb', line 77

def pop(*args)
  # No args expected
  if !args.empty?
    return Report.error(:ARGS, args.length, "0", "align.pop")
  end

  if (@stack.length > 1)
    @stack.shift
  else
    # Cannot pop top level alignment
    return Report.error(:R3001, self)
  end
end

#push(*args) ⇒ Object

push int_value, push a new alignment



61
62
63
64
65
66
67
68
69
70
71
72
73
74
# File 'lib/rmasm/stacker.rb', line 61

def push(*args)
  # Check args length
  if args.length != 1
    return Report.error(:ARGS, args.length, "1", "align.push")
  end

  # Get 1st value, check and set
  arg = args[0]
  if check_value(arg)
    @stack.unshift arg
    return arg
  end
  nil
end

#to_sObject

Friendly to string



92
93
94
# File 'lib/rmasm/stacker.rb', line 92

def to_s()
  "#{self.class.name} with value = #{@stack[0]}, stack = [#{@stack * ' , '}]"
end

#valueObject

Return the current stack value



47
48
49
# File 'lib/rmasm/stacker.rb', line 47

def value
  @stack[0]
end

#value=(arg) ⇒ Object

Assign a new stack value to the current value. This method does not modify the stack



52
53
54
55
56
57
58
# File 'lib/rmasm/stacker.rb', line 52

def value= (arg)
  if check_value(arg)
    @stack[0] = arg
    return arg
  end
  return nil
end