pull/10/head
Matthew Butterick 7 years ago
parent 86c57d32dd
commit 92c5eb8a4d

@ -1,29 +1,28 @@
#lang br
(provide (all-defined-out))
(provide b-expr b-sum b-product b-neg b-expt)
(define (b-expr expr)
(if (integer? expr) (inexact->exact expr) expr))
;; b-sum : b-product (("+" | "-") b-product)*
(define-macro-cases b-sum
[(_ PROD) #'PROD]
[(_ LEFT-PROD "+" RIGHT-PROD) #'(+ LEFT-PROD RIGHT-PROD)]
[(_ LEFT-PROD "-" RIGHT-PROD) #'(- LEFT-PROD RIGHT-PROD)])
[(_ VAL) #'VAL]
[(_ LEFT "+" RIGHT) #'(+ LEFT RIGHT)]
[(_ LEFT "-" RIGHT) #'(- LEFT RIGHT)])
;; b-product : [b-product ("*"|"/"|"%"|"^")] b-value
(define-macro-cases b-product
[(_ VAL) #'VAL]
[(_ LEFT "*" RIGHT) #'(* LEFT RIGHT)]
[(_ LEFT "/" RIGHT) #'(/ LEFT RIGHT 1.0)]
[(_ LEFT "mod" RIGHT) #'(modulo LEFT RIGHT)])
;; b-expt : [b-expt "^"] b-value
(define-macro-cases b-neg
[(_ VAL) #'VAL]
[(_ "-" VAL) #'(- VAL)])
(define-macro-cases b-expt
[(_ VAL) #'VAL]
[(_ LEFT "^" RIGHT) #'(expt LEFT RIGHT)])
(define (b-expr expr)
(if (integer? expr) (inexact->exact expr) expr))
(define (b-negative num) (- num))
(define (b-not expr) (if (zero? expr) 1 0))
(define-macro (b-def ID VAR EXPR)

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

@ -16,23 +16,20 @@ b-if : /"if" b-expr /"then" b-expr [/"else" b-expr]
b-gosub : /"gosub" b-expr
b-return : /"return"
b-input : /"input" b-id
@b-id : ID
b-def : /"def" b-id /"(" b-id /")" /"=" b-expr
b-for : /"for" b-id /"=" b-expr /"to" b-expr [/"step" b-expr]
b-next : /"next" [b-id]
;; expressions with precedence & order
b-expr : b-logic-expr
b-logic-expr : [b-logic-expr ("and" | "or")] b-comp-expr
b-comp-expr : [b-comp-expr ("=" | "<" | ">")] b-sum
b-sum : [b-sum ("+"|"-")] b-product
b-product : [b-product ("*"|"/"|"mod")] b-expt
b-expt : [b-expt "^"] b-value
b-product : [b-product ("*"|"/"|"mod")] b-neg
b-neg : ["-"] b-expt
b-expt : [b-expt ("^")] b-value
;; values
@b-value : b-id | b-number | /"(" b-expr /")" | b-not | b-func
@b-value : b-number | b-id | /"(" b-expr /")" | b-not | b-func
/b-func : b-id /"(" b-expr /")"
b-not : /"!" b-value
@b-id : ID
@b-number : b-positive | b-negative
@b-positive : INTEGER | DECIMAL
b-negative : /"-" b-positive
@b-number : INTEGER | DECIMAL
Loading…
Cancel
Save