jmejiao5-rubycalc 0.0.3 → 0.0.4

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 4f725d9b06b4be7a783e6630192d41258de624c3
4
- data.tar.gz: 470edb50f9000659d1d85f078b556ad3325e79b7
3
+ metadata.gz: e356e670b90165e3c10023ef55c575340033d3af
4
+ data.tar.gz: 926015f5092fcd22be8e1af04b73f1b6dba0c468
5
5
  SHA512:
6
- metadata.gz: bf5f53c1270ec3163061f1b80b4489577b71c3e22688a35abd7197cf76b79e693a8ad4abad785945b9e4bb186e78632e0dd2f3565f690aecad2ed8247ca75920
7
- data.tar.gz: 6c6daf29a06eb0dccbf32a9a278bb130ddee8bdff960123973dc2852d82ff519f245f7dbf21386732f1626a493f48e4bad5b70667650552fa920bbbca2197e9e
6
+ metadata.gz: bc6ea85157f7b0827958854d39a8469ebf58a74d477b1569f05f6974dbe020af76102ba6033a6cfcc0a6673409ae10b91cc561ca01bb49738b91ca826e49b52d
7
+ data.tar.gz: 91636ba4b25e86e9ca13ac596c81fb3cad75d0801c647e376716693ed29129b9d5bb2e3fdb105c4e935d94cd520638a5a6105252fe77d09df2ce171ea10de53f
data/bin/rubycalc CHANGED
@@ -4,19 +4,151 @@ require 'rubygems'
4
4
  require 'calculator'
5
5
  require 'calcex'
6
6
 
7
- $stdout.print "> "
8
- $stdout.flush
7
+ $calc = Calculator.new()
9
8
 
10
- text = gets
9
+ if ARGV.size >= 1
10
+ i=0
11
+ while i < ARGV.size do
12
+ #puts ARGV[i]
11
13
 
12
- $calc = Calculator.new()
14
+ if ARGV[i] == "-v" then
15
+ i=i+1
16
+ var = ARGV[i].split("=")
17
+ nomVar = var[0]
18
+
19
+ valVar = $calc.eval(var[1].to_s+"\n")
20
+
21
+ #puts $calc.public_methods
22
+
23
+ $calc.storeVar(nomVar, valVar)
24
+
25
+ puts nomVar+" <- #{$calc.recallVar(nomVar)}"
26
+
27
+ end
28
+
29
+ if ARGV[i].include? ".calc"
30
+ puts "Lectura de archivos"
31
+ archivo = "lib/#{ARGV[i]}"
32
+
33
+ File.open( archivo ) do |f|
34
+ loop do
35
+ break if not line = f.gets
36
+ #puts "#{f.lineno}: #{line}"
37
+
38
+ text = line
39
+
40
+ begin
41
+ if text.chomp == "" || text.chomp == "exit" then
42
+ exit
43
+ else
44
+ while text.include? "let" do
45
+ #puts text
46
+
47
+ var = text.split("=")
48
+
49
+ #puts var[0].to_s
50
+
51
+ expresion = var[1]
52
+
53
+ #puts expresion.to_s
54
+
55
+ aux = var[0].split(" ")
56
+
57
+ nomVar = aux[1]
58
+
59
+ #puts nomVar.to_s
60
+
61
+ valVar = $calc.eval(var[1].to_s)
62
+
63
+ $calc.storeVar(nomVar, valVar)
64
+
65
+ puts nomVar+" <- #{$calc.recallVar(nomVar)}"
66
+
67
+ $stdout.print "> "
68
+ $stdout.flush
69
+
70
+ text = $stdin.gets
71
+ end
72
+ puts "= " + ($calc.eval(text).to_s)
73
+ end
74
+ rescue ParseError => pe
75
+ puts "Parse Error"
76
+ rescue UnrecognizedTokenException => ute
77
+ puts "UnrecognizedTokenException"
78
+ rescue
79
+ puts "Unkown exception"
80
+ end
81
+ end
82
+ end
83
+ exit
84
+ end
85
+
86
+ if ARGV[i] == "-c"
87
+ puts "Crear .ewe"
88
+ archivo = File.open("lib/a.ewe","w")
89
+ $stdout.print "> "
90
+ $stdout.flush
91
+ text = $stdin.gets
92
+
93
+ archivo.puts $calc.comp(text).to_s
94
+
95
+ archivo.close
96
+
97
+ exit
98
+ end
99
+
100
+ i=i+1
101
+ end
13
102
 
