Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
tree: b622d89156
Fetching contributors…

Cannot retrieve contributors at this time

executable file 410 lines (324 sloc) 12.551 kb
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410
/*!
@file macro_1.m
@description Nu macros.

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
#import "NuMacro_0.h"
#import "NuMacro_1.h"
#import "NuCell.h"
#import "NuSymbol.h"
#import "NuClass.h"
#import "NuExtensions.h"
#import "NuObjCRuntime.h"
#import "NuOperator.h"
//#import "match.h"

extern id Nu__null;

//#define MACRO1_DEBUG 1

// Following debug output on and off for this file only
#ifdef MACRO1_DEBUG
#define Macro1Debug(arg...) NSLog(arg)
#else
#define Macro1Debug(arg...)
#endif

@implementation NuMacro_1

+ (id) macroWithName:(NSString *)n parameters:(NuCell*)p body:(NuCell *)b
{
    return [[[self alloc] initWithName:n parameters:p body:b] autorelease];
}

- (void) dealloc
{
    [parameters release];
    [super dealloc];
}

- (BOOL) findAtom:(id)atom inSequence:(id)sequence
{
    if (atom == nil || atom == Nu__null)
        return NO;

    if (sequence == nil || sequence == Nu__null)
        return NO;

    if ([[atom stringValue] isEqualToString:[sequence stringValue]])
        return YES;

    if ([sequence class] == [NuCell class]) {
        return ( [self findAtom:atom inSequence:[sequence car]]
            || [self findAtom:atom inSequence:[sequence cdr]]);
    }

    return NO;
}

- (id) initWithName:(NSString *)n parameters:(NuCell *)p body:(NuCell *)b
{
    [super initWithName:n body:b];
    parameters = [p retain];

    if (([parameters length] == 1)
    && ([[[parameters car] stringValue] isEqualToString:@"*args"])) {
        // Skip the check
    }
    else {
        BOOL foundArgs = [self findAtom:@"*args" inSequence:parameters];

        if (foundArgs) {
            printf("Warning: Overriding implicit variable '*args'.\n");
        }
    }

    return self;
}

- (NSString *) stringValue
{
    return [NSString stringWithFormat:@"(macro-1 %@ %@ %@)", name, [parameters stringValue], [body stringValue]];
}

- (void) dumpContext:(NSMutableDictionary*)context
{
    NSArray* keys = [context allKeys];
    int count = [keys count];
    for (int i = 0; i < count; i++) {
#ifdef MACRO1_DEBUG
        id key = [keys objectAtIndex:i];
#endif
        Macro1Debug(@"contextdump: %@ = %@ [%@]", key,
            [[context objectForKey:key] stringValue],
            [[context objectForKey:key] class]);
    }
}

- (void) restoreArgs:(id)old_args context:(NSMutableDictionary*)calling_context
{
    NuSymbolTable *symbolTable = [calling_context objectForKey:SYMBOLS_KEY];

    if (old_args == nil) {
        [calling_context removeObjectForKey:[symbolTable symbolWithCString:"*args"]];
    }
    else {
        [calling_context setPossiblyNullObject:old_args forKey:[symbolTable symbolWithCString:"*args"]];
    }
}

- (void)restoreBindings:(id)bindings
forMaskedVariables:(NSMutableDictionary*)maskedVariables
fromContext:(NSMutableDictionary*)calling_context
{
    id plist = bindings;

    while (plist && (plist != Nu__null)) {
        id param = [[plist car] car];

        Macro1Debug(@"restoring bindings: looking up key: %@",
            [param stringValue]);

        [calling_context removeObjectForKey:param];
        id pvalue = [maskedVariables objectForKey:param];

        Macro1Debug(@"restoring calling context for: %@, value: %@",
            [param stringValue], [pvalue stringValue]);

        if (pvalue) {
            [calling_context setPossiblyNullObject:pvalue forKey:param];
        }

        plist = [plist cdr];
    }
}

- (id) destructuringListAppend:(id)lhs withList:(id)rhs
{
    Macro1Debug(@"Append: lhs = %@ rhs = %@", [lhs stringValue], [rhs stringValue]);

    if (lhs == nil || lhs == Nu__null)
        return rhs;

    if (rhs == nil || rhs == Nu__null)
        return lhs;

    id cursor = lhs;

    while ( cursor
        && (cursor != Nu__null)
        && [cursor cdr]
    && ([cursor cdr] != Nu__null)) {
        cursor = [cursor cdr];
    }

    [cursor setCdr:rhs];

    Macro1Debug(@"Append: result = %@", [lhs stringValue]);

    return lhs;
}

- (id) mdestructure:(id)pattern withSequence:(id)sequence
{
    Macro1Debug(@"mdestructure: pat: %@ seq: %@", [pattern stringValue], [sequence stringValue]);

// ((and (not pat) seq)
if ( ((pattern == nil) || (pattern == Nu__null))
&& !((sequence == Nu__null) || (sequence == nil))) {
        [NSException raise:@"NuDestructureException"
            format:@"Attempt to match empty pattern to non-empty object %@", [self stringValue]];
    }
    // ((not pat) nil)
    else if ((pattern == nil) || (pattern == Nu__null)) {
        return nil;
    }
    // ((eq pat '_) '()) ; wildcard match produces no binding
    else if ([[pattern stringValue] isEqualToString:@"_"]) {
        return nil;
    }
    // ((symbol? pat)
    // (let (seq (if (eq ((pat stringValue) characterAtIndex:0) '*')
    // (then (list seq))
    // (else seq)))
    // (list (list pat seq))))
    else if ([pattern class] == [NuSymbol class]) {
        id result;

        if ([[pattern stringValue] characterAtIndex:0] == '*') {
            // List-ify sequence
            id l = [[[NuCell alloc] init] autorelease];
            [l setCar:sequence];
            result = l;
        }
        else {
            result = sequence;
        }

        // (list pattern sequence)
        id p = [[[NuCell alloc] init] autorelease];
        id s = [[[NuCell alloc] init] autorelease];

        [p setCar:pattern];
        [p setCdr:s];
        [s setCar:result];

        // (list (list pattern sequence))
        id l = [[[NuCell alloc] init] autorelease];
        [l setCar:p];

        return l;
    }
    // ((pair? pat)
    // (if (and (symbol? (car pat))
    // (eq (((car pat) stringValue) characterAtIndex:0) '*'))
    // (then (list (list (car pat) seq)))
    // (else ((let ((bindings1 (mdestructure (car pat) (car seq)))
    // (bindings2 (mdestructure (cdr pat) (cdr seq))))
    // (append bindings1 bindings2))))))
    else if ([pattern class] == [NuCell class]) {
        if ( ([[pattern car] class] == [NuSymbol class])
        && ([[[pattern car] stringValue] characterAtIndex:0] == '*')) {

            id l1 = [[[NuCell alloc] init] autorelease];
            id l2 = [[[NuCell alloc] init] autorelease];
            id l3 = [[[NuCell alloc] init] autorelease];
            [l1 setCar:[pattern car]];
            [l1 setCdr:l2];
            [l2 setCar:sequence];
            [l3 setCar:l1];

            return l3;
        }
        else {
            if (sequence == nil || sequence == Nu__null) {
                [NSException raise:@"NuDestructureException"
                    format:@"Attempt to match non-empty pattern to empty object"];
            }

            id b1 = [self mdestructure:[pattern car] withSequence:[sequence car]];
            id b2 = [self mdestructure:[pattern cdr] withSequence:[sequence cdr]];

            id newList = [self destructuringListAppend:b1 withList:b2];

            Macro1Debug(@"jsb: dbind: %@", [newList stringValue]);
            return newList;
        }
    }
    // (else (throw* "NuMatchException"
    // "pattern is not nil, a symbol or a pair: #{pat}"))))
    else {
        [NSException raise:@"NuDestructureException"
            format:@"Pattern is not nil, a symbol or a pair: %@", [pattern stringValue]];
    }

    // Just for aesthetics...
    return nil;
}

- (id) expandAndEval:(id)cdr context:(NSMutableDictionary*)calling_context evalFlag:(BOOL)evalFlag
{
    NuSymbolTable *symbolTable = [calling_context objectForKey:SYMBOLS_KEY];

    NSMutableDictionary* maskedVariables = [[NSMutableDictionary alloc] init];

    id plist;

    Macro1Debug(@"Dumping context:");
    Macro1Debug(@"---------------:");
    [self dumpContext:calling_context];

    id old_args = [calling_context objectForKey:[symbolTable symbolWithCString:"*args"]];
    [calling_context setPossiblyNullObject:cdr forKey:[symbolTable symbolWithCString:"*args"]];

    id destructure;

    @try
    {
        // Destructure the arguments
        destructure = [self mdestructure:parameters withSequence:cdr];
    }
    @catch (id exception) {
        // Destructure failed...restore/remove *args
        [self restoreArgs:old_args context:calling_context];

        @throw;
    }

    plist = destructure;
    while (plist && (plist != Nu__null)) {
        id parameter = [[plist car] car];
        id value = [[[plist car] cdr] car];
        Macro1Debug(@"Destructure: %@ = %@", [parameter stringValue], [value stringValue]);

        id pvalue = [calling_context objectForKey:parameter];

        if (pvalue) {
            Macro1Debug(@" Saving context: %@ = %@",
                [parameter stringValue],
                [pvalue stringValue]);
            [maskedVariables setPossiblyNullObject:pvalue forKey:parameter];
        }

        [calling_context setPossiblyNullObject:value forKey:parameter];

        plist = [plist cdr];
    }

    Macro1Debug(@"Dumping context (after destructure):");
    Macro1Debug(@"-----------------------------------:");
    [self dumpContext:calling_context];

    // evaluate the body of the block in the calling context (implicit progn)
    id value = Nu__null;

    // if the macro contains gensyms, give them a unique prefix
    int gensymCount = [[self gensyms] count];
    id gensymPrefix = nil;
    if (gensymCount > 0) {
        gensymPrefix = [NSString stringWithFormat:@"g%ld", [NuMath random]];
    }

    id bodyToEvaluate = (gensymCount == 0)
        ? (id)body : [self body:body withGensymPrefix:gensymPrefix symbolTable:symbolTable];

    // Macro1Debug(@"macro evaluating: %@", [bodyToEvaluate stringValue]);
    // Macro1Debug(@"macro context: %@", [calling_context stringValue]);

    @try
    {
        // Macro expansion
        id cursor = [self expandUnquotes:bodyToEvaluate withContext:calling_context];
        while (cursor && (cursor != Nu__null)) {
            Macro1Debug(@"macro eval cursor: %@", [cursor stringValue]);
            value = [[cursor car] evalWithContext:calling_context];
            Macro1Debug(@"macro expand value: %@", [value stringValue]);
            cursor = [cursor cdr];
        }

        // Now that macro expansion is done, restore the masked calling context variables
        [self restoreBindings:destructure
            forMaskedVariables:maskedVariables
            fromContext:calling_context];

        [maskedVariables release];
        maskedVariables = nil;

        // Macro evaluation
        // If we're just macro-expanding, don't do this step...
        if (evalFlag) {
            Macro1Debug(@"About to execute: %@", [value stringValue]);
            value = [value evalWithContext:calling_context];
            Macro1Debug(@"macro eval value: %@", [value stringValue]);
        }

        Macro1Debug(@"Dumping context at end:");
        Macro1Debug(@"----------------------:");
        [self dumpContext:calling_context];

        // restore the old value of *args
        [self restoreArgs:old_args context:calling_context];

        Macro1Debug(@"macro result: %@", value);
    }
    @catch (id exception) {
        if (maskedVariables) {
            Macro1Debug(@"Caught exception in macro, restoring bindings");

            [self restoreBindings:destructure
                forMaskedVariables:maskedVariables
                fromContext:calling_context];

            Macro1Debug(@"Caught exception in macro, releasing maskedVariables");

            [maskedVariables release];
        }

        Macro1Debug(@"Caught exception in macro, restoring masked arguments");

        [self restoreArgs:old_args context:calling_context];

        Macro1Debug(@"Caught exception in macro, rethrowing...");

        @throw;
    }

    return value;
}

- (id) expand1:(id)cdr context:(NSMutableDictionary*)calling_context
{
    return [self expandAndEval:cdr context:calling_context evalFlag:NO];
}

- (id) evalWithArguments:(id)cdr context:(NSMutableDictionary *)calling_context
{
    return [self expandAndEval:cdr context:calling_context evalFlag:YES];
}

@end
Something went wrong with that request. Please try again.