KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > kawa > lang > Eval


1 // Copyright (C) 2005 Per M.A. Bothner.
2
// This is free software; for terms and warranty disclaimer see ../../COPYING.
3

4 package kawa.lang;
5 import gnu.mapping.*;
6 import gnu.expr.*;
7 import gnu.text.SourceMessages;
8 import gnu.lists.*;
9
10 /* This implements the R5RS "eval" procedure. */
11
12 public class Eval extends Procedure1or2
13 {
14   public static final Eval eval = new Eval();
15   static { eval.setName("eval"); }
16
17   final static String JavaDoc evalFunctionName = "atEvalLevel";
18
19   public static void eval (Object JavaDoc sexpr, Environment env, CallContext ctx)
20     throws Throwable JavaDoc
21   {
22     PairWithPosition body;
23     if (sexpr instanceof PairWithPosition)
24       body = new PairWithPosition((PairWithPosition) sexpr,
25                   sexpr, LList.Empty);
26     else
27       {
28     body = new PairWithPosition(sexpr, LList.Empty);
29     body.setFile("<eval>");
30       }
31     evalBody(body, env, new SourceMessages(), ctx);
32   }
33
34   public static Object JavaDoc evalBody (Object JavaDoc body, Environment env,
35                  SourceMessages messages)
36     throws Throwable JavaDoc
37   {
38     CallContext ctx = CallContext.getInstance();
39     int oldIndex = ctx.startFromContext();
40     try
41       {
42     evalBody(body, env, messages, ctx);
43     return ctx.getFromContext(oldIndex);
44       }
45     catch (Throwable JavaDoc ex)
46       {
47     ctx.cleanupFromContext(oldIndex);
48     throw ex;
49       }
50   }
51
52   public static Object JavaDoc eval (Object JavaDoc sexpr, Environment env)
53         throws Throwable JavaDoc
54   {
55     CallContext ctx = CallContext.getInstance();
56     int oldIndex = ctx.startFromContext();
57     try
58       {
59     eval(sexpr, env, ctx);
60     return ctx.getFromContext(oldIndex);
61       }
62     catch (Throwable JavaDoc ex)
63       {
64     ctx.cleanupFromContext(oldIndex);
65     throw ex;
66       }
67   }
68
69   public static void evalBody (Object JavaDoc body, Environment env,
70                    SourceMessages messages, CallContext ctx)
71     throws Throwable JavaDoc
72   {
73     Language language = Language.getDefaultLanguage();
74     Environment saveGlobalEnv = Environment.getCurrent();
75     try
76       {
77     if (env != saveGlobalEnv)
78       Environment.setCurrent(env);
79     Translator tr = new Translator(language, messages);
80         tr.immediate = true;
81     ModuleExp mod = tr.pushNewModule((String JavaDoc) null);
82         Compilation save_comp = Compilation.getCurrent();
83         try
84           {
85             Compilation.setCurrent(tr);
86             int first = tr.formStack.size();
87             tr.scanBody(body, mod, false);
88             tr.firstForm = first;
89             tr.finishModule(mod);
90           }
91         finally
92           {
93             Compilation.setCurrent(save_comp);
94           }
95
96     if (body instanceof PairWithPosition)
97       mod.setFile(((PairWithPosition) body).getFileName());
98     mod.setName (evalFunctionName);
99     ModuleExp.evalModule(env, ctx, tr, null, null);
100     if (messages.seenErrors())
101       throw new RuntimeException JavaDoc("invalid syntax in eval form:\n"
102                      + messages.toString(20));
103       }
104     finally
105       {
106     if (env != saveGlobalEnv)
107       Environment.setCurrent(saveGlobalEnv);
108       }
109   }
110
111   public Object JavaDoc apply1 (Object JavaDoc arg1)
112     throws Throwable JavaDoc
113   {
114     return eval (arg1, Environment.user ());
115   }
116
117   public Object JavaDoc apply2 (Object JavaDoc arg1, Object JavaDoc arg2)
118     throws Throwable JavaDoc
119   {
120     return eval (arg1, (Environment) arg2);
121   }
122
123   public void apply (CallContext ctx)
124     throws Throwable JavaDoc
125   {
126     Procedure.checkArgCount(this, ctx.count);
127     Object JavaDoc exp = ctx.getNextArg();
128     Environment env;
129     env = (Environment) ctx.getNextArg(null);
130     if (env == null)
131       env = Environment.user();
132     ctx.lastArg();
133     eval(exp, env, ctx);
134   }
135 }
136
Popular Tags