calc_jgomez88 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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 61a2b5a27d01d7760aedb6ed9a2715c2dda44b8c
4
- data.tar.gz: 844adc3e28d0d7e5e88d7dc32686127bb6133aa6
3
+ metadata.gz: 484290abe479b03f1415c585afc1a4debdf5782f
4
+ data.tar.gz: 334f22367cc64dda2c42dc19d79348e6eec3b243
5
5
  SHA512:
6
- metadata.gz: 8a8d4dd2b9d7cd230d39111c4f9626645dab902c6dcfc9740a9aafa5fd22dcadf78bcc03f313ffed0faefb50ed9abe781925535e2c12eb8b3e0e7a2319c47328
7
- data.tar.gz: ff5f8da5c84f4f1d62fc44435944d42edacb81fa3ce755c045f75e2e95416f81c569641c1c183fc21fbf2fc4c687b5f36eed26bf47f7f646179b25577d0635c1
6
+ metadata.gz: 6b659ce91e3389dcb216a9a514f4b7e0c155216f3212097f5185edf9f7768d77b9453d30c68a3b45f208f70f6c420cd9bf872a80d39bc22225aa93eabe2619d8
7
+ data.tar.gz: 82759f854a7eef1867082729122dabe99630dd99839505c404b1134bfbdf285d5cb13e6f3b4adcc9881a1b33191f10ea9bc4a249a35607ec81c830bd70de7f31
data/bin/calc CHANGED
@@ -8,16 +8,26 @@ require 'calcex'
8
8
  $stdout.print "> "
9
9
  $stdout.flush
10
10
 
11
- text = gets
12
-
13
11
  $calc = Calculator.new()
12
+ entro = true #Varaible para saber si entro datos
13
+ text = gets
14
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"
15
+ while entro do
16
+ begin
17
+ puts "=> " + $calc.eval(text).to_s
18
+ rescue ParseError
19
+ puts "Parse Error"
20
+ rescue UnrecognizedTokenException
21
+ puts "UnrecognizedTokenException"
22
+ rescue
23
+ puts "Unkown exception"
24
+ end
25
+ $stdout.print "> "
26
+ if text = gets then #Verifica si entro otra linea
27
+ entro = true
28
+ else
29
+ entro = false
30
+ end
23
31
  end
32
+
33
+
data/lib/ast.rb CHANGED
@@ -58,10 +58,19 @@ class DivideNode < BinaryNode
58
58
  return @left.evaluate() / @right.evaluate()
59
59
  end
60
60
  end
61
+ #modulo--------------------------------------------------
62
+ class ModuleNode < BinaryNode
63
+ def initialize(left, right)
64
+ super(left,right)
65
+ end
61
66
 
67
+ def evaluate()
68
+ return @left.evaluate() % @right.evaluate()
69
+ end
70
+ end
71
+ #------------------------------------------------------------
62
72
 
63
73
  class StoreNode < UnaryNode
64
- #
65
74
  def initialize(subTree)
66
75
  super(subTree)
67
76
  end
@@ -71,13 +80,59 @@ class StoreNode < UnaryNode
71
80
  end
72
81
  end
73
82
 
83
+ #plus---------------------------------
84
+ class PlusNode < UnaryNode
85
+ def initialize(subTree)
86
+ super(subTree)
87
+ end
88
+ def evaluate
89
+ $calc.memory =$calc.memory + subTree.evaluate()
90
+ end
91
+ end
92
+ #----------------------------------------
93
+ #minus-------------------------------------
94
+ class MinusNode < UnaryNode
95
+ def initialize(subTree)
96
+ super(subTree)
97
+ end
98
+ def evaluate
99
+ $calc.memory =$calc.memory - subTree.evaluate()
100
+ end
101
+ end
102
+
103
+ #----------------------------------------
74
104
  class RecallNode
75
- #
105
+
76
106
  def evaluate
77
107
  $calc.memory
78
108
  end
79
109
  end
