dev-elider-3
Matthew Butterick 9 years ago
parent aeac5dcd31
commit 7164c4afec

@ -2,38 +2,87 @@
(require (for-syntax racket/base syntax/parse racket/syntax syntax/datum syntax/strip-context) sugar/define) (require (for-syntax racket/base syntax/parse racket/syntax syntax/datum syntax/strip-context) sugar/define)
(provide (all-defined-out)) (provide (all-defined-out))
;; everything is prefixed br: whether it needs it or not so it can be stripped by #lang br
(define-syntax-rule (br:debug-define (syntax (id pat-arg ... . rest-arg)) body-exp) ;; todo: support `else` case
(br:define #'(id pat-arg ... . rest-arg) (define-syntax (br:define-cases stx)
#`(begin (define-syntax-class syntaxed-id
(for-each displayln #:literals (syntax)
(list #:description "id in syntaxed form"
(format "input pattern = #'~a" '#,'(id pat-arg ... . rest-arg)) (pattern (syntax name:id)))
(format "output pattern = #'~a" (cadr '#,'body-exp))
(format "invoked as = ~a" (syntax->datum #'(id pat-arg ... . rest-arg))) (define-syntax-class syntaxed-thing
(format "expanded as = ~a" '#,(syntax->datum body-exp)) #:literals (syntax)
(format "evaluated as = ~a" #,body-exp))) #:description "some datum in syntaxed form"
#,body-exp))) (pattern (syntax thing:expr)))
(syntax-parse stx
#:literals (syntax)
;; defective for syntax or function
[(_ top-id)
(raise-syntax-error 'define-cases "no cases given" (syntax->datum #'top-id))]
;; defective for syntax
[(_ (sid:syntaxed-id _ ...) _ ...) ; (define (#'f1 stx) expr ...)
(raise-syntax-error 'define-cases "definition of a syntax transformer must use lambda notation, because otherwise it's too easy to confuse the compile-time shape and the run-time shape" (syntax->datum #'sid.name))]
;; syntax matcher
[(_ top-id:syntaxed-id [(syntax pat) body ...] ...+)
#'(define-syntax top-id.name (λ (stx)
(define result
(syntax-case stx ()
[pat body ...] ...
[else (raise-syntax-error 'define-cases (format "no matching case for syntax pattern `~a`" (syntax->datum stx)) (syntax->datum #'top-id.name))]))
(if (not (syntax? result))
(datum->syntax stx result)
result)))]
;; function matcher
[(_ top-id:id [(_ pat-arg ... . rest-arg) body ...] ...)
#'(define top-id
(case-lambda
[(pat-arg ... . rest-arg) body ...] ...
[else (raise-syntax-error 'define-cases "no matching case for argument pattern" (object-name top-id))]))]))
(module+ test (module+ test
(require rackunit racket/port) (require rackunit)
(parameterize ([current-output-port (open-output-nowhere)]) (define foo-val 'got-foo-val)
(check-equal? (let () (define (foo-func) 'got-foo-func)
(br:debug-define #'(foo X Y Z) (br:define-cases #'op
#'(apply + (list X Y Z))) [#'(_ "+") #''got-plus]
(foo 1 2 3)) 6) [#'(_ arg) #''got-something-else]
(check-equal? (let () [#'(_) #'(foo-func)]
(br:debug-define #'(foo X ...) #'(apply * (list X ...))) [#'_ #'foo-val])
(foo 10 11 12)) 1320)))
(check-equal? (op "+") 'got-plus)
(check-equal? (op 42) 'got-something-else)
(check-equal? (op) 'got-foo-func)
(check-equal? op 'got-foo-val)
(br:define-cases f
[(_ arg) (add1 arg)]
[(_ arg1 arg2) (+ arg1 arg2)])
(check-equal? (f 42) 43)
(check-equal? (f 42 5) 47)
;; todo: error from define-cases not trapped by check-exn
;;(check-exn exn:fail:syntax? (λ _ (define-cases (#'times stx stx2) #'*)))
)
(define-syntax (br:define stx) (define-syntax (br:define stx)
;;todo: share syntax classes
(define-syntax-class syntaxed-id (define-syntax-class syntaxed-id
#:literals (syntax) #:literals (syntax)
#:description "id in syntaxed form" #:description "id in syntaxed form"
(pattern (syntax name:id))) (pattern (syntax name:id)))
(define-syntax-class syntaxed-thing (define-syntax-class syntaxed-thing
#:literals (syntax) #:literals (syntax)
#:description "some datum in syntaxed form" #:description "some datum in syntaxed form"
@ -41,23 +90,19 @@
(syntax-parse stx (syntax-parse stx
#:literals (syntax) #:literals (syntax)
;; syntax
[(_ (syntax (id pat-arg ... . rest-arg)) body ...) ; (define #'(foo arg) #'(+ arg arg)) [(_ (syntax (id pat-arg ... . rest-arg)) body ...) ; (define #'(foo arg) #'(+ arg arg))
#'(define-syntax id (λ (stx) #'(br:define-cases (syntax id) [(syntax (_ pat-arg ... . rest-arg)) body ...])]
(define result
(syntax-case stx ()
[(_ pat-arg ... . rest-arg) body ...]))
(if (not (syntax? result))
(datum->syntax stx result)
result)))]
[(_ sid:syntaxed-id sid2:syntaxed-id) ; (define #'f1 #'f2) [(_ sid:syntaxed-id sid2:syntaxed-id) ; (define #'f1 #'f2)
#'(define-syntax sid.name (make-rename-transformer sid2))] #'(define-syntax sid.name (make-rename-transformer sid2))]
[(_ sid:syntaxed-id sid2:syntaxed-thing) ; (define #'f1 #'42) [(_ (syntax id) (syntax thing)) ; (define #'f1 #'42)
#'(define-syntax sid.name (λ (stx) sid2))] #'(br:define-cases (syntax id) [#'_ (syntax thing)])]
[(_ (sid:syntaxed-id stx-arg ...) expr ...) ; (define (#'f1 stx) expr ...) [(_ (sid:syntaxed-id stx-arg ...) expr ...) ; (define (#'f1 stx) expr ...)
(raise-syntax-error 'define "definition of a syntax transformer must use lambda notation, because otherwise it's too easy to confuse the compile-time shape and the run-time shape" (syntax->datum #'sid.name))] (raise-syntax-error 'define "definition of a syntax transformer must use lambda notation, because otherwise it's too easy to confuse the compile-time shape and the run-time shape" (syntax->datum #'sid.name))]
[(_ sid:syntaxed-id (λ (stx-arg ...) expr ...)) ; (define #'f1 (λ(stx) expr ...) [(_ sid:syntaxed-id (λ (stx-arg ...) expr ...)) ; (define #'f1 (λ(stx) expr ...)
#:fail-when (not (= (length (syntax->datum #'(stx-arg ...))) 1)) #:fail-when (not (= (length (syntax->datum #'(stx-arg ...))) 1))
@ -69,63 +114,53 @@
(module+ test (module+ test
(require rackunit) (require rackunit)
(br:define #'plus (λ(stx) #'+)) (br:define #'plus (λ(stx) #'+))
(br:define #'plusser #'plus)
(br:define #'(times arg) #'(* arg arg))
(br:define #'timeser #'times)
(br:define #'fortytwo #'42)
(check-equal? (plus 42) +) (check-equal? (plus 42) +)
(check-equal? plusser +) (br:define #'plusser #'plus)
(check-equal? (plusser 42) +) (check-equal? (plusser 42) +)
(check-equal? plusser +)
(br:define #'(times arg) #'(* arg arg))
(check-equal? (times 10) 100) (check-equal? (times 10) 100)
(br:define #'timeser #'times)
(check-equal? (timeser 12) 144) (check-equal? (timeser 12) 144)
(br:define #'fortytwo #'42)
(check-equal? fortytwo 42)
(check-equal? (let () (check-equal? (let ()
(br:define #'(foo x) (br:define #'(foo x)
(with-syntax ([zam +]) (with-syntax ([zam +])
#'(zam x x))) (foo 42)) 84) #'(zam x x))) (foo 42)) 84)
;; todo: error from define not trapped by check-exn ;; todo: error from define not trapped by check-exn
#;(check-exn exn:fail:syntax? (λ _ (br:define (#'times stx stx2) #'*))) #;(check-exn exn:fail:syntax? (λ _ (br:define (#'times stx stx2) #'*)))
(check-equal? fortytwo 42)
(begin (begin
(br:define #'(redefine ID) #'(define ID 42)) (br:define #'(redefine ID) #'(define ID 42))
(redefine zoombar) (redefine zoombar)
(check-equal? zoombar 42))) (check-equal? zoombar 42)))
;; todo: support `else` case (define-syntax-rule (br:debug-define (syntax (id pat-arg ... . rest-arg)) body-exp)
(define-syntax (br:define-cases stx) (br:define #'(id pat-arg ... . rest-arg)
(syntax-parse stx #`(begin
#:literals (syntax) (for-each displayln
; (define-cases #'foo [#'(_ arg) #'(+ arg arg)] [#'(_ 42 bar) #'42] ...) (list
[(_ (syntax top-id) [(syntax (_ pat-arg ... . rest-arg)) body ...] ...) (format "input pattern = #'~a" '#,'(id pat-arg ... . rest-arg))
#'(define-syntax top-id (λ (stx) (format "output pattern = #'~a" (cadr '#,'body-exp))
(define result (format "invoked as = ~a" (syntax->datum #'(id pat-arg ... . rest-arg)))
(syntax-case stx () (format "expanded as = ~a" '#,(syntax->datum body-exp))
[(_ pat-arg ... . rest-arg) body ...] ...)) (format "evaluated as = ~a" #,body-exp)))
(if (not (syntax? result)) #,body-exp)))
(datum->syntax stx result)
result)))]
[(_ top-id [(_ pat-arg ... . rest-arg) body ...] ...)
#'(define top-id
(case-lambda
[(pat-arg ... . rest-arg) body ...] ...))]))
(module+ test
(br:define-cases #'op
[#'(_ "+") #''got-plus]
[#'(_ arg) #''got-something-else])
(check-equal? (op "+") 'got-plus) (module+ test
(check-equal? (op 42) 'got-something-else) (require rackunit racket/port)
(parameterize ([current-output-port (open-output-nowhere)])
(br:define-cases f (check-equal? (let ()
[(_ arg) (add1 arg)] (br:debug-define #'(foo X Y Z)
[(_ arg1 arg2) (+ arg1 arg2)]) #'(apply + (list X Y Z)))
(foo 1 2 3)) 6)
(check-equal? (let ()
(br:debug-define #'(foo X ...) #'(apply * (list X ...)))
(foo 10 11 12)) 1320)))
(check-equal? (f 42) 43)
(check-equal? (f 42 5) 47))
(define-syntax-rule (br:define+provide arg ...) (define-syntax-rule (br:define+provide arg ...)
(define+provide arg ...)) (define+provide arg ...))

@ -3,7 +3,10 @@
(rename-out [basic-module-begin #%module-begin]) (rename-out [basic-module-begin #%module-begin])
(rename-out [basic-top #%top]) (rename-out [basic-top #%top])
(all-defined-out)) (all-defined-out))
(require br/stxparam) (require br/stxparam (for-syntax br/datum))
; BASIC implementation details
; http://www.atariarchives.org/basicgames/showpage.php?page=i12
(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-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$ ""])
@ -36,16 +39,19 @@
(exn:line-not-found (exn:line-not-found
(format "line number ~a not found in program" ln) (format "line number ~a not found in program" ln)
(current-continuation-marks))))) (current-continuation-marks)))))
(void (with-handlers ([exn:program-end? (λ (exn) (void))]) (with-handlers ([exn:program-end? (λ _ (void))])
(for/fold ([program-counter 0]) (for/fold ([program-counter 0])
([i (in-naturals)] ([i (in-naturals)])
#:break (= program-counter (vector-length program-lines))) (cond
(match-define (cons line-number proc) [(= program-counter (vector-length program-lines)) (basic:END)]
(vector-ref program-lines program-counter)) [else
(define maybe-jump-number (and proc (proc))) (match-define (cons line-number proc)
(if (number? maybe-jump-number) (vector-ref program-lines program-counter))
(line-number->index maybe-jump-number) (define maybe-jump-number (and proc (proc)))
(add1 program-counter)))))) (if (number? maybe-jump-number)
(line-number->index maybe-jump-number)
(add1 program-counter))])))
(void))
(define #'(cr-line ARG ...) #'(begin ARG ...)) (define #'(cr-line ARG ...) #'(begin ARG ...))
@ -53,7 +59,7 @@
(define current-return-stack (make-parameter empty)) (define current-return-stack (make-parameter empty))
(define-cases #'line (define-cases #'line
[#'(_ NUMBER (STATEMENT "GOSUB" WHERE)) [#'(_ NUMBER (statement-list (statement "GOSUB" WHERE)))
#'(cons NUMBER #'(cons NUMBER
(λ _ (λ _
(let ([return-stack (current-return-stack)]) (let ([return-stack (current-return-stack)])
@ -61,16 +67,24 @@
[(or (empty? return-stack) [(or (empty? return-stack)
(not (= NUMBER (car return-stack)))) (not (= NUMBER (car return-stack))))
(current-return-stack (cons NUMBER (current-return-stack))) (current-return-stack (cons NUMBER (current-return-stack)))
(GOTO WHERE)] (basic:GOTO WHERE)]
[else (current-return-stack (cdr (current-return-stack)))]))))] [else (current-return-stack (cdr (current-return-stack)))]))))]
[#'(_ NUMBER STATEMENT ...) #'(cons NUMBER (λ _ STATEMENT ...))]) [#'(_ NUMBER STATEMENT-LIST) #'(cons NUMBER (λ _ STATEMENT-LIST))])
(define-cases #'statement-list
[#'(_ STATEMENT) #'(begin STATEMENT)]
[#'(_ STATEMENT ":" STATEMENT-LIST) #'(begin STATEMENT STATEMENT-LIST)])
(define-cases #'statement (define-cases #'statement
[#'(statement ID "=" EXPR) #'(set! ID EXPR)] [#'(statement ID "=" EXPR) #'(set! ID EXPR)]
[#'(statement PROC ARG ...) #'(PROC ARG ...)]) ;[#'(statement "PRINT" ARG ...) #'(print ARG ...)]
;[#'(statement "RETURN" ARG ...) #'(return ARG ...)]
(define-cases #'IF ;[#'(statement "END" ARG ...) #'(end ARG ...)]
[#'(statement PROC-STRING ARG ...)
(inject-syntax ([#'PROC-ID (format-datum "basic:~a" #'PROC-STRING)])
#'(PROC-ID ARG ...))])
(define-cases #'basic:IF
[#'(_ COND "THEN" TRUE-RESULT "ELSE" FALSE-RESULT) [#'(_ COND "THEN" TRUE-RESULT "ELSE" FALSE-RESULT)
#'(if (true? COND) #'(if (true? COND)
TRUE-RESULT TRUE-RESULT
@ -85,72 +99,65 @@
[#'(value ID-OR-DATUM) #'ID-OR-DATUM]) [#'(value ID-OR-DATUM) #'ID-OR-DATUM])
(define true? (compose1 not zero?)) (define true? (compose1 not zero?))
(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)))
(define-cases #'expr (define-cases #'expr
[#'(_ LEXPR "AND" REXPR) [#'(_ COMP-EXPR "AND" EXPR) #'(basic:and COMP-EXPR EXPR)]
#'(if (and (true? LEXPR) (true? REXPR)) 1 0)] [#'(_ COMP-EXPR "OR" EXPR) #'(basic:or COMP-EXPR EXPR)]
[#'(_ LEXPR "OR" REXPR) [#'(_ COMP-EXPR) #'COMP-EXPR])
#'(if (or (true? LEXPR) (true? REXPR)) 1 0)]
[#'(_ EXPR) #'EXPR])
(define-cases #'comp-expr (define-cases #'comp-expr
[#'(_ lexpr "=" rexpr) #'(comp-expr lexpr equal? rexpr)] ; special case because = is overloaded [#'(_ LEXPR "=" REXPR) #'(comp-expr LEXPR "equal?" REXPR)] ; special case because = is overloaded
[#'(_ lexpr op rexpr) #'(if (op lexpr rexpr) 1 0)] [#'(_ LEXPR op REXPR) (inject-syntax ([#'OP (string->symbol (syntax->datum #'op))])
#'(cond->int (OP LEXPR REXPR)))]
[#'(_ expr) #'expr]) [#'(_ expr) #'expr])
(define (<> lexpr rexpr) (not (equal? lexpr rexpr))) (define <> (compose1 not equal?))
(provide < > <= >= <>)
(define-cases sum (define-cases #'sum
[(_ term op sum) (op term sum)] [#'(_ term "+" sum) #'(+ term sum)]
[(_ term) term]) [#'(_ term "-" sum) #'(- term sum)]
(provide - +) [#'(_ term) #'term])
(define-cases product (define-cases #'product
[(_ factor op product) (op factor product)] [#'(_ factor "*" product) #'(* factor product)]
[(_ factor) factor]) [#'(_ factor "/" product) #'(/ factor product)]
(provide * /) [#'(_ factor) #'factor])
(define print-list list) (define print-list list)
(define (PRINT args) (define (basic:PRINT args)
(match args (match args
[(list) (displayln "")] [(list) (displayln "")]
[(list print-list-item ... ";" pl) (begin (for-each display print-list-item) [(list print-list-item ... ";" pl) (begin (for-each display print-list-item)
(display " ") (print pl))]
(PRINT pl))] [(list print-list-item ... ";") (for-each display print-list-item)]
[(list print-list-item ... ";") (begin
(for-each display print-list-item)
(display " "))]
[(list print-list-item ...) (for-each displayln print-list-item)])) [(list print-list-item ...) (for-each displayln print-list-item)]))
(define (TAB num) (make-string num #\space)) (define (TAB num) (make-string num #\space))
(define #'(INT EXPR ...) #'(inexact->exact (round (expr EXPR ...)))) (define #'(INT EXPR ...) #'(inexact->exact (truncate (expr EXPR ...))))
(define (SIN num) (sin num)) (define (SIN num) (sin num))
(define (ABS num) (inexact->exact (abs num))) (define (ABS num) (inexact->exact (abs num)))
(define (RND num) (* (random) num)) (define (RND num) (* (random) num))
(define-cases #'INPUT (define-cases #'basic:INPUT
[#'(_ PRINT-LIST ";" ID) [#'(_ PRINT-LIST ";" ID)
#'(begin #'(begin
(PRINT (append PRINT-LIST (list ";"))) (basic:PRINT (append PRINT-LIST (list ";")))
(INPUT ID))] (basic:INPUT ID))]
[#'(_ ID) #'(set! ID (let* ([str (read-line)] [#'(_ ID) #'(set! ID (let* ([str (read-line)]
[num (string->number str)]) [num (string->number str)])
(if num num str)))]) (if num num str)))])
(define (GOTO where) (define (basic:GOTO where) where)
where)
(define (RETURN) (define (basic:RETURN) (car (current-return-stack)))
(car (current-return-stack)))
(struct exn:program-end exn:fail ()) (struct exn:program-end exn:fail ())
(define (END) (define (basic:END)
(raise (raise
(exn:program-end (exn:program-end
"program ended" "program ended"
(current-continuation-marks)))) (current-continuation-marks))))
(define (comment . args) void)

@ -1,21 +1,20 @@
#lang ragg #lang ragg
;; recursive rules destucture easily in the expander ;; recursive rules destucture easily in the expander
program : [line [CR line]*] program : [CR]* [line [CR line]*] [CR]*
line: INTEGER statement+ line: NUMBER statement-list
statement-list : statement [":" statement-list]
statement : "END" statement : "END"
| "FOR" ID "=" expr "TO" expr ["STEP" expr] | "GOSUB" NUMBER
| "GOSUB" INTEGER
| "GOTO" expr | "GOTO" expr
| "IF" expr "THEN" (statement | expr) ["ELSE" (statement | expr)]; change: add expr | "IF" expr "THEN" (statement | expr) ["ELSE" (statement | expr)]
| "INPUT" [print-list ";"] ID | "INPUT" [print-list ";"] ID
| ["LET"] ID "=" expr ; change: make "LET" opt | ID "=" expr ; change: make "LET" opt
| "NEXT" ID+
| "PRINT" print-list | "PRINT" print-list
| "RETURN" | "RETURN"
| REM-COMMENT
print-list : [expr [";" [print-list]]] print-list : [expr [";" [print-list]]]
@ -28,9 +27,7 @@ sum : product [("+" | "-") sum]
product : value [("*" | "/") product] product : value [("*" | "/") product]
value : "(" expr ")" value : "(" expr ")"
| ID | ID ["(" expr* ")"]
| PROC "(" expr* ")"
| INTEGER
| STRING | STRING
| REAL | NUMBER

@ -2,6 +2,5 @@
(require br/reader-utils "parser.rkt" "tokenizer.rkt") (require br/reader-utils "parser.rkt" "tokenizer.rkt")
(define-read-and-read-syntax (source-path input-port) (define-read-and-read-syntax (source-path input-port)
(strip-context #`(module bf-mod br/demo/basic/expander
#`(module bf-mod br/demo/basic/expander #,(parse source-path (tokenize input-port))))
#,(parse source-path (tokenize (open-input-string (string-trim (port->string input-port))))))))

@ -5,34 +5,30 @@
racket/string) racket/string)
(provide tokenize) (provide tokenize)
(define-lex-abbrevs
(natural (repetition 1 +inf.0 numeric))
(integer (:seq (:? "-") natural))
(number (:seq integer (:? (:seq "." natural))))
(quoted-string (:seq "\"" (repetition 0 +inf.0 (char-complement "\"")) "\"")))
(define (tokenize input-port) (define (tokenize input-port)
(define (next-token) (define (next-token)
(define get-token (define get-token
(lexer (lexer
[(:seq "REM" (repetition 1 +inf.0 (char-complement "\n"))) [(eof) eof]
(token 'REM-COMMENT (format-datum '(comment "~v") lexeme))] [(union #\tab #\space
[(repetition 1 +inf.0 "\n") (token 'CR "cr")] (:seq number " REM" (repetition 1 +inf.0 (char-complement #\newline)) #\newline)) (get-token input-port)]
[(:seq #\newline (repetition 0 +inf.0 whitespace)) (token 'CR "cr")]
[(union "PRINT" "FOR" "TO" "STEP" "IF" "GOTO" [(union "PRINT" "FOR" "TO" "STEP" "IF" "GOTO"
"INPUT" "LET" "NEXT" "RETURN" "INPUT" "LET" "NEXT" "RETURN"
"CLEAR" "LIST" "RUN" "END") (string->symbol lexeme)] "CLEAR" "LIST" "RUN" "END"
[(union "THEN" "ELSE" "GOSUB") lexeme] "THEN" "ELSE" "GOSUB" "AND" "OR"
";" "=" "(" ")" "+" "-" "*" "/"
;; this only matches integers "<=" ">=" "<>" "><" "<" ">" "=" ":") lexeme]
[(repetition 1 +inf.0 numeric) (token 'INTEGER (string->number lexeme))] [(union ",") (get-token input-port)]
[(repetition 1 +inf.0 (union "." numeric)) (token 'REAL (string->number lexeme))] [number (token 'NUMBER (string->number lexeme))]
;; things that get thrown out: pass through as strings,
;; because they can be matched literally in macros.
;; things that become identifiers: pass through as symbols,
;; so they can get bound by the expander.
[(union "," ":") (token 'SEPARATOR lexeme #:skip? #t)]
[(union ";" "=" "(" ")") lexeme]
[(union "+" "-" "*" "/"
"<=" ">=" "<>" "><" "<" ">" "=" ) (string->symbol lexeme)]
[(union "RND" "INT" "TAB" "SIN" "ABS") (token 'PROC (string->symbol lexeme))]
[(:seq (repetition 1 +inf.0 upper-case) (:? "$")) (token 'ID (string->symbol lexeme))] [(:seq (repetition 1 +inf.0 upper-case) (:? "$")) (token 'ID (string->symbol lexeme))]
[upper-case (token 'UPPERCASE (string->symbol lexeme))] [upper-case (token 'UPPERCASE (string->symbol lexeme))]
[whitespace (token 'WHITESPACE lexeme #:skip? #t)] [quoted-string (token 'STRING (string-trim lexeme "\""))]))
[(:seq "\"" (complement (:: any-string "\"" any-string)) "\"") (token 'STRING (string-trim lexeme "\""))]
[(eof) eof]))
(get-token input-port)) (get-token input-port))
next-token) next-token)

Loading…
Cancel
Save