fixes
parent
5d7242fccb
commit
3d8f7f2ded
@ -1,4 +1,14 @@
|
|||||||
#lang br/quicklang
|
#lang br/quicklang
|
||||||
(module reader br
|
(require "parser.rkt" "tokenizer.rkt")
|
||||||
(require "reader.rkt")
|
|
||||||
(provide read-syntax))
|
(module+ reader (provide read-syntax))
|
||||||
|
|
||||||
|
(define (read-syntax path port)
|
||||||
|
(define-values (line col pos) (port-next-location port))
|
||||||
|
(define port+newline (input-port-append #f port (open-input-string "\n")))
|
||||||
|
(port-count-lines! port+newline)
|
||||||
|
(set-port-next-location! port+newline line col pos)
|
||||||
|
(define parse-tree (parse path (tokenize port+newline)))
|
||||||
|
(strip-bindings
|
||||||
|
#`(module basic-mod basic-demo/expander
|
||||||
|
#,parse-tree)))
|
@ -1,6 +0,0 @@
|
|||||||
#lang br
|
|
||||||
(require br/reader-utils "parser.rkt" "tokenizer.rkt")
|
|
||||||
|
|
||||||
(define-read-and-read-syntax (source-path input-port)
|
|
||||||
#`(module bf-mod basic-demo/expander
|
|
||||||
#,(parse source-path (tokenize input-port))))
|
|
@ -1,34 +1,46 @@
|
|||||||
#lang br
|
#lang br
|
||||||
(require parser-tools/lex parser-tools/lex-sre
|
(require parser-tools/lex (prefix-in : parser-tools/lex-sre)
|
||||||
brag/support
|
brag/support racket/string)
|
||||||
racket/string)
|
|
||||||
(provide tokenize)
|
(provide tokenize)
|
||||||
|
|
||||||
(define-lex-abbrevs
|
(define-lex-abbrevs
|
||||||
(natural (repetition 1 +inf.0 numeric))
|
(positive-integer (:+ numeric))
|
||||||
;; don't lex the leading "-": muddles "-X" and "Y-X"
|
;; don't lex the leading "-": muddles "-X" and "Y-X"
|
||||||
(number (union (seq natural)
|
(positive-number (:or positive-integer (:seq (:? positive-integer) (:seq "." positive-integer)))))
|
||||||
(seq (? natural) (seq "." natural))))
|
|
||||||
(quoted-string (seq "\"" (repetition 0 +inf.0 (char-complement "\"")) "\"")))
|
|
||||||
|
|
||||||
(define (tokenize input-port)
|
(define (tokenize ip)
|
||||||
|
(port-count-lines! ip)
|
||||||
(define (next-token)
|
(define (next-token)
|
||||||
(define get-token
|
(define get-token
|
||||||
(lexer-src-pos
|
(lexer-src-pos
|
||||||
[(eof) eof]
|
[(eof) eof]
|
||||||
[(seq "/*" (complement (seq any-string "*/" any-string)) "*/") (get-token input-port)]
|
[whitespace (next-token)]
|
||||||
[(union #\tab #\space #\newline
|
[(from/to "/*" "*/") (next-token)]
|
||||||
(seq number " REM" (repetition 0 +inf.0 (char-complement #\newline)) #\newline)) (get-token input-port)]
|
[(:: positive-number (:+ whitespace) (from/to (uc+lc "rem") "\n")) (next-token)]
|
||||||
[(union "PRINT" "print" "FOR" "for" "TO" "to" "STEP" "step" "IF" "if"
|
[(:or (uc+lc "print" "for" "to" "step" "if"
|
||||||
"GOTO" "goto" "INPUT" "input" "LET" "let" "NEXT" "next"
|
"goto" "input" "let" "next"
|
||||||
"RETURN" "return" "CLEAR" "clear" "LIST" "list" "RUN" "run"
|
"return" "clear" "list" "run"
|
||||||
"END" "end" "THEN" "then" "ELSE" "else" "GOSUB" "gosub"
|
"end" "then" "else" "gosub"
|
||||||
"AND" "and" "OR" "or" "STOP" "stop" "LET" "let" "DEF" "def" "DIM" "dim" "ON" "on"
|
"and" "or" "stop" "let" "def" "dim" "on")
|
||||||
";" "=" "(" ")" "+" "-" "*" "/" "^"
|
";" "=" "(" ")" "+" "-" "*" "/" "^"
|
||||||
"<=" ">=" "<>" "<" ">" "=" ":" ",") (string-downcase lexeme)]
|
"<=" ">=" "<>" "<" ">" "=" ":" ",") (string-downcase lexeme)]
|
||||||
[number (token 'NUMBER (string->number lexeme))]
|
[positive-number (token 'NUMBER (string->number lexeme)
|
||||||
[(seq upper-case (repetition 0 +inf.0 (or upper-case numeric)) (? "$")) (token 'ID (string->symbol lexeme))]
|
#:position (pos lexeme-start)
|
||||||
[quoted-string (token 'STRING (string-trim lexeme "\""))]))
|
#:line (line lexeme-start)
|
||||||
(get-token input-port))
|
#:column (col lexeme-start)
|
||||||
next-token)
|
#:span (- (pos lexeme-end)
|
||||||
|
(pos lexeme-start)))]
|
||||||
|
[(:: alphabetic (:* (:or alphabetic numeric)) (:? "$")) (token 'ID (string->symbol lexeme)
|
||||||
|
#:position (pos lexeme-start)
|
||||||
|
#:line (line lexeme-start)
|
||||||
|
#:column (col lexeme-start)
|
||||||
|
#:span (- (pos lexeme-end)
|
||||||
|
(pos lexeme-start)))]
|
||||||
|
[(from/to "\"" "\"") (token 'STRING (trim-ends "\"" lexeme "\"")
|
||||||
|
#:position (+ (pos lexeme-start) 1)
|
||||||
|
#:line (line lexeme-start)
|
||||||
|
#:column (+ (col lexeme-start) 1)
|
||||||
|
#:span (- (pos lexeme-end)
|
||||||
|
(pos lexeme-start) 2))]))
|
||||||
|
(get-token ip))
|
||||||
|
next-token)
|
Loading…
Reference in New Issue