You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
beautiful-racket/beautiful-racket-demo/basic-demo-2/expr.rkt

31 lines
829 B
Racket

#lang br
(provide (all-defined-out))
;; 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)])
;; 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-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)
#'(set! ID (λ (VAR) EXPR)))