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

28 lines
834 B
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)
;; b-if : /"if" b-expr /"then" b-expr [/"else" b-expr]
(define (b-if cond-expr then-expr [else-expr #f])
(cond
[(not (zero? cond-expr)) (b-goto then-expr)]
[else-expr => b-goto]))
(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)])