Class: Calculator
- Inherits:
-
Object
- Object
- Calculator
- Defined in:
- lib/calculator.rb
Instance Attribute Summary collapse
-
#memory ⇒ Object
Returns the value of attribute memory.
-
#nomVar ⇒ Object
Returns the value of attribute nomVar.
-
#valVar ⇒ Object
Returns the value of attribute valVar.
Instance Method Summary collapse
- #comp(expr) ⇒ Object
- #eval(expr) ⇒ Object
-
#initialize ⇒ Calculator
constructor
A new instance of Calculator.
- #recallVar(key) ⇒ Object
- #store(val) ⇒ Object
- #storeVar(key, val) ⇒ Object
Constructor Details
#initialize ⇒ Calculator
9 10 11 12 13 |
# File 'lib/calculator.rb', line 9 def initialize() @memory = 0 @nomVar = [] @valVar = [] end |
Instance Attribute Details
#memory ⇒ Object
Returns the value of attribute memory.
5 6 7 |
# File 'lib/calculator.rb', line 5 def memory @memory end |
#nomVar ⇒ Object
Returns the value of attribute nomVar.
6 7 8 |
# File 'lib/calculator.rb', line 6 def nomVar @nomVar end |
#valVar ⇒ Object
Returns the value of attribute valVar.
7 8 9 |
# File 'lib/calculator.rb', line 7 def valVar @valVar end |
Instance Method Details
#comp(expr) ⇒ Object
42 43 44 45 46 47 48 49 50 51 52 |
# File 'lib/calculator.rb', line 42 def comp(expr) parser = Parser.new(StringIO.new(expr)) inicioConst = "main: sp := 6 \n cero := 0 \n uno := 1 \n tres := 3 \n sp := sp + uno \n" finalConst = " writeInt(tmp) \n halt \n equ tmp M[0] \n equ tmp2 M[1] \n equ uno M[2] \n equ tres M[3] \n equ mem M[4] \n equ sp M[5] \n equ stack M[6] equ cero M[7]" ast = parser.parse() ret = inicioConst + ast.ewe() + finalConst return ret end |
#eval(expr) ⇒ Object
24 25 26 27 28 |
# File 'lib/calculator.rb', line 24 def eval(expr) parser = Parser.new(StringIO.new(expr)) ast = parser.parse() return ast.evaluate() end |
#recallVar(key) ⇒ Object
30 31 32 33 34 35 36 37 38 39 40 |
# File 'lib/calculator.rb', line 30 def recallVar(key) i = 0 value = 0 while i < @nomVar.size do if @nomVar[i] == key then value = @valVar[i] end i=i+1 end return value end |
#store(val) ⇒ Object
20 21 22 |
# File 'lib/calculator.rb', line 20 def store (val) @memory = val end |
#storeVar(key, val) ⇒ Object
15 16 17 18 |
# File 'lib/calculator.rb', line 15 def storeVar(key, val) @nomVar << key @valVar << val end |