calc_dsalaz26 0.0.1 → 0.0.2
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 +16 -14
- data/lib/ast.rb +67 -2
- data/lib/calculator.rb +4 -0
- data/lib/parser.rb +57 -13
- data/lib/scanner.rb +12 -5
- 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: 1c24d48dc0ab7733702f0c1468332146150ad405
|
4
|
+
data.tar.gz: dec3877c75106826c36bdf81c97c9ec178e66af1
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 4c2348ed89ecf116d335ee176f87a2c75cdf864ca779af98c29504bd78419069888aff45aef5ec79cfcefac9e174c8a2665d3082016ff7b3775bbd7a8076580a
|
7
|
+
data.tar.gz: 16b5f1145be09892b6c9a5e58b6524a68c2cf766b5e1d6269abee2fde6503d02dc9169bde55433f24e1b7efc410ea8c7a711f7361223d4a0915ed904d024d7dc
|
data/bin/calc
CHANGED
@@ -4,19 +4,21 @@ require 'rubygems'
|
|
4
4
|
require 'calculator'
|
5
5
|
require 'calcex'
|
6
6
|
|
7
|
-
|
8
|
-
$stdout.
|
7
|
+
while 2>1 do
|
8
|
+
$stdout.print "> "
|
9
|
+
$stdout.flush
|
10
|
+
|
11
|
+
text = gets
|
9
12
|
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
rescue
|
17
|
-
|
18
|
-
rescue
|
19
|
-
|
20
|
-
|
21
|
-
puts "Unkown exception"
|
13
|
+
$calc = Calculator.new()
|
14
|
+
|
15
|
+
begin
|
16
|
+
puts "= " + $calc.eval(text).to_s
|
17
|
+
rescue ParseError
|
18
|
+
puts "Parse Error"
|
19
|
+
rescue UnrecognizedTokenException
|
20
|
+
puts "UnrecognizedTokenException"
|
21
|
+
rescue
|
22
|
+
puts "Unkown exception"
|
23
|
+
end
|
22
24
|
end
|
data/lib/ast.rb
CHANGED
@@ -1,4 +1,6 @@
|
|
1
|
+
# coding: utf-8
|
1
2
|
require 'set'
|
3
|
+
##require 'hash'
|
2
4
|
|
3
5
|
class BinaryNode
|
4
6
|
attr_reader :left, :right
|
@@ -38,8 +40,8 @@ class TimesNode < BinaryNode
|
|
38
40
|
end
|
39
41
|
|
40
42
|
class DivideNode < BinaryNode
|
41
|
-
def initialize(left,right)
|
42
|
-
super(left,right)
|
43
|
+
def initialize(left, right)
|
44
|
+
super(left, right)
|
43
45
|
end
|
44
46
|
|
45
47
|
def evaluate()
|
@@ -57,6 +59,15 @@ class SubNode < BinaryNode
|
|
57
59
|
end
|
58
60
|
end
|
59
61
|
|
62
|
+
class ModuleNode < BinaryNode
|
63
|
+
def initialize(left,right)
|
64
|
+
super(left,right)
|
65
|
+
end
|
66
|
+
def evaluate()
|
67
|
+
return @left.evaluate % @right.evaluate()
|
68
|
+
end
|
69
|
+
end
|
70
|
+
|
60
71
|
class StoreNode < UnaryNode
|
61
72
|
def initialize(subTree)
|
62
73
|
super(subTree)
|
@@ -82,3 +93,57 @@ class RecallNode
|
|
82
93
|
$calc.memory
|
83
94
|
end
|
84
95
|
end
|
96
|
+
|
97
|
+
class CleanNode
|
98
|
+
def evaluate()
|
99
|
+
$calc.memory = 0
|
100
|
+
end
|
101
|
+
end
|
102
|
+
|
103
|
+
class MinusNode < UnaryNode
|
104
|
+
def initialize(subTree)
|
105
|
+
super(subTree)
|
106
|
+
end
|
107
|
+
|
108
|
+
def evaluate()
|
109
|
+
$calc.memory = $calc.memory - subTree.evaluate()
|
110
|
+
end
|
111
|
+
end
|
112
|
+
|
113
|
+
class PlusNode < UnaryNode
|
114
|
+
def initialize(subTree)
|
115
|
+
super(subTree)
|
116
|
+
end
|
117
|
+
def evaluate()
|
118
|
+
valor = $calc.memory
|
119
|
+
$calc.memory = valor + subTree.evaluate()
|
120
|
+
end
|
121
|
+
end
|
122
|
+
|
123
|
+
class VariableNode < UnaryNode
|
124
|
+
def initialize(variable, subTree)
|
125
|
+
super(subTree)
|
126
|
+
@variable=variable
|
127
|
+
end
|
128
|
+
def evaluate()
|
129
|
+
puts "Va a entrar"
|
130
|
+
$calc.memory2[@variable]= subTree.evaluate()
|
131
|
+
puts "Entró #{@variable}"
|
132
|
+
|
133
|
+
return subTree.evaluate()
|
134
|
+
end
|
135
|
+
end
|
136
|
+
|
137
|
+
class BuscarNode
|
138
|
+
def initialize(variable)
|
139
|
+
@variable = variable
|
140
|
+
end
|
141
|
+
def evaluate()
|
142
|
+
var = @variable
|
143
|
+
if $calc.memory2[var] != nil then
|
144
|
+
return $calc.memory2[var]
|
145
|
+
else
|
146
|
+
return 0
|
147
|
+
end
|
148
|
+
end
|
149
|
+
end
|
data/lib/calculator.rb
CHANGED
data/lib/parser.rb
CHANGED
@@ -1,3 +1,4 @@
|
|
1
|
+
# coding: utf-8
|
1
2
|
require 'ast'
|
2
3
|
require 'stringio'
|
3
4
|
require 'scanner'
|
@@ -54,15 +55,15 @@ class Parser
|
|
54
55
|
t = @scan.getToken()
|
55
56
|
|
56
57
|
if t.type == :times then
|
57
|
-
return RestTerm(TimesNode.new(e, Storable))
|
58
|
+
return RestTerm(TimesNode.new(e, Storable()))
|
58
59
|
end
|
59
60
|
|
60
61
|
if t.type == :divide then
|
61
|
-
return
|
62
|
+
return RestTerm(DivideNode.new(e, Storable()))
|
62
63
|
end
|
63
64
|
|
64
|
-
if t.type == :
|
65
|
-
return
|
65
|
+
if t.type == :module then
|
66
|
+
return RestTerm(ModuleNode.new(e, Storable()))
|
66
67
|
end
|
67
68
|
|
68
69
|
@scan.putBackToken()
|
@@ -78,18 +79,28 @@ class Parser
|
|
78
79
|
t = @scan.getToken
|
79
80
|
|
80
81
|
if t.type == :keyword then
|
81
|
-
|
82
|
-
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
|
-
end
|
82
|
+
return MemOperation(result,t)
|
87
83
|
end
|
84
|
+
|
88
85
|
@scan.putBackToken
|
89
86
|
|
90
87
|
result
|
91
88
|
|
92
89
|
end
|
90
|
+
|
91
|
+
def MemOperation (result,t)
|
92
|
+
if t.lex == "S" then
|
93
|
+
return StoreNode.new(result)
|
94
|
+
elsif t.lex == "M" then
|
95
|
+
return MinusNode.new(result)
|
96
|
+
elsif t.lex == "P" then
|
97
|
+
return PlusNode.new(result)
|
98
|
+
else
|
99
|
+
puts "Syntax Error: found other keyword "
|
100
|
+
puts "at line: #{t.line} col: #{t.col}"
|
101
|
+
raise ParseError.new
|
102
|
+
end
|
103
|
+
end
|
93
104
|
|
94
105
|
def Factor
|
95
106
|
t = @scan.getToken
|
@@ -99,10 +110,12 @@ class Parser
|
|
99
110
|
if t.type == :keyword then
|
100
111
|
if t.lex == "R" then
|
101
112
|
return RecallNode.new
|
113
|
+
elsif t.lex == "C" then
|
114
|
+
return CleanNode.new
|
115
|
+
else
|
116
|
+
puts "*Expected R or C found: #{t.lex} at line: #{t.line} col: #{t.col}"
|
117
|
+
raise ParserError.new # "Parse Error"
|
102
118
|
end
|
103
|
-
else
|
104
|
-
puts "*Expected s found: #{t.lex} at line: #{t.line} col: #{t.col}"
|
105
|
-
raise ParserError.new # "Parse Error"
|
106
119
|
end
|
107
120
|
if t.type == :lparen then
|
108
121
|
result = Expr()
|
@@ -115,7 +128,38 @@ class Parser
|
|
115
128
|
raise ParseError.new
|
116
129
|
end
|
117
130
|
|
131
|
+
@scan.putBackToken
|
132
|
+
return Assignable()
|
133
|
+
|
118
134
|
puts "Factor not implemented"
|
119
135
|
raise ParserError.new # "Parse Error"
|
120
136
|
end
|
137
|
+
|
138
|
+
def Assignable
|
139
|
+
t = @scan.getToken
|
140
|
+
puts "Va a pasar"
|
141
|
+
if t.type == :identifier then
|
142
|
+
puts "Pasó"
|
143
|
+
return Assign(t.lex)
|
144
|
+
else
|
145
|
+
puts "Expected identifier, found: #{t.type}"
|
146
|
+
raise PerseError.new
|
147
|
+
end
|
148
|
+
end
|
149
|
+
|
150
|
+
def Assign (name)
|
151
|
+
|
152
|
+
##return name
|
153
|
+
puts "Hola"
|
154
|
+
t=@scan.getToken
|
155
|
+
if t.type == :igual then
|
156
|
+
puts "Hola2"
|
157
|
+
return VariableNode.new(name,Expr())
|
158
|
+
end
|
159
|
+
|
160
|
+
@scan.putBackToken
|
161
|
+
puts "Hola 3"
|
162
|
+
return BuscarNode.new(name)
|
163
|
+
end
|
164
|
+
|
121
165
|
end
|
data/lib/scanner.rb
CHANGED
@@ -4,8 +4,7 @@ require 'calcex'
|
|
4
4
|
class Scanner
|
5
5
|
def initialize(inStream)
|
6
6
|
@istream = inStream
|
7
|
-
@keywords = Set.new(%w{S R})
|
8
|
-
#@keywords = Set.new(["S","R"])
|
7
|
+
@keywords = Set.new(%w{S R C M P})
|
9
8
|
@lineCount = 1
|
10
9
|
@colCount = -1
|
11
10
|
@needToken = true
|
@@ -17,7 +16,7 @@ class Scanner
|
|
17
16
|
end
|
18
17
|
|
19
18
|
def getToken()
|
20
|
-
|
19
|
+
unless @needToken
|
21
20
|
@needToken = true
|
22
21
|
return @lastToken
|
23
22
|
end
|
@@ -31,8 +30,8 @@ class Scanner
|
|
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 = ""
|
@@ -46,6 +45,8 @@ class Scanner
|
|
46
45
|
elsif c == ?/ then state = 6
|
47
46
|
elsif c == ?( then state = 7
|
48
47
|
elsif c == ?) then state = 8
|
48
|
+
elsif c == ?% then state = 9
|
49
|
+
elsif c == ?= then state = 10
|
49
50
|
elsif c == ?\n then
|
50
51
|
@colCount = -1
|
51
52
|
@lineCount = @lineCount+1
|
@@ -92,6 +93,12 @@ class Scanner
|
|
92
93
|
when 8
|
93
94
|
type = :rparen
|
94
95
|
foundOne = true
|
96
|
+
when 9
|
97
|
+
type = :module
|
98
|
+
foundOne = true
|
99
|
+
when 10
|
100
|
+
type = :igual
|
101
|
+
foundOne = true
|
95
102
|
end
|
96
103
|
|
97
104
|
if !foundOne then
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: calc_dsalaz26
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.2
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Kent D. Lee - Juan Francisco Cardona Mc - Diego Salazar Noreña
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2015-11-
|
11
|
+
date: 2015-11-23 00:00:00.000000000 Z
|
12
12
|
dependencies: []
|
13
13
|
description: An calculator implementation on ruby
|
14
14
|
email: dsalaz26@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
|