schemerald 0.0.8
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/Gemfile +2 -0
- data/LICENSE.txt +19 -0
- data/README.md +71 -0
- data/bin/schemerald +26 -0
- data/lib/schemerald.rb +13 -0
- data/lib/schemerald/Cons.rb +29 -0
- data/lib/schemerald/Environment.rb +35 -0
- data/lib/schemerald/Exceptions/SchemeError.rb +2 -0
- data/lib/schemerald/Interpreter/Interpreter.rb +30 -0
- data/lib/schemerald/Interpreter/defaults.rb +130 -0
- data/lib/schemerald/Interpreter/special_forms.rb +70 -0
- data/lib/schemerald/Lambda.rb +18 -0
- data/lib/schemerald/monkey_patching/Array.rb +5 -0
- data/lib/schemerald/monkey_patching/NilClass.rb +5 -0
- data/lib/schemerald/monkey_patching/Object.rb +17 -0
- data/lib/schemerald/monkey_patching/Symbol.rb +18 -0
- data/schemerald.gemspec +33 -0
- metadata +100 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 60689d7bf14d2adcf692b753264639c347498cb1
|
4
|
+
data.tar.gz: 5964daf8fc56eb6b6ee44686bb31e3f3f0a82e2a
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 1ad657e7e3338f7097856a565515a8328450496c553ab0af300def009a7d30236be7fed49ee3b98d39471caf1d2ae07186c63e62b97d3ac7fe438e335e019d3d
|
7
|
+
data.tar.gz: 75e6dd82c3a11a9ecd620f39186ab5a8a38d552fffceee072142d65947f800d96938857f63c45b0e865e00333fd9848352976ab677dec2fa1079a4959506d152
|
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,19 @@
|
|
1
|
+
Copyright (C) 2014 Ventsislav Velkov ( vntzyv@gmail.com )
|
2
|
+
|
3
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy of
|
4
|
+
this software and associated documentation files (the "Software"), to deal in
|
5
|
+
the Software without restriction, including without limitation the rights to
|
6
|
+
use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
|
7
|
+
of the Software, and to permit persons to whom the Software is furnished to do
|
8
|
+
so, subject to the following conditions:
|
9
|
+
|
10
|
+
The above copyright notice and this permission notice shall be included in all
|
11
|
+
copies or substantial portions of the Software.
|
12
|
+
|
13
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
14
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
15
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
16
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
17
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
18
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
19
|
+
SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,71 @@
|
|
1
|
+
schemerald
|
2
|
+
=======================
|
3
|
+
|
4
|
+
Schemerald is a Scheme R5RS interpreter written in Ruby.
|
5
|
+
|
6
|
+
Features
|
7
|
+
=======================
|
8
|
+
|
9
|
+
Currently implemented R5RS features include:
|
10
|
+
|
11
|
+
* Boolean literals: #t and #f
|
12
|
+
* Numeric literals for integers, reals, rationals and complexes
|
13
|
+
* string literals
|
14
|
+
* Proper and improper lists
|
15
|
+
* Definition and assignment: <tt>define</tt>, <tt>set!</tt>
|
16
|
+
* Lambdas
|
17
|
+
* Conditionals: <tt>if</tt>, <tt>cond</tt>
|
18
|
+
* Binding constructs: <tt>let</tt>
|
19
|
+
* Quoting
|
20
|
+
* Delayed evaluation: <tt>delay</tt> and <tt>force</tt>
|
21
|
+
* Equivalance predicates: <tt>eqv?</tt>, <tt>eq?</tt>, <tt>equal?</tt>
|
22
|
+
* Numeric library: <tt>number?</tt>, <tt>complex?</tt>, <tt>real?</tt>,
|
23
|
+
<tt>rational?</tt>, <tt>integer?</tt>, <tt>=</tt>, <tt><</tt>, <tt>></tt>, <tt><=</tt>, <tt>>=</tt>, <tt>zero?</tt>,
|
24
|
+
<tt>positive?</tt>, <tt>negative?</tt>, <tt>odd?</tt>, <tt>even?</tt>,
|
25
|
+
<tt>max</tt>, <tt>min</tt>, <tt>+</tt>, <tt>*</tt>, <tt>-</tt>, <tt>/</tt>,
|
26
|
+
<tt>abs</tt>, <tt>quotient</tt>, <tt>remainder</tt>, <tt>modulo</tt>, <tt>lcm</tt>,
|
27
|
+
<tt>floor</tt>, <tt>ceiling</tt>, <tt>sqrt</tt>
|
28
|
+
* Boolean library: <tt>and</tt>, <tt>or</tt>, <tt>not</tt>, <tt>boolean?</tt>
|
29
|
+
* List library: <tt>pair?</tt>, <tt>cons</tt>, <tt>car</tt>, <tt>cdr</tt>,
|
30
|
+
<tt>set-car!</tt>, <tt>set-cdr!</tt>, <tt>null?</tt>, <tt>list?</tt>, <tt>list</tt>
|
31
|
+
* Symbol library: <tt>symbol?</tt>, <tt>symbol->string</tt>, <tt>string->symbol</tt>
|
32
|
+
* String library: <tt>string?</tt>, <tt>string-length</tt>, <tt>string-ref</tt>, <tt>string=?</tt>, <tt>string-ci=?</tt>, <tt>string<?</tt>, <tt>string>?</tt>, <tt>string<=?</tt>, <tt>string>=?</tt>, <tt>string-ci<?</tt>, <tt>string-ci>?</tt>, <tt>string-ci<=?</tt>, <tt>string-ci>=?</tt>, <tt>substring</tt>, <tt>string-append</tt>, <tt>string-copy</tt>
|
33
|
+
* Control features: <tt>procedure?</tt>, <tt>apply</tt>, <tt>eval</tt>
|
34
|
+
|
35
|
+
Installation
|
36
|
+
=======================
|
37
|
+
|
38
|
+
<tt> gem install schemerald </tt>
|
39
|
+
|
40
|
+
To start a REPL session:
|
41
|
+
|
42
|
+
<tt> Interpreter.new.repl </tt>
|
43
|
+
|
44
|
+
To exit a REPL session:
|
45
|
+
|
46
|
+
<tt> > exit </tt>
|
47
|
+
|
48
|
+
Licence
|
49
|
+
=======================
|
50
|
+
|
51
|
+
(The MIT License)
|
52
|
+
|
53
|
+
Copyright (C) 2014 Ventsislav Velkov ( vntzyv@gmail.com )
|
54
|
+
|
55
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy of
|
56
|
+
this software and associated documentation files (the "Software"), to deal in
|
57
|
+
the Software without restriction, including without limitation the rights to
|
58
|
+
use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
|
59
|
+
of the Software, and to permit persons to whom the Software is furnished to do
|
60
|
+
so, subject to the following conditions:
|
61
|
+
|
62
|
+
The above copyright notice and this permission notice shall be included in all
|
63
|
+
copies or substantial portions of the Software.
|
64
|
+
|
65
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
66
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
67
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
68
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
69
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
70
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
71
|
+
SOFTWARE.
|
data/bin/schemerald
ADDED
@@ -0,0 +1,26 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
dir = File.expand_path(File.dirname(__FILE__))
|
3
|
+
require dir + '/../lib/schemerald'
|
4
|
+
|
5
|
+
require 'oyster'
|
6
|
+
spec = Oyster.spec do
|
7
|
+
name "schemerald -- Ruby embedded Scheme interpreter, v.0.0.5"
|
8
|
+
author 'Ventsislav Velkov <vntzyv@gmail.com>'
|
9
|
+
|
10
|
+
synopsis <<-EOS
|
11
|
+
schemerald -i [options]
|
12
|
+
EOS
|
13
|
+
|
14
|
+
flag :interactive, :default => true,
|
15
|
+
:desc => 'Start an interactive Scheme session'
|
16
|
+
end
|
17
|
+
|
18
|
+
begin
|
19
|
+
options = spec.parse
|
20
|
+
|
21
|
+
if options[:interactive]
|
22
|
+
Interpreter.new.repl
|
23
|
+
end
|
24
|
+
|
25
|
+
rescue Oyster::HelpRendered
|
26
|
+
end
|
data/lib/schemerald.rb
ADDED
@@ -0,0 +1,13 @@
|
|
1
|
+
require 'sxp'
|
2
|
+
require 'json'
|
3
|
+
require 'schemerald/Cons'
|
4
|
+
require 'schemerald/Environment'
|
5
|
+
require 'schemerald/Lambda'
|
6
|
+
require 'schemerald/monkey_patching/Array'
|
7
|
+
require 'schemerald/monkey_patching/NilClass'
|
8
|
+
require 'schemerald/monkey_patching/Object'
|
9
|
+
require 'schemerald/monkey_patching/Symbol'
|
10
|
+
require 'schemerald/Interpreter/Interpreter'
|
11
|
+
require 'schemerald/Interpreter/defaults'
|
12
|
+
require 'schemerald/Interpreter/special_forms'
|
13
|
+
require 'schemerald/Exceptions/SchemeError'
|
@@ -0,0 +1,29 @@
|
|
1
|
+
class Cons
|
2
|
+
attr_reader :car, :cdr
|
3
|
+
|
4
|
+
def initialize(car, cdr)
|
5
|
+
@car = car
|
6
|
+
@cdr = cdr
|
7
|
+
end
|
8
|
+
|
9
|
+
def arrayify
|
10
|
+
return self unless list?
|
11
|
+
return [car] + cdr.arrayify
|
12
|
+
end
|
13
|
+
|
14
|
+
def list?
|
15
|
+
cdr.list?
|
16
|
+
end
|
17
|
+
|
18
|
+
def scheme_eval(environment, forms)
|
19
|
+
return forms.get_value(car).
|
20
|
+
call(environment, forms, *cdr.arrayify) if forms.defined?(car)
|
21
|
+
return car.scheme_eval(environment, forms).
|
22
|
+
call(*cdr.arrayify.map{|x| x.scheme_eval(environment, forms)})
|
23
|
+
end
|
24
|
+
|
25
|
+
def to_sxp
|
26
|
+
return "(#{car.to_sxp} . #{cdr.to_sxp})" unless list?
|
27
|
+
return "(#{arrayify.map{|x| x.to_sxp}.join(' ')})"
|
28
|
+
end
|
29
|
+
end
|
@@ -0,0 +1,35 @@
|
|
1
|
+
class Environment
|
2
|
+
def initialize(parent = nil, defaults = {})
|
3
|
+
@parent = parent
|
4
|
+
@defaults = defaults
|
5
|
+
end
|
6
|
+
|
7
|
+
def define(symbol, value)
|
8
|
+
@defaults[symbol] = value
|
9
|
+
return value
|
10
|
+
end
|
11
|
+
|
12
|
+
def defined?(symbol)
|
13
|
+
return true if @defaults.has_key?(symbol)
|
14
|
+
return false if @parent.nil?
|
15
|
+
return @parent.defined?(symbol)
|
16
|
+
end
|
17
|
+
|
18
|
+
def set_value(symbol, value)
|
19
|
+
if @defaults.has_key?(symbol)
|
20
|
+
@defaults[symbol] = value
|
21
|
+
elsif @parent.nil?
|
22
|
+
raise SchemeError,
|
23
|
+
"assignment disallowed;\ncannot set undefined\nvariable: #{symbol}"
|
24
|
+
else
|
25
|
+
@parent.set_value(symbol, value)
|
26
|
+
end
|
27
|
+
end
|
28
|
+
|
29
|
+
def get_value(symbol)
|
30
|
+
return @defaults[symbol] if @defaults.has_key?(symbol)
|
31
|
+
raise SchemeError,
|
32
|
+
"#{symbol}: undefined;\nCannot reference undefined identifier" if @parent.nil?
|
33
|
+
return @parent.get_value(symbol)
|
34
|
+
end
|
35
|
+
end
|
@@ -0,0 +1,30 @@
|
|
1
|
+
class Interpreter
|
2
|
+
def initialize
|
3
|
+
@environment = Environment.new(nil, DEFAULTS)
|
4
|
+
@special_forms = Environment.new(nil, FORMS)
|
5
|
+
end
|
6
|
+
|
7
|
+
def evaluate(string)
|
8
|
+
SXP::Reader::Scheme.read("(#{string})").
|
9
|
+
map{|x| x.consify.scheme_eval(@environment, @special_forms) }
|
10
|
+
end
|
11
|
+
|
12
|
+
def scheme_print(output)
|
13
|
+
output.each{|exp| puts exp.to_sxp }
|
14
|
+
end
|
15
|
+
|
16
|
+
def repl
|
17
|
+
print "> "
|
18
|
+
STDIN.each_line do |line|
|
19
|
+
begin
|
20
|
+
return if line.start_with? "exit"
|
21
|
+
scheme_print(evaluate(line))
|
22
|
+
rescue SchemeError => e
|
23
|
+
puts "SchemeError: #{e}"
|
24
|
+
rescue StandardError => e
|
25
|
+
puts "ERROR: #{e}"
|
26
|
+
end
|
27
|
+
print "> "
|
28
|
+
end
|
29
|
+
end
|
30
|
+
end
|
@@ -0,0 +1,130 @@
|
|
1
|
+
class Interpreter
|
2
|
+
DEFAULTS = {
|
3
|
+
#Boolean library:
|
4
|
+
:and => lambda {|*args| args != [] ? eval(args.join(' and ')) : true },
|
5
|
+
:or => lambda {|*args| args != [] ? eval(args.join(' or ')) : false },
|
6
|
+
:not => lambda {|x| not x },
|
7
|
+
:boolean? => lambda {|x| x == true or x == false },
|
8
|
+
#Numeric library:
|
9
|
+
:+ => lambda {|*args| args.reduce(:+) },
|
10
|
+
:- => lambda {|*args| args.reduce(:-) },
|
11
|
+
:* => lambda {|*args| args.reduce(:*) },
|
12
|
+
:/ => lambda {|*args|
|
13
|
+
if args.reduce(:/) == args.reduce(:fdiv) or args.reduce(:/).class == Float
|
14
|
+
args.reduce(:/)
|
15
|
+
else
|
16
|
+
args.map{|x| x.to_r }.reduce(:/)
|
17
|
+
end
|
18
|
+
},
|
19
|
+
:quotient => lambda {|x, y| x / y },
|
20
|
+
:remainder => lambda {|x, y| x.remainder(y) },
|
21
|
+
:abs => lambda {|x| x.abs },
|
22
|
+
:positive? => lambda {|x| x > 0 },
|
23
|
+
:negative? => lambda {|x| x < 0 },
|
24
|
+
:zero? => lambda {|x| x == 0 },
|
25
|
+
:odd? => lambda {|x| x.odd? },
|
26
|
+
:even? => lambda {|x| x.even? },
|
27
|
+
:max => lambda {|*args| args.max },
|
28
|
+
:min => lambda {|*args| args.min },
|
29
|
+
:modulo => lambda {|x, y| x.modulo(y) },
|
30
|
+
:number? => lambda {|x| x.is_a? Numeric },
|
31
|
+
:complex? => lambda {|x| x.is_a? Complex },
|
32
|
+
:real? => lambda {|x| x.is_a? Float },
|
33
|
+
:rational? => lambda {|x| x.is_a? Rational },
|
34
|
+
:integer? => lambda {|x| x.is_a? Integer },
|
35
|
+
:"=" => lambda {|*args| args.all?{|x| x == args.first }},
|
36
|
+
:> => lambda {|*args| args.each_with_object([1]){|i, a| a << (a.last > i) << i }.
|
37
|
+
drop(2).all? },
|
38
|
+
:< => lambda {|*args| args.each_with_object([1]){|i, a| a << (a.last < i) << i }.
|
39
|
+
drop(2).all? },
|
40
|
+
:>= => lambda {|*args| args.each_with_object([1]){|i, a| a << (a.last >= i) << i }.
|
41
|
+
drop(2).all? },
|
42
|
+
:<= => lambda {|*args| args.each_with_object([1]){|i, a| a << (a.last <= i) << i }.
|
43
|
+
drop(2).all? },
|
44
|
+
#Equivalance predicates:
|
45
|
+
:eq? => lambda {|x, y| x.eql? y },
|
46
|
+
:eqv? => lambda {|x, y| x == y },
|
47
|
+
:equal? => lambda {|x, y| x.eql? y },
|
48
|
+
#List library:
|
49
|
+
:car => lambda {|x| x.car },
|
50
|
+
:cdr => lambda {|x| x.cdr },
|
51
|
+
:cons => lambda {|x, y| Cons.new(x, y) },
|
52
|
+
:list => lambda {|*args| args.consify },
|
53
|
+
:pair? => lambda {|x| x.is_a?(Cons) ? true : true },
|
54
|
+
:null? => lambda {|x| x == :nil },
|
55
|
+
:list? => lambda {|x| x.list? },
|
56
|
+
#Symbol library:
|
57
|
+
:symbol? => lambda {|x| x.is_a? Symbol and x != :nil },
|
58
|
+
:"symbol->string" => lambda {|x| DEFAULTS[:symbol?].
|
59
|
+
call(x) ? x.to_s : raise(SchemeError, "#{x} isn't a symbol") },
|
60
|
+
:"string->symbol" => lambda {|x|
|
61
|
+
x.is_a?(String) ? x.to_sym : raise(SchemeError, "#{x} isn't a string") },
|
62
|
+
#String library:
|
63
|
+
:string? => lambda {|x| x.is_a? String },
|
64
|
+
:"string-length" => lambda {|x|
|
65
|
+
x.is_a?(String) ? x.size : raise(SchemeError, "#{x} isn't a string") },
|
66
|
+
:"string-ref" => lambda {|str, k|
|
67
|
+
raise(SchemeError, "#{str} isn't a string") unless str.is_a? String
|
68
|
+
raise(SchemeError,
|
69
|
+
"#{k} isn't an Integer or in the range [0, #{str.size.pred}]") unless
|
70
|
+
k.is_a? Integer and k >= 0 and k < str.size
|
71
|
+
str[k]
|
72
|
+
},
|
73
|
+
:"string=?" => lambda {|x, y, *args|
|
74
|
+
raise(SchemeError, "all arguments must be strings") unless
|
75
|
+
([x, y] + args).all? {|i| i.is_a? String }
|
76
|
+
([x, y] + args).all? {|i| i == x }
|
77
|
+
},
|
78
|
+
:"string-ci=?" => lambda {|*args| DEFAULTS[:"string=?"].
|
79
|
+
call(*args.map {|x| x.downcase }) },
|
80
|
+
:"string<?" => lambda {|x, y, *args|
|
81
|
+
raise(SchemeError, "all arguments must be strings") unless
|
82
|
+
([x, y] + args).all? {|i| i.is_a? String }
|
83
|
+
([x, y] + args).each_with_object([""]) {|i, a| a << (a.last < i) << i }.
|
84
|
+
drop(2).all?
|
85
|
+
},
|
86
|
+
:"string>?" => lambda {|x, y, *args|
|
87
|
+
raise(SchemeError, "all arguments must be strings") unless
|
88
|
+
([x, y] + args).all? {|i| i.is_a? String }
|
89
|
+
([x, y] + args).each_with_object([""]) {|i, a| a << (a.last > i) << i }.
|
90
|
+
drop(2).all?
|
91
|
+
},
|
92
|
+
:"string<=?" => lambda {|x, y, *args|
|
93
|
+
raise(SchemeError, "all arguments must be strings") unless
|
94
|
+
([x, y] + args).all? {|i| i.is_a? String }
|
95
|
+
([x, y] + args).each_with_object([""]) {|i, a| a << (a.last <= i) << i }.
|
96
|
+
drop(2).all?
|
97
|
+
},
|
98
|
+
:"string>=?" => lambda {|x, y, *args|
|
99
|
+
raise(SchemeError, "all arguments must be strings") unless
|
100
|
+
([x, y] + args).all? {|i| i.is_a? String }
|
101
|
+
([x, y] + args).each_with_object([""]) {|i, a| a << (a.last < i) << i }.
|
102
|
+
drop(2).all?
|
103
|
+
},
|
104
|
+
:"string-ci<?" => lambda {|x, y, *args| DEFAULTS[:"string<?"].
|
105
|
+
call(*args.map {|x| x.downcase }) },
|
106
|
+
:"string-ci>?" => lambda {|x, y, *args| DEFAULTS[:"string>?"].
|
107
|
+
call(*args.map {|x| x.downcase }) },
|
108
|
+
:"string-ci<=?" => lambda {|x, y, *args| DEFAULTS[:"string<=?"].
|
109
|
+
call(*args.map {|x| x.downcase }) },
|
110
|
+
:"string-ci>=?" => lambda {|x, y, *args| DEFAULTS[:"string>=?"].
|
111
|
+
call(*args.map {|x| x.downcase }) },
|
112
|
+
:substring => lambda {|string, start, ending|
|
113
|
+
raise(SchemeError, "#{string} isn't a string") unless string.is_a? String
|
114
|
+
raise(SchemeError, "ending index is smaller than starting index") if start > ending
|
115
|
+
raise(SchemeError, "starting index must be in range [0, #{string.size}]") unless
|
116
|
+
0.upto(string.size).include? start
|
117
|
+
raise(SchemeError, "ending index must be in range [0, #{string.size}]") unless
|
118
|
+
0.upto(string.size).include? ending
|
119
|
+
string[start..ending.pred]
|
120
|
+
},
|
121
|
+
:"string-append" => lambda {|*args|
|
122
|
+
raise(SchemeError, "all arguments must be strings") unless
|
123
|
+
args.all? {|i| i.is_a? String } or args.empty?
|
124
|
+
args.reduce("", :+)
|
125
|
+
},
|
126
|
+
:"string-copy" => lambda {|string| String.new(string) },
|
127
|
+
#Control features:
|
128
|
+
:procedure? => lambda {|x| x.is_a? Proc or x.is_a? Lambda },
|
129
|
+
}
|
130
|
+
end
|
@@ -0,0 +1,70 @@
|
|
1
|
+
class Interpreter
|
2
|
+
FORMS = {
|
3
|
+
:define => lambda {|env, forms, params, value|
|
4
|
+
if params.list?
|
5
|
+
env.define(params.car, Lambda.new(env, forms, params.cdr, value))
|
6
|
+
else
|
7
|
+
env.define(params, value.scheme_eval(env, forms))
|
8
|
+
end
|
9
|
+
},
|
10
|
+
:set! => lambda {|env, forms, name, value|
|
11
|
+
env.set_value(name, value.scheme_eval(env, forms))
|
12
|
+
},
|
13
|
+
:"set-car!" => lambda {|env, forms, list, obj|
|
14
|
+
env.set_value(list, Cons.new(obj.scheme_eval(env, forms), env.get_value(list).cdr))
|
15
|
+
},
|
16
|
+
:"set-cdr!" => lambda {|env, forms, list, obj|
|
17
|
+
env.set_value(list, Cons.new(env.get_value(list).car, obj.scheme_eval(env, forms)))
|
18
|
+
},
|
19
|
+
:quote => lambda {|env, forms, exp| exp },
|
20
|
+
:if => lambda {|env, forms, if_clause, then_clause, else_clause|
|
21
|
+
if if_clause.scheme_eval(env, forms) != false
|
22
|
+
then_clause.scheme_eval(env, forms)
|
23
|
+
else
|
24
|
+
else_clause.scheme_eval(env, forms)
|
25
|
+
end
|
26
|
+
},
|
27
|
+
:cond => lambda {|env, forms, *clauses|
|
28
|
+
found_clause = clauses.find {|x| x.car == :else or x.car.scheme_eval(env, forms) }
|
29
|
+
if found_clause
|
30
|
+
found_clause.cdr.arrayify.map{|x| x.scheme_eval(env, forms) }.last or
|
31
|
+
found_clause.car.scheme_eval(env, forms)
|
32
|
+
else
|
33
|
+
nil
|
34
|
+
end
|
35
|
+
},
|
36
|
+
:lambda => lambda {|env, forms, params, *code|
|
37
|
+
Lambda.new(env, forms, params, *code)
|
38
|
+
},
|
39
|
+
:apply => lambda {|env, forms, func, list|
|
40
|
+
Cons.new(func, list.scheme_eval(env, forms)).scheme_eval(env, forms)
|
41
|
+
},
|
42
|
+
:let => lambda {|env, forms, bindings, body|
|
43
|
+
params, values = [], []
|
44
|
+
while bindings != :nil do
|
45
|
+
params << bindings.car.car
|
46
|
+
values << bindings.car.cdr.car
|
47
|
+
bindings = bindings.cdr
|
48
|
+
end
|
49
|
+
Lambda.new(env, forms, params, body).
|
50
|
+
call(*values.map{|value| value.scheme_eval(env, forms) })
|
51
|
+
},
|
52
|
+
:eval => lambda {|env, forms, *code|
|
53
|
+
code.map{|c| c.scheme_eval(env, forms)}.map{|c| c.scheme_eval(env, forms) }.last
|
54
|
+
},
|
55
|
+
:"define-syntax" => lambda {|env, forms, name, exp|
|
56
|
+
func = exp.scheme_eval(env, forms)
|
57
|
+
forms.define(name, lambda{|env2, forms2, *rest|
|
58
|
+
func.call(*rest).scheme_eval(env, forms) })
|
59
|
+
name
|
60
|
+
},
|
61
|
+
:"let-syntax" => lambda{|env, forms, binding, body|
|
62
|
+
name = binding.car
|
63
|
+
func = binding.cdr.car.scheme_eval(env, forms)
|
64
|
+
newforms = Environment.new(forms)
|
65
|
+
newforms.define(name, lambda{|env2, forms2, *rest| func.call(*rest).
|
66
|
+
scheme_eval(env, forms)})
|
67
|
+
body.scheme_eval(env, newforms)
|
68
|
+
},
|
69
|
+
}
|
70
|
+
end
|
@@ -0,0 +1,18 @@
|
|
1
|
+
class Lambda
|
2
|
+
def initialize(env, forms, params, *code)
|
3
|
+
@environment = env
|
4
|
+
@forms = forms
|
5
|
+
@parameters = params.arrayify
|
6
|
+
@code = code
|
7
|
+
end
|
8
|
+
|
9
|
+
def call(*args)
|
10
|
+
raise SchemeError, "arity mismatch: expected #{@parameters.size} arguments" unless
|
11
|
+
args.size == @parameters.size
|
12
|
+
new_env = Environment.new(@environment)
|
13
|
+
@parameters.zip(args).each do |name, value|
|
14
|
+
new_env.define(name, value)
|
15
|
+
end
|
16
|
+
@code.map{|x| x.scheme_eval(new_env, @forms) }.last
|
17
|
+
end
|
18
|
+
end
|
data/schemerald.gemspec
ADDED
@@ -0,0 +1,33 @@
|
|
1
|
+
Gem::Specification.new do |s|
|
2
|
+
s.name = 'schemerald'
|
3
|
+
s.version = '0.0.8'
|
4
|
+
s.summary = "(scheme? yes no)"
|
5
|
+
s.description = "A Scheme interpreter in Ruby"
|
6
|
+
s.authors = ["Ventsislav Velkov"]
|
7
|
+
s.email = 'vntzyv@gmail.com'
|
8
|
+
s.files = [
|
9
|
+
"README.md",
|
10
|
+
"LICENSE.txt",
|
11
|
+
"Gemfile",
|
12
|
+
"bin/schemerald",
|
13
|
+
"lib/schemerald.rb",
|
14
|
+
"lib/schemerald/Cons.rb",
|
15
|
+
"lib/schemerald/Environment.rb",
|
16
|
+
"lib/schemerald/Lambda.rb",
|
17
|
+
"lib/schemerald/Interpreter/Interpreter.rb",
|
18
|
+
"lib/schemerald/Interpreter/defaults.rb",
|
19
|
+
"lib/schemerald/Interpreter/special_forms.rb",
|
20
|
+
"lib/schemerald/Exceptions/SchemeError.rb",
|
21
|
+
"lib/schemerald/monkey_patching/Object.rb",
|
22
|
+
"lib/schemerald/monkey_patching/Array.rb",
|
23
|
+
"lib/schemerald/monkey_patching/Object.rb",
|
24
|
+
"lib/schemerald/monkey_patching/Symbol.rb",
|
25
|
+
"lib/schemerald/monkey_patching/NilClass.rb",
|
26
|
+
"schemerald.gemspec"
|
27
|
+
]
|
28
|
+
s.homepage = "https://github.com/vntzy/schemerald"
|
29
|
+
s.license = 'MIT'
|
30
|
+
|
31
|
+
s.add_runtime_dependency 'sxp', '~> 0.1', '>= 0.1.5'
|
32
|
+
s.add_runtime_dependency "oyster", '~> 0.9', '>= 0.9.5'
|
33
|
+
end
|
metadata
ADDED
@@ -0,0 +1,100 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: schemerald
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.8
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Ventsislav Velkov
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2014-08-30 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: sxp
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - "~>"
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '0.1'
|
20
|
+
- - ">="
|
21
|
+
- !ruby/object:Gem::Version
|
22
|
+
version: 0.1.5
|
23
|
+
type: :runtime
|
24
|
+
prerelease: false
|
25
|
+
version_requirements: !ruby/object:Gem::Requirement
|
26
|
+
requirements:
|
27
|
+
- - "~>"
|
28
|
+
- !ruby/object:Gem::Version
|
29
|
+
version: '0.1'
|
30
|
+
- - ">="
|
31
|
+
- !ruby/object:Gem::Version
|
32
|
+
version: 0.1.5
|
33
|
+
- !ruby/object:Gem::Dependency
|
34
|
+
name: oyster
|
35
|
+
requirement: !ruby/object:Gem::Requirement
|
36
|
+
requirements:
|
37
|
+
- - "~>"
|
38
|
+
- !ruby/object:Gem::Version
|
39
|
+
version: '0.9'
|
40
|
+
- - ">="
|
41
|
+
- !ruby/object:Gem::Version
|
42
|
+
version: 0.9.5
|
43
|
+
type: :runtime
|
44
|
+
prerelease: false
|
45
|
+
version_requirements: !ruby/object:Gem::Requirement
|
46
|
+
requirements:
|
47
|
+
- - "~>"
|
48
|
+
- !ruby/object:Gem::Version
|
49
|
+
version: '0.9'
|
50
|
+
- - ">="
|
51
|
+
- !ruby/object:Gem::Version
|
52
|
+
version: 0.9.5
|
53
|
+
description: A Scheme interpreter in Ruby
|
54
|
+
email: vntzyv@gmail.com
|
55
|
+
executables: []
|
56
|
+
extensions: []
|
57
|
+
extra_rdoc_files: []
|
58
|
+
files:
|
59
|
+
- Gemfile
|
60
|
+
- LICENSE.txt
|
61
|
+
- README.md
|
62
|
+
- bin/schemerald
|
63
|
+
- lib/schemerald.rb
|
64
|
+
- lib/schemerald/Cons.rb
|
65
|
+
- lib/schemerald/Environment.rb
|
66
|
+
- lib/schemerald/Exceptions/SchemeError.rb
|
67
|
+
- lib/schemerald/Interpreter/Interpreter.rb
|
68
|
+
- lib/schemerald/Interpreter/defaults.rb
|
69
|
+
- lib/schemerald/Interpreter/special_forms.rb
|
70
|
+
- lib/schemerald/Lambda.rb
|
71
|
+
- lib/schemerald/monkey_patching/Array.rb
|
72
|
+
- lib/schemerald/monkey_patching/NilClass.rb
|
73
|
+
- lib/schemerald/monkey_patching/Object.rb
|
74
|
+
- lib/schemerald/monkey_patching/Symbol.rb
|
75
|
+
- schemerald.gemspec
|
76
|
+
homepage: https://github.com/vntzy/schemerald
|
77
|
+
licenses:
|
78
|
+
- MIT
|
79
|
+
metadata: {}
|
80
|
+
post_install_message:
|
81
|
+
rdoc_options: []
|
82
|
+
require_paths:
|
83
|
+
- lib
|
84
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
85
|
+
requirements:
|
86
|
+
- - ">="
|
87
|
+
- !ruby/object:Gem::Version
|
88
|
+
version: '0'
|
89
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
90
|
+
requirements:
|
91
|
+
- - ">="
|
92
|
+
- !ruby/object:Gem::Version
|
93
|
+
version: '0'
|
94
|
+
requirements: []
|
95
|
+
rubyforge_project:
|
96
|
+
rubygems_version: 2.2.2
|
97
|
+
signing_key:
|
98
|
+
specification_version: 4
|
99
|
+
summary: "(scheme? yes no)"
|
100
|
+
test_files: []
|