Skip to content

Commit b86cb11

Browse files
committed
Port execute_fn, clean up a bunch of functions no longer used in C
1 parent 5e65bbc commit b86cb11

12 files changed

Lines changed: 418 additions & 860 deletions

File tree

crates/engine_bibtex/bibtex/bibtex.c

Lines changed: 8 additions & 275 deletions
Original file line numberDiff line numberDiff line change
@@ -16,18 +16,10 @@
1616

1717
static jmp_buf error_jmpbuf, recover_jmpbuf;
1818

19-
/*14:*/
20-
21-
#define hash_base 1 /*empty 1*/
22-
#define quote_next_fn (hash_base - 1)
2319
#define aux_stack_size 20
2420

25-
/*22: */
26-
2721
typedef size_t hash_loc;
2822
typedef size_t cite_number;
29-
typedef size_t wiz_fn_loc;
30-
typedef size_t str_ent_loc;
3123
typedef size_t field_loc;
3224

3325
static void unwrap(CResult res) {
@@ -82,8 +74,6 @@ static LookupRes unwrap_lookup(CResultLookup res) {
8274
return res.ok;
8375
}
8476

85-
/*:473*//*12: *//*3: */
86-
8777
#define FMT_BUF_SIZE 1024
8878
static char fmt_buf[FMT_BUF_SIZE] = "";
8979

@@ -99,262 +89,6 @@ printf_log(const char *fmt, ...)
9989
puts_log(fmt_buf);
10090
}
10191

