Debian Codegraph @

doc/minilisp/minilisp.in

raw
1 ;; -*- Lisp -*-
2 ;; ---------------------------------------------------------------
3 ;; MiniLisp - Very small lisp interpreter to demonstrate MiniExp.
4 ;; Copyright (c) 2005 Leon Bottou
5 ;;
6 ;; This software is subject to, and may be distributed under, the
7 ;; GNU General Public License, either Version 2 of the license,
8 ;; or (at your option) any later version. The license should have
9 ;; accompanied the software or you may obtain a copy of the license
10 ;; from the Free Software Foundation at http://www.fsf.org .
11 ;;
12 ;; This program is distributed in the hope that it will be useful,
13 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
14 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 ;; GNU General Public License for more details.
16 ;; ---------------------------------------------------------------
17
18 ;; Executable <minilisp> loads this file on startup.
19 ;; It contains a few helpful minilisp functions and macros.
20
21 (defvar defun
22 (mlambda((name . args) . body)
23 (list 'defvar name (list 'lambda args . body)) ) )
24
25 (defvar defmacro
26 (mlambda((name . args) . body)
27 (list 'defvar name (list 'mlambda args . body)) ) )
28
29 (defmacro (pretty f)
30 (let ((s (if (symbolp f) (list (list quote f)))))
31 (list 'let
32 (list (list 's (list 'funcdef f . s)))
33 (list 'pprint 's 72)
34 () ) ) )
35
36 (defun (copyrev l)
37 (let ((r ()))
38 (while l
39 (setq r (cons (car l) r))
40 (setq l (cdr l)) )
41 r ) )
42
43 (defun (copy l)
44 (reverse (copyrev l)) )
45
46 (defun (append . l)
47 (setq l (copyrev l))
48 (let ((r (car l)))
49 (while (consp (setq l (cdr l)))
50 (let ((x (copyrev (car l))))
51 (while (consp x)
52 (let ((y (cdr x)))
53 (setq r (rplacd x r))
54 (setq x y) ) ) ) )
55 r ) )
56
57 (defmacro (when cond . body)
58 (list 'if cond (list 'progn . body)) )
59
60 (let ((sym-to '|S 0001|)
61 (sym-res '|S 0002|) )
62 (defmacro (for (v from to) . body)
63 (list 'let (list (list v from)
64 (list sym-to to)
65 (list sym-res ()) )
66 (list 'while (list <= v sym-to)
67 (list 'setq sym-res (list 'progn . body))
68 (list 'setq v (list '+ v 1)) )
69 sym-res ) ) )
70
71 (let ((sym-to '|S 0001|)
72 (sym-res '|S 0002|) )
73 (defmacro (mapfor (v from to) . body)
74 (list 'let (list (list v from)
75 (list sym-to to)
76 (list sym-res ()) )
77 (list 'while (list <= v sym-to)
78 (list 'setq sym-res (list 'cons (list 'progn . body) sym-res))
79 (list 'setq v (list '+ v 1)) )
80 (list 'reverse sym-res) ) ) )
81
82 (defmacro (cond . args)
83 (let ((a (copyrev args))
84 (r ()))
85 (while (consp a)
86 (setq r (cons 'if (cons (caar a)
87 (if (= (length (cdar a)) 1)
88 (list (car (cdar a)) r)
89 (list (cons 'progn (cdar a)) r) ) )))
90 (setq a (cdr a)) )
91 r ) )
92
93 (defmacro (not n)
94 (list 'nullp n) )
95
96 (defmacro (and . args)
97 (let ((r 't)
98 (a (copyrev args)) )
99 (while a
100 (setq r (list 'if (car a) r))
101 (setq a (cdr a)) )
102 r ) )
103
104 (defmacro (or . args)
105 (let ((r ())
106 (a (copyrev args))
107 (f (mlambda(x . b)
108 (list 'let (list (list 's x))
109 (list 'if 's 's . b) ) )) )
110 (while a
111 (setq r (list f (car a) r))
112 (setq a (cdr a)) )
113 r ) )
114
115 (defun (mapcar f l)
116 (let ((r ()))
117 (while l
118 (setq r (cons (f (car l)) r))
119 (setq l (cdr l)) )
120 (reverse r) ) )
121
122 (defun (1+ x) (+ x 1))
123
124 (defun (1- x) (- x 1))
125
126 (defun (2* x) (* x 2))
127
128 (defun (2/ x) (/ x 2))
129
130 (defun (div n d)
131 (floor (/ n d)) )
132
133 (defun (mod n d)
134 (- n (* d (div n d))) )
135
136 (defvar set! setq)
137
138 (defvar set-car! rplaca)
139
140 (defvar set-cdr! rplacd)
141
142 (display "MiniLisp, (C) 2005, Leon Bottou.\n"
143 "Available under the GNU General Public Licence.\n\n")