resume in hdl/tst

dev-elider-3
Matthew Butterick 9 years ago
parent 5303f4ced1
commit 30d4dec8c3

@ -0,0 +1,56 @@
#lang br
#|
load Xor.hdl,
output-list a, b, out;
set a 0, set b 0,
eval, output;
set a 0, set b 1,
eval, output;
set a 1, set b 0,
eval, output;
set a 1, set b 1,
eval, output;
|#
(define (vals->text vals)
(string-join (map ~a vals) " | "))
(define (display-values . vals)
(displayln (vals->text vals)))
(define (display-dashes . vals)
(displayln (make-string (string-length (vals->text vals)) #\-)))
(define #'(display-header _val ...)
#'(begin
(apply display-values (list '_val ...))
(apply display-dashes (list '_val ...))))
(define (display-status)
(display-values a b (out)))
(define proc (dynamic-require "Xor.hdl" 'Xor))
(display-header a b out)
(define a #f)
(define b #f)
(define (out)
(keyword-apply proc '(#:a #:b) (list a b) null))
(set! a 0)
(set! b 0)
(display-status)
(set! a 0)
(set! b 1)
(display-status)
(set! a 1)
(set! b 0)
(display-status)
(set! a 1)
(set! b 1)
(display-status)

@ -0,0 +1,18 @@
#lang br/demo/hdl
/* Xor (exclusive or) gate:
If a<>b out=1 else out=0. */
CHIP Xor {
IN a, b;
OUT out;
PARTS:
Not(in=a, out=nota);
Not(in=b, out=notb);
And(a=a, b=notb, out=w1);
And(a=nota, b=b, out=w2);
Or(a=w1, b=w2, out=out);
}

@ -0,0 +1,14 @@
#lang br/demo/hdl/tst
load Xor.hdl,
output-list a, b, out;
/*
set a 0, set b 0,
eval, output;
set a 0, set b 1,
eval, output;
set a 1, set b 0,
eval, output;
set a 1, set b 1,
eval, output;
*/

@ -9,6 +9,8 @@
(define get-token
(lexer
[(eof) eof]
[(seq "/*" (complement (seq any-string "*/" any-string)) "*/")
(token 'COMMENT lexeme #:skip? #t)]
[(union #\tab #\space #\newline) (get-token input-port)]
[(union "CHIP" "IN" "OUT" "PARTS:") lexeme]
[(char-set "{}(),;=") lexeme]

@ -0,0 +1,7 @@
#lang br
(module reader br
(require br/reader-utils "tst/parser.rkt" "tst/tokenizer.rkt")
(define-read-and-read-syntax (source-path input-port)
#`(module hdl-mod br/demo/hdl/tst/expander
#,(parse source-path (tokenize input-port)))))

@ -0,0 +1,82 @@
#lang br
(provide #%top-interaction #%module-begin #%datum #%top #%app)
(provide tst-program)
(define #'(tst-program _arg ...)
#'(begin _arg ...))
(define-for-syntax private-proc-name (generate-temporary))
(provide load-expr)
;; parse shape: (load-expr "load" Xor.hdl ",")
(define #'(load-expr "load" _filename ",")
(inject-syntax ([#'filename-string (symbol->string (syntax->datum #'_filename))]
[#'proc-name (string->symbol (cadr (regexp-match #rx"^(.*)\\.hdl$"(symbol->string (syntax->datum #'_filename)))))])
#'(begin
(define _filename (dynamic-require filename-string 'proc-name)))))
(begin-for-syntax
(define (expand-macro mac)
(syntax-disarm (local-expand mac 'expression #f) #f)))
;; parse shape:
;; (header-expr "output-list" a (comma-id "," b) "," "out" ";")
(provide header-expr)
(define #'(header-expr "output-list" _first-id _comma-id ... "," "out" ";")
(inject-syntax ([#'(_other-id ...) (map expand-macro (syntax->list #'(_comma-id ...)))])
#'(begin
(display-header _first-id _other-id ... out)
(define _first-id #f)
(define _other-id #f) ...
(define (out)
(keyword-apply proc '(#:a #:b) (list a b) null))
)))
(provide comma-id)
(define #'(comma-id "," _id)
#'_id)
(define #'(display-header _val ...)
#'(begin
(apply display-values (list '_val ...))
(apply display-dashes (list '_val ...))))
(define (vals->text vals)
(string-join (map ~a vals) " | "))
(define (display-values . vals)
(displayln (vals->text vals)))
(define (display-dashes . vals)
(displayln (make-string (string-length (vals->text vals)) #\-)))
(provide test-expr)
(define #'(test-expr _first-step _comma-step ... ";")
(inject-syntax ([#'(_other-step ...) (expand-macro #'(_comma-step ...))])
#'(let ()
_first-step
_other-step ...)))
(provide step-expr)
(define #'(step-expr _step)
#'_step)
(provide set-expr)
(define #'(set-expr "set" _id _val)
#'(set! _id _val))
(provide comma-step)
(define #'(comma-step "," _step)
#'_step)
(provide eval-expr)
(define #'(eval-expr "eval")
#'(set! result (param-proc)))
#|
(tst-program (load-expr "load" Xor.hdl ",") (header-expr "output-list" a "," b "," out ";") (test-expr (step-expr (set-expr "set" a 0)) "," (step-expr (set-expr "set" b 0)) "," (step-expr (eval-expr "eval")) "," (step-expr (output-expr "output")) ";") (test-expr (step-expr (set-expr "set" a 0)) "," (step-expr (set-expr "set" b 1)) "," (step-expr (eval-expr "eval")) "," (step-expr (output-expr "output")) ";") (test-expr (step-expr (set-expr "set" a 1)) "," (step-expr (set-expr "set" b 0)) "," (step-expr (eval-expr "eval")) "," (step-expr (output-expr "output")) ";") (test-expr (step-expr (set-expr "set" a 1)) "," (step-expr (set-expr "set" b 1)) "," (step-expr (eval-expr "eval")) "," (step-expr (output-expr "output")) ";"))
|#

@ -0,0 +1,21 @@
#lang ragg
tst-program : load-expr header-expr test-expr*
load-expr : "load" ID ","
header-expr : "output-list" ID comma-id* "," "out" ";"
comma-id : "," ID
test-expr : step-expr comma-step* ";"
comma-step : "," step-expr
step-expr : set-expr | eval-expr | output-expr
set-expr : "set" ID VAL
eval-expr : "eval"
output-expr : "output"

@ -0,0 +1,19 @@
#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]
[(seq "/*" (complement (seq any-string "*/" any-string)) "*/")
(token 'COMMENT lexeme #:skip? #t)]
[(union #\tab #\space #\newline) (get-token input-port)]
[(union "load" "output-list" "set" "eval" "output" "out" (char-set ",;")) lexeme]
[(repetition 1 +inf.0 numeric) (token 'VAL (string->number lexeme))]
[(repetition 1 +inf.0 (union alphabetic numeric (char-set "-."))) (token 'ID (string->symbol lexeme))]))
(get-token input-port))
next-token)
Loading…
Cancel
Save