From 8345b0a0dd089e6b7a5de2e241cd57346c999cb9 Mon Sep 17 00:00:00 2001 From: Matthew Butterick Date: Sun, 24 Apr 2016 13:32:03 -0700 Subject: [PATCH] improve --- beautiful-racket-lib/br/define.rkt | 24 +- beautiful-racket/br/demo/hdl.rkt | 5 + beautiful-racket/br/demo/hdl/#Not2.hdl.rkt#1# | 259 ++++++++++++++++++ beautiful-racket/br/demo/hdl/And.hdl | 21 +- beautiful-racket/br/demo/hdl/Nand.hdl | 13 + beautiful-racket/br/demo/hdl/Not.hdl | 18 +- beautiful-racket/br/demo/hdl/Not2.hdl.rkt | 8 + beautiful-racket/br/demo/hdl/Or.hdl | 23 +- beautiful-racket/br/demo/hdl/Xor1.hdl | 2 +- beautiful-racket/br/demo/hdl/Xor2.hdl | 9 +- .../br/demo/hdl/{hdl.rkt => expander.rkt} | 7 +- beautiful-racket/br/demo/hdl/parser.rkt | 21 ++ beautiful-racket/br/demo/hdl/reader.rkt | 6 + beautiful-racket/br/demo/hdl/tokenizer.rkt | 17 ++ 14 files changed, 392 insertions(+), 41 deletions(-) create mode 100644 beautiful-racket/br/demo/hdl.rkt create mode 100644 beautiful-racket/br/demo/hdl/#Not2.hdl.rkt#1# create mode 100644 beautiful-racket/br/demo/hdl/Nand.hdl create mode 100644 beautiful-racket/br/demo/hdl/Not2.hdl.rkt rename beautiful-racket/br/demo/hdl/{hdl.rkt => expander.rkt} (85%) create mode 100644 beautiful-racket/br/demo/hdl/parser.rkt create mode 100644 beautiful-racket/br/demo/hdl/reader.rkt create mode 100644 beautiful-racket/br/demo/hdl/tokenizer.rkt diff --git a/beautiful-racket-lib/br/define.rkt b/beautiful-racket-lib/br/define.rkt index 09c3de8..d23c40f 100644 --- a/beautiful-racket-lib/br/define.rkt +++ b/beautiful-racket-lib/br/define.rkt @@ -4,9 +4,17 @@ ;; everything is prefixed br: whether it needs it or not so it can be stripped by #lang br +(define-for-syntax (syntax-flatten stx) + (flatten + (let loop ([stx stx]) + (define maybe-list (syntax->list stx)) + (if maybe-list + (map loop maybe-list) + stx)))) + (define-for-syntax (generate-literals pats) ;; generate literals for any symbols that are not ... or _ or _underscore-prefixed - (for*/list ([pat-arg (in-list (flatten (map (λ(stx) (or (syntax->list stx) stx)) (syntax->list pats))))] + (for*/list ([pat-arg (in-list (syntax-flatten pats))] [pat-datum (in-value (syntax->datum pat-arg))] #:when (and (symbol? pat-datum) (not (eq? pat-datum '...)) (not (eq? pat-datum '_)) @@ -51,7 +59,7 @@ (syntax-case stx (LITERAL ...) [pat (syntax-parameterize ([caller-stx (make-rename-transformer #'stx)]) body ...)] ... - [else (raise-syntax-error 'define-cases (format "no matching case for syntax pattern `~a`" (syntax->datum stx)) (syntax->datum #'top-id.name))])) + [else (raise-syntax-error 'define-cases (format "no matching case for syntax pattern ~v" (syntax->datum stx)) (syntax->datum #'top-id.name))])) (if (not (syntax? result)) (datum->syntax stx result) result))))] @@ -136,10 +144,10 @@ (br:define #'plusser #'plus) (check-equal? (plusser 42) +) (check-equal? plusser +) - (br:define #'(times _ARG) #'(* _ARG _ARG)) - (check-equal? (times 10) 100) + (br:define #'(times [nested _ARG]) #'(* _ARG _ARG)) + (check-equal? (times [nested 10]) 100) (br:define #'timeser #'times) - (check-equal? (timeser 12) 144) + (check-equal? (timeser [nested 12]) 144) (br:define #'fortytwo #'42) (check-equal? fortytwo 42) (check-equal? (let () @@ -152,11 +160,11 @@ (br:define #'(redefine _id) #'(define _id 42)) (redefine zoombar) (check-equal? zoombar 42)) - + ;; use caller-stx parameter to introduce identifier unhygienically (br:define #'(zam _arg1 _arg2 _arg3) - (with-syntax ([dz (datum->syntax caller-stx 'dirty-zam)]) - #`(define dz 'got-dirty-zam))) + (with-syntax ([dz (datum->syntax caller-stx 'dirty-zam)]) + #`(define dz 'got-dirty-zam))) (zam 'this 'that 42) (check-equal? dirty-zam 'got-dirty-zam)) diff --git a/beautiful-racket/br/demo/hdl.rkt b/beautiful-racket/br/demo/hdl.rkt new file mode 100644 index 0000000..358fcc7 --- /dev/null +++ b/beautiful-racket/br/demo/hdl.rkt @@ -0,0 +1,5 @@ +#lang br + +(module+ reader + (require "hdl/reader.rkt") + (provide (all-from-out "hdl/reader.rkt"))) diff --git a/beautiful-racket/br/demo/hdl/#Not2.hdl.rkt#1# b/beautiful-racket/br/demo/hdl/#Not2.hdl.rkt#1# new file mode 100644 index 0000000..b0cd3c1 --- /dev/null +++ b/beautiful-racket/br/demo/hdl/#Not2.hdl.rkt#1# @@ -0,0 +1,259 @@ +#reader(lib"read.ss""wxme")WXME0108 ## +#| + This file uses the GRacket editor format. + Open this file in DrRacket version 6.4.0.15 or later to read it. + + Most likely, it was created by saving a program in DrRacket, + and it probably contains a program with non-text elements + (such as images or comment boxes). + + http://racket-lang.org/ +|# + 22 7 #"wxtext\0" +3 1 6 #"wxtab\0" +1 1 8 #"wximage\0" +2 0 8 #"wxmedia\0" +4 1 34 #"(lib \"syntax-browser.ss\" \"mrlib\")\0" +1 0 16 #"drscheme:number\0" +3 0 44 #"(lib \"number-snip.ss\" \"drscheme\" \"private\")\0" +1 0 36 #"(lib \"comment-snip.ss\" \"framework\")\0" +1 0 93 +( + #"((lib \"collapsed-snipclass.ss\" \"framework\") (lib \"collapsed-sni" + #"pclass-wxme.ss\" \"framework\"))\0" +) 0 0 43 #"(lib \"collapsed-snipclass.ss\" \"framework\")\0" +0 0 19 #"drscheme:sexp-snip\0" +0 0 36 #"(lib \"cache-image-snip.ss\" \"mrlib\")\0" +1 0 68 +( + #"((lib \"image-core.ss\" \"mrlib\") (lib \"image-core-wxme.rkt\" \"mr" + #"lib\"))\0" +) 1 0 29 #"drscheme:bindings-snipclass%\0" +1 0 101 +( + #"((lib \"ellipsis-snip.rkt\" \"drracket\" \"private\") (lib \"ellipsi" + #"s-snip-wxme.rkt\" \"drracket\" \"private\"))\0" +) 2 0 88 +( + #"((lib \"pict-snip.rkt\" \"drracket\" \"private\") (lib \"pict-snip.r" + #"kt\" \"drracket\" \"private\"))\0" +) 0 0 34 #"(lib \"bullet-snip.rkt\" \"browser\")\0" +0 0 25 #"(lib \"matrix.ss\" \"htdp\")\0" +1 0 22 #"drscheme:lambda-snip%\0" +1 0 29 #"drclickable-string-snipclass\0" +0 0 26 #"drracket:spacer-snipclass\0" +0 0 57 +#"(lib \"hrule-snip.rkt\" \"macro-debugger\" \"syntax-browser\")\0" +1 0 1 6 #"wxloc\0" + 0 0 63 0 1 #"\0" +0 75 1 #"\0" +0 12 90 -1 90 -1 3 -1 0 1 0 1 0 0 0 0 0 0 0 0 0 0 0 255 255 255 1 -1 0 9 +#"Standard\0" +0 75 15 #"Triplicate T3c\0" +0 16 90 -1 90 -1 3 -1 0 1 0 1 0 0 0 0 0 0 0 0 0 0 0 255 255 255 1 -1 2 1 +#"\0" +0 -1 1 #"\0" +1 0 -1 -1 -1 -1 -1 -1 0 0 0 0 0 0 1 1 1 1 1 1 0 0 0 0 0 0 -1 -1 2 24 +#"framework:default-color\0" +0 -1 1 #"\0" +1 0 -1 -1 -1 -1 -1 -1 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 255 255 255 -1 -1 2 +1 #"\0" +0 -1 1 #"\0" +1 0 -1 -1 -1 -1 -1 -1 0 0 0 0 0 0 0 0 0 1 1 1 150 0 150 0 0 0 -1 -1 2 15 +#"text:ports out\0" +0 -1 1 #"\0" +1 0 -1 -1 -1 -1 -1 -1 0 0 0 0 0 0 0 0 0 1 1 1 34 119 34 0 0 0 -1 -1 2 1 +#"\0" +0 -1 1 #"\0" +1.0 0 -1 -1 93 -1 -1 -1 0 0 0 0 0 0 0 0 0 1.0 1.0 1.0 255 0 0 0 0 0 -1 +-1 2 15 #"text:ports err\0" +0 -1 1 #"\0" +1 0 -1 -1 -1 -1 -1 -1 0 0 0 0 0 0 0 0 0 1 1 1 136 17 17 0 0 0 -1 -1 2 1 +#"\0" +0 -1 1 #"\0" +1 0 -1 -1 -1 -1 -1 -1 0 0 0 0 0 0 0 0 0 1 1 1 0 0 175 0 0 0 -1 -1 2 17 +#"text:ports value\0" +0 -1 1 #"\0" +1 0 -1 -1 -1 -1 -1 -1 0 0 0 0 0 0 0 0 0 1 1 1 34 34 119 0 0 0 -1 -1 2 1 +#"\0" +0 -1 1 #"\0" +1.0 0 92 -1 -1 -1 -1 -1 0 0 0 0 0 0 0 0 0 1.0 1.0 1.0 34 139 34 0 0 0 -1 +-1 2 27 #"Matching Parenthesis Style\0" +0 -1 1 #"\0" +1.0 0 92 -1 -1 -1 -1 -1 0 0 0 0 0 0 0 0 0 1.0 1.0 1.0 34 139 34 0 0 0 -1 +-1 2 1 #"\0" +0 -1 1 #"\0" +1 0 -1 -1 -1 93 -1 -1 0 1 0 0 0 0 0 0 0 1 1 1 38 38 128 0 0 0 -1 -1 2 37 +#"framework:syntax-color:scheme:symbol\0" +0 -1 1 #"\0" +1 0 -1 -1 -1 -1 -1 -1 0 0 0 0 0 0 0 0 0 1 1 1 34 34 119 0 0 0 -1 -1 2 38 +#"framework:syntax-color:scheme:keyword\0" +0 -1 1 #"\0" +1 0 -1 -1 -1 -1 -1 -1 0 0 0 0 0 0 0 0 0 1 1 1 34 34 119 0 0 0 -1 -1 2 1 +#"\0" +0 -1 1 #"\0" +1 0 -1 -1 -1 93 -1 -1 0 1 0 0 0 0 0 0 0 1 1 1 194 116 31 0 0 0 -1 -1 2 +38 #"framework:syntax-color:scheme:comment\0" +0 -1 1 #"\0" +1 0 -1 -1 -1 -1 -1 -1 0 0 0 0 0 0 0 0 0 1 1 1 119 34 119 0 0 0 -1 -1 2 1 +#"\0" +0 -1 1 #"\0" +1 0 -1 -1 -1 93 -1 -1 0 1 0 0 0 0 0 0 0 1 1 1 41 128 38 0 0 0 -1 -1 2 37 +#"framework:syntax-color:scheme:string\0" +0 -1 1 #"\0" +1 0 -1 -1 -1 -1 -1 -1 0 0 0 0 0 0 0 0 0 1 1 1 34 119 34 0 0 0 -1 -1 2 35 +#"framework:syntax-color:scheme:text\0" +0 -1 1 #"\0" +1 0 -1 -1 -1 -1 -1 -1 0 0 0 0 0 0 0 0 0 1 1 1 34 119 34 0 0 0 -1 -1 2 39 +#"framework:syntax-color:scheme:constant\0" +0 -1 1 #"\0" +1 0 -1 -1 -1 -1 -1 -1 0 0 0 0 0 0 0 0 0 1 1 1 34 119 34 0 0 0 -1 -1 2 1 +#"\0" +0 -1 1 #"\0" +1 0 -1 -1 -1 93 -1 -1 0 1 0 0 0 0 0 0 0 1 1 1 132 60 36 0 0 0 -1 -1 2 49 +#"framework:syntax-color:scheme:hash-colon-keyword\0" +0 -1 1 #"\0" +1 0 -1 -1 -1 -1 -1 -1 0 0 0 0 0 0 0 0 0 1 1 1 34 119 34 0 0 0 -1 -1 2 42 +#"framework:syntax-color:scheme:parenthesis\0" +0 -1 1 #"\0" +1 0 -1 -1 -1 -1 -1 -1 0 0 0 0 0 0 0 0 0 1 1 1 0 178 178 0 0 0 -1 -1 2 1 +#"\0" +0 -1 1 #"\0" +1 0 -1 -1 -1 93 -1 -1 0 1 0 0 0 0 0 0 0 1 1 1 255 0 0 0 0 0 -1 -1 2 36 +#"framework:syntax-color:scheme:error\0" +0 -1 1 #"\0" +1 0 -1 -1 -1 -1 -1 -1 0 0 0 0 0 0 0 0 0 1 1 1 136 17 17 0 0 0 -1 -1 2 1 +#"\0" +0 -1 1 #"\0" +1 0 -1 -1 -1 93 -1 -1 0 1 0 0 0 0 0 0 0 1 1 1 0 0 0 0 0 0 -1 -1 2 36 +#"framework:syntax-color:scheme:other\0" +0 -1 1 #"\0" +1 0 -1 -1 -1 -1 -1 -1 0 0 0 0 0 0 0 0 0 1 1 1 34 34 119 0 0 0 -1 -1 2 16 +#"Misspelled Text\0" +0 -1 1 #"\0" +1 0 -1 -1 -1 -1 -1 -1 0 0 0 0 0 0 0 0 0 1 1 1 0 0 0 0 0 0 -1 -1 2 1 +#"\0" +0 -1 1 #"\0" +1 0 -1 -1 -1 93 -1 -1 0 1 0 0 0 0 0 0 0 1 1 1 81 112 203 0 0 0 -1 -1 2 +38 #"drracket:check-syntax:lexically-bound\0" +0 -1 1 #"\0" +1 0 -1 -1 -1 93 -1 -1 0 1 0 0 0 0 0 0 0 1 1 1 81 112 203 0 0 0 -1 -1 2 1 +#"\0" +0 -1 1 #"\0" +1 0 -1 -1 -1 93 -1 -1 0 1 0 0 0 0 0 0 0 1 1 1 178 34 34 0 0 0 -1 -1 2 28 +#"drracket:check-syntax:set!d\0" +0 -1 1 #"\0" +1 0 -1 -1 -1 -1 -1 -1 0 0 0 0 0 0 0 0 0 1 1 1 136 17 17 0 0 0 -1 -1 2 37 +#"drracket:check-syntax:unused-require\0" +0 -1 1 #"\0" +1 0 -1 -1 -1 -1 -1 -1 0 0 0 0 0 0 0 0 0 1 1 1 136 17 17 0 0 0 -1 -1 2 36 +#"drracket:check-syntax:free-variable\0" +0 -1 1 #"\0" +1 0 -1 -1 -1 -1 -1 -1 0 0 0 0 0 0 0 0 0 1 1 1 136 17 17 0 0 0 -1 -1 2 1 +#"\0" +0 -1 1 #"\0" +1 0 -1 -1 -1 93 -1 -1 0 1 0 0 0 0 0 0 0 1 1 1 68 0 203 0 0 0 -1 -1 2 31 +#"drracket:check-syntax:imported\0" +0 -1 1 #"\0" +1 0 -1 -1 -1 93 -1 -1 0 1 0 0 0 0 0 0 0 1 1 1 68 0 203 0 0 0 -1 -1 2 47 +#"drracket:check-syntax:my-obligation-style-pref\0" +0 -1 1 #"\0" +1 0 -1 -1 -1 93 -1 -1 0 1 0 0 0 0 0 0 0 1 1 1 178 34 34 0 0 0 -1 -1 2 1 +#"\0" +0 -1 1 #"\0" +1 0 -1 -1 -1 93 -1 -1 0 1 0 0 0 0 0 0 0 1 1 1 0 116 0 0 0 0 -1 -1 2 50 +#"drracket:check-syntax:their-obligation-style-pref\0" +0 -1 1 #"\0" +1 0 -1 -1 -1 93 -1 -1 0 1 0 0 0 0 0 0 0 1 1 1 0 116 0 0 0 0 -1 -1 2 48 +#"drracket:check-syntax:unk-obligation-style-pref\0" +0 -1 1 #"\0" +1 0 -1 -1 -1 93 -1 -1 0 1 0 0 0 0 0 0 0 1 1 1 0 0 0 0 0 0 -1 -1 2 1 +#"\0" +0 -1 1 #"\0" +1 0 -1 -1 -1 93 -1 -1 0 1 0 0 0 0 0 0 0 1 1 1 139 142 28 0 0 0 -1 -1 2 +49 #"drracket:check-syntax:both-obligation-style-pref\0" +0 -1 1 #"\0" +1 0 -1 -1 -1 93 -1 -1 0 1 0 0 0 0 0 0 0 1 1 1 139 142 28 0 0 0 -1 -1 2 +37 #"plt:module-language:test-coverage-on\0" +0 -1 1 #"\0" +1 0 -1 -1 -1 93 -1 -1 0 1 0 0 0 0 0 0 0 1 1 1 0 0 0 0 0 0 -1 -1 2 1 +#"\0" +0 -1 1 #"\0" +1 0 -1 -1 -1 93 -1 -1 0 1 0 0 0 1 0 0 0 0 0 0 255 165 0 0 0 0 -1 -1 2 38 +#"plt:module-language:test-coverage-off\0" +0 -1 1 #"\0" +1 0 -1 -1 -1 93 -1 -1 0 1 0 0 0 1 0 0 0 0 0 0 255 165 0 0 0 0 -1 -1 4 1 +#"\0" +0 71 1 #"\0" +1.0 0 -1 -1 -1 -1 -1 -1 0 0 0 0 0 0 1.0 1.0 1.0 1.0 1.0 1.0 0 0 0 0 0 0 +-1 -1 4 1 #"\0" +0 -1 1 #"\0" +1.0 0 -1 -1 -1 -1 -1 -1 1 0 0 0 0 0 0 0 0 1.0 1.0 1.0 0 0 255 0 0 0 -1 +-1 4 1 #"\0" +0 71 1 #"\0" +1.0 0 -1 -1 -1 -1 -1 -1 1 0 0 0 0 0 0 0 0 1.0 1.0 1.0 0 0 255 0 0 0 -1 +-1 4 1 #"\0" +0 71 1 #"\0" +1.0 0 -1 -1 -1 -1 -1 -1 0 0 0 0 0 0 0 0 0 1.0 1.0 1.0 0 100 0 0 0 0 -1 +-1 2 1 #"\0" +0 -1 1 #"\0" +1 0 -1 -1 -1 -1 -1 -1 0 0 0 0 0 0 0 0 0 1 1 1 200 0 0 0 0 0 -1 -1 4 1 +#"\0" +0 -1 1 #"\0" +1.0 0 92 -1 -1 -1 -1 -1 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 255 255 0 -1 -1 0 +1 #"\0" +0 75 1 #"\0" +1.0 0 -1 -1 -1 -1 -1 -1 0 0 0 0 0 0 1.0 1.0 1.0 1.0 1.0 1.0 0 0 0 0 0 0 +-1 -1 2 1 #"\0" +0 75 1 #"\0" +1.0 0 -1 -1 -1 -1 -1 -1 0 0 0 0 0 0 1.0 1.0 1.0 1.0 1.0 1.0 0 0 0 0 0 0 +-1 -1 2 1 #"\0" +0 75 1 #"\0" +1.0 0 -1 -1 -1 -1 -1 -1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 255 255 255 -1 -1 +2 1 #"\0" +0 75 1 #"\0" +1.0 0 -1 -1 -1 -1 -1 -1 0 0 0 0 0 0 0 0 0 0 0 0 34 139 34 255 255 255 -1 +-1 2 1 #"\0" +0 -1 1 #"\0" +1.0 0 92 -1 -1 -1 -1 -1 0 0 0 0 0 0 0 0 0 1.0 1.0 1.0 36 36 140 0 0 0 -1 +-1 2 1 #"\0" +0 -1 1 #"\0" +1.0 0 92 -1 -1 -1 -1 -1 0 0 0 0 0 0 1.0 1.0 1.0 1.0 1.0 1.0 0 0 0 0 0 0 +-1 -1 2 1 #"\0" +0 -1 1 #"\0" +0 4 -1 -1 -1 -1 -1 -1 0 0 0 0 0 0 1.0 1.0 1.0 1.0 1.0 1.0 0 0 0 0 0 0 -1 +-1 2 1 #"\0" +0 75 1 #"\0" +1.0 0 92 -1 -1 -1 -1 -1 0 0 0 0 0 0 0 0 0 1.0 1.0 1.0 36 36 140 0 0 0 -1 +-1 2 1 #"\0" +0 75 1 #"\0" +1.0 0 92 -1 -1 -1 -1 -1 0 0 0 0 0 0 1.0 1.0 1.0 1.0 1.0 1.0 0 0 0 0 0 0 +-1 -1 2 1 #"\0" +0 75 1 #"\0" +0 4 -1 -1 -1 -1 -1 -1 0 0 0 0 0 0 1.0 1.0 1.0 1.0 1.0 1.0 0 0 0 0 0 0 -1 +-1 0 25 0 28 3 17 #"#lang br/demo/hdl" +0 0 24 29 1 #"\n" +0 0 24 29 1 #"\n" +0 0 14 3 4 #"CHIP" +0 0 24 3 1 #" " +0 0 14 3 3 #"Not" +0 0 24 3 2 #" {" +0 0 24 29 1 #"\n" +0 0 24 3 10 #" " +0 0 14 3 2 #"IN" +0 0 24 3 1 #" " +0 0 14 3 1 #"x" +0 0 28 3 1 #"," +0 0 24 3 1 #" " +0 0 14 3 1 #"y" +0 0 28 3 1 #"," +0 0 24 3 1 #" " +0 0 14 3 1 #"z" +0 0 17 3 1 #";" +0 0 24 29 1 #"\n" +0 0 24 3 1 #"}" +0 0 24 29 1 #"\n" +0 0 24 29 1 #"\n" +0 0 24 29 1 #"\n" +0 0 24 29 1 #"\n" +0 0 diff --git a/beautiful-racket/br/demo/hdl/And.hdl b/beautiful-racket/br/demo/hdl/And.hdl index 2d3ecf9..bbf7067 100644 --- a/beautiful-racket/br/demo/hdl/And.hdl +++ b/beautiful-racket/br/demo/hdl/And.hdl @@ -1,7 +1,14 @@ -#lang racket/base -(provide And) -(define (And #:a a #:b b) - (define out (if (and (not (zero? a)) (not (zero? b))) - 1 - 0)) - out) +#lang s-exp br/demo/hdl/expander + +(chip And + (a b) + (out) + ((Nand [a a] [b b] [out nand-out]) + (Not [in nand-out] [out out]))) + +(module+ test + (require rackunit) + (check-equal? (And #:a 0 #:b 0) 0) + (check-equal? (And #:a 0 #:b 1) 0) + (check-equal? (And #:a 1 #:b 0) 0) + (check-equal? (And #:a 1 #:b 1) 1)) \ No newline at end of file diff --git a/beautiful-racket/br/demo/hdl/Nand.hdl b/beautiful-racket/br/demo/hdl/Nand.hdl new file mode 100644 index 0000000..8655fa2 --- /dev/null +++ b/beautiful-racket/br/demo/hdl/Nand.hdl @@ -0,0 +1,13 @@ +#lang br + +(define+provide (Nand #:a a #:b b) + (if (< (+ a b) 2) + 1 + 0)) + +(module+ test + (require rackunit) + (check-equal? (Nand #:a 0 #:b 0) 1) + (check-equal? (Nand #:a 0 #:b 1) 1) + (check-equal? (Nand #:a 1 #:b 0) 1) + (check-equal? (Nand #:a 1 #:b 1) 0)) diff --git a/beautiful-racket/br/demo/hdl/Not.hdl b/beautiful-racket/br/demo/hdl/Not.hdl index e4d38fd..5b9838f 100644 --- a/beautiful-racket/br/demo/hdl/Not.hdl +++ b/beautiful-racket/br/demo/hdl/Not.hdl @@ -1,7 +1,11 @@ -#lang racket/base -(provide Not) -(define (Not #:in in) - (define out (if (zero? in) - 1 - 0)) - out) \ No newline at end of file +#lang s-exp br/demo/hdl/expander + +(chip Not + (in) + (out) + ((Nand [a in] [b in] [out out]))) + +(module+ test + (require rackunit) + (check-equal? (Not #:in 0) 1) + (check-equal? (Not #:in 1) 0)) \ No newline at end of file diff --git a/beautiful-racket/br/demo/hdl/Not2.hdl.rkt b/beautiful-racket/br/demo/hdl/Not2.hdl.rkt new file mode 100644 index 0000000..54080a6 --- /dev/null +++ b/beautiful-racket/br/demo/hdl/Not2.hdl.rkt @@ -0,0 +1,8 @@ +#lang br/demo/hdl + +CHIP Not { + IN x; +} + + + diff --git a/beautiful-racket/br/demo/hdl/Or.hdl b/beautiful-racket/br/demo/hdl/Or.hdl index 0a2526b..a20a449 100644 --- a/beautiful-racket/br/demo/hdl/Or.hdl +++ b/beautiful-racket/br/demo/hdl/Or.hdl @@ -1,7 +1,16 @@ -#lang racket/base -(provide Or) -(define (Or #:a a #:b b) - (define out (if (or (not (zero? a)) (not (zero? b))) - 1 - 0)) - out) \ No newline at end of file +#lang s-exp br/demo/hdl/expander + +(chip Or + (a b) + (out) + ((Not [in a] [out nota]) + (Not [in b] [out notb]) + (And [a nota] [b notb] [out and-out]) + (Not [in and-out] [out out]))) + +(module+ test + (require rackunit) + (check-equal? (Or #:a 0 #:b 0) 0) + (check-equal? (Or #:a 0 #:b 1) 1) + (check-equal? (Or #:a 1 #:b 0) 1) + (check-equal? (Or #:a 1 #:b 1) 1)) \ No newline at end of file diff --git a/beautiful-racket/br/demo/hdl/Xor1.hdl b/beautiful-racket/br/demo/hdl/Xor1.hdl index 669d931..3a08564 100644 --- a/beautiful-racket/br/demo/hdl/Xor1.hdl +++ b/beautiful-racket/br/demo/hdl/Xor1.hdl @@ -1,4 +1,4 @@ -#lang s-exp "hdl.rkt" +#lang s-exp br/demo/hdl/expander (chip Xor (IN a b) (OUT out) diff --git a/beautiful-racket/br/demo/hdl/Xor2.hdl b/beautiful-racket/br/demo/hdl/Xor2.hdl index 50e9024..1d3d615 100644 --- a/beautiful-racket/br/demo/hdl/Xor2.hdl +++ b/beautiful-racket/br/demo/hdl/Xor2.hdl @@ -1,4 +1,4 @@ -#lang reader "hdl-reader.rkt" +#lang br/demo/hdl CHIP Xor { IN a, b; @@ -12,9 +12,4 @@ CHIP Xor { } -(module+ test - (require rackunit) - (check-equal? (Xor #:a 0 #:b 0) 0) - (check-equal? (Xor #:a 0 #:b 1) 1) - (check-equal? (Xor #:a 1 #:b 0) 1) - (check-equal? (Xor #:a 1 #:b 1) 0)) + diff --git a/beautiful-racket/br/demo/hdl/hdl.rkt b/beautiful-racket/br/demo/hdl/expander.rkt similarity index 85% rename from beautiful-racket/br/demo/hdl/hdl.rkt rename to beautiful-racket/br/demo/hdl/expander.rkt index 3db85b2..4e4aa4c 100644 --- a/beautiful-racket/br/demo/hdl/hdl.rkt +++ b/beautiful-racket/br/demo/hdl/expander.rkt @@ -3,10 +3,9 @@ (define #'(chip _Chip - (IN _input-pin ...) - (OUT _output-pin) - (PARTS - (_Part [_pin-in _val-id] ... [out _pin-out]) ...)) + (_input-pin ...) + (_output-pin) + ((_Part [_pin-in _val-id] ... [out _pin-out]) ...)) #'(begin (provide _Chip) (define _Chip diff --git a/beautiful-racket/br/demo/hdl/parser.rkt b/beautiful-racket/br/demo/hdl/parser.rkt new file mode 100644 index 0000000..1955cee --- /dev/null +++ b/beautiful-racket/br/demo/hdl/parser.rkt @@ -0,0 +1,21 @@ +#lang ragg + +chip-program : "CHIP" chip-name "{" in-pin-spec "}" + +chip-name : ID + +in-pin-spec : "IN" pin-list ";" + +out-pin-spec : "OUT" pin-list ";" + +pin-list : pin ["," pin-list] + +pin : ID + +part-spec : "PARTS:" part+ + +part : ID "(" part-arg-list ")" ";" + +part-arg-list : part-arg ["," part-arg-list] + +part-arg : ID "=" ID \ No newline at end of file diff --git a/beautiful-racket/br/demo/hdl/reader.rkt b/beautiful-racket/br/demo/hdl/reader.rkt new file mode 100644 index 0000000..a6bf51f --- /dev/null +++ b/beautiful-racket/br/demo/hdl/reader.rkt @@ -0,0 +1,6 @@ +#lang br +(require br/reader-utils "parser.rkt" "tokenizer.rkt") + +(define-read-and-read-syntax (source-path input-port) + #`(module hdl-mod br/demo/hdl/expander + #,(parse source-path (tokenize input-port)))) diff --git a/beautiful-racket/br/demo/hdl/tokenizer.rkt b/beautiful-racket/br/demo/hdl/tokenizer.rkt new file mode 100644 index 0000000..37b5986 --- /dev/null +++ b/beautiful-racket/br/demo/hdl/tokenizer.rkt @@ -0,0 +1,17 @@ +#lang br +(require parser-tools/lex parser-tools/lex-sre + ragg/support + racket/string) + +(provide tokenize) +(define (tokenize input-port) + (define (next-token) + (define get-token + (lexer + [(eof) eof] + [(union #\tab #\space #\newline) (get-token input-port)] + [(union "CHIP" "IN" "OUT" "PARTS:") lexeme] + [(char-set "{}(),;=") lexeme] + [(repetition 1 +inf.0 (union alphabetic numeric)) (token 'ID (string->symbol lexeme))])) + (get-token input-port)) + next-token)