Created
February 9, 2014 14:30
-
-
Save Glorp/8899854 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#lang racket | |
(provide (rename-out (module-begin #%module-begin)) | |
#%app) | |
(define (push stack x) | |
(cons x stack)) | |
(define (add stack) | |
(match stack | |
[(list b a xs ...) (cons (+ a b) xs)])) | |
(define (sub stack) | |
(match stack | |
[(list b a xs ...) (cons (- a b) xs)])) | |
(define-syntax foo | |
(syntax-rules (+ -) | |
[(_ stack +) (add stack)] | |
[(_ stack -) (sub stack)] | |
[(_ stack x) (push stack x)])) | |
(define-syntax foo-all | |
(syntax-rules () | |
[(_ stack x) (foo stack x)] | |
[(_ stack x xs ...) (foo-all (foo stack x) xs ...)])) | |
(define-for-syntax (intern stx) | |
(syntax-case stx () | |
[(_ xs ...) | |
(datum->syntax #'c (syntax->datum #'(xs ...)) #'(xs ...))])) | |
(define-syntax (run stx) | |
(syntax-case (intern stx) () | |
[(xs ...) #'(foo-all '() xs ...)])) | |
(define-syntax-rule (module-begin form ...) | |
(#%module-begin (run form ...))) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#lang racket | |
(provide (rename-out (module-begin #%module-begin) | |
(top-interaction #%top-interaction) | |
(top #%top)) | |
#%app) | |
(define stack '()) | |
(define (push x) | |
(set! stack (cons x stack))) | |
(define (pop) | |
(define res (car stack)) | |
(set! stack (cdr stack)) | |
res) | |
(define (add) | |
(push (+ (pop) (pop))) | |
(car stack)) | |
(define (sub) | |
(define subtrahend (pop)) | |
(push (- (pop) subtrahend)) | |
(car stack)) | |
(define-syntax foo | |
(syntax-rules (+ -) | |
[(_ +) (add)] | |
[(_ -) (sub)] | |
[(_ x) (push x)])) | |
(define-for-syntax (intern stx) | |
(syntax-case stx () | |
[(_ xs ...) | |
(datum->syntax #'c (syntax->datum #'(xs ...)) #'(xs ...))])) | |
(define-syntax (run stx) | |
(syntax-case (intern stx) () | |
[(x) #'(foo x)])) | |
(define-syntax-rule (module-begin form ...) | |
(#%module-begin (run form) ...)) | |
(define-syntax-rule (top-interaction . form) | |
(run form)) | |
(define-syntax-rule (top . form) | |
(run form)) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#lang s-exp "stacky.rkt" | |
10 3 - | |
2 + |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment