Class: Calculator

Inherits:
Object
  • Object
show all
Defined in:
lib/calculator.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeCalculator



9
10
11
12
13
# File 'lib/calculator.rb', line 9

def initialize()
  @memory = 0
  @nomVar = []
  @valVar = []
end

Instance Attribute Details

#memoryObject

Returns the value of attribute memory.



5
6
7
# File 'lib/calculator.rb', line 5

def memory
  @memory
end

#nomVarObject

Returns the value of attribute nomVar.



6
7
8
# File 'lib/calculator.rb', line 6

def nomVar
  @nomVar
end

#valVarObject

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