| 1 |
% omfilter.ch: Running OCP programs |
| 2 |
% |
| 3 |
% This file is part of the Omega project, which |
| 4 |
% is based on the web2c distribution of TeX. |
| 5 |
% |
| 6 |
% Copyright (c) 1994--2000 John Plaice and Yannis Haralambous |
| 7 |
% |
| 8 |
% This library is free software; you can redistribute it and/or |
| 9 |
% modify it under the terms of the GNU Library General Public |
| 10 |
% License as published by the Free Software Foundation; either |
| 11 |
% version 2 of the License, or (at your option) any later version. |
| 12 |
% |
| 13 |
% This library is distributed in the hope that it will be useful, |
| 14 |
% but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 15 |
% MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 16 |
% Library General Public License for more details. |
| 17 |
% |
| 18 |
% You should have received a copy of the GNU Library General Public |
| 19 |
% License along with this library; if not, write to the Free Software |
| 20 |
% Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. |
| 21 |
% |
| 22 |
%--------------------------------------- |
| 23 |
@x [1] m.11 l.412 - Omega Filtering |
| 24 |
{string of length |file_name_size|; tells where the string pool appears} |
| 25 |
@.TeXformats@> |
| 26 |
@y |
| 27 |
{string of length |file_name_size|; tells where the string pool appears} |
| 28 |
@.TeXformats@> |
| 29 |
@!active_mem_size=50000; {number of words of |active_info| for active ocps} |
| 30 |
@z |
| 31 |
%--------------------------------------- |
| 32 |
@x [5] - Omega Translation |
| 33 |
procedure overflow(@!s:str_number;@!n:integer); {stop due to finiteness} |
| 34 |
begin normalize_selector; |
| 35 |
print_err("Omega capacity exceeded, sorry ["); |
| 36 |
@.TeX capacity exceeded ...@> |
| 37 |
print(s); print_char("="); print_int(n); print_char("]"); |
| 38 |
help2("If you really absolutely need more capacity,")@/ |
| 39 |
("you can ask a wizard to enlarge me."); |
| 40 |
succumb; |
| 41 |
end; |
| 42 |
@y |
| 43 |
procedure overflow(@!s:str_number;@!n:integer); {stop due to finiteness} |
| 44 |
begin normalize_selector; |
| 45 |
print_err("Omega capacity exceeded, sorry ["); |
| 46 |
@.TeX capacity exceeded ...@> |
| 47 |
print(s); print_char("="); print_int(n); print_char("]"); |
| 48 |
help2("If you really absolutely need more capacity,")@/ |
| 49 |
("you can ask a wizard to enlarge me."); |
| 50 |
succumb; |
| 51 |
end; |
| 52 |
|
| 53 |
procedure overflow_ocp_buf_size; |
| 54 |
begin |
| 55 |
overflow("ocp_buf_size",ocp_buf_size); |
| 56 |
end; |
| 57 |
|
| 58 |
procedure overflow_ocp_stack_size; |
| 59 |
begin |
| 60 |
overflow("ocp_stack_size",ocp_stack_size); |
| 61 |
end; |
| 62 |
@z |
| 63 |
%--------------------------------------- |
| 64 |
@x [22] m.300 l.6425 - Omega Filtering |
| 65 |
@!in_state_record = record |
| 66 |
@!state_field, @!index_field: quarterword; |
| 67 |
@!start_field,@!loc_field, @!limit_field, @!name_field: halfword; |
| 68 |
end; |
| 69 |
@y |
| 70 |
@!in_state_record = record |
| 71 |
@!state_field, @!index_field: quarterword; |
| 72 |
@!start_field,@!loc_field, @!limit_field, @!name_field, |
| 73 |
@!ocp_lstack_field: halfword; {used for omega translation processes} |
| 74 |
@!ocp_no_field: halfword; {used for omega translation processes} |
| 75 |
end; |
| 76 |
@z |
| 77 |
%--------------------------------------- |
| 78 |
@x [22] m.300 l.6444 - Omega Filtering |
| 79 |
@d limit==cur_input.limit_field {end of current line in |buffer|} |
| 80 |
@d name==cur_input.name_field {name of the current file} |
| 81 |
@y |
| 82 |
@d limit==cur_input.limit_field {end of current line in |buffer|} |
| 83 |
@d name==cur_input.name_field {name of the current file} |
| 84 |
@d current_ocp_lstack==cur_input.ocp_lstack_field {name of the current ocp} |
| 85 |
@d current_ocp_no==cur_input.ocp_no_field {name of the current ocp} |
| 86 |
@z |
| 87 |
%--------------------------------------- |
| 88 |
@x [22] m.312 l.6766 - Omega Filtering |
| 89 |
if state<>token_list then |
| 90 |
begin @<Print location of current line@>; |
| 91 |
@<Pseudoprint the line@>; |
| 92 |
@y |
| 93 |
if current_ocp_lstack>0 then |
| 94 |
begin print_nl("OCP stack "); print_scaled(current_ocp_lstack); |
| 95 |
print(" entry "); print_int(current_ocp_no); print(":"); |
| 96 |
@<Pseudoprint the line@>; |
| 97 |
end |
| 98 |
else if state<>token_list then |
| 99 |
begin @<Print location of current line@>; |
| 100 |
@<Pseudoprint the line@>; |
| 101 |
@z |
| 102 |
%--------------------------------------- |
| 103 |
@x [23] m.331 l.7069 - Omega Filtering |
| 104 |
state:=new_line; start:=1; index:=0; line:=0; name:=0; |
| 105 |
@y |
| 106 |
state:=new_line; start:=1; index:=0; line:=0; name:=0; |
| 107 |
current_ocp_lstack:=0; current_ocp_no:=0; |
| 108 |
@z |
| 109 |
%--------------------------------------- |
| 110 |
@x [24] m.343 l.7248 - Omega Filtering |
| 111 |
else begin state:=new_line;@/ |
| 112 |
@<Move to next line of file, |
| 113 |
@y |
| 114 |
else begin |
| 115 |
if current_ocp_lstack>0 then |
| 116 |
begin |
| 117 |
pop_input; goto restart; |
| 118 |
end; |
| 119 |
state:=new_line; |
| 120 |
@<Move to next line of file, |
| 121 |
@z |
| 122 |
%--------------------------------------- |
| 123 |
@x [30] m.582 l.11283 - Omega Filtering |
| 124 |
char_warning(f,c); |
| 125 |
new_character:=null; |
| 126 |
exit:end; |
| 127 |
@y |
| 128 |
char_warning(f,c); |
| 129 |
new_character:=null; |
| 130 |
exit:end; |
| 131 |
|
| 132 |
@ |
| 133 |
@<Types...@>= |
| 134 |
@!active_index=0..active_mem_size; |
| 135 |
|
| 136 |
@ |
| 137 |
@d active_ocp(#)==active_info[#].hh.b0 |
| 138 |
@d active_counter(#)==active_info[#].hh.b1 |
| 139 |
@d active_lstack_no(#)==active_info[#+1].sc |
| 140 |
|
| 141 |
@<Glob...@>= |
| 142 |
@!active_info:array[active_index] of memory_word; |
| 143 |
@!active_min_ptr:active_index; {first unused word of |active_info|} |
| 144 |
@!active_max_ptr:active_index; {last unused word of |active_info|} |
| 145 |
@!active_real:active_index; |
| 146 |
@!holding:array[active_index] of ocp_list_index; |
| 147 |
|
| 148 |
@ |
| 149 |
@<Initialize table...@>= |
| 150 |
active_min_ptr:=0; |
| 151 |
active_real:=0; |
| 152 |
|
| 153 |
@ |
| 154 |
@p |
| 155 |
function is_last_ocp(llstack_no:scaled; counter:integer):integer; |
| 156 |
begin |
| 157 |
active_min_ptr:=equiv(ocp_active_min_ptr_base); |
| 158 |
active_max_ptr:=equiv(ocp_active_max_ptr_base); |
| 159 |
active_real:=active_min_ptr; |
| 160 |
while (active_real < active_max_ptr) and |
| 161 |
(active_lstack_no(active_real) < llstack_no) do begin |
| 162 |
active_real:=active_real+2; |
| 163 |
end; |
| 164 |
while (active_real < active_max_ptr) and |
| 165 |
(active_lstack_no(active_real) = llstack_no) and |
| 166 |
(active_counter(active_real) <= counter) do begin |
| 167 |
active_real:=active_real+2; |
| 168 |
end; |
| 169 |
is_last_ocp := (active_real=active_max_ptr); |
| 170 |
end; |
| 171 |
|
| 172 |
procedure print_active_ocps; |
| 173 |
var i:integer; |
| 174 |
begin |
| 175 |
print_nl("Active ocps: ["); |
| 176 |
i:=active_min_ptr; |
| 177 |
while i<active_max_ptr do begin |
| 178 |
print("("); |
| 179 |
print_scaled(active_lstack_no(i)); |
| 180 |
print(","); |
| 181 |
print_int(active_counter(i)); |
| 182 |
print(","); |
| 183 |
print_esc(ocp_id_text(active_ocp(i))); |
| 184 |
print(")"); |
| 185 |
if i<>(active_max_ptr-2) then print(","); |
| 186 |
i:=i+2; |
| 187 |
end; |
| 188 |
print("]"); |
| 189 |
end; |
| 190 |
|
| 191 |
procedure add_ocp_stack(min_index:integer; min_value:scaled); |
| 192 |
var p:ocp_lstack_index; |
| 193 |
llstack_no:scaled; |
| 194 |
counter:integer; |
| 195 |
begin |
| 196 |
p:=ocp_list_lstack(holding[min_index]); |
| 197 |
llstack_no:=ocp_list_lstack_no(holding[min_index]); |
| 198 |
counter:=0; |
| 199 |
while not (is_null_ocp_lstack(p)) do begin |
| 200 |
active_ocp(active_max_ptr):=ocp_lstack_ocp(p); |
| 201 |
active_counter(active_max_ptr):=counter; |
| 202 |
active_lstack_no(active_max_ptr):=llstack_no; |
| 203 |
p:=ocp_lstack_lnext(p); |
| 204 |
active_max_ptr:=active_max_ptr+2; |
| 205 |
incr(counter); {no overflow problem} |
| 206 |
end; |
| 207 |
end; |
| 208 |
|
| 209 |
procedure active_compile; |
| 210 |
var i:integer; |
| 211 |
min_index:integer; |
| 212 |
min_stack_ocp:scaled; |
| 213 |
old_min:scaled; |
| 214 |
max_active:integer; |
| 215 |
stack_value:scaled; |
| 216 |
begin |
| 217 |
active_min_ptr:=active_max_ptr; |
| 218 |
min_stack_ocp:=ocp_maxint; |
| 219 |
max_active:=equiv(ocp_active_number_base)-1; |
| 220 |
for i:=max_active downto 0 do begin |
| 221 |
holding[i]:=ocp_list_list[equiv(ocp_active_base+i)]; |
| 222 |
stack_value:=ocp_list_lstack_no(holding[i]); |
| 223 |
if stack_value<min_stack_ocp then begin |
| 224 |
min_index:=i; |
| 225 |
min_stack_ocp:=stack_value; |
| 226 |
end; |
| 227 |
end; |
| 228 |
while min_stack_ocp<ocp_maxint do begin |
| 229 |
add_ocp_stack(min_index, min_stack_ocp); |
| 230 |
old_min:=min_stack_ocp; |
| 231 |
min_stack_ocp:=ocp_maxint; |
| 232 |
for i:=max_active downto 0 do begin |
| 233 |
stack_value:=ocp_list_lstack_no(holding[i]); |
| 234 |
while old_min=stack_value do begin |
| 235 |
holding[i]:=ocp_list_lnext(holding[i]); |
| 236 |
stack_value:=ocp_list_lstack_no(holding[i]); |
| 237 |
end; |
| 238 |
if stack_value<min_stack_ocp then begin |
| 239 |
min_index:=i; |
| 240 |
min_stack_ocp:=stack_value; |
| 241 |
end; |
| 242 |
end; |
| 243 |
end; |
| 244 |
end; |
| 245 |
|
| 246 |
@z |
| 247 |
%--------------------------------------- |
| 248 |
@x [46] m.1030 l.19984 - Omega Filtering |
| 249 |
main_loop:@<Append character |cur_chr| and the following characters (if~any) |
| 250 |
to the current hlist in the current font; |goto reswitch| when |
| 251 |
a non-character has been fetched@>; |
| 252 |
@y |
| 253 |
main_loop: |
| 254 |
if is_last_ocp(current_ocp_lstack,current_ocp_no) then |
| 255 |
begin |
| 256 |
@<Append character |cur_chr| and the following characters (if~any) |
| 257 |
to the current hlist in the current font; |goto reswitch| when |
| 258 |
a non-character has been fetched@>; |
| 259 |
end |
| 260 |
else begin |
| 261 |
@<Create a buffer with character |cur_chr| and the following |
| 262 |
characters (if~any) and then apply the current active OCP filter |
| 263 |
to this buffer@>; |
| 264 |
end; |
| 265 |
@z |
| 266 |
%--------------------------------------- |
| 267 |
@x [50] m.1302 l.23862 - Omega Filtering |
| 268 |
@<Dump the font information@>; |
| 269 |
@y |
| 270 |
@<Dump the font information@>; |
| 271 |
@<Dump the active ocp information@>; |
| 272 |
@z |
| 273 |
%--------------------------------------- |
| 274 |
@x [50] m.1303 l.23711 - Omega Filtering |
| 275 |
@<Undump the font information@>; |
| 276 |
@y |
| 277 |
@<Undump the font information@>; |
| 278 |
@<Undump the active ocp information@>; |
| 279 |
@z |
| 280 |
%--------------------------------------- |
| 281 |
@x [50] m.1323 l.24907 - Omega Filtering |
| 282 |
begin undump_font_table(k);@/ |
| 283 |
end |
| 284 |
@y |
| 285 |
begin undump_font_table(k);@/ |
| 286 |
end |
| 287 |
|
| 288 |
@ @<Dump the active ocp information@>= |
| 289 |
dump_int(active_min_ptr); |
| 290 |
dump_int(active_max_ptr); |
| 291 |
for k:=0 to active_max_ptr-1 do dump_wd(active_info[k]); |
| 292 |
print_ln; print_int(active_max_ptr); print(" words of active ocps"); |
| 293 |
|
| 294 |
@ @<Undump the active ocp information@>= |
| 295 |
undump_size(0)(active_mem_size)('active start point')(active_min_ptr); |
| 296 |
undump_size(0)(active_mem_size)('active mem size')(active_max_ptr); |
| 297 |
for k:=0 to active_max_ptr-1 do undump_wd(active_info[k]); |
| 298 |
|
| 299 |
@z |
| 300 |
%--------------------------------------- |
| 301 |
@x [54] m.1379 l.24883 - Omega Filtering |
| 302 |
@ Here we do the main work required for reading and interpreting |
| 303 |
$\Omega$ Compiled Translation Processes. |
| 304 |
@y |
| 305 |
@ Here we do the main work required for reading and interpreting |
| 306 |
$\Omega$ Compiled Translation Processes. |
| 307 |
|
| 308 |
@ |
| 309 |
@<Types...@>= |
| 310 |
|
| 311 |
@ Here are all the instructions in our mini-assembler. |
| 312 |
@d otp_right_output=1 |
| 313 |
@d otp_right_num=2 |
| 314 |
@d otp_right_char=3 |
| 315 |
@d otp_right_lchar=4 |
| 316 |
@d otp_right_some=5 |
| 317 |
|
| 318 |
@d otp_pback_output=6 |
| 319 |
@d otp_pback_num=7 |
| 320 |
@d otp_pback_char=8 |
| 321 |
@d otp_pback_lchar=9 |
| 322 |
@d otp_pback_some=10 |
| 323 |
|
| 324 |
@d otp_add=11 |
| 325 |
@d otp_sub=12 |
| 326 |
@d otp_mult=13 |
| 327 |
@d otp_div=14 |
| 328 |
@d otp_mod=15 |
| 329 |
@d otp_lookup=16 |
| 330 |
@d otp_push_num=17 |
| 331 |
@d otp_push_char=18 |
| 332 |
@d otp_push_lchar=19 |
| 333 |
|
| 334 |
@d otp_state_change=20 |
| 335 |
@d otp_state_push=21 |
| 336 |
@d otp_state_pop=22 |
| 337 |
|
| 338 |
@d otp_left_start=23 |
| 339 |
@d otp_left_return=24 |
| 340 |
@d otp_left_backup=25 |
| 341 |
|
| 342 |
@d otp_goto=26 |
| 343 |
@d otp_goto_ne=27 |
| 344 |
@d otp_goto_eq=28 |
| 345 |
@d otp_goto_lt=29 |
| 346 |
@d otp_goto_le=30 |
| 347 |
@d otp_goto_gt=31 |
| 348 |
@d otp_goto_ge=32 |
| 349 |
@d otp_goto_no_advance=33 |
| 350 |
@d otp_goto_beg=34 |
| 351 |
@d otp_goto_end=35 |
| 352 |
|
| 353 |
@d otp_stop=36 |
| 354 |
|
| 355 |
@ |
| 356 |
@<Glob...@>= |
| 357 |
@!otp_init_input_start:halfword; |
| 358 |
@!otp_init_input_last:halfword; |
| 359 |
@!otp_init_input_end:halfword; |
| 360 |
@!otp_i:halfword; |
| 361 |
@!otp_init_input_buf:array[0..20000] of quarterword; |
| 362 |
|
| 363 |
@!otp_input_start:halfword; |
| 364 |
@!otp_input_last:halfword; |
| 365 |
@!otp_input_end:halfword; |
| 366 |
@!otp_input_buf:array[0..20000] of quarterword; |
| 367 |
|
| 368 |
@!otp_output_end:halfword; |
| 369 |
@!otp_output_buf:array[0..20000] of quarterword; |
| 370 |
|
| 371 |
@!otp_stack_used:halfword; |
| 372 |
@!otp_stack_last:halfword; |
| 373 |
@!otp_stack_new:halfword; |
| 374 |
@!otp_stack_buf:array[0..1000] of quarterword; |
| 375 |
|
| 376 |
@!otp_pc:halfword; |
| 377 |
|
| 378 |
@!otp_calc_ptr:halfword; |
| 379 |
@!otp_calcs:array[0..1000] of halfword; |
| 380 |
@!otp_state_ptr:halfword; |
| 381 |
@!otp_states:array[0..1000] of halfword; |
| 382 |
|
| 383 |
@!otp_input_char:halfword; |
| 384 |
@!otp_calculated_char:halfword; |
| 385 |
@!otp_no_input_chars:halfword; |
| 386 |
|
| 387 |
@!otp_instruction:halfword; |
| 388 |
@!otp_instr:halfword; |
| 389 |
@!otp_arg:halfword; |
| 390 |
@!otp_first_arg:halfword; |
| 391 |
@!otp_second_arg:halfword; |
| 392 |
|
| 393 |
@!otp_input_ocp:halfword; |
| 394 |
@!otp_counter:halfword; |
| 395 |
|
| 396 |
@!otp_finished:boolean; |
| 397 |
@!otp_ext_str:integer; |
| 398 |
@!otp_ext_str_arg:integer; |
| 399 |
@!otp_ext_i:integer; |
| 400 |
|
| 401 |
@ |
| 402 |
@<Initialize table...@>= |
| 403 |
|
| 404 |
@ |
| 405 |
@<Create a buffer with character |cur_chr| and the following |
| 406 |
characters (if~any) and then apply the current active OCP filter |
| 407 |
to this buffer@>= |
| 408 |
begin |
| 409 |
if (cur_cmd=char_given) or (cur_cmd=char_num) then |
| 410 |
@<Skip OCP processing for explicit characters@> |
| 411 |
else begin |
| 412 |
@<Read in the buffer for treatment@>; |
| 413 |
@<Apply the OCP to the input buffer@>; |
| 414 |
@<Place the output buffer back on the stream@>; |
| 415 |
end; |
| 416 |
goto big_switch; |
| 417 |
end; |
| 418 |
|
| 419 |
@ |
| 420 |
@<Skip OCP processing for explicit characters@>= |
| 421 |
begin |
| 422 |
back_input; |
| 423 |
current_ocp_lstack:=active_lstack_no(active_real); |
| 424 |
current_ocp_no:=active_counter(active_real); |
| 425 |
end |
| 426 |
|
| 427 |
@ |
| 428 |
@<Read in the buffer for treatment@>= |
| 429 |
begin |
| 430 |
otp_init_input_end:=0; |
| 431 |
while (cur_cmd=letter) or (cur_cmd=other_char) or |
| 432 |
{| (cur_cmd=char_given) or |} (cur_cmd=spacer) do begin |
| 433 |
incr(otp_init_input_end); |
| 434 |
if otp_init_input_end >ocp_buf_size then |
| 435 |
overflow_ocp_buf_size; |
| 436 |
otp_init_input_buf[otp_init_input_end]:=cur_chr; |
| 437 |
get_x_token; |
| 438 |
{| if (cur_cmd<>letter) and (cur_cmd<>other_char) |} |
| 439 |
{| and (cur_cmd<>char_given) then begin |} |
| 440 |
{| x_token; |} |
| 441 |
if (cur_cmd=char_num) then begin |
| 442 |
scan_char_num; cur_chr:=cur_val; |
| 443 |
end; |
| 444 |
{| end; |} |
| 445 |
end; |
| 446 |
back_input; |
| 447 |
otp_input_end:=otp_init_input_end; |
| 448 |
for otp_i:=0 to otp_init_input_end do begin |
| 449 |
otp_input_buf[otp_i]:=otp_init_input_buf[otp_i]; |
| 450 |
end; |
| 451 |
end |
| 452 |
|
| 453 |
@ |
| 454 |
@<Apply the OCP to the input buffer@>= |
| 455 |
begin |
| 456 |
otp_input_ocp:=active_ocp(active_real); |
| 457 |
if otp_input_ocp=0 then begin |
| 458 |
print_nl("Null ocp being used: all input lost"); |
| 459 |
otp_output_end:=0; |
| 460 |
end |
| 461 |
else if ocp_external(otp_input_ocp)=0 then begin |
| 462 |
otp_input_start:=0; |
| 463 |
otp_input_last:=0; |
| 464 |
otp_stack_used:=0; |
| 465 |
otp_stack_last:=0; |
| 466 |
otp_stack_new:=0; |
| 467 |
otp_output_end:=0; |
| 468 |
otp_pc:=0; |
| 469 |
otp_finished:=false; |
| 470 |
otp_calc_ptr:=0; |
| 471 |
otp_calcs[otp_calc_ptr]:=0; |
| 472 |
otp_state_ptr:=0; |
| 473 |
otp_states[otp_state_ptr]:=0; |
| 474 |
while not otp_finished do |
| 475 |
@<Run the OTP program@>; |
| 476 |
end |
| 477 |
else begin |
| 478 |
k:=0; |
| 479 |
if name_of_file then libc_free (name_of_file); |
| 480 |
otp_ext_str:=ocp_external(otp_input_ocp); |
| 481 |
otp_ext_str_arg:=ocp_external_arg(otp_input_ocp); |
| 482 |
name_of_file := xmalloc (4 + length(otp_ext_str) + length(otp_ext_str_arg)); |
| 483 |
for otp_ext_i:=str_start(otp_ext_str) to |
| 484 |
str_start(otp_ext_str)+length(otp_ext_str)-1 do |
| 485 |
append_to_name(str_pool[otp_ext_i]); |
| 486 |
append_to_name(" "); |
| 487 |
for otp_ext_i:=str_start(otp_ext_str_arg) to |
| 488 |
str_start(otp_ext_str_arg)+length(otp_ext_str_arg)-1 do |
| 489 |
append_to_name(str_pool[otp_ext_i]); |
| 490 |
name_of_file[length(otp_ext_str)+length(otp_ext_str_arg)+2]:=0; |
| 491 |
run_external_ocp(name_of_file); |
| 492 |
end |
| 493 |
end |
| 494 |
|
| 495 |
@ |
| 496 |
@<Place the output buffer back on the stream@>= |
| 497 |
if otp_output_end>0 then |
| 498 |
begin |
| 499 |
{|if (first+otp_output_end)>=ocp_buf_size then| |
| 500 |
|overflow_ocp_buf_size;|} |
| 501 |
if (first+otp_output_end)>=buf_size then |
| 502 |
begin |
| 503 |
overflow("(OTP invaded) buffer size", buf_size); |
| 504 |
{ FIXME do something about it!!! } |
| 505 |
end; |
| 506 |
push_input; { FIXME has no |pop_input|!!! } |
| 507 |
current_ocp_lstack:=active_lstack_no(active_real); |
| 508 |
current_ocp_no:=active_counter(active_real); |
| 509 |
state:=mid_line; |
| 510 |
last:=first; |
| 511 |
start:=first; |
| 512 |
loc:=first; |
| 513 |
for otp_counter:=1 to otp_output_end do begin |
| 514 |
buffer[last]:=otp_output_buf[otp_counter]; |
| 515 |
incr(last); {no overflow problem} |
| 516 |
end; |
| 517 |
{|if last>=max_buf_stack then max_buf_stack:=last+1;|} |
| 518 |
limit:=last-1; |
| 519 |
first:=last; |
| 520 |
end |
| 521 |
|
| 522 |
@ |
| 523 |
@d otp_cur_state==otp_states[otp_state_ptr] |
| 524 |
@d otp_set_instruction==begin |
| 525 |
if otp_pc>=ocp_state_no(otp_input_ocp)(otp_cur_state) then begin |
| 526 |
print_err("bad OCP program -- PC not valid"); |
| 527 |
succumb; |
| 528 |
end; |
| 529 |
otp_instruction:=ocp_state_entry(otp_input_ocp)(otp_cur_state)(otp_pc); |
| 530 |
otp_instr:=otp_instruction div @"1000000; |
| 531 |
otp_arg:=otp_instruction mod @"1000000; |
| 532 |
end |
| 533 |
|
| 534 |
@<Run the OTP program@>= |
| 535 |
begin |
| 536 |
otp_set_instruction; |
| 537 |
case otp_instr of |
| 538 |
@<Run the |otp_right| instructions@>; |
| 539 |
@<Run the |otp_pback| instructions@>; |
| 540 |
@<Run the arithmetic instructions@>; |
| 541 |
@<Run the |otp_state| instructions@>; |
| 542 |
@<Run the |otp_left| instructions@>; |
| 543 |
@<Run the |otp_goto| instructions@>; |
| 544 |
@<Run the |otp_stop| instruction@>; |
| 545 |
othercases begin |
| 546 |
print_err("bad OCP program -- unknown instruction"); |
| 547 |
succumb; |
| 548 |
end |
| 549 |
endcases; |
| 550 |
end |
| 551 |
|
| 552 |
@ |
| 553 |
|
| 554 |
@d otp_check_char(#)== |
| 555 |
if (1>#) or (#>otp_no_input_chars) then begin |
| 556 |
print_err("right hand side of OCP expression is bad"); |
| 557 |
succumb; |
| 558 |
end |
| 559 |
|
| 560 |
@d otp_get_char(#)==begin |
| 561 |
otp_check_char(#); |
| 562 |
if #>otp_stack_last then begin |
| 563 |
otp_calculated_char:=otp_input_buf[otp_input_start+#-otp_stack_last]; |
| 564 |
end |
| 565 |
else begin |
| 566 |
otp_calculated_char:=otp_stack_buf[#]; |
| 567 |
end; |
| 568 |
end |
| 569 |
|
| 570 |
@<Run the |otp_right| instructions@>= |
| 571 |
otp_right_output: begin |
| 572 |
incr(otp_output_end); |
| 573 |
if otp_output_end >ocp_buf_size then |
| 574 |
overflow_ocp_buf_size; |
| 575 |
otp_output_buf[otp_output_end]:=otp_calcs[otp_calc_ptr]; |
| 576 |
decr(otp_calc_ptr); |
| 577 |
incr(otp_pc); |
| 578 |
end; |
| 579 |
otp_right_num: begin |
| 580 |
incr(otp_output_end); |
| 581 |
if otp_output_end >ocp_buf_size then |
| 582 |
overflow_ocp_buf_size; |
| 583 |
otp_output_buf[otp_output_end]:=otp_arg; |
| 584 |
incr(otp_pc); |
| 585 |
end; |
| 586 |
otp_right_char: begin |
| 587 |
otp_get_char(otp_arg); |
| 588 |
incr(otp_output_end); |
| 589 |
if otp_output_end >ocp_buf_size then |
| 590 |
overflow_ocp_buf_size; |
| 591 |
otp_output_buf[otp_output_end]:=otp_calculated_char; |
| 592 |
incr(otp_pc); |
| 593 |
end; |
| 594 |
otp_right_lchar: begin |
| 595 |
otp_get_char(otp_no_input_chars-otp_arg); |
| 596 |
incr(otp_output_end); |
| 597 |
if otp_output_end >ocp_buf_size then |
| 598 |
overflow_ocp_buf_size; |
| 599 |
otp_output_buf[otp_output_end]:=otp_calculated_char; |
| 600 |
incr(otp_pc); |
| 601 |
end; |
| 602 |
otp_right_some: begin |
| 603 |
otp_first_arg:=otp_arg+1; |
| 604 |
incr(otp_pc); |
| 605 |
otp_set_instruction; |
| 606 |
otp_second_arg:=otp_no_input_chars-otp_arg; |
| 607 |
for otp_counter:=otp_first_arg to otp_second_arg do begin |
| 608 |
otp_get_char(otp_counter); |
| 609 |
incr(otp_output_end); |
| 610 |
if otp_output_end >ocp_buf_size then |
| 611 |
overflow_ocp_buf_size; |
| 612 |
otp_output_buf[otp_output_end]:=otp_calculated_char; |
| 613 |
end; |
| 614 |
incr(otp_pc); |
| 615 |
end |
| 616 |
|
| 617 |
@ |
| 618 |
@<Run the |otp_pback| instructions@>= |
| 619 |
otp_pback_output: begin |
| 620 |
incr(otp_stack_new); |
| 621 |
if otp_stack_new >= ocp_stack_size then |
| 622 |
overflow_ocp_stack_size; |
| 623 |
otp_stack_buf[otp_stack_new]:=otp_calcs[otp_calc_ptr]; |
| 624 |
decr(otp_calc_ptr); |
| 625 |
incr(otp_pc); |
| 626 |
end; |
| 627 |
otp_pback_num: begin |
| 628 |
incr(otp_stack_new); |
| 629 |
if otp_stack_new >= ocp_stack_size then |
| 630 |
overflow_ocp_stack_size; |
| 631 |
otp_stack_buf[otp_stack_new]:=otp_arg; |
| 632 |
incr(otp_pc); |
| 633 |
end; |
| 634 |
otp_pback_char: begin |
| 635 |
otp_get_char(otp_arg); |
| 636 |
incr(otp_stack_new); |
| 637 |
if otp_stack_new >= ocp_stack_size then |
| 638 |
overflow_ocp_stack_size; |
| 639 |
otp_stack_buf[otp_stack_new]:=otp_calculated_char; |
| 640 |
incr(otp_pc); |
| 641 |
end; |
| 642 |
otp_pback_lchar: begin |
| 643 |
otp_get_char(otp_no_input_chars-otp_arg); |
| 644 |
incr(otp_stack_new); |
| 645 |
if otp_stack_new >= ocp_stack_size then |
| 646 |
overflow_ocp_stack_size; |
| 647 |
otp_stack_buf[otp_stack_new]:=otp_calculated_char; |
| 648 |
incr(otp_pc); |
| 649 |
end; |
| 650 |
otp_pback_some: begin |
| 651 |
otp_first_arg:=otp_arg+1; |
| 652 |
incr(otp_pc); |
| 653 |
otp_set_instruction; |
| 654 |
otp_second_arg:=otp_no_input_chars-otp_arg; |
| 655 |
for otp_counter:=otp_first_arg to otp_second_arg do begin |
| 656 |
otp_get_char(otp_counter); |
| 657 |
incr(otp_stack_new); |
| 658 |
if otp_stack_new >= ocp_stack_size then |
| 659 |
overflow_ocp_stack_size; |
| 660 |
otp_stack_buf[otp_stack_new]:=otp_calculated_char; |
| 661 |
end; |
| 662 |
incr(otp_pc); |
| 663 |
end |
| 664 |
|
| 665 |
@ |
| 666 |
@<Run the arithmetic instructions@>= |
| 667 |
otp_add: begin |
| 668 |
otp_calcs[otp_calc_ptr-1] := |
| 669 |
otp_calcs[otp_calc_ptr-1] + otp_calcs[otp_calc_ptr]; |
| 670 |
incr(otp_pc); |
| 671 |
decr(otp_calc_ptr); |
| 672 |
end; |
| 673 |
otp_sub: begin |
| 674 |
otp_calcs[otp_calc_ptr-1] := |
| 675 |
otp_calcs[otp_calc_ptr-1] - otp_calcs[otp_calc_ptr]; |
| 676 |
incr(otp_pc); |
| 677 |
decr(otp_calc_ptr); |
| 678 |
end; |
| 679 |
otp_mult: begin |
| 680 |
otp_calcs[otp_calc_ptr-1] := |
| 681 |
otp_calcs[otp_calc_ptr-1] * otp_calcs[otp_calc_ptr]; |
| 682 |
incr(otp_pc); |
| 683 |
decr(otp_calc_ptr); |
| 684 |
end; |
| 685 |
otp_div: begin |
| 686 |
otp_calcs[otp_calc_ptr-1] := |
| 687 |
otp_calcs[otp_calc_ptr-1] div otp_calcs[otp_calc_ptr]; |
| 688 |
incr(otp_pc); |
| 689 |
decr(otp_calc_ptr); |
| 690 |
end; |
| 691 |
otp_mod: begin |
| 692 |
otp_calcs[otp_calc_ptr-1] := |
| 693 |
otp_calcs[otp_calc_ptr-1] mod otp_calcs[otp_calc_ptr]; |
| 694 |
incr(otp_pc); |
| 695 |
decr(otp_calc_ptr); |
| 696 |
end; |
| 697 |
otp_lookup: begin |
| 698 |
if otp_calcs[otp_calc_ptr]>= |
| 699 |
ocp_table_no(otp_input_ocp)(otp_calcs[otp_calc_ptr-1]) then begin |
| 700 |
print_err("bad OCP program -- table index not valid"); |
| 701 |
succumb; |
| 702 |
end; |
| 703 |
otp_calcs[otp_calc_ptr-1]:= |
| 704 |
ocp_table_entry(otp_input_ocp)(otp_calcs[otp_calc_ptr-1]) |
| 705 |
(otp_calcs[otp_calc_ptr]); |
| 706 |
incr(otp_pc); |
| 707 |
decr(otp_calc_ptr); |
| 708 |
end; |
| 709 |
otp_push_num: begin |
| 710 |
incr(otp_calc_ptr); |
| 711 |
if otp_calc_ptr >= ocp_stack_size then |
| 712 |
overflow_ocp_stack_size; |
| 713 |
otp_calcs[otp_calc_ptr]:=otp_arg; |
| 714 |
incr(otp_pc); |
| 715 |
end; |
| 716 |
otp_push_char: begin |
| 717 |
otp_get_char(otp_arg); |
| 718 |
incr(otp_calc_ptr); |
| 719 |
if otp_calc_ptr >= ocp_stack_size then |
| 720 |
overflow_ocp_stack_size; |
| 721 |
otp_calcs[otp_calc_ptr]:=otp_calculated_char; |
| 722 |
incr(otp_pc); |
| 723 |
end; |
| 724 |
otp_push_lchar: begin |
| 725 |
otp_get_char(otp_no_input_chars-otp_arg); |
| 726 |
incr(otp_calc_ptr); |
| 727 |
if otp_calc_ptr >= ocp_stack_size then |
| 728 |
overflow_ocp_stack_size; |
| 729 |
otp_calcs[otp_calc_ptr]:=otp_calculated_char; |
| 730 |
incr(otp_pc); |
| 731 |
end |
| 732 |
|
| 733 |
@ |
| 734 |
@<Run the |otp_state| instructions@>= |
| 735 |
otp_state_change: begin |
| 736 |
otp_input_start:=otp_input_last; |
| 737 |
for otp_counter:=1 to (otp_stack_new-otp_stack_used) do begin |
| 738 |
otp_stack_buf[otp_counter] := |
| 739 |
otp_stack_buf[otp_counter+otp_stack_used]; |
| 740 |
end; |
| 741 |
otp_stack_new:=otp_stack_new-otp_stack_used; |
| 742 |
otp_stack_last:=otp_stack_new; |
| 743 |
otp_stack_used:=0; |
| 744 |
otp_states[otp_state_ptr]:=otp_arg; |
| 745 |
otp_pc:=0; |
| 746 |
end; |
| 747 |
otp_state_push: begin |
| 748 |
otp_input_start:=otp_input_last; |
| 749 |
for otp_counter:=1 to (otp_stack_new-otp_stack_used) do begin |
| 750 |
otp_stack_buf[otp_counter] := |
| 751 |
otp_stack_buf[otp_counter+otp_stack_used]; |
| 752 |
end; |
| 753 |
otp_stack_new:=otp_stack_new-otp_stack_used; |
| 754 |
otp_stack_last:=otp_stack_new; |
| 755 |
otp_stack_used:=0; |
| 756 |
incr(otp_state_ptr); |
| 757 |
if otp_state_ptr >= ocp_stack_size then |
| 758 |
overflow_ocp_stack_size; |
| 759 |
otp_states[otp_state_ptr]:=otp_arg; |
| 760 |
otp_pc:=0; |
| 761 |
end; |
| 762 |
otp_state_pop: begin |
| 763 |
otp_input_start:=otp_input_last; |
| 764 |
for otp_counter:=1 to (otp_stack_new-otp_stack_used) do begin |
| 765 |
otp_stack_buf[otp_counter] := |
| 766 |
otp_stack_buf[otp_counter+otp_stack_used]; |
| 767 |
end; |
| 768 |
otp_stack_new:=otp_stack_new-otp_stack_used; |
| 769 |
otp_stack_last:=otp_stack_new; |
| 770 |
otp_stack_used:=0; |
| 771 |
if otp_state_ptr>0 then decr(otp_state_ptr); |
| 772 |
otp_pc:=0; |
| 773 |
end |
| 774 |
|
| 775 |
@ |
| 776 |
@<Run the |otp_left| instructions@>= |
| 777 |
otp_left_start: begin |
| 778 |
otp_input_start:=otp_input_last; |
| 779 |
otp_input_last:=otp_input_start; |
| 780 |
otp_stack_used:=0; |
| 781 |
if (otp_stack_last=0) and (otp_input_last>=otp_input_end) then |
| 782 |
otp_finished:=true |
| 783 |
else if (otp_stack_used < otp_stack_last) then begin |
| 784 |
incr(otp_stack_used); {no overflow problem} |
| 785 |
otp_input_char:=otp_stack_buf[otp_stack_used]; |
| 786 |
otp_no_input_chars:=1; |
| 787 |
incr(otp_pc); |
| 788 |
end |
| 789 |
else begin |
| 790 |
incr(otp_input_last); {no overflow problem} |
| 791 |
otp_input_char:=otp_input_buf[otp_input_last]; |
| 792 |
otp_no_input_chars:=1; |
| 793 |
incr(otp_pc); |
| 794 |
end; |
| 795 |
end; |
| 796 |
otp_left_return: begin |
| 797 |
otp_input_last:=otp_input_start; |
| 798 |
otp_stack_used:=0; |
| 799 |
if (otp_stack_used < otp_stack_last) then begin |
| 800 |
incr(otp_stack_used); {no overflow problem} |
| 801 |
otp_input_char:=otp_stack_buf[otp_stack_used]; |
| 802 |
otp_no_input_chars:=1; |
| 803 |
incr(otp_pc); |
| 804 |
end |
| 805 |
else begin |
| 806 |
incr(otp_input_last); {no overflow problem} |
| 807 |
otp_input_char:=otp_input_buf[otp_input_last]; |
| 808 |
otp_no_input_chars:=1; |
| 809 |
incr(otp_pc); |
| 810 |
end; |
| 811 |
end; |
| 812 |
otp_left_backup: begin |
| 813 |
if otp_input_start < otp_input_last then begin |
| 814 |
decr(otp_input_last); otp_input_char:=otp_input_buf[otp_input_last]; |
| 815 |
end |
| 816 |
else begin |
| 817 |
decr(otp_stack_used); otp_input_char:=otp_stack_buf[otp_stack_used]; |
| 818 |
end; |
| 819 |
decr(otp_no_input_chars); |
| 820 |
incr(otp_pc); |
| 821 |
end |
| 822 |
|
| 823 |
@ |
| 824 |
@<Run the |otp_goto| instructions@>= |
| 825 |
otp_goto: begin |
| 826 |
otp_pc:=otp_arg; |
| 827 |
end; |
| 828 |
otp_goto_ne: begin |
| 829 |
otp_first_arg:=otp_arg; |
| 830 |
incr(otp_pc); |
| 831 |
otp_set_instruction; |
| 832 |
if otp_input_char <> otp_first_arg then begin |
| 833 |
otp_pc:=otp_arg; |
| 834 |
end |
| 835 |
else begin |
| 836 |
incr(otp_pc); |
| 837 |
end; |
| 838 |
end; |
| 839 |
otp_goto_eq: begin |
| 840 |
otp_first_arg:=otp_arg; |
| 841 |
incr(otp_pc); |
| 842 |
otp_set_instruction; |
| 843 |
if otp_input_char = otp_first_arg then begin |
| 844 |
otp_pc:=otp_arg; |
| 845 |
end |
| 846 |
else begin |
| 847 |
incr(otp_pc); |
| 848 |
end; |
| 849 |
end; |
| 850 |
otp_goto_lt: begin |
| 851 |
otp_first_arg:=otp_arg; |
| 852 |
incr(otp_pc); |
| 853 |
otp_set_instruction; |
| 854 |
if otp_input_char < otp_first_arg then begin |
| 855 |
otp_pc:=otp_arg; |
| 856 |
end |
| 857 |
else begin |
| 858 |
incr(otp_pc); |
| 859 |
end; |
| 860 |
end; |
| 861 |
otp_goto_le: begin |
| 862 |
otp_first_arg:=otp_arg; |
| 863 |
incr(otp_pc); |
| 864 |
otp_set_instruction; |
| 865 |
if otp_input_char <= otp_first_arg then begin |
| 866 |
otp_pc:=otp_arg; |
| 867 |
end |
| 868 |
else begin |
| 869 |
incr(otp_pc); |
| 870 |
end; |
| 871 |
end; |
| 872 |
otp_goto_gt: begin |
| 873 |
otp_first_arg:=otp_arg; |
| 874 |
incr(otp_pc); |
| 875 |
otp_set_instruction; |
| 876 |
if otp_input_char > otp_first_arg then begin |
| 877 |
otp_pc:=otp_arg; |
| 878 |
end |
| 879 |
else begin |
| 880 |
incr(otp_pc); |
| 881 |
end; |
| 882 |
end; |
| 883 |
otp_goto_ge: begin |
| 884 |
otp_first_arg:=otp_arg; |
| 885 |
incr(otp_pc); |
| 886 |
otp_set_instruction; |
| 887 |
if otp_input_char >= otp_first_arg then begin |
| 888 |
otp_pc:=otp_arg; |
| 889 |
end |
| 890 |
else begin |
| 891 |
incr(otp_pc); |
| 892 |
end; |
| 893 |
end; |
| 894 |
otp_goto_no_advance: begin |
| 895 |
if (otp_stack_used < otp_stack_last) then begin |
| 896 |
incr(otp_stack_used); {no overflow problem} |
| 897 |
otp_input_char:=otp_stack_buf[otp_stack_used]; |
| 898 |
incr(otp_no_input_chars); {no overflow problem} |
| 899 |
incr(otp_pc); |
| 900 |
end |
| 901 |
else if otp_input_last>=otp_input_end then begin |
| 902 |
otp_pc:=otp_arg; |
| 903 |
end |
| 904 |
else begin |
| 905 |
incr(otp_input_last); {no overflow problem} |
| 906 |
otp_input_char:=otp_input_buf[otp_input_last]; |
| 907 |
incr(otp_no_input_chars); {no overflow problem} |
| 908 |
incr(otp_pc); |
| 909 |
end; |
| 910 |
end; |
| 911 |
otp_goto_beg: begin |
| 912 |
if otp_input_last=0 then begin |
| 913 |
otp_pc:=otp_arg; |
| 914 |
end |
| 915 |
else begin |
| 916 |
incr(otp_pc); |
| 917 |
end; |
| 918 |
end; |
| 919 |
otp_goto_end: begin |
| 920 |
if otp_input_last>=otp_input_end then begin |
| 921 |
otp_pc:=otp_arg; |
| 922 |
end |
| 923 |
else begin |
| 924 |
incr(otp_pc); |
| 925 |
end; |
| 926 |
end |
| 927 |
|
| 928 |
@ |
| 929 |
@<Run the |otp_stop| instruction@>= |
| 930 |
otp_stop: begin |
| 931 |
otp_input_start:=otp_input_last; |
| 932 |
for otp_counter:=1 to (otp_stack_new-otp_stack_used) do begin |
| 933 |
otp_stack_buf[otp_counter] := |
| 934 |
otp_stack_buf[otp_counter+otp_stack_used]; |
| 935 |
end; |
| 936 |
otp_stack_new:=otp_stack_new-otp_stack_used; |
| 937 |
otp_stack_last:=otp_stack_new; |
| 938 |
otp_stack_used:=0; |
| 939 |
otp_pc:=0; |
| 940 |
end |
| 941 |
|
| 942 |
@z |