-
Notifications
You must be signed in to change notification settings - Fork 0
/
SltRuntime.ts
645 lines (524 loc) · 18 KB
/
SltRuntime.ts
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
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
'use strict'
import { sha1 } from "./compiler_libs/hashLib.js"
let uCount = 0
const Mutates = -1
function error(errType: string, message: string, value: SltValue): SltValue{
if (value.loc != null) {
const [ln, cn, fn] = value.loc
const lnString = ln.toString()
const cnString = cn.toString()
console.log("In file: \"" + fn + "\", line no: " + lnString + ", col no: " + cnString)
}
throw (`Uncaught Error: ${errType} ${message}`)
}
function checkType(expected: string, val: SltValue){
if (expected != val.type_) error("TypeError", "expected " + expected + " got " + val.type_, val)
return val
}
function sameTypes(t: SltValue, other: SltValue, fun: (a: SltValue, b: SltValue) => SltValue) {
other = other || t
if (other.type_ == t.type_){
return true
}
return fun(t, other)
}
async function write(text: string){
const te = new TextEncoder();
await Deno.writeAll(Deno.stdout, te.encode(text));
}
class SltValue extends Function {
type_: string = ""
loc: [number, number, string] = [0, 0, ""]
isEmpty: boolean = true
hashAble: boolean = false
value: any = null
mutates: number | null = null
overarch: string = ""
tail: Function | (() => SltValue)
head: Function | (() => SltValue)
constructor(...args: any[]) {
super()
this.tail = () => error("TypeError", "Cannot get the tail of " + this.type_ , this)
this.head = () => error("TypeError", "Cannot get the head of " + this.type_ , this)
return new Proxy(this, {
apply: (target, thisArg, args) => target._call(...args)
})
}
_call(..._: SltValue[]) {
return error("Type Error", "Can't call a value of type " + this.type_, this)
}
add(other: SltValue): SltValue { return error("TypeError", "Cannot add " + this.type_ + " to " + other.type_, this) }
sub(other: SltValue): SltValue { return error("TypeError", "Cannot substract " + this.type_ + " from " + other.type_, this) }
mul(other: SltValue): SltValue { return error("TypeError", "Cannot multiply " + this.type_ + " from " + other.type_, this) }
div(other: SltValue): SltValue { return error("TypeError", "Cannot divide " + this.type_ + " from " + other.type_, this) }
isType(other: string | SltValue): SltValue {
if (typeof other == "string") {return new SltBool(this.type_ == other, this.loc)}
return new SltBool(this.type_ == other.type_, this.loc)
}
neg(): SltValue { return error("TypeError", "Can't negate " + this.type_, this) }
notted(): SltValue { return error("TypeError", "Can't not " + this.type_, this) }
is_true(): boolean { error("TypeError", "Can't use " + this.type_ + " in as conditions", this); return false }
eq(_: SltValue): SltValue { return new SltBool(false, this.loc) }
lt(other: SltValue): SltValue {return error("TypeError", "Cannot quantify " + this.type_ + " and " + other.type_ + " with appropriate quantities for '<'", this)}
lte(other: SltValue): SltValue {return error("TypeError", "Cannot quantify " + this.type_ + " and " + other.type_ + " with appropriate quantities for '<='", this)}
gt(other: SltValue): SltValue {return error("TypeError", "Cannot quantify " + this.type_ + " and " + other.type_ + " with appropriate quantities for '>'", this)}
gte(other: SltValue): SltValue {return error("TypeError", "Cannot quantify " + this.type_ + " and " + other.type_ + " with appropriate quantities for '>='", this)}
async getOutput(_: boolean = false) {
await write(this.toString())
}
neq(_: SltValue): SltValue { return new SltBool(true, this.loc) }
anded(other: SltValue): SltValue { return error("TypeError", "Can't make 'and' of " + this.type_ + " and " + other.type_, this) }
getHash() {
uCount = uCount + 1
return "Unhasable" + this.type_ + uCount.toString()
}
ored(other: SltValue): SltValue { return error("TypeError", "Can't make 'or' of " + this.type_ + " and " + other.type_, this) }
getProperty(other: SltValue): SltValue { return error("KeyError", "Cannot get the property " + other.toString() + " of " + this.type_, this) }
locate(loc: [number, number, string]): SltValue {
this.loc = loc
return this
}
concat(other: SltValue): SltValue { return error("TypeError", "Cannot concatenate " + this.type_ + " to " + other.type_, this); }
}
function destructure(struct: SltValue, args: number): SltValue[] {
if (struct instanceof SltStruct) {
struct.checkLength(args)
let arr: SltValue[] = []
for (const k in struct.value){
arr.push(struct.value[k])
}
return arr
}
return [error("TypeError", "Cannot destructure " + struct.type_, struct)]
}
function unwrap(tup: SltValue, args: number){
if (tup instanceof SltTuple){
tup.checkLength(args)
return tup.value
}
error("TypeError", "Cannot destructure " + tup.type_, tup)
}
class SltThunk extends SltValue {
fun: () => SltValue
constructor(func: () => SltValue, mutates: number | null = null){
super()
this.type_ = "SltThunk";
this.mutates = mutates
this.fun = func;
this.value = null;
}
toString() { return "SltThunk" }
_call() {
if (this.value == null || this.mutates == Mutates) this.value = this.fun()
return this.value
}
}
class SltBool extends SltValue {
value: boolean
constructor(bool: boolean, loc: [number, number, string]) {
super()
this.type_ = "SltBool"
this.value = bool
this.loc = loc
return new Proxy(this, {
apply: (target, thisArg, args) => target._call(...args)
})
}
ored(other: SltValue): SltValue {
if (this.value) return new SltBool(true, this.loc)
sameTypes(this, other(), (a, b) => a.ored(b))
if (other().value) return new SltBool(true, this.loc)
return new SltBool(false, this.loc)
}
anded(other: SltValue): SltValue {
if (this.value) {
if (other().value) return new SltBool(true, this.loc)
}
return new SltBool(false, this.loc)
}
notted() { return new SltBool(!this.value, this.loc) }
eq(other: SltValue) {
if (this.type_ != other.type_) return new SltBool(false, this.loc)
return new SltBool(this.value == other.value, this.loc)
}
neq(other: SltValue) { return this.eq(other).notted() }
is_true() { return this.value }
toString() { return this.value.toString() }
}
class SltNum extends SltValue {
constructor(num: number, location: [number, number, string]){
super()
this.type_ = "SltNum";
this.hashAble = false;
this.loc = location;
this.value = num;
}
add(other: SltValue): SltValue {
sameTypes(this, other, (a, b) => a.add(b))
return new SltNum(this.value + other.value, this.loc)
}
sub(other: SltValue): SltValue {
sameTypes(this, other, (a, b) => a.sub(b))
return new SltNum(this.value - other.value, this.loc)
}
mul(other: SltValue): SltValue {
sameTypes(this, other, (a, b) => a.mul(b))
return new SltNum(this.value * other.value, this.loc)
}
div(other: SltValue): SltValue {
sameTypes(this, other, (a, b) => a.div(b))
if (other.value == 0) error("DivisionByZero", "Can't divide by zero", this)
return new SltNum(this.value / other.value, this.loc)
}
getHash() { return sha1(this.value.toString()) + "IsANumber" }
neg() { return new SltNum(-this.value, this.loc) }
concat(other: SltValue){
if (this.value != 0) return super.concat(other)
return other()
}
eq(other: SltValue) {
if (this.type_ != other.type_) return new SltBool(false, this.loc)
return new SltBool(this.value == other.value, this.loc)
}
neq(other: SltValue) {
if (this.type_ != other.type_) return new SltBool(true, this.loc)
return new SltBool(this.value != other.value, this.loc)
}
lt(other: SltValue) {
sameTypes(this, other, (_, b) => super.lt(b))
return new SltBool(this.value < other.value, this.loc)
}
lte(other: SltValue) {
sameTypes(this, other, (_, b) => super.lte(b))
return new SltBool(this.value <= other.value, this.loc)
}
gte(other: SltValue) {
sameTypes(this, other, (_, b) => super.gte(b))
return new SltBool(this.value >= other.value, this.loc)
}
gt(other: SltValue) {
sameTypes(this, other, (_, b) => super.gt(b))
return new SltBool(this.value > other.value, this.loc)
}
toString() { return this.value.toString() }
}
interface HashTable<T> {
[key: string]: T;
}
class SltFunc extends SltValue {
fun: (a: SltValue) => SltValue
values: HashTable<SltValue>
hashes: boolean
constructor(fun: (a: SltValue) => SltValue, loc: [number, number, string], shouldHash: boolean = true){
super()
this.fun = fun;
this.type_ = "SltFunc"
this.values = {}
this.hashes = shouldHash
this.hashAble = false
this.loc = loc
}
toString() { return "<function>" }
eq(other: SltValue) { return new SltBool(false, this.loc) }
neq(other: SltValue) { return new SltBool(true, this.loc) }
getValue(arg: SltValue) {
if (this.hashes && arg().hashAble) return this.values[arg().getHash()]
return null
}
_call(a: SltValue, ..._: SltValue[]): SltValue {
const val = this.getValue(a)
if (val != null) return val
const res = this.fun(a)
if (this.hashes && a().hashAble) this.values[a().getHash()] = res
return res
}
}
class SltList extends SltValue {
constructor(head: SltValue, tail: SltValue, loc: [number, number, string]){
super()
this.head = new SltThunk(() => head);
this.tail = new SltThunk(() => tail);
this.isEmpty = false;
this.loc = loc;
this.type_ = "SltList";
}
static fromValues(vals: SltValue[], loc: [number, number, string]){
function reversed<T>(array: T[]){
return array.map((item,idx) => array[array.length-1-idx])
}
let result = new SltNum(0, loc)
const list = reversed(vals)
const length = list.length
let i = 0
while (i<length){
let headThunk = list[i]
let tailThunk = result
result = new SltList(headThunk, tailThunk, loc)
i = i+1
}
return result
}
static direct(head: SltValue, tail: SltValue, loc: [number, number, string]){
const list = new SltList(new SltValue(), new SltValue(), loc)
list.head = head
list.tail = tail
list.loc = loc
return list
}
toList(){
let tailIter = this.tail()
const result: SltValue[] = [this.head()]
while (!tailIter.isEmpty){
result.push(tailIter.head(tailIter))
tailIter = tailIter.tail(tailIter)
}
return result
}
concat(other: SltValue) {
let tail: SltValue = this.tail()
if (tail.isEmpty) {
const tailThunk = other
const x = SltList.direct(new SltThunk(() => this.head()), tailThunk, this.loc)
return x
}
const thunk = new SltThunk(() => tail.concat(other))
return SltList.direct(new SltThunk(() => this.head()), thunk, this.loc)
}
eq(other: SltValue){
if (other.type_ != this.type_) return new SltBool(false, this.loc)
if (!(other instanceof SltList)) return new SltBool(false, this.loc)
const this_seq = this.toList()
const other_seq = other.toList()
for (const k in this_seq) {
const v = this_seq[k]
if (other_seq[k] != undefined) return new SltBool(false, this.loc)
if (!v.eq(other_seq[k]).value) return new SltBool(false, this.loc)
}
return new SltBool(true, this.loc)
}
neq(other: SltValue){
return new SltBool(!(this.eq(other).value), this.loc)
}
locate(location: [number, number, string]) {
this.loc = location
return this
}
async getOutput(whole: boolean){
let output = this
if (!whole) {
await write("[")
while (output.type_ == "SltList"){
await output.head().getOutput(true)
await write(", ")
output = output.tail()
}
await write("]")
return
}
while (output.type_ == "SltList") {
console.log(output.head().toString())
console.log("\n")
output = output.tail()
}
return
}
toString(){
return "[" + this.toList().map(x => x.toString()).join(", ") + "]"
}
}
class SltTuple extends SltValue {
constructor(tp: SltValue[], loc: [number, number, string]){
super()
this.type_ = "SltTuple"
this.value = tp
this.loc = loc
}
checkLength(length: number){
if (length != this.value.length) {
error(
"TypeError", "Cannot destructure length " + this.value.length.toString() + " tuple to " + length + " names", this
)
}
return this
}
eq(other: SltValue) {
if (this.type_ != other.type_) return new SltBool(false, this.loc)
const bool_false = new SltBool(false, this.loc)
if (this.value.length != other.value.length) return bool_false
let main_bool = new SltBool(true, this.loc)
for (const i in this.value){
const v = this.value[i]
if (other.value[i] == undefined || other.value[i] == null) return bool_false
if (v().neq(other.value[i]()).is_true()) return bool_false
}
return main_bool
}
getHash() { return this.value.map((v: SltValue) => v().getHash()).join("NexT") }
neq(other: SltValue){
sameTypes(this, other, super.neq)
return (this.eq(other)).notted()
}
toString() { return "(" + this.value.map((v: SltValue) => v().toString()).join(", ") + ",)" }
}
class SltType extends SltValue {
constructor(t: string, loc: [number, number, string]){
super()
this.type_ = t;
this.loc = loc;
}
toString(){ return "<" + this.type_ + ">" }
getHash(){ return sha1(this.type_) + "IsATypeRepr" }
}
class SltString extends SltValue {
constructor(str: string, loc: [number, number, string]){
super()
this.type_ = "SltString"
this.hashAble = false
this.value = str
this.loc = loc
}
concat(other: SltValue){
sameTypes(this, other(), super.concat)
return new SltString(this.value + other().value, this.loc)
}
getHash() { return sha1(this.value.toString()) + "IsAString" }
eq(other: SltValue) { return new SltBool(this.value == other.value, this.loc) }
neq(other: SltValue) { return this.eq(other).notted() }
toString() { return this.value }
}
class SltStruct extends SltValue {
constructor(name: string, overarch: string, canHash: boolean, tb: HashTable<SltValue>, loc: [number, number, string]){
super()
this.type_ = name
this.overarch = overarch
this.hashAble = canHash
this.loc = loc
this.value = tb
}
isType(other: SltValue | string) {
if (other instanceof SltValue) return new SltBool(this.type_ == other.type_ || this.overarch == other.type_, this.loc)
return new SltBool(this.type_ == other || this.overarch == other, this.loc)
}
checkLength(length: number){
if (length != Object.keys(this.value).length) {
error(
"TypeError", "Cannot destructure length " + Object.keys(this.value).length.toString() + " struct to " + length.toString() + " names", this
)
}
return this
}
getHash(){
const init = sha1(this.type_)
let ls = init
for (const k in this.value) {
ls = ls + sha1(k) + "-to-" + this.value[k]().getHash()
}
return ls + "From" + sha1(this.overarch || "NoOverArch")
}
eq(other: SltValue): SltValue {
const eq_struct = (ta: HashTable<SltValue>, tb: HashTable<SltValue>) => {
if (ta.length != tb.length) return new SltBool(false, this.loc)
for (const k in ta){
const v = ta[k]
if (!(k in tb)) return new SltBool(false, this.loc)
if (!tb[k]().eq(v())) return new SltBool(false, this.loc)
}
return new SltBool(true, this.loc)
}
if (other instanceof SltStruct) return new SltBool(this.type_ == other.type_ && this.overarch == other.overarch && eq_struct(this.value, other.value).value, this.loc)
else return new SltBool(false, this.loc)
}
neq(other: SltValue) { return this.eq(other).notted() }
async getOutput(_: boolean){
await write(this.type_)
await write("{")
for (const k in this.value) {
const v = this.value[k]
await write(k)
await write(" :: ")
await v().getOutput()
await write(", ")
}
await write("}")
}
toString(){
const init = this.type_
let ls = init + "{"
const slen = Object.keys(this.value).length
if (slen == 0) return init
let i = 0
for (const k in this.value){
const v = this.value[k]
ls = ls + k + " :: " + v().toString()
ls = i == slen-1 && ls || ls + ", "
i = i+1
}
return ls + "}"
}
tableString() { return "{" + this.value.join(", ") + "}" }
getProperty(property: SltValue){
if (!(property instanceof SltString)) error("TypeError", "Expected string found " + property.toString(), this)
if (property.value in this.value) return this.value[property.value]
error("IndexError", property.toString() + " not found in " + this.tableString(), this)
}
}
/////////////////////////////
// Base functions
const listHead =
new SltThunk(
() => new SltFunc((ls) => ls().head(), [0, 0, "core"])
)
const listTail =
new SltThunk(
() => new SltFunc((ls) => ls().tail(), [0, 0, "core"])
)
const baseStringify =
new SltThunk(
() => new SltFunc(t => new SltString(t().toString(), t().loc), [0, 0, "core"])
)
const evaluate = new SltThunk(
() =>
new SltFunc(
t => {
if (t.type_ != "SltThunk") return error("TypeError", "Cannot evaluate a " + t.type_, t)
t().toString()
return t()
},
[0, 0, "core"]
)
)
const typeOf = new SltThunk(
() =>
new SltFunc(
a => new SltType(a().type_, a().loc),
[0, 0, "core"]
)
)
const ovTypeOf = new SltThunk(
() =>
new SltFunc(
a => new SltType(a().overarch ? a().overarch : a().type_, a().loc),
[0, 0, "core"]
)
)
const unsafeWrite = new SltThunk(
() => new SltFunc(
exp => new SltFunc(
st => {
st().getOutput(true)
return exp()
},
exp().loc
),
[0, 0, "core"]
),
Mutates
)
export {
SltValue, SltNum, SltFunc, SltString,
SltStruct, SltList, SltThunk, SltTuple,
SltType, destructure, unwrap, listHead,
listTail, baseStringify, evaluate,
typeOf, ovTypeOf, unsafeWrite, SltBool,
error, Mutates, write
}