pull/10/head
Matthew Butterick 8 years ago
parent 33fe21405f
commit 32acbeb501

@ -3,8 +3,7 @@
(define-lex-abbrev digits (:+ (char-set "0123456789"))) (define-lex-abbrev digits (:+ (char-set "0123456789")))
(define (handle-tok-error tok-ok? tok-name tok-value start-pos end-pos) (define-lex-abbrev reserved-terms (:or "print" "goto" "end" "+" ":" "let" "=" "gosub" "return" "-" "for" "to" "step" "next" "if" "then" "else" "and" "or" "<" ">" "*" "/" "(" ")" "^" "!" "%" "input" ";" "def"))
(token 'ERROR tok-value))
(define basic-lexer (define basic-lexer
(lexer-srcloc (lexer-srcloc
@ -12,9 +11,9 @@
["\n" (token 'NEWLINE lexeme)] ["\n" (token 'NEWLINE lexeme)]
[whitespace (token lexeme #:skip? #t)] [whitespace (token lexeme #:skip? #t)]
[(from/stop-before "rem" "\n") (token 'REM lexeme)] [(from/stop-before "rem" "\n") (token 'REM lexeme)]
[(:or "print" "goto" "end" "+" ":" "gosub" "return" "let" "=" "-" "for" "to" "step" "next" [reserved-terms (token lexeme lexeme)]
"if" "then" "else" "and" "or" "<" ">" "*" "/" "(" ")" "^" "!" "%" "input" ";" "def") (token lexeme lexeme)] [(:seq alphabetic (:* (:or alphabetic numeric "$")))
[(:seq (:+ alphabetic) (:* (:or alphabetic numeric "$"))) (token 'ID (string->symbol lexeme))] (token 'ID (string->symbol lexeme))]
[digits (token 'INTEGER (string->number lexeme))] [digits (token 'INTEGER (string->number lexeme))]
[(:or (:seq (:? digits) "." digits) [(:or (:seq (:? digits) "." digits)
(:seq digits ".")) (:seq digits "."))

@ -1,22 +1,22 @@
#lang brag #lang brag
;; program & lines ;; program & lines
b-program : [b-line] (/NEWLINE [b-line])* b-program : [b-line] (/NEWLINE [b-line])*
b-line : b-line-number [b-statement] (/":" [b-statement])* [b-rem] b-line : b-line-num [b-statement] (/":" [b-statement])* [b-rem]
@b-line-number : INTEGER @b-line-num : INTEGER
b-rem : REM b-rem : REM
@b-statement : b-rem | b-end | b-print | b-goto | b-let
;; statements | b-input | b-def | b-gosub
@b-statement : b-end | b-print | b-let | b-input | b-def | b-return | b-for | b-next | b-if
| b-goto | b-gosub | b-return | b-for | b-next | b-if
b-end : /"end" b-end : /"end"
b-print : /"print" [STRING | b-expr] (/";" [STRING | b-expr])* b-print : /"print" [b-printable] (/";" [b-printable])*
@b-printable : STRING | b-expr
b-goto : /"goto" b-expr b-goto : /"goto" b-expr
b-let : [/"let"] b-id /"=" (STRING | b-expr)
b-if : /"if" b-expr /"then" b-expr [/"else" b-expr] b-if : /"if" b-expr /"then" b-expr [/"else" b-expr]
b-gosub : /"gosub" b-expr b-gosub : /"gosub" b-expr
b-return : /"return" b-return : /"return"
b-input : /"input" b-id b-input : /"input" b-id
b-def : /"def" b-id /"(" b-id /")" /"=" b-expr b-def : /"def" b-id /"(" b-id /")" /"=" b-expr
b-let : [/"let"] b-id /"=" [STRING | b-expr]
b-for : /"for" b-id /"=" b-expr /"to" b-expr [/"step" b-expr] b-for : /"for" b-id /"=" b-expr /"to" b-expr [/"step" b-expr]
b-next : /"next" [b-id] b-next : /"next" [b-id]

@ -1,7 +1,7 @@
#lang brag #lang brag
b-program : [b-line] (/NEWLINE [b-line])* b-program : [b-line] (/NEWLINE [b-line])*
b-line : b-line-number [b-statement] (/":" [b-statement])* b-line : b-line-num [b-statement] (/":" [b-statement])*
@b-line-number : INTEGER @b-line-num : INTEGER
@b-statement : b-rem | b-end | b-print | b-goto @b-statement : b-rem | b-end | b-print | b-goto
b-rem : REM b-rem : REM
b-end : /"end" b-end : /"end"

@ -1,8 +1,8 @@
#lang brag #lang brag
;; program & lines ;; program & lines
b-program : [b-line] (/NEWLINE [b-line])* b-program : [b-line] (/NEWLINE [b-line])*
b-line : b-line-number [b-statement] (/":" [b-statement])* b-line : b-line-num [b-statement] (/":" [b-statement])*
@b-line-number : INTEGER @b-line-num : INTEGER
;; statements ;; statements
@b-statement : b-rem | b-end | b-print | b-let | b-input | b-def @b-statement : b-rem | b-end | b-print | b-let | b-input | b-def

Loading…
Cancel
Save