[util] enum printer codegen

This commit is contained in:
Milo Turner 2020-02-12 12:38:16 -05:00
parent da031f297e
commit b918ffca5e
1 changed files with 15 additions and 0 deletions

View File

@ -30,6 +30,9 @@
(define (parser-file-name dsc)
(format "~a.parser.inc" (desc-name dsc)))
(define (printer-file-name dsc)
(format "~a.printer.inc" (desc-name dsc)))
(define (write-parser dsc [port (current-output-port)])
(match-define (desc name vals) dsc)
(for ([v+s (in-list vals)]
@ -43,6 +46,15 @@
(fprintf port "} else "))
(fprintf port "{\n return 1;\n}\n"))
(define (write-printer dsc [port (current-output-port)])
(match-define (desc name vals) dsc)
(fprintf port "switch (val) {\n")
(for ([v+s (in-list vals)])
(match-define (cons val str) v+s)
(fprintf port "case ~a:\n str = ~s;\n break;\n" val str))
(fprintf port "default:\n IMPOSSIBLE(\"invalid enum value for `~a' printer\");\n}\n"
name))
;; -----
(define (in-scan dir-path)
@ -57,6 +69,9 @@
(for ([dsc (in-scan scan-dir)])
(write-parser dsc
(open-output-file (build-path build-dir (parser-file-name dsc))
#:exists 'replace))
(write-printer dsc
(open-output-file (build-path build-dir (printer-file-name dsc))
#:exists 'replace))))
;; -----