risp-lang 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 +4 -0
- data/Gemfile +4 -0
- data/LICENSE.txt +21 -0
- data/README.md +135 -0
- data/Rakefile +6 -0
- data/bin/console +14 -0
- data/bin/setup +7 -0
- data/exe/risp +7 -0
- data/exe/risp-repl +31 -0
- data/lib/risp.rb +22 -0
- data/lib/risp/core.risp +103 -0
- data/lib/risp/interpreter.rb +149 -0
- data/lib/risp/lexer.rb +26 -0
- data/lib/risp/parser.rb +75 -0
- data/lib/risp/version.rb +3 -0
- data/risp.gemspec +36 -0
- metadata +135 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: dee12380e1191bbf433642063e884fa3a25ef51e
|
4
|
+
data.tar.gz: 77dec2a46e3b2c3cffcd4d904ac52c1b5dfb4e56
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 0de0e23fc14fa6c155748d942ced4a60d7be29aace0635c0d596da28261566ccf0b247ed5eb4ac3331cb70a00ad84e81e0a88e8496bae9e323fcd1b357964798
|
7
|
+
data.tar.gz: d22e9e7e96b632e12f2f98c8975e3d0980a77a0c083c2489eb6d116047f067f154ec722846ce8b22e28a395e83a173087cc5566f67ccc4b693e40e8b2ed4e1fd
|
data/.gitignore
ADDED
data/.rspec
ADDED
data/.travis.yml
ADDED
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
The MIT License (MIT)
|
2
|
+
|
3
|
+
Copyright (c) 2015 Luca Ongaro
|
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,135 @@
|
|
1
|
+
# Risp
|
2
|
+
|
3
|
+
`Risp` is a LISP implementation written in Ruby. The syntax is reminescent of
|
4
|
+
Clojure, and it interoperates with Ruby.
|
5
|
+
|
6
|
+
|
7
|
+
## Why?
|
8
|
+
|
9
|
+
Why not? :P
|
10
|
+
|
11
|
+
Mostly I did this to learn a bit more about programming language design. Or
|
12
|
+
maybe I was bored. And also I really like LISPs and I like the idea of writing
|
13
|
+
LISP leveraging on the Ruby ecosystem. But still this is mostly an experiment,
|
14
|
+
so if you use it you cannot blame me if it ends up eating your laundry or
|
15
|
+
setting your kitchen on fire.
|
16
|
+
|
17
|
+
|
18
|
+
## Installation
|
19
|
+
|
20
|
+
Add this line to your application's Gemfile:
|
21
|
+
|
22
|
+
```ruby
|
23
|
+
gem 'risp-lang'
|
24
|
+
```
|
25
|
+
|
26
|
+
And then execute:
|
27
|
+
|
28
|
+
$ bundle
|
29
|
+
|
30
|
+
Or install it yourself as:
|
31
|
+
|
32
|
+
$ gem install risp
|
33
|
+
|
34
|
+
|
35
|
+
## Usage
|
36
|
+
|
37
|
+
### Start a REPL
|
38
|
+
|
39
|
+
Just run `risp-repl`
|
40
|
+
|
41
|
+
### Execute a file
|
42
|
+
|
43
|
+
`risp my_program.risp`
|
44
|
+
|
45
|
+
### Inside Ruby
|
46
|
+
|
47
|
+
Instantiate an interpreter and evaluate code:
|
48
|
+
|
49
|
+
```ruby
|
50
|
+
require 'risp'
|
51
|
+
risp = Risp::Interpreter.new
|
52
|
+
|
53
|
+
risp.eval <<-CODE
|
54
|
+
(def double [x]
|
55
|
+
(* 2 x))
|
56
|
+
|
57
|
+
(double 5)
|
58
|
+
CODE
|
59
|
+
```
|
60
|
+
|
61
|
+
|
62
|
+
## Syntax
|
63
|
+
|
64
|
+
The LISP syntax is very similar to Clojure:
|
65
|
+
|
66
|
+
```lisp
|
67
|
+
; Define a function
|
68
|
+
(defn dec [n]
|
69
|
+
(- n 1))
|
70
|
+
|
71
|
+
; Define recursive factorial
|
72
|
+
(defn fact [n]
|
73
|
+
(if (<= n 1)
|
74
|
+
1
|
75
|
+
(* n (fact (dec n)))))
|
76
|
+
|
77
|
+
(fact 10) ; => 3628800
|
78
|
+
|
79
|
+
; Rest argument
|
80
|
+
(defn foo [a b &more]
|
81
|
+
[a b more])
|
82
|
+
|
83
|
+
(foo 1 2 3 4 5) ; => [1 2 [3 4 5]]
|
84
|
+
|
85
|
+
; Argument destructuring
|
86
|
+
(defn swap-pairs [[a b] [c d]]
|
87
|
+
[[a c] [b d]])
|
88
|
+
|
89
|
+
(swap-pairs [1 2] [3 4]) ; => [[1 3] [2 4]]
|
90
|
+
```
|
91
|
+
|
92
|
+
|
93
|
+
## Macro support
|
94
|
+
|
95
|
+
Macros, quoting and unquoting are supported:
|
96
|
+
|
97
|
+
```lisp
|
98
|
+
(defmacro defn- [name args body]
|
99
|
+
'(def ~name (fn ~args ~body)))
|
100
|
+
|
101
|
+
(defn- sum [a b]
|
102
|
+
(+ a b))
|
103
|
+
|
104
|
+
(sum 4 5)
|
105
|
+
```
|
106
|
+
|
107
|
+
|
108
|
+
## Ruby interoperability
|
109
|
+
|
110
|
+
```lisp
|
111
|
+
; Ruby methods can be called the same way as LISP functions, just prepend a
|
112
|
+
; dot to the method name, pass the receiver as the first argument, followed
|
113
|
+
; by any other argument:
|
114
|
+
|
115
|
+
(.join ["highway" "to" "the" "danger" "zone"] " ")
|
116
|
+
|
117
|
+
; Ruby constants, modules and classes are available, just use Foo/Bar instead
|
118
|
+
; of Foo::Bar:
|
119
|
+
|
120
|
+
(defn circle-area [radius]
|
121
|
+
(* 2 Math/PI radius))
|
122
|
+
```
|
123
|
+
|
124
|
+
|
125
|
+
## Contributing
|
126
|
+
|
127
|
+
Bug reports and pull requests are welcome on GitHub at
|
128
|
+
https://github.com/lucaong/risp.
|
129
|
+
|
130
|
+
|
131
|
+
## License
|
132
|
+
|
133
|
+
The gem is available as open source under the terms of the [MIT
|
134
|
+
License](http://opensource.org/licenses/MIT).
|
135
|
+
|
data/Rakefile
ADDED
data/bin/console
ADDED
@@ -0,0 +1,14 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
|
3
|
+
require "bundler/setup"
|
4
|
+
require "risp"
|
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/exe/risp
ADDED
data/exe/risp-repl
ADDED
@@ -0,0 +1,31 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
|
3
|
+
require "bundler/setup"
|
4
|
+
require "risp"
|
5
|
+
require "readline"
|
6
|
+
|
7
|
+
risp = Risp::Interpreter.new
|
8
|
+
|
9
|
+
def complete?(input)
|
10
|
+
input.count('(') == input.count(')') &&
|
11
|
+
input.count('[') == input.count(']') &&
|
12
|
+
input.count('{') == input.count('}')
|
13
|
+
end
|
14
|
+
|
15
|
+
puts "The Risp v#{Risp::VERSION} REPL"
|
16
|
+
|
17
|
+
input = ''
|
18
|
+
while line = Readline.readline('> ', true)
|
19
|
+
begin
|
20
|
+
input << "\n#{line}"
|
21
|
+
if complete?(input) && input.gsub(/\s/, '').size > 0
|
22
|
+
puts "=> #{risp.eval(input).inspect}"
|
23
|
+
input = ''
|
24
|
+
else
|
25
|
+
next
|
26
|
+
end
|
27
|
+
rescue => e
|
28
|
+
puts e.inspect
|
29
|
+
input = ''
|
30
|
+
end
|
31
|
+
end
|
data/lib/risp.rb
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
require "risp/version"
|
2
|
+
require "risp/interpreter"
|
3
|
+
|
4
|
+
module Risp
|
5
|
+
class Symbol < Struct.new(:name)
|
6
|
+
def inspect
|
7
|
+
name.to_s
|
8
|
+
end
|
9
|
+
end
|
10
|
+
|
11
|
+
class Method < Struct.new(:name)
|
12
|
+
def inspect
|
13
|
+
name.to_s
|
14
|
+
end
|
15
|
+
end
|
16
|
+
|
17
|
+
class Splat < Struct.new(:name)
|
18
|
+
def inspect
|
19
|
+
"&#{name.to_s}"
|
20
|
+
end
|
21
|
+
end
|
22
|
+
end
|
data/lib/risp/core.risp
ADDED
@@ -0,0 +1,103 @@
|
|
1
|
+
(defmacro defn [name args body]
|
2
|
+
'(def ~name (fn ~args ~body)))
|
3
|
+
|
4
|
+
(defmacro ->> [coll &forms]
|
5
|
+
(.reduce forms coll (fn [a f] (.<< f a))))
|
6
|
+
|
7
|
+
(defmacro -> [coll &forms]
|
8
|
+
(.reduce forms coll
|
9
|
+
(fn [a f]
|
10
|
+
(.concat '(~(first f) ~a) (rest f)))))
|
11
|
+
|
12
|
+
(defn + [&xs]
|
13
|
+
(.reduce xs 0 :+))
|
14
|
+
|
15
|
+
(defn - [x &xs]
|
16
|
+
(if (.empty? xs)
|
17
|
+
(- 0 x)
|
18
|
+
(.reduce xs x :-)))
|
19
|
+
|
20
|
+
(defn * [&xs]
|
21
|
+
(.reduce xs 1 :*))
|
22
|
+
|
23
|
+
(defn / [x &xs]
|
24
|
+
(if (.empty? xs)
|
25
|
+
(/ 1 x)
|
26
|
+
(.reduce xs x :/)))
|
27
|
+
|
28
|
+
(defn > [&xs]
|
29
|
+
(.all? (.each_cons xs 2) (fn [[a b]] (.> a b))))
|
30
|
+
|
31
|
+
(defn >= [&xs]
|
32
|
+
(.all? (.each_cons xs 2) (fn [[a b]] (.>= a b))))
|
33
|
+
|
34
|
+
(defn < [&xs]
|
35
|
+
(.all? (.each_cons xs 2) (fn [[a b]] (.< a b))))
|
36
|
+
|
37
|
+
(defn <= [&xs]
|
38
|
+
(.all? (.each_cons xs 2) (fn [[a b]] (.<= a b))))
|
39
|
+
|
40
|
+
(defn = [&xs]
|
41
|
+
(.all? (.each_cons xs 2) (fn [[a b]] (.== a b))))
|
42
|
+
|
43
|
+
(defn compare [a b]
|
44
|
+
(.<=> a b))
|
45
|
+
|
46
|
+
(defn vector [&xs]
|
47
|
+
(.new Hamster/Vector xs))
|
48
|
+
|
49
|
+
(defn set [&xs]
|
50
|
+
(.new Hamster/Set xs))
|
51
|
+
|
52
|
+
(defn sorted-set [&xs]
|
53
|
+
(.new Hamster/SortedSet xs))
|
54
|
+
|
55
|
+
(defn sorted-set-by [comparator &xs]
|
56
|
+
(.new Hamster/SortedSet xs comparator))
|
57
|
+
|
58
|
+
(defn list [&xs]
|
59
|
+
(.new Hamster/List xs))
|
60
|
+
|
61
|
+
(defn hash-map [&xs]
|
62
|
+
(.new Hamster/Hash (.each_slice xs 2)))
|
63
|
+
|
64
|
+
(defn ruby-array [&xs]
|
65
|
+
(.new Array xs))
|
66
|
+
|
67
|
+
(defn ruby-set [&xs]
|
68
|
+
(.new Set xs))
|
69
|
+
|
70
|
+
(defn ruby-hash [&xs]
|
71
|
+
(.to_h (.each_slice xs 2)))
|
72
|
+
|
73
|
+
(defn nil? [x]
|
74
|
+
(.nil? x))
|
75
|
+
|
76
|
+
(defn filter [f coll]
|
77
|
+
(.select coll f))
|
78
|
+
|
79
|
+
(defn map [f coll]
|
80
|
+
(.map coll f))
|
81
|
+
|
82
|
+
(defn reduce [f s coll]
|
83
|
+
(if (nil? coll)
|
84
|
+
(.reduce s f)
|
85
|
+
(.reduce coll s f)))
|
86
|
+
|
87
|
+
(defn take [n coll]
|
88
|
+
(.take coll n))
|
89
|
+
|
90
|
+
(defn drop [n coll]
|
91
|
+
(.drop coll n))
|
92
|
+
|
93
|
+
(defn first [coll]
|
94
|
+
(.first coll))
|
95
|
+
|
96
|
+
(defn rest [coll]
|
97
|
+
(drop 1 coll))
|
98
|
+
|
99
|
+
(defn get [coll k default]
|
100
|
+
(.fetch coll k default))
|
101
|
+
|
102
|
+
(defn println [&xs]
|
103
|
+
(.puts (.new Object) (.join xs " ")))
|
@@ -0,0 +1,149 @@
|
|
1
|
+
require "risp/lexer"
|
2
|
+
require "risp/parser"
|
3
|
+
require "hamster"
|
4
|
+
|
5
|
+
module Risp
|
6
|
+
class Interpreter
|
7
|
+
attr_reader :binding, :macros, :lexer, :parser
|
8
|
+
|
9
|
+
SPECIAL_FORMS = {
|
10
|
+
def: -> (elems, binding, locals, macros) {
|
11
|
+
symbol, value = elems
|
12
|
+
binding[symbol.name] = eval(value, binding, locals, macros)
|
13
|
+
},
|
14
|
+
let: -> (elems, binding, locals, macros) {
|
15
|
+
(*assigns), *forms = elems
|
16
|
+
locals = assigns.each_slice(2).reduce(locals.dup) do |locals, (s, v)|
|
17
|
+
locals.merge assign_args([s], [eval(v, binding, locals, macros)])
|
18
|
+
end
|
19
|
+
forms.map { |form| eval(form, binding, locals, macros) }.last
|
20
|
+
},
|
21
|
+
fn: -> (elems, binding, locals, macros) {
|
22
|
+
(*as), body = elems
|
23
|
+
-> (*args) do
|
24
|
+
locals = locals.merge(assign_args(as, args))
|
25
|
+
eval(body, binding, locals, macros)
|
26
|
+
end
|
27
|
+
},
|
28
|
+
do: -> (elems, binding, locals, macros) {
|
29
|
+
elems.map { |el| eval(el, binding, locals, macros) }.last
|
30
|
+
},
|
31
|
+
if: -> (elems, binding, locals, macros) {
|
32
|
+
condition, _then, _else = elems
|
33
|
+
if eval(condition, binding, locals, macros)
|
34
|
+
eval(_then, binding, locals, macros)
|
35
|
+
else
|
36
|
+
eval(_else, binding, locals, macros)
|
37
|
+
end
|
38
|
+
},
|
39
|
+
quote: -> (elems, binding, locals, macros) {
|
40
|
+
unquote(elems.first, binding, locals, macros)
|
41
|
+
},
|
42
|
+
defmacro: -> (elems, binding, locals, macros) {
|
43
|
+
symbol, (*as), body = elems
|
44
|
+
macros[symbol.name] = -> (*args) do
|
45
|
+
locals = locals.merge(assign_args(as, args))
|
46
|
+
eval(body, binding, locals, macros)
|
47
|
+
end
|
48
|
+
},
|
49
|
+
apply: -> (elems, binding, locals, macros) {
|
50
|
+
fn, args = elems.map { |x| eval(x, binding, locals, macros) }
|
51
|
+
fn.call(*args)
|
52
|
+
},
|
53
|
+
require: -> (elems, binding, locals, macros) {
|
54
|
+
elems.each do |lib|
|
55
|
+
require lib
|
56
|
+
end
|
57
|
+
}
|
58
|
+
}
|
59
|
+
|
60
|
+
def initialize()
|
61
|
+
@binding = {}
|
62
|
+
@macros = {}
|
63
|
+
@lexer = Risp::Lexer.new
|
64
|
+
@parser = Risp::Parser.new
|
65
|
+
corelib = File.read(File.expand_path('core.risp', File.dirname(__FILE__)))
|
66
|
+
eval(corelib)
|
67
|
+
end
|
68
|
+
|
69
|
+
def eval(code)
|
70
|
+
parser.parse(lexer.lex(code)).map { |x| self.class.eval(x, binding, {}, macros) }.last
|
71
|
+
end
|
72
|
+
|
73
|
+
def self.eval(expr, binding, locals, macros)
|
74
|
+
case expr
|
75
|
+
when Hamster::List
|
76
|
+
first = expr.first
|
77
|
+
if special = first.is_a?(Risp::Symbol) && SPECIAL_FORMS[first.name]
|
78
|
+
special.call(expr.drop(1), binding, locals, macros)
|
79
|
+
elsif macro = first.is_a?(Risp::Symbol) && macros[first.name]
|
80
|
+
_, *args = expr
|
81
|
+
eval(macro.call(*args), binding, locals, macros)
|
82
|
+
elsif first.is_a?(Risp::Method)
|
83
|
+
receiver, *args = expr.drop(1).map { |x| eval(x, binding, locals, macros) }
|
84
|
+
if args.last.is_a?(Proc) && receiver.method(first.name).arity < args.size
|
85
|
+
*args, block = args
|
86
|
+
receiver.send(first.name, *args, &block)
|
87
|
+
else
|
88
|
+
receiver.send(first.name, *args)
|
89
|
+
end
|
90
|
+
else
|
91
|
+
fn, *args = expr.map { |x| eval(x, binding, locals, macros) }
|
92
|
+
fn.call(*args)
|
93
|
+
end
|
94
|
+
when Risp::Symbol
|
95
|
+
symbol = expr.name
|
96
|
+
resolve(symbol, binding, locals, macros)
|
97
|
+
when Enumerable
|
98
|
+
expr.map { |x| eval(x, binding, locals, macros) }
|
99
|
+
else
|
100
|
+
expr
|
101
|
+
end
|
102
|
+
end
|
103
|
+
|
104
|
+
def self.unquote(expr, binding, locals, macros)
|
105
|
+
if expr.is_a?(Enumerable) && !expr.is_a?(Risp::Symbol)
|
106
|
+
first, second = expr
|
107
|
+
if first.is_a?(Risp::Symbol) && first.name == :unquote
|
108
|
+
eval(second, binding, locals, macros)
|
109
|
+
else
|
110
|
+
expr.map { |x| unquote(x, binding, locals, macros) }
|
111
|
+
end
|
112
|
+
else
|
113
|
+
expr
|
114
|
+
end
|
115
|
+
end
|
116
|
+
|
117
|
+
def self.resolve(symbol, binding, locals, macros)
|
118
|
+
if locals.has_key?(symbol)
|
119
|
+
locals[symbol]
|
120
|
+
elsif binding.has_key?(symbol)
|
121
|
+
binding[symbol]
|
122
|
+
else
|
123
|
+
begin
|
124
|
+
symbol.to_s.split('/').reduce(Object) do |c, p|
|
125
|
+
c.const_get(p)
|
126
|
+
end
|
127
|
+
rescue NameError => e
|
128
|
+
raise "cannot resolve #{symbol}"
|
129
|
+
end
|
130
|
+
end
|
131
|
+
end
|
132
|
+
|
133
|
+
def self.assign_args(symbols, values)
|
134
|
+
symbols.each_with_index.reduce({}) do |a, (s, i)|
|
135
|
+
v = values[i]
|
136
|
+
if s.is_a?(Hamster::Vector) && v.is_a?(Enumerable)
|
137
|
+
a.merge(assign_args(s, v))
|
138
|
+
elsif s.is_a?(Risp::Splat)
|
139
|
+
a[s.name] = values.drop(i)
|
140
|
+
a
|
141
|
+
else
|
142
|
+
a[s.name] = v
|
143
|
+
a
|
144
|
+
end
|
145
|
+
end
|
146
|
+
end
|
147
|
+
end
|
148
|
+
end
|
149
|
+
|
data/lib/risp/lexer.rb
ADDED
@@ -0,0 +1,26 @@
|
|
1
|
+
require "rltk/lexer"
|
2
|
+
|
3
|
+
module Risp
|
4
|
+
class Lexer < RLTK::Lexer
|
5
|
+
rule(/"[^"]*"/) { |t| [:STRING, t[1...-1]] }
|
6
|
+
rule(/-?\d+\.\d+/) { |t| [:FLOAT, t.to_f] }
|
7
|
+
rule(/-?\d+/) { |t| [:INTEGER, t.to_i] }
|
8
|
+
rule(/true|false/) { |b| [:BOOLEAN, b == 'true'] }
|
9
|
+
rule(/nil|\(\)/) { :NIL }
|
10
|
+
rule(/\(/) { :LPAREN }
|
11
|
+
rule(/\)/) { :RPAREN }
|
12
|
+
rule(/\[/) { :LSQBRACK }
|
13
|
+
rule(/\]/) { :RSQBRACK }
|
14
|
+
rule(/\{/) { :LBRACE }
|
15
|
+
rule(/\}/) { :RBRACE }
|
16
|
+
rule(/\./) { :DOT }
|
17
|
+
rule(/#/) { :POUND }
|
18
|
+
rule(/'/) { :QUOTE }
|
19
|
+
rule(/~/) { :UNQUOTE }
|
20
|
+
rule(/&/) { :AMPERSAND }
|
21
|
+
rule(/;[^\n]*/) # ignore comments
|
22
|
+
rule(/:[^&'~\(\)\{\}\[\]\.#\s,]+/i) { |t| [:KEYWORD, t] }
|
23
|
+
rule(/[^:&'~\(\)\{\}\[\]\.#\s,]+/i) { |t| [:SYMBOL, t] }
|
24
|
+
rule(/[\s,]/) # ignore whitespaces and commas
|
25
|
+
end
|
26
|
+
end
|
data/lib/risp/parser.rb
ADDED
@@ -0,0 +1,75 @@
|
|
1
|
+
require 'rltk/parser'
|
2
|
+
|
3
|
+
module Risp
|
4
|
+
class Parser < RLTK::Parser
|
5
|
+
build_list_production(:expressions, :expression)
|
6
|
+
|
7
|
+
production(:expression) do
|
8
|
+
clause('atom') do |atom|
|
9
|
+
atom
|
10
|
+
end
|
11
|
+
clause('list') do |list|
|
12
|
+
list
|
13
|
+
end
|
14
|
+
clause('QUOTE expression') do |_, expr|
|
15
|
+
Hamster::List[Risp::Symbol.new(:quote), expr]
|
16
|
+
end
|
17
|
+
clause('UNQUOTE expression') do |_, expr|
|
18
|
+
Hamster::List[Risp::Symbol.new(:unquote), expr]
|
19
|
+
end
|
20
|
+
end
|
21
|
+
|
22
|
+
production(:list) do
|
23
|
+
clause('LPAREN expressions RPAREN') do |_, elems, _|
|
24
|
+
Hamster::List[*elems]
|
25
|
+
end
|
26
|
+
end
|
27
|
+
|
28
|
+
production(:atom) do
|
29
|
+
clause('literal') do |l|
|
30
|
+
l
|
31
|
+
end
|
32
|
+
clause('SYMBOL') do |name|
|
33
|
+
Risp::Symbol.new(name.to_sym)
|
34
|
+
end
|
35
|
+
clause('DOT SYMBOL') do |_, name|
|
36
|
+
Risp::Method.new(name.to_sym)
|
37
|
+
end
|
38
|
+
clause('AMPERSAND SYMBOL') do |_, name|
|
39
|
+
Risp::Splat.new(name.to_sym)
|
40
|
+
end
|
41
|
+
end
|
42
|
+
|
43
|
+
production(:literal) do
|
44
|
+
clause('INTEGER') do |n|
|
45
|
+
n
|
46
|
+
end
|
47
|
+
clause('FLOAT') do |f|
|
48
|
+
f
|
49
|
+
end
|
50
|
+
clause('STRING') do |s|
|
51
|
+
s
|
52
|
+
end
|
53
|
+
clause('BOOLEAN') do |b|
|
54
|
+
b
|
55
|
+
end
|
56
|
+
clause('KEYWORD') do |k|
|
57
|
+
k[1..-1].to_sym
|
58
|
+
end
|
59
|
+
clause('LSQBRACK expressions RSQBRACK') do |_, exprs, _|
|
60
|
+
Hamster::Vector.new(exprs)
|
61
|
+
end
|
62
|
+
clause('POUND LBRACE expressions RBRACE') do |_, _, exprs, _|
|
63
|
+
Hamster::Set.new(exprs)
|
64
|
+
end
|
65
|
+
clause('LBRACE expressions RBRACE') do |_, keyvals, _|
|
66
|
+
Hamster::Hash.new(keyvals.each_slice(2).to_h)
|
67
|
+
end
|
68
|
+
clause('NIL') do |_|
|
69
|
+
nil
|
70
|
+
end
|
71
|
+
end
|
72
|
+
|
73
|
+
finalize
|
74
|
+
end
|
75
|
+
end
|
data/lib/risp/version.rb
ADDED
data/risp.gemspec
ADDED
@@ -0,0 +1,36 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require 'risp/version'
|
5
|
+
|
6
|
+
Gem::Specification.new do |spec|
|
7
|
+
spec.name = "risp-lang"
|
8
|
+
spec.version = Risp::VERSION
|
9
|
+
spec.authors = ["Luca Ongaro"]
|
10
|
+
spec.email = ["lukeongaro@gmail.com"]
|
11
|
+
|
12
|
+
spec.summary = "LISP implementation in Ruby"
|
13
|
+
spec.description = "LISP implementation in Ruby"
|
14
|
+
spec.homepage = "https://github.com/lucaong/risp"
|
15
|
+
spec.license = "MIT"
|
16
|
+
|
17
|
+
# Prevent pushing this gem to RubyGems.org by setting 'allowed_push_host', or
|
18
|
+
# delete this section to allow pushing this gem to any host.
|
19
|
+
if spec.respond_to?(:metadata)
|
20
|
+
spec.metadata['allowed_push_host'] = "https://rubygems.org"
|
21
|
+
else
|
22
|
+
raise "RubyGems 2.0 or newer is required to protect against public gem pushes."
|
23
|
+
end
|
24
|
+
|
25
|
+
spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
|
26
|
+
spec.bindir = "exe"
|
27
|
+
spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
|
28
|
+
spec.require_paths = ["lib"]
|
29
|
+
|
30
|
+
spec.add_dependency "rltk", "~> 3.0"
|
31
|
+
spec.add_dependency "hamster", "~> 1.0"
|
32
|
+
|
33
|
+
spec.add_development_dependency "bundler", "~> 1.10"
|
34
|
+
spec.add_development_dependency "rake", "~> 10.0"
|
35
|
+
spec.add_development_dependency "rspec"
|
36
|
+
end
|
metadata
ADDED
@@ -0,0 +1,135 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: risp-lang
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Luca Ongaro
|
8
|
+
autorequire:
|
9
|
+
bindir: exe
|
10
|
+
cert_chain: []
|
11
|
+
date: 2015-08-10 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: rltk
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - "~>"
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '3.0'
|
20
|
+
type: :runtime
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - "~>"
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '3.0'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: hamster
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - "~>"
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '1.0'
|
34
|
+
type: :runtime
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - "~>"
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '1.0'
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: bundler
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - "~>"
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '1.10'
|
48
|
+
type: :development
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - "~>"
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '1.10'
|
55
|
+
- !ruby/object:Gem::Dependency
|
56
|
+
name: rake
|
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
|
+
- !ruby/object:Gem::Dependency
|
70
|
+
name: rspec
|
71
|
+
requirement: !ruby/object:Gem::Requirement
|
72
|
+
requirements:
|
73
|
+
- - ">="
|
74
|
+
- !ruby/object:Gem::Version
|
75
|
+
version: '0'
|
76
|
+
type: :development
|
77
|
+
prerelease: false
|
78
|
+
version_requirements: !ruby/object:Gem::Requirement
|
79
|
+
requirements:
|
80
|
+
- - ">="
|
81
|
+
- !ruby/object:Gem::Version
|
82
|
+
version: '0'
|
83
|
+
description: LISP implementation in Ruby
|
84
|
+
email:
|
85
|
+
- lukeongaro@gmail.com
|
86
|
+
executables:
|
87
|
+
- risp
|
88
|
+
- risp-repl
|
89
|
+
extensions: []
|
90
|
+
extra_rdoc_files: []
|
91
|
+
files:
|
92
|
+
- ".gitignore"
|
93
|
+
- ".rspec"
|
94
|
+
- ".travis.yml"
|
95
|
+
- Gemfile
|
96
|
+
- LICENSE.txt
|
97
|
+
- README.md
|
98
|
+
- Rakefile
|
99
|
+
- bin/console
|
100
|
+
- bin/setup
|
101
|
+
- exe/risp
|
102
|
+
- exe/risp-repl
|
103
|
+
- lib/risp.rb
|
104
|
+
- lib/risp/core.risp
|
105
|
+
- lib/risp/interpreter.rb
|
106
|
+
- lib/risp/lexer.rb
|
107
|
+
- lib/risp/parser.rb
|
108
|
+
- lib/risp/version.rb
|
109
|
+
- risp.gemspec
|
110
|
+
homepage: https://github.com/lucaong/risp
|
111
|
+
licenses:
|
112
|
+
- MIT
|
113
|
+
metadata:
|
114
|
+
allowed_push_host: https://rubygems.org
|
115
|
+
post_install_message:
|
116
|
+
rdoc_options: []
|
117
|
+
require_paths:
|
118
|
+
- lib
|
119
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
120
|
+
requirements:
|
121
|
+
- - ">="
|
122
|
+
- !ruby/object:Gem::Version
|
123
|
+
version: '0'
|
124
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
125
|
+
requirements:
|
126
|
+
- - ">="
|
127
|
+
- !ruby/object:Gem::Version
|
128
|
+
version: '0'
|
129
|
+
requirements: []
|
130
|
+
rubyforge_project:
|
131
|
+
rubygems_version: 2.2.2
|
132
|
+
signing_key:
|
133
|
+
specification_version: 4
|
134
|
+
summary: LISP implementation in Ruby
|
135
|
+
test_files: []
|