102-
/*:159*//*160: */
103-
104-
static void execute_fn(ExecCtx* ctx, hash_loc ex_fn_loc)
105-
{
106-
wiz_fn_loc wiz_ptr;
107-
108-
switch ((fn_type(ex_fn_loc))) {
109-
case FN_CLASS_BUILTIN:
110-
// printf_log("Executing builtin %d\n", ilk_info(ex_fn_loc));
111-
switch ((ilk_info(ex_fn_loc))) {
112-
case 0:
113-
unwrap(x_equals(ctx));
114-
break;
115-
case 1:
116-
unwrap(x_greater_than(ctx));
117-
break;
118-
case 2:
119-
unwrap(x_less_than(ctx));
120-
break;
121-
case 3:
122-
unwrap(x_plus(ctx));
123-
break;
124-
case 4:
125-
unwrap(x_minus(ctx));
126-
break;
127-
case 5:
128-
unwrap(x_concatenate(ctx));
129-
break;
130-
case 6:
131-
unwrap(x_gets(ctx));
132-
break;
133-
case 7:
134-
unwrap(x_add_period(ctx));
135-
break;
136-
case 8:
137-
if (!ctx->mess_with_entries)
138-
unwrap(bst_cant_mess_with_entries_print(ctx));
139-
else if (type_list(cite_ptr()) == undefined())
140-
execute_fn(ctx, ctx->_default);
141-
else if (type_list(cite_ptr()) == 0 /*empty */ ) ;
142-
else
143-
execute_fn(ctx, type_list(cite_ptr()));
144-
break;
145-
case 9:
146-
unwrap(x_change_case(ctx));
147-
break;
148-
case 10:
149-
unwrap(x_chr_to_int(ctx));
150-
break;
151-
case 11:
152-
unwrap(x_cite(ctx));
153-
break;
154-
case 12:
155-
unwrap(x_duplicate(ctx));
156-
break;
157-
case 13:
158-
unwrap(x_empty(ctx));
159-
break;
160-
case 14:
161-
unwrap(x_format_name(ctx));
162-
break;
163-
case 15:
164-
unwrap(pop_lit_stk(ctx, &ctx->pop1));
165-
unwrap(pop_lit_stk(ctx, &ctx->pop2));
166-
unwrap(pop_lit_stk(ctx, &ctx->pop3));
167-
if (ctx->pop1.tag != ExecVal_Function)
168-
unwrap(print_wrong_stk_lit(ctx, ctx->pop1, STK_TYPE_FUNCTION));
169-
else if (ctx->pop2.tag != ExecVal_Function)
170-
unwrap(print_wrong_stk_lit(ctx, ctx->pop2, STK_TYPE_FUNCTION));
171-
else if (ctx->pop3.tag != ExecVal_Integer)
172-
unwrap(print_wrong_stk_lit(ctx, ctx->pop3, STK_TYPE_INTEGER));
173-
else if (ctx->pop3.integer > 0)
174-
execute_fn(ctx, ctx->pop2.function);
175-
else
176-
execute_fn(ctx, ctx->pop1.function);
177-
break;
178-
case 16:
179-
unwrap(x_int_to_chr(ctx));
180-
break;
181-
case 17:
182-
unwrap(x_int_to_str(ctx));
183-
break;
184-
case 18:
185-
unwrap(x_missing(ctx));
186-
break;
187-
case 19:
188-
output_bbl_line(ctx->glbl_ctx);
189-
break;
190-
case 20:
191-
unwrap(x_num_names(ctx));
192-
break;
193-
case 21:
194-
unwrap(pop_lit_stk(ctx, &ctx->pop1));
195-
break;
196-
case 22:
197-
unwrap(x_preamble(ctx));
198-
break;
199-
case 23:
200-
unwrap(x_purify(ctx));
201-
break;
202-
case 24:
203-
unwrap(x_quote(ctx));
204-
break;
205-
case 25:
206-
break;
207-
case 26:
208-
unwrap(pop_whole_stack(ctx));
209-
break;
210-
case 27:
211-
unwrap(x_substring(ctx));
212-
break;
213-
case 28:
214-
unwrap(x_swap(ctx));
215-
break;
216-
case 29:
217-
unwrap(x_text_length(ctx));
218-
break;
219-
case 30:
220-
unwrap(x_text_prefix(ctx));
221-
break;
222-
case 31:
223-
unwrap(pop_top_and_print(ctx));
224-
break;
225-
case 32:
226-
unwrap(x_type(ctx));
227-
break;
228-
case 33:
229-
unwrap(x_warning(ctx));
230-
break;
231-
case 34:
232-
{
233-
ExecVal r_pop1, r_pop2;
234-
unwrap(pop_lit_stk(ctx, &r_pop1));
235-
unwrap(pop_lit_stk(ctx, &r_pop2));
236-
if (r_pop1.tag != ExecVal_Function)
237-
unwrap(print_wrong_stk_lit(ctx, r_pop1, STK_TYPE_FUNCTION));
238-
else if (r_pop2.tag != ExecVal_Function)
239-
unwrap(print_wrong_stk_lit(ctx, r_pop2, STK_TYPE_FUNCTION));
240-
else
241-
while (true) {
242-
243-
execute_fn(ctx, r_pop2.function);
244-
unwrap(pop_lit_stk(ctx, &ctx->pop1));
245-
if (ctx->pop1.tag != ExecVal_Integer) {
246-
unwrap(print_wrong_stk_lit(ctx, ctx->pop1, STK_TYPE_INTEGER));
247-
goto lab51;
248-
} else if (ctx->pop1.integer > 0)
249-
execute_fn(ctx, r_pop1.function);
250-
else
251-
goto lab51;
252-
}
253-
lab51: /*end_while */ ;
254-
}
255-
break;
256-
case 35:
257-
unwrap(x_width(ctx));
258-
break;
259-
case 36:
260-
unwrap(x_write(ctx));
261-
break;
262-
default:
263-
puts_log("Unknown built-in function");
264-
print_confusion();
265-
longjmp(error_jmpbuf, 1);
266-
break;
267-
}
268-
// printf_log("Exiting builtin %d\n", ilk_info(ex_fn_loc));
269-
break;
270-
case FN_CLASS_WIZARD:
271-
// printf_log("Executing wizard %d\n", ilk_info(ex_fn_loc));
272-
wiz_ptr = ilk_info(ex_fn_loc);
273-
while (wiz_functions(wiz_ptr) != end_of_def()) {
274-
275-
if (wiz_functions(wiz_ptr) != quote_next_fn)
276-
execute_fn(ctx, wiz_functions(wiz_ptr));
277-
else {
278-
279-
wiz_ptr = wiz_ptr + 1;
280-
push_lit_stk(ctx, func_val(wiz_functions(wiz_ptr)));
281-
}
282-
wiz_ptr = wiz_ptr + 1;
283-
}
284-
// printf_log("Exiting wizard %d\n", ilk_info(ex_fn_loc));
285-
break;
286-
case FN_CLASS_INT_LIT:
287-
push_lit_stk(ctx, int_val(ilk_info(ex_fn_loc)));
288-
break;
289-
case FN_CLASS_STR_LIT:
290-
push_lit_stk(ctx, str_val(hash_text(ex_fn_loc)));
291-
break;
292-
case FN_CLASS_FIELD:
293-
if (!ctx->mess_with_entries)
294-
unwrap(bst_cant_mess_with_entries_print(ctx));
295-
else {
296-
field_loc field_ptr = cite_ptr() * num_fields() + ilk_info(ex_fn_loc);
297-
if (field_ptr >= max_fields()) {
298-
puts_log("field_info index is out of range");
299-
print_confusion();
300-
longjmp(error_jmpbuf, 1);
301-
}
302-
if (field_info(field_ptr) == 0 /*missing */ )
303-
push_lit_stk(ctx, missing_val(hash_text(ex_fn_loc)));
304-
else
305-
push_lit_stk(ctx, str_val(field_info(field_ptr)));
306-
}
307-
break;
308-
case FN_CLASS_INT_ENTRY_VAR:
309-
if (!ctx->mess_with_entries)
310-
unwrap(bst_cant_mess_with_entries_print(ctx));
311-
else
312-
push_lit_stk(ctx, int_val(entry_ints(cite_ptr() * num_ent_ints() + ilk_info(ex_fn_loc))));
313-
break;
314-
case FN_CLASS_STR_ENTRY_VAR:
315-
if (!ctx->mess_with_entries)
316-
unwrap(bst_cant_mess_with_entries_print(ctx));
317-
else {
318-
319-
str_ent_loc str_ent_ptr = cite_ptr() * num_ent_strs() + ilk_info(ex_fn_loc);
320-
bib_set_buf_offset(BUF_TY_EX, 1, 0);
321-
while (entry_strs((str_ent_ptr) * (ENT_STR_SIZE + 1) + (bib_buf_offset(BUF_TY_EX, 1))) != 127 /*end_of_string */ ) {
322-
323-
bib_set_buf(BUF_TY_EX, bib_buf_offset(BUF_TY_EX, 1), entry_strs((str_ent_ptr) * (ENT_STR_SIZE + 1) + (bib_buf_offset(BUF_TY_EX, 1))));
324-
bib_set_buf_offset(BUF_TY_EX, 1, bib_buf_offset(BUF_TY_EX, 1) + 1);
325-
}
326-
bib_set_buf_len(BUF_TY_EX, bib_buf_offset(BUF_TY_EX, 1));
327-
unwrap(add_pool_buf_and_push(ctx));
328-
}
329-
break;
330-
case FN_CLASS_INT_GLBL_VAR:
331-
push_lit_stk(ctx, int_val(ilk_info(ex_fn_loc)));
332-
break;
333-
case FN_CLASS_STR_GLBL_VAR:
334-
{
335-
int32_t str_glb_ptr = ilk_info(ex_fn_loc);
336-
if (glb_bib_str_ptr(str_glb_ptr) > 0) {
337-
push_lit_stk(ctx, str_val(glb_bib_str_ptr(str_glb_ptr)));
338-
} else {
339-
while (bib_pool_ptr() + glb_str_end(str_glb_ptr) > bib_pool_size())
340-
pool_overflow();
341-
int32_t glob_chr_ptr = 0;
342-
while (glob_chr_ptr < glb_str_end(str_glb_ptr)) {
343-
bib_set_str_pool(bib_pool_ptr(), global_strs((str_glb_ptr) * (GLOB_STR_SIZE + 1) + (glob_chr_ptr)));
344-
bib_set_pool_ptr(bib_pool_ptr() + 1);
345-
glob_chr_ptr = glob_chr_ptr + 1;
346-
}
347-
push_lit_stk(ctx, str_val(unwrap_str(bib_make_string())));
348-
}
349-
}
350-
break;
351-
default:
352-
unknwn_function_class_confusion();
353-
longjmp(error_jmpbuf, 1);
354-
break;
355-
}
356-
}
357-
35892
static void aux_bib_data_command(Bibtex* ctx)
35993
{
36094
if (ctx->bib_seen) {
@@ -509,7 +243,7 @@ static void aux_citation_command(Bibtex* ctx)
509243
(bib_buf_offset(BUF_TY_BASE, 2) - bib_buf_offset(BUF_TY_BASE, 1)),
510244
STR_ILK_LC_CITE, true));
511245
cite_number lc_cite_loc = hash.loc;
512-
if (hash.exists) { /*136: */
246+
if (hash.exists) {
513247
hash = unwrap_lookup(str_lookup(BUF_TY_BASE, bib_buf_offset(BUF_TY_BASE, 1),
514248
(bib_buf_offset(BUF_TY_BASE, 2) - bib_buf_offset(BUF_TY_BASE, 1)),
515249
STR_ILK_CITE, false));
@@ -522,7 +256,7 @@ static void aux_citation_command(Bibtex* ctx)
522256
unwrap(aux_err_print());
523257
return;
524258
}
525-
} else { /*137: */
259+
} else {
526260
hash = unwrap_lookup(str_lookup(BUF_TY_BASE, bib_buf_offset(BUF_TY_BASE, 1),
527261
(bib_buf_offset(BUF_TY_BASE, 2) - bib_buf_offset(BUF_TY_BASE, 1)),
528262
STR_ILK_CITE, true));
@@ -1288,7 +1022,7 @@ static void get_bib_command_or_entry_and_process(Bibtex* ctx, hash_loc* cur_macr
12881022
(bib_buf_offset(BUF_TY_BASE, 2) - bib_buf_offset(BUF_TY_BASE, 1)),
12891023
STR_ILK_BIB_COMMAND, false));
12901024
command_num = ilk_info(hash.loc);
1291-
if (hash.exists) { /*240: */
1025+
if (hash.exists) {
12921026
at_bib_command = true;
12931027
switch ((command_num)) {
12941028
case 0:
@@ -1485,7 +1219,7 @@ static void get_bib_command_or_entry_and_process(Bibtex* ctx, hash_loc* cur_macr
14851219
lab26: /*first_time_entry */ ;
14861220
}
14871221
bool store_entry = true;
1488-
if (ctx->all_entries) { /*273: */
1222+
if (ctx->all_entries) {
14891223
cite_number cite_loc = 0;
14901224
if (hash.exists) {
14911225
if (entry_cite_ptr() < all_marker())
@@ -1509,7 +1243,7 @@ static void get_bib_command_or_entry_and_process(Bibtex* ctx, hash_loc* cur_macr
15091243
lab22: /*cite_already_set */ ;
15101244
} else if (!hash.exists)
15111245
store_entry = false;
1512-
if (store_entry) { /*274: */
1246+
if (store_entry) {
15131247
if (type_exists)
15141248
set_type_list(entry_cite_ptr(), entry_type_loc);
15151249
else {
@@ -1740,7 +1474,7 @@ static void bst_read_command(Bibtex* ctx)
17401474
} else {
17411475

17421476
field_loc field_parent_ptr = cite_parent_ptr * num_fields() + crossref_num();
1743-
if (field_info(field_parent_ptr) != 0 /*missing */ ) { /*283: */
1477+
if (field_info(field_parent_ptr) != 0 /*missing */ ) {
17441478
puts_log("Warning--you've nested cross references");
17451479
unwrap(bad_cross_reference_print(cite_list(cite_parent_ptr)));
17461480
puts_log("\", which also refers to something\n");
@@ -1760,7 +1494,7 @@ static void bst_read_command(Bibtex* ctx)
17601494
if (type_list(cite_ptr()) == 0 /*empty */ ) {
17611495
unwrap(print_missing_entry(cite_list(cite_ptr())));
17621496
} else if ((ctx->all_entries) || (cite_ptr() < old_num_cites()) || (cite_info(cite_ptr()) >= ctx->config.min_crossrefs)) {
1763-
if (cite_ptr() > ctx->cite_xptr) { /*286: */
1497+
if (cite_ptr() > ctx->cite_xptr) {
17641498
if ((ctx->cite_xptr + 1) * num_fields() > max_fields()) {
17651499
puts_log("field_info index is out of range");
17661500
print_confusion();
@@ -1795,7 +1529,7 @@ static void bst_read_command(Bibtex* ctx)
17951529
set_cite_ptr(cite_ptr() + 1);
17961530
}
17971531
set_num_cites(ctx->cite_xptr);
1798-
if (ctx->all_entries) { /*287: */
1532+
if (ctx->all_entries) {
17991533
set_cite_ptr(all_marker());
18001534
while (cite_ptr() < old_num_cites()) {
18011535

@@ -2120,7 +1854,6 @@ bibtex_main(Bibtex* glbl_ctx, const char *aux_file_name)
21201854
ttstub_output_close (glbl_ctx->bbl_file);
21211855

21221856
close_up_shop:
2123-
/*456:*/
21241857

21251858
if (glbl_ctx->read_performed && !glbl_ctx->reading_completed) {
21261859
printf_log("Aborted at line %ld of file ", (long) bib_line_num());

0 commit comments

Comments
 (0)