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

49 lines
1.2 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
7 years ago
[(wire ARG -> ID)
#'(define/display (ID) (val ARG))]
[(wire OP ARG -> ID)
#'(wire (OP (val ARG)) -> ID)]
[(wire ARG1 OP ARG2 -> ID)
#'(wire (OP (val ARG1) (val ARG2)) -> ID)]
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
7 years ago
(displayln (format "~a: ~a" 'ID (ID))))))
8 years ago
(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)))
7 years ago
(provide AND OR NOT LSHIFT RSHIFT)