4dc63bbf8e82c88daf93493c8557f3ba8f159c64
[fw/altos] / src / lisp / ao_lisp_const.lisp
1 ;
2 ; Copyright © 2016 Keith Packard <keithp@keithp.com>
3 ;
4 ; This program is free software; you can redistribute it and/or modify
5 ; it under the terms of the GNU General Public License as published by
6 ; the Free Software Foundation, either version 2 of the License, or
7 ; (at your option) any later version.
8 ;
9 ; This program is distributed in the hope that it will be useful, but
10 ; WITHOUT ANY WARRANTY; without even the implied warranty of
11 ; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12 ; General Public License for more details.
13 ;
14 ; Lisp code placed in ROM
15
16                                         ; return a list containing all of the arguments
17
18 (set (quote list) (lexpr (l) l))
19
20                                         ;
21                                         ; Define a variable without returning the value
22                                         ; Useful when defining functions to avoid
23                                         ; having lots of output generated
24                                         ;
25
26 (setq def (macro (name val rest)
27                  (list
28                   'progn
29                   (list
30                    'set
31                    (list 'quote name)
32                    val)
33                   (list 'quote name)
34                   )
35                  )
36       )
37
38                                         ;
39                                         ; A slightly more convenient form
40                                         ; for defining lambdas.
41                                         ;
42                                         ; (defun <name> (<params>) s-exprs)
43                                         ;
44
45 (def defun (macro (name args exprs)
46                   (list
47                    def
48                    name
49                    (list
50                     'lambda
51                     args
52                     (cond ((cdr exprs)
53                            (cons progn exprs))
54                           ((car exprs))
55                           )
56                     )
57                    )
58                   )
59      )
60                                         ; basic list accessors
61
62
63 (defun cadr (l) (car (cdr l)))
64
65 (defun caddr (l) (car (cdr (cdr l))))
66
67 (defun nth (list n)
68   (cond ((= n 0) (car list))
69         ((nth (cdr list) (1- n)))
70         )
71   )
72
73                                         ; simple math operators
74
75 (defun 1+ (x) (+ x 1))
76 (defun 1- (x) (- x 1))
77
78                                         ; boolean operators
79
80 (def or (lexpr (l)
81                (let ((ret nil))
82                  (while l
83                    (cond ((setq ret (car l))
84                           (setq l nil))
85                          ((setq l (cdr l)))))
86                  ret
87                  )
88                )
89      )
90
91 (def and (lexpr (l)
92                (let ((ret t))
93                  (while l
94                    (cond ((setq ret (car l))
95                           (setq l (cdr l)))
96                          ((setq ret (setq l nil)))
97                          )
98                    )
99                  ret
100                  )
101                )
102      )
103
104                                         ; define a set of local
105                                         ; variables and then evaluate
106                                         ; a list of sexprs
107                                         ;
108                                         ; (let (var-defines) sexprs)
109                                         ;
110                                         ; where var-defines are either
111                                         ;
112                                         ; (name value)
113                                         ;
114                                         ; or
115                                         ;
116                                         ; (name)
117                                         ;
118                                         ; e.g.
119                                         ;
120                                         ; (let ((x 1) (y)) (setq y (+ x 1)) y)
121
122 (def let (macro (vars exprs)
123                 ((lambda (make-names make-exprs make-nils)
124                    (progn
125
126                                         ;
127                                         ; make the list of names in the let
128                                         ;
129
130                      (setq make-names (lambda (vars)
131                                        (cond (vars
132                                               (cons (car (car vars))
133                                                     (make-names (cdr vars))))
134                                              )
135                                        )
136                           )
137
138                                         ; the set of expressions is
139                                         ; the list of set expressions
140                                         ; pre-pended to the
141                                         ; expressions to evaluate
142
143                      (setq make-exprs (lambda (vars exprs)
144                                        (progn
145                                          (cond (vars (cons
146                                                       (list set
147                                                             (list quote
148                                                                   (car (car vars))
149                                                                   )
150                                                             (cadr (car vars))
151                                                             )
152                                                       (make-exprs (cdr vars) exprs)
153                                                       )
154                                                      )
155                                                (exprs)
156                                                )
157                                          )
158                                        )
159                           )
160
161                                         ; the parameters to the lambda is a list
162                                         ; of nils of the right length
163
164                      (setq make-nils (lambda (vars)
165                                       (cond (vars (cons nil (make-nils (cdr vars))))
166                                             )
167                                       )
168                           )
169                                         ; prepend the set operations
170                                         ; to the expressions
171
172                      (setq exprs (make-exprs vars exprs))
173
174                                         ; build the lambda.
175
176                      (cons
177                       (list
178                        'lambda
179                        (make-names vars)
180                        (cond ((cdr exprs) (cons 'progn exprs))
181                              ((car exprs))
182                              )
183                        )
184                       (make-nils vars)
185                       )
186                      )
187                    )
188                  ()
189                  ()
190                  ()
191                  )
192                 )
193      )
194
195                                         ; run the let macro once to
196                                         ; evaluate all of the internal
197                                         ; macro calls
198
199 (let ((let-param 1)))
200