/
eval.js.ca
157 lines (129 loc) · 5.1 KB
/
eval.js.ca
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
var environment=[];
var curEnv; //index of current Environment
var replEnv = {};
replEnv['+'] = function(a,b){return a+b;};
replEnv['-'] = function(a,b){return a-b;};
replEnv['*'] = function(a,b){return a*b;};
replEnv['/'] = function(a,b){return a/b;};
replEnv["def!"]=function(a,b){console.log("def ! executed with "+a+" "+b);
return envSet(a,b,curEnv);};
replEnv['t']=function(){return true;}
replEnv['f']=function(){return false;}
replEnv['nil']=function(){return false;}
environment.push(replEnv);
function isFunction(x) {
return Object.prototype.toString.call(x) == '[object Function]';
}
// complete rewrite after looking at Paul Graham's Evaluator and reading Norvig's interpreter
var evaluate = function (tt,env){
var nt=[];
function evaluu(tt,env,y,vl,ia){ // wrapping the function
var t=tt;
var el=t[y];
var inArray=ia;
if(el===undefined)
{console.log("current el is undefined");
return vl;
}
if(Array.isArray(el))
{console.log(" list detected");
console.log(JSON.stringify(el));
var vnl=[];
var oT=t,oY=y,oVl=vl,oIA=inArray; //saving old values
t=el;el=t[0];y=0;
vl=vnl; //starting a new list
inArray=true;
}
if(isAtom(el))
{if(el.type=="INTEGER")
{console.log(el.value+" added to vl");
vl.push(el);
++y; //index for adding atom elements
return evaluu(t,env,y,vl);
}
if((el.type=="SYMBOL")&&(envFind(el.value,env)!="error!"))
{var pval=environment[env][el.value];
console.log("function found "+el.value)
if(isExecutable(t))
{console.log("executable");
printList(t);
++y; //index for adding atom elements
if(t.length==3) // to be changed ; for 2 arguments only
{console.log(pval(t[1].value,t[2].value));
var nxp={type: "INTEGER", value: pval(t[1].value,t[2].value)};
oVl.push(nxp);
return evaluu(oT,env,++oY,oVl,oIA); // virtually goes up the tree again
}
else
{console.log("currently 2 arguments only");}
}
else
{console.log(" nested -- next list : ");
vl.push(el);
++y; //index for adding atom elements
if(!inArray)
{return evaluu(t,env,y,vl,false);}
else
{vl=vl.push(evaluu(t,env,y,vl,true));
return vl;
}
}
}
else
{console.log("not a function");}
}
}
nt=evaluu(tt,env,0,nt,false);
console.log("evaluate finished ");
console.log(JSON.stringify(nt));
return nt;
};
function getNextList(x){
var i;
for(i=0;i<x.length;++i)
{if(Array.isArray(x[i]))
{return i;}
}
return undefined; // returns undefined when there is no other list
}
function isExecutable(x){ // checks if there are no more "nested" parameters
var i=0;
console.log("executing isExecutable x.length "+x.length);
for(i=0;i<x.length;++i)
{if(Array.isArray(x[i])){return false;}
}
return true;
}
function printList(x){
var i=0;
var p=[];
for(i=0;i<x.length;++i){p.push(x[i]);}
console.log(JSON.stringify(p));
}
function envSet(s,m,e){environment[e][s]=m;
return m;};
function envFind(s,e){for(var x in environment[e])
{if(x==s)
{return e;}
}
console.log(s+" not found! error!");
return "error!";
};
function envGet(s,e){var en;
en=envFind(s,e);
if(en!="error!")
{var nev=environment[en];
for(var x in nev)
{if(x==s)
{return environment[en][x];}
}
}
else
{return en;}
};
function isAtom(x){
if(x.type=="INTEGER"){return true;}
if(x.type=="SYMBOL"){return true;}
return false;
}
module.exports = ({evaluate: evaluate});