lisp 1.0.1 → 1.0.2
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/lib/lisp.rb +20 -45
- data/lib/lisp/repl.rb +26 -0
- data/lib/lisp/version.rb +1 -1
- data/lisp.gemspec +1 -0
- data/test/test_lisp.rb +0 -8
- metadata +17 -3
- data/lib/lisp/scope.rb +0 -14
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: f7f4f9a7fe02524ebd2dc9df1d606df0285550ad
|
4
|
+
data.tar.gz: 6d2cc99951c8c70482840e55735c4f5c7510d4b4
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: c015b086967313e762108778826e94e1d4c4d798904486ad9a4be28759120bb82466c9edd0bf1acdca9bbbcfa5d2a1eaea0abb18a7d962180575823243b319e3
|
7
|
+
data.tar.gz: cfc010cb803a93068a14c1b5418c1cce5b36833df0d05011bd5b2cd8fcb58f8cbfeee6b55bb549e2201a5c9ddc0d8b538de0b3fde97d7ee92e2db56676bec3c7
|
data/lib/lisp.rb
CHANGED
@@ -1,24 +1,8 @@
|
|
1
|
-
#!/usr/bin/env ruby
|
2
|
-
|
3
1
|
require "bundler/setup"
|
4
2
|
require "lisp/version"
|
5
|
-
require "lisp/
|
3
|
+
require "lisp/repl"
|
6
4
|
|
7
5
|
module Lisp
|
8
|
-
def self.repl
|
9
|
-
puts "ctrl-c to exit"
|
10
|
-
catch(:exit) do
|
11
|
-
loop do
|
12
|
-
print "> "
|
13
|
-
puts begin
|
14
|
-
eval gets
|
15
|
-
rescue Exception => e
|
16
|
-
e.message
|
17
|
-
end
|
18
|
-
end
|
19
|
-
end
|
20
|
-
end
|
21
|
-
|
22
6
|
def self.eval(string)
|
23
7
|
execute(parse(tokenize(string)))
|
24
8
|
end
|
@@ -54,41 +38,32 @@ module Lisp
|
|
54
38
|
end
|
55
39
|
|
56
40
|
def self.execute(exp, scope = global)
|
57
|
-
|
58
|
-
|
59
|
-
|
60
|
-
|
61
|
-
|
62
|
-
|
63
|
-
|
64
|
-
|
65
|
-
|
66
|
-
|
67
|
-
|
68
|
-
|
69
|
-
|
70
|
-
|
71
|
-
func, *args = exp.map { |exp| execute(exp, scope) }
|
72
|
-
func.call(*args)
|
73
|
-
end
|
74
|
-
when Symbol
|
75
|
-
scope[exp]
|
41
|
+
return scope[exp] if exp.is_a? Symbol
|
42
|
+
return exp unless exp.is_a? Array
|
43
|
+
|
44
|
+
case exp[0]
|
45
|
+
when :define
|
46
|
+
_, var, exp = exp
|
47
|
+
scope[var] = execute(exp, scope)
|
48
|
+
when :lambda
|
49
|
+
_, params, exp = exp
|
50
|
+
lambda { |*args| execute(exp, scope.merge(Hash[params.zip(args)])) }
|
51
|
+
when :if
|
52
|
+
_, test, conseq, alt = exp
|
53
|
+
exp = execute(test, scope) ? conseq : alt
|
54
|
+
execute(exp, scope)
|
76
55
|
else
|
77
|
-
exp
|
56
|
+
func, *args = exp.map { |exp| execute(exp, scope) }
|
57
|
+
func.call(*args)
|
78
58
|
end
|
79
59
|
end
|
80
60
|
|
81
61
|
def self.global
|
82
62
|
@scope ||= begin
|
83
|
-
|
84
|
-
|
85
|
-
scope.merge(
|
63
|
+
operators = [:==, :"!=", :"<", :"<=", :">", :">=", :+, :-, :*, :/]
|
64
|
+
operators.inject({}) do |scope, operator|
|
65
|
+
scope.merge(operator => lambda { |*args| args.inject(&operator) })
|
86
66
|
end
|
87
67
|
end
|
88
68
|
end
|
89
69
|
end
|
90
|
-
|
91
|
-
if __FILE__ == $0
|
92
|
-
trap("SIGINT") { throw :exit }
|
93
|
-
Lisp.repl
|
94
|
-
end
|
data/lib/lisp/repl.rb
ADDED
@@ -0,0 +1,26 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
require "coolline"
|
3
|
+
|
4
|
+
module Lisp
|
5
|
+
def self.repl
|
6
|
+
trap("SIGINT") { throw :exit }
|
7
|
+
puts "ctrl-c to exit"
|
8
|
+
catch(:exit) do
|
9
|
+
loop do
|
10
|
+
puts begin
|
11
|
+
eval input.readline
|
12
|
+
rescue Exception => e
|
13
|
+
e.message
|
14
|
+
end
|
15
|
+
end
|
16
|
+
end
|
17
|
+
end
|
18
|
+
|
19
|
+
def self.input
|
20
|
+
Coolline.new
|
21
|
+
end
|
22
|
+
end
|
23
|
+
|
24
|
+
if __FILE__ == $0
|
25
|
+
Lisp.repl
|
26
|
+
end
|
data/lib/lisp/version.rb
CHANGED
data/lisp.gemspec
CHANGED
@@ -18,6 +18,7 @@ Gem::Specification.new do |spec|
|
|
18
18
|
spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
|
19
19
|
spec.require_paths = ["lib"]
|
20
20
|
|
21
|
+
spec.add_dependency "coolline"
|
21
22
|
spec.add_development_dependency "bundler", "~> 1.6"
|
22
23
|
spec.add_development_dependency "rake"
|
23
24
|
spec.add_development_dependency "pry"
|
data/test/test_lisp.rb
CHANGED
@@ -54,12 +54,4 @@ class TestLisp < MiniTest::Unit::TestCase
|
|
54
54
|
Lisp.eval("(define fact (lambda (n) (if (<= n 1) 1 (* n (fact (- n 1))))))")
|
55
55
|
assert_equal 3628800, Lisp.eval("(fact 10)")
|
56
56
|
end
|
57
|
-
|
58
|
-
def test_repl
|
59
|
-
assert_output "ctrl-c to exit\n> " do
|
60
|
-
thread = Thread.new { Lisp.repl }
|
61
|
-
sleep 1.0/10.0
|
62
|
-
thread.terminate
|
63
|
-
end
|
64
|
-
end
|
65
57
|
end
|
metadata
CHANGED
@@ -1,15 +1,29 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: lisp
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 1.0.
|
4
|
+
version: 1.0.2
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- James Moriarty
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2014-06-
|
11
|
+
date: 2014-06-20 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: coolline
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - ">="
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '0'
|
20
|
+
type: :runtime
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - ">="
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '0'
|
13
27
|
- !ruby/object:Gem::Dependency
|
14
28
|
name: bundler
|
15
29
|
requirement: !ruby/object:Gem::Requirement
|
@@ -65,7 +79,7 @@ files:
|
|
65
79
|
- README.md
|
66
80
|
- Rakefile
|
67
81
|
- lib/lisp.rb
|
68
|
-
- lib/lisp/
|
82
|
+
- lib/lisp/repl.rb
|
69
83
|
- lib/lisp/version.rb
|
70
84
|
- lisp.gemspec
|
71
85
|
- test/test_lisp.rb
|
data/lib/lisp/scope.rb
DELETED