14
- begin
15
- puts "= " #{$calc.eval(text)}
16
- rescue ParseError => pe
17
- puts "Parse Error"
18
- rescue UnrecognizedTokenException => ute
19
- puts "UnrecognizedTokenException"
20
- rescue
21
- puts "Unkown exception"
22
103
  end
104
+
105
+ while true do
106
+
107
+ $stdout.print "> "
108
+ $stdout.flush
109
+
110
+ text = $stdin.gets
111
+
112
+ begin
113
+ if text.chomp == "" || text.chomp == "exit" then
114
+ exit
115
+ else
116
+ while text.include? "let" do
117
+ #puts text
118
+
119
+ var = text.split("=")
120
+
121
+ #puts var[0].to_s
122
+
123
+ expresion = var[1]
124
+
125
+ #puts expresion.to_s
126
+
127
+ aux = var[0].split(" ")
128
+
129
+ nomVar = aux[1]
130
+
131
+ #puts nomVar.to_s
132
+
133
+ valVar = $calc.eval(var[1].to_s)
134
+
135
+ $calc.storeVar(nomVar, valVar)
136
+
137
+ puts nomVar+" <- #{$calc.recallVar(nomVar)}"
138
+
139
+ $stdout.print "> "
140
+ $stdout.flush
141
+
142
+ text = $stdin.gets
143
+ end
144
+ puts "= " + ($calc.eval(text).to_s)
145
+ end
146
+ rescue ParseError => pe
147
+ puts "Parse Error"
148
+ rescue UnrecognizedTokenException => ute
149
+ puts "UnrecognizedTokenException"
150
+ rescue
151
+ puts "Unkown exception"
152
+ end
153
+
154
+ end
data/lib/ast.rb CHANGED
@@ -25,6 +25,18 @@ class AddNode < BinaryNode
25
25
  def evaluate()
26
26
  return @left.evaluate() + @right.evaluate()
27
27
  end
28
+
29
+ def ewe()
30
+ iniciaAN = " sp := sp + tres \n"
31
+ medioAN = "\n sp := sp - tres \n tmp := M[sp + 3] \n"
32
+ medioAN = medioAN + " M[sp + 1] := tmp \n sp := sp + tres \n"
33
+ finAN ="\n sp := sp - tres \n tmp := M[sp + 3] \n"
34
+ finAN = finAN + " M[sp + 2] := tmp \n tmp := M[sp + 1] \n"
35
+ finAN = finAN + " tmp2 := M[sp + 2] \n tmp := tmp + tmp2 \n"
36
+ finAN = finAN + " M[sp + 0] := tmp \n"
37
+ suma = iniciaAN + @left.ewe() + medioAN + @right.ewe() + finAN
38
+ return suma
39
+ end
28
40
  end
29
41
 
30
42
  class SubNode < BinaryNode
@@ -35,6 +47,18 @@ class SubNode < BinaryNode
35
47
  def evaluate()
36
48
  return @left.evaluate() - @right.evaluate()
37
49
  end
50
+
51
+ def ewe()
52
+ iniciaSubN = " sp := sp + tres \n"
53
+ medioSubN = "\n sp := sp - tres \n tmp := M[sp + 3] \n"
54
+ medioSubN+=" M[sp + 1] := tmp \n sp := sp + tres \n"
55
+ finSubN ="\n sp := sp - tres \n tmp := M[sp + 3] \n"
56
+ finSubN+=" M[sp + 2] := tmp \n tmp := M[sp + 1] \n"
57
+ finSubN+=" tmp2 := M[sp + 2] \n tmp := tmp - tmp2 \n"
58
+ finSubN+=" M[sp + 0] := tmp \n"
59
+ sub = iniciaSubN + @left.ewe() + medioSubN + @right.ewe() + finSubN
60
+ return sub
61
+ end
38
62
  end
39
63
 
40
64
  class TimesNode < BinaryNode
@@ -44,15 +68,99 @@ class TimesNode < BinaryNode
44
68
  def evaluate()
45
69
  return @left.evaluate() * @right.evaluate()
46
70
  end
71
+
72
+ def ewe()
73
+ iniciaMN = " sp := sp + tres \n"
74
+ medioMN = "\n sp := sp - tres \n tmp := M[sp + 3] \n"
75
+ medioMN+=" M[sp + 1] := tmp \n sp := sp + tres \n"
76
+ finMN ="\n sp := sp - tres \n tmp := M[sp + 3] \n"
77
+ finMN+=" M[sp + 2] := tmp \n tmp := M[sp + 1] \n"
78
+ finMN+=" tmp2 := M[sp + 2] \n tmp := tmp * tmp2 \n"
79
+ finMN+=" M[sp + 0] := tmp \n"
80
+ times = iniciaMN + @left.ewe() + medioMN + @right.ewe() + finMN
81
+ return times
82
+ end
83
+
47
84
  end
