[theme] super basic codegen

This commit is contained in:
Milo Turner 2020-02-09 11:59:16 -05:00
parent ad6a7bec26
commit 2d9bad3303
4 changed files with 105 additions and 4 deletions

View File

@ -18,9 +18,13 @@ test_bin = axtest
all: ${test_bin}
clean:
rm -rf ${test_bin}
rm -rf ${test_bin} _build
gen:
mkdir -p _build
racket scripts/enum.rkt src _build
${test_bin}: ${srcs} ${hdrs}
${cc} ${cflags} ${srcs} -o ${test_bin}
.PHONY: all clean
.PHONY: all clean gen

82
scripts/enum.rkt Normal file
View File

@ -0,0 +1,82 @@
#lang racket/base
(require
racket/port
racket/path
racket/match
racket/stream)
;; -----
(struct desc [name vals-alist]
#:transparent)
(define (make-desc name syms strs)
(desc name
(sort (map cons syms strs)
string<?
#:key cdr)))
(define (read-desc port)
(match (read port)
[(? eof-object?) eof]
[`[,(? symbol? name)
(,(? symbol? syms)
,(? string? strs))
...]
(make-desc name syms strs)]
[s
(error (format "invalid enum description: ~s" s))]))
(define (parser-file-name dsc)
(format "~a.parser.inc" (desc-name dsc)))
(define (write-parser dsc [port (current-output-port)])
(match-define (desc name vals) dsc)
(for ([v+s (in-list vals)]
[i (in-naturals)])
(match-define (cons val str) v+s)
(when (> i 0)
(fprintf port "} else "))
(fprintf port "if (strcmp(str, ~s) == 0) {\n val = ~a;\n"
str val))
(unless (null? vals)
(fprintf port "} else "))
(fprintf port "{\n return 1;\n}\n"))
;; -----
(define (in-scan dir-path)
(for*/stream ([file-path (in-directory dir-path)]
#:when (equal? (path-get-extension file-path)
#".enums")
[desc (in-port read-desc (open-input-file file-path))])
desc))
(define (scan+gen scan-dir
build-dir)
(for ([dsc (in-scan scan-dir)])
(write-parser dsc
(open-output-file (build-path build-dir (parser-file-name dsc))
#:exists 'replace))))
;; -----
(module+ test
(define (display-parser dsc)
(for ([x (in-lines (open-input-string
(with-output-to-string
(λ () (write-parser dsc)))))])
(printf "; ~a\n" x)))
(display-parser (make-desc 'foobar
'(BAR FOO FAR BOO)
'("bar" "foo" "far" "boo")))
(displayln (make-string 80 #\-))
(display-parser (make-desc 'empty '() '())))
(module+ main
(match (current-command-line-arguments)
[(vector src build)
(scan+gen src build)]))

View File

@ -47,8 +47,12 @@ void ax__theme_builder_finish(
int ax__string_to_color_cat(const char* str, enum ax_color_cat* out_cat)
{
// TODO: this
return 1;
enum ax_color_cat val;
#include "../../_build/color.parser.inc"
if (out_cat != NULL) {
*out_cat = val;
}
return 0;
}
void ax__color_cat_to_string(enum ax_color_cat cat, const char** out_str)

11
src/ctxt/theme.enums Normal file
View File

@ -0,0 +1,11 @@
[color (AX_COLOR_BACKGROUND "background")
(AX_COLOR_SURFACE "surface")
(AX_COLOR_PRIMARY "primary")
(AX_COLOR_PRIMARY_ALT "primary alt")
(AX_COLOR_SECONDARY "secondary")
(AX_COLOR_SECONDARY_ALT "secondary alt")
(AX_COLOR_ON_BACKGROUND "on background")
(AX_COLOR_ON_SURFACE "on surface")
(AX_COLOR_ON_PRIMARY "on primary")
(AX_COLOR_ON_SECONDARY "on secondary")
(AX_COLOR__DEFAULT "default")]