8000 add some operator logic · dragoncoder047/pickle@1de0513 · GitHub
[go: up one dir, main page]

Skip to content

Commit 1de0513

Browse files
add some operator logic
1 parent 0ca8edd commit 1de0513

File tree

1 file changed

+146
-50
lines changed

1 file changed

+146
-50
lines changed

pickle.js

Lines changed: 146 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -371,6 +371,41 @@ class PickleTokenizer {
371371
}
372372
}
373373

374+
class PickleOperator {
375+
/**
376+
* @type {Map<string, PickleOperator>}
377+
*/
378+
static _interned = new Map();
379+
/**
380+
* Create a new operator data.
381+
* @param {string} symbol
382+
* @param {"prefix" | "right" | "left" | "postfix"} associativity
383+
* @param {number?} precedence
384+
*/
385+
constructor(symbol, associativity, precedence = 1) {
386+
/**
387+
* @type {string}
388+
*/
389+
this.symbol = symbol;
390+
/**
391+
* @type {"prefix" | "right" | "left" | "postfix"}
392+
*/
393+
this.associativity = associativity;
394+
/**
395+
* @type {number}
396+
*/
397+
this.precedence = precedence;
398+
if (PickleOperator._interned.has(this.toString())) return PickleOperator._interned.get(this.toString());
399+
PickleOperator._interned.set(this.toString(), this);
400+
}
401+
toString() {
402+
return JSON.stringify({
403+
symbol: this.symbol,
404+
associativity: this.associativity,
405+
precedence: this.precedence
406+
});
407+
}
408+
}
374409