80
-
110
+ #------------------------------------
111
+
112
+ class AssignableNode < UnaryNode
113
+ def initialize(valor1,nombre)
114
+ super(valor1)
115
+ @name=nombre
116
+ end
117
+
118
+ def evaluate
119
+ guardar=@subTree.evaluate()
120
+ $calc.addVar(@name,guardar)
121
+ return guardar
122
+ end
123
+ end
124
+ #-----------------------------------------
125
+
126
+
127
+ #C----------------------------------------
128
+ class ClearNode
129
+ def evaluate
130
+ $calc.memory=$calc.memory-$calc.memory#0
131
+
132
+ end
133
+ end
134
+ #----------------------------------------
135
+
81
136
  class NumNode
82
137
  def initialize(num) #
83
138
  @num = num
data/lib/calculator.rb CHANGED
@@ -7,6 +7,7 @@ class Calculator
7
7
 
8
8
  def initialize()
9
9
  @memory = 0
10
+ @varmap = Hash.new() #crea un mapax
10
11
  end
11
12
 
12
13
  def eval(expr)
@@ -14,4 +15,12 @@ class Calculator
14
15
  ast = parser.parse()
15
16
  return ast.evaluate()
16
17
  end
18
+
19
+ def addVar(nombre,valor)
20
+ @varmap[nombre]=valor
21
+ end
22
+ def getVar(valor)
23
+ return @varmap[valor]
24
+ end
25
+
17
26
  end
data/lib/parser.rb CHANGED
@@ -8,7 +8,7 @@ class Parser
8
8
  @scan = Scanner.new(istream)
9
9
  #recibe un flujo de datos
10
10
  end
11
-
11
+
12
12
  def parse()
13
13
  return Prog()
14
14
  end
@@ -32,21 +32,21 @@ class Parser
32
32
  def Expr()
33
33
  return RestExpr(Term())
34
34
  end
35
-
35
+
36
36
  def RestExpr(e)
37
37
  t = @scan.getToken()
38
38
  #leo el token
39
39
 
40
40
  if t.type == :add then
41
- return RestExpr(AddNode.new(e,Term()))
41
+ return RestExpr(AddNode.new(e,Term()))
42
42
  end
43
43
 
44
44
  if t.type == :sub then
45
45
  return RestExpr(SubNode.new(e,Term()))
46
46
  end
47
-
47
+
48
48
  @scan.putBackToken()
49
-
49
+
50
50
 
51
51
  return e
52
52
  end
@@ -67,7 +67,7 @@ class Parser
67
67
  # raise ParseError.new
68
68
  RestTerm(Storable())
69
69
  end
70
-
70
+
71
71
  def RestTerm(e)
72
72
 
73
73
  # puts "RestTerm not implemented"
@@ -75,12 +75,15 @@ class Parser
75
75
  #lo borramos
76
76
  t = @scan.getToken
77
77
  if t.type == :times
78
- return RestTerm (TimesNode.new(e, Storable()))
78
+ return RestTerm(TimesNode.new(e, Storable()))
79
79
  end
80
80
  if t.type == :divide
81
81
  #mira el tipo de token
82
82
  return RestTerm(DivideNode.new(e, Storable()))
83
83
  end
84
+ if t.type == :mod
85
+ return RestTerm(ModuleNode.new(e, Storable()))
86
+ end
84
87
  @scan.putBackToken
85
88
 
86
89
  return e
@@ -91,19 +94,44 @@ class Parser
91
94
  # puts "Storable not implemented"
92
95
  # raise ParseError.new # "Parse Error"
93
96
  #lo borramos
94
- result=Factor()
95
- t = @scan.getToken
97
+ #-------------------------------------------
98
+ # result=Factor()
99
+ # t = @scan.getToken
100
+ # if t.type== :keyword then
101
+ # if t.lex == "S" then
102
+ # return StoreNode.new(result)
103
+ #end
104
+ # puts "Expected S found: ",t.lex
105
+ # raise ParseError.new
106
+ # end
107
+ # @scan.putBackToken
108
+ # return result
109
+ # end
110
+ #----------------------------------------------------
111
+ return MemOperation(Factor())
112
+ end
113
+ #------------------------------------------------------
114
+
115
+
116
+ def MemOperation(result)
117
+ # result=Factor()
118
+ t= @scan.getToken
96
119
  if t.type== :keyword then
97
120
  if t.lex == "S" then
98
121
  return StoreNode.new(result)
122
+ elsif t.lex == "P" then
123
+ return PlusNode.new(result)
124
+ elsif t.lex == "M" then
125
+ return MinusNode.new(result)
99
126
  end
