You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
beautiful-racket/beautiful-racket-demo/basic-demo/expander.rkt

260 lines
8.8 KiB
Racket

8 years ago
#lang br/quicklang
8 years ago
(require (for-syntax syntax/strip-context))
9 years ago
(provide #%top-interaction #%app #%datum
(rename-out [basic-module-begin #%module-begin])
(all-defined-out))
9 years ago
; BASIC implementation details
; http://www.atariarchives.org/basicgames/showpage.php?page=i12
9 years ago
(begin-for-syntax
9 years ago
(require racket/list)
(define (gather-unique-ids stx)
9 years ago
(remove-duplicates (map syntax->datum (filter (λ(s) (syntax-property s 'id)) (syntax-flatten stx))) eq?)))
(define-macro (basic-module-begin (basic-program PROGRAM-LINE ...))
(with-pattern ([(UNIQUE-ID ...)
(map (compose1 syntax-local-introduce (λ(id) (datum->syntax #f id)))
(gather-unique-ids #'(PROGRAM-LINE ...)))])
#'(#%module-begin
(define UNIQUE-ID 0) ...
(provide UNIQUE-ID ...)
8 years ago
(run (sort (cons (line +inf.0 (statement "end"))
(list PROGRAM-LINE ...)) #:key $line-number <)))))
9 years ago
9 years ago
(struct exn:line-not-found exn:fail ())
9 years ago
(define (raise-line-not-found-error ln)
(raise
(exn:line-not-found
(format "line number ~a not found in program" ln)
(current-continuation-marks))))
9 years ago
9 years ago
(struct end-program-signal exn:fail ())
(define (raise-end-program-signal)
(raise (end-program-signal "" (current-continuation-marks))))
9 years ago
9 years ago
(struct end-line-signal exn:fail ())
(define (raise-end-line-signal)
(raise (end-line-signal "" (current-continuation-marks))))
8 years ago
(define (run line-list)
9 years ago
(define lines (list->vector line-list))
(define (find-index ln)
9 years ago
(or
9 years ago
(for/or ([idx (in-range (vector-length lines))])
8 years ago
(and (= ($line-number (vector-ref lines idx)) ln)
idx))
9 years ago
(raise-line-not-found-error ln)))
(void
9 years ago
(with-handlers ([end-program-signal? void])
9 years ago
(for/fold ([program-counter 0])
([i (in-naturals)])
(let* ([line-thunk ($line-thunk (vector-ref lines program-counter))]
[maybe-line-number (line-thunk)])
(if (number? maybe-line-number)
(find-index maybe-line-number)
(add1 program-counter)))))))
9 years ago
8 years ago
(define current-return-stack (make-parameter empty))
9 years ago
(define (basic:gosub where)
(let/cc return-k
8 years ago
(current-return-stack (cons return-k (current-return-stack)))
(basic:goto where)))
9 years ago
(define current-line (make-parameter #f))
8 years ago
(struct $line (number thunk) #:transparent)
(define-macro (line NUMBER . STATEMENTS)
#'($line NUMBER (λ ()
(current-line NUMBER)
(with-handlers ([end-line-signal? (λ _ #f)]
[end-program-signal? raise]
[exn:fail? (λ(exn)
(displayln (format "in line ~a" NUMBER))
(raise exn))])
. STATEMENTS))))
9 years ago
8 years ago
(define-macro-cases statement
[(statement ID "=" EXPR) #'(basic:let ID EXPR)]
9 years ago
[(statement PROC-NAME . ARGS)
9 years ago
(with-pattern
([PROC-ID (prefix-id "basic:" #'PROC-NAME)])
#'(PROC-ID . ARGS))])
9 years ago
8 years ago
(define-macro-cases basic:let
[(_ (id-expr ID) EXPR)
8 years ago
#'(set! ID EXPR)]
[(_ (id-expr ID DIM-IDX ...) EXPR)
#'(array-set! ID DIM-IDX ... EXPR)])
8 years ago
(define-macro-cases basic:if
9 years ago
[(_ COND-EXPR TRUE-EXPR FALSE-EXPR)
#'(if (true? COND-EXPR)
TRUE-EXPR
FALSE-EXPR)]
[(_ COND-EXPR TRUE-EXPR)
#'(if (true? COND-EXPR)
TRUE-EXPR
9 years ago
(raise-end-line-signal))]) ; special short-circuit rule for one-armed conditional
9 years ago
9 years ago
(define true? (compose1 not zero?))
9 years ago
(define (cond->int cond) (if cond 1 0))
(define (basic:and . args) (cond->int (andmap true? args)))
(define (basic:or . args) (cond->int (ormap true? args)))
9 years ago
8 years ago
(define-macro-cases id-expr
[(_ ID) #'(cond
[(procedure? ID) (ID)]
[(array? ID) (array-ref ID (make-vector (array-rank ID) 0))] ; no subscript => zeroth element
[else ID])]
[(_ ID EXPR0 EXPR ...) #'(cond
[(procedure? ID) (ID EXPR0 EXPR ...)]
[(array? ID) (array-ref ID EXPR0 EXPR ...)]
[else (error 'id-expr-confused)])])
8 years ago
(define-macro-cases expr
9 years ago
[(_ COMP-EXPR) #'COMP-EXPR]
[(_ COMP-EXPR "and" SUBEXPR) #'(basic:and COMP-EXPR SUBEXPR)]
[(_ COMP-EXPR "or" SUBEXPR) #'(basic:or COMP-EXPR SUBEXPR)])
9 years ago
8 years ago
(define-macro-cases comp-expr
9 years ago
[(_ SUM) #'SUM]
[(_ SUM "=" COMP-EXPR)
#'(cond->int (equal? SUM COMP-EXPR))] ; special case because `=` is overloaded in basic
[(_ SUM OP-STR COMP-EXPR)
(with-pattern
([OP (replace-context #'here (prefix-id #'OP-STR))])
#'(cond->int (OP SUM COMP-EXPR)))])
9 years ago
9 years ago
(define <> (compose1 not equal?))
9 years ago
8 years ago
(define-macro-cases sum
9 years ago
[(_ SUM) #'SUM]
[(_ SUM "+" PRODUCT) #'(+ SUM PRODUCT)]
[(_ SUM "-" PRODUCT) #'(- SUM PRODUCT)])
8 years ago
(define-macro-cases product
[(_ "-" VALUE) #'(- VALUE)]
9 years ago
[(_ VALUE) #'VALUE]
[(_ PRODUCT "*" VALUE) #'(* PRODUCT VALUE)]
[(_ PRODUCT "/" VALUE) #'(/ PRODUCT VALUE)])
8 years ago
(define-macro-cases power
[(_ BASE) #'BASE]
[(_ BASE POWER) #'(expt BASE POWER)])
8 years ago
(define-macro-cases maybe-negative-val
[(_ "-" ID) #'(- ID)]
[(_ ID) #'ID])
9 years ago
(define print-list list)
9 years ago
(define (basic:print [args #f])
(define (println [x ""])
(define xstr (format "~a" x))
(displayln xstr)
8 years ago
(current-print-position 0))
(define (print x)
(define xstr (format "~a" x))
(display xstr)
8 years ago
(current-print-position (+ (current-print-position) (string-length xstr))))
9 years ago
(match args
[#f (println)]
[(list print-list-items ... ";" pl)
(begin
(for-each
(λ(pli)
(print (if (number? pli)
(format "~a " pli)
pli)))
print-list-items)
(basic:print pl))]
[(list print-list-items ... ";") (for-each print print-list-items)]
[(list print-list-items ...)
(for-each println print-list-items)]))
8 years ago
(define current-print-position (make-parameter 0))
(define (TAB num) (make-string (max 0 (INT (- num (current-print-position)))) #\space))
(define (INT num) (inexact->exact (truncate num)))
9 years ago
(define (SIN num) (sin num))
9 years ago
(define (ABS num) (inexact->exact (abs num)))
9 years ago
(define (RND num) (* (random) num))
(define (EXP num) (exp num))
(define (SQR num) (sqrt num))
8 years ago
(define-macro-cases basic:input
[(_ (print-list . PL-ITEMS) ID ...)
9 years ago
#'(begin
(basic:print (append (print-list . PL-ITEMS) (list ";")))
(basic:input ID) ...)]
[(_ ID ...) #'(begin
(set! ID (let* ([str (read-line)]
[num (string->number (string-trim str))])
(or num str))) ...)])
9 years ago
9 years ago
(define (basic:goto where) where)
9 years ago
8 years ago
(define-macro-cases basic:on
[(_ TEST-EXPR "goto" OPTION ...)
#'(basic:goto (list-ref (list OPTION ...) (sub1 TEST-EXPR)))]
[(_ TEST-EXPR "gosub" OPTION ...)
#'(basic:gosub (list-ref (list OPTION ...) (sub1 TEST-EXPR)))])
(define (basic:return)
8 years ago
(define return-k (car (current-return-stack)))
(current-return-stack (cdr (current-return-stack)))
(return-k #f))
9 years ago
(define (basic:stop) (basic:end))
9 years ago
(define (basic:end) (raise-end-program-signal))
(require srfi/25)
(define-macro (basic:dim (id-expr ID EXPR ...) ...)
#'(begin
(set! ID (make-array (apply shape (append (list 0 (add1 EXPR)) ...)))) ...))
8 years ago
(define current-for-stack (make-parameter empty))
(define (push-for-stack thunk)
8 years ago
(current-for-stack (cons thunk (current-for-stack))))
(define (pop-for-stack)
8 years ago
(current-for-stack (cdr (current-for-stack))))
(define (in-closed-interval? x left right)
(define cmp (if (< left right) <= >=))
(cmp left x right))
8 years ago
(define-macro-cases basic:for
[(_ VAR START-VALUE END-VALUE)
#'(basic:for VAR START-VALUE END-VALUE 1)]
[(_ VAR START-VALUE END-VALUE STEP-VALUE)
#'(begin
(statement (id-expr VAR) "=" START-VALUE) ; initialize the loop counter
9 years ago
(let/cc return-k ; create a return point
(push-for-stack (cons 'VAR
9 years ago
(λ () ; thunk that increments counter & teleports back to beginning of loop
(define next-val (+ VAR STEP-VALUE))
(if (next-val . in-closed-interval? . START-VALUE END-VALUE)
9 years ago
(begin
(set! VAR next-val)
(return-k #f)) ; return value for subsequent visits to line
(pop-for-stack)))))
#f))]) ; return value for first visit to line
(define (handle-next [which #f])
8 years ago
(unless (pair? (current-for-stack)) (error 'next "for-stack is empty"))
(define for-thunk (cdr (if which
8 years ago
(assq which (current-for-stack))
(car (current-for-stack)))))
(for-thunk))
(define-macro (basic:next VAR ...)
#'(handle-next 'VAR ...))
(define-macro (basic:def DEF-ID LAMBDA-ID EXPR)
#'(set! DEF-ID (λ (LAMBDA-ID) EXPR)))