48
85
 
49
- class DivideNode < BinaryNode
86
+ class DivNode < BinaryNode
50
87
  def initialize(lef, right)
51
88
  super(left, right)
52
89
  end
53
90
  def evaluate()
54
91
  return @left.evaluate() / @right.evaluate()
55
92
  end
93
+
94
+ def ewe()
95
+ iniciaDN = " sp := sp + tres \n"
96
+ medioDN = "\n sp := sp - tres \n tmp := M[sp + 3] \n"
97
+ medioDN+=" M[sp + 1] := tmp \n sp := sp + tres \n"
98
+ finDN = "\n sp := sp - tres \n tmp := M[sp + 3] \n"
99
+ finDN+=" M[sp + 2] := tmp \n tmp := M[sp + 1] \n"
100
+ finDN+=" tmp2 := M[sp + 2] \n tmp := tmp / tmp2 \n"
101
+ finDN+=" M[sp + 0] := tmp \n"
102
+ div = iniciaDN + @left.ewe() +medioDN + @right.ewe() + finDN
103
+ return div
104
+ end
105
+ end
106
+
107
+ class ModNode < BinaryNode
108
+ def initialize(lef, right)
109
+ super(left, right)
110
+ end
111
+ def evaluate()
112
+ return @left.evaluate() % @right.evaluate()
113
+ end
114
+
115
+ def ewe()
116
+ iniciaModN = " sp := sp + tres \n"
117
+ medioModN = "\n sp := sp - tres \n tmp := M[sp + 3] \n"
118
+ medioModN+=" M[sp + 1] := tmp \n sp := sp + tres \n";
119
+ finModN = "\n sp := sp - tres \n tmp := M[sp + 3] \n"
120
+ finModN+=" M[sp + 2] := tmp \n tmp := M[sp + 1] \n"
121
+ finModN+=" tmp2 := M[sp + 2] \n tmp := tmp % tmp2 \n"
122
+ finModN+=" M[sp + 0] := tmp \n"
123
+ mod = iniciaModN + @left.ewe() +medioModN + @right.ewe() + finModN
124
+ return mod
125
+ end
126
+ end
127
+
128
+ class PNode < UnaryNode
129
+ def initialize(subtree)
130
+ super(subtree)
131
+ end
132
+ def evaluate()
133
+ plus = @subTree.evaluate()
134
+ memory = $calc.memory
135
+ $calc.memory = plus + memory
136
+ end
137
+
138
+ def ewe()
139
+ iniciaSN = "sp := sp + uno\n"
140
+ finSN = "\n sp := sp - uno\n tmp := M[sp + 1]\n"
141
+ finSN += " M[sp + 0] := tmp\n mem := tmp\n"
142
+ store = iniciaNN + @subTree.ewe().to_s + finSN
143
+ return store
144
+ end
145
+ end
146
+
147
+ class MNode < UnaryNode
148
+ def initialize(subtree)
149
+ super(subtree)
150
+ end
151
+ def evaluate()
152
+ minus = @subTree.evaluate()
153
+ memory = $calc.memory
154
+ $calc.memory = memory - minus
155
+ end
156
+
157
+ def ewe()
158
+ iniciaSN = "sp := sp + uno\n"
159
+ finSN = "\n sp := sp - uno\n tmp := M[sp + 1]\n"
160
+ finSN += " M[sp + 0] := tmp\n mem := tmp\n"
161
+ store = iniciaNN + @subTree.ewe().to_s + finSN
162
+ return store
163
+ end
56
164
  end
57
165
 
58
166
  class StoreNode < UnaryNode
@@ -62,6 +170,14 @@ class StoreNode < UnaryNode
62
170
  def evaluate()
63
171
  $calc.memory = @subTree.evaluate()
64
172
  end
173
+
174
+ def ewe()
175
+ iniciaSN = "sp := sp + uno\n"
176
+ finSN = "\n sp := sp - uno\n tmp := M[sp + 1]\n"
177
+ finSN += " M[sp + 0] := tmp\n mem := tmp\n"
178
+ store = iniciaNN + @subTree.ewe().to_s + finSN
179
+ return store
180
+ end
65
181
  end
