iode 0.0.1
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 +17 -0
- data/Gemfile +4 -0
- data/LICENSE.txt +202 -0
- data/README.md +112 -0
- data/Rakefile +1 -0
- data/bin/iode-rb +34 -0
- data/iode.gemspec +31 -0
- data/lib/iode.rb +36 -0
- data/lib/iode/built_ins.rb +61 -0
- data/lib/iode/interpreter.rb +119 -0
- data/lib/iode/reader.rb +96 -0
- data/lib/iode/scope.rb +83 -0
- data/lib/iode/version.rb +19 -0
- metadata +104 -0
checksums.yaml
ADDED
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
---
|
|
2
|
+
SHA1:
|
|
3
|
+
metadata.gz: 88c450a99b55f2e4e32ba2889a229da5efc6f37c
|
|
4
|
+
data.tar.gz: 00e32521320732d46ce067b49dcb53c6594cd7b0
|
|
5
|
+
SHA512:
|
|
6
|
+
metadata.gz: 8fc46489cacd07abc6d46c574040f3643aef9f99e7ff5176a9bf47b8c08c8d23cc3ffe954917c0fe81709987b088779d41be2ea360829c009c5768124c2cc7ed
|
|
7
|
+
data.tar.gz: 20b0ead4cbbaf356c768acb45965a423907ee2acee8a7a57a82404c1f5cc9722e9b83c78dda32643e08b0131175b2a503af2822505b13053d8c09d5fb4e53661
|
data/.gitignore
ADDED
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
|
@@ -0,0 +1,202 @@
|
|
|
1
|
+
|
|
2
|
+
Apache License
|
|
3
|
+
Version 2.0, January 2004
|
|
4
|
+
http://www.apache.org/licenses/
|
|
5
|
+
|
|
6
|
+
TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
|
|
7
|
+
|
|
8
|
+
1. Definitions.
|
|
9
|
+
|
|
10
|
+
"License" shall mean the terms and conditions for use, reproduction,
|
|
11
|
+
and distribution as defined by Sections 1 through 9 of this document.
|
|
12
|
+
|
|
13
|
+
"Licensor" shall mean the copyright owner or entity authorized by
|
|
14
|
+
the copyright owner that is granting the License.
|
|
15
|
+
|
|
16
|
+
"Legal Entity" shall mean the union of the acting entity and all
|
|
17
|
+
other entities that control, are controlled by, or are under common
|
|
18
|
+
control with that entity. For the purposes of this definition,
|
|
19
|
+
"control" means (i) the power, direct or indirect, to cause the
|
|
20
|
+
direction or management of such entity, whether by contract or
|
|
21
|
+
otherwise, or (ii) ownership of fifty percent (50%) or more of the
|
|
22
|
+
outstanding shares, or (iii) beneficial ownership of such entity.
|
|
23
|
+
|
|
24
|
+
"You" (or "Your") shall mean an individual or Legal Entity
|
|
25
|
+
exercising permissions granted by this License.
|
|
26
|
+
|
|
27
|
+
"Source" form shall mean the preferred form for making modifications,
|
|
28
|
+
including but not limited to software source code, documentation
|
|
29
|
+
source, and configuration files.
|
|
30
|
+
|
|
31
|
+
"Object" form shall mean any form resulting from mechanical
|
|
32
|
+
transformation or translation of a Source form, including but
|
|
33
|
+
not limited to compiled object code, generated documentation,
|
|
34
|
+
and conversions to other media types.
|
|
35
|
+
|
|
36
|
+
"Work" shall mean the work of authorship, whether in Source or
|
|
37
|
+
Object form, made available under the License, as indicated by a
|
|
38
|
+
copyright notice that is included in or attached to the work
|
|
39
|
+
(an example is provided in the Appendix below).
|
|
40
|
+
|
|
41
|
+
"Derivative Works" shall mean any work, whether in Source or Object
|
|
42
|
+
form, that is based on (or derived from) the Work and for which the
|
|
43
|
+
editorial revisions, annotations, elaborations, or other modifications
|
|
44
|
+
represent, as a whole, an original work of authorship. For the purposes
|
|
45
|
+
of this License, Derivative Works shall not include works that remain
|
|
46
|
+
separable from, or merely link (or bind by name) to the interfaces of,
|
|
47
|
+
the Work and Derivative Works thereof.
|
|
48
|
+
|
|
49
|
+
"Contribution" shall mean any work of authorship, including
|
|
50
|
+
the original version of the Work and any modifications or additions
|
|
51
|
+
to that Work or Derivative Works thereof, that is intentionally
|
|
52
|
+
submitted to Licensor for inclusion in the Work by the copyright owner
|
|
53
|
+
or by an individual or Legal Entity authorized to submit on behalf of
|
|
54
|
+
the copyright owner. For the purposes of this definition, "submitted"
|
|
55
|
+
means any form of electronic, verbal, or written communication sent
|
|
56
|
+
to the Licensor or its representatives, including but not limited to
|
|
57
|
+
communication on electronic mailing lists, source code control systems,
|
|
58
|
+
and issue tracking systems that are managed by, or on behalf of, the
|
|
59
|
+
Licensor for the purpose of discussing and improving the Work, but
|
|
60
|
+
excluding communication that is conspicuously marked or otherwise
|
|
61
|
+
designated in writing by the copyright owner as "Not a Contribution."
|
|
62
|
+
|
|
63
|
+
"Contributor" shall mean Licensor and any individual or Legal Entity
|
|
64
|
+
on behalf of whom a Contribution has been received by Licensor and
|
|
65
|
+
subsequently incorporated within the Work.
|
|
66
|
+
|
|
67
|
+
2. Grant of Copyright License. Subject to the terms and conditions of
|
|
68
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
69
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
70
|
+
copyright license to reproduce, prepare Derivative Works of,
|
|
71
|
+
publicly display, publicly perform, sublicense, and distribute the
|
|
72
|
+
Work and such Derivative Works in Source or Object form.
|
|
73
|
+
|
|
74
|
+
3. Grant of Patent License. Subject to the terms and conditions of
|
|
75
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
76
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
77
|
+
(except as stated in this section) patent license to make, have made,
|
|
78
|
+
use, offer to sell, sell, import, and otherwise transfer the Work,
|
|
79
|
+
where such license applies only to those patent claims licensable
|
|
80
|
+
by such Contributor that are necessarily infringed by their
|
|
81
|
+
Contribution(s) alone or by combination of their Contribution(s)
|
|
82
|
+
with the Work to which such Contribution(s) was submitted. If You
|
|
83
|
+
institute patent litigation against any entity (including a
|
|
84
|
+
cross-claim or counterclaim in a lawsuit) alleging that the Work
|
|
85
|
+
or a Contribution incorporated within the Work constitutes direct
|
|
86
|
+
or contributory patent infringement, then any patent licenses
|
|
87
|
+
granted to You under this License for that Work shall terminate
|
|
88
|
+
as of the date such litigation is filed.
|
|
89
|
+
|
|
90
|
+
4. Redistribution. You may reproduce and distribute copies of the
|
|
91
|
+
Work or Derivative Works thereof in any medium, with or without
|
|
92
|
+
modifications, and in Source or Object form, provided that You
|
|
93
|
+
meet the following conditions:
|
|
94
|
+
|
|
95
|
+
(a) You must give any other recipients of the Work or
|
|
96
|
+
Derivative Works a copy of this License; and
|
|
97
|
+
|
|
98
|
+
(b) You must cause any modified files to carry prominent notices
|
|
99
|
+
stating that You changed the files; and
|
|
100
|
+
|
|
101
|
+
(c) You must retain, in the Source form of any Derivative Works
|
|
102
|
+
that You distribute, all copyright, patent, trademark, and
|
|
103
|
+
attribution notices from the Source form of the Work,
|
|
104
|
+
excluding those notices that do not pertain to any part of
|
|
105
|
+
the Derivative Works; and
|
|
106
|
+
|
|
107
|
+
(d) If the Work includes a "NOTICE" text file as part of its
|
|
108
|
+
distribution, then any Derivative Works that You distribute must
|
|
109
|
+
include a readable copy of the attribution notices contained
|
|
110
|
+
within such NOTICE file, excluding those notices that do not
|
|
111
|
+
pertain to any part of the Derivative Works, in at least one
|
|
112
|
+
of the following places: within a NOTICE text file distributed
|
|
113
|
+
as part of the Derivative Works; within the Source form or
|
|
114
|
+
documentation, if provided along with the Derivative Works; or,
|
|
115
|
+
within a display generated by the Derivative Works, if and
|
|
116
|
+
wherever such third-party notices normally appear. The contents
|
|
117
|
+
of the NOTICE file are for informational purposes only and
|
|
118
|
+
do not modify the License. You may add Your own attribution
|
|
119
|
+
notices within Derivative Works that You distribute, alongside
|
|
120
|
+
or as an addendum to the NOTICE text from the Work, provided
|
|
121
|
+
that such additional attribution notices cannot be construed
|
|
122
|
+
as modifying the License.
|
|
123
|
+
|
|
124
|
+
You may add Your own copyright statement to Your modifications and
|
|
125
|
+
may provide additional or different license terms and conditions
|
|
126
|
+
for use, reproduction, or distribution of Your modifications, or
|
|
127
|
+
for any such Derivative Works as a whole, provided Your use,
|
|
128
|
+
reproduction, and distribution of the Work otherwise complies with
|
|
129
|
+
the conditions stated in this License.
|
|
130
|
+
|
|
131
|
+
5. Submission of Contributions. Unless You explicitly state otherwise,
|
|
132
|
+
any Contribution intentionally submitted for inclusion in the Work
|
|
133
|
+
by You to the Licensor shall be under the terms and conditions of
|
|
134
|
+
this License, without any additional terms or conditions.
|
|
135
|
+
Notwithstanding the above, nothing herein shall supersede or modify
|
|
136
|
+
the terms of any separate license agreement you may have executed
|
|
137
|
+
with Licensor regarding such Contributions.
|
|
138
|
+
|
|
139
|
+
6. Trademarks. This License does not grant permission to use the trade
|
|
140
|
+
names, trademarks, service marks, or product names of the Licensor,
|
|
141
|
+
except as required for reasonable and customary use in describing the
|
|
142
|
+
origin of the Work and reproducing the content of the NOTICE file.
|
|
143
|
+
|
|
144
|
+
7. Disclaimer of Warranty. Unless required by applicable law or
|
|
145
|
+
agreed to in writing, Licensor provides the Work (and each
|
|
146
|
+
Contributor provides its Contributions) on an "AS IS" BASIS,
|
|
147
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
|
|
148
|
+
implied, including, without limitation, any warranties or conditions
|
|
149
|
+
of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
|
|
150
|
+
PARTICULAR PURPOSE. You are solely responsible for determining the
|
|
151
|
+
appropriateness of using or redistributing the Work and assume any
|
|
152
|
+
risks associated with Your exercise of permissions under this License.
|
|
153
|
+
|
|
154
|
+
8. Limitation of Liability. In no event and under no legal theory,
|
|
155
|
+
whether in tort (including negligence), contract, or otherwise,
|
|
156
|
+
unless required by applicable law (such as deliberate and grossly
|
|
157
|
+
negligent acts) or agreed to in writing, shall any Contributor be
|
|
158
|
+
liable to You for damages, including any direct, indirect, special,
|
|
159
|
+
incidental, or consequential damages of any character arising as a
|
|
160
|
+
result of this License or out of the use or inability to use the
|
|
161
|
+
Work (including but not limited to damages for loss of goodwill,
|
|
162
|
+
work stoppage, computer failure or malfunction, or any and all
|
|
163
|
+
other commercial damages or losses), even if such Contributor
|
|
164
|
+
has been advised of the possibility of such damages.
|
|
165
|
+
|
|
166
|
+
9. Accepting Warranty or Additional Liability. While redistributing
|
|
167
|
+
the Work or Derivative Works thereof, You may choose to offer,
|
|
168
|
+
and charge a fee for, acceptance of support, warranty, indemnity,
|
|
169
|
+
or other liability obligations and/or rights consistent with this
|
|
170
|
+
License. However, in accepting such obligations, You may act only
|
|
171
|
+
on Your own behalf and on Your sole responsibility, not on behalf
|
|
172
|
+
of any other Contributor, and only if You agree to indemnify,
|
|
173
|
+
defend, and hold each Contributor harmless for any liability
|
|
174
|
+
incurred by, or claims asserted against, such Contributor by reason
|
|
175
|
+
of your accepting any such warranty or additional liability.
|
|
176
|
+
|
|
177
|
+
END OF TERMS AND CONDITIONS
|
|
178
|
+
|
|
179
|
+
APPENDIX: How to apply the Apache License to your work.
|
|
180
|
+
|
|
181
|
+
To apply the Apache License to your work, attach the following
|
|
182
|
+
boilerplate notice, with the fields enclosed by brackets "[]"
|
|
183
|
+
replaced with your own identifying information. (Don't include
|
|
184
|
+
the brackets!) The text should be enclosed in the appropriate
|
|
185
|
+
comment syntax for the file format. We also recommend that a
|
|
186
|
+
file or class name and description of purpose be included on the
|
|
187
|
+
same "printed page" as the copyright notice for easier
|
|
188
|
+
identification within third-party archives.
|
|
189
|
+
|
|
190
|
+
Copyright [yyyy] [name of copyright owner]
|
|
191
|
+
|
|
192
|
+
Licensed under the Apache License, Version 2.0 (the "License");
|
|
193
|
+
you may not use this file except in compliance with the License.
|
|
194
|
+
You may obtain a copy of the License at
|
|
195
|
+
|
|
196
|
+
http://www.apache.org/licenses/LICENSE-2.0
|
|
197
|
+
|
|
198
|
+
Unless required by applicable law or agreed to in writing, software
|
|
199
|
+
distributed under the License is distributed on an "AS IS" BASIS,
|
|
200
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
201
|
+
See the License for the specific language governing permissions and
|
|
202
|
+
limitations under the License.
|
data/README.md
ADDED
|
@@ -0,0 +1,112 @@
|
|
|
1
|
+
# Iode RB
|
|
2
|
+
|
|
3
|
+
An experimental lisp-family language hosted on Ruby.
|
|
4
|
+
|
|
5
|
+
## Installation
|
|
6
|
+
|
|
7
|
+
Add this line to your application's Gemfile:
|
|
8
|
+
|
|
9
|
+
gem 'iode'
|
|
10
|
+
|
|
11
|
+
And then execute:
|
|
12
|
+
|
|
13
|
+
$ bundle
|
|
14
|
+
|
|
15
|
+
Or install it yourself as:
|
|
16
|
+
|
|
17
|
+
$ gem install iode
|
|
18
|
+
|
|
19
|
+
## Usage
|
|
20
|
+
|
|
21
|
+
Firstly, please don't think that this project is a serious attempt to build
|
|
22
|
+
some shiny new language that runs in Ruby. It is possible it may become that
|
|
23
|
+
one day, but right now it exists purely for my own experimentation purposes.
|
|
24
|
+
|
|
25
|
+
I am building a real language on the LLVM (currently in a private git repo),
|
|
26
|
+
however I wanted a "quick and dirty" environment to hack together ideas for
|
|
27
|
+
what the syntax and language features may include. Ruby is perfect for that.
|
|
28
|
+
Once I have good ideas put down as working examples in this project, they will
|
|
29
|
+
be ported upstream to the native JIT interpreter for LLVM.
|
|
30
|
+
|
|
31
|
+
Currently this project just implements the guts of a functional lisp-family of
|
|
32
|
+
languages. It will change considerably from the current implementation.
|
|
33
|
+
|
|
34
|
+
### Command Line
|
|
35
|
+
|
|
36
|
+
Program source files end in ".io". You can run them like so:
|
|
37
|
+
|
|
38
|
+
```
|
|
39
|
+
iode-rb path/to/file.io
|
|
40
|
+
```
|
|
41
|
+
|
|
42
|
+
Or you can send the source code to STDIN:
|
|
43
|
+
|
|
44
|
+
```
|
|
45
|
+
iode-rb < path/to/file.io
|
|
46
|
+
```
|
|
47
|
+
|
|
48
|
+
### In Ruby Code
|
|
49
|
+
|
|
50
|
+
Using Iode from inside Ruby code can be interesting, as it will interoperate
|
|
51
|
+
with Ruby.
|
|
52
|
+
|
|
53
|
+
``` ruby
|
|
54
|
+
require "iode"
|
|
55
|
+
|
|
56
|
+
puts Iode.run <<-PROG
|
|
57
|
+
(if ((lambda (x) x) false)
|
|
58
|
+
"x = true"
|
|
59
|
+
"x = false")
|
|
60
|
+
PROG
|
|
61
|
+
```
|
|
62
|
+
|
|
63
|
+
The above code creates a lambda function that simply acts like the identity
|
|
64
|
+
function (i.e. it returns its input). That lambda is then immediately applied
|
|
65
|
+
with the input `false`, thereby returning `false`.
|
|
66
|
+
|
|
67
|
+
The `if` form evaluates false and therefore evaluates the else part of the
|
|
68
|
+
`if`, returning the string `"x = false"`.
|
|
69
|
+
|
|
70
|
+
Here's another example showing how you can pass values from Ruby into Iode.
|
|
71
|
+
|
|
72
|
+
``` ruby
|
|
73
|
+
require "iode"
|
|
74
|
+
|
|
75
|
+
prog = Iode.run <<-PROG
|
|
76
|
+
(lambda (x)
|
|
77
|
+
(if x
|
|
78
|
+
42
|
|
79
|
+
7))
|
|
80
|
+
PROG
|
|
81
|
+
|
|
82
|
+
prog[false] #=> 7
|
|
83
|
+
prog[true] #=> 42
|
|
84
|
+
```
|
|
85
|
+
|
|
86
|
+
This works because internally, Iode lambdas as represented as Ruby Procs.
|
|
87
|
+
|
|
88
|
+
Incidentally, that means you can even pass higher-order functions from Ruby
|
|
89
|
+
to Iode.
|
|
90
|
+
|
|
91
|
+
``` ruby
|
|
92
|
+
require "iode"
|
|
93
|
+
|
|
94
|
+
prog = Iode.run <<-PROG
|
|
95
|
+
(lambda (f)
|
|
96
|
+
(if (f)
|
|
97
|
+
42
|
|
98
|
+
7))
|
|
99
|
+
PROG
|
|
100
|
+
|
|
101
|
+
prog.call(->(){ false }) #=> 7
|
|
102
|
+
prog.call(->(){ true }) #=> 42
|
|
103
|
+
```
|
|
104
|
+
|
|
105
|
+
I'd show a more complex example if the language were more than 3 hours old!
|
|
106
|
+
There are not even any mathematic operators or persistent definitions yet,
|
|
107
|
+
though all the wiring is there, I just need to define a core library.
|
|
108
|
+
|
|
109
|
+
## Copyright & Licensing
|
|
110
|
+
|
|
111
|
+
Licensed under the Apache License, Version 2.0. See the LICENSE.txt file for
|
|
112
|
+
full details.
|
data/Rakefile
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
require "bundler/gem_tasks"
|
data/bin/iode-rb
ADDED
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
#!/usr/bin/env ruby
|
|
2
|
+
|
|
3
|
+
# iode: bin/iode
|
|
4
|
+
#
|
|
5
|
+
# Copyright 2014 Chris Corbyn <chris@w3style.co.uk>
|
|
6
|
+
#
|
|
7
|
+
# Licensed under the Apache License, Version 2.0 (the "License");
|
|
8
|
+
# you may not use this file except in compliance with the License.
|
|
9
|
+
# You may obtain a copy of the License at
|
|
10
|
+
#
|
|
11
|
+
# http://www.apache.org/licenses/LICENSE-2.0
|
|
12
|
+
#
|
|
13
|
+
# Unless required by applicable law or agreed to in writing, software
|
|
14
|
+
# distributed under the License is distributed on an "AS IS" BASIS,
|
|
15
|
+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
16
|
+
# See the License for the specific language governing permissions and
|
|
17
|
+
# limitations under the License.
|
|
18
|
+
|
|
19
|
+
$:<<File.expand_path("../../lib", __FILE__)
|
|
20
|
+
|
|
21
|
+
require "iode"
|
|
22
|
+
|
|
23
|
+
case ARGV.length
|
|
24
|
+
when 1
|
|
25
|
+
Iode.run(File.read(ARGV[0]))
|
|
26
|
+
when 0
|
|
27
|
+
Iode.run(STDIN.read)
|
|
28
|
+
else
|
|
29
|
+
puts <<-EOF.gsub(/^ /, "")
|
|
30
|
+
Iode Version #{Iode::VERSION}
|
|
31
|
+
Usage:
|
|
32
|
+
iode-rb <path>
|
|
33
|
+
EOF
|
|
34
|
+
end
|
data/iode.gemspec
ADDED
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
# coding: utf-8
|
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
|
4
|
+
require 'iode/version'
|
|
5
|
+
|
|
6
|
+
Gem::Specification.new do |spec|
|
|
7
|
+
spec.name = "iode"
|
|
8
|
+
spec.version = Iode::VERSION
|
|
9
|
+
spec.authors = ["Chris Corbyn"]
|
|
10
|
+
spec.email = ["chris@w3style.co.uk"]
|
|
11
|
+
spec.summary = %q{An experimental lisp-family language hosted on Ruby}
|
|
12
|
+
spec.description = <<-EOF
|
|
13
|
+
Iode is a work in progress real language on LLVM.
|
|
14
|
+
This Ruby Gem exists solely so the author can experiment with new language
|
|
15
|
+
features before committing those ideas to the real language. It is not
|
|
16
|
+
intended for general use, nor is it intended to be fast or concise.
|
|
17
|
+
EOF
|
|
18
|
+
|
|
19
|
+
spec.homepage = "https://github.com/d11wtq/iode-rb"
|
|
20
|
+
spec.license = "MIT"
|
|
21
|
+
|
|
22
|
+
spec.files = `git ls-files -z`.split("\x0")
|
|
23
|
+
spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
|
|
24
|
+
spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
|
|
25
|
+
spec.require_paths = ["lib"]
|
|
26
|
+
|
|
27
|
+
spec.add_runtime_dependency "whittle", "~> 0.0.8"
|
|
28
|
+
|
|
29
|
+
spec.add_development_dependency "bundler", "~> 1.5"
|
|
30
|
+
spec.add_development_dependency "rake"
|
|
31
|
+
end
|
data/lib/iode.rb
ADDED
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
# iode: iode.rb
|
|
2
|
+
#
|
|
3
|
+
# Copyright 2014 Chris Corbyn <chris@w3style.co.uk>
|
|
4
|
+
#
|
|
5
|
+
# Licensed under the Apache License, Version 2.0 (the "License");
|
|
6
|
+
# you may not use this file except in compliance with the License.
|
|
7
|
+
# You may obtain a copy of the License at
|
|
8
|
+
#
|
|
9
|
+
# http://www.apache.org/licenses/LICENSE-2.0
|
|
10
|
+
#
|
|
11
|
+
# Unless required by applicable law or agreed to in writing, software
|
|
12
|
+
# distributed under the License is distributed on an "AS IS" BASIS,
|
|
13
|
+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
14
|
+
# See the License for the specific language governing permissions and
|
|
15
|
+
# limitations under the License.
|
|
16
|
+
|
|
17
|
+
require "iode/version"
|
|
18
|
+
require "iode/scope"
|
|
19
|
+
require "iode/built_ins"
|
|
20
|
+
require "iode/interpreter"
|
|
21
|
+
require "iode/reader"
|
|
22
|
+
|
|
23
|
+
module Iode
|
|
24
|
+
class << self
|
|
25
|
+
# Run a string of iode source code and return a value to Ruby.
|
|
26
|
+
#
|
|
27
|
+
# @param [String] source
|
|
28
|
+
# iode source code
|
|
29
|
+
#
|
|
30
|
+
# @return [Object]
|
|
31
|
+
# the return value of the program
|
|
32
|
+
def run(source)
|
|
33
|
+
Interpreter.new.eval(Reader.new.read(source))
|
|
34
|
+
end
|
|
35
|
+
end
|
|
36
|
+
end
|
|
@@ -0,0 +1,61 @@
|
|
|
1
|
+
# iode: built_ins.rb
|
|
2
|
+
#
|
|
3
|
+
# Copyright 2014 Chris Corbyn <chris@w3style.co.uk>
|
|
4
|
+
#
|
|
5
|
+
# Licensed under the Apache License, Version 2.0 (the "License");
|
|
6
|
+
# you may not use this file except in compliance with the License.
|
|
7
|
+
# You may obtain a copy of the License at
|
|
8
|
+
#
|
|
9
|
+
# http://www.apache.org/licenses/LICENSE-2.0
|
|
10
|
+
#
|
|
11
|
+
# Unless required by applicable law or agreed to in writing, software
|
|
12
|
+
# distributed under the License is distributed on an "AS IS" BASIS,
|
|
13
|
+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
14
|
+
# See the License for the specific language governing permissions and
|
|
15
|
+
# limitations under the License.
|
|
16
|
+
|
|
17
|
+
module Iode
|
|
18
|
+
module BuiltIns
|
|
19
|
+
def car(list)
|
|
20
|
+
v, *_ = list
|
|
21
|
+
v
|
|
22
|
+
end
|
|
23
|
+
|
|
24
|
+
def cdr(list)
|
|
25
|
+
_, *v = list
|
|
26
|
+
v
|
|
27
|
+
end
|
|
28
|
+
|
|
29
|
+
def cadr(list)
|
|
30
|
+
car(cdr(list))
|
|
31
|
+
end
|
|
32
|
+
|
|
33
|
+
def cddr(list)
|
|
34
|
+
cdr(cdr(list))
|
|
35
|
+
end
|
|
36
|
+
|
|
37
|
+
def caddr(list)
|
|
38
|
+
car(cddr(list))
|
|
39
|
+
end
|
|
40
|
+
|
|
41
|
+
def cdddr(list)
|
|
42
|
+
cdr(cddr(list))
|
|
43
|
+
end
|
|
44
|
+
|
|
45
|
+
def cadddr(list)
|
|
46
|
+
car(cdddr(list))
|
|
47
|
+
end
|
|
48
|
+
|
|
49
|
+
def cddddr(list)
|
|
50
|
+
cdr(cdddr(list))
|
|
51
|
+
end
|
|
52
|
+
|
|
53
|
+
def caddddr(list)
|
|
54
|
+
car(cdddr(list))
|
|
55
|
+
end
|
|
56
|
+
|
|
57
|
+
def cdddddr(list)
|
|
58
|
+
cdr(cddddr(list))
|
|
59
|
+
end
|
|
60
|
+
end
|
|
61
|
+
end
|
|
@@ -0,0 +1,119 @@
|
|
|
1
|
+
# iode: interpreter.rb
|
|
2
|
+
#
|
|
3
|
+
# Copyright 2014 Chris Corbyn <chris@w3style.co.uk>
|
|
4
|
+
#
|
|
5
|
+
# Licensed under the Apache License, Version 2.0 (the "License");
|
|
6
|
+
# you may not use this file except in compliance with the License.
|
|
7
|
+
# You may obtain a copy of the License at
|
|
8
|
+
#
|
|
9
|
+
# http://www.apache.org/licenses/LICENSE-2.0
|
|
10
|
+
#
|
|
11
|
+
# Unless required by applicable law or agreed to in writing, software
|
|
12
|
+
# distributed under the License is distributed on an "AS IS" BASIS,
|
|
13
|
+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
14
|
+
# See the License for the specific language governing permissions and
|
|
15
|
+
# limitations under the License.
|
|
16
|
+
|
|
17
|
+
module Iode
|
|
18
|
+
# Iode interpreter, providing the central #eval function.
|
|
19
|
+
class Interpreter
|
|
20
|
+
include BuiltIns # FIXME: Remove this coupling!
|
|
21
|
+
|
|
22
|
+
# Create a new Interpreter with a given Scope.
|
|
23
|
+
#
|
|
24
|
+
# @param [Scope] scope
|
|
25
|
+
# the initial environment
|
|
26
|
+
def initialize(scope = Scope.new)
|
|
27
|
+
@env = scope
|
|
28
|
+
end
|
|
29
|
+
|
|
30
|
+
# Create an explicit progn block.
|
|
31
|
+
#
|
|
32
|
+
# A progn encapsulates a list of S-Expressions to be evaluated in sequence.
|
|
33
|
+
# The last evaluated S-Expression becomes the value of the progn.
|
|
34
|
+
#
|
|
35
|
+
# @param [Object...] *sexps
|
|
36
|
+
# a list of S-Expressions to wrap in a progn
|
|
37
|
+
#
|
|
38
|
+
# @return [Object]
|
|
39
|
+
# the value of the last S-Expression
|
|
40
|
+
def progn(*sexps)
|
|
41
|
+
sexps.inject(nil){|_,s| eval(s)}
|
|
42
|
+
end
|
|
43
|
+
|
|
44
|
+
# Create a new lambda for Iode.
|
|
45
|
+
#
|
|
46
|
+
# These lambdas act as closures in their environment.
|
|
47
|
+
#
|
|
48
|
+
# @param [Array] argnames
|
|
49
|
+
# a list of argument names as function inputs
|
|
50
|
+
#
|
|
51
|
+
# @param [Object...] *sexps
|
|
52
|
+
# variadic list of S-Expressions for the body
|
|
53
|
+
#
|
|
54
|
+
# @return [Proc]
|
|
55
|
+
# a callable lambda
|
|
56
|
+
def lambda(argnames, *sexps)
|
|
57
|
+
Proc.new do |*args|
|
|
58
|
+
Interpreter.new(
|
|
59
|
+
@env.push_scope(Hash[argnames.zip(args)])
|
|
60
|
+
).progn(*sexps)
|
|
61
|
+
end
|
|
62
|
+
end
|
|
63
|
+
|
|
64
|
+
# Apply a function to its arguments.
|
|
65
|
+
#
|
|
66
|
+
# @param [Callable] fn
|
|
67
|
+
# a Proc or a Lambda
|
|
68
|
+
#
|
|
69
|
+
# @param [Array] args
|
|
70
|
+
# a list of arguments to apply with
|
|
71
|
+
#
|
|
72
|
+
# @return [Object]
|
|
73
|
+
# the function return value
|
|
74
|
+
def apply(fn, args)
|
|
75
|
+
if fn.respond_to?(:call)
|
|
76
|
+
fn.call(*args)
|
|
77
|
+
else
|
|
78
|
+
raise "Cannot apply non-function `#{fn}`"
|
|
79
|
+
end
|
|
80
|
+
end
|
|
81
|
+
|
|
82
|
+
# Given an iode data structure, execute it.
|
|
83
|
+
#
|
|
84
|
+
# @param [Object] sexp
|
|
85
|
+
# any valid S-expression in iode
|
|
86
|
+
#
|
|
87
|
+
# @return [Object]
|
|
88
|
+
# whatever the expression evaluates to
|
|
89
|
+
def eval(sexp)
|
|
90
|
+
case sexp
|
|
91
|
+
when Array
|
|
92
|
+
case car(sexp)
|
|
93
|
+
when nil
|
|
94
|
+
nil
|
|
95
|
+
when :quote
|
|
96
|
+
cadr(sexp)
|
|
97
|
+
when :if
|
|
98
|
+
if eval(cadr(sexp))
|
|
99
|
+
eval(caddr(sexp))
|
|
100
|
+
else
|
|
101
|
+
eval(cadddr(sexp))
|
|
102
|
+
end
|
|
103
|
+
when :progn
|
|
104
|
+
progn(*cdr(sexp))
|
|
105
|
+
when :set!
|
|
106
|
+
@env[cadr(sexp)] = eval(caddr(sexp))
|
|
107
|
+
when :lambda
|
|
108
|
+
lambda(cadr(sexp), *cddr(sexp))
|
|
109
|
+
else
|
|
110
|
+
apply(eval(car(sexp)), cdr(sexp).map(&method(:eval)))
|
|
111
|
+
end
|
|
112
|
+
when Symbol
|
|
113
|
+
@env[sexp]
|
|
114
|
+
else
|
|
115
|
+
sexp
|
|
116
|
+
end
|
|
117
|
+
end
|
|
118
|
+
end
|
|
119
|
+
end
|
data/lib/iode/reader.rb
ADDED
|
@@ -0,0 +1,96 @@
|
|
|
1
|
+
# iode: reader.rb
|
|
2
|
+
#
|
|
3
|
+
# Copyright 2014 Chris Corbyn <chris@w3style.co.uk>
|
|
4
|
+
#
|
|
5
|
+
# Licensed under the Apache License, Version 2.0 (the "License");
|
|
6
|
+
# you may not use this file except in compliance with the License.
|
|
7
|
+
# You may obtain a copy of the License at
|
|
8
|
+
#
|
|
9
|
+
# http://www.apache.org/licenses/LICENSE-2.0
|
|
10
|
+
#
|
|
11
|
+
# Unless required by applicable law or agreed to in writing, software
|
|
12
|
+
# distributed under the License is distributed on an "AS IS" BASIS,
|
|
13
|
+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
14
|
+
# See the License for the specific language governing permissions and
|
|
15
|
+
# limitations under the License.
|
|
16
|
+
|
|
17
|
+
require "whittle"
|
|
18
|
+
|
|
19
|
+
module Iode
|
|
20
|
+
# Lisp reader, converting strings to lisp data structures.
|
|
21
|
+
class Reader < Whittle::Parser
|
|
22
|
+
# whitespace
|
|
23
|
+
rule(/\s+/).skip!
|
|
24
|
+
|
|
25
|
+
# comments
|
|
26
|
+
rule(/;.*/).skip!
|
|
27
|
+
|
|
28
|
+
# syntax
|
|
29
|
+
rule("(")
|
|
30
|
+
rule(")")
|
|
31
|
+
|
|
32
|
+
# scalars
|
|
33
|
+
{
|
|
34
|
+
float: /[0-9]*\.[0-9]+/,
|
|
35
|
+
int: /[0-9]+/,
|
|
36
|
+
string: /"(\\.|[^"])*"/,
|
|
37
|
+
regexp: /\/(\\.|[^\/])*\//
|
|
38
|
+
}.each do |name, pattern|
|
|
39
|
+
rule(name => pattern).as(&method(:eval))
|
|
40
|
+
end
|
|
41
|
+
|
|
42
|
+
# variables/symbols
|
|
43
|
+
rule(symbol: /[^\(\)\s;]+/).as do |v|
|
|
44
|
+
case v
|
|
45
|
+
when "nil"
|
|
46
|
+
nil
|
|
47
|
+
when "false"
|
|
48
|
+
false
|
|
49
|
+
when "true"
|
|
50
|
+
true
|
|
51
|
+
else
|
|
52
|
+
v.intern
|
|
53
|
+
end
|
|
54
|
+
end
|
|
55
|
+
|
|
56
|
+
# atoms
|
|
57
|
+
rule(:atom) do |r|
|
|
58
|
+
r[:int]
|
|
59
|
+
r[:float]
|
|
60
|
+
r[:string]
|
|
61
|
+
r[:symbol]
|
|
62
|
+
end
|
|
63
|
+
|
|
64
|
+
# lists of s-exps
|
|
65
|
+
rule(:sexp_list) do |r|
|
|
66
|
+
r[].as {[]}
|
|
67
|
+
r[:sexp].as {|sexp| [sexp]}
|
|
68
|
+
r[:sexp_list, :sexp].as{|list, sexp| list << sexp}
|
|
69
|
+
end
|
|
70
|
+
|
|
71
|
+
# s-exprs
|
|
72
|
+
rule(:sexp) do |r|
|
|
73
|
+
r[:atom]
|
|
74
|
+
r["(", :sexp_list, ")"].as {|_, list, _| list}
|
|
75
|
+
end
|
|
76
|
+
|
|
77
|
+
# a valid program is a list of s-exprs
|
|
78
|
+
start(:sexp_list)
|
|
79
|
+
|
|
80
|
+
# Read a string in as lisp data.
|
|
81
|
+
#
|
|
82
|
+
# @param [String] source
|
|
83
|
+
# iode source code to read in as lisp
|
|
84
|
+
#
|
|
85
|
+
# @return [Array]
|
|
86
|
+
# the source code as data for execution
|
|
87
|
+
def read(source)
|
|
88
|
+
progn = parse(source)
|
|
89
|
+
if progn.length > 1
|
|
90
|
+
[:progn, *progn]
|
|
91
|
+
else
|
|
92
|
+
progn.first
|
|
93
|
+
end
|
|
94
|
+
end
|
|
95
|
+
end
|
|
96
|
+
end
|
data/lib/iode/scope.rb
ADDED
|
@@ -0,0 +1,83 @@
|
|
|
1
|
+
# iode: scope.rb
|
|
2
|
+
#
|
|
3
|
+
# Copyright 2014 Chris Corbyn <chris@w3style.co.uk>
|
|
4
|
+
#
|
|
5
|
+
# Licensed under the Apache License, Version 2.0 (the "License");
|
|
6
|
+
# you may not use this file except in compliance with the License.
|
|
7
|
+
# You may obtain a copy of the License at
|
|
8
|
+
#
|
|
9
|
+
# http://www.apache.org/licenses/LICENSE-2.0
|
|
10
|
+
#
|
|
11
|
+
# Unless required by applicable law or agreed to in writing, software
|
|
12
|
+
# distributed under the License is distributed on an "AS IS" BASIS,
|
|
13
|
+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
14
|
+
# See the License for the specific language governing permissions and
|
|
15
|
+
# limitations under the License.
|
|
16
|
+
|
|
17
|
+
module Iode
|
|
18
|
+
# Lexical scope environment for iode.
|
|
19
|
+
#
|
|
20
|
+
# Maintains a stack of execution contexts.
|
|
21
|
+
class Scope
|
|
22
|
+
# Create a new Scope with +values+ as available variables.
|
|
23
|
+
#
|
|
24
|
+
# @param [Hash] values
|
|
25
|
+
# key-values pairs of Symbol => Object
|
|
26
|
+
#
|
|
27
|
+
# @param [Scope] parent
|
|
28
|
+
# the parent scope, if any
|
|
29
|
+
def initialize(values = {}, parent = nil)
|
|
30
|
+
@values = values
|
|
31
|
+
@parent = parent
|
|
32
|
+
end
|
|
33
|
+
|
|
34
|
+
# Reference a variable in this scope.
|
|
35
|
+
#
|
|
36
|
+
# Raises a RuntimeError if the variable does not exist.
|
|
37
|
+
#
|
|
38
|
+
# @param [Symbol] k
|
|
39
|
+
# the variable name to lookup
|
|
40
|
+
#
|
|
41
|
+
# @return [Object]
|
|
42
|
+
# the object stored in this variable
|
|
43
|
+
def [](k)
|
|
44
|
+
if @values.key?(k)
|
|
45
|
+
@values[k]
|
|
46
|
+
elsif @parent
|
|
47
|
+
@parent[k]
|
|
48
|
+
else
|
|
49
|
+
raise "Undefined variable `#{k}`"
|
|
50
|
+
end
|
|
51
|
+
end
|
|
52
|
+
|
|
53
|
+
# Re-assign a variable in this scope.
|
|
54
|
+
#
|
|
55
|
+
# If the variable does not exist, it will be initialized.
|
|
56
|
+
#
|
|
57
|
+
# @param [Symbol] k
|
|
58
|
+
# the variable name to assign
|
|
59
|
+
#
|
|
60
|
+
# @param [Object] v
|
|
61
|
+
# the value to assign
|
|
62
|
+
#
|
|
63
|
+
# @return [Object]
|
|
64
|
+
# the assigned object
|
|
65
|
+
def []=(k, v)
|
|
66
|
+
@values[k] = v
|
|
67
|
+
end
|
|
68
|
+
|
|
69
|
+
# Create a new Scope with this Scope as its parent.
|
|
70
|
+
#
|
|
71
|
+
# The new Scope will have access to all variables in this Scope,
|
|
72
|
+
# but assignments will only mask variables, not replace them.
|
|
73
|
+
#
|
|
74
|
+
# @param [Hash] values
|
|
75
|
+
# variables to exist in the new Scope
|
|
76
|
+
#
|
|
77
|
+
# @return [Scope]
|
|
78
|
+
# a new Scope with this Scope as its parent
|
|
79
|
+
def push_scope(values = {})
|
|
80
|
+
Scope.new(values, self)
|
|
81
|
+
end
|
|
82
|
+
end
|
|
83
|
+
end
|
data/lib/iode/version.rb
ADDED
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
# iode: version.rb
|
|
2
|
+
#
|
|
3
|
+
# Copyright 2014 Chris Corbyn <chris@w3style.co.uk>
|
|
4
|
+
#
|
|
5
|
+
# Licensed under the Apache License, Version 2.0 (the "License");
|
|
6
|
+
# you may not use this file except in compliance with the License.
|
|
7
|
+
# You may obtain a copy of the License at
|
|
8
|
+
#
|
|
9
|
+
# http://www.apache.org/licenses/LICENSE-2.0
|
|
10
|
+
#
|
|
11
|
+
# Unless required by applicable law or agreed to in writing, software
|
|
12
|
+
# distributed under the License is distributed on an "AS IS" BASIS,
|
|
13
|
+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
14
|
+
# See the License for the specific language governing permissions and
|
|
15
|
+
# limitations under the License.
|
|
16
|
+
|
|
17
|
+
module Iode
|
|
18
|
+
VERSION = "0.0.1"
|
|
19
|
+
end
|
metadata
ADDED
|
@@ -0,0 +1,104 @@
|
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
|
2
|
+
name: iode
|
|
3
|
+
version: !ruby/object:Gem::Version
|
|
4
|
+
version: 0.0.1
|
|
5
|
+
platform: ruby
|
|
6
|
+
authors:
|
|
7
|
+
- Chris Corbyn
|
|
8
|
+
autorequire:
|
|
9
|
+
bindir: bin
|
|
10
|
+
cert_chain: []
|
|
11
|
+
date: 2014-03-17 00:00:00.000000000 Z
|
|
12
|
+
dependencies:
|
|
13
|
+
- !ruby/object:Gem::Dependency
|
|
14
|
+
name: whittle
|
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
|
16
|
+
requirements:
|
|
17
|
+
- - "~>"
|
|
18
|
+
- !ruby/object:Gem::Version
|
|
19
|
+
version: 0.0.8
|
|
20
|
+
type: :runtime
|
|
21
|
+
prerelease: false
|
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
|
23
|
+
requirements:
|
|
24
|
+
- - "~>"
|
|
25
|
+
- !ruby/object:Gem::Version
|
|
26
|
+
version: 0.0.8
|
|
27
|
+
- !ruby/object:Gem::Dependency
|
|
28
|
+
name: bundler
|
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
|
30
|
+
requirements:
|
|
31
|
+
- - "~>"
|
|
32
|
+
- !ruby/object:Gem::Version
|
|
33
|
+
version: '1.5'
|
|
34
|
+
type: :development
|
|
35
|
+
prerelease: false
|
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
|
37
|
+
requirements:
|
|
38
|
+
- - "~>"
|
|
39
|
+
- !ruby/object:Gem::Version
|
|
40
|
+
version: '1.5'
|
|
41
|
+
- !ruby/object:Gem::Dependency
|
|
42
|
+
name: rake
|
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
|
44
|
+
requirements:
|
|
45
|
+
- - ">="
|
|
46
|
+
- !ruby/object:Gem::Version
|
|
47
|
+
version: '0'
|
|
48
|
+
type: :development
|
|
49
|
+
prerelease: false
|
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
|
51
|
+
requirements:
|
|
52
|
+
- - ">="
|
|
53
|
+
- !ruby/object:Gem::Version
|
|
54
|
+
version: '0'
|
|
55
|
+
description: |2
|
|
56
|
+
Iode is a work in progress real language on LLVM.
|
|
57
|
+
This Ruby Gem exists solely so the author can experiment with new language
|
|
58
|
+
features before committing those ideas to the real language. It is not
|
|
59
|
+
intended for general use, nor is it intended to be fast or concise.
|
|
60
|
+
email:
|
|
61
|
+
- chris@w3style.co.uk
|
|
62
|
+
executables:
|
|
63
|
+
- iode-rb
|
|
64
|
+
extensions: []
|
|
65
|
+
extra_rdoc_files: []
|
|
66
|
+
files:
|
|
67
|
+
- ".gitignore"
|
|
68
|
+
- Gemfile
|
|
69
|
+
- LICENSE.txt
|
|
70
|
+
- README.md
|
|
71
|
+
- Rakefile
|
|
72
|
+
- bin/iode-rb
|
|
73
|
+
- iode.gemspec
|
|
74
|
+
- lib/iode.rb
|
|
75
|
+
- lib/iode/built_ins.rb
|
|
76
|
+
- lib/iode/interpreter.rb
|
|
77
|
+
- lib/iode/reader.rb
|
|
78
|
+
- lib/iode/scope.rb
|
|
79
|
+
- lib/iode/version.rb
|
|
80
|
+
homepage: https://github.com/d11wtq/iode-rb
|
|
81
|
+
licenses:
|
|
82
|
+
- MIT
|
|
83
|
+
metadata: {}
|
|
84
|
+
post_install_message:
|
|
85
|
+
rdoc_options: []
|
|
86
|
+
require_paths:
|
|
87
|
+
- lib
|
|
88
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
|
89
|
+
requirements:
|
|
90
|
+
- - ">="
|
|
91
|
+
- !ruby/object:Gem::Version
|
|
92
|
+
version: '0'
|
|
93
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
|
94
|
+
requirements:
|
|
95
|
+
- - ">="
|
|
96
|
+
- !ruby/object:Gem::Version
|
|
97
|
+
version: '0'
|
|
98
|
+
requirements: []
|
|
99
|
+
rubyforge_project:
|
|
100
|
+
rubygems_version: 2.2.0
|
|
101
|
+
signing_key:
|
|
102
|
+
specification_version: 4
|
|
103
|
+
summary: An experimental lisp-family language hosted on Ruby
|
|
104
|
+
test_files: []
|