This repository has been archived by the owner on Dec 5, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
lsystem.lisp
169 lines (147 loc) · 6.31 KB
/
lsystem.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
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
(in-package :cl-lsystem)
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;;; Definitions
(deftype parametrized-letter ()
'(rte:rte (:cat symbol (:* t))))
(deftype parametrized-word ()
'(rte:rte (:* parametrized-letter)))
(defclass rule ()
((name :initform (error "a rule must have a name")
:initarg :name
:reader name
:type symbol)
(function :initform nil
:initarg :function
:accessor function
:type (or null cl:function))
(instruction :initform (error "a rule must have an instruction")
:initarg :instruction
:accessor instruction
:type cl:function)))
(defclass lsystem ()
((axiom :initform ()
:initarg :axiom
:reader axiom
:type parametrized-word)
(rules :initform (make-hash-table)
:initarg :rules
:accessor rules
:type hash-table)))
(defgeneric set-rule (lsystem rule)
(:method ((lsystem lsystem) (rule rule))
(with-slots (name) rule
(when (gethash name (rules lsystem))
(warn "redefining rule ~a for lsystem ~S" name lsystem))
(setf (gethash name (rules lsystem))
rule))))
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;;; Axiom expansion
(defun rule-of-parametrized-letter (lsystem letter)
(declare (type lsystem lsystem)
(type parametrized-letter letter))
(destructuring-bind (name &rest args) letter
(values (gethash name (rules lsystem))
args)))
(defun parametrized-letter->instruction (lsystem letter)
(declare (type lsystem lsystem)
(type parametrized-letter letter))
(multiple-value-bind (rule args)
(rule-of-parametrized-letter lsystem letter)
(assert rule nil "rule not found in L-System ~S for parametrized letter ~S" lsystem letter)
(apply (instruction rule)
args)))
(defun expand-letter (lsystem letter)
(declare (type lsystem lsystem)
(type parametrized-letter letter))
(multiple-value-bind (rule args)
(rule-of-parametrized-letter lsystem letter)
(if rule
(if-let (f (function rule))
(apply (function rule)
args)
(list letter))
(list letter))))
(defun %iter-lsystem (lsystem n procedure word)
(declare (type lsystem lsystem)
(type unsigned-byte n)
(type cl:function procedure))
;; last iteration?
(if (zerop n)
;; call the procedure for each letter of the final word section
(dolist (letter word)
(let ((instruction (parametrized-letter->instruction lsystem letter)))
(funcall procedure instruction)))
;; otherwise expand and recurse
(dolist (letter word)
(let ((new-word (expand-letter lsystem letter)))
(%iter-lsystem lsystem (1- n) procedure new-word))))
(values))
(defun iter-lsystem (lsystem n procedure)
(declare (type lsystem lsystem)
(type unsigned-byte n)
(type cl:function procedure))
(%iter-lsystem lsystem n procedure (axiom lsystem))
(values))
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;;; define-lsystem, define-rule, #w and #i
(defparameter *lsystem* nil)
(defmacro define-lsystem (name axiom)
`(defparameter ,name (setf *lsystem*
(make-instance 'lsystem :axiom ,axiom))))
(defmacro define-rule (name lambda-list instruction &body body)
(let* ((ll (set-difference lambda-list lambda-list-keywords))
(ignore-statement (etypecase ll
((rte:rte (:+ symbol)) `((declare (ignorable ,@ll))))
(list '()))))
`(set-rule *lsystem*
(make-instance 'rule
:name ',name
:function ,(if body
`(lambda ,lambda-list
,@ignore-statement
,@body)
nil)
:instruction (lambda ,lambda-list
,@ignore-statement
,instruction)))))
(defmacro parametric-word (&rest words)
(flet ((word->list (word)
(destructuring-bind (rule &rest parameters)
word
`(list ',rule ,@parameters))))
(if (= 1 (length words)) ; (FAF) => (list (list 'F) (list 'A) (list 'F))
`(list ,@(map 'list
(lambda (character)
`(list ',(symbolicate character)))
(symbol-name (first words))))
(etypecase (first words)
(symbol ; (A 1 2) => (list (list 'A 1 2))
`(list ,(word->list words)))
((and list
(not null)) ; ((A) (B 1)) => (list (list 'A) (list 'B 1))
(cons 'list (mapcar #'word->list words)))))))
(defmacro instruction-constructor ((class-name &rest args))
(let* ((class (find-class class-name))
(lambda-list (car (constructor class))))
(closer-mop:ensure-finalized class)
(let* ((slots (sb-mop:class-slots class))
(slot-names (mapcar #'sb-mop:slot-definition-name slots)))
(with-gensyms (instruction)
`(let ((,instruction (make-instance ',class-name)))
(destructuring-bind ,lambda-list (list ,@args)
,@(mapcar (lambda (name)
`(setf (slot-value ,instruction ',name) ,name))
slot-names))
,instruction)))))
(set-dispatch-macro-character #\# #\w
(lambda (stream subchar arg)
(declare (ignore subchar arg))
(let ((words (read stream t nil t)))
(etypecase words
(symbol `(parametric-word ,words))
(list `(parametric-word ,@words))))))
(set-dispatch-macro-character #\# #\i
(lambda (stream subchar arg)
(declare (ignore subchar arg))
(let ((instruction (read stream t nil t)))
`(instruction-constructor ,instruction))))