-
Notifications
You must be signed in to change notification settings - Fork 0
/
Jenny.js
187 lines (137 loc) Β· 4.31 KB
/
Jenny.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
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
var Environment = require("./Environment");
var globalEnv = require("./GlobalEev");
var Transformer = require("./parser/transformer");
//Interpretor for jenny code.
class Jenny {
constructor(global = globalEnv) {
this.global = global;
this.transformer = new Transformer();
}
eval(exp, env = this.global) {
if (isNumber(exp)) {
return exp;
}
if (isString(exp)) {
return exp.slice(1, -1);
}
if (isBoolean(exp)) {
return true;
}
/**** Variable declarations */
if (exp[0] === "var") {
let [_, name, value] = exp;
return env.define(name, this.eval(value, env))
}
if (isVariableName(exp)) {
//console.log("here I am", exp[0])
return env.lookup(exp);
}
/** Implementing Block */
if (exp[0] === "begin") {
const blockEnv = new Environment({}, env);
return this.evalBlock(exp, blockEnv);
}
//Implementing set keyword
if (exp[0] === "set") {
let [_, name, value] = exp;
return env.setValue(name, this.eval(value, env))
}
//Ending Comparision operators ============
//Implementing if statement
if (exp[0] === "if") {
let [_, condition, consequent, alternative] = exp;
if (this.eval(condition, env)) {
return this.eval(consequent, env);
} else {
return this.eval(alternative, env);
}
}
// Implementing Switch statement;
if(exp[0] === "switch") {
return this.eval(this.transformer.transformSwitchToIf(exp), env);
}
//Implementing for loop
if(exp[0] === "for") {
return this.eval(this.transformer.transformForToWhile(exp), env);
}
// Implementing while statement
if (exp[0] === "while") {
let [_, condition, expression] = exp;
let result;
while (this.eval(condition, env)) {
result = this.eval(expression, env);
}
return result;
}
// Function definition here
if (exp[0] === "func") {
let [_, funcName, params, body] = exp;
const fn = {
funcName,
params,
body,
env
}
return env.define(funcName, fn);
}
if (exp[0] === "lambda") {
let [_, params, body] = exp
return {
params,
body,
env
}
}
if (Array.isArray(exp)) {
let fn = this.eval(exp[0], env);
let args = exp.slice(1).map(arg => this.eval(arg, env));
if (typeof fn === "function") {
return fn(...args);
}
let activationRecord = {}
//console.log(fn.funcName);
fn.params.forEach((param, index) => {
activationRecord[param] = args[index];
});
let activationEnv = new Environment(activationRecord, fn.env);
return this.evalBody(fn.body, activationEnv);
}
//console.stack();
//If this happens we have syntax error
throw new Error(`Unexpected syntax ${exp[0]}`);
}
evalBody(body, env) {
if (body[0] === "begin") {
return this.evalBlock(body, env);
} else {
return this.eval(body, env);
}
}
evalBlock(exp, blockEnv) {
let result;
let [_, ...block] = exp;
block.forEach(expression => {
result = this.eval(expression, blockEnv);
});
return result;
}
}
function isNumber(exp) {
return typeof exp === "number";
}
function isString(exp) {
return typeof exp === "string" && exp[0] === '"' && exp.slice(-1) === '"';
}
function isArray(exp) {
return Array.isArray(exp);
}
function isVariableName(exp) {
//console.log(typeof exp === "string" && /^[+\-*/<>=a-zA-Z0-9_]*$/.test(exp), exp)
return typeof exp === "string" && /^[+\-*/<>=a-zA-Z0-9_]*$/.test(exp)
}
function isBoolean(exp) {
if (typeof exp === "boolean") {
return exp;
}
}
module.exports = Jenny