calcula 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 +7 -0
- data/.gitignore +9 -0
- data/.rspec +2 -0
- data/.travis.yml +5 -0
- data/CODE_OF_CONDUCT.md +74 -0
- data/Gemfile +4 -0
- data/LICENSE.txt +21 -0
- data/README.md +50 -0
- data/Rakefile +6 -0
- data/bin/console +14 -0
- data/bin/setup +8 -0
- data/calcula.gemspec +35 -0
- data/lib/Expr.rb +21 -0
- data/lib/Exprs/FuncExpr.rb +39 -0
- data/lib/Exprs/IdentExpr.rb +27 -0
- data/lib/Exprs/NumExpr.rb +27 -0
- data/lib/Exprs/ParamsExpr.rb +27 -0
- data/lib/Lexer.rb +137 -0
- data/lib/Parser.rb +130 -0
- data/lib/Token.rb +15 -0
- data/lib/calcula/version.rb +3 -0
- data/lib/calcula.rb +15 -0
- metadata +108 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: acb99c969e886a94ca30cc534dc3db8c76f0ea0a
|
4
|
+
data.tar.gz: f5801be3a546e8aef049aa56cd566dcf53e27d0f
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 6c254c1b82fd8b658e28b876fbaa8c2bde737e57360cc748640cca4253b5d2a451845d1196fc4ee0645cb5769faec78ecb014f7495c90967580fccdc94b60c46
|
7
|
+
data.tar.gz: 92c14c7803111bde6c62e52b128fccc6bc7f40d175b348f6178a67fd6def282222dea3407416076ad63bbd822255d75450ff7dc6d4d2caae982549a70142eecd
|
data/.gitignore
ADDED
data/.rspec
ADDED
data/.travis.yml
ADDED
data/CODE_OF_CONDUCT.md
ADDED
@@ -0,0 +1,74 @@
|
|
1
|
+
# Contributor Covenant Code of Conduct
|
2
|
+
|
3
|
+
## Our Pledge
|
4
|
+
|
5
|
+
In the interest of fostering an open and welcoming environment, we as
|
6
|
+
contributors and maintainers pledge to making participation in our project and
|
7
|
+
our community a harassment-free experience for everyone, regardless of age, body
|
8
|
+
size, disability, ethnicity, gender identity and expression, level of experience,
|
9
|
+
nationality, personal appearance, race, religion, or sexual identity and
|
10
|
+
orientation.
|
11
|
+
|
12
|
+
## Our Standards
|
13
|
+
|
14
|
+
Examples of behavior that contributes to creating a positive environment
|
15
|
+
include:
|
16
|
+
|
17
|
+
* Using welcoming and inclusive language
|
18
|
+
* Being respectful of differing viewpoints and experiences
|
19
|
+
* Gracefully accepting constructive criticism
|
20
|
+
* Focusing on what is best for the community
|
21
|
+
* Showing empathy towards other community members
|
22
|
+
|
23
|
+
Examples of unacceptable behavior by participants include:
|
24
|
+
|
25
|
+
* The use of sexualized language or imagery and unwelcome sexual attention or
|
26
|
+
advances
|
27
|
+
* Trolling, insulting/derogatory comments, and personal or political attacks
|
28
|
+
* Public or private harassment
|
29
|
+
* Publishing others' private information, such as a physical or electronic
|
30
|
+
address, without explicit permission
|
31
|
+
* Other conduct which could reasonably be considered inappropriate in a
|
32
|
+
professional setting
|
33
|
+
|
34
|
+
## Our Responsibilities
|
35
|
+
|
36
|
+
Project maintainers are responsible for clarifying the standards of acceptable
|
37
|
+
behavior and are expected to take appropriate and fair corrective action in
|
38
|
+
response to any instances of unacceptable behavior.
|
39
|
+
|
40
|
+
Project maintainers have the right and responsibility to remove, edit, or
|
41
|
+
reject comments, commits, code, wiki edits, issues, and other contributions
|
42
|
+
that are not aligned to this Code of Conduct, or to ban temporarily or
|
43
|
+
permanently any contributor for other behaviors that they deem inappropriate,
|
44
|
+
threatening, offensive, or harmful.
|
45
|
+
|
46
|
+
## Scope
|
47
|
+
|
48
|
+
This Code of Conduct applies both within project spaces and in public spaces
|
49
|
+
when an individual is representing the project or its community. Examples of
|
50
|
+
representing a project or community include using an official project e-mail
|
51
|
+
address, posting via an official social media account, or acting as an appointed
|
52
|
+
representative at an online or offline event. Representation of a project may be
|
53
|
+
further defined and clarified by project maintainers.
|
54
|
+
|
55
|
+
## Enforcement
|
56
|
+
|
57
|
+
Instances of abusive, harassing, or otherwise unacceptable behavior may be
|
58
|
+
reported by contacting the project team at [plankp@outlook.com](mailto:plankp@outlook.com). All
|
59
|
+
complaints will be reviewed and investigated and will result in a response that
|
60
|
+
is deemed necessary and appropriate to the circumstances. The project team is
|
61
|
+
obligated to maintain confidentiality with regard to the reporter of an incident.
|
62
|
+
Further details of specific enforcement policies may be posted separately.
|
63
|
+
|
64
|
+
Project maintainers who do not follow or enforce the Code of Conduct in good
|
65
|
+
faith may face temporary or permanent repercussions as determined by other
|
66
|
+
members of the project's leadership.
|
67
|
+
|
68
|
+
## Attribution
|
69
|
+
|
70
|
+
This Code of Conduct is adapted from the [Contributor Covenant][homepage], version 1.4,
|
71
|
+
available at [http://contributor-covenant.org/version/1/4][version]
|
72
|
+
|
73
|
+
[homepage]: http://contributor-covenant.org
|
74
|
+
[version]: http://contributor-covenant.org/version/1/4/
|
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
The MIT License (MIT)
|
2
|
+
|
3
|
+
Copyright (c) 2016 TODO: Write your name
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
7
|
+
in the Software without restriction, including without limitation the rights
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
10
|
+
furnished to do so, subject to the following conditions:
|
11
|
+
|
12
|
+
The above copyright notice and this permission notice shall be included in
|
13
|
+
all copies or substantial portions of the Software.
|
14
|
+
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
21
|
+
THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,50 @@
|
|
1
|
+
# Calcula [](https://travis-ci.org/plankp/Calcula) [](http://inch-ci.org/github/plankp/Calcula)
|
2
|
+
|
3
|
+
When mathematics meets programming
|
4
|
+
|
5
|
+
Think of this as a very over powered calculator since it allows you to define functions and such.
|
6
|
+
|
7
|
+
## Installation
|
8
|
+
|
9
|
+
Add this line to your application's Gemfile:
|
10
|
+
|
11
|
+
```ruby
|
12
|
+
gem 'calcula'
|
13
|
+
```
|
14
|
+
|
15
|
+
And then execute:
|
16
|
+
|
17
|
+
$ bundle
|
18
|
+
|
19
|
+
Or install it yourself as:
|
20
|
+
|
21
|
+
$ gem install calcula
|
22
|
+
|
23
|
+
## Usage
|
24
|
+
|
25
|
+
At the moment, all it does is converts source code into tokens and then a syntax tree.
|
26
|
+
To do so, type in:
|
27
|
+
|
28
|
+
```ruby
|
29
|
+
require "calcula"
|
30
|
+
src = <<-'EOS'
|
31
|
+
# Write some calcula code here
|
32
|
+
EOS
|
33
|
+
tokens = Calcula.lex(src)
|
34
|
+
puts tokens.collect { |e| e.text }.to_s
|
35
|
+
puts Calcula.parse(tokens).to_tree
|
36
|
+
```
|
37
|
+
|
38
|
+
## Development
|
39
|
+
|
40
|
+
After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
|
41
|
+
|
42
|
+
To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
|
43
|
+
|
44
|
+
## Contributing
|
45
|
+
|
46
|
+
Bug reports and pull requests are welcome on GitHub at https://github.com/plankp/calcula. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [Contributor Covenant](./CODE_OF_CONDUCT.md) code of conduct.
|
47
|
+
|
48
|
+
## License
|
49
|
+
|
50
|
+
The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
|
data/Rakefile
ADDED
data/bin/console
ADDED
@@ -0,0 +1,14 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
|
3
|
+
require "bundler/setup"
|
4
|
+
require "calcula"
|
5
|
+
|
6
|
+
# You can add fixtures and/or initialization code here to make experimenting
|
7
|
+
# with your gem easier. You can also use a different console, if you like.
|
8
|
+
|
9
|
+
# (If you use this, don't forget to add pry to your Gemfile!)
|
10
|
+
# require "pry"
|
11
|
+
# Pry.start
|
12
|
+
|
13
|
+
require "irb"
|
14
|
+
IRB.start
|
data/bin/setup
ADDED
data/calcula.gemspec
ADDED
@@ -0,0 +1,35 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require 'calcula/version'
|
5
|
+
|
6
|
+
Gem::Specification.new do |spec|
|
7
|
+
spec.name = "calcula"
|
8
|
+
spec.version = Calcula::VERSION
|
9
|
+
spec.authors = ["Paul T."]
|
10
|
+
spec.email = ["plankp@outlook.com"]
|
11
|
+
|
12
|
+
spec.summary = %q{An experimental DSL that aims to be very math like}
|
13
|
+
spec.homepage = "https://github.com/plankp/Calcula"
|
14
|
+
spec.license = "MIT"
|
15
|
+
|
16
|
+
# Prevent pushing this gem to RubyGems.org. To allow pushes either set the 'allowed_push_host'
|
17
|
+
# to allow pushing to a single host or delete this section to allow pushing to any host.
|
18
|
+
if spec.respond_to?(:metadata)
|
19
|
+
spec.metadata['allowed_push_host'] = "https://rubygems.org"
|
20
|
+
else
|
21
|
+
raise "RubyGems 2.0 or newer is required to protect against " \
|
22
|
+
"public gem pushes."
|
23
|
+
end
|
24
|
+
|
25
|
+
spec.files = `git ls-files -z`.split("\x0").reject do |f|
|
26
|
+
f.match(%r{^(test|spec|features)/})
|
27
|
+
end
|
28
|
+
spec.bindir = "exe"
|
29
|
+
spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
|
30
|
+
spec.require_paths = ["lib"]
|
31
|
+
|
32
|
+
spec.add_development_dependency "bundler", "~> 1.13"
|
33
|
+
spec.add_development_dependency "rake", "~> 10.0"
|
34
|
+
spec.add_development_dependency "rspec", "~> 3.0"
|
35
|
+
end
|
data/lib/Expr.rb
ADDED
@@ -0,0 +1,39 @@
|
|
1
|
+
require_relative "../Expr"
|
2
|
+
|
3
|
+
module Calcula::Exprs
|
4
|
+
class FuncExpr < Calcula::Expr
|
5
|
+
|
6
|
+
# name: IdentExpr | Nil (for lambdas), params: ParamsExpr, action: Expr
|
7
|
+
def initialize(name, params, action)
|
8
|
+
@name = name
|
9
|
+
@params = params
|
10
|
+
@action = action
|
11
|
+
end
|
12
|
+
|
13
|
+
def to_s()
|
14
|
+
return "\\#{@params.to_s}(#{@action.to_s})" if @name == nil
|
15
|
+
return "#{@name.to_s}(#{@params.to_s})=#{@action.to_s}"
|
16
|
+
end
|
17
|
+
|
18
|
+
def to_tree()
|
19
|
+
return "(lambda #{@params.to_tree} #{@action.to_tree})" if @name == nil
|
20
|
+
return "(func #{@name.to_tree} #{@params.to_tree} #{@action.to_tree})"
|
21
|
+
end
|
22
|
+
|
23
|
+
def exec(binding)
|
24
|
+
if @name == nil then
|
25
|
+
# We return it (its a lambda function afterall...)
|
26
|
+
else
|
27
|
+
# We do not invoke this function. We just add it to the `binding`
|
28
|
+
end
|
29
|
+
end
|
30
|
+
|
31
|
+
def children()
|
32
|
+
[@name, @params, @action]
|
33
|
+
end
|
34
|
+
|
35
|
+
def self.mkLambda(params, action)
|
36
|
+
FuncExpr.new(nil, params, action)
|
37
|
+
end
|
38
|
+
end
|
39
|
+
end
|
@@ -0,0 +1,27 @@
|
|
1
|
+
require_relative "../Expr"
|
2
|
+
|
3
|
+
module Calcula::Exprs
|
4
|
+
class IdentExpr < Calcula::Expr
|
5
|
+
|
6
|
+
# name: Token
|
7
|
+
def initialize(name)
|
8
|
+
@name = name
|
9
|
+
end
|
10
|
+
|
11
|
+
def to_s()
|
12
|
+
@name.text
|
13
|
+
end
|
14
|
+
|
15
|
+
def to_tree()
|
16
|
+
"(ident #{@name.text})"
|
17
|
+
end
|
18
|
+
|
19
|
+
def exec(binding)
|
20
|
+
binding[@name.text]
|
21
|
+
end
|
22
|
+
|
23
|
+
def children()
|
24
|
+
[]
|
25
|
+
end
|
26
|
+
end
|
27
|
+
end
|
@@ -0,0 +1,27 @@
|
|
1
|
+
require_relative "../Expr"
|
2
|
+
|
3
|
+
module Calcula::Exprs
|
4
|
+
class NumExpr < Calcula::Expr
|
5
|
+
|
6
|
+
# num: Token
|
7
|
+
def initialize(num)
|
8
|
+
@num = num
|
9
|
+
end
|
10
|
+
|
11
|
+
def to_s()
|
12
|
+
@num.text
|
13
|
+
end
|
14
|
+
|
15
|
+
def to_tree()
|
16
|
+
"(num #{@num.text})"
|
17
|
+
end
|
18
|
+
|
19
|
+
def exec(binding)
|
20
|
+
return @num.text.to_f
|
21
|
+
end
|
22
|
+
|
23
|
+
def children()
|
24
|
+
[]
|
25
|
+
end
|
26
|
+
end
|
27
|
+
end
|
@@ -0,0 +1,27 @@
|
|
1
|
+
require_relative "../Expr"
|
2
|
+
|
3
|
+
module Calcula::Exprs
|
4
|
+
class ParamsExpr < Calcula::Expr
|
5
|
+
|
6
|
+
# params: List[Token]
|
7
|
+
def initialize(params)
|
8
|
+
@params = params
|
9
|
+
end
|
10
|
+
|
11
|
+
def to_s()
|
12
|
+
@params.collect { |e| e.text }.join(',')
|
13
|
+
end
|
14
|
+
|
15
|
+
def to_tree()
|
16
|
+
"(#{to_s()})"
|
17
|
+
end
|
18
|
+
|
19
|
+
def exec(binding)
|
20
|
+
return to_s()
|
21
|
+
end
|
22
|
+
|
23
|
+
def children()
|
24
|
+
[]
|
25
|
+
end
|
26
|
+
end
|
27
|
+
end
|
data/lib/Lexer.rb
ADDED
@@ -0,0 +1,137 @@
|
|
1
|
+
class Array
|
2
|
+
|
3
|
+
# el: [T]
|
4
|
+
def ===(el)
|
5
|
+
return find_index(el) != nil
|
6
|
+
end
|
7
|
+
end
|
8
|
+
|
9
|
+
module Calcula
|
10
|
+
class Lexer
|
11
|
+
ZERO_TO_NINE = "0".."9"
|
12
|
+
A_TO_Z = ("a".."z").to_a + ("A" .. "Z").to_a
|
13
|
+
|
14
|
+
# src: string
|
15
|
+
def initialize(src)
|
16
|
+
@src = src
|
17
|
+
@i = 0
|
18
|
+
@lineNum = 0
|
19
|
+
@linePos = 0
|
20
|
+
end
|
21
|
+
|
22
|
+
# msg: string
|
23
|
+
def lexError(msg)
|
24
|
+
raise "Failed to lex past (L#{@lineNum + 1}:#{@linePos + 1}): #{if msg != "" then msg else "<no message>" end}"
|
25
|
+
end
|
26
|
+
|
27
|
+
# c: char
|
28
|
+
def advancePos(c)
|
29
|
+
@i += 1
|
30
|
+
if c == '\n' then
|
31
|
+
@lineNum += 1
|
32
|
+
@linePos = 0
|
33
|
+
else
|
34
|
+
@linePos += 1
|
35
|
+
end
|
36
|
+
end
|
37
|
+
|
38
|
+
def lex()
|
39
|
+
len = @src.length
|
40
|
+
rst = []
|
41
|
+
while @i < len do
|
42
|
+
case (@src[@i])
|
43
|
+
when "(" then
|
44
|
+
advancePos(@src[@i])
|
45
|
+
rst << Token.new(:PAREN_O, "(")
|
46
|
+
when ")" then
|
47
|
+
advancePos(@src[@i])
|
48
|
+
rst << Token.new(:PAREN_C, ")")
|
49
|
+
when "[" then
|
50
|
+
advancePos(@src[@i])
|
51
|
+
rst << Token.new(:SQUARE_O, "[")
|
52
|
+
when "]" then
|
53
|
+
advancePos(@src[@i])
|
54
|
+
rst << Token.new(:SQUARE_C, "]")
|
55
|
+
when "+" then
|
56
|
+
advancePos(@src[@i])
|
57
|
+
rst << Token.new(:OP_ADD, "+")
|
58
|
+
when "-" then
|
59
|
+
advancePos(@src[@i])
|
60
|
+
rst << Token.new(:OP_SUB, "-")
|
61
|
+
when "*" then
|
62
|
+
advancePos(@src[@i])
|
63
|
+
if @src[@i] == "*" then
|
64
|
+
advancePos(@src[@i])
|
65
|
+
rst << Token.new(:OP_POW, "**")
|
66
|
+
else
|
67
|
+
rst << Token.new(:OP_MUL, "*")
|
68
|
+
end
|
69
|
+
when "/" then
|
70
|
+
advancePos(@src[@i])
|
71
|
+
rst << Token.new(:OP_DIV, "/")
|
72
|
+
when "=" then
|
73
|
+
advancePos(@src[@i])
|
74
|
+
rst << Token.new(:ASSIGN, "=")
|
75
|
+
when "%" then
|
76
|
+
advancePos(@src[@i])
|
77
|
+
rst << Token.new(:OP_REM, "%")
|
78
|
+
when "$" then
|
79
|
+
advancePos(@src[@i])
|
80
|
+
rst << Token.new(:ROUND_DOLLAR, "$")
|
81
|
+
when "@" then
|
82
|
+
advancePos(@src[@i])
|
83
|
+
rst << Token.new(:COMPOSE, "@")
|
84
|
+
when ";" then
|
85
|
+
advancePos(@src[@i])
|
86
|
+
rst << Token.new(:DISP, ";")
|
87
|
+
when "\\" then
|
88
|
+
advancePos(@src[@i])
|
89
|
+
rst << Token.new(:LAMBDA, "\\")
|
90
|
+
when "," then
|
91
|
+
advancePos(@src[@i])
|
92
|
+
rst << Token.new(:COMMA, ",")
|
93
|
+
when "#" then
|
94
|
+
advancePos(buf = @src[@i])
|
95
|
+
while @i < len && @src[@i] != "\n" do
|
96
|
+
buf += @src[@i]
|
97
|
+
advancePos(@src[@i])
|
98
|
+
end
|
99
|
+
rst << Token.new(:COMMENT, buf)
|
100
|
+
when ZERO_TO_NINE then
|
101
|
+
advancePos(buf = @src[@i])
|
102
|
+
inDecimals = false
|
103
|
+
while @i < len && (ZERO_TO_NINE === @src[@i] || "." == @src[@i]) do
|
104
|
+
if @src[@i] == "." then
|
105
|
+
if !inDecimals then
|
106
|
+
inDecimals = true
|
107
|
+
else
|
108
|
+
break
|
109
|
+
end
|
110
|
+
end
|
111
|
+
buf += @src[@i]
|
112
|
+
advancePos(@src[@i])
|
113
|
+
end
|
114
|
+
rst << Token.new(:NUM, buf)
|
115
|
+
when A_TO_Z then
|
116
|
+
advancePos(buf = @src[@i])
|
117
|
+
while @i < len && (A_TO_Z === @src[@i] || "'" == @src[@i]) do
|
118
|
+
buf += @src[@i]
|
119
|
+
advancePos(@src[@i])
|
120
|
+
end
|
121
|
+
rst << Token.new(:ID, buf)
|
122
|
+
when [" ", "\t", "\r", "\f", "\n"] then
|
123
|
+
advancePos(ws = @src[@i])
|
124
|
+
rst << Token.new(:WS, ws)
|
125
|
+
else lexError("Unrecognized token starting with '#{@src[@i]}'")
|
126
|
+
end
|
127
|
+
end
|
128
|
+
return rst
|
129
|
+
end
|
130
|
+
|
131
|
+
def reset()
|
132
|
+
@i = 0
|
133
|
+
@lineNum = 0
|
134
|
+
@linePos = 0
|
135
|
+
end
|
136
|
+
end
|
137
|
+
end
|
data/lib/Parser.rb
ADDED
@@ -0,0 +1,130 @@
|
|
1
|
+
require_relative "./Exprs/IdentExpr"
|
2
|
+
require_relative "./Exprs/NumExpr"
|
3
|
+
require_relative "./Exprs/ParamsExpr"
|
4
|
+
require_relative "./Exprs/FuncExpr"
|
5
|
+
|
6
|
+
class Calcula::Parser
|
7
|
+
|
8
|
+
include Calcula::Exprs
|
9
|
+
|
10
|
+
# toks: List[Tokens]
|
11
|
+
def initialize(toks)
|
12
|
+
@toks = toks
|
13
|
+
@i = 0
|
14
|
+
end
|
15
|
+
|
16
|
+
# unexpectedTok: Token | Nil, msg: string
|
17
|
+
def parseError(unexpectedTok, msg)
|
18
|
+
if unexpectedTok == nil then
|
19
|
+
raise "Unexpected EOF: #{if msg != "" then msg else "<no message>" end}"
|
20
|
+
end
|
21
|
+
raise "Unexpected token #{unexpectedTok.to_s}: #{if msg != "" then msg else "<no message>" end}"
|
22
|
+
end
|
23
|
+
|
24
|
+
# tokId: symbol
|
25
|
+
def consume(tokId)
|
26
|
+
cons = lookahead()
|
27
|
+
if cons == nil then
|
28
|
+
parseError(nil, "Expected token #{tokId}")
|
29
|
+
elsif cons.id == tokId then
|
30
|
+
@i += 1
|
31
|
+
return cons
|
32
|
+
else
|
33
|
+
parseError(@toks[@i], "Expected token #{tokId}")
|
34
|
+
end
|
35
|
+
end
|
36
|
+
|
37
|
+
def lookahead()
|
38
|
+
return nil if @i == @toks.length
|
39
|
+
while @toks[@i].id == :WS || @toks[@i].id == :COMMENT do
|
40
|
+
@i += 1
|
41
|
+
end
|
42
|
+
if @i < @toks.length then
|
43
|
+
return @toks[@i]
|
44
|
+
else
|
45
|
+
return nil
|
46
|
+
end
|
47
|
+
end
|
48
|
+
|
49
|
+
def parse()
|
50
|
+
if lookahead() != nil then
|
51
|
+
return basicValExpr()
|
52
|
+
end
|
53
|
+
return nil
|
54
|
+
end
|
55
|
+
|
56
|
+
# expr = parenExpr
|
57
|
+
def expr()
|
58
|
+
parenExpr()
|
59
|
+
end
|
60
|
+
|
61
|
+
# parenExpr = PAREN_O expr PAREN_C
|
62
|
+
# | basicValExpr
|
63
|
+
def parenExpr()
|
64
|
+
tmp = lookahead()
|
65
|
+
parseError(nil, "Expected PAREN_O or basicValExpr") if tmp == nil
|
66
|
+
|
67
|
+
case tmp.id
|
68
|
+
when :PAREN_O then
|
69
|
+
consume(:PAREN_O)
|
70
|
+
inner = expr()
|
71
|
+
consume(:PAREN_C)
|
72
|
+
return inner
|
73
|
+
else return basicValExpr()
|
74
|
+
end
|
75
|
+
end
|
76
|
+
|
77
|
+
# basicValExpr = numExpr | identExpr
|
78
|
+
def basicValExpr()
|
79
|
+
tmp = lookahead()
|
80
|
+
parseError(nil, "Expected numExpr or identExpr") if tmp == nil
|
81
|
+
|
82
|
+
case tmp.id
|
83
|
+
when :NUM then
|
84
|
+
return numExpr()
|
85
|
+
when :ID then
|
86
|
+
return identExpr()
|
87
|
+
when :LAMBDA then
|
88
|
+
return lambdaExpr()
|
89
|
+
else parseError(tmp, "Expected numExpr or identExpr")
|
90
|
+
end
|
91
|
+
end
|
92
|
+
|
93
|
+
# paramsExpr = ID (COMMA ID)+
|
94
|
+
# | ID
|
95
|
+
def paramsExpr()
|
96
|
+
params = [consume(:ID)]
|
97
|
+
while true
|
98
|
+
begin
|
99
|
+
tmp = consume(:COMMA)
|
100
|
+
rescue Exception
|
101
|
+
break
|
102
|
+
end
|
103
|
+
params << consume(:ID)
|
104
|
+
end
|
105
|
+
return ParamsExpr.new(params)
|
106
|
+
end
|
107
|
+
|
108
|
+
# lambdaExpr = LAMBDA paramsExpr parenExpr
|
109
|
+
def lambdaExpr()
|
110
|
+
consume(:LAMBDA)
|
111
|
+
params = paramsExpr()
|
112
|
+
return FuncExpr.mkLambda(params, parenExpr())
|
113
|
+
end
|
114
|
+
|
115
|
+
# numExpr = NUM
|
116
|
+
def numExpr()
|
117
|
+
tok = consume(:NUM)
|
118
|
+
return NumExpr.new(tok)
|
119
|
+
end
|
120
|
+
|
121
|
+
# identExpr = ID
|
122
|
+
def identExpr()
|
123
|
+
tok = consume(:ID)
|
124
|
+
return IdentExpr.new(tok)
|
125
|
+
end
|
126
|
+
|
127
|
+
def reset()
|
128
|
+
@i = 0
|
129
|
+
end
|
130
|
+
end
|
data/lib/Token.rb
ADDED
data/lib/calcula.rb
ADDED
metadata
ADDED
@@ -0,0 +1,108 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: calcula
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Paul T.
|
8
|
+
autorequire:
|
9
|
+
bindir: exe
|
10
|
+
cert_chain: []
|
11
|
+
date: 2016-10-03 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: bundler
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - "~>"
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '1.13'
|
20
|
+
type: :development
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - "~>"
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '1.13'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: rake
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - "~>"
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '10.0'
|
34
|
+
type: :development
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - "~>"
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '10.0'
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: rspec
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - "~>"
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '3.0'
|
48
|
+
type: :development
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - "~>"
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '3.0'
|
55
|
+
description:
|
56
|
+
email:
|
57
|
+
- plankp@outlook.com
|
58
|
+
executables: []
|
59
|
+
extensions: []
|
60
|
+
extra_rdoc_files: []
|
61
|
+
files:
|
62
|
+
- ".gitignore"
|
63
|
+
- ".rspec"
|
64
|
+
- ".travis.yml"
|
65
|
+
- CODE_OF_CONDUCT.md
|
66
|
+
- Gemfile
|
67
|
+
- LICENSE.txt
|
68
|
+
- README.md
|
69
|
+
- Rakefile
|
70
|
+
- bin/console
|
71
|
+
- bin/setup
|
72
|
+
- calcula.gemspec
|
73
|
+
- lib/Expr.rb
|
74
|
+
- lib/Exprs/FuncExpr.rb
|
75
|
+
- lib/Exprs/IdentExpr.rb
|
76
|
+
- lib/Exprs/NumExpr.rb
|
77
|
+
- lib/Exprs/ParamsExpr.rb
|
78
|
+
- lib/Lexer.rb
|
79
|
+
- lib/Parser.rb
|
80
|
+
- lib/Token.rb
|
81
|
+
- lib/calcula.rb
|
82
|
+
- lib/calcula/version.rb
|
83
|
+
homepage: https://github.com/plankp/Calcula
|
84
|
+
licenses:
|
85
|
+
- MIT
|
86
|
+
metadata:
|
87
|
+
allowed_push_host: https://rubygems.org
|
88
|
+
post_install_message:
|
89
|
+
rdoc_options: []
|
90
|
+
require_paths:
|
91
|
+
- lib
|
92
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
93
|
+
requirements:
|
94
|
+
- - ">="
|
95
|
+
- !ruby/object:Gem::Version
|
96
|
+
version: '0'
|
97
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
98
|
+
requirements:
|
99
|
+
- - ">="
|
100
|
+
- !ruby/object:Gem::Version
|
101
|
+
version: '0'
|
102
|
+
requirements: []
|
103
|
+
rubyforge_project:
|
104
|
+
rubygems_version: 2.5.1
|
105
|
+
signing_key:
|
106
|
+
specification_version: 4
|
107
|
+
summary: An experimental DSL that aims to be very math like
|
108
|
+
test_files: []
|