-
Notifications
You must be signed in to change notification settings - Fork 22
/
index.js
329 lines (297 loc) · 10.6 KB
/
index.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
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
import { combineLatest, of, throwError } from "rxjs"
import { map, tap, shareReplay, catchError, switchMap } from "rxjs/operators"
import { get, isFunction, isObject, set, merge, assign, hasIn } from "lodash-es"
import Symbols from "./symbols"
import RootEnv from "./rootEnvironment"
class Interpreter {
issues = null
rootEnv = RootEnv
interpret = (ast, env = {}, issues) => {
this.issues = issues
const state = of(Object.assign(Object.create(this.rootEnv), { Self: this.rootEnv }, env))
const result = this.processToken(ast, state)
return {
success: {
result,
state
}
}
}
processToken = (token, state) => {
console.log(token)
if (!state) { console.error("No state to operate on!") }
const fn = this.tokenActions[token.type] || this.tokenActions.__unknown__
let result
try {
result = fn(token, state)
} catch (e) {
const issue = {
source: token._source || null,
message: e.message,
severity: e.severity
}
this.reportIssue(issue)
console.error(e)
result = null
}
return result
}
reportIssue({ source, message, severity = "error" }) {
const issue = {
...source,
message,
severity
}
this.issues.next(issue)
}
catchIssue = (e, token, value) => {
const issue = {
source: token._source || null,
message: e.message,
severity: "error"
}
this.reportIssue(issue)
return value || of(e)
}
tokenActions = {
Program: (token, state) => {
let stateAcc = state.pipe(
tap(
(state) => {
console.groupCollapsed("State")
console.log("Create new state from:", state)
}
),
map(
(state) => Object.assign(
Object.create(state),
{
[Symbols.meta]: {}
}
)
),
shareReplay(),
tap(
(state) => {
console.log("New state:", (state))
console.groupEnd()
}
)
)
token.value.forEach(token => {
const stateDelta = this.processToken(token, stateAcc)
stateAcc = combineLatest(stateAcc, stateDelta).pipe(
tap(
([state, stateDelta]) => {
console.groupCollapsed("State merge")
console.log("Going to merge state and stateDelta", state, stateDelta)
}
),
map(
([state, stateDelta]) => {
const key = Object.keys(stateDelta)[0]
let newState
let meta = merge(state[Symbols.meta], stateDelta[Symbols.meta])
if (state.hasOwnProperty(key)) {
newState = merge(state, stateDelta)
} else {
// newState = assign(state, stateDelta)
newState = Object.assign(state, stateDelta)
}
newState[Symbols.meta] = meta
return newState
}
),
tap(
(state) => {
console.log("New merged state", state)
console.groupEnd()
}
),
shareReplay(),
)
})
// Object.freeze?
return stateAcc
},
Assignment: (token, state) => {
const path = this.processToken(token.path, state)
const value = this.processToken(token.value, state)
return combineLatest(path, value).pipe(
tap(
([path, value]) => {
console.groupCollapsed("Assignment")
console.log("Creating state delta", path, value)
}
),
catchError(
e => this.catchIssue(e, token)
),
map(
([path, value]) => {
const metaPath = [Symbols.meta, ...path]
const obj = set({}, metaPath, {
silent: token.silent,
source: token._source
})
set(obj, path, value)
return obj
}
),
tap(
(stateDelta) => {
console.log("New state delta", stateDelta)
console.groupEnd()
}
)
)
},
Path: (token, state) => of(token.value),
FunctionApplication: (token, state) => {
const fn = this.processToken(token.function, state)
const arg = this.processToken(token.argument, state)
return combineLatest(fn, arg).pipe(
tap(
([fn, arg]) => {
console.groupCollapsed("Function Application")
console.log("Function", fn)
console.log("Argument", arg)
}
),
switchMap(
([fn, arg]) => {
return call(fn, arg)
}
),
catchError(
e => this.catchIssue(e, token)
),
tap(
(value) => {
console.log("Value", value)
console.groupEnd()
}
)
)
},
Reference: (token, state) => {
const path = this.processToken(token.value, state)
return combineLatest(path, state).pipe(
tap(
([path, state]) => {
console.groupCollapsed("Reference")
console.log("Getting a value from reference")
console.log("Path", path)
console.log("State", state)
}
),
map(
([path, state]) => {
const exists = hasIn(state, path)
if (!exists) {
// return throwError(new Error(`No value for ${path.join(".")}`))
throw new Error(`No value for ${path.join(".")}`)
}
const value = get(state, path)
return value
}
),
catchError(
e => this.catchIssue(e, token, of(undefined))
),
tap(
(value) => {
console.log("Value", value)
console.groupEnd()
}
)
)
},
Function: (token, state) => {
return state.pipe(
tap(
(state) => {
console.groupCollapsed("Function")
console.log("State", state)
console.log("Argument", token.argument)
}
),
map(
(state) => {
const fn = (arg) => {
const boundEnv = Object.assign(Object.create(state), {
[token.argument]: arg
})
return this.processToken(token.value, of(boundEnv))
}
return fn
}
),
tap(
(fn) => {
console.log("Function", fn)
console.groupEnd()
}
)
)
},
Operation: (token, state) => {
const left = this.processToken(token.left, state)
const right = this.processToken(token.right, state)
const fn = of(this.rootEnv[token.operator])
return combineLatest(fn, left, right).pipe(
tap(
([fn, left, right]) => {
console.groupCollapsed("Operation")
console.log("Operator", fn)
console.log("Left argument", left)
console.log("Right argument", right)
}
),
map(
([fn, a, b]) => {
return call(fn, { a, b })
}
),
catchError(
e => this.catchIssue(e, token)
),
tap(
(result) => {
console.log("Result from operation:", result)
console.groupEnd()
}
),
)
},
Tensor: (token, state) => {
const value = this.processToken(token.value, state)
return of(tf.tensor(value))
},
TensorLiteral: (token, state) => token.value,
Object: (token, state) => {
const result = this.processToken(token.value, state)
return result
},
String: (token, state) => of(token.value),
None: (token, state) => of(undefined),
__unknown__: (token, state) => {
throw new Error(`Unrecognized token: ${token.type}, rest: ${token}`)
}
}
}
// TODO: when arg is {}, consider only own props
// arg = isObject(arg) ? {...arg} : arg
const call = (fn, arg) => {
if (isFunction(fn)) {
return fn(arg)
}
if (isObject(fn)) {
const isCallable = fn.hasOwnProperty(Symbols.call)
if (isCallable) {
return call(fn[Symbols.call], arg)
}
}
throw new Error(`${fn} is not callable`)
}
export default new Interpreter