-
Notifications
You must be signed in to change notification settings - Fork 0
/
rscheme.clj
1086 lines (978 loc) · 31.5 KB
/
rscheme.clj
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
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
;; Copyright (c) Andrzej Radecki. All rights reserved.
;; The use and distribution terms for this software are covered by the
;; Common Public License 1.0 (http://opensource.org/licenses/cpl.php)
;; which can be found in the file CPL.txt at the root of this distribution.
;; By using this software in any fashion, you are agreeing to be bound by
;; the terms of this license.
;; You must not remove this notice, or any other, from this software.
;;
;; File: rscheme.clj
;;
;; rscheme.clj contains an implementation of a reader and an evaluator
;; of a simple, scheme-like language. It can be used as a sand-box
;; (an isolated and restricted execution environment) for interpreting
;; scripts that if executed directly by clojure could pose a security
;; risk to the system.
;;
;; Note that this software was neither developed for conformance with the R5RS
;; standard nor for the execution efficiency.
;;
;; Functions:
;; 'eval' - scheme evaluator (stub)
;; 'read' - scheme reader
;; 'repl' - Read-Eval-Print-Loop function of the scheme interpreter
;;
;; (gmail) nrdwrdck
;; 09 May 2008
;;
(in-ns 'rscheme)
(clojure/refer 'clojure :exclude '(eval read))
(def #^{:private true} stack-depth 100)
(def #^{:private true} trace 0)
(def #^{:private true} eval)
(def #^{:private true} print-exp)
(defn- make-number [n]
{:type :number :val n})
(defn- make-bool [n]
{:type :bool :val n})
(defn- make-true
{:type :bool :val true})
(defn- make-false
{:type :bool :val false})
(defn- make-list [l]
{:type :list :val l})
(defn- make-nil []
(make-list nil))
(defn- is-symbol? [t]
(= (:type t) :symbol))
(defn- is-list? [t]
(= (:type t) :list))
(defn- is-nil? [l]
(and (= (:type l) :list) (nil? (:val l))))
(defn- with-env [val env]
(assoc val :env env))
(defn- val-op
"Performs an operation op on field :val of a map mp
and return the modified map"
[mp op]
(let [inner (:val mp)]
(assoc mp :val (op inner))))
(defn- val-conj
[mp val]
(val-op mp (fn [inner] (conj inner val))))
(defn- val-first [mp]
(val-op mp first))
(defn- val-rest [mp]
(val-op mp rest))
(defn- val-frest [mp]
(val-op mp frest))
(defn- val-rrest [mp]
(val-op mp rrest))
(defn- prim_add
[& xs]
(make-number (apply + (map :val xs))))
(defn- prim_sub
[& xs]
(make-number (apply - (map :val xs))))
(defn- prim_mul
[& xs]
(make-number (apply * (map :val xs))))
(defn- prim_div
[& xs]
(make-number (apply / (map :val xs))))
(defn- prim_gt
[& xs]
(make-bool (apply > (map :val xs))))
(defn- prim_lt
[& xs]
(make-bool (apply < (map :val xs))))
(defn- prim_eq
[& xs]
(make-bool (apply = (map :val xs))))
(defn- prim_display-helper
[int? first? & xs]
;;(println first? xs)
(if (nil? xs)
(make-nil)
(do (if (not first?)
(print " "))
(print-exp int? (first xs))
(recur int? false (rest xs)))))
(defn- prim_display
[& xs]
(apply prim_display-helper false true xs))
(defn- prim_write
[& xs]
(apply prim_display-helper true true xs))
(defn- frrest [l] (-> l rrest first))
(defn- frrrest [l] (-> l rrest rest first))
(defn- pos-string [ann]
(str (:fname ann) ":" (:line ann) ":" (:col ann)))
(defn- indent
"Displays lvl consequtive [char] characters"
[char lvl]
(when (pos? lvl)
(do (print char) (recur char (dec lvl)))))
(defn- print-bool
[val]
(if val
(print "#t")
(print "#f")))
(defn- print-number
[val]
(print val))
(defn- print-char
[int? val]
(if int?
(pr val)
(print val)))
(defn- print-string
[int? val]
(if int?
(pr val)
(print val)))
(defn- print-symbol
[val]
(print (name val)))
(defn- print-list
([val]
(print-list val true))
([val first?]
(if first? (print "("))
(if (nil? val)
(print ")")
(do (if (not first?)
(print " "))
(print-exp true (first val))
(recur (rest val) false)))))
(defn- print-exp
[int? exp]
(when (map? exp)
(let [type (:type exp) val (:val exp)]
;;(println type val)
(cond
(= type :bool) (print-bool (:val exp))
(= type :number) (print-number (:val exp))
(= type :char) (print-char int? (:val exp))
(= type :string) (print-string int? (:val exp))
(= type :symbol) (print-symbol (:val exp))
(= type :list) (print-list (:val exp))))))
(defn- extend-environment
[env arg-list param-list]
(cond (and (nil? arg-list) (nil? param-list))
env
(or (= (count arg-list) 0) (= (count param-list) 0))
nil
:else
(recur (assoc env (:val (first arg-list)) (first param-list))
(rest arg-list) (rest param-list))))
(defn- extend-env
[env arg-list param-list def-ok? tail?]
(extend-environment
{:parent env :level (+ 1 (:level env)) :tail? tail? :def-ok? def-ok?}
arg-list param-list))
(defn- make-env [env def-ok? tail?]
(extend-env env nil nil def-ok? tail?))
(defn- eval-sequence
"Eval a list of forms. Tail recursive.
\"define's\" are only allowed at the beginning of the list."
([exps env]
(eval-sequence exps env (with-env (make-nil) env) true))
([exps env res def-ok]
(if (nil? exps)
res
(let [res (eval (first exps)
(assoc env
:def-ok? def-ok
:tail? (nil? (rest exps))))
env (:env res)
def-ok (:definition res)]
(recur (rest exps) env res def-ok)))))
(defn- eval-sequence-top
"Eval a top-level list of forms.
\"define's\" are always allowed.
Not tail recursive (is it necessary(?))"
([exps env]
(eval-sequence-top exps env (with-env (make-nil) env)))
([exps env res]
(if (nil? exps)
res
(let [res (eval (first exps) env)
env (:env res)]
(recur (rest exps) env res)))))
(defn- eval-symbol
([exp env]
(let [name (:val exp)]
(eval-symbol exp env env name)))
([exp env-orig env var]
(if (contains? env var)
(with-env (get env var) env-orig)
(if (nil? (:parent env))
(do (println
(str "Unbound variable " (:val exp) ", "
(pos-string (:start exp))))
(with-env (make-nil) env-orig))
(recur exp env-orig (:parent env) var)))))
(defn- eval-quote
[exp env]
(when (> (count (:val exp)) 2)
(let [ann (:start exp)]
(println
(str "Unused arguments to \"quote\", "
(pos-string ann)))))
(with-env (frest (:val exp)) env))
(defn- eval-assign
[exp env]
)
(defn- eval-if
"add error checking"
[exp env]
(let [vec (:val exp) cond (frest vec) pred (frrest vec) alt (frrrest vec)]
;;(let [c (eval cond (make-env env false true))]
(let [c (eval cond (assoc env :def-ok? false :tail? true))]
(if (:val c)
;;(eval pred (make-env env false true))
(eval pred (assoc env :def-ok? false :tail? true))
(if (nil? alt)
(with-env (make-nil) env)
;;(eval alt (make-env env false true)))))))
(eval alt (assoc env :def-ok false :tail? true)))))))
(defn- eval-cond
[exp env]
)
(defn- parse-formals-list
([lst exp]
(parse-formals-list lst exp (list) nil))
([lst exp res res2]
(if (nil? lst)
{:arg-list (reverse res) :arg-rest (reverse res2)}
(if (is-symbol? (first lst))
(recur (rest lst) exp (cons (first lst) res) res2)
(println
(str "Invalid symbol in the argument list "
(pos-string (:start exp))))))))
(defn- parse-formals
([var exp env]
(cond (= (:type var) :symbol)
{:arg-rest var :arg-list nil}
(= (:type var) :list)
(parse-formals-list (:val var) exp)
:else
(println
(str "Invalid argument list "
(pos-string (:start exp)))))))
(defn- eval-lambda
[exp env]
(let [vec (:val exp) var (frest vec)
body (rrest vec)]
;;body (subvec vec 2)]
(if (or (nil? var) (nil? body))
(do (println
(str
"Not enough arguments to \"lambda\" "
(pos-string (:start exp))))
(with-env (make-nil) env))
(let [formals (parse-formals var exp env)
arg-list (:arg-list formals)
arg-rest (:arg-rest formals)]
(if (nil? formals)
(with-env (make-nil) env)
(with-env {:body body :arg-list arg-list
:arg-rest arg-rest :type :fun
:start (:start exp) :end (:end exp)}
env))))))
(defn- eval-define
[exp env]
(let [vec (:val exp) var (frest vec) val-exp (frrest vec)]
(cond
;;value
(and (= (:type var) :symbol)
(not (nil? val-exp)))
(let [res (eval val-exp (make-env env false true))
val (dissoc res :env)
env (:parent (:env res))]
(with-env res (assoc env (:val var) val)))
;;procedure
(and (= (:type var) :list)
(= (:type (first (:val var))) :symbol)
(not (nil? val-exp)))
(let [sym (first (:val var))
;;args (subvec (:val var) 1)
;;body (subvec vec 2)
args (rest (:val var))
body (rrest vec)
formals (parse-formals-list args var)
arg-list (:arg-list formals)
arg-rest (:arg-rest formals)
res {:body body :arg-list arg-list
:arg-rest arg-rest :type :fun
:start (:start exp) :end (:end exp)}]
(if (nil? formals)
(with-env (make-nil) env)
(with-env res (assoc env (:symbol sym) res))))
;;unknown
:else
(let [ann (:start var)]
(println
(str "Invalid format of \"define\" form "
(pos-string ann)))
(with-env (make-nil) env)))))
(defn- eval-begin
[exp env]
)
(defn- eval-and
[exp env]
)
(defn- eval-or
[exp env]
)
(defn- eval-arguments
([exps env]
(eval-arguments exps env (list)))
([exps env lst]
(if (nil? exps)
(reverse lst)
;;(let [res (eval (first exps) (make-env env false false))
(let [res (eval (first exps) (assoc env :def-ok? false :tail? false))
val (dissoc res :env)]
(recur (rest exps) env (cons val lst))))))
(defn- eval-primitive
[op exp env]
;;{:val (apply (get primitives (:val (first exp))) '(5 6))
(let [res (apply (:val op)
(eval-arguments
(rest (:val exp))
env))]
(when (> trace 1)
(indent "+" (:level env))
(print " ")
(print-exp true res)
(print "\t")
(print-exp true exp)(newline))
(with-env res env)))
(defn- eval-procedure
[op exp env]
(let [params (eval-arguments (rest (:val exp)) env)
nenv
(if (:tail? env)
(extend-environment env (:arg-list op) params)
(extend-environment
{:parent env :level (+ 1 (:level env)) :tail? false}
(:arg-list op) params))]
(cond (nil? nenv)
(do (println
(str
"Procedure applied to wrong number of arguments. "
"Expected " (count (:arg-list op))
", given " (count params) ". " (pos-string (:start exp))))
(with-env (make-nil) env))
(> (:level env) stack-depth)
(do (println
(str
"Stack overflow, " (pos-string (:start exp))))
(with-env (make-nil) env))
:else
(let [res (eval-sequence (:body op) nenv)]
(if (:tail? env)
res
(do (with-env (dissoc res :env) (:parent (:env res)))))))))
(defn- eval-application
[exp env]
;;(let [op (eval (first (:val exp)) (make-env env false false))]
(let [op (eval (first (:val exp))
(assoc env :def-ok? false :tail? false))]
(cond (= (:type op) :primitive-fun)
(eval-primitive op exp env)
(= (:type op) :fun)
(eval-procedure op exp env)
;;(is-nil? op)
;;(with-env (make-nil) env)
:else
(do (println
(str "First element of the form is not a procedure, "
(pos-string (:start exp))))
(with-env (make-nil) env)))))
;;----
(defmulti eval-form
(fn [exp env]
(when (> trace 0)
(indent "-" (:level env))
(print " ")
(print-exp true exp)(newline))
(:val (first (:val exp)))))
(defmethod eval-form 'quote
[exp env]
(eval-quote exp env))
(defmethod eval-form 'set!
[exp env]
(eval-assign exp env))
(defmethod eval-form 'define
[exp env]
(if (:def-ok? env)
(eval-define exp env)
(do (println
(println
(str "Definition is not allowed in this context, "
(pos-string (:start exp)))))
(with-env (make-nil) env))))
(defmethod eval-form 'if
[exp env]
(eval-if exp env))
(defmethod eval-form 'cond
[exp env]
(eval-cond exp env))
(defmethod eval-form 'lambda
[exp env]
(eval-lambda exp env))
(defmethod eval-form 'begin
[exp env]
(eval-begin exp env))
(defmethod eval-form 'and
[exp env]
(eval-and exp env))
(defmethod eval-form 'or
[exp env]
(eval-or exp env))
(defmethod eval-form :default
[exp env]
(eval-application exp env))
;;-----
(defmulti eval (fn [exp env] (:type exp)))
(defmethod eval :bool
[exp env]
(with-env exp env))
(defmethod eval :number
[exp env]
(with-env exp env))
(defmethod eval :char
[exp env]
(with-env exp env))
(defmethod eval :string
[exp env]
(with-env exp env))
(defmethod eval :symbol
[exp env]
(when (> trace 2)
(indent "-" (:level env))
(print " ")
(print-exp true exp)(newline))
(let [res (eval-symbol exp env)]
(when (> trace 2)
(indent "+" (:level env))
(print " ")
(print-exp true res)
(print "\t")
(print-exp true exp)(newline))
res))
(defmethod eval :list
[exp env]
(eval-form exp env))
(defmethod eval :default
[exp env]
(println
(str "Unrecognised expression "
(pos-string (:start exp))))
(with-env (make-nil) env))
(defn- prompt-for-input
"Displays an input prompt"
[]
(println)
(println "Scheme: ")
(flush))
(defn- output-prompt
"Displays a result prompt"
[]
(newline)
(println "Result:"))
(defn- pprint
"A simple \"pretty print\" function suitable for debugging syntax trees
(as returned by read function)"
([val]
(pprint val 0))
([val lvl]
(indent " " lvl)
(if (and (= (:type val) :list) (not (nil? (:val val))))
(do
(println "(")
(doseq elem (:val val) (pprint elem (+ lvl 2)))
(indent " " lvl)
(println ")"))
(prn val))))
(defn- next-pos
"Returns a structure containing line, column and character number of the
character to be read.
pos - line, column and character number of the current character
ch - current character"
[pos ch]
(let [ch (char ch) line (:line pos) col (:col pos) cnt (+ (:cnt pos) 1)]
(if (= ch \newline)
(assoc pos :line (+ line 1) :col 1 :cnt cnt)
(assoc pos :line line :col (+ col 1) :cnt cnt))))
(defn- char-seq-helper
[stream close? pos]
(let [ch (. stream (read))]
(if (== ch -1)
(when close?
(. stream (close)))
;; (lazy-cons (. Character (toString (char ch)))
;; (char-seq stream close?))))))
(lazy-cons (assoc pos :ch (char ch))
(char-seq-helper stream close? (next-pos pos ch))))))
(defn char-seq
"Reads an input stream (*in* if called without arguments) and returns
a lazy sequence of characters in this stream"
([]
(char-seq *in*))
([stream]
(char-seq stream "STDIN" false))
([stream fname close?]
(char-seq-helper stream close? {:line 1, :col 1, :cnt 1, :fname fname})))
(defn char-seq-from-file
"Reads an input file f and returns a lazy sequence of characters
included this file"
[#^String f]
;; (with-open r (new java.io.FileReader f)
(let [r (new java.io.FileReader f)]
(char-seq r f true)))
(defn- whitespace?
[ch]
(. Character (isWhitespace ch)))
(defn- digit?
[ch]
(. Character (isDigit ch )))
(defn- tappend
"Appends characters (chars) to the :txt field of the token structure"
[token & chars]
(assoc token :txt (reduce str (:txt token) chars)))
(defn- delimiter?
"Is character (ch) a delimiter?"
[ch]
(or (whitespace? ch) (= ch \() (= ch \)) (= ch \;)
(= ch \") (= ch \') (= ch \`) (= ch \,)))
(defn- parse-unicode
([cseq]
(parse-unicode cseq 4 0))
([cseq cnt val]
(if (= cnt 0)
val
(let [ch (:ch (first cseq))
digit (. Character (digit ch 16))]
(if (= digit -1)
nil
(recur (rest cseq) (- cnt 1) (+ (* val 16) digit)))))))
(defn- escape-char
[cseq]
(let [ch (:ch (frest cseq))]
(cond (= ch \n) [\newline (rrest cseq)]
(= ch \t) [\tab (rrest cseq)]
(= ch \r) [(char 13) (rrest cseq)]
(= ch \b) [\backspace (rrest cseq)]
(= ch \f) [\formfeed (rrest cseq)]
(= ch \\) [\\ (rrest cseq)]
(= ch \') [\' (rrest cseq)]
(= ch \") [\" (rrest cseq)]
(= ch \u) (let [val (parse-unicode (rrest cseq))]
(if (not (nil? val))
[(char val) (rrest (rrest (rrest cseq)))]
[nil (rrest cseq)]))
:else [nil (rrest cseq)])))
(defn token-seq
"Consumes a lazy sequence of characters (as returned by char-seq)
and produces a lazy sequence of tokens.
If called without arguments it attempts to read characters from
the *in* stream"
([]
(token-seq (char-seq *in*)))
([cseq]
(token-seq cseq {:type :whitespace}))
([cseq token]
(let [ch (:ch (first cseq)) ann (dissoc (first cseq) :ch)
type (:type token) text (:txt token)]
;; (println ch)
(cond
;; end of stream
(nil? ch) nil
;; comments
(= type :comment)
(if (= ch \newline)
(recur (rest cseq) (assoc token :type :whitespace))
(recur (rest cseq) token))
;; whitespaces (or borders of tokens)
(= type :whitespace)
(cond (whitespace? ch) (recur (rest cseq) token)
(= ch \;) (recur (rest cseq) (assoc token :type :comment))
(= ch \") (recur (rest cseq)
(assoc token :type :string :start ann))
(or (= ch \-) (= ch \+))
(recur (rest cseq)
(assoc (tappend token ch) :type :sign :start ann))
(digit? ch) (recur (rest cseq)
(assoc (tappend token ch)
:type :begin-number :start ann))
(= ch \#) (recur (rest cseq)
(assoc (tappend token ch)
:type :hash :start ann))
(= ch \\) (recur cseq
(assoc (tappend token ch)
:type :char :start ann))
(= ch \') (lazy-cons
{:type :symbol :txt (str ch)
:val (symbol "quote") :expand true
:start ann :end ann}
(token-seq (rest cseq)))
(= ch \`) (lazy-cons
{:type :symbol :txt (str ch)
:val (symbol "quasiquote") :expand true
:start ann :end ann}
(token-seq (rest cseq)))
(= ch \,) (lazy-cons
{:type :symbol :txt (str ch)
:val (symbol "unquote") :expand true
:start ann :end ann}
(token-seq (rest cseq)))
(= ch \() (lazy-cons
{:type :lparen :txt (str ch)
:start ann :end ann}
(token-seq (rest cseq)))
(= ch \)) (lazy-cons
{:type :rparen :txt (str ch)
:start ann :end ann}
(token-seq (rest cseq)))
:else (recur (rest cseq)
(assoc (tappend token ch) :type :symbol
:start ann)))
;; strings
(= type :string)
(cond (= ch \\) (let [res (escape-char cseq)
ch (first res)
ncseq (second res)]
(recur ncseq (tappend token ch)))
(= ch \") (lazy-cons
(assoc token :val (:txt token) :end ann)
(token-seq (rest cseq)))
:else (recur (rest cseq) (tappend token ch)))
;; characters
(= type :char)
(let [res (escape-char cseq)
ch2 (first res)
ncseq (second res)]
(if (nil? ch2)
;;(recur cseq (assoc token :type :unknown))
(let [ch (:ch (frest cseq))
ann (dissoc (frest cseq) :ch)]
(lazy-cons (assoc (tappend token ch)
:val ch :end ann)
(token-seq (rrest cseq))))
(let [ann (dissoc (first ncseq) :ch)]
(lazy-cons (assoc (tappend token ch2)
:val ch2 :end ann :ch)
(token-seq ncseq)))))
;; boolean literals
(= type :hash)
(cond (or (= ch \t) (= ch \T))
(lazy-cons
(assoc (tappend token ch)
:type :bool :val true :end ann)
(token-seq (rest cseq)))
(or (= ch \f) (= ch \F))
(lazy-cons
(assoc (tappend token ch)
:type :bool :val false :end ann)
(token-seq (rest cseq)))
:else (recur cseq (assoc token :type :symbol)))
;; numbers
;; take first digit after sign
(= type :sign)
(cond (digit? ch) (recur (rest cseq)
(assoc (tappend token ch)
:type :begin-number))
:else (recur cseq (assoc token :type :symbol)))
;; take remaining digits of the number and detect the format
(= type :begin-number)
(cond (digit? ch) (recur (rest cseq) (tappend token ch))
(= ch \.) (recur (rest cseq)
(assoc (tappend token ch)
:type :begin-decimal))
(= ch \/) (recur (rest cseq)
(assoc (tappend token ch)
:type :begin-quotient))
(or (= ch \e) (= ch \E)) (recur
(rest cseq)
(assoc (tappend token ch)
:type :begin-exponent))
(delimiter? ch)
(lazy-cons
(assoc token
:type :number :val (new BigInteger text) :end ann)
(token-seq cseq))
:else (recur (rest cseq)
(assoc (tappend token ch)
:type :unknown :error :numformat)))
;; take at least one digit of the decimal part
(= type :begin-decimal)
(cond (digit? ch) (recur (rest cseq)
(assoc (tappend token ch)
:type :decimal))
:else (recur cseq
(assoc token :type :unknown :error :numformat)))
;; collect remaining digits of the decimal part
(= type :decimal)
(cond (digit? ch) (recur (rest cseq) (tappend token ch))
(or (= ch \e) (= ch \E))
(recur (rest cseq)
(assoc (tappend token ch) :type :begin-exponent))
(or (= ch \m) (= ch \M))
(recur (rest cseq) (assoc token :type :big-decimal))
(delimiter? ch)
(lazy-cons
(assoc token
:type :number :val (new Double text) :end ann)
(token-seq cseq))
:else (recur (rest cseq)
(assoc (tappend token ch)
:type :unknown :error :numformat)))
;; make a "big decimal" value
(= type :big-decimal)
(cond
(delimiter? ch)
(lazy-cons
(assoc (tappend token \M) :type :number
:val (new BigDecimal text) :end ann)
(token-seq cseq))
:else (recur (rest cseq)
(assoc (tappend token ch)
:type :unknown :error :numformat)))
;; at least one digit in quotient
(= type :begin-quotient)
(cond (digit? ch) (recur (rest cseq)
(assoc (tappend token ch)
:type :quotient))
:else (recur cseq (assoc token
:type :unknown :error :numformat)))
;; rest of the quotient
(= type :quotient)
(cond (digit? ch) (recur (rest cseq) (tappend token ch))
(delimiter? ch)
(let [[n d] (split-with (fn [c] (not (= c \/))) text)]
(lazy-cons
(assoc token
:type :number
:val
(. clojure.lang.Numbers
(divide (new BigInteger (apply str n))
(new BigInteger (apply str (rest d)))))
:end ann)
(token-seq cseq)))
:else (recur
(rest cseq) (assoc (tappend token ch)
:type :unknown :error :numformat)))
;; take first exponent digit or exponent sign
(= type :begin-exponent)
(cond (digit? ch) (recur
(rest cseq)
(assoc (tappend token ch) :type :exponent))
(or (= ch \-) (= ch \+)) (recur
(rest cseq)
(assoc (tappend token ch)
:type :begin-exponent-digits))
:else (recur cseq (assoc token
:type :unknown :error :numformat)))
;; at least one digit in the exponent
(= type :begin-exponent-digits)
(cond (digit? ch) (recur (rest cseq) (assoc (tappend token ch)
:type :exponent))
:else (recur cseq (assoc token
:type :unknown :error :numformat)))
;; collect digits in the exponent
(= type :exponent)
(cond (digit? ch) (recur (rest cseq) (tappend token ch))
(delimiter? ch)
(lazy-cons (assoc token
:type :number :val (new Double text) :end ann)
(token-seq cseq))
:else (recur (rest cseq)
(assoc (tappend token ch)
:type :unknown :error :numformat)))
;; symbols
(= type :symbol)
(if
(delimiter? ch)
(lazy-cons (assoc token :val (symbol text) :end ann)
(token-seq cseq))
(recur (rest cseq) (tappend token ch)))
(= type :unknown)
(if
(delimiter? ch)
(lazy-cons (assoc token :end ann) (token-seq cseq))
(recur (rest cseq) (tappend token ch)))
))))
(defn token-seq-from-file
"Reads a file f and produces a lazy sequence of tokens"
[#^String f]
(token-seq (char-seq-from-file f)))
(defn- exit
"Exits from the clojure repl."
([]
(exit 0))
([exitcode]
(. System (exit exitcode))))
(defn- print-error
"Displays an error message selected by err keyword, requires a token
so that additional information can be provided to the user"
[err token]
(let [end (:end token) start (:start token)
fname (:fname start) line (:line start) col (:col start)]
;;(prn token)
(cond
(= err :eof)
(do
(println
(str "Unmatched left parenthesis, "
fname ":" line ":" col)))
(= err :rparen)
(println (str "Unmatched right parenthesis, "
fname ":" line ":" col))
(= err :numformat)
(println (str "Invalid number format: \"" (:txt token)
"\", " fname ":" line ":" col))
:else
(println (str "Unknown error, "
fname ":" line ":" col)))
;; (exit 1)
))
(defn- read-helper
[tseq vec root? single?]
(let [token (first tseq) type (:type token) ann (:end token)]
;;(prn token)
(if token
(cond
(= type :lparen)
(let [res (read-helper
(rest tseq)
{:type :list :val (list) :start (:start token)}
false false)
nvec (dissoc res :tseq :eof)
ntseq (:tseq res)]
(when (:eof res)
(print-error :eof token))
(let [val (if (= (count (:val nvec)) 0)
(assoc nvec :type :list :val nil)
nvec)]
(if single?
(assoc (val-conj vec val) :tseq ntseq)
(recur (rest ntseq) (val-conj vec val) root? false))))
(= type :rparen)
(if root?
(do (print-error :rparen token)
(assoc vec :tseq tseq :end ann :val (reverse (:val vec))))
(assoc vec :tseq tseq :end ann :val (reverse (:val vec))))
(:expand token)
(let [res (read-helper (rest tseq)
{:type :list :val (list token)}
false true)
nvec (dissoc res :tseq)
ntseq (:tseq res)]
(if single?
(assoc (val-conj vec nvec) :tseq ntseq)
(recur (rest ntseq) (val-conj vec nvec) root? false)))
(contains? token :error)
(do (print-error (:error token) token)
(if single?
(assoc (val-conj vec token) :tseq tseq)
(recur (rest tseq) (val-conj vec token) root? false)))
:else
(if single?
(assoc (val-conj vec token) :tseq tseq)
(recur (rest tseq) (val-conj vec token) root? false)))
(assoc vec :eof true :val (reverse (:val vec))))))
(defn read
"Consumes a sequence of tokens as returned by token-seq and produces
a syntax tree. Note that this function attempts to read the whole
token sequence"
([]
(read (token-seq (char-seq *in*))))
([tseq]
(dissoc
(read-helper tseq
{:type :list :val (list)}
true false)