-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmacro.lisp
32 lines (27 loc) · 1.03 KB
/
macro.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
(in-package #:regmach4wasm)
(declaim (optimize (debug 3)))
(defconstant max-macro-args 100
"The maximum number of args a macro may have.")
(defun macro-name (expr) (cadr expr))
(defun macro-args (expr) (caddr expr))
(defun macro-body (expr) (cdddr expr))
(defun put-macro (env sym mac)
(if (env-contains? env sym)
;; get the macro vector index by number of args.
(let ((vec (env-get env sym))
(num-args (length (macro-args mac))))
(setf (elt vec num-args) mac))
;; else allocate a vector for the macros.
(progn
(env-put env sym (make-array max-macro-args :initial-element nil))
(put-macro env sym mac))))
(defun get-macro (env sym num-args)
(if (env-contains? env sym)
(let* ((vec (env-get env sym))
(macrodef (elt vec num-args)))
(if macrodef macrodef
(error (format nil "macro called ~a is defined for ~a arguments" sym num-args))))))
(defun defined-macro? (env sym)
(check-type sym symbol)
(and (symbolp sym)
(env-contains? env sym)))