| 1 4 package kawa.lang; 5 import gnu.mapping.*; 6 import gnu.expr.*; 7 import gnu.text.SourceMessages; 8 import gnu.lists.*; 9 10 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 evalFunctionName = "atEvalLevel"; 18 19 public static void eval (Object sexpr, Environment env, CallContext ctx) 20 throws Throwable  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 evalBody (Object body, Environment env, 35 SourceMessages messages) 36 throws Throwable  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 ex) 46 { 47 ctx.cleanupFromContext(oldIndex); 48 throw ex; 49 } 50 } 51 52 public static Object eval (Object sexpr, Environment env) 53 throws Throwable  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 ex) 63 { 64 ctx.cleanupFromContext(oldIndex); 65 throw ex; 66 } 67 } 68 69 public static void evalBody (Object body, Environment env, 70 SourceMessages messages, CallContext ctx) 71 throws Throwable  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 ) 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 ("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 apply1 (Object arg1) 112 throws Throwable  113 { 114 return eval (arg1, Environment.user ()); 115 } 116 117 public Object apply2 (Object arg1, Object arg2) 118 throws Throwable  119 { 120 return eval (arg1, (Environment) arg2); 121 } 122 123 public void apply (CallContext ctx) 124 throws Throwable  125 { 126 Procedure.checkArgCount(this, ctx.count); 127 Object 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 |