Eval.java 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. // Copyright (C) 2005, 2010 Per M.A. Bothner.
  2. // This is free software; for terms and warranty disclaimer see ../../COPYING.
  3. package kawa.lang;
  4. import gnu.mapping.*;
  5. import gnu.expr.*;
  6. import gnu.text.SourceMessages;
  7. import gnu.kawa.io.InPort;
  8. import gnu.lists.*;
  9. /* This implements the R5RS "eval" procedure. */
  10. public class Eval
  11. {
  12. public static void evalForm$X (Object sexpr, Environment env, CallContext ctx)
  13. throws Throwable
  14. {
  15. PairWithPosition body;
  16. if (sexpr instanceof PairWithPosition)
  17. body = new PairWithPosition((PairWithPosition) sexpr,
  18. sexpr, LList.Empty);
  19. else
  20. {
  21. body = new PairWithPosition(sexpr, LList.Empty);
  22. body.setFile(InPort.evalPathname);
  23. }
  24. evalBody(body, env, new SourceMessages(), ctx);
  25. }
  26. public static Object eval (Object sexpr, Environment env)
  27. throws Throwable
  28. {
  29. CallContext ctx = CallContext.getInstance();
  30. int oldIndex = ctx.startFromContext();
  31. try
  32. {
  33. evalForm$X(sexpr, env, ctx);
  34. return ctx.getFromContext(oldIndex);
  35. }
  36. catch (Throwable ex)
  37. {
  38. ctx.cleanupFromContext(oldIndex);
  39. throw ex;
  40. }
  41. }
  42. public static Object evalBody (Object body, Environment env,
  43. SourceMessages messages)
  44. throws Throwable
  45. {
  46. CallContext ctx = CallContext.getInstance();
  47. int oldIndex = ctx.startFromContext();
  48. try
  49. {
  50. evalBody(body, env, messages, ctx);
  51. return ctx.getFromContext(oldIndex);
  52. }
  53. catch (Throwable ex)
  54. {
  55. ctx.cleanupFromContext(oldIndex);
  56. throw ex;
  57. }
  58. }
  59. public static void evalBody (Object body, Environment env,
  60. SourceMessages messages, CallContext ctx)
  61. throws Throwable
  62. {
  63. Language language = Language.getDefaultLanguage();
  64. Environment saveGlobalEnv = Environment.getCurrent();
  65. try
  66. {
  67. if (env != saveGlobalEnv)
  68. Environment.setCurrent(env);
  69. Translator tr = (Translator) language.getCompilation(messages,
  70. NameLookup.getInstance(env, language));
  71. tr.immediate = true;
  72. // The state value BODY_PARSED-1 causes require#importDefinitions
  73. // to do the right thing.
  74. tr.setState(Compilation.BODY_PARSED-1);
  75. tr.setSharedModuleDefs(true);
  76. ModuleExp mod = tr.pushNewModule(null);
  77. if (env == Environment.user())
  78. mod.setFlag(ModuleExp.INTERACTIVE);
  79. Compilation saveComp = Compilation.setSaveCurrent(tr);
  80. try
  81. {
  82. tr.scanBody(body, mod, false);
  83. tr.finishModule(mod);
  84. if (body instanceof PairWithPosition)
  85. mod.setFile(((PairWithPosition) body).getFileName());
  86. tr.setEvalName();
  87. tr.process(Compilation.RESOLVED);
  88. }
  89. finally
  90. {
  91. Compilation.restoreCurrent(saveComp);
  92. }
  93. ModuleExp.evalModule(env, ctx, tr, null, null);
  94. if (messages.seenErrors())
  95. throw new RuntimeException("invalid syntax in eval form:\n"
  96. + messages.toString(20));
  97. }
  98. finally
  99. {
  100. if (env != saveGlobalEnv)
  101. Environment.setCurrent(saveGlobalEnv);
  102. }
  103. }
  104. }