refactor
parent
50eb4964cd
commit
6221ca5959
@ -0,0 +1,21 @@
|
|||||||
|
#lang br
|
||||||
|
(require brag/support racket/contract)
|
||||||
|
|
||||||
|
(define basic-lexer
|
||||||
|
(lexer-srcloc
|
||||||
|
[(eof) eof]
|
||||||
|
[whitespace (token lexeme #:skip? #t)]
|
||||||
|
[(from/to "rem" "\n") (token 'REM lexeme)]
|
||||||
|
[(:or "print" "goto" "end" "+" ":") lexeme]
|
||||||
|
[(:+ numeric) (token 'INTEGER (string->number lexeme))]
|
||||||
|
[(:or (:seq (:+ numeric) ".")
|
||||||
|
(:seq (:* numeric) "." (:+ numeric)))
|
||||||
|
(token 'DECIMAL (string->number lexeme))]
|
||||||
|
[(from/to "\"" "\"")
|
||||||
|
(token 'STRING (trim-ends "\"" lexeme "\""))]))
|
||||||
|
|
||||||
|
(provide
|
||||||
|
(contract-out
|
||||||
|
[basic-lexer
|
||||||
|
(input-port? . -> .
|
||||||
|
(or/c eof-object? string? srcloc-token?))]))
|
@ -1,15 +1,16 @@
|
|||||||
#lang br/quicklang
|
#lang br/quicklang
|
||||||
(require "parser.rkt" "tokenizer.rkt" brag/support)
|
(require "parser.rkt" "tokenizer.rkt")
|
||||||
|
|
||||||
(module+ reader (provide read-syntax))
|
(module+ reader (provide read-syntax))
|
||||||
|
|
||||||
(define (read-syntax path port)
|
(define (read-syntax path port)
|
||||||
(define-values (line col pos) (port-next-location port))
|
(define-values (pline pcol ppos) (port-next-location port))
|
||||||
(define port+newline (input-port-append #f port (open-input-string "\n")))
|
(define port+newline
|
||||||
|
(input-port-append #f port (open-input-string "\n")))
|
||||||
(port-count-lines! port+newline)
|
(port-count-lines! port+newline)
|
||||||
(set-port-next-location! port+newline line col pos)
|
(set-port-next-location! port+newline pline pcol ppos)
|
||||||
(with-handlers ([exn:fail:parsing? (λ (exn) (displayln "Sorry!") (raise exn))])
|
(define parse-tree
|
||||||
(define parse-tree (parse path (make-tokenizer port+newline)))
|
(parse path (make-tokenizer port+newline path)))
|
||||||
(strip-bindings
|
(strip-bindings
|
||||||
#`(module basic-mod basic-demo/expander
|
#`(module basic-mod basic-demo/expander
|
||||||
#,parse-tree))))
|
#,parse-tree)))
|
@ -1,8 +1,8 @@
|
|||||||
#lang basic-demo
|
#lang basic-demo
|
||||||
30 rem print "statement ignored"
|
30 rem print "ignored"
|
||||||
40 end
|
|
||||||
50 print "never gets here"
|
50 print "never gets here"
|
||||||
60 print "two" : print 1.2 + 1.8
|
40 end
|
||||||
|
60 print "three" : print 2 + 2
|
||||||
70 goto 11 + 10.5 + 8.5
|
70 goto 11 + 10.5 + 8.5
|
||||||
10 print "one"
|
10 print "one"
|
||||||
20 print : goto 60 : end
|
20 print : goto 60 : end
|
@ -1,20 +1,14 @@
|
|||||||
#lang br
|
#lang br
|
||||||
(require brag/support)
|
(require "lexer.rkt" brag/support racket/contract)
|
||||||
(provide (all-defined-out))
|
|
||||||
|
|
||||||
(define basic-lexer
|
(define (make-tokenizer ip [path #f])
|
||||||
(lexer-srcloc
|
|
||||||
[(eof) eof]
|
|
||||||
[whitespace (token lexeme #:skip? #t)]
|
|
||||||
[(from/to "rem" "\n") (token 'REM lexeme)]
|
|
||||||
[(:or "print" "goto" "end" "+" ":") lexeme]
|
|
||||||
[(:+ numeric) (token 'INTEGER (string->number lexeme))]
|
|
||||||
[(:or (:seq (:+ numeric) ".")
|
|
||||||
(:seq (:* numeric) "." (:+ numeric)))
|
|
||||||
(token 'DECIMAL (string->number lexeme))]
|
|
||||||
[(from/to "\"" "\"") (token 'STRING (trim-ends "\"" lexeme "\""))]))
|
|
||||||
|
|
||||||
(define (make-tokenizer ip)
|
|
||||||
(port-count-lines! ip)
|
(port-count-lines! ip)
|
||||||
|
(file-path path)
|
||||||
(define (next-token) (basic-lexer ip))
|
(define (next-token) (basic-lexer ip))
|
||||||
next-token)
|
next-token)
|
||||||
|
|
||||||
|
(provide
|
||||||
|
(contract-out
|
||||||
|
[make-tokenizer
|
||||||
|
((input-port?) (path?) . ->* .
|
||||||
|
(-> (or/c eof-object? string? srcloc-token?)))]))
|
Loading…
Reference in New Issue