This file is indexed.

/usr/lib/goo/mods/eval/syntax.goo is in goo 0.155-13.

This file is owned by root:root, with mode 0o644.

The actual contents of the file can be viewed below.

  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
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
;;;; Copyright 2002, Jonathan Bachrach.  See file TERMS.

(use goo)
(use goo/boot)
(use goo/cols/lst)

;;  Self-evaluating entities
(dm sexpr-self-evaluating? (exp => <log>)
  (or (isa? exp <num>)
      (isa? exp <chr>)
      (== exp #t)
      (== exp #f)
      (isa? exp <str>)
      (isa? exp <vec>)
      (isa? exp <tup>)))

;;  Our prompt (viz., "EVAL==> ") is a string.
;;  Variables
(dm sexpr-variable? (exp => <log>)
  (isa? exp <sym>))

;;  SPECIAL FORMS (IN GENERAL)

(dm sexpr-tagged-list? (exp tag => <log>)
  (and (isa? exp <lst>) (== (head exp) tag)))

;;  QUOTATIONS

(dv $sexpr-quote-tag 'quote)

(dm sexpr-quoted? (exp => <log>)
  (sexpr-tagged-list? exp $sexpr-quote-tag))

(dm sexpr-make-quote (exp => <lst>)
  (lst $sexpr-quote-tag exp))

(dm sexpr-text-of-quotation (quot => <any>)
  (2nd quot))

;;  ASSIGNMENT--- SET!

(dv $sexpr-set-tag 'set)

(dm sexpr-assignment? (exp => <log>)
  (sexpr-tagged-list? exp $sexpr-set-tag))

(dm sexpr-assignment-variable (assn|<lst> => <any>)
  (2nd assn))

(dm sexpr-assignment-value (assn|<lst> => <any>)
  (3rd assn))

;; VARIABLE

(dm sexpr-variable-name (exp|<lst> => <any>)
  (1st exp))

(dm sexpr-variable-name (exp => <any>)
  exp)

(dm sexpr-variable-type (exp|<lst> => <any>)
  (2nd exp))

(dm sexpr-variable-type (exp => <any>)
  '<Any>)

;; DEFINITIONS -- (DEFINE foo ...)

(dv $sexpr-define-constant-tag 'd.)

(dm sexpr-define-constant? (exp => <log>)
  (sexpr-tagged-list? exp $sexpr-define-constant-tag))

(dv $sexpr-define-tag 'dv)

(dm sexpr-definition? (exp => <log>)
  (sexpr-tagged-list? exp $sexpr-define-tag))

(dv $sexpr-quasiquote-tag 'quasiquote)

(dm sexpr-definition-variable (defn|<lst> => <any>)
  (2nd defn))

(dm sexpr-definition-value (defn => <any>)
  (3rd defn))

(dv $sexpr-define-syntax-tag 'ds)

(dm sexpr-syntax-definition? (exp => <log>)
  (sexpr-tagged-list? exp $sexpr-define-syntax-tag))

(dv $sexpr-macro-tag 'mac)

(dm sexpr-syntax-definition-variable (defn|<lst> => <sym>)
  (2nd defn))

(dm sexpr-syntax-definition-value (defn => <any>)
  (def var (2nd defn))
  ;; (ds foo (p ...) b...)
  `(,$sexpr-macro-tag (,var ,@(3rd defn)) ,@(tail (tail (tail defn)))))

(dv $sexpr-define-function-tag 'df)

(dm sexpr-function-definition? (exp => <log>)
  (sexpr-tagged-list? exp $sexpr-define-function-tag))

(dv $sexpr-define-method-tag 'dm)

(dm sexpr-method-definition? (exp => <log>)
  (sexpr-tagged-list? exp $sexpr-define-method-tag))

(dv $sexpr-define-generic-tag 'dg)

(dm sexpr-generic-definition? (exp => <log>)
  (sexpr-tagged-list? exp $sexpr-define-generic-tag))

;; (df foo (x ...) form ...)
;; (df (foo adjectives ...) (x ...) form ...)
(dm sexpr-function-definition-variable (defn|<lst> => <any>)
  (def var (2nd defn))
  (if (isa? var <lst>)
      (head var)
      var))

(dm sexpr-function-definition-adjectives (defn|<lst> => <any>)
  (def var (2nd defn))
  (if (isa? var <lst>)
      (tail var)
      '()))

(dm sexpr-forward-primitive? (defn|<lst> => <log>)
  (= (len defn) 2))

(dm sexpr-signature-parameters (signature|<lst> => <lst>)
  (def values-spec-index (pos signature '=>))
  (if values-spec-index
      (sub signature 0 values-spec-index)
      signature))

(dm sexpr-value-type (x|<lst> => <any>) (2nd x))

(dm sexpr-value-type (x|<any> => <any>) x)

(dm sexpr-signature-value (signature|<lst> => <any>)
  (def values-spec-index (pos signature '=>))
  (and values-spec-index
       (let ((value (elt signature (+ values-spec-index 1))))
         (if (sexpr-tagged-list? value 'tup)
             `(t* ,@(map sexpr-value-type (tail value)))
             value))))
              
(df as-signature (sig|<lst> => <lst>)
  sig)
              
(dm sexpr-function-signature (defn => <lst>)
  (as-signature (3rd defn)))

(dm sexpr-function-parameters (defn => <lst>)
  (sexpr-signature-parameters (sexpr-function-signature defn)))

(dm sexpr-function-value (defn => <any>)
  (sexpr-signature-value (sexpr-function-signature defn)))

(dm sexpr-function-body (defn => <any>)
  (tail (tail (tail defn))))

(dv $sexpr-method-tag 'fun)

;; (df foo (x ...) form ...)
(dm sexpr-function-definition-value (defn => <any>)
  (line-pair $sexpr-method-tag 
             (pair (3rd defn) (tail (tail (tail defn))))
             (line-of defn)))

;;  METHOD -- (METHOD (p...) b...)

(dm sexpr-method? (exp => <log>)
  (sexpr-tagged-list? exp $sexpr-method-tag))

(dm sexpr-method-signature (method-exp|<lst> => <lst>)
  (as-signature (2nd method-exp)))

(dm sexpr-method-parameters (method-exp|<lst> => <lst>)
  (sexpr-signature-parameters (sexpr-method-signature method-exp)))

(dm sexpr-method-value (method-exp|<lst> => <any>)
  (sexpr-signature-value (sexpr-method-signature method-exp)))

(dm sexpr-method-body (method-exp|<lst> => <lst>)
  (tail (tail method-exp)))

;; TODO: RENAME LIST TO LST

(dm sexpr-make-anonymous-method 
    (parameters|<lst> body|<lst> => <lst>)
  (line-list-of (if (isa? parameters <line-list>) parameters body)
    (cat (lst $sexpr-method-tag) (lst parameters) body)))

(dm sexpr-make-method (name parameters|<lst> body|<lst> => <lst>)
  (line-list-of (if (isa? parameters <line-list>) parameters body)
    (cat (lst $sexpr-define-method-tag name parameters) body)))

;;  IF CONDITIONALS

(dv $sexpr-if-tag 'if)

(dm sexpr-if? (exp => <log>)
  (sexpr-tagged-list? exp $sexpr-if-tag))

(dm sexpr-if-test (exp|<lst> => <any>)
  (2nd exp))

(dm sexpr-if-then (exp|<lst> => <any>)
  (3rd exp))

(dm sexpr-if-else (exp|<lst> => <any>)
  (if (nul? (tail (tail (tail exp)))) ;;cdddr
      #f
      (head (tail (tail (tail exp)))))) ;;cadddr

(dm sexpr-make-if (test then else => <lst>)
  (lst $sexpr-if-tag test then else))

;;  BLOCK

(dv $sexpr-bind-exit-tag 'esc)

(dm sexpr-block? (exp => <log>)
  (sexpr-tagged-list? exp $sexpr-bind-exit-tag))

;; TODO: perhaps second should be 2nd

(dm sexpr-block-name (exp|<lst> => <any>)
  (2nd exp))

(dm sexpr-block-body (exp|<lst> => <lst>)
  (tail (tail exp)))

;;  UNWIND-PROTECT

(dv $sexpr-unwind-protect-tag 'fin)

(dm sexpr-unwind-protect? (exp => <log>)
  (sexpr-tagged-list? exp $sexpr-unwind-protect-tag))

(dm sexpr-unwind-protect-protected-form (exp|<lst> => <any>)
  (2nd exp))

(dm sexpr-unwind-protect-cleanup-forms (exp|<lst> => <any>)
  (tail (tail exp)))

;;  MONITOR

(dv $sexpr-monitor-tag 'try)

(dm sexpr-monitor? (exp => <log>)
  (sexpr-tagged-list? exp $sexpr-monitor-tag))

(dm sexpr-monitor-expand (exp|<lst>)
  (mif ((TRY ,options ,handler ,@body) exp)
       (let ((options (if (isa? options <sym>) `((type ,options)) options)))
         (tup (sexpr-monitor-type options)
              (sexpr-monitor-info options)
              (sexpr-monitor-test options)
              handler
              (sexpr-monitor-body body)))
       (syntax-error "Malformed TRY expression: ~=" exp)))

;;; Find an option.  Given '((foo 1) (bar 2)) and 'foo, return '(1).
(df find-option (options|<lst> option|<sym> default => <any>)
  (cond
    ((nul? options) default)
    ((== option (head (1st options))) (tail (1st options)))
    (#t (find-option (tail options) option default))))

(dm sexpr-monitor-type (options|<lst> => <any>)
  (def type-list (find-option options 'type '(<condition>)))
  (mif ((,type) type-list)
       type
       (syntax-error "Malformed TRY condition type: %=" type-list)))

;; Build an expression which returns an appropriate <handler-info>.
(dm sexpr-monitor-info (options|<lst> => <any>)
  (def info        (find-option options 'info #f))
  (def description (find-option options 'description #f))
  (cond
    ((and info description)
     (syntax-error "TRY may only have one of INFO or DESCRIPTION: %=" options))
    ;; TODO - Do we want to keep the 'info' option?
    (info
     (mif ((,info-expr) info)
          info-expr
          (syntax-error "Malformed TRY info expression: %=" info)))
    (description
     (mif ((,message ,@arguments) description)
          `(new <simple-handler-info>
             handler-info-message ,message
             handler-info-arguments (lst ,@arguments))
          (syntax-error "Malformed TRY description: %=" description)))
    (#t '$default-handler-info)))

(dm sexpr-monitor-test (options|<lst> => <lst>)
  (def test-body (find-option options 'test '(#t)))
  (sexpr-make-anonymous-method '() test-body))

(dm sexpr-monitor-body (body|<lst> => <lst>)
  (sexpr-make-anonymous-method '() body))

;;  BEGIN expressions (a.k.a. sequences)

(dv $sexpr-begin-tag 'seq)

(dm sexpr-begin? (exp => <log>)
  (sexpr-tagged-list? exp $sexpr-begin-tag))

(dm sexpr-begin-actions (begin-exp => <lst>)
  (tail begin-exp))

(dm sexpr-last-exp? (seq|<lst> => <log>)
  (nul? (tail seq)))

(dm sexpr-first-exp (seq|<lst> => <any>)
  (head seq))

(dm sexpr-rest-exps (seq|<lst> => <lst>)
  (tail seq))

(dm sexpr-sequence->begin (seq|<lst> => <any>)
  (if (nul? seq)
      seq
      (if (sexpr-last-exp? seq)
          (sexpr-first-exp seq)
          (sexpr-make-begin seq))))

(dm sexpr-make-begin (exp|<lst> => <lst>)
  (pair $sexpr-begin-tag exp))

;;  Procedure applications -- NO-ARGS? and LAST-OPERAND? added
(dm sexpr-application? (exp => <log>)
  (and (isa? exp <lst>) (not (nul? exp))))

(dm sexpr-operator (app|<lst> => <any>)
  (head app))

(dm sexpr-operands (app|<lst> => <lst>)
  (tail app))

(dm sexpr-no-operands? (args|<lst> => <log>)
  (nul? args))

(dm sexpr-first-operand (args|<lst> => <any>)
  (head args))

(dm sexpr-rest-operands (args|<lst> => <lst>)
  (tail args))

(dm sexpr-make-application (f operands|<lst> => <lst>)
  (cat (lst f) operands))

(dv *unique-name-counter* 0)

(dm sexpr-new-unique-name-counter! (=> <int>)
  ;; without-interrupts
  (set *unique-name-counter* (+ *unique-name-counter* 1))
  *unique-name-counter*)

(dv <name> <any>) ;; TODO: (union <str> <sym>)

(dm sexpr-unique-name (prefix|<name> => <sym>)
  (as <sym> (cat (as <str> prefix)
                 (num-to-str (sexpr-new-unique-name-counter!)))))

;;  LET -- (let ((var value) ...) form ...)
;;         var:        simple-var | (tup tup-var ...)
;;         tup-var:    simple-var | (name "...")
;;         simple-var: name | (name type)

(dv $sexpr-def-tag 'def)

(dm sexpr-def? (exp => <log>)
  (sexpr-tagged-list? exp $sexpr-def-tag))

(dm sexpr-def-variable (exp|<lst>) (2nd exp))

(dm sexpr-def-value (exp|<lst>) (3rd exp))

(dv $sexpr-let-tag 'let)

(dm sexpr-let? (exp => <log>)
  (sexpr-tagged-list? exp $sexpr-let-tag))

(dm sexpr-let-bound-variables (let-exp|<lst> => <lst>)
  (map (fun (x) (1st (as-signature x))) (2nd let-exp))))

(dm sexpr-let-values (let-exp|<lst> => <lst>)
  (map (fun (x) (2nd (as-signature x))) (2nd let-exp)))

(dm sexpr-let-body (let-exp|<lst> => <any>)
  (sexpr-sequence->begin (tail (tail let-exp))))

(dm sexpr-let-names+values
    (names|<lst> vals|<lst> => (tup names|<lst> values|<lst>))
  (rep loop ((rnames '()) (rvalues '()) (names names) (vals vals))
    (if (nul? names)
        (tup (rev! rnames) (rev! rvalues))
        (let ((name  (head names))
              (value (head vals)))
          (if (and (isa? name <lst>) (== (head name) 'tup))
              (let ((tupname
                     (sexpr-unique-name "tup"))
                    ((tup rtnames rtvalues)
                     (rep inner
                         ((rtnames (lst tupname)) (rtvalues (lst value))
                          (index 0) (tnames (tail name)))
                       (if (nul? tnames)
                           (tup rtnames rtvalues)
                           (let ((name (head tnames)))
                             (if (and (isa? name <lst>) 
                                      (== (head (tail name)) '...))
                                 (inner (pair (lst (head name) '<lst>)
                                              rtnames)
                                        (pair (lst 'sub tupname index 
                                                   (lst 'len tupname)) 
                                              rtvalues)
                                        (+ index 1) (tail tnames))
                                 (inner (pair name rtnames)
                                        (pair (lst 'elt tupname index)
                                              rtvalues)
                                        (+ index 1) (tail tnames))))))))
                (loop (cat rtnames rnames) (cat rtvalues rvalues)
                      (tail names) (tail vals)))
              (loop (pair name rnames) (pair value rvalues)
                    (tail names) (tail vals)))))))

(dm sexpr-let->combination (let-exp|<lst> => <any>)
  (def onames             (sexpr-let-bound-variables let-exp))
  (def ovalues            (sexpr-let-values let-exp))
  (def body               (sexpr-let-body let-exp))
  (def (tup names values) (sexpr-let-names+values onames ovalues))
  (rep col ((names names) (values values))
    (if (nul? names)
        body
        (pair (lst $sexpr-method-tag (lst (head names))
                   (col (tail names) (tail values)))
              (lst (head values))))))

;;  LOC -- (loc ((?var (?param ...) ?body) ...) ?body)

(dv $sexpr-locals-tag 'loc)

(dm sexpr-loc? (exp => <log>)
  (sexpr-tagged-list? exp $sexpr-locals-tag))

(dm sexpr-loc-bound-names (loc-exp|<lst> => <lst>)
  (map 1st (2nd loc-exp)))

(dm sexpr-loc-bound-signatures (loc-exp|<lst> => <lst>)
  (map (fun (x) (as-signature (2nd x))) (2nd loc-exp)))

;; COMPOSE DEFINED

(dm sexpr-loc-bound-bodies (loc-exp|<lst> => <lst>)
  (map (compose tail tail) (2nd loc-exp)))

(dm sexpr-loc-raw-body (loc-exp|<lst> => <lst>)
  (tail (tail loc-exp)))

(dm sexpr-loc-body (loc-exp|<lst> => <any>)
  (sexpr-sequence->begin (sexpr-loc-raw-body loc-exp)))

(dv $sexpr-iterate-tag 'rep)

;; ITERATE expressions (REP ?name ((?var ?init) ...) ?body)
(dm sexpr-iterate? (exp => <log>)
  (sexpr-tagged-list? exp $sexpr-iterate-tag))

(dm sexpr-iterate-name (rep-exp|<lst> => <sym>)
  (2nd rep-exp))

(dv $sexpr-values-tag '=>)

(dm sexpr-iterate-signature (rep-exp|<lst> => <lst>)
  (def sig (3rd rep-exp))
  (cat (map (fun (x) (1st (as-signature x)))
            (sexpr-signature-parameters sig))
       (if (sexpr-signature-value sig)
           '()
           (lst $sexpr-values-tag))
       (lst (sexpr-signature-value sig))))

(dm sexpr-iterate-inits (rep-exp|<lst> => <lst>)
  (map (fun (x) (2nd (as-signature x))) (3rd rep-exp)))

(dm sexpr-iterate-body (rep-exp|<lst> => <lst>)
  (tail (tail (tail rep-exp))))

(dm sexpr-iterate->loc (rep-exp|<lst> => <lst>)
  (def name  (sexpr-iterate-name rep-exp))
  (def sig   (sexpr-iterate-signature rep-exp))
  (def body  (sexpr-iterate-body rep-exp))
  (def inits (sexpr-iterate-inits rep-exp))
  (line-list-of rep-exp
    (lst $sexpr-locals-tag
         (lst (cat (lst name sig) body))
         (cat (lst name) inits))))

;;  ISA

(dv $sexpr-isa-tag 'isa)

(dm sexpr-isa? (exp => <log>)
  (sexpr-tagged-list? exp $sexpr-isa-tag))

(dm sexpr-isa-parent (exp|<lst> => <any>)
  (2nd exp))

(dm sexpr-isa-prop-inits (exp|<lst> => <lst>)
  (tail (tail exp)))

(dm sexpr-isa-init-props (exp|<lst> => <lst>)
  (map 2nd exp))

(dm sexpr-isa-init-values (exp|<lst> => <lst>)
  (map 3rd exp))

;;  DC

(dv $sexpr-define-class-tag 'dc)

(dm sexpr-define-class? (exp => <log>)
  (sexpr-tagged-list? exp $sexpr-define-class-tag))

(dm sexpr-define-class-parents (exp|<lst> => <lst>)
  (3rd exp))

;;  PROP

;; (dp point-x (<point> => <int>) 0)

(dv $sexpr-prop-tag 'dp)

(dv $sexpr-prop!-tag 'dp!)

(dm sexpr-prop!? (exp => <log>)
  (sexpr-tagged-list? exp $sexpr-prop!-tag))

(dm sexpr-prop? (exp => <log>)
  (sexpr-tagged-list? exp $sexpr-prop-tag))

(dm sexpr-prop-name (exp|<lst> => <sym>)
  (2nd exp))

(dm sexpr-prop-owner (exp|<lst> => <any>)
  (def param (1st (sexpr-function-parameters exp)))
  (if (isa? param <lst>)
      (2nd param)
      param))

(dm sexpr-prop-init-var (exp|<lst> => <any>)
  (def param (1st (sexpr-function-parameters exp)))
  (if (isa? param <lst>)
      (1st param)
      '_x))

(dm sexpr-prop-type (exp|<lst> => <any>)
  (sexpr-function-value exp))

(dm sexpr-prop-init? (exp|<lst> => <log>)
  (= (len exp) 4))

(dm sexpr-prop-init (exp|<lst> => <any>)
  (elt exp 3))

(dm sexpr-fab-getter (name type line => <lst>)
  (sexpr-make-method 
   name (line-list line (lst '_x type)) 
   (lst (lst 'prop-value '_x name))))

(dm sexpr-fab-setter (name type prop-type line => <lst>)
  (sexpr-make-method
    (sexpr-fab-setter-name name)
    (line-list line (lst '_z prop-type) (lst '_x type))
    (lst (lst 'prop-value-setter '_z '_x name))))

(dm sexpr-fab-setter-name (name => <sym>)
  (cat-sym name "-setter"))

;; BACKQUOTE

(dm sexpr-macro? (exp => <log>)
  (sexpr-tagged-list? exp $sexpr-macro-tag))

(dv $sexpr-macro-expand-tag 'macro-expand)

(dm sexpr-macro-expand? (exp => <log>)
  (sexpr-tagged-list? exp $sexpr-macro-expand-tag))

(dv $sexpr-syntax-if-tag 'mif)

(dm sexpr-make-syntax-if 
    (pat exp then else => <lst>)
  (lst $sexpr-syntax-if-tag (lst pat exp) then else))

;; (dv *comma*          'unquote)
(dv *comma-atsign*   'comma-atsign)
(dv *bq-list*        'bq-list)
(dv *bq-list**       'bq-list*)
(dv *bq-append*      'bq-append)
(dv *bq-nconc*       'bq-nconc)
(dv *bq-clobberable* 'bq-clobberable)
(dv *bq-quote*       'bq-quote)

(df sexpr-expand-backquote (x|<lst> => <lst>)
  (eval (sexpr-build-backquote-expander x) 'goo))

(dm sexpr-build-backquote-expander (x|<lst> => <lst>)
  (def raw-result (bq-process x))
  (def res        (bq-remove-tokens raw-result))
  ;; (msg-out "RES = %=\n" res)
  res)

(dm sexpr-unquote? (x => <log>)
  (sexpr-tagged-list? x 'unquote))

(dm sexpr-unquote-splicing? (x => <log>)
  (sexpr-tagged-list? x 'unquote-splicing))

(dm sexpr-backquote? (x => <log>)
  (sexpr-tagged-list? x $sexpr-quasiquote-tag))

(dm sexpr-atom? (x => <log>)
  (or (not (isa? x <lst>)) (nul? x)))

(dm nreconc (x|<lst> y|<lst> => <lst>)
  (cat! (rev! x) y))

(dm bq-process (x => <any>)
  (cond ((sexpr-atom? x)
         (lst *bq-quote* x))
        ((sexpr-backquote? x)
         (bq-process (sexpr-build-backquote-expander (head (tail x)))))
        ((sexpr-unquote? x)
         (head (tail x)))
        ((sexpr-unquote-splicing? x)
         (syntax-error "UNQUOTE-SPLICING %= AFTER BQ" (head (tail (x)))))
        (#t
         (esc return
           (rep loop ((p x) (q '()))
             (if (sexpr-atom? p)
                 (return (pair *bq-append*
                               (nreconc q (lst (lst *bq-quote* p)))))
                 (seq (when (sexpr-unquote? p)
                        (unless (nul? (tail (tail p)))
                          (syntax-error "Malformed ,%=" p))
                        (return 
                         (pair *bq-append* 
                               (nreconc q (lst (head (tail p)))))))
                      (when (== (tail p) *comma-atsign*)
                        (syntax-error "Dotted ,@%=" p))
                      (loop (tail p) (pair (bracket (head p)) q)))))))))

(dm bracket (x => <any>)
  (cond ((sexpr-atom? x)
         (lst *bq-list* (bq-process x)))
        ((sexpr-unquote? x)
         (lst *bq-list* (head (tail x))))
        ((sexpr-unquote-splicing? x)
         (lst *bq-clobberable* (head (tail x))))
        (#t
         (lst *bq-list* (bq-process x)))))

(dm map-tree (fn|<fun> x => <any>)
  (if (sexpr-atom? x)
      (fn x)
      (let ((a (fn (head x)))
            (d (map-tree fn (tail x))))
        (if (and (= a (head x)) (= d (tail x)))
            x
            (pair a d)))))

(dm bq-remove-tokens (x)
  (cond ((== x *bq-list*)   'lst)
        ((== x *bq-append*) 'cat)
        ((== x *bq-nconc*)  'cat!)
        ((== x *bq-list**)  'list*)
        ((== x *bq-quote*)  'quote)
        ((sexpr-atom? x)    x)
        ((== (head x) *bq-clobberable*)
         (bq-remove-tokens (head (tail x))))
        ((and (== (head x) *bq-list**)
              (not (nul? (tail (tail x))))
              (nul? (tail (tail (tail x)))))
         (pair 'pair (map-tree bq-remove-tokens (tail x))))
        (#t
         (map-tree bq-remove-tokens x))))

;;/ SEXPR-IF -- (sexpr-if (pattern expression) then [ else ])

(dm sexpr-syntax-if? (exp => <log>)
  (sexpr-tagged-list? exp $sexpr-syntax-if-tag))

(dm sexpr-syntax-if-value (exp|<lst> => <any>)
  (2nd (2nd exp)))

(dm sexpr-syntax-if-pattern (exp|<lst> => <any>)
  (1st (2nd exp)))

(dm sexpr-syntax-if-then (exp|<lst> => <any>)
  (3rd exp))

;; TODO: DEFAULT ON ELT

(dm sexpr-syntax-if-else (exp|<lst> => <any>)
  (elt-or exp 3 #f))

(dm sexpr-pattern-variable? (pat => <log>)
  (or (sexpr-unquote? pat) (sexpr-unquote-splicing? pat)))

(dm sexpr-pattern-variable-name (pat|<lst> => <any>)
  (2nd pat))

(dm sexpr-bind-pattern-variables (pat|<lst> => <lst>)
  (del-dups
    (rep col ((pat pat))
      (if (or (not (isa? pat <lst>)) (nul? pat))
          '()
          (let ((x (head pat)))
            (if (sexpr-pattern-variable? x)
                (pair (sexpr-pattern-variable-name x) (col (tail pat)))
                (cat (col (head pat)) (col (tail pat)))))))))

;; MACROS

;;; TODO: GENSYM 'EXP
(dm sexpr-make-macro-function (name exp|<lst> => <any>)
  (def mif  (sexpr-make-syntax-if
             (sexpr-method-parameters exp) 'exp
             (sexpr-sequence->begin (sexpr-method-body exp)) 
             `(macro-error ',name exp)))
  (def func (sexpr-make-anonymous-method (lst 'exp) (lst mif)))
  func)

(need-implementation
  eval)

(export
  sexpr-assignment-value
  sexpr-assignment-variable
  $sexpr-begin-tag
  $sexpr-bind-exit-tag
  sexpr-bind-pattern-variables
  sexpr-block-body
  sexpr-block-name
  $sexpr-define-function-tag
  $sexpr-define-generic-tag
  $sexpr-define-method-tag
  $sexpr-define-syntax-tag
  $sexpr-define-class-tag
  sexpr-define-class?
  sexpr-define-class-parents
  $sexpr-define-tag
  $sexpr-define-constant-tag
  sexpr-definition-value
  sexpr-definition-variable
  sexpr-expand-backquote
  sexpr-forward-primitive?
  sexpr-function-body
  sexpr-function-definition-value
  sexpr-function-definition-variable
  sexpr-function-definition-adjectives
  sexpr-function-signature
  sexpr-if-else
  $sexpr-if-tag
  sexpr-if-test
  sexpr-if-then
  sexpr-isa-init-props
  sexpr-isa-init-values
  sexpr-isa-parent
  sexpr-isa-prop-inits
  $sexpr-isa-tag
  sexpr-iterate->loc
  $sexpr-iterate-tag
  $sexpr-def-tag
  sexpr-def?
  sexpr-def-variable
  sexpr-def-value
  $sexpr-let-tag
  sexpr-let->combination
  sexpr-loc-bound-bodies
  sexpr-loc-bound-names
  sexpr-loc-bound-signatures
  sexpr-loc-raw-body
  $sexpr-locals-tag
  $sexpr-macro-expand-tag
  sexpr-make-anonymous-method
  sexpr-make-application
  sexpr-make-begin
  sexpr-fab-getter
  sexpr-make-macro-function
  sexpr-make-method
  sexpr-fab-setter
  sexpr-fab-setter-name
  sexpr-method-body
  sexpr-method-signature
  $sexpr-method-tag
  sexpr-monitor-expand
  $sexpr-monitor-tag
  sexpr-operands
  sexpr-operator
  $sexpr-quasiquote-tag
  $sexpr-quote-tag
  sexpr-sequence->begin
  $sexpr-set-tag
  sexpr-signature-parameters
  sexpr-signature-value
  sexpr-prop-init-var
  sexpr-prop-init?
  sexpr-prop-init
  sexpr-prop-name
  sexpr-prop-owner
  sexpr-prop-type
  $sexpr-prop-tag
  $sexpr-prop!-tag
  sexpr-syntax-definition-value
  sexpr-syntax-definition-variable
  sexpr-syntax-if-else
  sexpr-syntax-if-pattern
  $sexpr-syntax-if-tag
  sexpr-syntax-if-then
  sexpr-syntax-if-value
  sexpr-text-of-quotation
  sexpr-unquote?
  sexpr-unquote-splicing?
  sexpr-unwind-protect-cleanup-forms
  sexpr-unwind-protect-protected-form
  $sexpr-unwind-protect-tag
  sexpr-variable?
  sexpr-variable-name
  sexpr-variable-type)