6fbc35b67993fc2798effb659063db749d247c9a
[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                                         ; define a set of local
79                                         ; variables and then evaluate
80                                         ; a list of sexprs
81                                         ;
82                                         ; (let (var-defines) sexprs)
83                                         ;
84                                         ; where var-defines are either
85                                         ;
86                                         ; (name value)
87                                         ;
88                                         ; or
89                                         ;
90                                         ; (name)
91                                         ;
92                                         ; e.g.
93                                         ;
94                                         ; (let ((x 1) (y)) (setq y (+ x 1)) y)
95
96 (def let (macro (vars exprs)
97                 ((lambda (make-names make-exprs make-nils)
98                    (progn
99
100                                         ;
101                                         ; make the list of names in the let
102                                         ;
103
104                      (setq make-names (lambda (vars)
105                                        (cond (vars
106                                               (cons (car (car vars))
107                                                     (make-names (cdr vars))))
108                                              )
109                                        )
110                           )
111
112                                         ; the set of expressions is
113                                         ; the list of set expressions
114                                         ; pre-pended to the
115                                         ; expressions to evaluate
116
117                      (setq make-exprs (lambda (vars exprs)
118                                        (progn
119                                          (cond (vars (cons
120                                                       (list set
121                                                             (list quote
122                                                                   (car (car vars))
123                                                                   )
124                                                             (cadr (car vars))
125                                                             )
126                                                       (make-exprs (cdr vars) exprs)
127                                                       )
128                                                      )
129                                                (exprs)
130                                                )
131                                          )
132                                        )
133                           )
134
135                                         ; the parameters to the lambda is a list
136                                         ; of nils of the right length
137
138                      (setq make-nils (lambda (vars)
139                                       (cond (vars (cons nil (make-nils (cdr vars))))
140                                             )
141                                       )
142                           )
143                                         ; prepend the set operations
144                                         ; to the expressions
145
146                      (setq exprs (make-exprs vars exprs))
147
148                                         ; build the lambda.
149
150                      (cons
151                       (list
152                        'lambda
153                        (make-names vars)
154                        (cond ((cdr exprs) (cons 'progn exprs))
155                              ((car exprs))
156                              )
157                        )
158                       (make-nils vars)
159                       )
160                      )
161                    )
162                  ()
163                  ()
164                  ()
165                  )
166                 )
167      )
168
169                                         ; boolean operators
170
171 (def or (lexpr (l)
172                (let ((ret nil))
173                  (while l
174                    (cond ((setq ret (car l))
175                           (setq l nil))
176                          ((setq l (cdr l)))))
177                  ret
178                  )
179                )
180      )
181
182                                         ; execute to resolve macros
183
184 (or nil t)
185
186 (def and (lexpr (l)
187                (let ((ret t))
188                  (while l
189                    (cond ((setq ret (car l))
190                           (setq l (cdr l)))
191                          ((setq ret (setq l nil)))
192                          )
193                    )
194                  ret
195                  )
196                )
197      )
198
199                                         ; execute to resolve macros
200
201 (and t nil)
202