/
eval.crap.js
151 lines (124 loc) · 5.74 KB
/
eval.crap.js
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
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]';
}
var evaluate = function (tt,env){
var i=0,y=0;
var fset=false;
var param=[];
var t,f,pI=0,lpush=0;
var ntok=[];
var nImmuts=[];//useless new immutables
var nI=0;// index for useless new immutables
curEnv=env;// added in step 3
console.log("eval: trying to evaluate "+JSON.stringify(tt));
t=tt;
while(t[y]!==undefined){
if(Array.isArray(t[y])){for(var k=lpush;k<i;++k)
{ntok.push(t[k]);
}
lpush=i;
fset=false;
pI=0;
console.log(" FSET set to false !!!");
t=t[y];
y=0;
}
console.log(" current value to test "+t[y].value);
if(t[y].type=="SYMBOL")
{if(fset)
{if(envFind(t[y].value,env)!="error!")
{var pval=environment[env][t[y].value];
nImmuts.push(pval);
console.log("parameter was "+pval+" and is "+nImmuts[param[pI]]);
}
else
{nImmuts.push(t[y].value);
console.log("string pushed as "+nImmuts[param[pI]]);
}
param[pI]=nI;
++nI;
++pI;
}
if(!fset)
{if(envFind(t[y].value,env)!="error!")
{console.log(t[y].value+" exists in env. !");
if(isFunction(environment[env][t[y].value])) // if SYMBOL is a function
{fset=true;
f=y;//index of SYMBOL
pI=0;
var ff=environment[env][t[f].value];
console.log(t[f].value+" is a function");
}
else
{console.log(t[y].value+" is a value");
var pval=environment[env][t[y].value];
ntok.push(pval);
console.log(pval+" pushed");
}
}
else
{console.log("unknown SYMBOL detected !!!");
return "error!unknown SYMBOL!";
}
}
}
if( t[y].type=="INTEGER" )
{param[pI]=nI;
nImmuts.push(t[y].value)
console.log("INTEGER detected and pushed "+nImmuts[param[pI]]);
console.log(" y "+y+" t[y].value "+t[y].value);
console.log(" nI : "+param[0]+" "+param[1]);
++nI;
++pI;
}
if( pI==2 )
{var ff=environment[env][t[f].value];
console.log("operation "+t[f].value+" "+nImmuts[param[0]]+
" "+nImmuts[param[1]]);
//console.log("env structure: "+JSON.stringify(environment));
var nt={type: "INTEGER", value: ff(nImmuts[param[0]],nImmuts[param[1]])};
ntok.push(nt);
console.log("nImmuts-debug nI="+nI);
nImmuts.map(function(x){console.log(" "+x)});
}
++i;
++y;
}
return ntok;
};
// addition for step 3
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;}
};
module.exports = ({evaluate: evaluate,replEnv: replEnv});