Permalink
Browse files

fix bugs with variable arity function definitions

  • Loading branch information...
1 parent 11d475d commit 819740f2b15a4ac60e9eeb2c81f5250551e425b2 @kiril kiril committed Aug 7, 2012
Showing with 15 additions and 18 deletions.
  1. +13 −2 ObjScheme/ObjScheme.m
  2. +2 −16 ObjSchemeTests/ObjSchemeTests.m
View
15 ObjScheme/ObjScheme.m
@@ -238,7 +238,11 @@ + (id)expandToken:(id)token atTopLevel:(BOOL)topLevel {
// we're going to change (define (f args) body) => (define f (lambda (args) body)) for simplicity
ObSCons* lambdaSpec = defineSpec;
ObSSymbol* lambdaName = [lambdaSpec car];
- ObSCons* params = [lambdaSpec cdr];
+ ObSCons* paramSpec = [lambdaSpec cdr];
+ id params = paramSpec;
+ if ( params != C_NULL && [params car] == S_DOT ) {
+ params = [params cadr];
+ }
// => (f (params) body)
ObSCons* lambda = CONS(S_LAMBDA, CONS(params, body));
return [ObjScheme expandToken: CONS(S_DEFINE, CONS(lambdaName, CONS(lambda, C_NULL)))
@@ -998,6 +1002,10 @@ - (void)dealloc {
[super dealloc];
}
+- (NSString*)description {
+ return [NSString stringWithFormat: @"%@", _environ];
+}
+
- (id)resolveSymbol:(ObSSymbol*)symbol {
id myValue = [_environ objectForKey: symbol.string];
if ( myValue ) {
@@ -1234,7 +1242,7 @@ - (id)initWithParameters:(id)parameters
if ( [cell car] == S_DOT ) {
NSAssert(last, @". as first param invalid");
_listParameter = [[cell cadr] retain];
- [last setCar: C_NULL];
+ [last setCdr: C_NULL];
break;
}
@@ -1278,6 +1286,9 @@ - (id)callWith:(ObSCons*)arguments {
if ( (id)arguments != C_NULL ) {
NSAssert( _listParameter, @"too many arguments" );
+ }
+
+ if ( _listParameter ) {
[invocationScope define: _listParameter as: arguments];
}
View
18 ObjSchemeTests/ObjSchemeTests.m
@@ -97,6 +97,8 @@ - (void)testBuiltIns {
OSAssertTrue(@"(not #f)");
OSAssertTrue(@"(begin (define x 128) (= x 128))");
+ OSAssertTrue(@"(begin (define (f x) x) (= (f 1) 1))");
+ OSAssertTrue(@"(begin (define (f . x) x) (equal? (f 1) '(1)))");
OSAssertTrue(@"(string? \"hello\")");
OSAssertFalse(@"(string? #f)");
@@ -357,20 +359,4 @@ - (void)testNSArrayBridge {
OSAssertTrue(@"(equal? (NSArray:array 56 7) (list->NSMutableArray (list 56 7)))");
}
-- (void)testStatEngine {
- NSError* error;
- NSString* engineSource = [[NSString alloc] initWithContentsOfFile: @"/Users/kiril/code/gc/gcstatengine/scheme/engine.scm"
- encoding: NSUTF8StringEncoding
- error: &error];
- id engine = [ObjScheme parseString: engineSource];
- [[ObjScheme globalScope] evaluate: engine];
-}
-
-/*
-- (void)testExample
-{
- STFail(@"Unit tests are not implemented yet in ObjSchemeTests");
-}
-*/
-
@end

0 comments on commit 819740f

Please sign in to comment.