-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstacker.rkt
54 lines (45 loc) · 1.64 KB
/
stacker.rkt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
#lang br/quicklang
;;;; `C-c C-z` to launch your REPL from one of the rkt files.
;;;; `C-c C-k` to compile and load your file in the repl.
;;;; `C-x C-e` will evaluate the form and show it in the debug buffer.
;;;; `C-c C-c` will evaluate the form and show in the Emacs gutter.
;;;; Geiser Cheat Sheet: http://www.nongnu.org/geiser/geiser_5.html
;; 1.
;; (Lexx?) A reader, which converts the source code of our language from a string
;; of characters into Racket-style parenthesized forms, also known as
;; S-expressions.
;; 2.
;; (Yacc?) An expander, which determines how these parenthesized forms
;; correspond to real Racket expressions (which are then evaluated to produce a
;; result).
(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))
(display "\n")))
(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)
(equal? * arg)
(equal? / arg))
(define op-result (arg (pop-stack!) (pop-stack!)))
(push-stack! op-result)]))
(provide handle)
(provide + - * /)