generated from 8dcc/c-template
-
Notifications
You must be signed in to change notification settings - Fork 0
/
macros.lisp
67 lines (62 loc) · 1.97 KB
/
macros.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
;;------------------------------------------------------------------------------
;; Features tested in this source:
;; - Backquote (`), unquote (,) and splice (,@)
;; - Macro definitions
;; - Macro expansion
;; - Macro calls
;; - Usage of `defmacro' and `defun' macros
;;------------------------------------------------------------------------------
;; Define a macro called `defmacro'. It will take a `name', and the rest of the
;; arguments will be stored in a list `macro-args'. The expanded macro will just
;; append a symbol "macro" to the beginning of `macro-args', and wrap that
;; expression in a call to `define' with the specified `name'. For example:
;;
;; (defmacro 1+ (var)
;; (list '+ 1 var))
;;
;; Will expand to:
;;
;; (define 1+
;; (macro (var)
;; (list '+ 1 var)))
;;
(define defmacro
(macro (name &rest macro-args)
`(define ,name (macro ,@macro-args))))
;; Example usage of `defmacro'. Create a macro that will take a variable and
;; re-define it to its value plus one.
(defmacro inc (var)
`(define ,var (+ ,var 1)))
;; First, test the `macroexpand' primitive, which should return:
;; (define my-variable (+ my-variable 1))
;;
;; Then the actual macro call, which is just the evaluated expansion.
;;
;; Then try to evaluate the expression returned by `macroexpand', which should
;; be equivalent to calling the macro normally.
(define my-variable 10)
(macroexpand '(inc my-variable))
(inc my-variable)
(eval (macroexpand '(inc my-variable)))
;; Simple implmentation of `defun'. For example:
;;
;; (defun my-function (n)
;; (+ 1 n))
;;
;; Will expand to:
;;
;; (define my-function
;; (lambda (n)
;; (+ 1 n)))
;;
(defmacro defun (name &rest lambda-args)
`(define ,name (lambda ,@lambda-args)))
;; Define a simple function using the previous `defun' macro. Wrap the call in:
;;
;; (macroexpand 'EXPR)
;;
;; To see how the macro expands.
(defun my-function (a b)
(define unused 'not-returned)
(+ a b 10))
(my-function 1 2)