Definitions.
There are things to like about this implementation and things to dislike. Things to like: - It works. - The defs are baked into the wordlist. Cons: - The def bodies are built at start time by parsing strings. it would be nice to somehow generate initializer literals if possible. But would that mess up the garbage collector? - It requires manual labor to update the defs when defs.txt changes. It would be nice to have a solution that *make* can use.
This commit is contained in:
parent
15482c1fdd
commit
7874496090
|
|
@ -16,3 +16,22 @@ branch, branch
|
|||
clear, clear
|
||||
cmp, cmp_joyfunc
|
||||
i, i_joyfunc
|
||||
abs, def_abs
|
||||
anamorphism, def_anamorphism
|
||||
app1, def_app1
|
||||
app2, def_app2
|
||||
app3, def_app3
|
||||
appN, def_appN
|
||||
at, def_at
|
||||
average, def_average
|
||||
b, def_b
|
||||
binary, def_binary
|
||||
ccccons, def_ccccons
|
||||
ccons, def_ccons
|
||||
cleave, def_cleave
|
||||
clop, def_clop
|
||||
codi, def_codi
|
||||
codireco, def_codireco
|
||||
dinfrirst, def_dinfrirst
|
||||
dipd, def_dipd
|
||||
disenstacken, def_disenstacken
|
||||
|
|
|
|||
|
|
@ -36,7 +36,7 @@ print('init_defs(void)')
|
|||
print('{')
|
||||
for line in defs:
|
||||
name, body = line.split(None, 1)
|
||||
print(f'\tdef_{name}_body = text_to_expression({repr(body)});')
|
||||
print(f'\tdef_{name}_body = text_to_expression("{body}");')
|
||||
print('}')
|
||||
|
||||
|
||||
|
|
@ -45,7 +45,14 @@ print()
|
|||
print()
|
||||
for line in defs:
|
||||
name, body = line.split(None, 1)
|
||||
print(f'void def_{name}(JoyListPtr stack, JoyListPtr expression)')
|
||||
print(f'JoyList def_{name}_body;')
|
||||
|
||||
|
||||
print()
|
||||
print()
|
||||
for line in defs:
|
||||
name, body = line.split(None, 1)
|
||||
print(f'void def_{name}(JoyListPtr stack, JoyListPtr expression);')
|
||||
|
||||
print()
|
||||
print()
|
||||
|
|
|
|||
|
|
@ -585,7 +585,7 @@ clear(JoyListPtr stack, __attribute__((unused)) JoyListPtr expression)
|
|||
void
|
||||
truthy(JoyListPtr stack, JoyListPtr expression)
|
||||
{
|
||||
*stack = EMPTY_LIST;
|
||||
stack = expression;
|
||||
}
|
||||
/*
|
||||
JoyListPtr s = stack;
|
||||
|
|
@ -605,6 +605,79 @@ truthy(JoyListPtr stack, JoyListPtr expression)
|
|||
*/
|
||||
|
||||
|
||||
JoyList def_abs_body;
|
||||
JoyList def_anamorphism_body;
|
||||
JoyList def_app1_body;
|
||||
JoyList def_app2_body;
|
||||
JoyList def_app3_body;
|
||||
JoyList def_appN_body;
|
||||
JoyList def_at_body;
|
||||
JoyList def_average_body;
|
||||
JoyList def_b_body;
|
||||
JoyList def_binary_body;
|
||||
JoyList def_ccccons_body;
|
||||
JoyList def_ccons_body;
|
||||
JoyList def_clear_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;
|
||||
JoyList def_dipd_body;
|
||||
JoyList def_disenstacken_body;
|
||||
|
||||
|
||||
void def_abs(__attribute__((unused)) JoyListPtr stack, JoyListPtr expression) { push_quote(def_abs_body, expression); }
|
||||
void def_anamorphism(__attribute__((unused)) JoyListPtr stack, JoyListPtr expression) { push_quote(def_anamorphism_body, expression); }
|
||||
void def_app1(__attribute__((unused)) JoyListPtr stack, JoyListPtr expression) { push_quote(def_app1_body, expression); }
|
||||
void def_app2(__attribute__((unused)) JoyListPtr stack, JoyListPtr expression) { push_quote(def_app2_body, expression); }
|
||||
void def_app3(__attribute__((unused)) JoyListPtr stack, JoyListPtr expression) { push_quote(def_app3_body, expression); }
|
||||
void def_appN(__attribute__((unused)) JoyListPtr stack, JoyListPtr expression) { push_quote(def_appN_body, expression); }
|
||||
void def_at(__attribute__((unused)) JoyListPtr stack, JoyListPtr expression) { push_quote(def_at_body, expression); }
|
||||
void def_average(__attribute__((unused)) JoyListPtr stack, JoyListPtr expression) { push_quote(def_average_body, expression); }
|
||||
void def_b(__attribute__((unused)) JoyListPtr stack, JoyListPtr expression) { push_quote(def_b_body, expression); }
|
||||
void def_binary(__attribute__((unused)) JoyListPtr stack, JoyListPtr expression) { push_quote(def_binary_body, expression); }
|
||||
void def_ccccons(__attribute__((unused)) JoyListPtr stack, JoyListPtr expression) { push_quote(def_ccccons_body, expression); }
|
||||
void def_ccons(__attribute__((unused)) JoyListPtr stack, JoyListPtr expression) { push_quote(def_ccons_body, expression); }
|
||||
void def_clear(__attribute__((unused)) JoyListPtr stack, JoyListPtr expression) { push_quote(def_clear_body, expression); }
|
||||
void def_cleave(__attribute__((unused)) JoyListPtr stack, JoyListPtr expression) { push_quote(def_cleave_body, expression); }
|
||||
void def_clop(__attribute__((unused)) JoyListPtr stack, JoyListPtr expression) { push_quote(def_clop_body, expression); }
|
||||
void def_cmp(__attribute__((unused)) JoyListPtr stack, JoyListPtr expression) { push_quote(def_cmp_body, expression); }
|
||||
void def_codi(__attribute__((unused)) JoyListPtr stack, JoyListPtr expression) { push_quote(def_codi_body, expression); }
|
||||
void def_codireco(__attribute__((unused)) JoyListPtr stack, JoyListPtr expression) { push_quote(def_codireco_body, expression); }
|
||||
void def_dinfrirst(__attribute__((unused)) JoyListPtr stack, JoyListPtr expression) { push_quote(def_dinfrirst_body, expression); }
|
||||
void def_dipd(__attribute__((unused)) JoyListPtr stack, JoyListPtr expression) { push_quote(def_dipd_body, expression); }
|
||||
void def_disenstacken(__attribute__((unused)) JoyListPtr stack, JoyListPtr expression) { push_quote(def_disenstacken_body, expression); }
|
||||
|
||||
|
||||
void
|
||||
init_defs(void)
|
||||
{
|
||||
def_abs_body = text_to_expression("dup 0 < [] [neg] branch");
|
||||
def_anamorphism_body = text_to_expression("[pop []] swap [dip swons] genrec");
|
||||
def_app1_body = text_to_expression("grba infrst");
|
||||
def_app2_body = text_to_expression("[grba swap grba swap] dip [infrst] cons ii");
|
||||
def_app3_body = text_to_expression("3 appN");
|
||||
def_appN_body = text_to_expression("[grabN] codi map reverse disenstacken");
|
||||
def_at_body = text_to_expression("drop first");
|
||||
def_average_body = text_to_expression("[sum] [size] cleave /");
|
||||
def_b_body = text_to_expression("[i] dip i");
|
||||
def_binary_body = text_to_expression("unary popd");
|
||||
def_ccccons_body = text_to_expression("ccons ccons");
|
||||
def_ccons_body = text_to_expression("cons cons");
|
||||
def_clear_body = text_to_expression("[] swaack pop");
|
||||
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");
|
||||
def_dipd_body = text_to_expression("[dip] codi");
|
||||
def_disenstacken_body = text_to_expression("? [uncons ?] loop pop");
|
||||
}
|
||||
|
||||
|
||||
/*
|
||||
██╗███╗ ██╗████████╗███████╗██████╗ ██████╗ ██████╗ ███████╗████████╗███████╗██████╗
|
||||
██║████╗ ██║╚══██╔══╝██╔════╝██╔══██╗██╔══██╗██╔══██╗██╔════╝╚══██╔══╝██╔════╝██╔══██╗
|
||||
|
|
@ -673,6 +746,8 @@ main(void)
|
|||
&deallocate_function
|
||||
);
|
||||
|
||||
init_defs();
|
||||
|
||||
line = (char *)GC_malloc(1025);
|
||||
|
||||
while (1) {
|
||||
|
|
|
|||
|
|
@ -67,6 +67,28 @@ void tdiv_q(JoyListPtr stack, JoyListPtr expression);
|
|||
void tdiv_r(JoyListPtr stack, JoyListPtr expression);
|
||||
void truthy(JoyListPtr stack, JoyListPtr expression);
|
||||
|
||||
void def_abs(JoyListPtr stack, JoyListPtr expression);
|
||||
void def_anamorphism(JoyListPtr stack, JoyListPtr expression);
|
||||
void def_app1(JoyListPtr stack, JoyListPtr expression);
|
||||
void def_app2(JoyListPtr stack, JoyListPtr expression);
|
||||
void def_app3(JoyListPtr stack, JoyListPtr expression);
|
||||
void def_appN(JoyListPtr stack, JoyListPtr expression);
|
||||
void def_at(JoyListPtr stack, JoyListPtr expression);
|
||||
void def_average(JoyListPtr stack, JoyListPtr expression);
|
||||
void def_b(JoyListPtr stack, JoyListPtr expression);
|
||||
void def_binary(JoyListPtr stack, JoyListPtr expression);
|
||||
void def_ccccons(JoyListPtr stack, JoyListPtr expression);
|
||||
void def_ccons(JoyListPtr stack, JoyListPtr expression);
|
||||
void def_clear(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);
|
||||
void def_dipd(JoyListPtr stack, JoyListPtr expression);
|
||||
void def_disenstacken(JoyListPtr stack, JoyListPtr expression);
|
||||
|
||||
|
||||
struct dict_entry {
|
||||
char *name;
|
||||
|
|
|
|||
|
|
@ -1,6 +1,6 @@
|
|||
/* ANSI-C code produced by gperf version 3.1 */
|
||||
/* Command-line: gperf --output-file=keywords.c --readonly-tables --enum --includes --hash-function-name=keyword_hash KEYWORDS.txt */
|
||||
/* Computed positions: -k'1' */
|
||||
/* Computed positions: -k'$' */
|
||||
|
||||
#if !((' ' == 32) && ('!' == 33) && ('"' == 34) && ('#' == 35) \
|
||||
&& ('%' == 37) && ('&' == 38) && ('\'' == 39) && ('(' == 40) \
|
||||
|
|
@ -35,7 +35,7 @@
|
|||
#line 7 "KEYWORDS.txt"
|
||||
struct dict_entry;
|
||||
#include <string.h>
|
||||
/* maximum key range = 16, duplicates = 0 */
|
||||
/* maximum key range = 34, duplicates = 0 */
|
||||
|
||||
#ifdef __GNUC__
|
||||
__inline
|
||||
|
|
@ -49,34 +49,34 @@ keyword_hash (register const char *str, register size_t len)
|
|||
{
|
||||
static const unsigned char asso_values[] =
|
||||
{
|
||||
17, 17, 17, 17, 17, 17, 17, 17, 17, 17,
|
||||
17, 17, 17, 17, 17, 17, 17, 17, 17, 17,
|
||||
17, 17, 17, 17, 17, 17, 17, 17, 17, 17,
|
||||
17, 17, 17, 17, 17, 17, 17, 8, 17, 17,
|
||||
17, 17, 14, 9, 17, 15, 17, 10, 17, 17,
|
||||
17, 17, 17, 17, 17, 17, 17, 17, 17, 17,
|
||||
17, 17, 17, 17, 17, 17, 17, 17, 17, 17,
|
||||
17, 17, 17, 17, 17, 17, 17, 17, 17, 17,
|
||||
17, 17, 17, 17, 17, 17, 17, 17, 17, 17,
|
||||
17, 17, 17, 17, 17, 17, 17, 17, 0, 0,
|
||||
17, 17, 17, 17, 17, 0, 17, 17, 17, 17,
|
||||
17, 17, 17, 17, 17, 17, 17, 17, 17, 17,
|
||||
17, 17, 17, 17, 17, 17, 17, 17, 17, 17,
|
||||
17, 17, 17, 17, 17, 17, 17, 17, 17, 17,
|
||||
17, 17, 17, 17, 17, 17, 17, 17, 17, 17,
|
||||
17, 17, 17, 17, 17, 17, 17, 17, 17, 17,
|
||||
17, 17, 17, 17, 17, 17, 17, 17, 17, 17,
|
||||
17, 17, 17, 17, 17, 17, 17, 17, 17, 17,
|
||||
17, 17, 17, 17, 17, 17, 17, 17, 17, 17,
|
||||
17, 17, 17, 17, 17, 17, 17, 17, 17, 17,
|
||||
17, 17, 17, 17, 17, 17, 17, 17, 17, 17,
|
||||
17, 17, 17, 17, 17, 17, 17, 17, 17, 17,
|
||||
17, 17, 17, 17, 17, 17, 17, 17, 17, 17,
|
||||
17, 17, 17, 17, 17, 17, 17, 17, 17, 17,
|
||||
17, 17, 17, 17, 17, 17, 17, 17, 17, 17,
|
||||
17, 17, 17, 17, 17, 17
|
||||
35, 35, 35, 35, 35, 35, 35, 35, 35, 35,
|
||||
35, 35, 35, 35, 35, 35, 35, 35, 35, 35,
|
||||
35, 35, 35, 35, 35, 35, 35, 35, 35, 35,
|
||||
35, 35, 35, 35, 35, 35, 35, 24, 35, 35,
|
||||
35, 35, 19, 14, 35, 31, 35, 30, 35, 23,
|
||||
18, 30, 35, 35, 35, 35, 35, 35, 35, 35,
|
||||
35, 35, 35, 35, 35, 35, 35, 35, 35, 35,
|
||||
35, 35, 35, 35, 35, 35, 35, 35, 25, 35,
|
||||
35, 35, 35, 35, 35, 35, 35, 35, 35, 35,
|
||||
35, 35, 35, 35, 35, 35, 35, 35, 25, 35,
|
||||
20, 5, 35, 35, 15, 0, 35, 35, 15, 5,
|
||||
5, 0, 10, 35, 5, 0, 0, 35, 35, 35,
|
||||
35, 0, 35, 35, 35, 35, 35, 35, 35, 35,
|
||||
35, 35, 35, 35, 35, 35, 35, 35, 35, 35,
|
||||
35, 35, 35, 35, 35, 35, 35, 35, 35, 35,
|
||||
35, 35, 35, 35, 35, 35, 35, 35, 35, 35,
|
||||
35, 35, 35, 35, 35, 35, 35, 35, 35, 35,
|
||||
35, 35, 35, 35, 35, 35, 35, 35, 35, 35,
|
||||
35, 35, 35, 35, 35, 35, 35, 35, 35, 35,
|
||||
35, 35, 35, 35, 35, 35, 35, 35, 35, 35,
|
||||
35, 35, 35, 35, 35, 35, 35, 35, 35, 35,
|
||||
35, 35, 35, 35, 35, 35, 35, 35, 35, 35,
|
||||
35, 35, 35, 35, 35, 35, 35, 35, 35, 35,
|
||||
35, 35, 35, 35, 35, 35, 35, 35, 35, 35,
|
||||
35, 35, 35, 35, 35, 35, 35, 35, 35, 35,
|
||||
35, 35, 35, 35, 35, 35
|
||||
};
|
||||
return len + asso_values[(unsigned char)str[0]];
|
||||
return len + asso_values[(unsigned char)str[len - 1]];
|
||||
}
|
||||
|
||||
const struct dict_entry *
|
||||
|
|
@ -84,11 +84,11 @@ in_word_set (register const char *str, register size_t len)
|
|||
{
|
||||
enum
|
||||
{
|
||||
TOTAL_KEYWORDS = 10,
|
||||
TOTAL_KEYWORDS = 29,
|
||||
MIN_WORD_LENGTH = 1,
|
||||
MAX_WORD_LENGTH = 6,
|
||||
MAX_WORD_LENGTH = 12,
|
||||
MIN_HASH_VALUE = 1,
|
||||
MAX_HASH_VALUE = 16
|
||||
MAX_HASH_VALUE = 34
|
||||
};
|
||||
|
||||
static const struct dict_entry wordlist[] =
|
||||
|
|
@ -96,27 +96,67 @@ in_word_set (register const char *str, register size_t len)
|
|||
{""},
|
||||
#line 18 "KEYWORDS.txt"
|
||||
{"i", i_joyfunc},
|
||||
{""},
|
||||
#line 17 "KEYWORDS.txt"
|
||||
{"cmp", cmp_joyfunc},
|
||||
#line 14 "KEYWORDS.txt"
|
||||
{"bool", truthy},
|
||||
#line 25 "KEYWORDS.txt"
|
||||
{"at", def_at},
|
||||
#line 19 "KEYWORDS.txt"
|
||||
{"abs", def_abs},
|
||||
#line 33 "KEYWORDS.txt"
|
||||
{"codi", def_codi},
|
||||
#line 30 "KEYWORDS.txt"
|
||||
{"ccons", def_ccons},
|
||||
#line 28 "KEYWORDS.txt"
|
||||
{"binary", def_binary},
|
||||
#line 29 "KEYWORDS.txt"
|
||||
{"ccccons", def_ccccons},
|
||||
#line 34 "KEYWORDS.txt"
|
||||
{"codireco", def_codireco},
|
||||
#line 35 "KEYWORDS.txt"
|
||||
{"dinfrirst", def_dinfrirst},
|
||||
#line 16 "KEYWORDS.txt"
|
||||
{"clear", clear},
|
||||
#line 15 "KEYWORDS.txt"
|
||||
{"branch", branch},
|
||||
{""}, {""},
|
||||
#line 9 "KEYWORDS.txt"
|
||||
{"%", tdiv_r},
|
||||
#line 31 "KEYWORDS.txt"
|
||||
{"cleave", def_cleave},
|
||||
#line 26 "KEYWORDS.txt"
|
||||
{"average", def_average},
|
||||
#line 17 "KEYWORDS.txt"
|
||||
{"cmp", cmp_joyfunc},
|
||||
#line 32 "KEYWORDS.txt"
|
||||
{"clop", def_clop},
|
||||
#line 11 "KEYWORDS.txt"
|
||||
{"+", add},
|
||||
#line 13 "KEYWORDS.txt"
|
||||
{"/", tdiv_q},
|
||||
{""}, {""}, {""},
|
||||
#line 20 "KEYWORDS.txt"
|
||||
{"anamorphism", def_anamorphism},
|
||||
#line 37 "KEYWORDS.txt"
|
||||
{"disenstacken", def_disenstacken},
|
||||
{""},
|
||||
#line 14 "KEYWORDS.txt"
|
||||
{"bool", truthy},
|
||||
#line 10 "KEYWORDS.txt"
|
||||
{"*", mul},
|
||||
#line 15 "KEYWORDS.txt"
|
||||
{"branch", branch},
|
||||
#line 22 "KEYWORDS.txt"
|
||||
{"app2", def_app2},
|
||||
{""},
|
||||
#line 36 "KEYWORDS.txt"
|
||||
{"dipd", def_dipd},
|
||||
#line 9 "KEYWORDS.txt"
|
||||
{"%", tdiv_r},
|
||||
#line 27 "KEYWORDS.txt"
|
||||
{"b", def_b},
|
||||
#line 21 "KEYWORDS.txt"
|
||||
{"app1", def_app1},
|
||||
{""},
|
||||
#line 24 "KEYWORDS.txt"
|
||||
{"appN", def_appN},
|
||||
{""},
|
||||
#line 13 "KEYWORDS.txt"
|
||||
{"/", tdiv_q},
|
||||
#line 12 "KEYWORDS.txt"
|
||||
{"-", sub}
|
||||
{"-", sub},
|
||||
{""},
|
||||
#line 23 "KEYWORDS.txt"
|
||||
{"app3", def_app3}
|
||||
};
|
||||
|
||||
if (len <= MAX_WORD_LENGTH && len >= MIN_WORD_LENGTH)
|
||||
|
|
|
|||
Loading…
Reference in New Issue