From 8bda5d511a8326cd369e1df052e162a4b81a21d8 Mon Sep 17 00:00:00 2001 From: Simon Forman Date: Sun, 5 Feb 2023 13:50:16 -0800 Subject: [PATCH] Auto-generate defintions from defs.txt But it doesn't yet handle names with non-C-indentifier characters in them. --- implementations/C/KEYWORDS.in | 27 ++++++++ implementations/C/KEYWORDS.txt | 8 ++- implementations/C/Makefile | 7 +- implementations/C/convert_defs.py | 7 +- implementations/C/definitions.c | 3 - implementations/C/definitions.h | 1 - implementations/C/keywords.c | 105 +++++++++++++++--------------- 7 files changed, 98 insertions(+), 60 deletions(-) create mode 100644 implementations/C/KEYWORDS.in diff --git a/implementations/C/KEYWORDS.in b/implementations/C/KEYWORDS.in new file mode 100644 index 0000000..68a6dc4 --- /dev/null +++ b/implementations/C/KEYWORDS.in @@ -0,0 +1,27 @@ +%compare-strncmp +%readonly-tables +%struct-type +%{ +#include "joy.h" +#include "definitions.h" +%} +struct dict_entry; +%% +"%", tdiv_r +*, mul ++, add +-, sub +/, tdiv_q +bool, truthy +branch, branch +clear, clear +cmp, cmp_joyfunc +cons, cons +concat, concat +dip, dip +dup, dup +i, i_joyfunc +pop, pop +stack, stack +swaack, swaack +swap, swap diff --git a/implementations/C/KEYWORDS.txt b/implementations/C/KEYWORDS.txt index cafe101..0ee4ced 100644 --- a/implementations/C/KEYWORDS.txt +++ b/implementations/C/KEYWORDS.txt @@ -1,3 +1,8 @@ +/* +Auto-generated file by convert_defs.py --keywords +Do not edit. +*/ + %compare-strncmp %readonly-tables %struct-type @@ -25,7 +30,6 @@ pop, pop stack, stack swaack, swaack swap, swap -swons, def_swons abs, def_abs anamorphism, def_anamorphism app1, def_app1 @@ -45,3 +49,5 @@ codireco, def_codireco dinfrirst, def_dinfrirst dipd, def_dipd disenstacken, def_disenstacken +swons, def_swons +infra, def_infra diff --git a/implementations/C/Makefile b/implementations/C/Makefile index ccdf10b..06af1a3 100644 --- a/implementations/C/Makefile +++ b/implementations/C/Makefile @@ -18,13 +18,16 @@ joy: ${OBJS} $(STATIC_GCLIB) joy.o: joy.h definitions.c: defs.txt convert_defs.py - python convert_defs.py > definitions.c + python convert_defs.py > $@ definitions.h: defs.txt convert_defs.py - python convert_defs.py --header > definitions.h + python convert_defs.py --header > $@ definitions.o: definitions.h +KEYWORDS.txt: KEYWORDS.in defs.txt convert_defs.py + python convert_defs.py --keywords > $@ + # Gperf doesn't add anything to '{""}' unused entries in the wordlist. # This causes "warning: missing field 'func' initializer [-Wmissing-field-initializers]" # which is promoted to an error and holds up the show. diff --git a/implementations/C/convert_defs.py b/implementations/C/convert_defs.py index beb7478..9a2a8a7 100644 --- a/implementations/C/convert_defs.py +++ b/implementations/C/convert_defs.py @@ -31,7 +31,6 @@ ccccons ccons ccons ccons cons cons cleave fork popdd clop cleave popdd -cmp [[>] swap] dipd [ifte] ccons [=] swons ifte codi cons dip codireco codi reco dinfrirst dip infrst @@ -62,6 +61,12 @@ if sys.argv[-1] == '--header': print(f'void def_{name}(JoyListPtr stack, JoyListPtr expression);') +elif sys.argv[-1] == '--keywords': + sys.stdout.write(open('KEYWORDS.in').read()) + for line in defs: + name, body = line.split(None, 1) + print(f'{name}, def_{name}') + else: print(''' diff --git a/implementations/C/definitions.c b/implementations/C/definitions.c index 7f45943..3e26150 100644 --- a/implementations/C/definitions.c +++ b/implementations/C/definitions.c @@ -27,7 +27,6 @@ JoyList def_ccccons_body; JoyList def_ccons_body; JoyList def_cleave_body; JoyList def_clop_body; -JoyList def_cmp_body; JoyList def_codi_body; JoyList def_codireco_body; JoyList def_dinfrirst_body; @@ -59,7 +58,6 @@ JoyList def_infra_body; def_ccons_body = text_to_expression("cons cons"); def_cleave_body = text_to_expression("fork popdd"); def_clop_body = text_to_expression("cleave popdd"); - def_cmp_body = text_to_expression("[[>] swap] dipd [ifte] ccons [=] swons ifte"); def_codi_body = text_to_expression("cons dip"); def_codireco_body = text_to_expression("codi reco"); def_dinfrirst_body = text_to_expression("dip infrst"); @@ -88,7 +86,6 @@ void def_ccccons(__attribute__((unused)) JoyListPtr stack, JoyListPtr expression void def_ccons(__attribute__((unused)) JoyListPtr stack, JoyListPtr expression) { push_quote_onto_expression(def_ccons_body, expression); } void def_cleave(__attribute__((unused)) JoyListPtr stack, JoyListPtr expression) { push_quote_onto_expression(def_cleave_body, expression); } void def_clop(__attribute__((unused)) JoyListPtr stack, JoyListPtr expression) { push_quote_onto_expression(def_clop_body, expression); } -void def_cmp(__attribute__((unused)) JoyListPtr stack, JoyListPtr expression) { push_quote_onto_expression(def_cmp_body, expression); } void def_codi(__attribute__((unused)) JoyListPtr stack, JoyListPtr expression) { push_quote_onto_expression(def_codi_body, expression); } void def_codireco(__attribute__((unused)) JoyListPtr stack, JoyListPtr expression) { push_quote_onto_expression(def_codireco_body, expression); } void def_dinfrirst(__attribute__((unused)) JoyListPtr stack, JoyListPtr expression) { push_quote_onto_expression(def_dinfrirst_body, expression); } diff --git a/implementations/C/definitions.h b/implementations/C/definitions.h index 083524e..638fbb8 100644 --- a/implementations/C/definitions.h +++ b/implementations/C/definitions.h @@ -17,7 +17,6 @@ void def_ccccons(JoyListPtr stack, JoyListPtr expression); void def_ccons(JoyListPtr stack, JoyListPtr expression); void def_cleave(JoyListPtr stack, JoyListPtr expression); void def_clop(JoyListPtr stack, JoyListPtr expression); -void def_cmp(JoyListPtr stack, JoyListPtr expression); void def_codi(JoyListPtr stack, JoyListPtr expression); void def_codireco(JoyListPtr stack, JoyListPtr expression); void def_dinfrirst(JoyListPtr stack, JoyListPtr expression); diff --git a/implementations/C/keywords.c b/implementations/C/keywords.c index 6c37496..f79da53 100644 --- a/implementations/C/keywords.c +++ b/implementations/C/keywords.c @@ -29,11 +29,11 @@ #error "gperf generated tables don't work with this execution character set. Please report a bug to ." #endif -#line 4 "KEYWORDS.txt" +#line 9 "KEYWORDS.txt" #include "joy.h" #include "definitions.h" -#line 8 "KEYWORDS.txt" +#line 13 "KEYWORDS.txt" struct dict_entry; #include /* maximum key range = 61, duplicates = 0 */ @@ -59,7 +59,7 @@ keyword_hash (register const char *str, register size_t len) 62, 62, 62, 62, 62, 62, 62, 62, 62, 62, 62, 62, 62, 62, 62, 62, 62, 62, 30, 62, 62, 62, 62, 62, 62, 62, 62, 62, 62, 62, - 62, 62, 62, 62, 62, 62, 62, 62, 0, 0, + 62, 62, 62, 62, 62, 62, 62, 15, 0, 0, 20, 0, 62, 62, 30, 10, 62, 5, 25, 5, 0, 0, 10, 62, 0, 0, 0, 15, 5, 10, 62, 10, 62, 62, 62, 62, 62, 62, 62, 62, @@ -95,7 +95,7 @@ in_word_set (register const char *str, register size_t len) { enum { - TOTAL_KEYWORDS = 38, + TOTAL_KEYWORDS = 39, MIN_WORD_LENGTH = 1, MAX_WORD_LENGTH = 12, MIN_HASH_VALUE = 1, @@ -105,98 +105,99 @@ in_word_set (register const char *str, register size_t len) static const struct dict_entry wordlist[] = { {""}, -#line 37 "KEYWORDS.txt" +#line 41 "KEYWORDS.txt" {"b", def_b}, -#line 35 "KEYWORDS.txt" - {"at", def_at}, -#line 29 "KEYWORDS.txt" - {"abs", def_abs}, -#line 19 "KEYWORDS.txt" - {"cons", cons}, -#line 40 "KEYWORDS.txt" - {"ccons", def_ccons}, -#line 20 "KEYWORDS.txt" - {"concat", concat}, #line 39 "KEYWORDS.txt" - {"ccccons", def_ccccons}, + {"at", def_at}, +#line 33 "KEYWORDS.txt" + {"abs", def_abs}, +#line 24 "KEYWORDS.txt" + {"cons", cons}, #line 44 "KEYWORDS.txt" + {"ccons", def_ccons}, +#line 25 "KEYWORDS.txt" + {"concat", concat}, +#line 43 "KEYWORDS.txt" + {"ccccons", def_ccccons}, +#line 48 "KEYWORDS.txt" {"codireco", def_codireco}, {""}, -#line 25 "KEYWORDS.txt" - {"stack", stack}, -#line 23 "KEYWORDS.txt" - {"i", i_joyfunc}, -#line 36 "KEYWORDS.txt" - {"average", def_average}, -#line 24 "KEYWORDS.txt" - {"pop", pop}, -#line 43 "KEYWORDS.txt" - {"codi", def_codi}, -#line 28 "KEYWORDS.txt" - {"swons", def_swons}, #line 30 "KEYWORDS.txt" + {"stack", stack}, +#line 28 "KEYWORDS.txt" + {"i", i_joyfunc}, +#line 40 "KEYWORDS.txt" + {"average", def_average}, +#line 29 "KEYWORDS.txt" + {"pop", pop}, +#line 47 "KEYWORDS.txt" + {"codi", def_codi}, +#line 52 "KEYWORDS.txt" + {"swons", def_swons}, +#line 34 "KEYWORDS.txt" {"anamorphism", def_anamorphism}, {""}, -#line 18 "KEYWORDS.txt" +#line 23 "KEYWORDS.txt" {"cmp", cmp_joyfunc}, -#line 45 "KEYWORDS.txt" +#line 49 "KEYWORDS.txt" {"dinfrirst", def_dinfrirst}, - {""}, -#line 26 "KEYWORDS.txt" +#line 53 "KEYWORDS.txt" + {"infra", def_infra}, +#line 31 "KEYWORDS.txt" {"swaack", swaack}, -#line 47 "KEYWORDS.txt" +#line 51 "KEYWORDS.txt" {"disenstacken", def_disenstacken}, -#line 21 "KEYWORDS.txt" +#line 26 "KEYWORDS.txt" {"dip", dip}, -#line 27 "KEYWORDS.txt" +#line 32 "KEYWORDS.txt" {"swap", swap}, {""}, -#line 38 "KEYWORDS.txt" +#line 42 "KEYWORDS.txt" {"binary", def_binary}, {""}, -#line 22 "KEYWORDS.txt" +#line 27 "KEYWORDS.txt" {"dup", dup}, -#line 15 "KEYWORDS.txt" +#line 20 "KEYWORDS.txt" {"bool", truthy}, -#line 17 "KEYWORDS.txt" +#line 22 "KEYWORDS.txt" {"clear", clear}, -#line 41 "KEYWORDS.txt" +#line 45 "KEYWORDS.txt" {"cleave", def_cleave}, {""}, {""}, -#line 46 "KEYWORDS.txt" +#line 50 "KEYWORDS.txt" {"dipd", def_dipd}, {""}, -#line 16 "KEYWORDS.txt" +#line 21 "KEYWORDS.txt" {"branch", branch}, {""}, {""}, -#line 42 "KEYWORDS.txt" +#line 46 "KEYWORDS.txt" {"clop", def_clop}, {""}, -#line 14 "KEYWORDS.txt" +#line 19 "KEYWORDS.txt" {"/", tdiv_q}, {""}, {""}, -#line 34 "KEYWORDS.txt" +#line 38 "KEYWORDS.txt" {"appN", def_appN}, {""}, -#line 13 "KEYWORDS.txt" +#line 18 "KEYWORDS.txt" {"-", sub}, {""}, {""}, -#line 33 "KEYWORDS.txt" +#line 37 "KEYWORDS.txt" {"app3", def_app3}, {""}, -#line 12 "KEYWORDS.txt" +#line 17 "KEYWORDS.txt" {"+", add}, {""}, {""}, -#line 32 "KEYWORDS.txt" +#line 36 "KEYWORDS.txt" {"app2", def_app2}, {""}, -#line 11 "KEYWORDS.txt" +#line 16 "KEYWORDS.txt" {"*", mul}, {""}, {""}, -#line 31 "KEYWORDS.txt" +#line 35 "KEYWORDS.txt" {"app1", def_app1}, {""}, -#line 10 "KEYWORDS.txt" +#line 15 "KEYWORDS.txt" {"%", tdiv_r} };