Skip to content

Instantly share code, notes, and snippets.

@Yoxem
Created August 14, 2020 17:53
Show Gist options
  • Save Yoxem/d2db2b95348d18e0523af7ab20086ce7 to your computer and use it in GitHub Desktop.
Save Yoxem/d2db2b95348d18e0523af7ab20086ce7 to your computer and use it in GitHub Desktop.
Extend a macroin Racket
#lang racket
(define (combine-two-list x y)
(if (eq? x '()) '()
(cons (cons (car x) (car y)) (combine-two-list (cdr x) (cdr y)))))
; (combine-two-list '(a b c) '(1 2 3)) -> '((a . 1) (b . 2) (c . 3))
(define (walk-the-tree expr-tree macro-var-list)
(cond
((pair? expr-tree) (cons (walk-the-tree (car expr-tree) macro-var-list)
(walk-the-tree (cdr expr-tree) macro-var-list)))
((assoc expr-tree macro-var-list) (cdr(assoc expr-tree macro-var-list)))
(else expr-tree)))
(define (extend-a-fixed-macro-aux expr macro-pattern macro-body)
(cond ((not (= (length expr) (length macro-pattern)))
(error "expr length and macro-pattern length aren't equal."))
(else
(let
((macro-var-list (combine-two-list macro-pattern expr)))
(walk-the-tree macro-body macro-var-list)
)))
)
(define (extend-a-fixed-macro expr macro)
(match macro
[`(,pattern ,body) (extend-a-fixed-macro-aux expr pattern body)]
)
)
(extend-a-fixed-macro-aux '(a 1 2) '(a b c) '(+ (* b b) (* c c)))
; macro = '((a b c) (+ (* b b) (* c c)))
; (a 1 2) => '(+ (* 1 1) (* 2 2))
(extend-a-fixed-macro '(a 1 2) '((a b c) (+ (* b b) (* c c))))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment