Skip to content

Instantly share code, notes, and snippets.

@Glorp
Created February 9, 2014 14:30
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Glorp/8899854 to your computer and use it in GitHub Desktop.
Save Glorp/8899854 to your computer and use it in GitHub Desktop.
#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 ...)))
#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))
#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