375410
/**
376411
* All objects in Pickle are instances of this.
@@ -380,7 +415,7 @@ class PickleObject {
380415
* The printable name of the type of the object.
381416
* @type {string}
382417
*/
383-
static typeName = "baseobject";
418+
static typeName = "object";
384419
/**
385420
* Create a new Pickle object.
386421
* @param {...PickleObject} prototypes
@@ -391,7 +426,7 @@ class PickleObject {
391426
*/
392427
this.properties = new Map();
393428
/**
394-
* @type {Map<string, PickleObject>}
429+
* @type {Map<PickleOperator, PickleObject>}
395430
*/
396431
this.operators = new Map();
397432
/**
@@ -457,6 +492,55 @@ class PickleObject {
457492
}
458493
}
459494

495+
/**
496+
* @param {string | number | BigInt | PickleBuiltinFunctionCallback} it
497+
* @returns {PickleObject}
498+
*/
499+
function toPickle(it) {
500+
switch (typeof it) {
501+
case "string":
502+
return new PickleString(it);
503+
case "number":
504+
return new PickleFloat(it);
505+
case "function":
506+
return new PickleBuiltinFunction(it);
507+
default:
508+
if (it instanceof BigInt) return new PickleInteger(it);
509+
}
510+
throw "can't convert " + typeof it;
511+
}
512+
513+
/**
514+
* Create an object with the specified properties.
515+
* @param {{properties: string | number | BigInt | function, operators: [PickleOperator, string | number | BigInt | function][]}} data
516+
* @param {...PickleObject} prototypes
517+
*/
518+
function newPickleObject(data, ...prototypes) {
519+
var o = new PickleObject(...prototypes);
520+
for (var pname of Object.getOwnPropertyNames(data.properties)) {
521+
o.properties.set(pname, toPickle(data.properties[pname]));
522+
}
523+
for (var [op, payload] of data.operators) {
524+
o.operators.set(op, toPickle(payload));
525+
}
526+
return o;
527+
}
528+
529+
const PickleSymbolPrototype = newPickleObject({
530+
operators: [
531+
[new PickleOperator("$", "prefix", 100), function (args, scope) {
532+
var x;
533+
for (var s of this.getMRO())
534+
if (s instanceof PickleScope && (x = s.bindings.find(this.sym)))
535+
return x;
536+
throw new PickleError(`undefined variable ${this.sym}`);
537+
}],
538+
[new PickleOperator("=", "left", -1), function (args, scope) {
539+
return new PicklePropertySetter(this, args.get(0));
540+
}]
541+
]
542+
});
543+
460544
class PickleSymbol extends PickleObject {
461545
static typeName = "symbol";
462546
/**
@@ -465,17 +549,19 @@ class PickleSymbol extends PickleObject {
465549
static _interned = new Map();
466550
/**
467551
* @param {string} sym The symbol content
468-
* @param {...PickleObject} prototypes
469552
*/
470-
constructor(sym, ...prototypes) {
471-
super(...prototypes);
553+
constructor(sym) {
554+
super(PickleSymbolPrototype);
472555
if (PickleSymbol._interned.has(sym)) return PickleSymbol._interned.get(sym);
473556
/**
474557
* @type {string}
475558
*/
476559
this.sym = sym;
477560
PickleSymbol._interned.set(sym, this);
478561
}
562+
get length() {
563+
return this.sym.length;
564+
}
479565
}
480566

481567
class PickleString extends PickleObject {
@@ -486,17 +572,19 @@ class PickleString extends PickleObject {
486572
static _interned = new Map();
487573
/**
488574
* @param {string} str The string content
489-
* @param {...PickleObject} prototypes
490575
*/
491-
constructor(str, ...prototypes) {
492-
super(...prototypes);
576+
constructor(str) {
577+
super(FOO);
493578
if (PickleString._interned.has(str)) return PickleString._interned.get(str);
494579
/**
495580
* @type {string}
496581
*/
497582
this.str = str;
498583
PickleString._interned.set(str, this);
499584
}
585+
get length() {
586+
return this.str.length;
587+
}
500588
}
501589

502590
class PickleFloat extends PickleObject {
@@ -507,10 +595,9 @@ class PickleFloat extends PickleObject {
507595
static _interned = new Map();
508596
/**
509597
* @param {number} num The number
510-
* @param {...PickleObject} prototypes
511598
*/
512-
constructor(num, ...prototypes) {
513-
super(...prototypes);
599+
constructor(num) {
600+
super(FOO);
514601
if (PickleFloat._interned.has(num)) return PickleFloat._interned.get(num);
515602
/**
516603
* @type {number}
@@ -521,14 +608,16 @@ class PickleFloat extends PickleObject {
521608
}
522609

523610
class PickleComplex extends PickleObject {
611+
static typeName = "complex";
524612
constructor(...x) {
525-
throw 'not supported yet';
613+
throw new PickleError('not supported yet');
526614
}
527615
}
528616

529617
class PickleRational extends PickleObject {
618+
static typeName = "rational";
530619
constructor(...x) {
531-
throw 'not supported yet';
620+
throw new PickleError('not supported yet');
532621
}
533622
}
534623

@@ -540,10 +629,9 @@ class PickleInteger extends PickleObject {
540629
static _interned = new Map();
541630
/**
542631
* @param {BigInt} num The number
543-
* @param {...PickleObject} prototypes
544632
*/
545-
constructor(num, ...prototypes) {
546-
super(...prototypes);
633+
constructor(num) {
634+
super(FOO);
547635
if (PickleInteger._interned.has(num)) return PickleInteger._interned.get(num);
548636
/**
549637
* @type {BigInt}
@@ -557,10 +645,9 @@ class PickleErrorObject extends PickleObject {
557645
static typeName = "error";
558646
/**
559647
* @param {PickleError} error The thrown error
560-
* @param {...PickleObject} prototypes
561648
*/
562-
constructor(error, ...prototypes) {
563-
super(...prototypes);
649+
constructor(error) {
650+
super(FOO);
564651
/**
565652
* @type {PickleError}
566653
*/
@@ -573,10 +660,9 @@ class PickleFunction extends PickleObject {
573660
/**
574661
* @param {PickleCollection} args The signature
575662
* @param {PickleString} body The code of the function
576-
* @param {...PickleObject} prototypes
577663
*/
578-
constructor(args, body, ...prototypes) {
579-
super(...prototypes);
664+
constructor(args, body) {
665+
super(FOO);
580666
/**
581667
* @type {PickleCollection}
582668
*/
@@ -596,10 +682,9 @@ class PickleBuiltinFunction extends PickleObject {
596682
static typeName = "builtin_function";
597683
/**
598684
* @param {PickleBuiltinFunctionCallback} fun The callable interface
599-
* @param {...PickleObject} prototypes
600685
*/
601-
constructor(fun, ...prototypes) {
602-
super(...prototypes);
686+
constructor(fun) {
687+
super(FOO);
603688
/**
604689
* @type {PickleBuiltinFunctionCallback}
605690
*/
@@ -610,11 +695,10 @@ class PickleBuiltinFunction extends PickleObject {
610695
class PickleStream extends PickleObject {
611696
static typeName = "stream";
612697
/**
613-
* @param {stream} s The stream
614-
* @param {...PickleObject} prototypes
698+
* @param {stream} stream The stream
615699
*/
616-
constructor(s, ...prototypes) {
617-
super(...prototypes);
700+
constructor(stream) {
701+
super(FOO);
618702
/**
619703
* @type {stream}
620704
*/
@@ -627,10 +711,9 @@ class PickleCollectionEntry extends PickleObject {
627711
/**
628712
* @param {PickleObject?} key The key value
629713
* @param {PickleObject} value The value at the key
630-
* @param {...PickleObject} prototypes
631714
*/
632-
constructor(key, value, ...prototypes) {
633-
super(...prototypes);
715+
constructor(key, value) {
716+
super(FOO);
634717
/**
635718
* @type {PickleObject}
636719
*/
@@ -646,15 +729,33 @@ class PickleCollection extends PickleObject {
646729
static typeName = "collection";
647730
/**
648731
* @param {PickleCollectionEntry[]} items The entries
649-
* @param {...PickleObject} prototypes
650732
*/
651-
constructor(items, ...prototypes) {
652-
super(...prototypes);
733+
constructor(items) {
734+
super(FOO);
653735
/**
654736
* @type {PickleCollectionEntry[]}
655737
*/
656738
this.items = items;
657739
}
740+
/**
741+
* Get the item at this index
742+
* @param {number} index
743+
* @returns {PickleObject}
744+
*/
745+
get(index) {
746+
return this.items[index].value;
747+
}
748+
/**
749+
* Finds the value for this key.
750+
* @param {PickleObject} key
751+
* @returns {PickleObject?}
752+
*/
753+
find(key) {
754+
for (var kvp of this.items) {
755+
if (kvp.key === key) return kvp.value;
756+
}
757+
return null;
758+
}
658759
}
659760

660761
class PickleInternalObject extends PickleObject { }
@@ -663,10 +764,9 @@ class PickleExpression extends PickleInternalObject {
663764
static typeName = "expression";
664765
/**
665766
* @param {PickleObject[]} items The items to be evaluated
666-
* @param {...PickleObject} prototypes
667767
*/
668-
constructor(items, ...prototypes) {
669-
super(...prototypes);
768+
constructor(items) {
769+
super(FOO);
670770
/**
671771
* @type {PickleObject[]}
672772
*/
@@ -678,10 +778,9 @@ class PickleScope extends PickleInternalObject {
678778
static typeName = "scope";
679779
/**
680780
* @param {PickleCollection} bindings The bindings
681-
* @param {...PickleObject} prototypes
682781
*/
683-
constructor 10000 (bindings, ...prototypes) {
684-
super(...prototypes);
782+
constructor(bindings) {
783+
super(FOO);
685784
/**
686785
* @type {PickleCollection}
687786
*/
@@ -701,10 +800,9 @@ class PickleExpando extends PickleInternalObject {
701800
static typeName = "expando";
702801
/**
703802
* @param {PickleCollection} expanded The expanded items
704-
* @param {...PickleObject} prototypes
705803
*/
706-
constructor(expanded, ...prototypes) {
707-
super(...prototypes);
804+
constructor(expanded) {
805+
super(FOO);
708806
/**
709807
* @type {PickleCollection}
710808
*/
@@ -717,10 +815,9 @@ class PickleBoundProperty extends PickleInternalObject {
717815
/**
718816
* @param {PickleObject} thisArg The object bound to
719817
* @param {PickleSymbol} property The property key
720-
* @param {...PickleObject} prototypes
721818
*/
722-
constructor(thisArg, property, ...prototypes) {
723-
super(...prototypes);
819+
constructor(thisArg, property) {
820+
super(FOO);
724821
/**
725822
* @type {PickleObject}
726823
*/
@@ -737,10 +834,9 @@ class PicklePropertySetter extends PickleInternalObject {
737834
/**
738835
* @param {PickleSymbol} property The property key
739836
* @param {PickleObject} value The value to set to
740-
* @param {...PickleObject} prototypes
741837
*/
742-
constructor(property, value, ...prototypes) {
743-
super(...prototypes);
838+
constructor(property, value) {
839+
super(FOO);
744840
/**
745841
* @type {PickleSymbol}
746842
*/

0 commit comments

Comments
 (0)
0