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/wires-demo/main.rkt

48 lines
1.3 KiB
Racket

8 years ago
#lang br/quicklang
8 years ago
8 years ago
(module+ reader
(provide read-syntax))
(define (read-syntax path port)
8 years ago
(define wire-datums
(for/list ([wire-str (in-lines port)])
8 years ago
(format-datum '(wire ~a) wire-str)))
(strip-bindings
8 years ago
#`(module wires-mod wires-demo/main
8 years ago
#,@wire-datums)))
8 years ago
(provide #%module-begin)
8 years ago
(define-macro-cases wire
8 years ago
[(wire ARG -> WIRE) #'(define/display (WIRE)
(val ARG))]
[(wire OP ARG -> WIRE) #'(define/display (WIRE)
(OP (val ARG)))]
[(wire ARG1 OP ARG2 -> WIRE) #'(define/display (WIRE)
(OP (val ARG1) (val ARG2)))]
8 years ago
[else #'(void)])
8 years ago
(provide wire)
8 years ago
(define-macro (define/display (ID) BODY)
#'(begin
(define (ID) BODY)
(module+ main
(displayln (format "~a: ~a" 'ID (ID))))))
(define val
8 years ago
(let ([val-cache (make-hash)])
(lambda (num-or-wire)
8 years ago
(if (number? num-or-wire)
num-or-wire
8 years ago
(hash-ref! val-cache num-or-wire num-or-wire)))))
8 years ago
8 years ago
(define (mod-16bit x) (modulo x 65536))
8 years ago
(define-macro (define-16bit ID PROC-ID)
#'(define ID (compose1 mod-16bit PROC-ID)))
8 years ago
8 years ago
(define-16bit AND bitwise-and)
(define-16bit OR bitwise-ior)
(define-16bit NOT bitwise-not)
(define-16bit LSHIFT arithmetic-shift)
(define (RSHIFT x y) (LSHIFT x (- y)))
(provide AND OR NOT LSHIFT RSHIFT)