/
eval.js.irrweg
188 lines (157 loc) · 6.41 KB
/
eval.js.irrweg
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
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
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);
// after days of disappointment I finally decided to somehow copy the reader functions
var evaluate = function (t,env){
function evalForm(t,env,i){
if(t[i]!==undefined)
{if(Array.isArray(t[i]))
{if( (t[i][0].type=="INTEGER") && (t[i][1]===undefined) )
{console.log("singleton detected and replaced");
return t[i][0];}
else
{console.log("evalList started ");
return evalList(t[i],env);
}
}
else
{return evalAtom(t,env,i);}
}
}
function evalAtom(t,env,i){
var el=t[i];
if(isAtom(el))
{if(el.type=="INTEGER")
{return el;}
if(el.type=="SYMBOL")
{return el;}
}
else
{console.log("error! unknown character !");}
}
function evalList(t,env){
var nterm=[];
var atom;
var i=0,el;
while(t[i]!==undefined)
{el=t[i];
if((el.type=="SYMBOL")&&(envFind(el.value,env)!="error!"))
{
console.log("function found "+el.value)
if(isExecutable(t))
{console.log("executable");
var pval=environment[env][el.value];
printList(t);
if(t.length==3) // to be changed ; for 2 arguments only
{var a,b;
if(t[1].type=="SYMBOL"){a=parseInt(environment[env][t[1].value]);}
if(t[2].type=="SYMBOL"){b=parseInt(environment[env][t[2].value]);}
if(t[1].type=="INTEGER"){a=t[1].value;}
if(t[2].type=="INTEGER"){b=t[2].value;}
var nxp={type: "INTEGER", value: pval(a,b)};
console.log("apply "+a+" "+b+" to "+el.value);
nterm.push(nxp);
console.log("nxp "+JSON.stringify(nxp));
i+=3;
}
else
{console.log("currently 2 arguments only");}
}
else
{console.log("not executable but reads atom");
atom=evalForm(t,env,i);
++i;
nterm.push(atom);
}
}
else
{if((el.type=="SYMBOL")&&(el.value=="define"))
{console.log("define detected");
envSet(t[1].value,t[2].value,env);
nterm.push(t[2]);
i+=3;
}
if(el.type=="INTEGER")
{console.log("reads INTEGER "+el.value)
atom=evalForm(t,env,i);
++i;
nterm.push(atom);
}
if(el.type=="SYMBOL")
{console.log(el.value+" not in environment");
}
}
}
console.log("nterm created "+JSON.stringify(nterm));
return nterm;
}
var nevv=t;
for(var i=0;i<t.length;++i)
{var old=evalList(nevv,env);
var nevv=evalList(old,env);
}
//console.log("no further simplifications possible");
return old;
};
function isFunction(x) {
return Object.prototype.toString.call(x) == '[object Function]';
}
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});