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/br/demo/basic/expander.rkt

94 lines
3.2 KiB
Racket

#lang br
(provide #%top-interaction #%app #%datum
(rename-out [basic-module-begin #%module-begin])
(rename-out [basic-top #%top])
(all-defined-out))
(require br/stxparam)
(define-language-variables [A 0][B 0][C 0][D 0][E 0][F 0][G 0][H 0][I 0][J 0][K 0][L 0][M 0][N 0][O 0][P 0][Q 0][R 0][S 0][T 0][U 0][V 0][W 0][X 0][Y 0][Z 0][A$ ""][B$ ""][C$ ""][D$ ""][E$ ""][F$ ""][G$ ""][H$ ""][I$ ""][J$ ""][K$ ""][L$ ""][M$ ""][N$ ""][O$ ""][P$ ""][Q$ ""][R$ ""][S$ ""][T$ ""][U$ ""][V$ ""][W$ ""][X$ ""][Y$ ""][Z$ ""])
(define #'(basic-module-begin PARSE-TREE ...)
#'(#%module-begin
(inject-language-variables (A B C D E F G H I J K L M N O P Q R S T U V W X Y Z A$ B$ C$ D$ E$ F$ G$ H$ I$ J$ K$ L$ M$ N$ O$ P$ Q$ R$ S$ T$ U$ V$ W$ X$ Y$ Z$)
(println (quote PARSE-TREE ...))
PARSE-TREE ...)))
; #%app and #%datum have to be present to make #%top work
(define #'(basic-top . id)
#'(begin
(displayln (format "got unbound identifier: ~a" 'id))
(procedure-rename (λ xs (cons 'id xs)) (string->symbol (format "undefined:~a" 'id)))))
(define #'(program LINE ...) #'(run (list LINE ...)))
(define (run lines)
(define program-lines (list->vector (filter (λ(ln) (not (equal? ln "cr"))) lines)))
(void (for/fold ([line-idx 0])
([i (in-naturals)]
#:break (= line-idx (vector-length program-lines)))
(match-define (cons line-number proc)
(vector-ref program-lines line-idx))
(define maybe-jump-number (and proc (proc)))
(if (number? maybe-jump-number)
(let ([jump-number maybe-jump-number])
(for/or ([idx (in-range (vector-length program-lines))])
(and (= (car (vector-ref program-lines idx)) jump-number)
idx)))
(add1 line-idx)))))
(define #'(cr-line ARG ...) #'(begin ARG ...))
(define #'(line NUMBER STATEMENT ...)
#'(cons NUMBER (λ _ STATEMENT ...)))
(define vars (make-hasheq))
(define-cases #'statement
[#'(statement ID "=" EXPR) #'(set! ID EXPR)]
[#'(statement PROC ARG ...) #'(PROC ARG ...)])
(define-cases #'value
[#'(value "(" EXPR ")") #'EXPR]
[#'(value ID "(" ARG ... ")") #'(ID ARG ...)]
[#'(value ID-OR-DATUM) #'ID-OR-DATUM])
(define-cases expr
[(_ lexpr op rexpr) (if (op lexpr rexpr) 1 0)]
[(_ expr) expr])
(provide < > <= >=)
(define-cases sum
[(_ term op sum) (op term sum)]
[(_ term) term])
(provide - +)
(define-cases product
[(_ factor op product) (op factor product)]
[(_ factor) factor])
(provide * /)
(define print-list list)
(define (PRINT args)
(match args
[(list) (displayln "")]
[(list print-list-item ... ";" pl) (begin (for-each display print-list-item) (PRINT pl))]
[(list print-list-item ... ";") (for-each display print-list-item)]
[(list print-list-item ...) (for-each displayln print-list-item)]))
(define (TAB num) (make-string num #\space))
(define (INT num) (inexact->exact (round num)))
(define (SIN num) (sin num))
(define (RND num) (* (random) num))
(define #'(INPUT PRINT-LIST ";" ID)
#'(begin
(PRINT (append PRINT-LIST (list ";")))
(set! ID (read-line))))
(define (GOTO where)
where)
(define (comment . args) void)