Skip to content

Commit

Permalink
[nfc] Add ability to test code snippets
Browse files Browse the repository at this point in the history
This demonstrates a flow that can be used to validate that all code
snippets used in the FIRRTL specification can be compiled.  This is
implemented using a simple pandoc filter to extract all FIRRTL code
blocks, write them to the build directory, and then run them all through
firtool.  This may be a little cumbersome as every code example has to be
completely legal FIRRTL.  I.e., this would require a version and circuit
on every code example.  This may not be such a bad thing.

Signed-off-by: Schuyler Eldridge <[email protected]>
  • Loading branch information
seldridge committed Feb 12, 2024
1 parent 35de1a6 commit 7bccdb7
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 1 deletion.
6 changes: 5 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ IMG_SRCS=$(shell find include/img_src/ -type f -name '*.dot')
IMG_EPSS=$(IMG_SRCS:include/img_src/%.dot=build/%.eps)
IMG_PNG=$(IMG_SRCS:include/img_src/%.dot=build/%.png)

.PHONY: all clean format images
.PHONY: all clean format images test
.PRECIOUS: build/ build/img/

all: build/spec.pdf build/abi.pdf
Expand All @@ -18,6 +18,10 @@ format:

images: $(IMG_EPSS) $(IMG_PNGS)

test: abi.md spec.md | build/
pandoc --filter scripts/extract-firrtl-code.py $^ -o /dev/null
find -s build/ -type f -name '*.fir' | xargs -n1 firtool -parse-only -o /dev/null

PANDOC_FLAGS=\
--pdf-engine=latexmk \
--pdf-engine-opt=-logfilewarninglist \
Expand Down
17 changes: 17 additions & 0 deletions scripts/extract-firrtl-code.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
#!/usr/bin/env python3

from pandocfilters import toJSONFilter, CodeBlock

index = 0

def extractFIRRTLCode(key, value, format, meta):
global index
if key == 'CodeBlock':
[[ident, classes, keyvals], code] = value
if 'firrtl' in classes:
with open(f'build/code-example-{index:03d}.fir', 'w') as f:
f.write(code)
index += 1

if __name__ == "__main__":
toJSONFilter(extractFIRRTLCode)

0 comments on commit 7bccdb7

Please sign in to comment.