Skip to content

Commit 5e65bbc

Browse files
committed
Port x_width/x_write, push with_hash up stack, next up is just execute_fn
1 parent 5d3c472 commit 5e65bbc

6 files changed

Lines changed: 279 additions & 248 deletions

File tree

crates/engine_bibtex/bibtex/bibtex.c

Lines changed: 3 additions & 106 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,6 @@ static jmp_buf error_jmpbuf, recover_jmpbuf;
2424

2525
/*22: */
2626

27-
typedef size_t pool_pointer;
2827
typedef size_t hash_loc;
2928
typedef size_t cite_number;
3029
typedef size_t wiz_fn_loc;
@@ -102,108 +101,6 @@ printf_log(const char *fmt, ...)
102101

103102
/*:159*//*160: */
104103

105-
static void x_width(ExecCtx* ctx)
106-
{
107-
unwrap(pop_lit_stk(ctx, &ctx->pop1));
108-
if (ctx->pop1.tag != ExecVal_String) {
109-
unwrap(print_wrong_stk_lit(ctx, ctx->pop1, STK_TYPE_STRING));
110-
push_lit_stk(ctx, int_val(0));
111-
} else {
112-
113-
bib_set_buf_len(BUF_TY_EX, 0);
114-
add_buf_pool(ctx->pop1.string);
115-
int32_t string_width = 0;
116-
{
117-
int32_t brace_level = 0;
118-
bib_set_buf_offset(BUF_TY_EX, 1, 0);
119-
while (bib_buf_offset(BUF_TY_EX, 1) < bib_buf_len(BUF_TY_EX)) {
120-
121-
if (bib_buf(BUF_TY_EX, bib_buf_offset(BUF_TY_EX, 1)) == 123 /*left_brace */ ) {
122-
brace_level = brace_level + 1;
123-
if ((brace_level == 1) && (bib_buf_offset(BUF_TY_EX, 1) + 1 < bib_buf_len(BUF_TY_EX))) {
124-
125-
if (bib_buf(BUF_TY_EX, bib_buf_offset(BUF_TY_EX, 1) + 1) == 92 /*backslash */ ) { /*453: */
126-
bib_set_buf_offset(BUF_TY_EX, 1, bib_buf_offset(BUF_TY_EX, 1) + 1);
127-
while ((bib_buf_offset(BUF_TY_EX, 1) < bib_buf_len(BUF_TY_EX)) && (brace_level > 0)) {
128-
129-
bib_set_buf_offset(BUF_TY_EX, 1, bib_buf_offset(BUF_TY_EX, 1) + 1);
130-
BufPointer ex_buf_xptr = bib_buf_offset(BUF_TY_EX, 1);
131-
while (((bib_buf_offset(BUF_TY_EX, 1) < bib_buf_len(BUF_TY_EX))
132-
&& (LEX_CLASS[bib_buf(BUF_TY_EX, bib_buf_offset(BUF_TY_EX, 1))] == LEX_CLASS_ALPHA )))
133-
bib_set_buf_offset(BUF_TY_EX, 1, bib_buf_offset(BUF_TY_EX, 1) + 1);
134-
if ((bib_buf_offset(BUF_TY_EX, 1) < bib_buf_len(BUF_TY_EX)) && (bib_buf_offset(BUF_TY_EX, 1) == ex_buf_xptr))
135-
bib_set_buf_offset(BUF_TY_EX, 1, bib_buf_offset(BUF_TY_EX, 1) + 1);
136-
else {
137-
LookupRes hash = unwrap_lookup(str_lookup(BUF_TY_EX, ex_buf_xptr,
138-
bib_buf_offset(BUF_TY_EX, 1) -
139-
ex_buf_xptr, STR_ILK_CONTROL_SEQ ,
140-
false));
141-
hash_loc control_seq_loc = hash.loc;
142-
if (hash.exists) { /*454: */
143-
switch ((ilk_info(control_seq_loc))) {
144-
case 12:
145-
string_width = string_width + 500;
146-
break;
147-
case 4:
148-
string_width = string_width + 722;
149-
break;
150-
case 2:
151-
string_width = string_width + 778;
152-
break;
153-
case 5:
154-
string_width = string_width + 903;
155-
break;
156-
case 3:
157-
string_width = string_width + 1014;
158-
break;
159-
default:
160-
string_width = string_width + CHAR_WIDTH[bib_buf(BUF_TY_EX, ex_buf_xptr)];
161-
break;
162-
}
163-
}
164-
}
165-
while (((bib_buf_offset(BUF_TY_EX, 1) < bib_buf_len(BUF_TY_EX))
166-
&& (LEX_CLASS[bib_buf(BUF_TY_EX, bib_buf_offset(BUF_TY_EX, 1))] == LEX_CLASS_WHITESPACE )))
167-
bib_set_buf_offset(BUF_TY_EX, 1, bib_buf_offset(BUF_TY_EX, 1) + 1);
168-
while (((bib_buf_offset(BUF_TY_EX, 1) < bib_buf_len(BUF_TY_EX)) && (brace_level > 0)
169-
&& (bib_buf(BUF_TY_EX, bib_buf_offset(BUF_TY_EX, 1)) != 92 /*backslash */ ))) {
170-
171-
if (bib_buf(BUF_TY_EX, bib_buf_offset(BUF_TY_EX, 1)) == 125 /*right_brace */ )
172-
brace_level = brace_level - 1;
173-
else if (bib_buf(BUF_TY_EX, bib_buf_offset(BUF_TY_EX, 1)) == 123 /*left_brace */ )
174-
brace_level = brace_level + 1;
175-
else
176-
string_width = string_width + CHAR_WIDTH[bib_buf(BUF_TY_EX, bib_buf_offset(BUF_TY_EX, 1))];
177-
bib_set_buf_offset(BUF_TY_EX, 1, bib_buf_offset(BUF_TY_EX, 1) + 1);
178-
}
179-
}
180-
bib_set_buf_offset(BUF_TY_EX, 1, bib_buf_offset(BUF_TY_EX, 1) - 1);
181-
} else
182-
string_width = string_width + CHAR_WIDTH[123 /*left_brace */ ];
183-
} else
184-
string_width = string_width + CHAR_WIDTH[123 /*left_brace */ ];
185-
} else if (bib_buf(BUF_TY_EX, bib_buf_offset(BUF_TY_EX, 1)) == 125 /*right_brace */ ) {
186-
unwrap(decr_brace_level(ctx, ctx->pop1.string, &brace_level));
187-
string_width = string_width + CHAR_WIDTH[125 /*right_brace */ ];
188-
} else
189-
string_width = string_width + CHAR_WIDTH[bib_buf(BUF_TY_EX, bib_buf_offset(BUF_TY_EX, 1))];
190-
bib_set_buf_offset(BUF_TY_EX, 1, bib_buf_offset(BUF_TY_EX, 1) + 1);
191-
}
192-
unwrap(check_brace_level(ctx, ctx->pop1.string, brace_level));
193-
}
194-
push_lit_stk(ctx, int_val(string_width));
195-
}
196-
}
197-
198-
static void x_write(ExecCtx* ctx)
199-
{
200-
unwrap(pop_lit_stk(ctx, &ctx->pop1));
201-
if (ctx->pop1.tag != ExecVal_String)
202-
unwrap(print_wrong_stk_lit(ctx, ctx->pop1, STK_TYPE_STRING));
203-
else
204-
add_out_pool(ctx->glbl_ctx, ctx->pop1.string);
205-
}
206-
207104
static void execute_fn(ExecCtx* ctx, hash_loc ex_fn_loc)
208105
{
209106
wiz_fn_loc wiz_ptr;
@@ -329,7 +226,7 @@ static void execute_fn(ExecCtx* ctx, hash_loc ex_fn_loc)
329226
unwrap(x_type(ctx));
330227
break;
331228
case 33:
332-
x_warning(ctx);
229+
unwrap(x_warning(ctx));
333230
break;
334231
case 34:
335232
{
@@ -357,10 +254,10 @@ static void execute_fn(ExecCtx* ctx, hash_loc ex_fn_loc)
357254
}
358255
break;
359256
case 35:
360-
x_width(ctx);
257+
unwrap(x_width(ctx));
361258
break;
362259
case 36:
363-
x_write(ctx);
260+
unwrap(x_write(ctx));
364261
break;
365262
default:
366263
puts_log("Unknown built-in function");

crates/engine_bibtex/bibtex/bibtex_bindings.h

Lines changed: 4 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -284,8 +284,6 @@ extern "C" {
284284

285285
extern const LexClass LEX_CLASS[256];
286286

287-
extern const int32_t CHAR_WIDTH[256];
288-
289287
void reset_all(void);
290288

291289
bool bib_str_eq_buf(StrNumber s, BufTy buf, BufPointer ptr, BufPointer len);
@@ -494,6 +492,10 @@ CResult x_type(ExecCtx *ctx);
494492

495493
CResult x_warning(ExecCtx *ctx);
496494

495+
CResult x_width(ExecCtx *ctx);
496+
497+
CResult x_write(ExecCtx *ctx);
498+
497499
int32_t num_glb_strs(void);
498500

499501
void set_num_glb_strs(int32_t val);
@@ -668,14 +670,10 @@ void bib_set_pool_ptr(PoolPointer ptr);
668670

669671
CResultStr bib_make_string(void);
670672

671-
void add_buf_pool(StrNumber str);
672-
673673
CResultLookup str_lookup(BufTy buf, BufPointer ptr, BufPointer len, StrIlk ilk, bool insert);
674674

675675
CResult pre_def_certain_strings(Bibtex *ctx);
676676

677-
void add_out_pool(Bibtex *ctx, StrNumber str);
678-
679677
bool scan1(ASCIICode char1);
680678

681679
bool scan1_white(ASCIICode char1);
@@ -699,10 +697,6 @@ CResultBool scan_and_store_the_field_value_and_eat_white(Bibtex *ctx,
699697
ASCIICode right_outer_delim,
700698
HashPointer field_name_loc);
701699

702-
CResult decr_brace_level(const ExecCtx *ctx, StrNumber pop_lit_var, int32_t *brace_level);
703-
704-
CResult check_brace_level(const ExecCtx *ctx, StrNumber pop_lit_var, int32_t brace_level);
705-
706700
#ifdef __cplusplus
707701
} // extern "C"
708702
#endif // __cplusplus

crates/engine_bibtex/src/c_api/char_info.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,6 @@ pub const ID_CLASS: [IdClass; 256] = {
101101
id_class
102102
};
103103

104-
#[no_mangle]
105104
pub static CHAR_WIDTH: [i32; 256] = {
106105
let mut char_width = [0; 256];
107106

0 commit comments

Comments
 (0)