diff lib/js/urweb.js @ 983:2cd8c1aa0d3a

Add tail call optimization to interpretation
author Adam Chlipala <adamc@hcoop.net>
date Thu, 24 Sep 2009 15:45:37 -0400
parents 6934343267c0
children dfe34fad749d
line wrap: on
line diff
--- a/lib/js/urweb.js	Thu Sep 24 15:36:44 2009 -0400
+++ b/lib/js/urweb.js	Thu Sep 24 15:45:37 2009 -0400
@@ -856,6 +856,13 @@
 function exec0(env, e) {
   var stack = null;
 
+  var saveEnv = function() {
+    if (stack.next != null && stack.next.data.c != "<")
+      stack = cons({c: "<", env: env}, stack.next);
+    else
+      stack = stack.next;
+  };
+
   while (true) {
     switch (e.c) {
     case "c":
@@ -891,7 +898,7 @@
         if (fr.f == null)
           whine("Ur: applying null function");
         else if (fr.f.body) {
-          stack = cons({c: "<", env: env}, stack.next);
+          saveEnv();
           env = cons(v, fr.f.env);
           e = fr.f.body;
         } else {
@@ -923,7 +930,7 @@
         stack = stack.next;
         break;
       case "=":
-        stack = cons({c: "<", env: env}, stack.next);
+        saveEnv();
         env = cons(v, env);
         e = fr.e2;
         break;
@@ -932,7 +939,7 @@
         for (ps = fr.p; ps != null; ps = ps.next) {
           var r = execP(env, ps.data.p, v);
           if (r != false) {
-            stack = cons({c: "<", env: env}, stack.next);
+            saveEnv();
             env = r;
             e = ps.data.b;
             break;