fiddle with `case-pattern`

pull/10/head
Matthew Butterick 8 years ago
parent 17785714fe
commit 706e20b5fb

@ -2,11 +2,24 @@
(require "go.rkt") (require "go.rkt")
(provide b-if b-comp-expr b-logic-expr) (provide b-if b-comp-expr b-logic-expr)
;; b-if : /"if" b-expr /"then" b-expr [/"else" b-expr]
(define (b-if cond-expr then-expr [else-expr #f]) #|
(cond explain why this won't work due to premature eval of THEN & ELSE
[(not (zero? cond-expr)) (b-goto then-expr)] (define (b-if COND THEN ELSE)
[else-expr => b-goto])) (let ([result (if (not (zero? COND))
THEN
ELSE)])
(when (exact-positive-integer? result)
(b-goto result))))
|#
(define-macro-cases b-if
[(_ COND THEN) #'(b-if COND THEN #f)]
[(_ COND THEN ELSE) #'(let ([result (if (not (zero? COND))
THEN
ELSE)])
(when (exact-positive-integer? result)
(b-goto result)))])
(define bool-int (λ (val) (if val 1 0))) (define bool-int (λ (val) (if val 1 0)))
(define bi= (compose1 bool-int =)) (define bi= (compose1 bool-int =))

@ -3,7 +3,7 @@
(define-lex-abbrev digits (:+ (char-set "0123456789"))) (define-lex-abbrev digits (:+ (char-set "0123456789")))
(define-lex-abbrev reserved-terms (:or "print" "goto" "end" "+" ":" ";" "let" "=" "input" "-" "*" "/" "^" "mod" "(" ")" "def" "if" "then" "else" "and" "or" "<" ">" "!" "gosub" "return" "for" "to" "step" "next")) (define-lex-abbrev reserved-terms (:or "print" "goto" "end" "+" ":" ";" "let" "=" "input" "-" "*" "/" "^" "mod" "(" ")" "def" "if" "then" "else" "and" "or" "not" "<" ">" "<>" "gosub" "return" "for" "to" "step" "next"))
(define basic-lexer (define basic-lexer
(lexer-srcloc (lexer-srcloc

@ -12,7 +12,7 @@ b-print : /"print" [b-printable] (/";" [b-printable])*
@b-printable : STRING | b-expr @b-printable : STRING | b-expr
b-goto : /"goto" b-expr b-goto : /"goto" b-expr
b-let : [/"let"] b-id /"=" (STRING | 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-statement | b-expr) [/"else" (b-statement | 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
@ -22,8 +22,8 @@ b-for : /"for" b-id /"=" b-expr /"to" b-expr [/"step" b-expr]
b-next : /"next" [b-id] b-next : /"next" [b-id]
b-expr : b-logic-expr b-expr : b-logic-expr
b-logic-expr : [b-logic-expr ("and" | "or")] b-comp-expr b-logic-expr : [b-logic-expr ("and"|"or"|"not")] b-comp-expr
b-comp-expr : [b-comp-expr ("=" | "<" | ">")] b-sum b-comp-expr : [b-comp-expr ("="|"<"|">"|"<>")] b-sum
b-sum : [b-sum ("+"|"-")] b-product b-sum : [b-sum ("+"|"-")] b-product
b-product : [b-product ("*"|"/"|"mod")] b-neg b-product : [b-product ("*"|"/"|"mod")] b-neg
b-neg : ["-"] b-expt b-neg : ["-"] b-expt

@ -26,10 +26,22 @@
(require rackunit)) (require rackunit))
(define-macro (syntax-match STX-ARG [(syntax PATTERN) BODY ...] ...) (define-macro-cases case-pattern
#'(syntax-case STX-ARG () [(_ STX-ARG
[PATTERN BODY ...] ...)) [PAT . BODY]
...
[else . ELSEBODY]) (with-syntax ([(LITERAL ...) (generate-literals #'(PAT ...))])
#'(syntax-case STX-ARG (LITERAL ...)
[PAT . BODY]
...
[else . ELSEBODY]))]
[(_ STX-ARG
PAT+BODY
...) #'(case-pattern STX-ARG
PAT+BODY
...
[else (raise-syntax-error 'case-pattern
(format "unable to match pattern for ~v" (syntax->datum STX-ARG)))])])
(define-macro-cases with-pattern (define-macro-cases with-pattern
[(_ () . BODY) #'(begin . BODY)] [(_ () . BODY) #'(begin . BODY)]

Loading…
Cancel
Save