calc_mlopez12 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/lib/ast.rb +27 -28
- data/lib/calculator.rb +2 -1
- data/lib/parser.rb +65 -50
- data/lib/scanner.rb +7 -8
- 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: b3971fbee53f7f69d11557ffd1631574a1a5bc5c
|
|
4
|
+
data.tar.gz: 35fcf2b7a203fef9f7a9013094c2dd6bc6f95528
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
6
|
+
metadata.gz: 94eecb78945a447b6c8aea10c0bd7b8265e07452f53cec57f213f6b79995d7be7db44b9553ffe7bd99d1e1606206620c4555fda4fbe35bf7b11129db5d1132e1
|
|
7
|
+
data.tar.gz: 1417bde73ec921023d4aa1c01eea01110b39a7a3c682c4a796084d786c51f250efbd062e93b1e0a497d9207b423bdd9a5f6203485bcd2d1ab0d5d4221e31745a
|
data/lib/ast.rb
CHANGED
|
@@ -22,63 +22,62 @@ class AddNode < BinaryNode
|
|
|
22
22
|
super(left,right)
|
|
23
23
|
end
|
|
24
24
|
|
|
25
|
-
def evaluate
|
|
26
|
-
|
|
25
|
+
def evaluate
|
|
26
|
+
@left.evaluate() + @right.evaluate()
|
|
27
27
|
end
|
|
28
28
|
end
|
|
29
29
|
|
|
30
|
-
class
|
|
31
|
-
def initialize(left,right)
|
|
30
|
+
class SubNode < BinaryNode
|
|
31
|
+
def initialize(left, right)
|
|
32
32
|
super(left,right)
|
|
33
33
|
end
|
|
34
|
-
|
|
35
|
-
def evaluate
|
|
36
|
-
|
|
34
|
+
|
|
35
|
+
def evaluate
|
|
36
|
+
@left.evaluate() - @right.evaluate()
|
|
37
37
|
end
|
|
38
38
|
end
|
|
39
39
|
|
|
40
|
-
class
|
|
41
|
-
def initialize(left,right)
|
|
40
|
+
class TimesNode < BinaryNode
|
|
41
|
+
def initialize(left, right)
|
|
42
42
|
super(left,right)
|
|
43
43
|
end
|
|
44
|
-
|
|
45
|
-
def evaluate
|
|
46
|
-
|
|
44
|
+
|
|
45
|
+
def evaluate
|
|
46
|
+
@left.evaluate() * @right.evaluate()
|
|
47
47
|
end
|
|
48
48
|
end
|
|
49
49
|
|
|
50
|
-
class
|
|
50
|
+
class DivideNode < BinaryNode
|
|
51
51
|
def initialize(left, right)
|
|
52
52
|
super(left,right)
|
|
53
53
|
end
|
|
54
54
|
|
|
55
|
-
def evaluate
|
|
56
|
-
|
|
55
|
+
def evaluate
|
|
56
|
+
@left.evaluate() / @right.evaluate()
|
|
57
57
|
end
|
|
58
58
|
end
|
|
59
59
|
|
|
60
60
|
class StoreNode < UnaryNode
|
|
61
|
-
def initialize(
|
|
62
|
-
super(
|
|
61
|
+
def initialize(sub)
|
|
62
|
+
super(sub)
|
|
63
63
|
end
|
|
64
|
+
def evaluate
|
|
65
|
+
$calc.memory = @subTree.evaluate()
|
|
66
|
+
end
|
|
67
|
+
end
|
|
64
68
|
|
|
65
|
-
|
|
66
|
-
|
|
69
|
+
class RecallNode
|
|
70
|
+
def evaluate
|
|
71
|
+
$calc.memory
|
|
67
72
|
end
|
|
68
73
|
end
|
|
69
74
|
|
|
70
75
|
class NumNode
|
|
71
|
-
def initialize
|
|
76
|
+
def initialize num
|
|
72
77
|
@num = num
|
|
73
78
|
end
|
|
74
79
|
|
|
75
|
-
def evaluate
|
|
76
|
-
|
|
77
|
-
end
|
|
78
|
-
end
|
|
79
|
-
|
|
80
|
-
class RecallNode
|
|
81
|
-
def evaluate()
|
|
82
|
-
$calc.memory
|
|
80
|
+
def evaluate
|
|
81
|
+
@num
|
|
83
82
|
end
|
|
84
83
|
end
|
data/lib/calculator.rb
CHANGED
data/lib/parser.rb
CHANGED
|
@@ -1,5 +1,4 @@
|
|
|
1
1
|
require 'ast'
|
|
2
|
-
require 'stringio'
|
|
3
2
|
require 'scanner'
|
|
4
3
|
require 'token'
|
|
5
4
|
require 'calcex'
|
|
@@ -9,8 +8,8 @@ class Parser
|
|
|
9
8
|
@scan = Scanner.new(istream)
|
|
10
9
|
end
|
|
11
10
|
|
|
12
|
-
def parse
|
|
13
|
-
return Prog()
|
|
11
|
+
def parse
|
|
12
|
+
return Prog()
|
|
14
13
|
end
|
|
15
14
|
|
|
16
15
|
private
|
|
@@ -23,18 +22,18 @@ class Parser
|
|
|
23
22
|
raise ParseError.new
|
|
24
23
|
end
|
|
25
24
|
|
|
26
|
-
|
|
25
|
+
result
|
|
27
26
|
end
|
|
28
27
|
|
|
29
28
|
def Expr()
|
|
30
|
-
|
|
29
|
+
RestExpr(Term())
|
|
31
30
|
end
|
|
32
31
|
|
|
33
32
|
def RestExpr(e)
|
|
34
|
-
t = @scan.getToken
|
|
33
|
+
t = @scan.getToken
|
|
35
34
|
|
|
36
35
|
if t.type == :add then
|
|
37
|
-
|
|
36
|
+
return RestExpr(AddNode.new(e,Term()))
|
|
38
37
|
end
|
|
39
38
|
|
|
40
39
|
if t.type == :sub then
|
|
@@ -43,79 +42,95 @@ class Parser
|
|
|
43
42
|
|
|
44
43
|
@scan.putBackToken()
|
|
45
44
|
|
|
46
|
-
|
|
45
|
+
e
|
|
47
46
|
end
|
|
48
|
-
|
|
49
|
-
def Term
|
|
50
|
-
|
|
47
|
+
|
|
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
|
+
RestTerm(Storable())
|
|
51
63
|
end
|
|
52
|
-
|
|
64
|
+
|
|
53
65
|
def RestTerm(e)
|
|
54
|
-
|
|
55
|
-
|
|
66
|
+
|
|
67
|
+
t = @scan.getToken
|
|
56
68
|
if t.type == :times then
|
|
57
|
-
return RestTerm(TimesNode.new(e,
|
|
69
|
+
return RestTerm(TimesNode.new(e,Storable()))
|
|
58
70
|
end
|
|
59
|
-
|
|
71
|
+
|
|
60
72
|
if t.type == :divide then
|
|
61
|
-
return
|
|
73
|
+
return RestTerm(DivideNode.new(e,Storable()))
|
|
62
74
|
end
|
|
63
|
-
|
|
64
|
-
if t.type == :mod then
|
|
65
|
-
return Resterm(ModNode.new(e, Storable))
|
|
66
|
-
end
|
|
67
|
-
|
|
75
|
+
|
|
68
76
|
@scan.putBackToken()
|
|
69
|
-
|
|
77
|
+
|
|
70
78
|
e
|
|
71
79
|
|
|
72
80
|
end
|
|
73
|
-
|
|
74
|
-
def Storable
|
|
75
|
-
|
|
76
|
-
result=Factor()
|
|
77
|
-
|
|
81
|
+
|
|
82
|
+
def Storable()
|
|
83
|
+
result = Factor()
|
|
78
84
|
t = @scan.getToken
|
|
79
85
|
|
|
80
86
|
if t.type == :keyword then
|
|
81
|
-
if t.lex == "S" then
|
|
87
|
+
if t.lex == "S" then
|
|
82
88
|
return StoreNode.new(result)
|
|
83
|
-
else
|
|
84
|
-
puts "*Expected s found: #{t.lex} at line: #{t.line} col: #{t.col}"
|
|
85
|
-
raise ParserError.new # "Parse Error"
|
|
86
89
|
end
|
|
90
|
+
puts "Expected S found: #{t.lex} at line: #{t.line} col: #{t.col}"
|
|
91
|
+
raise ParseErro.new
|
|
87
92
|
end
|
|
88
|
-
@scan.putBackToken
|
|
89
|
-
|
|
93
|
+
@scan.putBackToken()
|
|
94
|
+
|
|
90
95
|
result
|
|
91
|
-
|
|
96
|
+
|
|
97
|
+
|
|
98
|
+
# puts "Storable not implemented"
|
|
99
|
+
# raise ParseError.new # "Parse Error"
|
|
92
100
|
end
|
|
93
|
-
|
|
94
|
-
def Factor
|
|
95
|
-
|
|
101
|
+
|
|
102
|
+
def Factor()
|
|
103
|
+
|
|
104
|
+
t = @scan.getToken()
|
|
105
|
+
|
|
96
106
|
if t.type == :number then
|
|
97
107
|
return NumNode.new(t.lex.to_i)
|
|
98
108
|
end
|
|
109
|
+
|
|
99
110
|
if t.type == :keyword then
|
|
100
|
-
if t.lex == "R" then
|
|
101
|
-
return RecallNode.new
|
|
111
|
+
if t.lex == "R" then
|
|
112
|
+
return RecallNode.new()
|
|
102
113
|
end
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
raise ParserError.new # "Parse Error"
|
|
114
|
+
puts "Expected R found: #{t.lex} at line: " + t.line.to_s + " col: " + t.col.to_s
|
|
115
|
+
raise ParseError.new
|
|
106
116
|
end
|
|
117
|
+
|
|
107
118
|
if t.type == :lparen then
|
|
108
|
-
|
|
119
|
+
expresion = Expr()
|
|
109
120
|
t = @scan.getToken
|
|
121
|
+
|
|
110
122
|
if t.type == :rparen then
|
|
111
|
-
|
|
123
|
+
expresion
|
|
112
124
|
end
|
|
113
|
-
puts "
|
|
114
|
-
puts "at line: #{t.line} col: #{t.col}"
|
|
125
|
+
puts "Syntax error expected ) at line: #{t.line}"
|
|
115
126
|
raise ParseError.new
|
|
116
127
|
end
|
|
117
128
|
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
129
|
+
print "Parser Error: expected number, R, ( found: #{t.lext}"
|
|
130
|
+
puts "at line #{t.line} col: #{t.col}"
|
|
131
|
+
raise ParseError.new
|
|
132
|
+
|
|
133
|
+
# puts "Factor not implemented"
|
|
134
|
+
# raise ParserError.new # "Parse Error"
|
|
135
|
+
end
|
|
121
136
|
end
|
data/lib/scanner.rb
CHANGED
|
@@ -4,35 +4,34 @@ require 'calcex'
|
|
|
4
4
|
class Scanner
|
|
5
5
|
def initialize(inStream)
|
|
6
6
|
@istream = inStream
|
|
7
|
-
@keywords = Set.new(%
|
|
8
|
-
#@keywords = Set.new(["S","R"])
|
|
7
|
+
@keywords = Set.new(%W{S R})
|
|
9
8
|
@lineCount = 1
|
|
10
9
|
@colCount = -1
|
|
11
10
|
@needToken = true
|
|
12
11
|
@lastToken = nil
|
|
13
12
|
end
|
|
14
13
|
|
|
15
|
-
def putBackToken
|
|
14
|
+
def putBackToken
|
|
16
15
|
@needToken = false
|
|
17
16
|
end
|
|
18
17
|
|
|
19
|
-
def getToken
|
|
20
|
-
|
|
18
|
+
def getToken
|
|
19
|
+
unless !@needToken
|
|
21
20
|
@needToken = true
|
|
22
21
|
return @lastToken
|
|
23
22
|
end
|
|
24
23
|
|
|
25
24
|
state = 0
|
|
26
25
|
foundOne = false
|
|
27
|
-
c = @istream.getc
|
|
26
|
+
c = @istream.getc
|
|
28
27
|
|
|
29
28
|
if @istream.eof? then
|
|
30
29
|
@lastToken = Token.new(:eof,@lineCount,@colCount)
|
|
31
30
|
return @lastToken
|
|
32
31
|
end
|
|
33
32
|
|
|
34
|
-
|
|
35
|
-
@colCount
|
|
33
|
+
until foundOne
|
|
34
|
+
@colCount += 1
|
|
36
35
|
case state
|
|
37
36
|
when 0
|
|
38
37
|
lex = ""
|
metadata
CHANGED
|
@@ -1,14 +1,14 @@
|
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
|
2
2
|
name: calc_mlopez12
|
|
3
3
|
version: !ruby/object:Gem::Version
|
|
4
|
-
version: 0.0
|
|
4
|
+
version: 0.1.0
|
|
5
5
|
platform: ruby
|
|
6
6
|
authors:
|
|
7
7
|
- Kent D. Lee - Juan Francisco Cardona Mc - Mayerli López
|
|
8
8
|
autorequire:
|
|
9
9
|
bindir: bin
|
|
10
10
|
cert_chain: []
|
|
11
|
-
date: 2015-11-
|
|
11
|
+
date: 2015-11-12 00:00:00.000000000 Z
|
|
12
12
|
dependencies: []
|
|
13
13
|
description: An calculator implementation on ruby
|
|
14
14
|
email: mlopez12@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.7
|
|
48
48
|
signing_key:
|
|
49
49
|
specification_version: 4
|
|
50
50
|
summary: Another calculator in ruby
|