Open
Description
I am trying to define a defmacro
variant that takes its arguments instead of hard-coding an args
parameter. This is how it's used:
(defmacro2 (add left-arg right-arg) (progn
(:= left (lower left-arg))
(:= right (lower right-arg))
(add-cexp left right)
(set-expression left (print-to-mem "(%s) + (%s)" (get-expression left) (get-expression right)))
left))
Arguably, it's rather redundant compared to just using args
directly since we need to name things after lowering anyways. But it's an exercise for me to play with the system.
This is how I defined it, hardcoding two arguments:
(defmacro comment
(make-cexp "" "" "" ""))
(comment "how to generalize to N arguments -- need a recursive builder")
(defmacro defmacro2 (progn
(comment "why is it an error to wrap an expect-ident around the RHS of name")
(:= name (car (car args)))
(:= params (cdr (car args)))
(:= param1 (car params))
(:= val1 (cons (make-ident-val "car") (cons (make-ident-val "args") (make-nil-val))))
(:= param2 (car (cdr params)))
(:= val2 (cons (make-ident-val "car") (cons (cons (make-ident-val "cdr") (cons (make-ident-val "args") (make-nil-val))) (make-nil-val))))
(:= body (car (cdr args)))
(call-macro "defmacro" (cons
name
(cons
(cons (make-ident-val "progn")
(cons (cons (make-ident-val ":=") (cons param1 (cons val1 (make-nil-val))))
(cons (cons (make-ident-val ":=") (cons param2 (cons val2 (make-nil-val))))
(cons body (make-nil-val)))))
(make-nil-val))))
))
Do you know why I am not supposed to use expect-ident
on the name?
Next, I'd like to experiment with the variant for any number of arguments. It will require recursion. Any tips?
Thanks.
Metadata
Metadata
Assignees
Labels
No labels