Source code: com/memoire/silk/SilkMacro.java
1
2
3 package com.memoire.silk;
4 import com.memoire.silk.*;
5
6
7
8 /** @author Peter Norvig, peter@norvig.com http://www.norvig.com
9 * Copyright 1998 Peter Norvig, see http://www.norvig.com/license.html **/
10
11 public class SilkMacro extends SilkClosure {
12
13 /** Make a macro from a parameter list, body, and environment. **/
14 public SilkMacro (Object parms, Object body, SilkEnvironment env) {
15 super(parms, body, env);
16 }
17
18 /** Replace the old cons cell with the macro expansion, and return it. **/
19 public SilkPair expand(SilkScheme interpreter, SilkPair oldSilkPair, Object args) {
20 Object expansion = apply(interpreter, args);
21 if (expansion instanceof SilkPair) {
22 oldSilkPair.first = ((SilkPair)expansion).first;
23 oldSilkPair.rest = ((SilkPair)expansion).rest;
24 } else {
25 oldSilkPair.first = "begin";
26 oldSilkPair.rest = cons(expansion, null);
27 }
28 return oldSilkPair;
29 }
30
31 /** SilkMacro expand an expression **/
32 public static Object macroExpand(SilkScheme interpreter, Object x) {
33 if (!(x instanceof SilkPair)) return x;
34 Object fn = interpreter.eval(first(x), interpreter.globalSilkEnvironment);
35 if (!(fn instanceof SilkMacro)) return x;
36 return ((SilkMacro)fn).expand(interpreter, (SilkPair)x, rest(x));
37 }
38 }