calc_rgotthei 0.0.2 → 0.1.0
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 +4 -4
- data/bin/calc +21 -11
- data/lib/ast.rb +64 -0
- data/lib/calculator.rb +44 -1
- data/lib/parser.rb +87 -14
- data/lib/scanner.rb +9 -1
- metadata +1 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 2d4df24cc58e891784c299bab0e6c8cb7e63ac98
|
4
|
+
data.tar.gz: 356b6b67931241b0edb63310b2a990efaa5a7470
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 797f4d2cc3d70ef8945aa16a8c5ff467f6e65d7b124fff54332301e9fbf1da1f76deeb3f0d39246db266812067c6b5e98a4e91a9806b28909a6f88da31231b95
|
7
|
+
data.tar.gz: 345cd4c742a8cb667312ac09d42a722970fb6b6dec37202172a716a7a4e4000be0589c1e7b2649b794ba860c7dec1bdfae4c9ce5b3f2e50e2d2d7fb16224a55f
|
data/bin/calc
CHANGED
@@ -7,16 +7,26 @@ require 'calcex'
|
|
7
7
|
$stdout.print "> "
|
8
8
|
$stdout.flush
|
9
9
|
|
10
|
-
text = gets
|
11
|
-
|
12
10
|
$calc = Calculator.new()
|
13
11
|
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
|
12
|
+
while(text = gets)
|
13
|
+
|
14
|
+
@res = $calc.eval(text)
|
15
|
+
|
16
|
+
begin
|
17
|
+
if $calc.getChange == 1 then
|
18
|
+
puts "=> " + @res.to_s + $calc.getVars().to_s
|
19
|
+
else
|
20
|
+
puts "=> " + @res.to_s
|
21
|
+
end
|
22
|
+
$calc.setChange
|
23
|
+
$calc.cleanVariables
|
24
|
+
rescue ParseError
|
25
|
+
puts "Parse Error"
|
26
|
+
rescue UnrecognizedTokenException
|
27
|
+
puts "UnrecognizedTokenException"
|
28
|
+
rescue
|
29
|
+
puts "Unkown exception"
|
30
|
+
end
|
31
|
+
$stdout.print "> "
|
32
|
+
end
|
data/lib/ast.rb
CHANGED
@@ -83,3 +83,67 @@ class DivideNode < BinaryNode
|
|
83
83
|
@left.evaluate / @right.evaluate
|
84
84
|
end
|
85
85
|
end
|
86
|
+
|
87
|
+
class ModuleNode < BinaryNode
|
88
|
+
def initialize(left, right)
|
89
|
+
super(left,right)
|
90
|
+
end
|
91
|
+
|
92
|
+
def evaluate
|
93
|
+
@left.evaluate % @right.evaluate
|
94
|
+
end
|
95
|
+
end
|
96
|
+
|
97
|
+
class PlusNode < UnaryNode
|
98
|
+
def initialize(sub)
|
99
|
+
super(sub)
|
100
|
+
end
|
101
|
+
|
102
|
+
def evaluate
|
103
|
+
$calc.memory += subTree.evaluate
|
104
|
+
end
|
105
|
+
end
|
106
|
+
|
107
|
+
class MinusNode < UnaryNode
|
108
|
+
def initialize(sub)
|
109
|
+
super(sub)
|
110
|
+
end
|
111
|
+
|
112
|
+
def evaluate
|
113
|
+
$calc.memory -= subTree.evaluate
|
114
|
+
end
|
115
|
+
end
|
116
|
+
|
117
|
+
class CleanNode
|
118
|
+
def evaluate
|
119
|
+
$calc.memory = 0
|
120
|
+
end
|
121
|
+
end
|
122
|
+
|
123
|
+
class IgualNode < BinaryNode
|
124
|
+
def initialize(left,right)
|
125
|
+
super(left,right)
|
126
|
+
end
|
127
|
+
end
|
128
|
+
|
129
|
+
class ConfigurarNode < UnaryNode
|
130
|
+
def initialize(sub, nombre)
|
131
|
+
super sub
|
132
|
+
@nombre = nombre
|
133
|
+
end
|
134
|
+
|
135
|
+
def evaluate
|
136
|
+
@val = subTree.evaluate
|
137
|
+
$calc.set(@val,@nombre)
|
138
|
+
end
|
139
|
+
end
|
140
|
+
|
141
|
+
class ObtenerNode
|
142
|
+
def initialize(key)
|
143
|
+
@key = key
|
144
|
+
end
|
145
|
+
|
146
|
+
def evaluate
|
147
|
+
$calc.get(@key)
|
148
|
+
end
|
149
|
+
end
|
data/lib/calculator.rb
CHANGED
@@ -1,11 +1,16 @@
|
|
1
1
|
require 'parser'
|
2
2
|
require 'ast'
|
3
|
+
require 'stringio'
|
3
4
|
|
4
5
|
class Calculator
|
5
6
|
attr_accessor :memory
|
6
7
|
|
7
|
-
def initialize
|
8
|
+
def initialize
|
8
9
|
@memory = 0
|
10
|
+
@variables = Hash.new
|
11
|
+
@cadena = ""
|
12
|
+
@count = 0
|
13
|
+
@change = 0
|
9
14
|
end
|
10
15
|
|
11
16
|
def eval(expr)
|
@@ -13,4 +18,42 @@ class Calculator
|
|
13
18
|
ast = parser.parse()
|
14
19
|
return ast.evaluate()
|
15
20
|
end
|
21
|
+
|
22
|
+
def set(val,nom)
|
23
|
+
@change = 1
|
24
|
+
if @count == 0 then
|
25
|
+
@cadena += " #{nom} <- #{val}"
|
26
|
+
else
|
27
|
+
@cadena += ", #{nom} <- #{val}"
|
28
|
+
end
|
29
|
+
@count += 1
|
30
|
+
@variables[nom]=val
|
31
|
+
end
|
32
|
+
|
33
|
+
def get(v)
|
34
|
+
tem = @variables[v]
|
35
|
+
if tem != nil then
|
36
|
+
return tem.to_i
|
37
|
+
else
|
38
|
+
return 0
|
39
|
+
end
|
40
|
+
|
41
|
+
end
|
42
|
+
|
43
|
+
def cleanVariables
|
44
|
+
@count = 0
|
45
|
+
@caracter = ""
|
46
|
+
end
|
47
|
+
|
48
|
+
def getVars
|
49
|
+
return a = " ["+ @cadena + "]"
|
50
|
+
end
|
51
|
+
|
52
|
+
def setChange
|
53
|
+
@change = 0
|
54
|
+
end
|
55
|
+
|
56
|
+
def getChange
|
57
|
+
return @change
|
58
|
+
end
|
16
59
|
end
|
data/lib/parser.rb
CHANGED
@@ -74,30 +74,49 @@ class Parser
|
|
74
74
|
return RestTerm(DivideNode.new(e,Storable()))
|
75
75
|
end
|
76
76
|
|
77
|
+
if t.type == :module then
|
78
|
+
return RestTerm(ModuleNode.new(e,Term()))
|
79
|
+
end
|
80
|
+
|
81
|
+
if t.type == :igual then
|
82
|
+
return RestTerm(IgualNode.new(e,Term()))
|
83
|
+
end
|
84
|
+
|
77
85
|
@scan.putBackToken
|
78
86
|
|
79
87
|
e
|
80
|
-
|
81
|
-
# puts "RestTerm not implemented"
|
82
|
-
# raise ParseError.new # "Parse Error"
|
83
88
|
end
|
84
89
|
|
85
90
|
def Storable()
|
86
91
|
|
87
92
|
result = Factor()
|
88
93
|
|
89
|
-
t = @scan.getToken
|
94
|
+
# t = @scan.getToken
|
90
95
|
|
91
|
-
if t.type == :keyword then
|
92
|
-
|
93
|
-
|
94
|
-
|
95
|
-
|
96
|
-
|
97
|
-
end
|
96
|
+
# if t.type == :keyword then
|
97
|
+
# if t.lex == "S" then
|
98
|
+
# return StoreNode.new(result)
|
99
|
+
# end
|
100
|
+
# puts "Expected S found: ", t.lex
|
101
|
+
# raise ParseError.new
|
98
102
|
|
99
|
-
|
100
|
-
result
|
103
|
+
# if t.lex == "P" then
|
104
|
+
# return PlusNode.new(result)
|
105
|
+
# end
|
106
|
+
# puts "Expected P found: ", t.lex
|
107
|
+
# raise ParseError.new
|
108
|
+
|
109
|
+
# if t.lex == "M" then
|
110
|
+
# return MinusNode.new(result)
|
111
|
+
# end
|
112
|
+
# puts "Expected M found: ", t.lex
|
113
|
+
# raise ParseError.new
|
114
|
+
|
115
|
+
# end
|
116
|
+
|
117
|
+
# @scan.putBackToken
|
118
|
+
|
119
|
+
MemOperation(result)
|
101
120
|
|
102
121
|
end
|
103
122
|
|
@@ -114,6 +133,10 @@ class Parser
|
|
114
133
|
return RecallNode.new
|
115
134
|
end
|
116
135
|
|
136
|
+
if t.lex == "C" then
|
137
|
+
return CleanNode.new
|
138
|
+
end
|
139
|
+
|
117
140
|
puts "Expected R found: "+ t.lex
|
118
141
|
raise ParseError.new
|
119
142
|
end
|
@@ -131,8 +154,58 @@ class Parser
|
|
131
154
|
raise ParseError.new
|
132
155
|
end
|
133
156
|
|
157
|
+
if t.type == :identifier then
|
158
|
+
return Assignable(t.lex)
|
159
|
+
end
|
160
|
+
|
134
161
|
puts "Expected number, R, ( found: " + t.type.to_s
|
135
162
|
raise ParseError.new
|
136
163
|
|
137
|
-
end
|
164
|
+
end
|
165
|
+
|
166
|
+
def MemOperation(e)
|
167
|
+
t = @scan.getToken
|
168
|
+
|
169
|
+
if t.type == :keyword then
|
170
|
+
if t.lex == "S" then
|
171
|
+
return StoreNode.new(e)
|
172
|
+
end
|
173
|
+
|
174
|
+
if t.lex == "P" then
|
175
|
+
return PlusNode.new(e)
|
176
|
+
end
|
177
|
+
|
178
|
+
if t.lex == "M" then
|
179
|
+
return MinusNode.new(e)
|
180
|
+
end
|
181
|
+
|
182
|
+
puts "Syntax error: ", t.lex
|
183
|
+
raise ParseError.new
|
184
|
+
|
185
|
+
|
186
|
+
end
|
187
|
+
|
188
|
+
@scan.putBackToken
|
189
|
+
e
|
190
|
+
end
|
191
|
+
|
192
|
+
def Assignable(a)
|
193
|
+
t = @scan.getToken
|
194
|
+
|
195
|
+
if t.type == :igual then
|
196
|
+
return Assign(a)
|
197
|
+
end
|
198
|
+
|
199
|
+
@scan.putBackToken
|
200
|
+
|
201
|
+
return ObtenerNode.new(a)
|
202
|
+
end
|
203
|
+
|
204
|
+
def Assign(nombre)
|
205
|
+
result = Expr()
|
206
|
+
|
207
|
+
return ConfigurarNode.new(result, nombre)
|
208
|
+
|
209
|
+
@scan.putBackToken
|
210
|
+
end
|
138
211
|
end
|
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","P","M","C"])
|
8
8
|
@lineCount = 1
|
9
9
|
@colCount = -1
|
10
10
|
@needToken = true
|
@@ -45,6 +45,8 @@ class Scanner
|
|
45
45
|
elsif c == ?/ then state = 6
|
46
46
|
elsif c == ?( then state = 7
|
47
47
|
elsif c == ?) then state = 8
|
48
|
+
elsif c == ?% then state = 9
|
49
|
+
elsif c == ?= then state = 10
|
48
50
|
elsif c == ?\n then
|
49
51
|
@colCount = -1
|
50
52
|
@lineCount = @lineCount+1
|
@@ -91,6 +93,12 @@ class Scanner
|
|
91
93
|
when 8
|
92
94
|
type = :rparen
|
93
95
|
foundOne = true
|
96
|
+
when 9
|
97
|
+
type = :module
|
98
|
+
foundOne = true
|
99
|
+
when 10
|
100
|
+
type = :igual
|
101
|
+
foundOne = true
|
94
102
|
end
|
95
103
|
|
96
104
|
if !foundOne then
|