-
Notifications
You must be signed in to change notification settings - Fork 0
/
uplang.js
676 lines (617 loc) · 17.2 KB
/
uplang.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
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
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
const util = require('util');
let nop = function () {}
class Expo{ //basic expression object
[util.inspect.custom](depth, options) { //
return this.toString();
}
constructor(){
this.wipe()
}
wipe(){ //removes values from object
this._value = null //literal value
this.children = [] //value defining children values to querry
this.data = {} //data storage
this.listens = false //listener type
}
setValue(src,target){
//is a setter therefore has a target
//TODO: finalize
/*
while(target.upValue !== undefined){
target = target.env.stackTop[src.upValue]; //point to correct object
if(!target){ //empty setter upvalues
}
}
*/
target.data = Object.assign({},src.data) //shallow copy data object
if(src.call){//is a function call
Object.assign(target,src)
target.target = undefined
}
else if(src.clobber){//its a word (clobber value clobbers whatever was in target)
Object.assign(target,src.clobber)
}
else{//no, its a value
target._value = src._value
}
}
get value(){ //chain get value
if(this.env.alive === false)return this.errorCatch(CodeError("script has stopped"));
let src = this; //source value
while(src.upValue !== undefined){
src = src.env.stackTop[src.upValue]; //point to correct object
if(!src)return null; //empty upvalues are null and not error, in js (a => a === undefined )() -> true, //this.errorCatch(new CodeError(`upvalue contains no object`));
}
//src is now at correct object
if(src.target){
this.setValue(src,src.target)
}else{
if(src.call){//is a function call
let fn = src.call.run
if(fn) return this.errorCatch(fn(src,src.call,src.children,src.data)); //expo.run = function(caller,self,arguments,dataStorage){ ... }
else {
return this.errorCatch(new CodeError(`caller, ${src.call.toString()} has no code `))
}
}else{//no, its a value
return this.errorCatch(src._value) //errorCatch here is mildly pointless
}
}
return null;
}
get type(){ //what type is it
if(this.target){//is a setter therefore has a target
if(this.call){//is a function call
return `setTo call`
}else if(this.clobber){//its a word
return "setTo "+this.clobber.type
}else{//no, its a value
return "setTo value"
}
}else{
if(this.run){
return `call source`
}else
if(this.call){//is a function call
return "call"
}
if(this.upValue !== undefined){ //is an upvalue therefore has .upvalue
return "upvalue"
}
else if(this._value !== null)
{//no, its a value
return "value"
}
{
return "null"
}
}
return null; //its nothing?!
}
get location(){
let n = this.context
if(n) n = n.name;
return `${this.name || "anonymous"}@${n || "anonymous"}`
}
toString(){
//totally needs reform
return `${this.location} = [${this.target ? "("+this.target.location+") " : ""}${this.type == "value" ? typeof this._value+" " : ""}${this.type}${this.call ? ` of ${this.call.location}` : ""}]`
}
errorCatch(error){
if ( error instanceof CodeError )error.stackAdd("from: "+this.toString());
return error
}
evaluate(){
let l = this.value
if(l instanceof CodeError){
}
}
}
exports.Expo = Expo
class EnvContext {
constructor(){
this.items = {}
}
item(name){ //gets an item from items
let l = this.items[name]
if(l)return l;
l = new Expo()
l.name = name
l.context = this
l.env = this.env
this.items[name] = l
return l;
}
itemExists(name){ //gets an item from items
return name in this.items
}
}
exports.EnvContext = EnvContext
class Env {
constructor(){
this.contexts = {}
this.stack = []
this.stackTop = [] //be careful not to have an unbalenced push to pop ratio and become null
this.imports = {}
}
stackPush(args){
this.stack.push(this.stackTop)
this.stackTop = args
}
stackPop(){
if(this.stack.length < 1)throw new Error("cannot pop empty stack")
this.stackTop = this.stack.pop()
}
context(name){ //gets an item from contexts
let l = this.contexts[name]
if(l)return l;
l = new EnvContext()
l.name = name
l.env = this
this.contexts[name] = l
if(this.imports[name]) this.imports[name](l);
return l;
}
makeInlineContext(name){
let ctxt = new EnvContext()
ctxt.name = name || "~inline"
ctxt.env = this
return ctxt
}
}
exports.Env = Env
//turn on as needed
//const homedir = require('os').homedir();
//const fs = require('fs');
let $break = Object.create(null) //token that represents the break between statements
$break[0] = "break"
Object.freeze($break)
class CompileError extends Error {
constructor(message,text,position) {
super(message+" char:"+position); // (1)
this.name = ""; // (2)
}
}
exports.CompileError = CompileError
class CodeError {
[util.inspect.custom](depth, options) {
return this.toString();
}
constructor(message,pos) {
this.message = message
this.dispMessage = message+"\n\n"
this.stack = []
}
toString(){
return this.dispMessage
}
stackAdd(name,comment){
this.stack.push(name)
this.dispMessage += name+(comment ? ", "+comment : "")+"\n"
}
}
exports.CodeError = CodeError
let compilePt1 = function(data){
let lchain = {} //unused left in for compatability (var passed to all substructures)
let p = 0
let bytes = (amt,noadv) => {
let l = data.substr(p,amt)
if(!noadv)p+=amt;
return l
}
let isLetter = char => char.match(/[a-zA-Z]/)
let isNumber = char => char.match(/[0-9]/)
let isWord = char => char.match(/[0-9A-Za-z\-\_]/)
let isStillNumber = char => char.match(/[0-9\.]/)
this.body = function (lme/*top of lchain*/,isTop,argL) {
argL = argL || [["start"]]
let doBreak = false
while (p <= data.length) {
let b = bytes(1)
switch (b) {
case "$":
argL.push(["dollar"])
break;
case "@":
if(argL[argL.length-1][0] == "word"){
argL[argL.length-1][0] = 'atWord'
}else{
//it scopes context using: @ place { ... }
argL.push(["at"])
}
break;
case "\n":
case "," :
{
let n = argL[argL.length-1]
if(n === $break || n === undefined){}else{
argL.push($break)
}}
break;
case ")":
if(isTop)throw new CompileError("attempted to close top level statement ",data,p);
doBreak = true
//console.log(bytes(5,true))
break;
case "/":
if(bytes(1,true) == "/"){
//console.log("comment");
this.comment()
}else
if(bytes(1,true) == "*"){
this.longComment()
}else
{
}
break;
case " " :
case "\t":
//nop
break;
case "=":
{
let n = argL[argL.length-2]
if (n === $break || n === undefined){
let l = argL[argL.length-1]
//console.log(l)
if(l[0] == "word"){
l[0] = 'setterTarget' //changes from word to setter target
}else{
throw new CompileError("unsupported left hand side of (single arg) equals",data,p);
}
}else{
//some other condition
//else{
//console.log(argL)
throw new CompileError("unsupported left hand side of (muli arg) equals",data,p);
//}
}
}
break;
case "(":
let l = argL[argL.length-1]
if(l[0] == "word"){
l[0] = "call"
//if(l[2] == null)l[2] = "func"; //invalid check and replace
let p = 3
l[p] = []
this.body(lme,false,l[p])
}else
if(l[0] == "atPtr"){
l[0] = "atSub"
let p = 2
l[p] = []
this.body(lme,false,l[p])
}else
if(l[0] == "at"){
l[0] = "atNamelessSub"
let p = 2
l[p] = []
this.body(lme,false,l[p])
}else
{
argL.push([ 'call', "do", 'func' , this.body(lme,false) ])
}
break;
case "\"":
argL.push(['value',this.string(lme)])
break;
default:
if (isLetter(b)) {
p--
let l = argL[argL.length-1] || []
if(l[0] == "atWord"){
l[0] = "word"
l[2] = this.word(lme)
}else
if(l[0] == "at"){
l[0] = "atPtr"
l[1] = this.word(lme)
}else
if(l[0] == "dollar"){ //supported but not used, should Ibe an upvalue?
l[0] = "word w/ dollar"
l[1] = this.word(lme)
}
else
{
argL.push(['word',this.word(lme),null])
}
}else if (isNumber(b)) {
p--
let l = argL[argL.length-1] || []
if(l[0] == "dollar"){ // $0
l[0] = 'upvalue'
l[1] = this.number(lme)
}
else {
argL.push(['value',this.number(lme)])
}
}else if ( false )
{
}
}
if(doBreak)break; //stop the loop
}
return argL;
}
this.word = function (lme/*top of lchain*/) {
let word = []
while(p <= data.length){
let b = bytes(1)
if(isWord(b)){
word.push(b)
}
else{
p--
break;
}
}
return word.join("")
}
this.number = function (lme/*top of lchain*/) {
let word = []
while(p <= data.length){
let b = bytes(1)
if(isStillNumber(b)){
word.push(b)
}
else{
p--
break;
}
}
return Number(word.join(""))
}
this.comment = function (lme/*top of lchain*/) {
while(p <= data.length){
let b = bytes(1)
if(b != "\n"){
}
else{
break;
}
}
return
}
this.string = function (lme/*top of lchain*/) {
let word = []
while(p <= data.length){
let b = bytes(1)
if(b != "\""){
word.push(b)
}
else{
break;
}
}
return word.join("")
}
this.longComment = function (lme/*top of lchain*/) {
while(p <= data.length){
let b = bytes(1)
if(b != "*"){
}
else{
if(bytes(1,true) == "/"){
break;
}
}
}
return
}
return this.body(lchain,true)
}
exports.compilePt1 = compilePt1
let compilePt2 = function(data,estruct,textData){
let econtext = estruct.context.bind(estruct)
let cstack = ['root','sys','func','main'].map(econtext) //stack of contexts
let ctop = ()=> cstack[cstack.length-1]
let cGet = key => {
let l;
for (let i = cstack.length-1; i+1 > 0;i--){ //find existing key closest to top
//WTF i => 0 is not the same as i+1 > 0
l = cstack[i]
//console.log(i,l.name)
if(l.itemExists(key))return cstack[i];
}
return ctop();
}
this.scanArgs = (chain,est,newst) => {
newst= newst || []
let unow = new Expo() //object w/ number keys too (what did I mean here?)
unow.env = estruct
let push = ()=>{ //push: push and prepare new
newst.push(unow)
unow = new Expo()
unow.env = estruct
}
let ctxtif = name => name ? estruct.context(name) : null
for(var i=0;i<chain.length;i++){
let l = chain[i];
switch(l[0]){
case "setterTarget":
if(unow.target){
//target allready exists (possably many words per statement)
throw new CompileError("too many setters per statement",data,l.i)
}else{
//console.log(l[1])
unow.target = ( ctxtif(l[2]) || ctop() ).item(l[1]) // .target = specific || top of stack
//console.log(ctop())
}
break;
case "break":
//push()
break;
case "upvalue":
if(unow.target)throw new CompileError("cannot set value to upvalue",data,l.i);
unow.upValue = l[1]
push()
break;
case "value":
unow._value = l[1]
push()
break;
case "call":
unow.call = ( ctxtif(l[2]) || cGet(l[1]) ).item(l[1]) // .call = specific || topmost existing key
unow.children = this.scanArgs(l[3],estruct)
{
let call = unow.call
if(call.onCallExpoFab)call.onCallExpoFab(unow) //whenever a new call type Expo is fabricated
}
push()
break;
case "word":
if(unow.target){
unow.impaler = ( ctxtif(l[2]) || cGet(l[1]) ).item(l[1]) //literal copy operation // .impailer = specific || topmost existing key
}else{
unow = ( ctxtif(l[2]) || cGet(l[1]) ).item(l[1]) // unow = specific || topmost existing key
}
push()
break;
case "atSub":
unow.call = estruct.context("func").item("do")
cstack.push( ctxtif(l[1]) )
unow.children = this.scanArgs(l[2],estruct)
cstack.pop()
push()
break;
case "atNamelessSub":
unow.call = estruct.context("func").item("do")
let ctxt = new EnvContext()
ctxt.name = "~inline"
ctxt.env = estruct
cstack.push( ctxt )
unow.children = this.scanArgs(l[2],estruct)
cstack.pop()
push()
break;
}
}
//if(chain.length > 0)newst.push(unow)
return newst
}
return this.scanArgs(data,estruct)
}
exports.compilePt2 = compilePt2
Env.prototype.addComonFunctions = function () {
let func = this.context("func")
{
func.item("do").run = function(caller,self,args,data){
let moob = null;
for (const item of args) {
moob = item.value
if(moob instanceof CodeError)return moob;
}
return moob;
}
}
{
let funcCmd = func.item("func")
funcCmd.run = function(caller,self,args,data){
}
funcCmd.onCallExpoFab = function(newbie) {
newbie.run = function(caller,self,args,data){
self.env.stackPush(args) //args are now upvalues
let code = self.children // the function body to execute my self's arguments, (property is named children)
//(code from do)
{
let moob = null;
for (const item of code) {
moob = item.value
if(moob instanceof CodeError)return moob;
}
return moob;
}
}
}
}
{
//list the env vars
func.item("dumpEnv").run = function(caller,self,args,data){
let l = ""
for (const [key, value] of Object.entries( self.env.contexts )) {
l += `${key}:`
if(Object.keys(value.items).length == 0) l += "\n";
for (const [key2, value2] of Object.entries( value.items )) {
l +=
`\t${key2}: `+
value2.type+
( value2.type == "value" ? " "+JSON.stringify(value2.value) : "" )+
"\n"
//value2,"\n\n\n");
}
}
return l;
}
}
{
func.item("print").run = function(caller,self,args,data){
console.log("",...args.map(v => v.value))
}
}
{
func.item("whatIs").run = function(caller,self,args,data){
console.log("",...args.map(v => v.toString()))
}
}
{
func.item("set").run = function(caller,self,args,data){
let l = new Expo()
l.env = self.env
l.target = args[0]
l.clobber = args[1]
return l.value
}
}
{
func.item("get").run = function(caller,self,args,data){ //mabe add property querry: get(cake,"type")
return args[0].value
}
}
{
func.item("dont").run = function(caller,self,args,data){
return null;
}
}
{
func.item("if").run = function(caller,self,args,data){
let ifTest = args[0].value
if(ifTest instanceof CodeError){
ifTest.stackAdd("as \'test\' of: if(test,a,b)")
return ifTest
}
return args[ifTest ? 1 : 2].value
}
}
{
func.item("dumpContextOf").run = function(caller,self,args,data){
if(args[0] || args[0].context) console.log(args[0].context.items);
}
func.item("static").run = function(caller,self,args,data){
//arg1 = static value, arg2 = release
if(data.isSet && !args[1].value) return data.value;
data.value = args[0].value
data.isSet = true
return data.value
//if(args[0] || args[0].context) console.log(args[0].context.items);
}
func.item("staticSet").run = function(caller,self,args,data){
//if(args[0] || args[0].context) console.log(args[0].context.items);
Object.assign(args[0],{
call:null,clobber:null,
_value: args[1].value
})
}
}
}
Env.prototype.compileCode = function (code) {
return compilePt2(compilePt1(code),this,code)
};
Env.prototype.runCode = function (code) {
let l = this.compileCode(code)
let moob;
for (const item of l) {
moob = item.value
if(moob instanceof CodeError)return moob;
}
return moob;
};