66
182
 
67
183
  class NumNode
@@ -72,10 +188,41 @@ class NumNode
72
188
  def evaluate()
73
189
  return @num
74
190
  end
191
+
192
+ def ewe()
193
+ aux = " tmp := " + @num.to_s()
194
+ num = aux + "\n M[sp + 0 ] := tmp"
195
+ return num
196
+ end
197
+ end
198
+
199
+ class IdentifierNod
200
+ def initialize(var, val)
201
+ @var = var
202
+ @val = val
203
+ end
204
+
205
+ def evaluate()
206
+ return @val
207
+ end
208
+
209
+ def ewe()
210
+ return ""
211
+ end
75
212
  end
76
213
 
214
+
215
+
77
216
  class RecallNode
78
217
  def evaluate()
79
218
  $calc.memory
80
219
  end
220
+ def ewe()
221
+ recall = "M[sp + 0] := mem \n"
222
+ return recall
223
+ end
81
224
  end
225
+
226
+
227
+
228
+
data/lib/calculator.rb CHANGED
@@ -3,9 +3,22 @@ require 'ast'
3
3
 
4
4
  class Calculator
5
5
  attr_accessor :memory
6
-
6
+ attr_accessor :nomVar
7
+ attr_accessor :valVar
8
+
7
9
  def initialize()
8
10
  @memory = 0
11
+ @nomVar = []
12
+ @valVar = []
13
+ end
14
+
15
+ def storeVar(key, val)
16
+ @nomVar << key
17
+ @valVar << val
18
+ end
19
+
20
+ def store (val)
21
+ @memory = val
9
22
  end
10
23
 
11
24
  def eval(expr)
@@ -13,4 +26,28 @@ class Calculator
13
26
  ast = parser.parse()
14
27
  return ast.evaluate()
15
28
  end
16
- end
29
+
30
+ def recallVar(key)
31
+ i = 0
32
+ value = 0
33
+ while i < @nomVar.size do
34
+ if @nomVar[i] == key then
35
+ value = @valVar[i]
36
+ end
37
+ i=i+1
38
+ end
39
+ return value
40
+ end
41
+
42
+ def comp(expr)
43
+ parser = Parser.new(StringIO.new(expr))
44
+ inicioConst = "main: sp := 6 \n cero := 0 \n uno := 1 \n tres := 3 \n
45
+ sp := sp + uno \n"
46
+ finalConst = " writeInt(tmp) \n halt \n equ tmp M[0] \n
47
+ equ tmp2 M[1] \n equ uno M[2] \n equ tres M[3] \n equ mem
48
+ M[4] \n equ sp M[5] \n equ stack M[6] equ cero M[7]"
49
+ ast = parser.parse()
50
+ ret = inicioConst + ast.ewe() + finalConst
51
+ return ret
52
+ end
53
+ end
data/lib/parser.rb CHANGED
@@ -2,10 +2,12 @@ require 'ast'
2
2
  require 'scanner'
3
3
  require 'token'
4
4
  require 'calcex'
5
+ require 'calculator'
5
6
 
6
7
  class Parser
7
8
  def initialize(istream)
8
9
  @scan = Scanner.new(istream)
10
+ @todo = true
9
11
  end
10
12
 
11
13
  def parse()
@@ -46,24 +48,26 @@ class Parser
46
48
  end
47
49
 
48
50
  def Term()
49
- return ResTerm(Storable())
51
+ return RestTerm(Storable())
50
52
  end
51
53
 
52
54
  def RestTerm(e)
53
- t = @scan.getToken()
55
+ t = @scan.getToken
54
56
 
55
57
  if t.type == :times then
56
- return RestTerm(TimesNode.new(e,Storable()))
58
+ return RestTerm(TimesNode.new(e,Term()))
57
59
  end
58
60
 
59
61
  if t.type == :divide then
60
- return RestTerm(DivideNode.new(e,Storable()))
62
+ return RestTerm(DivNode.new(e,Term()))
61
63
  end
62
64
 
63
- @scan.putBackToken()
64
-
65
- return e
65
+ if t.type == :mod then
66
+ return RestTerm(ModNode.new(e,Term()))
67
+ end
66
68
 
69
+ @scan.putBackToken
70
+ return e
67
71
  end
68
72
 
69
73
  def Storable()
@@ -74,13 +78,18 @@ class Parser
74
78
  if t.type == :keyword then
75
79
  if t.lex == "S" then
