lisp 1.0.6 → 1.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 +4 -4
- data/README.md +2 -2
- data/lib/lisp.rb +6 -0
- data/lib/lisp/repl.rb +1 -5
- data/lib/lisp/version.rb +1 -1
- data/test/test_lisp.rb +8 -0
- metadata +1 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: cd79b300ba14a7d6c45496292f33a72767c9f839
|
4
|
+
data.tar.gz: a7500b1c04cdc5262d899dee558b6a68648cb141
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 43b51dfb0eb3ace2920704f9bf54cf4690889a79bf38edca8e06cc703e453fddc3dd6055e45f8965e6a4d1410c8883ae9c2472624ed1e79d37e5157c9441f067
|
7
|
+
data.tar.gz: e67b94f6412a29046f759f1666d8fcefef4c5a4f07439f4ff2cfba076cfe04b63d4d14e5ee5ecb581fcd184e79a0684c2dc1a7c0cb963daff398a8d05ca9e3ea
|
data/README.md
CHANGED
@@ -47,6 +47,6 @@ Features
|
|
47
47
|
|
48
48
|
- [ ] __quotation__ - (quote exp) Return the exp literally; do not evaluate it. _Example: (quote (a b c)) ⇒ (a b c)_
|
49
49
|
|
50
|
-
- [
|
50
|
+
- [x] __assignment__ - (set! var exp) Evaluate exp and assign that value to var, which must have been previously defined (with a define or as a parameter to an enclosing procedure). _Example: (set! x2 (* x x))_
|
51
51
|
|
52
|
-
- [
|
52
|
+
- [x] __sequencing__ - (begin exp...) Evaluate each of the expressions in left-to-right order, and return the final value. _Example: (begin (set! x 1) (set! x (+ x 1)) (* x 2)) ⇒ 4_
|
data/lib/lisp.rb
CHANGED
@@ -52,6 +52,12 @@ module Lisp
|
|
52
52
|
_, test, conseq, alt = exp
|
53
53
|
exp = execute(test, scope) ? conseq : alt
|
54
54
|
execute(exp, scope)
|
55
|
+
when :set!
|
56
|
+
_, var, exp = exp
|
57
|
+
if scope.has_key?(var) then scope[var] = execute(exp, scope) else raise "#{var} is undefined" end
|
58
|
+
when :begin
|
59
|
+
_, *exp = exp
|
60
|
+
exp.map { |exp| execute(exp) }.last
|
55
61
|
else
|
56
62
|
func, *args = exp.map { |exp| execute(exp, scope) }
|
57
63
|
func.call(*args)
|
data/lib/lisp/repl.rb
CHANGED
@@ -8,17 +8,13 @@ module Lisp
|
|
8
8
|
catch(:exit) do
|
9
9
|
loop do
|
10
10
|
puts begin
|
11
|
-
eval
|
11
|
+
eval Coolline.new.readline
|
12
12
|
rescue Exception => e
|
13
13
|
e.message
|
14
14
|
end
|
15
15
|
end
|
16
16
|
end
|
17
17
|
end
|
18
|
-
|
19
|
-
def self.input
|
20
|
-
Coolline.new
|
21
|
-
end
|
22
18
|
end
|
23
19
|
|
24
20
|
if __FILE__ == $0
|
data/lib/lisp/version.rb
CHANGED
data/test/test_lisp.rb
CHANGED
@@ -48,6 +48,14 @@ class TestLisp < MiniTest::Unit::TestCase
|
|
48
48
|
assert_equal 1, Lisp.eval("(if(!= 1 2) 1 2)")
|
49
49
|
end
|
50
50
|
|
51
|
+
def test_set!
|
52
|
+
assert_raises(RuntimeError) { Lisp.eval("(set! foo 0)") }
|
53
|
+
end
|
54
|
+
|
55
|
+
def test_begin
|
56
|
+
assert_equal 4, Lisp.eval("(begin (define x 1) (set! x (+ x 1)) (* x 2))")
|
57
|
+
end
|
58
|
+
|
51
59
|
def test_lambda
|
52
60
|
Lisp.eval("(define area (lambda (r) (* 3.141592653 (* r r))))")
|
53
61
|
assert_equal 28.274333877, Lisp.eval("(area 3)")
|