comparison 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
comparison
equal deleted inserted replaced
982:6934343267c0 983:2cd8c1aa0d3a
854 } 854 }
855 855
856 function exec0(env, e) { 856 function exec0(env, e) {
857 var stack = null; 857 var stack = null;
858 858
859 var saveEnv = function() {
860 if (stack.next != null && stack.next.data.c != "<")
861 stack = cons({c: "<", env: env}, stack.next);
862 else
863 stack = stack.next;
864 };
865
859 while (true) { 866 while (true) {
860 switch (e.c) { 867 switch (e.c) {
861 case "c": 868 case "c":
862 var v = e.v; 869 var v = e.v;
863 if (stack == null) 870 if (stack == null)
889 break; 896 break;
890 case "a2": 897 case "a2":
891 if (fr.f == null) 898 if (fr.f == null)
892 whine("Ur: applying null function"); 899 whine("Ur: applying null function");
893 else if (fr.f.body) { 900 else if (fr.f.body) {
894 stack = cons({c: "<", env: env}, stack.next); 901 saveEnv();
895 env = cons(v, fr.f.env); 902 env = cons(v, fr.f.env);
896 e = fr.f.body; 903 e = fr.f.body;
897 } else { 904 } else {
898 e = {c: "c", v: fr.f(v)}; 905 e = {c: "c", v: fr.f(v)};
899 stack = stack.next; 906 stack = stack.next;
921 case ";": 928 case ";":
922 e = fr.e2; 929 e = fr.e2;
923 stack = stack.next; 930 stack = stack.next;
924 break; 931 break;
925 case "=": 932 case "=":
926 stack = cons({c: "<", env: env}, stack.next); 933 saveEnv();
927 env = cons(v, env); 934 env = cons(v, env);
928 e = fr.e2; 935 e = fr.e2;
929 break; 936 break;
930 case "m": 937 case "m":
931 var ps; 938 var ps;
932 for (ps = fr.p; ps != null; ps = ps.next) { 939 for (ps = fr.p; ps != null; ps = ps.next) {
933 var r = execP(env, ps.data.p, v); 940 var r = execP(env, ps.data.p, v);
934 if (r != false) { 941 if (r != false) {
935 stack = cons({c: "<", env: env}, stack.next); 942 saveEnv();
936 env = r; 943 env = r;
937 e = ps.data.b; 944 e = ps.data.b;
938 break; 945 break;
939 } 946 }
940 } 947 }