Put defs into a generated file.

This is only part of the solution, we still need to update joy.h and
KEYWORDS.txt when defs.txt changes, and we need to handle names that
have non-C-identifier characters in them.

But it's a start...
This commit is contained in:
Simon Forman 2023-02-05 13:04:31 -08:00
parent 6eae43c27f
commit 78b59dda35
5 changed files with 143 additions and 91 deletions

View File

@ -1,4 +1,4 @@
OBJS=joy.o keywords.o
OBJS=joy.o keywords.o definitions.o
# On FreeBSD gc and gmp are installed in /usr/local/:
STATIC_GCLIB=/usr/local/lib/libgc.a /usr/local/lib/libgmp.a
@ -17,6 +17,9 @@ joy: ${OBJS} $(STATIC_GCLIB)
joy.o: joy.h
definitions.c: defs.txt convert_defs.py
python convert_defs.py > definitions.c
# Gperf doesn't add anything to '{""}' unused entries in the wordlist.
# This causes "warning: missing field 'func' initializer [-Wmissing-field-initializers]"

View File

@ -40,16 +40,46 @@ disenstacken ? [uncons ?] loop pop
swons swap cons
infra swons swaack [i] dip swaack'''.splitlines()
##
##for line in defs:
## name, body = line.split(None, 1)
## print(f'{name}, def_{name}')
##
##print()
##print()
##for line in defs:
## name, body = line.split(None, 1)
## print(f'void def_{name}(JoyListPtr stack, JoyListPtr expression);')
print(f'''
/*
Auto-generated file by {sys.argv[0]}
*/
#include "joy.h"
/*
Declare a bunch of list pointers to eventually hold the body expressions
of the definitions.
*/
''')
for line in defs:
name, body = line.split(None, 1)
print(f'{name}, def_{name}')
print(f'JoyList def_{name}_body;')
print()
print()
print('void')
print('init_defs(void)')
print('{')
print('''
/*
Next, we want an initializer function to fill out the body pointers.
*/
void
init_defs(void)
{
''')
for line in defs:
name, body = line.split(None, 1)
print(f'\tdef_{name}_body = text_to_expression("{body}");')
@ -57,21 +87,12 @@ print('}')
print()
print()
for line in defs:
name, body = line.split(None, 1)
print(f'JoyList def_{name}_body;')
print('''
print()
print()
for line in defs:
name, body = line.split(None, 1)
print(f'void def_{name}(JoyListPtr stack, JoyListPtr expression);')
print()
print()
/*
Last, a set of functions to go in the wordlist, one for each definition.
*/
''')
for line in defs:
name, body = line.split(None, 1)
print(f'void def_{name}(__attribute__((unused)) JoyListPtr stack, JoyListPtr expression) {{ push_quote_onto_expression(def_{name}_body, expression); }}')

View File

@ -0,0 +1,93 @@
abs dup 0 < [] [neg] branch
anamorphism [pop []] swap [dip swons] genrec
app1 grba infrst
app2 [grba swap grba swap] dip [infrst] cons ii
app3 3 appN
appN [grabN] codi map reverse disenstacken
at drop first
average [sum] [size] cleave /
b [i] dip i
binary unary popd
ccccons ccons ccons
ccons cons cons
clear [] swaack pop
cleave fork popdd
clop cleave popdd
cmp [[>] swap] dipd [ifte] ccons [=] swons ifte
codi cons dip
codireco codi reco
dinfrirst dip infrst
dipd [dip] codi
disenstacken ? [uncons ?] loop pop
down_to_zero [0 >] [dup --] while
drop [rest] times
dupd [dup] dip
dupdd [dup] dipd
dupdip dupd dip
dupdipd dup dipd
enstacken stack [clear] dip
first uncons pop
flatten <{} [concat] step
fork [i] app2
fourth rest third
gcd true [tuck mod dup 0 >] loop pop
genrec [[genrec] ccccons] nullary swons concat ifte
grabN <{} [cons] times
grba [stack popd] dip
hypot [sqr] ii + sqrt
ifte [nullary] dipd swap branch
ii [dip] dupdip i
infra swons swaack [i] dip swaack
infrst infra first
make_generator [codireco] ccons
mod %
neg 0 swap -
not [true] [false] branch
nulco [nullary] cons
nullary [stack] dinfrirst
of swap at
pam [i] map
pm [+] [-] clop
popd [pop] dip
popdd [pop] dipd
popop pop pop
popopop pop popop
popopd [popop] dip
popopdd [popop] dipd
product 1 swap [*] step
quoted [unit] dip
range [0 <=] [1 - dup] anamorphism
range_to_zero unit [down_to_zero] infra
reco rest cons
rest uncons popd
reverse <{} shunt
rrest rest rest
run <{} infra
second rest first
shift uncons [swons] dip
shunt [swons] step
size [pop ++] step_zero
spiral_next [[[abs] ii <=] [[<>] [pop !-] ||] &&] [[!-] [[++]] [[--]] ifte dip] [[pop !-] [--] [++] ifte] ifte
split_at [drop] [take] clop
split_list [take reverse] [drop] clop
sqr dup *
stackd [stack] dip
step_zero 0 roll> step
stuncons stack uncons
sum [+] step_zero
swapd [swap] dip
swons swap cons
swoncat swap concat
sqr dup mul
tailrec [i] genrec
take <<{} [shift] times pop
ternary binary popd
third rest second
tuck dup swapd
unary nullary popd
uncons [first] [rest] cleave
unit [] cons
unquoted [i] dip
unswons uncons swap
while swap nulco dupdipd concat loop
x dup i

View File

@ -706,76 +706,6 @@ truthy(JoyListPtr stack, __attribute__((unused)) 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_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_onto_expression(def_abs_body, expression); }
void def_anamorphism(__attribute__((unused)) JoyListPtr stack, JoyListPtr expression) { push_quote_onto_expression(def_anamorphism_body, expression); }
void def_app1(__attribute__((unused)) JoyListPtr stack, JoyListPtr expression) { push_quote_onto_expression(def_app1_body, expression); }
void def_app2(__attribute__((unused)) JoyListPtr stack, JoyListPtr expression) { push_quote_onto_expression(def_app2_body, expression); }
void def_app3(__attribute__((unused)) JoyListPtr stack, JoyListPtr expression) { push_quote_onto_expression(def_app3_body, expression); }
void def_appN(__attribute__((unused)) JoyListPtr stack, JoyListPtr expression) { push_quote_onto_expression(def_appN_body, expression); }
void def_at(__attribute__((unused)) JoyListPtr stack, JoyListPtr expression) { push_quote_onto_expression(def_at_body, expression); }
void def_average(__attribute__((unused)) JoyListPtr stack, JoyListPtr expression) { push_quote_onto_expression(def_average_body, expression); }
void def_b(__attribute__((unused)) JoyListPtr stack, JoyListPtr expression) { push_quote_onto_expression(def_b_body, expression); }
void def_binary(__attribute__((unused)) JoyListPtr stack, JoyListPtr expression) { push_quote_onto_expression(def_binary_body, expression); }
void def_ccccons(__attribute__((unused)) JoyListPtr stack, JoyListPtr expression) { push_quote_onto_expression(def_ccccons_body, 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); }
void def_dipd(__attribute__((unused)) JoyListPtr stack, JoyListPtr expression) { push_quote_onto_expression(def_dipd_body, expression); }
void def_disenstacken(__attribute__((unused)) JoyListPtr stack, JoyListPtr expression) { push_quote_onto_expression(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_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");
}
/*

View File

@ -56,6 +56,11 @@ struct list_node {
typedef void (*JoyFunc)(JoyListPtr, JoyListPtr);
JoyList text_to_expression(char *text);
void push_quote_onto_expression(JoyList el, JoyListPtr expression);
void init_defs(void);
void add(JoyListPtr stack, JoyListPtr expression);
void branch(JoyListPtr stack, JoyListPtr expression);
void clear(JoyListPtr stack, JoyListPtr expression);