simplify grammar

pull/2/head
Matthew Butterick 9 years ago
parent c574ce3b54
commit 9b2fa914f5

@ -1,10 +1,10 @@
#lang br #lang br
(provide #%top-interaction #%module-begin #%app #%datum (all-defined-out)) (provide #%top-interaction #%module-begin #%app #%datum (all-defined-out))
(define-inverting #'(chip-program "CHIP" _chipname "{" (define #'(chip-program _chipname
(_input-pin ...) (pin-spec _input-pin ...)
(_output-pin ...) (pin-spec _output-pin ...)
_part-spec "}") (part-spec (part _partname (_pin _val) ... (_lastpin _pinout)) ...))
#'(begin #'(begin
(define+provide _chipname (define+provide _chipname
(procedure-rename (procedure-rename
@ -12,25 +12,9 @@
(λ (kws kw-args . rest) (λ (kws kw-args . rest)
(define kw-pairs (map cons kws kw-args)) (define kw-pairs (map cons kws kw-args))
(let ([_input-pin (cdr (assq (string->keyword (format "~a" '_input-pin)) kw-pairs))] ...) (let ([_input-pin (cdr (assq (string->keyword (format "~a" '_input-pin)) kw-pairs))] ...)
_part-spec (define _pinout (call-part _partname [_pin _val] ...)) ...
(values _output-pin ...)))) '_chipname)))) (values _output-pin ...)))) '_chipname))))
(define-inverting #'(pin-spec _label _pin ... ";")
#'(_pin ...))
(define-cases #'pin
[#'(_ _pin ",") #'_pin]
[#'(_ _pin) #'_pin])
(define #'(part-spec "PARTS:" _part ...)
#'(begin _part ...))
(define-inverting #'(part _partname "(" (_pin _val) ... (_lastpin _pinout) ")" ";")
#'(define _pinout (call-part _partname [_pin _val] ...)))
(define-cases #'pin-val-pair
[#'(_ _pin "=" _val ",") #'(_pin _val)]
[#'(_ _pin "=" _val) #'(_pin _val)])
(define #'(call-part _partname [_pin _val] ...) (define #'(call-part _partname [_pin _val] ...)
(inject-syntax ([#'part-path (findf file-exists? (list (format "~a.hdl" (syntax->datum #'_partname)) (format "~a.hdl.rkt" (syntax->datum #'_partname))))] (inject-syntax ([#'part-path (findf file-exists? (list (format "~a.hdl" (syntax->datum #'_partname)) (format "~a.hdl.rkt" (syntax->datum #'_partname))))]

@ -1,21 +1,17 @@
#lang brag #lang brag
;; rule of thumb: use [optional] bits judiciously as they multiply the cases needed for a production rule chip-program : /"CHIP" chipname /"{" pin-spec pin-spec part-spec /"}"
;; rule of thumb: for a set of related IDs, put each into the same grammar entity
;; rule of thumb: avoid mushing unrelated IDs into one grammar entity
;; whereas a * corresponds directly to an ... in the expander macro
;; syntax patterns are good for
;; + single case / nonrecursive structure
;; + nonalternating pattern (no "this that this that ...")
chip-program : "CHIP" ID "{" pin-spec pin-spec part-spec "}" @chipname : ID
pin-spec : ("IN" | "OUT") pin+ ";" pin-spec : (/"IN" | /"OUT") pin [/"," pin]* /";"
pin : ID [","] @pin : ID
part-spec : "PARTS:" part+ part-spec : /"PARTS:" part+
part : ID "(" pin-val-pair+ ")" ";" part : partname /"(" pin-val-pair [/"," pin-val-pair]* /")" /";"
pin-val-pair : ID "=" ID [","] @partname : ID
/pin-val-pair : pin /"=" ID
Loading…
Cancel
Save