calc_ccartag4 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 ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: ededb0e53221f78d21af82d77c80587feea796c6
4
+ data.tar.gz: 3dbddaaa34ebf56e5e9d59a828ad0fb84df421e8
5
+ SHA512:
6
+ metadata.gz: 7cf9576b822388f47bffe63876f5c4ceccb592fce746326ed73482c4f8201079efe960819e28f23d25c5703d2fe4359fe9c686e44e370d7612922d355f23017e
7
+ data.tar.gz: 3b145d10f8f3db551ae859e1a614e7d4a0063436ac37535b5a14b005957f6d0be4ab33f23c1df00d6db215cecad08d23cc438acabcfc4216d5fa9cc19de37fd5
data/bin/calc ADDED
@@ -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
data/lib/ast.rb ADDED
@@ -0,0 +1,83 @@
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
+ return @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
+ return @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
+ return @left.evaluate * @right.evalute()
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
+ return @left.evalute / @right.evaluate()
57
+ end
58
+ end
59
+
60
+ class StoreNode < UnaryNode
61
+ def initialize(subTree)
62
+ super(subTree)
63
+ end
64
+ def evaluate()
65
+ $calc.memory = subTree.evaluate()
66
+ end
67
+ end
68
+
69
+ class RecallNode
70
+ def evaluate()
71
+ $calc.memory
72
+ end
73
+ end
74
+
75
+ class NumNode
76
+ def initialize(num)
77
+ @num = num
78
+ end
79
+
80
+ def evaluate()
81
+ return @num
82
+ end
83
+ end
data/lib/calcex.rb ADDED
@@ -0,0 +1,3 @@
1
+
2
+ class ParseError < Exception; end
3
+ class UnrecognizedTokenException < Exception; end
data/lib/calculator.rb ADDED
@@ -0,0 +1,17 @@
1
+ require 'parser'
2
+ require 'ast'
3
+
4
+ class Calculator
5
+ attr_accessor :memory
6
+
7
+
8
+ def initialize()
9
+ @memory = 0
10
+ end
11
+
12
+ def eval(expr)
13
+ parser = Parser.new(StringIO.new(expr))
14
+ ast = parser.parse()
15
+ return ast.evaluate()
16
+ end
17
+ end
data/lib/parser.rb ADDED
@@ -0,0 +1,124 @@
1
+ # coding: utf-8
2
+ require 'ast'
3
+ require 'scanner'
4
+ require 'token'
5
+ require 'calcex'
6
+
7
+ class Parser
8
+ def initialize(istream)
9
+ @scan = Scanner.new(istream)
10
+ end
11
+
12
+ def parse()
13
+ return Prog()
14
+ end
15
+
16
+ private
17
+ def Prog()
18
+ result = Expr()
19
+ t = @scan.getToken()
20
+
21
+ if 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
+ 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
+
51
+ RestTerm(Storable())
52
+
53
+ end
54
+
55
+ def RestTerm(e)
56
+ t = @scan.getToken
57
+
58
+ if t.type == :times then
59
+ return ResTerm(TimesNode.new(e,Term()))
60
+ end
61
+
62
+ if t.type == :divide then
63
+ return ResTerm(DivideNode.new(e,Term()))
64
+ end
65
+
66
+ # if t.type == :modulo then
67
+ # return ResTerm(ModuloNode.new(e,Storable))
68
+ # end
69
+
70
+ @scan.putBackToken
71
+
72
+ return e
73
+
74
+ end
75
+
76
+ def Storable()
77
+ result = Factor()
78
+ t = @scan.getToken
79
+
80
+ if t.type == :keyword then
81
+ if t.lex == "S" then
82
+ return StoreNode.new(result)
83
+ end
84
+ puts "Expected S found: #{t.lex} at line: #{t.line} col: #{t.col}"
85
+ raise ParseError.new # "Parse Error"
86
+
87
+ end
88
+
89
+ @scan.putBackToken
90
+ result
91
+ end
92
+
93
+ def Factor()
94
+ t = @scan.getToken
95
+ if t.type == :number then
96
+ return NumNode.new t.lex.to_i
97
+ end
98
+
99
+ if t.type == :keyword then
100
+ if t.lex == "R" then
101
+ return RecallNode.new
102
+ end
103
+
104
+
105
+
106
+ puts "Parser Error: expected R found" + t.lex
107
+ puts "at line: " + t.line.to_s + "col: " + t.col.to_s
108
+ raise ParseError.new
109
+ end
110
+
111
+ if t.type == :lparen then
112
+ result = Expr()
113
+ t = @scan.getToken
114
+ if t.type == :rparen then
115
+ return result
116
+ end
117
+
118
+ puts "Expected ) Found #{t.type} at line: #{t.line} and column: #{t.col} .\n"
119
+ raise ParseError.new
120
+ end
121
+ puts "Expected ) Found #{t.type} at line: #{t.line} and column: #{t.col} .\n"
122
+ raise ParseError.new
123
+ end
124
+ end
data/lib/scanner.rb ADDED
@@ -0,0 +1,131 @@
1
+ require 'stringio'
2
+ require 'calcex'
3
+
4
+ class Scanner
5
+ def initialize(inStream)
6
+ @istream = inStream
7
+ @keywords = Set.new(%w{S R})
8
+ @lineCount = 1
9
+ @colCount = -1
10
+ @needToken = true
11
+ @lastToken = nil
12
+ end
13
+
14
+ def putBackToken()
15
+ @needToken = false
16
+ end
17
+
18
+ def getToken()
19
+ unless @needToken
20
+ @needToken = true
21
+ return @lastToken
22
+ end
23
+
24
+ state = 0
25
+ foundOne = false
26
+ c = @istream.getc()
27
+
28
+ if @istream.eof() then
29
+ @lastToken = Token.new(:eof,@lineCount,@colCount)
30
+ return @lastToken
31
+ end
32
+
33
+ until !foundOne
34
+ @colCount += 1
35
+ case state
36
+ when 0
37
+ lex = ""
38
+ column = @colCount
39
+ line = @lineCount
40
+ if isLetter(c) then state=1
41
+ elsif isDigit(c) then state=2
42
+ elsif c == ?+ then state = 3
43
+ elsif c == ?- then state = 4
44
+ elsif c == ?* then state = 5
45
+ elsif c == ?/ then state = 6
46
+ elsif c == ?( then state = 7
47
+ elsif c == ?) then state = 8
48
+ # else if c == % then state = 9
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
+ # when 9
96
+ # type = :modulo
97
+ # foundOne = true
98
+ end
99
+
100
+ if !foundOne then
101
+ lex.concat(c)
102
+ c = @istream.getc()
103
+ end
104
+
105
+ end
106
+
107
+ @istream.ungetc(c)
108
+ @colCount = @colCount - 1
109
+ if type == :number or type == :identifier or type == :keyword then
110
+ t = LexicalToken.new(type,lex,line,column)
111
+ else
112
+ t = Token.new(type,line,column)
113
+ end
114
+
115
+ @lastToken = t
116
+ return t
117
+ end
118
+
119
+ private
120
+ def isLetter(c)
121
+ return ((?a <= c and c <= ?z) or (?A <= c and c <= ?Z))
122
+ end
123
+
124
+ def isDigit(c)
125
+ return (?0 <= c and c <= ?9)
126
+ end
127
+
128
+ def isWhiteSpace(c)
129
+ return (c == ?\ or c == ?\n or c == ?\t)
130
+ end
131
+ end
data/lib/token.rb ADDED
@@ -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: calc_ccartag4
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Kent D. Lee - Juan Francisco Mackormic - Craig David Cartagena
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2015-11-12 00:00:00.000000000 Z
12
+ dependencies: []
13
+ description: An calculator implementation on ruby
14
+ email: ccartag4@eafit.edu.co
15
+ executables:
16
+ - calc
17
+ extensions: []
18
+ extra_rdoc_files: []
19
+ files:
20
+ - bin/calc
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.7
48
+ signing_key:
49
+ specification_version: 4
50
+ summary: Another calculator in ruby
51
+ test_files: []