public
Description: a Scheme written in Ruby, but implemented on the bus!
Homepage: http://bus-scheme.rubyforge.org
Clone URL: git://github.com/technomancy/bus-scheme.git
help lambdas work with list args better
technomancy (author)
Fri May 30 18:11:18 -0700 2008
commit  ce3651d24fac11d9e5f48c877049fc9e6d75e654
tree    bc9bba7c1b6d8f71bb4bddff1302544425beaade
parent  aa288decc927509e7594dcdf63a5b5a07cb6c165
...
79
80
81
82
83
 
 
84
85
86
...
79
80
81
 
 
82
83
84
85
86
0
@@ -79,8 +79,8 @@ module BusScheme
0
     (@loaded_files ||= ["(eval)"])
0
   end
0
 
0
-# ['core.scm', 'test.scm', 'list.scm', 'predicates.scm'
0
-# ].each { |f| load(f) }
0
+ ['core.scm', 'test.scm', 'list.scm', 'predicates.scm'
0
+ ].each { |f| load(f) }
0
 end
0
 
0
 begin
...
10
11
12
13
14
 
 
15
16
17
...
22
23
24
25
 
26
27
28
...
10
11
12
 
 
13
14
15
16
17
...
22
23
24
 
25
26
27
28
0
@@ -10,8 +10,8 @@ module BusScheme
0
 
0
   # Eval a form passed in as an array
0
   def eval(form)
0
- if (form.is_a?(Cons)) and form.first
0
- apply(form.first, form.rest)
0
+ if (form.is_a?(Cons)) and form.car
0
+ apply(form.car, form.cdr || cons)
0
     elsif form.is_a? Sym
0
       self[form.sym]
0
     else # well it must be a literal then
0
@@ -22,7 +22,7 @@ module BusScheme
0
   # Call a function with given args
0
   def apply(function_sym, args)
0
     function = eval(function_sym)
0
- args = args.map { |arg| eval(arg) } unless function.special_form
0
+ args = args.map { |arg| eval(arg) } unless function.special_form
0
     puts ' ' * stack.length + Cons.new(function_sym, args).inspect if (@trace ||= false)
0
 
0
     function.call_as function_sym, args
...
14
15
16
17
 
18
19
 
20
21
22
23
24
 
 
25
26
27
...
14
15
16
 
17
18
 
19
20
21
22
23
 
24
25
26
27
28
0
@@ -14,14 +14,15 @@ module BusScheme
0
     end
0
 
0
     # execute body with args bound to formals
0
- def call(*args)
0
+ def call(args)
0
       locals = if @formals.is_a? Sym # rest args
0
- { @formals => args.to_list }
0
+ { @formals => args }
0
                else # regular arg list
0
                  raise BusScheme::ArgumentError, "Wrong number of args:
0
   expected #{@formals.size}, got #{args.size}
0
   #{BusScheme.stacktrace.join("\n")}" if @formals.length != args.length
0
- @formals.to_a.zip(args).to_hash
0
+ # TODO: don't convert to an array first
0
+ @formals.to_a.zip(args.to_a).to_hash
0
                end
0
 
0
       @frame = StackFrame.new(locals, @enclosing_scope, @called_as)
...
6
7
8
9
 
10
11
12
13
 
14
15
16
...
54
55
56
57
 
58
59
60
...
129
130
131
132
133
134
135
136
 
 
137
138
...
6
7
8
 
9
10
11
12
 
13
14
15
16
...
54
55
56
 
57
58
59
60
...
129
130
131
 
 
 
 
 
132
133
134
135
0
@@ -6,11 +6,11 @@ class BusSchemeLambdaTest < Test::Unit::TestCase
0
     l = eval!("(lambda () (+ 1 1))")
0
     assert l.is_a?(Lambda)
0
     assert_equal [[:+.sym, 1, 1].to_list], l.body
0
- assert_equal 0, l.formals.length
0
+ assert_equal cons, l.formals
0
     
0
     eval!("(define foo (lambda () (+ 1 1)))")
0
     assert BusScheme[:foo.sym].is_a?(Lambda)
0
- assert_evals_to 2, [:foo.sym]
0
+ assert_evals_to 2, cons(:foo.sym)
0
   end
0
 
0
   def test_lambda_with_arg
0
@@ -54,7 +54,7 @@ class BusSchemeLambdaTest < Test::Unit::TestCase
0
     eval! "(define foo (lambda (xx) ((lambda (y) (+ xx y)) (* xx 2))))"
0
     assert foo = BusScheme[:foo.sym]
0
     
0
- assert_evals_to 3, foo.call(1)
0
+ assert_evals_to 3, foo.call(cons(1))
0
     eval! "(define holder ((lambda (x) (lambda () x)) 2))"
0
     assert_evals_to 2, "(holder)"
0
   end
0
@@ -129,10 +129,7 @@ class BusSchemeLambdaTest < Test::Unit::TestCase
0
   end
0
 
0
   def test_lambdas_accept_list_of_args
0
- BusScheme['prim'] = Primitive.new(lambda { |args| assert args.is_a?(Cons) })
0
- BusScheme['prim'].call(cons(1))
0
-
0
- apply(:prim.sym, cons(1, cons(2)))
0
- # lamb = Lambda.new
0
+ BusScheme['foo'] = eval!("(lambda (a) (assert (isa? a \"Cons\")))")
0
+ BusScheme['foo'].call(cons(1))
0
   end
0
 end
...
216
217
218
219
220
221
222
223
224
...
216
217
218
 
 
 
219
220
221
0
@@ -216,9 +216,6 @@ class BusSchemeParserTest < Test::Unit::TestCase
0
     
0
     list = parse_tokens [:'(', :lambda.sym, :'(', :')', 1, :')']
0
     assert_equal [:lambda.sym, [], 1].to_list(true), list
0
-
0
-# list = parse_list(tokenize("(lambda () 1)"))
0
-# assert_equal [:lambda.sym, [], 1].to_list(true), list
0
   end
0
   
0
   private

Comments

    No one has commented yet.