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