76
80
  return StoreNode.new(factor)
77
- else
78
- raise ParseError.new
81
+ end
82
+ if t.lex == "M" then
83
+ return MNode.new(factor)
84
+ end
85
+ if t.lex == "P" then
86
+ return PNode.new(factor)
79
87
  end
80
88
  end
81
89
 
82
90
  @scan.putBackToken
83
- return factor
91
+
92
+ factor
84
93
  end
85
94
 
86
95
  def Factor()
@@ -90,23 +99,49 @@ class Parser
90
99
  return NumNode.new(t.lex.to_i)
91
100
  end
92
101
 
102
+ if t.type == :identifier then
103
+
104
+ var = t.lex.to_s
105
+ value = $calc.recallVar(t.lex.to_s)
106
+
107
+ return IdentifierNod.new(var, value.to_i)
108
+ end
109
+
93
110
  if t.type == :keyword then
94
111
  if t.lex == "R" then
95
112
  return RecallNode.new
96
- else
97
- raise ParseError.new
113
+ end
114
+ if t.lex == "C" then
115
+ $calc.store(0)
116
+ return RecallNode.new
98
117
  end
99
118
  end
100
-
101
- if t.ype == :lparen then
119
+ if t.type == :lparen then
120
+
102
121
  expr = Expr()
103
- t = @scan.getToken()
122
+ t = @scan.getToken
123
+
104
124
  if t.type == :rparen then
105
125
  return expr
106
126
  else
107
127
  raise ParseError.new
108
128
  end
109
129
  end
130
+
110
131
  raise ParseError.new
111
- end
132
+ end
133
+
134
+ def ExprEWE()
135
+ result = Term()
136
+ result = RestExpr(result)
137
+ return result
138
+ end
112
139
  end
140
+
141
+
142
+
143
+
144
+
145
+
146
+
147
+
data/lib/scanner.rb CHANGED
@@ -4,7 +4,7 @@ require 'calcex'
4
4
  class Scanner
5
5
  def initialize(inStream)
6
6
  @istream = inStream
7
- @keywords = Set.new(["S","R"])
7
+ @keywords = Set.new(["S","R","C","M","P"])
8
8
  @lineCount = 1
9
9
  @colCount = -1
10
10
  @needToken = true
@@ -30,7 +30,7 @@ class Scanner
30
30
  return @lastToken
31
31
  end
32
32
 
33
- until foundOne do
33
+ while !foundOne
34
34
  @colCount = @colCount + 1
35
35
  case state
36
36
  when 0
@@ -43,8 +43,9 @@ class Scanner
43
43
  elsif c == ?- then state = 4
44
44
  elsif c == ?* then state = 5
45
45
  elsif c == ?/ then state = 6
46
- elsif c == ?( then state = 7
47
- elsif c == ?) then state = 8
46
+ elsif c == ?% then state = 7
47
+ elsif c == ?( then state = 8
48
+ elsif c == ?) then state = 9
48
49
  elsif c == ?\n then
49
50
  @colCount = -1
50
51
  @lineCount = @lineCount+1
@@ -86,9 +87,12 @@ class Scanner
86
87
  type = :divide
87
88
  foundOne = true
88
89
  when 7
89
- type = :lparen
90
+ type = :mod
90
91
  foundOne = true
91
92
  when 8
93
+ type = :lparen
94
+ foundOne = true
95
+ when 9
92
96
  type = :rparen
93
97
  foundOne = true
94
98
  end
@@ -117,20 +121,20 @@ class Scanner
117
121
  if c.nil? then
118
122
  return false
119
123
  end
120
- return ((?a <= c and c <= ?z) or (?A <= c and c <= ?Z))
124
+ (?a <= c and c <= ?z) or (?A <= c and c <= ?Z)
121
125
  end
122
126
 
123
127
  def isDigit(c)
124
128
  if c.nil? then
125
129
  return false
126
130
  end
127
- return (?0 <= c and c <= ?9)
131
+ return(?0 <= c and c <= ?9)
128
132
  end
129
133
 
130
134
  def isWhiteSpace(c)
131
135
  if c.nil? then
132
136
  return false
133
137
  end
134
- return (c == ?\ or c == ?\n or c == ?\t)
138
+ return(c == ?\ or c == ?\n or c == ?\t)
135
139
  end
136
140
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: jmejiao5-rubycalc
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.3
4
+ version: 0.0.4
5
5
  platform: ruby
6
6
  authors:
7
7
  - Kent D. Lee - Jose Alberto Mejia