calcula 0.1.0 → 1.0.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.travis.yml +3 -0
- data/Gemfile +2 -0
- data/LICENSE.txt +1 -1
- data/README.md +5 -8
- data/lib/Expr.rb +32 -12
- data/lib/Exprs/AssignExpr.rb +39 -0
- data/lib/Exprs/BinopExpr.rb +53 -0
- data/lib/Exprs/BracedExpr.rb +48 -0
- data/lib/Exprs/FuncExpr.rb +31 -31
- data/lib/Exprs/IdentExpr.rb +25 -17
- data/lib/Exprs/NumExpr.rb +25 -17
- data/lib/Exprs/ParamsExpr.rb +24 -18
- data/lib/Exprs/RatExpr.rb +37 -0
- data/lib/Exprs/UnaryExpr.rb +84 -0
- data/lib/Lexer.rb +158 -119
- data/lib/Parser.rb +317 -40
- data/lib/Token.rb +23 -4
- data/lib/calcula.rb +79 -4
- data/lib/calcula/version.rb +1 -1
- metadata +7 -2
data/lib/calcula.rb
CHANGED
@@ -1,15 +1,90 @@
|
|
1
1
|
require "calcula/version"
|
2
|
-
require "Token"
|
3
|
-
require "Lexer"
|
4
|
-
require "Parser"
|
5
2
|
|
3
|
+
# This module contains the lexer and the parser for Calcula. In the future, the
|
4
|
+
# runtime aspect will be included also.
|
5
|
+
#
|
6
|
+
# @author Paul T.
|
6
7
|
module Calcula
|
7
8
|
|
9
|
+
# This module contains all the tree expressions being created when the
|
10
|
+
# parse method is called. All valid tree expressions must subclass `Calcula::Expr`
|
11
|
+
#
|
12
|
+
# @see Calcula::Expr
|
13
|
+
# @author Paul T.
|
14
|
+
module Exprs
|
15
|
+
# The module here is only for documenting only. The real stuff is under ./Exprs
|
16
|
+
end
|
17
|
+
|
18
|
+
require "Token"
|
19
|
+
require "Lexer"
|
20
|
+
require "Parser"
|
21
|
+
|
22
|
+
# Short hand for calling `Calcula::Lexer.new(txt).lex`
|
23
|
+
#
|
24
|
+
# @see Calcula::Lexer#lex
|
25
|
+
# @param txt [String] The Calcula source code
|
26
|
+
# @return [Array<Calcula::Token>] Generated based on the source code
|
8
27
|
def Calcula::lex(txt)
|
9
28
|
Lexer.new(txt).lex
|
10
29
|
end
|
11
30
|
|
31
|
+
# Short hand for calling `Calcula::Parser.new(toks).parse` repetitively.
|
32
|
+
#
|
33
|
+
# @see Calcula::Parser#parse
|
34
|
+
# @param toks [Array<Calcula::Token>] This should be supplied by `Calcula::lex`.
|
35
|
+
# @return [Array<Calcula::Expr>] Multiple parse trees
|
12
36
|
def Calcula::parse(toks)
|
13
|
-
|
37
|
+
rst = []
|
38
|
+
parser = Parser.new(toks)
|
39
|
+
while (r = parser.parse) != nil do
|
40
|
+
rst << r
|
41
|
+
end
|
42
|
+
return rst
|
43
|
+
end
|
44
|
+
|
45
|
+
# Essential declarations so the Calcula to Ruby option will work
|
46
|
+
#
|
47
|
+
# @return [String] The declarations
|
48
|
+
def Calcula::stdRubyHeader
|
49
|
+
txt = <<-EOT
|
50
|
+
class Numeric
|
51
|
+
def call(x) # Allowing 10.(20) to work (becomes 200)
|
52
|
+
self * x
|
53
|
+
end
|
54
|
+
end
|
55
|
+
class Proc
|
56
|
+
def +(g)
|
57
|
+
->(*args){ self.(*args) + g.(*args) }
|
58
|
+
end
|
59
|
+
def -(g)
|
60
|
+
->(*args){ self.(*args) - g.(*args) }
|
61
|
+
end
|
62
|
+
def *(g)
|
63
|
+
->(*args){ self.(*args) * g.(*args) }
|
64
|
+
end
|
65
|
+
def /(g)
|
66
|
+
->(*args){ self.(*args) / g.(*args) }
|
67
|
+
end
|
68
|
+
def %(g)
|
69
|
+
->(*args){ self.(*args) % g.(*args) }
|
70
|
+
end
|
71
|
+
def **(g)
|
72
|
+
->(*args){ self.(*args) ** g.(*args) }
|
73
|
+
end
|
74
|
+
end
|
75
|
+
EOT
|
76
|
+
return txt
|
77
|
+
end
|
78
|
+
|
79
|
+
# Combines the essential declarations with the compiled Calcula code together
|
80
|
+
#
|
81
|
+
# @param src [String] The Calcula source code
|
82
|
+
# @return [String] The complete executable ruby code
|
83
|
+
def Calcula::compile(src)
|
84
|
+
<<-EOS
|
85
|
+
#{Calcula::stdRubyHeader}
|
86
|
+
# CALCULA SOURCE CODE STARTS HERE
|
87
|
+
#{Calcula::parse(Calcula::lex(src)).collect{ |e| e.to_s(form: :ruby) }.join("\n")}
|
88
|
+
EOS
|
14
89
|
end
|
15
90
|
end
|
data/lib/calcula/version.rb
CHANGED
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: calcula
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version:
|
4
|
+
version: 1.0.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Paul T.
|
8
8
|
autorequire:
|
9
9
|
bindir: exe
|
10
10
|
cert_chain: []
|
11
|
-
date: 2016-10-
|
11
|
+
date: 2016-10-11 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bundler
|
@@ -71,10 +71,15 @@ files:
|
|
71
71
|
- bin/setup
|
72
72
|
- calcula.gemspec
|
73
73
|
- lib/Expr.rb
|
74
|
+
- lib/Exprs/AssignExpr.rb
|
75
|
+
- lib/Exprs/BinopExpr.rb
|
76
|
+
- lib/Exprs/BracedExpr.rb
|
74
77
|
- lib/Exprs/FuncExpr.rb
|
75
78
|
- lib/Exprs/IdentExpr.rb
|
76
79
|
- lib/Exprs/NumExpr.rb
|
77
80
|
- lib/Exprs/ParamsExpr.rb
|
81
|
+
- lib/Exprs/RatExpr.rb
|
82
|
+
- lib/Exprs/UnaryExpr.rb
|
78
83
|
- lib/Lexer.rb
|
79
84
|
- lib/Parser.rb
|
80
85
|
- lib/Token.rb
|