100
- puts "Expected S found: ",t.lex
101
- raise ParseError.new
127
+ puts "Expected S || P || M found: ",t.lex
128
+ raise ParseError
102
129
  end
103
130
  @scan.putBackToken
104
- return result
131
+ return result
105
132
  end
106
133
 
134
+
107
135
  def Factor()
108
136
 
109
137
  # puts "Factor not implemented"
@@ -117,22 +145,43 @@ class Parser
117
145
  if t.type == :keyword then
118
146
  if t.lex == "R" then
119
147
  return RecallNode.new
148
+ elsif t.lex == "C" then
149
+ return ClearNode.new
120
150
  end
121
- puts "Expected R found: " + t.lex
151
+ puts "Expected R || C found: " + t.lex
122
152
  raise ParseError.new
123
153
  end
124
154
  if t.type == :lparen then
125
155
  result = Expr()
126
156
  t=@scan.getToken
127
-
128
157
  if t.type == :rparen then
129
158
  return result
130
159
  end
131
-
132
160
  puts "Expected ) found: " + t.type.to_s
133
161
  raise ParseError.new
134
162
  end
163
+ if t.type == :identifier then
164
+ return Assignable(t)
165
+ end
135
166
  puts "Expected number,R ( found: " + t.type.to_s
136
167
  raise ParseError
137
168
  end
169
+
170
+
171
+ def Assignable(t)
172
+ r=@scan.getToken
173
+ if r.type == :Asignav
174
+ return assign(t)
175
+ end
176
+ nombre=t.lex
177
+ @scan.putBackToken
178
+ return NumNode.new($calc.getVar(nombre))
179
+ end
180
+
181
+ def assign(t)
182
+ result=Expr()
183
+ paso=result.evaluate()
184
+ nombre=t.lex
185
+ return AssignableNode.new(NumNode.new(paso), nombre)
138
186
  end
187
+ end
data/lib/scanner.rb CHANGED
@@ -7,7 +7,7 @@ class Scanner
7
7
  def initialize(inStream)
8
8
  @istream = inStream
9
9
  #flujo de entrada
10
- @keywords = Set.new(["S","R"])
10
+ @keywords = Set.new(["S","R","P","M","C","="])
11
11
  @lineCount = 1
12
12
  @colCount = -1
13
13
  @needToken = true
@@ -44,7 +44,7 @@ class Scanner
44
44
  column = @colCount
45
45
  line = @lineCount
46
46
  if isLetter(c) then state=1
47
- #pregunta de que tipo de caracter
47
+ #pregunta de que tipo de caracter
48
48
  elsif isDigit(c) then state=2
49
49
  elsif c == ?+ then state = 3
50
50
  elsif c == ?- then state = 4
@@ -52,6 +52,8 @@ class Scanner
52
52
  elsif c == ?/ then state = 6
53
53
  elsif c == ?( then state = 7
54
54
  elsif c == ?) then state = 8
55
+ elsif c == ?% then state = 9
56
+ elsif c == ?= then state = 10
55
57
  elsif c == ?\n then
56
58
  @colCount = -1
57
59
  @lineCount = @lineCount+1
@@ -100,7 +102,15 @@ class Scanner
100
102
  when 8
101
103
  type = :rparen
102
104
  foundOne = true
105
+ when 9
106
+ type = :mod
107
+ foundOne = true
108
+ when 10
109
+ type = :Asignav
110
+ foundOne = true
103
111
  end
112
+
113
+
104
114
 
105
115
  if !foundOne then
106
116
  lex.concat(c)
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: calc_jgomez88
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.2
4
+ version: 0.0.3
5
5
  platform: ruby
6
6
  authors:
7
7
  - Kent D. Lee - Juan Francisco Cardona Mc-juan camilo gomez
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2015-11-12 00:00:00.000000000 Z
11
+ date: 2015-11-22 00:00:00.000000000 Z
12
12
  dependencies: []
13
13
  description: An calculator implementation on ruby
14
14
  email: fcardona@eafit.edu.co-jgomez88@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.7
47
+ rubygems_version: 2.4.8
48
48
  signing_key:
49
49
  specification_version: 4
50
50
  summary: Another calculator in ruby