@@ -371,6 +371,41 @@ class PickleTokenizer {
371
371
}
372
372
}
373
373
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
+ }
374
409
375
410
/**
376
411
* All objects in Pickle are instances of this.
@@ -380,7 +415,7 @@ class PickleObject {
380
415
* The printable name of the type of the object.
381
416
* @type {string }
382
417
*/
383
- static typeName = "baseobject " ;
418
+ static typeName = "object " ;
384
419
/**
385
420
* Create a new Pickle object.
386
421
* @param {...PickleObject } prototypes
@@ -391,7 +426,7 @@ class PickleObject {
391
426
*/
392
427
this . properties = new Map ( ) ;
393
428
/**
394
- * @type {Map<string , PickleObject> }
429
+ * @type {Map<PickleOperator , PickleObject> }
395
430
*/
396
431
this . operators = new Map ( ) ;
397
432
/**
@@ -457,6 +492,55 @@ class PickleObject {
457
492
}
458
493
}
459
494
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
+
460
544
class PickleSymbol extends PickleObject {
461
545
static typeName = "symbol" ;
462
546
/**
@@ -465,17 +549,19 @@ class PickleSymbol extends PickleObject {
465
549
static _interned = new Map ( ) ;
466
550
/**
467
551
* @param {string } sym The symbol content
468
- * @param {...PickleObject } prototypes
469
552
*/
470
- constructor ( sym , ... prototypes ) {
471
- super ( ... prototypes ) ;
553
+ constructor ( sym ) {
554
+ super ( PickleSymbolPrototype ) ;
472
555
if ( PickleSymbol . _interned . has ( sym ) ) return PickleSymbol . _interned . get ( sym ) ;
473
556
/**
474
557
* @type {string }
475
558
*/
476
559
this . sym = sym ;
477
560
PickleSymbol . _interned . set ( sym , this ) ;
478
561
}
562
+ get length ( ) {
563
+ return this . sym . length ;
564
+ }
479
565
}
480
566
481
567
class PickleString extends PickleObject {
@@ -486,17 +572,19 @@ class PickleString extends PickleObject {
486
572
static _interned = new Map ( ) ;
487
573
/**
488
574
* @param {string } str The string content
489
- * @param {...PickleObject } prototypes
490
575
*/
491
- constructor ( str , ... prototypes ) {
492
- super ( ... prototypes ) ;
576
+ constructor ( str ) {
577
+ super ( FOO ) ;
493
578
if ( PickleString . _interned . has ( str ) ) return PickleString . _interned . get ( str ) ;
494
579
/**
495
580
* @type {string }
496
581
*/
497
582
this . str = str ;
498
583
PickleString . _interned . set ( str , this ) ;
499
584
}
585
+ get length ( ) {
586
+ return this . str . length ;
587
+ }
500
588
}
501
589
502
590
class PickleFloat extends PickleObject {
@@ -507,10 +595,9 @@ class PickleFloat extends PickleObject {
507
595
static _interned = new Map ( ) ;
508
596
/**
509
597
* @param {number } num The number
510
- * @param {...PickleObject } prototypes
511
598
*/
512
- constructor ( num , ... prototypes ) {
513
- super ( ... prototypes ) ;
599
+ constructor ( num ) {
600
+ super ( FOO ) ;
514
601
if ( PickleFloat . _interned . has ( num ) ) return PickleFloat . _interned . get ( num ) ;
515
602
/**
516
603
* @type {number }
@@ -521,14 +608,16 @@ class PickleFloat extends PickleObject {
521
608
}
522
609
523
610
class PickleComplex extends PickleObject {
611
+ static typeName = "complex" ;
524
612
constructor ( ...x ) {
525
- throw 'not supported yet' ;
613
+ throw new PickleError ( 'not supported yet' ) ;
526
614
}
527
615
}
528
616
529
617
class PickleRational extends PickleObject {
618
+ static typeName = "rational" ;
530
619
constructor ( ...x ) {
531
- throw 'not supported yet' ;
620
+ throw new PickleError ( 'not supported yet' ) ;
532
621
}
533
622
}
534
623
@@ -540,10 +629,9 @@ class PickleInteger extends PickleObject {
540
629
static _interned = new Map ( ) ;
541
630
/**
542
631
* @param {BigInt } num The number
543
- * @param {...PickleObject } prototypes
544
632
*/
545
- constructor ( num , ... prototypes ) {
546
- super ( ... prototypes ) ;
633
+ constructor ( num ) {
634
+ super ( FOO ) ;
547
635
if ( PickleInteger . _interned . has ( num ) ) return PickleInteger . _interned . get ( num ) ;
548
636
/**
549
637
* @type {BigInt }
@@ -557,10 +645,9 @@ class PickleErrorObject extends PickleObject {
557
645
static typeName = "error" ;
558
646
/**
559
647
* @param {PickleError } error The thrown error
560
- * @param {...PickleObject } prototypes
561
648
*/
562
- constructor ( error , ... prototypes ) {
563
- super ( ... prototypes ) ;
649
+ constructor ( error ) {
650
+ super ( FOO ) ;
564
651
/**
565
652
* @type {PickleError }
566
653
*/
@@ -573,10 +660,9 @@ class PickleFunction extends PickleObject {
573
660
/**
574
661
* @param {PickleCollection } args The signature
575
662
* @param {PickleString } body The code of the function
576
- * @param {...PickleObject } prototypes
577
663
*/
578
- constructor ( args , body , ... prototypes ) {
579
- super ( ... prototypes ) ;
664
+ constructor ( args , body ) {
665
+ super ( FOO ) ;
580
666
/**
581
667
* @type {PickleCollection }
582
668
*/
@@ -596,10 +682,9 @@ class PickleBuiltinFunction extends PickleObject {
596
682
static typeName = "builtin_function" ;
597
683
/**
598
684
* @param {PickleBuiltinFunctionCallback } fun The callable interface
599
- * @param {...PickleObject } prototypes
600
685
*/
601
- constructor ( fun , ... prototypes ) {
602
- super ( ... prototypes ) ;
686
+ constructor ( fun ) {
687
+ super ( FOO ) ;
603
688
/**
604
689
* @type {PickleBuiltinFunctionCallback }
605
690
*/
@@ -610,11 +695,10 @@ class PickleBuiltinFunction extends PickleObject {
610
695
class PickleStream extends PickleObject {
611
696
static typeName = "stream" ;
612
697
/**
613
- * @param {stream } s The stream
614
- * @param {...PickleObject } prototypes
698
+ * @param {stream } stream The stream
615
699
*/
616
- constructor ( s , ... prototypes ) {
617
- super ( ... prototypes ) ;
700
+ constructor ( stream ) {
701
+ super ( FOO ) ;
618
702
/**
619
703
* @type {stream }
620
704
*/
@@ -627,10 +711,9 @@ class PickleCollectionEntry extends PickleObject {
627
711
/**
628
712
* @param {PickleObject? } key The key value
629
713
* @param {PickleObject } value The value at the key
630
- * @param {...PickleObject } prototypes
631
714
*/
632
- constructor ( key , value , ... prototypes ) {
633
- super ( ... prototypes ) ;
715
+ constructor ( key , value ) {
716
+ super ( FOO ) ;
634
717
/**
635
718
* @type {PickleObject }
636
719
*/
@@ -646,15 +729,33 @@ class PickleCollection extends PickleObject {
646
729
static typeName = "collection" ;
647
730
/**
648
731
* @param {PickleCollectionEntry[] } items The entries
649
- * @param {...PickleObject } prototypes
650
732
*/
651
- constructor ( items , ... prototypes ) {
652
- super ( ... prototypes ) ;
733
+ constructor ( items ) {
734
+ super ( FOO ) ;
653
735
/**
654
736
* @type {PickleCollectionEntry[] }
655
737
*/
656
738
this . items = items ;
657
739
}
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
+ }
658
759
}
659
760
660
761
class PickleInternalObject extends PickleObject { }
@@ -663,10 +764,9 @@ class PickleExpression extends PickleInternalObject {
663
764
static typeName = "expression" ;
664
765
/**
665
766
* @param {PickleObject[] } items The items to be evaluated
666
- * @param {...PickleObject } prototypes
667
767
*/
668
- constructor ( items , ... prototypes ) {
669
- super ( ... prototypes ) ;
768
+ constructor ( items ) {
769
+ super ( FOO ) ;
670
770
/**
671
771
* @type {PickleObject[] }
672
772
*/
@@ -678,10 +778,9 @@ class PickleScope extends PickleInternalObject {
678
778
static typeName = "scope" ;
679
779
/**
680
780
* @param {PickleCollection } bindings The bindings
681
- * @param {...PickleObject } prototypes
682
781
*/
683
- constructor
10000
( bindings , ... prototypes ) {
684
- super ( ... prototypes ) ;
782
+ constructor ( bindings ) {
783
+ super ( FOO ) ;
685
784
/**
686
785
* @type {PickleCollection }
687
786
*/
@@ -701,10 +800,9 @@ class PickleExpando extends PickleInternalObject {
701
800
static typeName = "expando" ;
702
801
/**
703
802
* @param {PickleCollection } expanded The expanded items
704
- * @param {...PickleObject } prototypes
705
803
*/
706
- constructor ( expanded , ... prototypes ) {
707
- super ( ... prototypes ) ;
804
+ constructor ( expanded ) {
805
+ super ( FOO ) ;
708
806
/**
709
807
* @type {PickleCollection }
710
808
*/
@@ -717,10 +815,9 @@ class PickleBoundProperty extends PickleInternalObject {
717
815
/**
718
816
* @param {PickleObject } thisArg The object bound to
719
817
* @param {PickleSymbol } property The property key
720
- * @param {...PickleObject } prototypes
721
818
*/
722
- constructor ( thisArg , property , ... prototypes ) {
723
- super ( ... prototypes ) ;
819
+ constructor ( thisArg , property ) {
820
+ super ( FOO ) ;
724
821
/**
725
822
* @type {PickleObject }
726
823
*/
@@ -737,10 +834,9 @@ class PicklePropertySetter extends PickleInternalObject {
737
834
/**
738
835
* @param {PickleSymbol } property The property key
739
836
* @param {PickleObject } value The value to set to
740
- * @param {...PickleObject } prototypes
741
837
*/
742
- constructor ( property , value , ... prototypes ) {
743
- super ( ... prototypes ) ;
838
+ constructor ( property , value ) {
839
+ super ( FOO ) ;
744
840
/**
745
841
* @type {PickleSymbol }
746
842
*/
0 commit comments