goodheart 0.4.9
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.gitignore +12 -0
- data/.rspec +3 -0
- data/.travis.yml +7 -0
- data/CODE_OF_CONDUCT.md +74 -0
- data/Gemfile +6 -0
- data/Gemfile.lock +37 -0
- data/LICENSE.txt +21 -0
- data/README.md +69 -0
- data/Rakefile +6 -0
- data/bin/console +14 -0
- data/bin/setup +8 -0
- data/goodheart.gemspec +39 -0
- data/lib/clojure/alias.rb +15 -0
- data/lib/clojure/core.rb +148 -0
- data/lib/clojure/lib.rb +20 -0
- data/lib/clojure/namespace.rb +62 -0
- data/lib/clojure/reader.rb +143 -0
- data/lib/clojure/runtime.rb +32 -0
- data/lib/clojure.rb +2 -0
- data/lib/goodheart/version.rb +3 -0
- data/lib/goodheart.rb +12 -0
- metadata +120 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: 9ec8e365e1c475a0239ebff0084bfe33febc6e05e9bf76890f56b44c6372837c
|
4
|
+
data.tar.gz: 18224a8f0eea4a71a7c5a13266e655a1a690fdc32c39f43c7ad0245ce93bcea2
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 1df02f448c712dabf4a6712a6da77f39e08d9c9aaead427079768984c10f3615fcba93e656c14c4205cb9b3832fa0ecef2def59f585643fb2e56969f72a8b717
|
7
|
+
data.tar.gz: 9d9415c8c8d86adb679d4830cbb30196989ee058656dccbdcaa364b00f45376a4a123e9924018ff79be43d8ca26579a2917b4e61133c5859db2f912f61f2ae8e
|
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 dmitrygrach@gmail.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/Gemfile.lock
ADDED
@@ -0,0 +1,37 @@
|
|
1
|
+
PATH
|
2
|
+
remote: .
|
3
|
+
specs:
|
4
|
+
goodheart (0.3.8)
|
5
|
+
|
6
|
+
GEM
|
7
|
+
remote: https://rubygems.org/
|
8
|
+
specs:
|
9
|
+
byebug (10.0.2)
|
10
|
+
diff-lcs (1.4.4)
|
11
|
+
rake (10.5.0)
|
12
|
+
rspec (3.10.0)
|
13
|
+
rspec-core (~> 3.10.0)
|
14
|
+
rspec-expectations (~> 3.10.0)
|
15
|
+
rspec-mocks (~> 3.10.0)
|
16
|
+
rspec-core (3.10.1)
|
17
|
+
rspec-support (~> 3.10.0)
|
18
|
+
rspec-expectations (3.10.1)
|
19
|
+
diff-lcs (>= 1.2.0, < 2.0)
|
20
|
+
rspec-support (~> 3.10.0)
|
21
|
+
rspec-mocks (3.10.2)
|
22
|
+
diff-lcs (>= 1.2.0, < 2.0)
|
23
|
+
rspec-support (~> 3.10.0)
|
24
|
+
rspec-support (3.10.2)
|
25
|
+
|
26
|
+
PLATFORMS
|
27
|
+
x86_64-linux
|
28
|
+
|
29
|
+
DEPENDENCIES
|
30
|
+
bundler (~> 2.2.29)
|
31
|
+
byebug (~> 10.0)
|
32
|
+
goodheart!
|
33
|
+
rake (~> 10.0)
|
34
|
+
rspec (~> 3.0)
|
35
|
+
|
36
|
+
BUNDLED WITH
|
37
|
+
2.2.29
|
data/LICENSE.txt
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
The MIT License (MIT)
|
2
|
+
|
3
|
+
Copyright (c) 2018 cawcaw
|
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,69 @@
|
|
1
|
+
# Good Heart
|
2
|
+
|
3
|
+
Clojure interpreter on pure Ruby. Now supports only the small subset.
|
4
|
+
|
5
|
+
_**The Wizard of Oz:** As for you, my galvanized friend, you want a heart. You don't know how lucky you are not to have one. Hearts will never be practical until they can be made unbreakable._
|
6
|
+
|
7
|
+
_**The Tin Man:** But, I... I still want one._
|
8
|
+
|
9
|
+
## For what?
|
10
|
+
1. It's just awesome.
|
11
|
+
2. Share logic between Clojure and Ruby apps in stack.
|
12
|
+
2. Gradually migrate from Ruby to Clojure (or back).
|
13
|
+
3. Ability to create some tooling for Clojure on Ruby.
|
14
|
+
|
15
|
+
# Roadmap bird-eye view
|
16
|
+
- [x] Read Clojure into Ruby's data structures
|
17
|
+
- [x] Evaluate AST
|
18
|
+
- [x] High-order functions
|
19
|
+
- [x] Closures
|
20
|
+
- [x] Many namespaces
|
21
|
+
- [ ] Macroses
|
22
|
+
- [ ] Full clojure.core
|
23
|
+
- [ ] Full core.async
|
24
|
+
- [ ] Repl
|
25
|
+
- [ ] A lot of other things...
|
26
|
+
|
27
|
+
## Installation
|
28
|
+
|
29
|
+
Add this line to your application's Gemfile:
|
30
|
+
|
31
|
+
```ruby
|
32
|
+
gem 'goodheart'
|
33
|
+
```
|
34
|
+
|
35
|
+
And then execute:
|
36
|
+
|
37
|
+
$ bundle
|
38
|
+
|
39
|
+
Or install it yourself as:
|
40
|
+
|
41
|
+
$ gem install goodheart
|
42
|
+
|
43
|
+
## Usage
|
44
|
+
|
45
|
+
```ruby
|
46
|
+
require "goodheart"
|
47
|
+
|
48
|
+
runtime = Clojure::Runtime.new
|
49
|
+
runtime.load("shared_validation.clj")
|
50
|
+
runtime.namespace("validation").evaluate(["validate", data])
|
51
|
+
```
|
52
|
+
|
53
|
+
## Development
|
54
|
+
|
55
|
+
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.
|
56
|
+
|
57
|
+
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).
|
58
|
+
|
59
|
+
## Contributing
|
60
|
+
|
61
|
+
Bug reports and pull requests are welcome on GitHub at https://github.com/[USERNAME]/goodheart. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [Contributor Covenant](http://contributor-covenant.org) code of conduct.
|
62
|
+
|
63
|
+
## License
|
64
|
+
|
65
|
+
The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
|
66
|
+
|
67
|
+
## Code of Conduct
|
68
|
+
|
69
|
+
Everyone interacting in the Goodheart project’s codebases, issue trackers, chat rooms and mailing lists is expected to follow the [code of conduct](https://github.com/[USERNAME]/goodheart/blob/master/CODE_OF_CONDUCT.md).
|
data/Rakefile
ADDED
data/bin/console
ADDED
@@ -0,0 +1,14 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
|
3
|
+
require "bundler/setup"
|
4
|
+
require "goodheart"
|
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(__FILE__)
|
data/bin/setup
ADDED
data/goodheart.gemspec
ADDED
@@ -0,0 +1,39 @@
|
|
1
|
+
|
2
|
+
lib = File.expand_path("../lib", __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require "goodheart/version"
|
5
|
+
|
6
|
+
Gem::Specification.new do |spec|
|
7
|
+
spec.name = "goodheart"
|
8
|
+
spec.version = Goodheart::VERSION
|
9
|
+
spec.authors = ["cawcaw"]
|
10
|
+
spec.email = ["dmitrygrach@gmail.com"]
|
11
|
+
|
12
|
+
spec.summary = %q{Clojure interpteter and runtime.}
|
13
|
+
spec.description = %q{Good Heart allows you to put some Clojure right inside you Ruby application.}
|
14
|
+
spec.homepage = "https://github.com/cawcaw/goodheart"
|
15
|
+
spec.license = "MIT"
|
16
|
+
|
17
|
+
# Prevent pushing this gem to RubyGems.org. To allow pushes either set the 'allowed_push_host'
|
18
|
+
# to allow pushing to a single host or delete this section to allow pushing to any host.
|
19
|
+
# if spec.respond_to?(:metadata)
|
20
|
+
# spec.metadata["allowed_push_host"] = "TODO: Set to 'http://mygemserver.com'"
|
21
|
+
# else
|
22
|
+
# raise "RubyGems 2.0 or newer is required to protect against " \
|
23
|
+
# "public gem pushes."
|
24
|
+
# end
|
25
|
+
|
26
|
+
# Specify which files should be added to the gem when it is released.
|
27
|
+
# The `git ls-files -z` loads the files in the RubyGem that have been added into git.
|
28
|
+
spec.files = Dir.chdir(File.expand_path('..', __FILE__)) do
|
29
|
+
`git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
|
30
|
+
end
|
31
|
+
spec.bindir = "exe"
|
32
|
+
spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
|
33
|
+
spec.require_paths = ["lib"]
|
34
|
+
|
35
|
+
spec.add_development_dependency "bundler", "~> 2.2.29"
|
36
|
+
spec.add_development_dependency "rake", "~> 10.0"
|
37
|
+
spec.add_development_dependency "rspec", "~> 3.0"
|
38
|
+
spec.add_development_dependency "byebug", "~> 10.0"
|
39
|
+
end
|
data/lib/clojure/core.rb
ADDED
@@ -0,0 +1,148 @@
|
|
1
|
+
module Clojure
|
2
|
+
class Core
|
3
|
+
extend Library
|
4
|
+
|
5
|
+
define "+", ->(_ctx, args) { args.reduce(:+) }
|
6
|
+
define "-", ->(_ctx, args) { args.reduce(:-) }
|
7
|
+
define "*", ->(_ctx, args) { args.reduce(:*) }
|
8
|
+
define "/", ->(_ctx, args) { args.reduce(:/) }
|
9
|
+
|
10
|
+
define "=", ->(_ctx, args) { !!args.reduce { |x, y| x == y ? x : break } }
|
11
|
+
define ">", ->(_ctx, args) { !!args.reduce { |x, y| x > y ? x : break } }
|
12
|
+
define "<", ->(_ctx, args) { !!args.reduce { |x, y| x < y ? x : break } }
|
13
|
+
|
14
|
+
define "vector", ->(_ctx, args) { Array[*args] }
|
15
|
+
define "hash-map", ->(_ctx, args) { Hash[*args] }
|
16
|
+
|
17
|
+
define "str", ->(_ctx, args) { args.map(&:to_s).join }
|
18
|
+
define "quote", ->(_ctx, args) { args.first }
|
19
|
+
|
20
|
+
define "not", ->(_ctx, args) { not args }
|
21
|
+
define "nil?", ->(_ctx, args) { args.first.nil? }
|
22
|
+
|
23
|
+
define "do", ->(_ctx, args) { args.map { |f| ctx.evaluate f } }
|
24
|
+
|
25
|
+
define "subs", ->(ctx, args) { (ctx.evaluate(args[0]))[ctx.evaluate(args[1])..(ctx.evaluate(args[2]) || -1)] }
|
26
|
+
|
27
|
+
define "let", (lambda do |ctx, forms|
|
28
|
+
# skip "vector" from params
|
29
|
+
bindings = forms[0][1..-1]
|
30
|
+
lctx = ctx.dup
|
31
|
+
bindings.each_slice(2) do |k, v|
|
32
|
+
lctx[k] = lctx.evaluate(v)
|
33
|
+
end
|
34
|
+
forms[1..-1].map do |f|
|
35
|
+
lctx.evaluate(f)
|
36
|
+
end.last
|
37
|
+
end)
|
38
|
+
|
39
|
+
define "for", (lambda do |ctx, forms|
|
40
|
+
head, expr, extra = *forms
|
41
|
+
raise Exception, "Wrong number of args passed to: core/for" if extra
|
42
|
+
key = head[1]
|
43
|
+
col = ctx.evaluate head[2]
|
44
|
+
col.map do |i|
|
45
|
+
fctx = ctx.merge key => i
|
46
|
+
fctx.evaluate expr
|
47
|
+
end
|
48
|
+
end)
|
49
|
+
|
50
|
+
define "and", ->(ctx, args) { args.map { |form| ctx.evaluate form }.all? }
|
51
|
+
define "or", (lambda do |ctx, args|
|
52
|
+
args.find { |form| !!ctx.evaluate(form) }
|
53
|
+
end)
|
54
|
+
|
55
|
+
define "if", (lambda do |ctx, args|
|
56
|
+
clause, then_expr, else_expr = args
|
57
|
+
ctx.evaluate(clause) ? then_expr : else_expr
|
58
|
+
end)
|
59
|
+
define "when", ->(ctx, args) { self["if"][ctx, [*args[0..1], nil]] }
|
60
|
+
define "when-not", ->(ctx, args) { self["if"][ctx, [args[0], nil, args[1]]] }
|
61
|
+
|
62
|
+
define "count", ->(_ctx, args) { (args[0] || []).length }
|
63
|
+
define "get", ->(_ctx, args) { args[0][args[1]] || args[2] }
|
64
|
+
|
65
|
+
define "map", (lambda do |ctx, args|
|
66
|
+
fn, coll = args
|
67
|
+
coll.map { |i| fn[ctx, [i]] }
|
68
|
+
end)
|
69
|
+
|
70
|
+
define "filter", (lambda do |ctx, args|
|
71
|
+
fn, coll = args
|
72
|
+
coll.select { |i| fn[ctx, [i]] }
|
73
|
+
end)
|
74
|
+
|
75
|
+
define "distinct", ->(_ctx, args) { (args[0] || []).uniq }
|
76
|
+
|
77
|
+
define "remove", (lambda do |ctx, args|
|
78
|
+
fn, coll = args
|
79
|
+
new_coll = coll.dup
|
80
|
+
new_coll.delete_if { |i| fn[ctx, [i]] }
|
81
|
+
end)
|
82
|
+
|
83
|
+
define "def", (lambda do |ctx, args|
|
84
|
+
ctx[args[0]] = ctx.evaluate args[1]
|
85
|
+
"#{ctx["*ns*"]}/#{args[0]}"
|
86
|
+
end)
|
87
|
+
|
88
|
+
define "byebug", ->(ctx, args) { byebug }
|
89
|
+
|
90
|
+
define "merge", (lambda do |ctx, args|
|
91
|
+
coll = ctx.evaluate args[0]
|
92
|
+
args[1..-1].each do |arg|
|
93
|
+
coll.merge! ctx.evaluate(arg)
|
94
|
+
end
|
95
|
+
coll
|
96
|
+
end)
|
97
|
+
|
98
|
+
define "assoc", (lambda do |ctx, args|
|
99
|
+
coll = ctx.evaluate args[0]
|
100
|
+
pairs = args[1..-1].map { |x| ctx.evaluate x }
|
101
|
+
coll.merge(Hash[*pairs])
|
102
|
+
end)
|
103
|
+
|
104
|
+
define "first", (lambda do |ctx, args|
|
105
|
+
args.first.first
|
106
|
+
end)
|
107
|
+
|
108
|
+
define "rest", (lambda do |ctx, args|
|
109
|
+
args.first[1..-1]
|
110
|
+
end)
|
111
|
+
|
112
|
+
define "ns", (lambda do |ctx, args|
|
113
|
+
self["def"][ctx, ["*ns*", ["quote", args[0]]]]
|
114
|
+
if args[1] && args[1][0] == :require
|
115
|
+
args[1][1..-1].each do |refer|
|
116
|
+
ns = refer[1].to_sym
|
117
|
+
binds = Hash[*refer[2..-1]]
|
118
|
+
bindings = {}
|
119
|
+
bindings[binds[:as]] = [ns] if binds[:as]
|
120
|
+
if binds[:refer]
|
121
|
+
binds[:refer][1..-1].each do |ref|
|
122
|
+
bindings[ref] = [ns, ref]
|
123
|
+
end
|
124
|
+
end
|
125
|
+
bindings.each do |k, v|
|
126
|
+
ctx[k] = Clojure::Alias.new do
|
127
|
+
-> { ctx.runtime.namespaces.dig(*v) }
|
128
|
+
end
|
129
|
+
end
|
130
|
+
end
|
131
|
+
end
|
132
|
+
nil
|
133
|
+
end)
|
134
|
+
|
135
|
+
define "fn", (lambda do |ctx, args|
|
136
|
+
# TODO: poor implementation
|
137
|
+
lambda do |_ctx, fn_args|
|
138
|
+
params = args[0][1..-1].zip(fn_args)
|
139
|
+
fn_ctx = ctx.merge(Hash[params])
|
140
|
+
args[1..-1].map do |form|
|
141
|
+
fn_ctx.evaluate form
|
142
|
+
end.last
|
143
|
+
end
|
144
|
+
end)
|
145
|
+
|
146
|
+
define "defn", ->(ctx, args) { self['def'].call(ctx, [args[0], self['fn'].call(ctx, args[1..-1])]) }
|
147
|
+
end
|
148
|
+
end
|
data/lib/clojure/lib.rb
ADDED
@@ -0,0 +1,62 @@
|
|
1
|
+
module Clojure
|
2
|
+
class Namespace < Hash
|
3
|
+
# Clojure's ns | evaluation context | class
|
4
|
+
|
5
|
+
def initialize(runtime)
|
6
|
+
@runtime = runtime
|
7
|
+
end
|
8
|
+
|
9
|
+
attr_reader :runtime
|
10
|
+
|
11
|
+
# calls woth postponed evaluation of expression
|
12
|
+
SPECIAL = %w[ns fn defn def quote let for].freeze
|
13
|
+
|
14
|
+
def evaluate(form)
|
15
|
+
case form
|
16
|
+
when Array
|
17
|
+
form_eval form
|
18
|
+
when String
|
19
|
+
resolve form
|
20
|
+
else
|
21
|
+
form
|
22
|
+
end
|
23
|
+
end
|
24
|
+
|
25
|
+
private
|
26
|
+
|
27
|
+
def resolve(symbol)
|
28
|
+
n, ns = symbol.split('/').reverse
|
29
|
+
i = self[ns] || self[n] || Clojure::Core[symbol] || raise("Can't resolve #{symbol}.")
|
30
|
+
case i
|
31
|
+
when Clojure::Alias
|
32
|
+
if ns
|
33
|
+
i.lookup()[n]
|
34
|
+
else
|
35
|
+
i.lookup()
|
36
|
+
end
|
37
|
+
else
|
38
|
+
i
|
39
|
+
end
|
40
|
+
end
|
41
|
+
|
42
|
+
def form_eval(form)
|
43
|
+
head, *expressions = form
|
44
|
+
fn = case head
|
45
|
+
when Array
|
46
|
+
form_eval head
|
47
|
+
when Symbol
|
48
|
+
# dirty keyword IFn
|
49
|
+
-> (_ctx, args) { args[0][head] }
|
50
|
+
else
|
51
|
+
resolve head
|
52
|
+
end
|
53
|
+
raise Exception, "Function #{head} not defined" unless fn
|
54
|
+
args = if head.is_a?(String) && SPECIAL.include?(head)
|
55
|
+
expressions
|
56
|
+
else
|
57
|
+
expressions.map { |f| evaluate f }
|
58
|
+
end
|
59
|
+
fn.call self, args
|
60
|
+
end
|
61
|
+
end
|
62
|
+
end
|
@@ -0,0 +1,143 @@
|
|
1
|
+
module Clojure
|
2
|
+
# read Clojure as Ruby's data-structures
|
3
|
+
class Reader
|
4
|
+
def initialize(source)
|
5
|
+
@source = source
|
6
|
+
@io = StringIO.new source
|
7
|
+
@ast = []
|
8
|
+
loop do
|
9
|
+
break if eof?
|
10
|
+
r = read_next
|
11
|
+
@ast << r if r
|
12
|
+
end
|
13
|
+
true
|
14
|
+
end
|
15
|
+
|
16
|
+
def inspect
|
17
|
+
@ast
|
18
|
+
end
|
19
|
+
|
20
|
+
attr_reader :ast
|
21
|
+
|
22
|
+
def cursor
|
23
|
+
@cursor || next_char
|
24
|
+
end
|
25
|
+
|
26
|
+
def eof?
|
27
|
+
@cursor == :eof
|
28
|
+
end
|
29
|
+
|
30
|
+
def next_char
|
31
|
+
@cursor = @io.getc || :eof
|
32
|
+
end
|
33
|
+
|
34
|
+
def read_next
|
35
|
+
# puts "reading next"
|
36
|
+
# puts "-> #{cursor}"
|
37
|
+
case cursor
|
38
|
+
when :eof then return
|
39
|
+
when /\s/ then skip_char
|
40
|
+
when /\,/ then skip_char
|
41
|
+
when /\;/ then skip_comment
|
42
|
+
when /\d/ then read_number
|
43
|
+
when /\(/ then read_form
|
44
|
+
when /\[/ then read_form till: "]", into: ["vector"]
|
45
|
+
when /\{/ then read_form till: "}", into: ["hash-map"]
|
46
|
+
when /\:/ then read_keyword
|
47
|
+
when /\"/ then read_string
|
48
|
+
when /\'/ then read_quote
|
49
|
+
when /\#/ then read_special
|
50
|
+
when /\S/ then read_symbol
|
51
|
+
end
|
52
|
+
end
|
53
|
+
|
54
|
+
def read_special
|
55
|
+
case next_char
|
56
|
+
when /\_/ then read_sexp_comment
|
57
|
+
else raise Exception, "Unknown token: ##{cur}"
|
58
|
+
end
|
59
|
+
end
|
60
|
+
|
61
|
+
def read_sexp_comment
|
62
|
+
next_char
|
63
|
+
read_next
|
64
|
+
nil
|
65
|
+
end
|
66
|
+
|
67
|
+
def skip_char
|
68
|
+
next_char
|
69
|
+
nil
|
70
|
+
end
|
71
|
+
|
72
|
+
def skip_comment
|
73
|
+
next_char until cursor == "\n" || eof?
|
74
|
+
nil
|
75
|
+
end
|
76
|
+
|
77
|
+
def read_form(till: ")", into: [])
|
78
|
+
opening = cursor
|
79
|
+
skip_char # opening parenthesis
|
80
|
+
ast = into
|
81
|
+
until cursor == till
|
82
|
+
raise Exception, "Unbalanced #{opening}#{till}" if eof?
|
83
|
+
r = read_next
|
84
|
+
ast << r if r
|
85
|
+
end
|
86
|
+
skip_char # closing parenthesis
|
87
|
+
ast
|
88
|
+
end
|
89
|
+
|
90
|
+
def read_number
|
91
|
+
n = cursor
|
92
|
+
while next_char.match /[\d|.]/
|
93
|
+
n << cursor
|
94
|
+
end
|
95
|
+
Integer(n) rescue Float(n)
|
96
|
+
end
|
97
|
+
|
98
|
+
def read_keyword
|
99
|
+
next_char
|
100
|
+
k = cursor
|
101
|
+
while next_char.match /\w|\.|#|-|_/
|
102
|
+
k << cursor
|
103
|
+
end
|
104
|
+
k.to_sym
|
105
|
+
end
|
106
|
+
|
107
|
+
def read_quote
|
108
|
+
skip_char # '
|
109
|
+
["quote", read_next]
|
110
|
+
end
|
111
|
+
|
112
|
+
def read_string
|
113
|
+
next_char
|
114
|
+
if cursor == '"'
|
115
|
+
next_char
|
116
|
+
return ['str', ['quote', '']]
|
117
|
+
end
|
118
|
+
s = cursor.dup
|
119
|
+
prev = cursor
|
120
|
+
until (next_char == '"' && prev != "\\")
|
121
|
+
if cursor == "\\"
|
122
|
+
elsif cursor == "\""
|
123
|
+
s << cursor
|
124
|
+
elsif prev == "\\"
|
125
|
+
s << "\\#{cursor}"
|
126
|
+
else
|
127
|
+
s << cursor
|
128
|
+
end
|
129
|
+
prev = cursor
|
130
|
+
end
|
131
|
+
next_char
|
132
|
+
['str', ['quote', s]]
|
133
|
+
end
|
134
|
+
|
135
|
+
def read_symbol
|
136
|
+
symbol = cursor
|
137
|
+
while next_char.match(/\w|-|\.|\?|\+|\//)
|
138
|
+
symbol << cursor
|
139
|
+
end
|
140
|
+
symbol
|
141
|
+
end
|
142
|
+
end
|
143
|
+
end
|
@@ -0,0 +1,32 @@
|
|
1
|
+
module Clojure
|
2
|
+
class Runtime
|
3
|
+
def initialize
|
4
|
+
@namespaces = {}
|
5
|
+
end
|
6
|
+
|
7
|
+
attr_reader :namespaces
|
8
|
+
|
9
|
+
def namespace(name)
|
10
|
+
@namespaces[name.to_sym] ||= Clojure::Namespace.new(self)
|
11
|
+
end
|
12
|
+
|
13
|
+
def include(lib)
|
14
|
+
@namespaces[lib.name.downcase.gsub("::", ".").to_sym] = lib
|
15
|
+
end
|
16
|
+
|
17
|
+
def load(filename)
|
18
|
+
ns = Clojure::Namespace.new(self)
|
19
|
+
source = open(filename).read
|
20
|
+
ast = Clojure::Reader.new(source).ast
|
21
|
+
ast.each { |form| ns.evaluate form }
|
22
|
+
ns_name = ns["*ns*"]
|
23
|
+
@namespaces[ns_name.to_sym] = namespace(ns_name).merge(ns)
|
24
|
+
end
|
25
|
+
|
26
|
+
def read(ns_name, source)
|
27
|
+
ast = Clojure::Reader.new(source).ast
|
28
|
+
ns = namespace ns_name.to_sym
|
29
|
+
ast.map { |form| ns.evaluate form }.last
|
30
|
+
end
|
31
|
+
end
|
32
|
+
end
|
data/lib/clojure.rb
ADDED
data/lib/goodheart.rb
ADDED
@@ -0,0 +1,12 @@
|
|
1
|
+
require "goodheart/version"
|
2
|
+
require "clojure"
|
3
|
+
require "clojure/alias"
|
4
|
+
require "clojure/lib"
|
5
|
+
require "clojure/core"
|
6
|
+
require "clojure/reader"
|
7
|
+
require "clojure/namespace"
|
8
|
+
require "clojure/runtime"
|
9
|
+
|
10
|
+
module Goodheart
|
11
|
+
# Your code goes here...
|
12
|
+
end
|
metadata
ADDED
@@ -0,0 +1,120 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: goodheart
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.4.9
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- cawcaw
|
8
|
+
autorequire:
|
9
|
+
bindir: exe
|
10
|
+
cert_chain: []
|
11
|
+
date: 2021-10-17 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: 2.2.29
|
20
|
+
type: :development
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - "~>"
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: 2.2.29
|
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
|
+
- !ruby/object:Gem::Dependency
|
56
|
+
name: byebug
|
57
|
+
requirement: !ruby/object:Gem::Requirement
|
58
|
+
requirements:
|
59
|
+
- - "~>"
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: '10.0'
|
62
|
+
type: :development
|
63
|
+
prerelease: false
|
64
|
+
version_requirements: !ruby/object:Gem::Requirement
|
65
|
+
requirements:
|
66
|
+
- - "~>"
|
67
|
+
- !ruby/object:Gem::Version
|
68
|
+
version: '10.0'
|
69
|
+
description: Good Heart allows you to put some Clojure right inside you Ruby application.
|
70
|
+
email:
|
71
|
+
- dmitrygrach@gmail.com
|
72
|
+
executables: []
|
73
|
+
extensions: []
|
74
|
+
extra_rdoc_files: []
|
75
|
+
files:
|
76
|
+
- ".gitignore"
|
77
|
+
- ".rspec"
|
78
|
+
- ".travis.yml"
|
79
|
+
- CODE_OF_CONDUCT.md
|
80
|
+
- Gemfile
|
81
|
+
- Gemfile.lock
|
82
|
+
- LICENSE.txt
|
83
|
+
- README.md
|
84
|
+
- Rakefile
|
85
|
+
- bin/console
|
86
|
+
- bin/setup
|
87
|
+
- goodheart.gemspec
|
88
|
+
- lib/clojure.rb
|
89
|
+
- lib/clojure/alias.rb
|
90
|
+
- lib/clojure/core.rb
|
91
|
+
- lib/clojure/lib.rb
|
92
|
+
- lib/clojure/namespace.rb
|
93
|
+
- lib/clojure/reader.rb
|
94
|
+
- lib/clojure/runtime.rb
|
95
|
+
- lib/goodheart.rb
|
96
|
+
- lib/goodheart/version.rb
|
97
|
+
homepage: https://github.com/cawcaw/goodheart
|
98
|
+
licenses:
|
99
|
+
- MIT
|
100
|
+
metadata: {}
|
101
|
+
post_install_message:
|
102
|
+
rdoc_options: []
|
103
|
+
require_paths:
|
104
|
+
- lib
|
105
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
106
|
+
requirements:
|
107
|
+
- - ">="
|
108
|
+
- !ruby/object:Gem::Version
|
109
|
+
version: '0'
|
110
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
111
|
+
requirements:
|
112
|
+
- - ">="
|
113
|
+
- !ruby/object:Gem::Version
|
114
|
+
version: '0'
|
115
|
+
requirements: []
|
116
|
+
rubygems_version: 3.0.3
|
117
|
+
signing_key:
|
118
|
+
specification_version: 4
|
119
|
+
summary: Clojure interpteter and runtime.
|
120
|
+
test_files: []
|