calc_fcardona 0.0.3 → 0.2.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/lib/ast.rb +3 -4
- data/lib/calculator.rb +1 -1
- data/lib/parser.rb +22 -23
- data/lib/scanner.rb +4 -4
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: b58be65020fec2385269041f11aa7df98b79aae1
|
4
|
+
data.tar.gz: 3234dc68ec5d71647472a0cb622224b2b1134d0f
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 9308eb98ecde6506f5e12d5dd804e9216c23f3dd1504c66d7260a52cc982f31c29ecfb3ed124e0a8b245bb14a1e20f75ad73047355a4472e149d9e62ab939dea
|
7
|
+
data.tar.gz: d259a8ec214ae0262045981e6cf21fc2c5070fec6c5be0cdfb5141c43e6fb15a199f9bc4655839dc718e529a7a47d76842d8ae025f772cb9d015831d851e2731
|
data/lib/ast.rb
CHANGED
@@ -23,7 +23,7 @@ class AddNode < BinaryNode
|
|
23
23
|
end
|
24
24
|
|
25
25
|
def evaluate
|
26
|
-
|
26
|
+
left.evaluate + right.evaluate
|
27
27
|
end
|
28
28
|
end
|
29
29
|
|
@@ -32,7 +32,7 @@ class SubNode < BinaryNode
|
|
32
32
|
super(left,right)
|
33
33
|
end
|
34
34
|
|
35
|
-
def evaluate
|
35
|
+
def evaluate
|
36
36
|
left.evaluate - right.evaluate
|
37
37
|
end
|
38
38
|
end
|
@@ -42,7 +42,7 @@ class TimesNode < BinaryNode
|
|
42
42
|
super(left,right)
|
43
43
|
end
|
44
44
|
|
45
|
-
def evaluate
|
45
|
+
def evaluate
|
46
46
|
left.evaluate * right.evaluate
|
47
47
|
end
|
48
48
|
end
|
@@ -61,7 +61,6 @@ class StoreNode < UnaryNode
|
|
61
61
|
def initialize(sub)
|
62
62
|
super(sub)
|
63
63
|
end
|
64
|
-
|
65
64
|
def evaluate
|
66
65
|
$calc.memory = subTree.evaluate
|
67
66
|
end
|
data/lib/calculator.rb
CHANGED
data/lib/parser.rb
CHANGED
@@ -8,35 +8,35 @@ class Parser
|
|
8
8
|
@scan = Scanner.new(istream)
|
9
9
|
end
|
10
10
|
|
11
|
-
def parse
|
12
|
-
|
11
|
+
def parse
|
12
|
+
Prog()
|
13
13
|
end
|
14
14
|
|
15
15
|
private
|
16
|
-
def Prog
|
16
|
+
def Prog
|
17
17
|
result = Expr()
|
18
|
-
t = @scan.getToken
|
18
|
+
t = @scan.getToken
|
19
19
|
|
20
20
|
if t.type != :eof then
|
21
21
|
print "Expected EOF. Found ", t.type, ".\n"
|
22
22
|
raise ParseError.new
|
23
23
|
end
|
24
24
|
|
25
|
-
|
25
|
+
result
|
26
26
|
end
|
27
27
|
|
28
|
-
def Expr
|
28
|
+
def Expr
|
29
29
|
RestExpr(Term())
|
30
30
|
end
|
31
31
|
|
32
32
|
def RestExpr(e)
|
33
|
-
t = @scan.getToken
|
33
|
+
t = @scan.getToken
|
34
34
|
|
35
|
-
if t.type == :add
|
35
|
+
if t.type == :add then
|
36
36
|
return RestExpr(AddNode.new(e,Term()))
|
37
37
|
end
|
38
38
|
|
39
|
-
if t.type == :sub
|
39
|
+
if t.type == :sub then
|
40
40
|
return RestExpr(SubNode.new(e,Term()))
|
41
41
|
end
|
42
42
|
|
@@ -45,12 +45,12 @@ class Parser
|
|
45
45
|
e
|
46
46
|
end
|
47
47
|
|
48
|
-
def Term
|
49
|
-
|
48
|
+
def Term()
|
50
49
|
RestTerm(Storable())
|
51
50
|
end
|
52
51
|
|
53
52
|
def RestTerm(e)
|
53
|
+
|
54
54
|
t = @scan.getToken
|
55
55
|
|
56
56
|
if t.type == :times then
|
@@ -66,8 +66,7 @@ class Parser
|
|
66
66
|
e
|
67
67
|
end
|
68
68
|
|
69
|
-
def Storable
|
70
|
-
|
69
|
+
def Storable
|
71
70
|
result = Factor()
|
72
71
|
|
73
72
|
t = @scan.getToken
|
@@ -76,16 +75,18 @@ class Parser
|
|
76
75
|
if t.lex == "S" then
|
77
76
|
return StoreNode.new(result)
|
78
77
|
end
|
79
|
-
|
78
|
+
print "Expected S found #{t.lex}"
|
79
|
+
puts " at line: #{t.line} col: #{t.col}"
|
80
80
|
raise ParseError.new
|
81
81
|
end
|
82
|
+
|
82
83
|
|
83
84
|
@scan.putBackToken
|
85
|
+
|
84
86
|
result
|
85
87
|
end
|
86
88
|
|
87
|
-
def Factor
|
88
|
-
|
89
|
+
def Factor
|
89
90
|
t = @scan.getToken
|
90
91
|
|
91
92
|
if t.type == :number then
|
@@ -97,24 +98,22 @@ class Parser
|
|
97
98
|
return RecallNode.new
|
98
99
|
end
|
99
100
|
|
100
|
-
puts "
|
101
|
+
puts "Error"
|
101
102
|
raise ParseError.new
|
102
103
|
end
|
103
104
|
|
104
105
|
if t.type == :lparen then
|
105
106
|
result = Expr()
|
106
|
-
|
107
107
|
t = @scan.getToken
|
108
|
-
|
109
108
|
if t.type == :rparen then
|
110
109
|
return result
|
111
110
|
end
|
112
|
-
|
113
|
-
puts "Expected ) found: " + t.type.to_s
|
111
|
+
puts "Error"
|
114
112
|
raise ParseError.new
|
115
113
|
end
|
116
114
|
|
117
|
-
puts "
|
118
|
-
raise ParseError.new
|
115
|
+
puts "Error"
|
116
|
+
raise ParseError.new
|
117
|
+
|
119
118
|
end
|
120
119
|
end
|
data/lib/scanner.rb
CHANGED
@@ -11,12 +11,12 @@ class Scanner
|
|
11
11
|
@lastToken = nil
|
12
12
|
end
|
13
13
|
|
14
|
-
def putBackToken
|
14
|
+
def putBackToken()
|
15
15
|
@needToken = false
|
16
16
|
end
|
17
17
|
|
18
|
-
def getToken
|
19
|
-
|
18
|
+
def getToken()
|
19
|
+
if !@needToken
|
20
20
|
@needToken = true
|
21
21
|
return @lastToken
|
22
22
|
end
|
@@ -30,7 +30,7 @@ class Scanner
|
|
30
30
|
return @lastToken
|
31
31
|
end
|
32
32
|
|
33
|
-
|
33
|
+
while !foundOne
|
34
34
|
@colCount = @colCount + 1
|
35
35
|
case state
|
36
36
|
when 0
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: calc_fcardona
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0
|
4
|
+
version: 0.2.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Kent D. Lee - Juan Francisco Cardona Mc
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2015-11-
|
11
|
+
date: 2015-11-13 00:00:00.000000000 Z
|
12
12
|
dependencies: []
|
13
13
|
description: An calculator implementation on ruby
|
14
14
|
email: fcardona@eafit.edu.co
|