(module pattern-runtime mzscheme (require "list-utils.ss" "rep.ss" (lib "match.ss")) (provide (all-defined)) (define (append/reverse code rest) (append (reverse code) rest)) (define (pattern-failed name asm) (error 'asm-pattern "match failed for: ~a, reverse asm: ~a" name asm)) ;; Pure asm transformers never use the data stack, only the asm ;; state. This lifts asm -> asm to (asm . stack) -> (asm . stack) (define (lift-transform xform) (lambda (old-code . stack) (pack (apply xform old-code) stack))) (define (lift-macro-executable macro) (lambda input (check-macro-egg (apply macro input)))) (define (lift-macro macro) (lift-macro-executable (lift-transform macro))) ;; Lifts an (asm . stack) -> (asm . stack) operation to one that ;; checks the assembler output for executable code = primitive ;; macro. If it's found, this macro is executed. (define (check-macro-egg output) (match output ((((and fn (= word? #t)) . asm) . stack) (apply fn `(,asm ,@stack))) (else output))) )