Created
February 28, 2022 22:01
-
-
Save heyodai/63e844037b6198e8633b4d064727427a to your computer and use it in GitHub Desktop.
Beautiful Racket - Stacker Tutorial
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 reader "stacker.rkt" | |
4 | |
8 | |
+ | |
3 | |
* |
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
#| | |
@see https://beautifulracket.com/stacker/intro.html | |
|# | |
#lang br/quicklang | |
(define (read-syntax path port) | |
(define src-lines (port->lines port)) | |
(define src-datums (format-datums '(handle ~a) src-lines)) | |
(define module-datum `(module stacker-mod "stacker.rkt" | |
,@src-datums)) | |
(datum->syntax #f module-datum)) | |
(provide read-syntax) | |
(define-macro (stacker-module-begin HANDLE-EXPR ...) | |
#'(#%module-begin | |
HANDLE-EXPR ... | |
(display (first stack)))) | |
(provide (rename-out [stacker-module-begin #%module-begin])) | |
(define stack empty) | |
(define (pop-stack!) | |
(define arg (first stack)) | |
(set! stack (rest stack)) | |
arg) | |
(define (push-stack! arg) | |
(set! stack (cons arg stack))) | |
(define (handle [arg #f]) | |
(cond | |
[(number? arg) (push-stack! arg)] | |
[(or (equal? * arg) (equal? + arg)) | |
(define op-result (arg (pop-stack!) (pop-stack!))) | |
(push-stack! op-result)])) | |
(provide handle) | |
(provide + *) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment