-
Notifications
You must be signed in to change notification settings - Fork 12
/
cons-cst.lisp
76 lines (59 loc) · 1.76 KB
/
cons-cst.lisp
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
(cl:in-package #:concrete-syntax-tree)
(defclass cons-cst (cst)
(;; This slot contains a CST that represents the CAR of the
;; corresponding expression.
(%first :initform nil :initarg :first :reader first)
;; This slot contains a CST that represents the CDR of the
;; corresponding expression.
(%rest :initform nil :initarg :rest :reader rest)))
(defmethod consp ((cst cons-cst))
(declare (ignorable cst))
t)
(defgeneric cons (first rest &key source))
(defun raw-or-nil (cst)
(raw cst))
(defmethod cons (first rest &key source)
(make-instance 'cons-cst
:raw (cl:cons (raw-or-nil first) (raw-or-nil rest))
:source source
:first first
:rest rest))
(defun list (&rest csts)
(loop for result = (make-instance 'atom-cst :raw nil) then (cons cst result)
for cst in (reverse csts)
finally (return result)))
(defgeneric nthrest (n cst)
(:method (n (cst cons-cst))
(loop for tail = cst then (rest tail)
repeat n
finally (return tail))))
(defgeneric nth (n cst)
(:method (n (cst cons-cst))
(first (nthrest n cst))))
(defgeneric second (cst)
(:method ((cst cons-cst))
(nth 1 cst)))
(defgeneric third (cst)
(:method ((cst cons-cst))
(nth 2 cst)))
(defgeneric fourth (cst)
(:method ((cst cons-cst))
(nth 3 cst)))
(defgeneric fifth (cst)
(:method ((cst cons-cst))
(nth 4 cst)))
(defgeneric sixth (cst)
(:method ((cst cons-cst))
(nth 5 cst)))
(defgeneric seventh (cst)
(:method ((cst cons-cst))
(nth 6 cst)))
(defgeneric eighth (cst)
(:method ((cst cons-cst))
(nth 7 cst)))
(defgeneric ninth (cst)
(:method ((cst cons-cst))
(nth 8 cst)))
(defgeneric tenth (cst)
(:method ((cst cons-cst))
(nth 9 cst)))