lmarinl1-rubycalc 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.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: fe9c00e4eb1ae4c5583e5dc7752334e04b9eecfd
4
+ data.tar.gz: e14178ba17c48944255bc2404cb360b767fcade9
5
+ SHA512:
6
+ metadata.gz: 4b165bb47121e6e5242ced51cde4807240283c1df2c9a1da6e14da6a43570a4b80da7d1c362c2ae18b81cb6266595475cebb5b0d030129e83ffdd96de9ed4065
7
+ data.tar.gz: 63aaabe08fdfe48d6c4553783793b1cd3be3e25ce0bce7243138991233a4de383094ead2a88a43c611bc16fa7db75584ef75ca6da24e39f4e19cef361cd31649
@@ -0,0 +1,22 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require 'rubygems'
4
+ require 'calculator'
5
+ require 'calcex'
6
+
7
+ $stdout.print "> "
8
+ $stdout.flush
9
+
10
+ text = gets
11
+
12
+ $calc = Calculator.new()
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
@@ -0,0 +1,95 @@
1
+ require 'set'
2
+
3
+ class BinaryNode
4
+ attr_reader :left, :right
5
+
6
+ def initialize(left,right)
7
+ @left = left
8
+ @right = right
9
+ end
10
+ end
11
+
12
+ class UnaryNode
13
+ attr_reader :subTree
14
+
15
+ def initialize(subTree)
16
+ @subTree = subTree
17
+ end
18
+ end
19
+
20
+ class AddNode < BinaryNode
21
+ def initialize(left, right)
22
+ super(left,right)
23
+ end
24
+
25
+ def evaluate()
26
+ @left.evaluate() + @right.evaluate()
27
+ end
28
+ end
29
+
30
+ class SubNode < BinaryNode
31
+ def initialize(left, right)
32
+ super(left,right)
33
+ end
34
+
35
+ def evaluate()
36
+ @left.evaluate() - @right.evaluate()
37
+ end
38
+ end
39
+
40
+ class TimesNode < BinaryNode
41
+ def initialize(left, right)
42
+ super(left,right)
43
+ end
44
+
45
+ def evaluate()
46
+ @left.evaluate() * @right.evaluate()
47
+ end
48
+ end
49
+
50
+ class DivideNode < BinaryNode
51
+ def initialize(left, right)
52
+ super(left,right)
53
+ end
54
+
55
+ def evaluate()
56
+ @left.evaluate() / @right.evaluate()
57
+ end
58
+ end
59
+
60
+ class ModNode < BinaryNode
61
+ def initialize(left, right)
62
+ super(left,right)
63
+ end
64
+
65
+ def evaluate()
66
+ @left.evaluate() % @right.evaluate()
67
+ end
68
+ end
69
+
70
+
71
+ class NumNode
72
+ def initialize(num)
73
+ @num = num
74
+ end
75
+
76
+ def evaluate()
77
+ return @num
78
+ end
79
+ end
80
+
81
+ class StoreNode < UnaryNode
82
+ def inizialice(sub)
83
+ super(sub)
84
+ end
85
+
86
+ def evaluate()
87
+ $calc.memory = @subTree.evaluate
88
+ end
89
+ end
90
+
91
+ class RecallNode
92
+ def evaluate()
93
+ $calc.memory
94
+ end
95
+ end
@@ -0,0 +1,3 @@
1
+
2
+ class ParseError < Exception; end
3
+ class UnrecognizedTokenException < Exception; end
@@ -0,0 +1,16 @@
1
+ require 'parser'
2
+ require 'ast'
3
+
4
+ class Calculator
5
+ attr_accessor :memory
6
+
7
+ def initialize()
8
+ @memory = 0
9
+ end
10
+
11
+ def eval(expr)
12
+ parser = Parser.new(StringIO.new(expr))
13
+ ast = parser.parse()
14
+ return ast.evaluate()
15
+ end
16
+ end
@@ -0,0 +1,138 @@
1
+ require 'ast'
2
+ require 'scanner'
3
+ require 'token'
4
+ require 'calcex'
5
+
6
+ class Parser
7
+ def initialize(istream)
8
+ @scan = Scanner.new(istream)
9
+ end
10
+
11
+ def parse()
12
+ return Prog()
13
+ end
14
+
15
+ #-----private public protected ------------
16
+
17
+ def Prog()
18
+ result = Expr()
19
+ t = @scan.getToken()
20
+
21
+ unless t.type == :eof then
22
+ print "Expected EOF. Found ", t.type, ".\n"
23
+ raise ParseError.new
24
+ end
25
+
26
+ return result
27
+ end
28
+
29
+ def Expr()
30
+ return RestExpr(Term())
31
+ end
32
+
33
+ def RestExpr(e)
34
+ t = @scan.getToken()
35
+
36
+ if t.type == :add then
37
+ return RestExpr(AddNode.new(e,Term()))
38
+ end
39
+
40
+ if t.type == :sub then
41
+ return RestExpr(SubNode.new(e,Term()))
42
+ end
43
+
44
+ @scan.putBackToken()
45
+
46
+ return e
47
+ end
48
+
49
+ def Term()
50
+ # Write your Term() code here. This code is just temporary
51
+ # so you can try the calculator out before finishing it.
52
+
53
+ # t = @scan.getToken()
54
+
55
+ # if t.type == :number then
56
+ # val = t.lex.to_i
57
+ # return NumNode.new(val)
58
+ # end
59
+
60
+ # puts "Term not implemented\n"
61
+
62
+ # raise ParseError.new
63
+ RestTerm(Storable())
64
+ end
65
+
66
+ def RestTerm(e)
67
+
68
+ # puts "RestTerm not implemented"
69
+ # raise ParseError.new # "Parse Error"
70
+
71
+ t = @scan.getToken
72
+ if t.type == :times then
73
+ return RestTerm(TimesNode.new(e, Storable()))
74
+ end
75
+
76
+ if t.type == :divide then
77
+ return RestTerm(DivideNode.new(e, Storable()))
78
+ end
79
+
80
+ @scan.putBackToken
81
+ return e
82
+
83
+ end
84
+
85
+ def Storable()
86
+
87
+ # puts "Storable not implemented"
88
+ # raise ParseError.new # "Parse Error"
89
+
90
+ fact = Factor();
91
+ t = @scan.getToken
92
+
93
+ if t.type == :keyword then
94
+ if t.lex == "S" then
95
+ return StoreNode(fact)
96
+ else
97
+ raise ParseError.new
98
+ end
99
+ end
100
+
101
+ @scan.putBackToken
102
+ return fact
103
+ end
104
+
105
+
106
+ def Factor()
107
+
108
+ # puts "Factor not implemented"
109
+ # raise ParserError.new # "Parse Error"
110
+
111
+ t = @scan.getToken
112
+
113
+ if t.type == :number then
114
+ return NumNode.new(t.lex.to_i)
115
+ end
116
+
117
+ if t.type == keyword then
118
+ if t.lex == "R" then
119
+ return RecallNode.new
120
+ else
121
+ raise ParseError.new
122
+ end
123
+ end
124
+
125
+ if t.type == :lparen then
126
+ expr = Expr()
127
+ t = @scan.getToken
128
+ if t.type == :rparen then
129
+ return expr
130
+ else
131
+ raise ParseError.new
132
+ end
133
+ end
134
+
135
+ raise ParseError.new
136
+ end
137
+
138
+ end
@@ -0,0 +1,128 @@
1
+ require 'set'
2
+ require 'stringio'
3
+ require 'calcex'
4
+
5
+ class Scanner
6
+ def initialize(inStream)
7
+ @istream = inStream
8
+ @keywords = Set.new(["S","R"])
9
+ @lineCount = 1
10
+ @colCount = -1
11
+ @needToken = true
12
+ @lastToken = nil
13
+ end
14
+
15
+ def putBackToken()
16
+ @needToken = false
17
+ end
18
+
19
+ def getToken()
20
+ unless @needToken then
21
+ @needToken = true
22
+ return @lastToken
23
+ end
24
+
25
+ state = 0
26
+ foundOne = false
27
+ c = @istream.getc()
28
+
29
+ if @istream.eof() then
30
+ @lastToken = Token.new(:eof,@lineCount,@colCount)
31
+ return @lastToken
32
+ end
33
+
34
+ until foundOne do
35
+ @colCount = @colCount + 1
36
+ case state
37
+ when 0
38
+ lex = ""
39
+ column = @colCount
40
+ line = @lineCount
41
+ if isLetter(c) then state=1
42
+ elsif isDigit(c) then state=2
43
+ elsif c == ?+ then state = 3
44
+ elsif c == ?- then state = 4
45
+ elsif c == ?* then state = 5
46
+ elsif c == ?/ then state = 6
47
+ elsif c == ?( then state = 7
48
+ elsif c == ?) then state = 8
49
+ elsif c == ?\n then
50
+ @colCount = -1
51
+ @lineCount = @lineCount+1
52
+ elsif isWhiteSpace(c) then state = state #ignore whitespace
53
+ elsif @istream.eof() then
54
+ @foundOne = true
55
+ type = :eof
56
+ else
57
+ puts "Unrecognized Token found at line ",line," and column ",column,"\n"
58
+ raise UnrecognizedTokenException # "Unrecognized Token"
59
+ end
60
+ when 1
61
+ if isLetter(c) or isDigit(c) then state = 1
62
+ else
63
+ if @keywords.include?(lex) then
64
+ foundOne = true
65
+ type = :keyword
66
+ else
67
+ foundOne = true
68
+ type = :identifier
69
+ end
70
+ end
71
+ when 2
72
+ if isDigit(c) then state = 2
73
+ else
74
+ type = :number
75
+ foundOne = true
76
+ end
77
+ when 3
78
+ type = :add
79
+ foundOne = true
80
+ when 4
81
+ type = :sub
82
+ foundOne = true
83
+ when 5
84
+ type = :times
85
+ foundOne = true
86
+ when 6
87
+ type = :divide
88
+ foundOne = true
89
+ when 7
90
+ type = :lparen
91
+ foundOne = true
92
+ when 8
93
+ type = :rparen
94
+ foundOne = true
95
+ end
96
+
97
+ if !foundOne then
98
+ lex.concat(c)
99
+ c = @istream.getc()
100
+ end
101
+
102
+ end
103
+
104
+ @istream.ungetc(c)
105
+ @colCount = @colCount - 1
106
+ if type == :number or type == :identifier or type == :keyword then
107
+ t = LexicalToken.new(type,lex,line,column)
108
+ else
109
+ t = Token.new(type,line,column)
110
+ end
111
+
112
+ @lastToken = t
113
+ return t
114
+ end
115
+
116
+ private
117
+ def isLetter(c)
118
+ return ((?a <= c and c <= ?z) or (?A <= c and c <= ?Z))
119
+ end
120
+
121
+ def isDigit(c)
122
+ return (!c.nil? and ?0 <= c and c <= ?9)
123
+ end
124
+
125
+ def isWhiteSpace(c)
126
+ return (!c.nil? and c == ?\ or c == ?\n or c == ?\t)
127
+ end
128
+ end
@@ -0,0 +1,19 @@
1
+ class Token
2
+ attr_reader :type, :line, :col
3
+
4
+ def initialize(type,lineNum,colNum)
5
+ @type = type
6
+ @line = lineNum
7
+ @col = colNum
8
+ end
9
+ end
10
+
11
+ class LexicalToken < Token
12
+ attr_reader :lex
13
+
14
+ def initialize(type,lex,lineNum,colNum)
15
+ super(type,lineNum,colNum)
16
+
17
+ @lex = lex
18
+ end
19
+ end
metadata ADDED
@@ -0,0 +1,51 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: lmarinl1-rubycalc
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.2
5
+ platform: ruby
6
+ authors:
7
+ - Kent D. Lee - Luis Miguel Marín Loaiza
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2016-05-11 00:00:00.000000000 Z
12
+ dependencies: []
13
+ description: An calculator implementation of a basic calculator on ruby
14
+ email: lmarinl1@eafit.edu.co
15
+ executables:
16
+ - rubycalc
17
+ extensions: []
18
+ extra_rdoc_files: []
19
+ files:
20
+ - bin/rubycalc
21
+ - lib/ast.rb
22
+ - lib/calcex.rb
23
+ - lib/calculator.rb
24
+ - lib/parser.rb
25
+ - lib/scanner.rb
26
+ - lib/token.rb
27
+ homepage: http://www1.eafit.edu.co/fcardona/cursos/st0244/rubycal
28
+ licenses:
29
+ - ARTISTIC
30
+ metadata: {}
31
+ post_install_message:
32
+ rdoc_options: []
33
+ require_paths:
34
+ - lib
35
+ required_ruby_version: !ruby/object:Gem::Requirement
36
+ requirements:
37
+ - - ">="
38
+ - !ruby/object:Gem::Version
39
+ version: '0'
40
+ required_rubygems_version: !ruby/object:Gem::Requirement
41
+ requirements:
42
+ - - ">="
43
+ - !ruby/object:Gem::Version
44
+ version: '0'
45
+ requirements: []
46
+ rubyforge_project:
47
+ rubygems_version: 2.4.8
48
+ signing_key:
49
+ specification_version: 4
50
+ summary: Another calculator in ruby
51
+ test_files: []