calc_jloaiz16 0.0.2 → 0.0.3
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/bin/calc +15 -12
- data/lib/ast.rb +72 -19
- data/lib/calcex.rb +0 -0
- data/lib/calculator.rb +14 -0
- data/lib/parser.rb +49 -34
- data/lib/scanner.rb +13 -5
- data/lib/token.rb +0 -0
- metadata +3 -3
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 7e1763c84f4f111060403fbe966b73d13e1a8eee
|
4
|
+
data.tar.gz: ae2a9593713a72541e56037e77a56eb99be79123
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 47d9baac765afccd480e400d4d06689c96564d6cc5d4df92ed494f273f2edaf0b823aaaea3ff1f8e60c45e343595c632ba29ff1ce652fdc16e1ddfd4038b14df
|
7
|
+
data.tar.gz: 90623321dbc1fa9ae638187b99580903cc17e50f4c260338b8c7984770f9626092beeb7aec82581d2d6da715eeab85a74070b6deaffa74422d172eeefa4a6a45
|
data/bin/calc
CHANGED
@@ -4,19 +4,22 @@ require 'rubygems'
|
|
4
4
|
require 'calculator'
|
5
5
|
require 'calcex'
|
6
6
|
|
7
|
+
$calc = Calculator.new()
|
8
|
+
|
7
9
|
$stdout.print "> "
|
8
10
|
$stdout.flush
|
9
11
|
|
10
|
-
text = gets
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
rescue
|
17
|
-
|
18
|
-
rescue
|
19
|
-
|
20
|
-
|
21
|
-
|
12
|
+
while text = gets do
|
13
|
+
|
14
|
+
begin
|
15
|
+
puts "=> " + $calc.eval(text).to_s
|
16
|
+
rescue ParseError
|
17
|
+
puts "Parse Error"
|
18
|
+
rescue UnrecognizedTokenException
|
19
|
+
puts "UnrecognizedTokenException"
|
20
|
+
rescue
|
21
|
+
puts "Unkown exception"
|
22
|
+
end
|
23
|
+
$stdout.print "> "
|
24
|
+
|
22
25
|
end
|
data/lib/ast.rb
CHANGED
@@ -27,38 +27,66 @@ class AddNode < BinaryNode
|
|
27
27
|
end
|
28
28
|
end
|
29
29
|
|
30
|
-
|
31
|
-
class
|
30
|
+
|
31
|
+
class SubNode < BinaryNode
|
32
32
|
def initialize(left, right)
|
33
33
|
super(left,right)
|
34
34
|
end
|
35
35
|
|
36
36
|
def evaluate()
|
37
|
-
@left.evaluate()
|
37
|
+
@left.evaluate() - @right.evaluate()
|
38
38
|
end
|
39
39
|
end
|
40
40
|
|
41
|
-
class
|
41
|
+
class StoreNode < UnaryNode
|
42
|
+
def initialize(subT)
|
43
|
+
super(subT)
|
44
|
+
end
|
45
|
+
def evaluate
|
46
|
+
$calc.memory = subTree.evaluate()
|
47
|
+
end
|
48
|
+
end
|
49
|
+
|
50
|
+
class RecallNode
|
51
|
+
def evaluate
|
52
|
+
$calc.memory
|
53
|
+
end
|
54
|
+
end
|
55
|
+
|
56
|
+
class NumNode
|
57
|
+
def initialize(num)
|
58
|
+
@num = num
|
59
|
+
end
|
60
|
+
|
61
|
+
def evaluate
|
62
|
+
@num
|
63
|
+
end
|
64
|
+
end
|
65
|
+
|
66
|
+
#Se agregaron TimesNode y DivideNode
|
67
|
+
class TimesNode < BinaryNode
|
42
68
|
def initialize(left, right)
|
43
69
|
super(left,right)
|
44
70
|
end
|
45
71
|
|
46
72
|
def evaluate()
|
47
|
-
@left.evaluate()
|
73
|
+
@left.evaluate() * @right.evaluate()
|
48
74
|
end
|
49
75
|
end
|
50
76
|
|
51
|
-
class
|
77
|
+
class DivideNode < BinaryNode
|
52
78
|
def initialize(left, right)
|
53
79
|
super(left,right)
|
54
80
|
end
|
55
81
|
|
56
82
|
def evaluate()
|
57
|
-
@left.evaluate()
|
83
|
+
@left.evaluate() / @right.evaluate()
|
58
84
|
end
|
59
85
|
end
|
60
86
|
|
61
|
-
|
87
|
+
# -- Modificaciones calculadora -- #
|
88
|
+
|
89
|
+
#clase Modulo
|
62
90
|
class MdNode < BinaryNode
|
63
91
|
def initialize(left, right)
|
64
92
|
super(left,right)
|
@@ -68,28 +96,53 @@ class MdNode < BinaryNode
|
|
68
96
|
@left.evaluate() % @right.evaluate()
|
69
97
|
end
|
70
98
|
end
|
71
|
-
|
72
|
-
|
99
|
+
|
100
|
+
#Clase plus
|
101
|
+
class PlusNode < UnaryNode
|
102
|
+
def initialize(subTree)
|
103
|
+
super(subTree)
|
104
|
+
end
|
105
|
+
def evaluate
|
106
|
+
$calc.memory = $calc.memory + subTree.evaluate()
|
107
|
+
end
|
108
|
+
end
|
109
|
+
|
110
|
+
#Clase Minus
|
111
|
+
class MinusNode < UnaryNode
|
73
112
|
def initialize(subT)
|
74
113
|
super(subT)
|
75
114
|
end
|
76
115
|
def evaluate
|
77
|
-
$calc.memory = subTree.evaluate
|
116
|
+
$calc.memory = $calc.memory - subTree.evaluate()
|
78
117
|
end
|
79
118
|
end
|
80
119
|
|
81
|
-
|
120
|
+
#Clase Clear
|
121
|
+
class ClearNode
|
82
122
|
def evaluate
|
83
|
-
$calc.memory
|
123
|
+
$calc.memory = 0
|
84
124
|
end
|
85
125
|
end
|
86
126
|
|
87
|
-
|
88
|
-
|
89
|
-
|
127
|
+
#Clase para asignar
|
128
|
+
class AssigNode < UnaryNode
|
129
|
+
def initialize(subT, nom)
|
130
|
+
super(subT)
|
131
|
+
@nombre = nom
|
90
132
|
end
|
91
|
-
|
92
|
-
|
93
|
-
@
|
133
|
+
def evaluate
|
134
|
+
value = @subTree.evaluate()
|
135
|
+
$calc.storevar(@nombre, value)
|
136
|
+
return value
|
137
|
+
end
|
138
|
+
end
|
139
|
+
|
140
|
+
#Obtener valor de memoria
|
141
|
+
class VarNode
|
142
|
+
def initialize(nom)
|
143
|
+
@nombre = nom
|
144
|
+
end
|
145
|
+
def evaluate
|
146
|
+
$calc.recallvar(@nombre)
|
94
147
|
end
|
95
148
|
end
|
data/lib/calcex.rb
CHANGED
File without changes
|
data/lib/calculator.rb
CHANGED
@@ -6,6 +6,7 @@ class Calculator
|
|
6
6
|
|
7
7
|
def initialize()
|
8
8
|
@memory = 0
|
9
|
+
@h = Hash.new()
|
9
10
|
end
|
10
11
|
|
11
12
|
def eval(expr)
|
@@ -13,4 +14,17 @@ class Calculator
|
|
13
14
|
ast = parser.parse()
|
14
15
|
return ast.evaluate()
|
15
16
|
end
|
17
|
+
|
18
|
+
def storevar(nombre,valor)
|
19
|
+
@h[nombre] = valor
|
20
|
+
end
|
21
|
+
|
22
|
+
def recallvar(nombre)
|
23
|
+
tem = @h[nombre]
|
24
|
+
if tem == nil then
|
25
|
+
return 0
|
26
|
+
else
|
27
|
+
return tem
|
28
|
+
end
|
29
|
+
end
|
16
30
|
end
|
data/lib/parser.rb
CHANGED
@@ -18,8 +18,8 @@ class Parser
|
|
18
18
|
t = @scan.getToken()
|
19
19
|
|
20
20
|
if t.type != :eof then
|
21
|
-
|
22
|
-
|
21
|
+
#print "Expected EOF. Found ", t.type, ".\n"
|
22
|
+
#raise ParseError.new
|
23
23
|
end
|
24
24
|
|
25
25
|
return result
|
@@ -46,19 +46,6 @@ class Parser
|
|
46
46
|
end
|
47
47
|
|
48
48
|
def Term()
|
49
|
-
# Write your Term() code here. This code is just temporary
|
50
|
-
# so you can try the calculator out before finishing it.
|
51
|
-
|
52
|
-
#t = @scan.getToken()
|
53
|
-
|
54
|
-
#if t.type == :number then
|
55
|
-
# val = t.lex.to_i
|
56
|
-
#return NumNode.new(val)
|
57
|
-
#end
|
58
|
-
|
59
|
-
#puts "Term not implemented\n"
|
60
|
-
|
61
|
-
#raise ParseError.new
|
62
49
|
RestTerm(Storable())
|
63
50
|
end
|
64
51
|
|
@@ -71,53 +58,81 @@ class Parser
|
|
71
58
|
if t.type == :divide then
|
72
59
|
return RestTerm(DivideNode.new(e,Storable()))
|
73
60
|
end
|
61
|
+
#Agregamos modulo
|
62
|
+
if t.type == :md then
|
63
|
+
return RestTerm(MdNode.new(e,Storable()))
|
64
|
+
end
|
74
65
|
|
75
66
|
@scan.putBackToken
|
76
67
|
|
77
68
|
return e
|
78
69
|
end
|
79
|
-
|
70
|
+
|
80
71
|
def Storable()
|
81
|
-
|
82
|
-
|
83
|
-
|
72
|
+
return MemOperation(Factor())
|
73
|
+
end
|
74
|
+
|
75
|
+
def MemOperation(result)
|
76
|
+
t= @scan.getToken
|
77
|
+
if t.type== :keyword then
|
84
78
|
if t.lex == "S" then
|
85
79
|
return StoreNode.new(result)
|
80
|
+
elsif t.lex == "P" then
|
81
|
+
return PlusNode.new(result)
|
82
|
+
elsif t.lex == "M" then
|
83
|
+
return MinusNode.new(result)
|
86
84
|
end
|
87
|
-
puts "Expected S found: "
|
88
|
-
raise
|
85
|
+
puts "Expected S || P || M found: ",t.lex
|
86
|
+
raise ParseError.new
|
89
87
|
end
|
90
|
-
|
91
88
|
@scan.putBackToken
|
92
89
|
return result
|
93
90
|
end
|
94
|
-
|
95
|
-
def Factor()
|
96
|
-
t = @scan.getToken
|
97
91
|
|
92
|
+
def Factor()
|
93
|
+
t = @scan.getToken
|
98
94
|
if t.type == :number then
|
99
95
|
return NumNode.new(t.lex.to_i)
|
100
96
|
end
|
101
|
-
|
102
97
|
if t.type == :keyword then
|
103
98
|
if t.lex == "R" then
|
104
99
|
return RecallNode.new
|
100
|
+
elsif t.lex == "C" then
|
101
|
+
return ClearNode.new
|
105
102
|
end
|
106
|
-
puts "Expected R found: "
|
103
|
+
puts "Expected R || C found: " + t.lex
|
107
104
|
raise ParseError.new
|
108
105
|
end
|
109
|
-
|
110
106
|
if t.type == :lparen then
|
111
107
|
result = Expr()
|
112
|
-
t
|
108
|
+
t=@scan.getToken
|
113
109
|
if t.type == :rparen then
|
114
110
|
return result
|
115
111
|
end
|
116
|
-
puts "Expected ) found: "
|
112
|
+
puts "Expected ) found: " + t.type.to_s
|
117
113
|
raise ParseError.new
|
118
114
|
end
|
119
|
-
|
120
|
-
|
121
|
-
|
122
|
-
|
115
|
+
if t.type == :identifier
|
116
|
+
return Assignable(t)
|
117
|
+
end
|
118
|
+
puts "Expected number || R || ( found: " + t.type.to_s
|
119
|
+
raise ParseError
|
120
|
+
end
|
121
|
+
|
122
|
+
def Assignable(t)
|
123
|
+
return Assign(t)
|
124
|
+
end
|
125
|
+
|
126
|
+
def Assign(t)
|
127
|
+
t2=@scan.getToken
|
128
|
+
nombre = t.lex
|
129
|
+
if t2.type == :igual then
|
130
|
+
result = Expr()
|
131
|
+
val = result.evaluate
|
132
|
+
return AssigNode.new(NumNode.new(val), nombre)
|
133
|
+
else
|
134
|
+
@scan.putBackToken
|
135
|
+
return VarNode.new(nombre)
|
136
|
+
end
|
137
|
+
end
|
123
138
|
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
|
@@ -30,7 +30,7 @@ class Scanner
|
|
30
30
|
return @lastToken
|
31
31
|
end
|
32
32
|
|
33
|
-
|
33
|
+
until foundOne
|
34
34
|
@colCount = @colCount + 1
|
35
35
|
case state
|
36
36
|
when 0
|
@@ -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 = :md
|
98
|
+
foundOne = true
|
99
|
+
when 10
|
100
|
+
type = :igual
|
101
|
+
foundOne = true
|
94
102
|
end
|
95
103
|
|
96
104
|
if !foundOne then
|
@@ -99,7 +107,7 @@ class Scanner
|
|
99
107
|
end
|
100
108
|
|
101
109
|
end
|
102
|
-
|
110
|
+
|
103
111
|
@istream.ungetc(c)
|
104
112
|
@colCount = @colCount - 1
|
105
113
|
if type == :number or type == :identifier or type == :keyword then
|
@@ -116,11 +124,11 @@ class Scanner
|
|
116
124
|
def isLetter(c)
|
117
125
|
return ((?a <= c and c <= ?z) or (?A <= c and c <= ?Z))
|
118
126
|
end
|
119
|
-
|
127
|
+
|
120
128
|
def isDigit(c)
|
121
129
|
return (?0 <= c and c <= ?9)
|
122
130
|
end
|
123
|
-
|
131
|
+
|
124
132
|
def isWhiteSpace(c)
|
125
133
|
return (c == ?\ or c == ?\n or c == ?\t)
|
126
134
|
end
|
data/lib/token.rb
CHANGED
File without changes
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: calc_jloaiz16
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.3
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Kent D. Lee - Juan Francisco Cardona Mc - Juan David Loaiza
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2015-11-
|
11
|
+
date: 2015-11-22 00:00:00.000000000 Z
|
12
12
|
dependencies: []
|
13
13
|
description: An calculator implementation on ruby
|
14
14
|
email: jloaiz16@eafit.edu.co
|
@@ -44,7 +44,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
44
44
|
version: '0'
|
45
45
|
requirements: []
|
46
46
|
rubyforge_project:
|
47
|
-
rubygems_version: 2.4.
|
47
|
+
rubygems_version: 2.4.8
|
48
48
|
signing_key:
|
49
49
|
specification_version: 4
|
50
50
|
summary: Another calculator in ruby
|