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/if.rkt

41 lines
1.2 KiB
Racket

This file contains invisible Unicode characters!

This file contains invisible Unicode characters that may be processed differently from what appears below. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to reveal hidden characters.

#lang br
(require "go.rkt")
(provide b-if b-comp-expr b-logic-expr)
#|
explain why this won't work due to premature eval of THEN & ELSE
(define (b-if COND THEN ELSE)
(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 bi= (compose1 bool-int =))
(define bi< (compose1 bool-int <))
(define bi> (compose1 bool-int >))
;; b-comp-expr : b-cond-expr [("and" | "or") b-cond-expr]
(define-macro-cases b-logic-expr
[(_ ARG) #'ARG]
[(_ LEFT "and" RIGHT) #'(and LEFT RIGHT)]
[(_ LEFT "or" RIGHT) #'(or LEFT RIGHT)])
;; b-cond-expr : b-expr [("=" | "<" | ">") b-expr]
(define-macro-cases b-comp-expr
[(_ ARG) #'ARG]
[(_ LEFT "=" RIGHT) #'(bi= LEFT RIGHT)]
[(_ LEFT "<" RIGHT) #'(bi< LEFT RIGHT)]
[(_ LEFT ">" RIGHT) #'(bi> LEFT RIGHT)])