1 /* web2c -- convert the pseudo-Pascal generated by Tangle to C.
2 The output depends on many C macros and some postprocessing by other
6 -f: force strict interpretation of semantics of for stmt
7 (never used with TeX and friends)
8 -t: special optimizations for tex.p->tex*.c
9 -m: special optimizations for mf.p->mf*.c
10 -c: supply the base part of the name of the coerce.h file
11 -h: supply the name of the standard header file
12 -d: generate some additional debugging output
14 The majority of this program (which includes ptoc.yacc and ptoc.lex)
15 was written by Tomas Rokicki, with modifications by Tim Morgan, et al. */
18 #include "web2c-parser.h"
21 /* Changing this value will probably stimulate bugs in some
22 preprocessors -- those which want to put the expansion of a macro
23 entirely on one line. */
24 #define max_line_length 78
26 #define max_strings 50000
27 #define hash_prime 7883
28 #define sym_table_size 50000
31 /* Says whether to give voluminous progress reports. */
32 boolean debug
= false;
38 int tex
= 0, strict_for
= 0, mf
= 0;
45 char next_temp
[] = "zzzaa";
46 char coerce_name
[100] = "coerce.h";
51 long lower_bound
, upper_bound
;
56 const char *std_header
= "null.h"; /* Default include filename */
58 char strings
[max_strings
];
59 int hash_list
[hash_prime
];
61 struct sym_entry sym_table
[sym_table_size
];
62 int next_sym_free
= -1, next_string_free
= 0;
63 int mark_sym_free
, mark_string_free
;
69 if (next_temp
[4] > 'z')
95 /* Output the string S to the file `out'. */
98 my_output (const_string s
)
100 int len
= strlen (s
);
106 if (line_pos
+ len
> max_line_length
)
109 if (indent
> 1 && (strcmp (s
, "case") == 0 || strcmp (s
, "default") == 0))
112 while (line_pos
< indent
* 2 - less_indent
) {
117 /* Output the token. */
120 /* Omitting the space for parentheses makes fixwrites lose. Sigh.
122 if (!(len
== 1 && (*s
== ';' || *s
== '[' || *s
== ']')))
126 last_brace
= (s
[0] == '}');
140 hash (const_string id
)
142 register int i
= 0, j
;
143 for (j
= 0; id
[j
] != 0; j
++)
144 i
= (i
+ i
+ id
[j
]) % hash_prime
;
149 search_table (const_string id
)
152 ptr
= hash_list
[hash (id
)];
155 if (strcmp (id
, sym_table
[ptr
].id
) == 0)
158 ptr
= sym_table
[ptr
].next
;
164 /* Add ID to the symbol table. Leave it up to the caller to assign to
165 the `typ' field. Return the index into the `sym_table' array. */
167 add_to_table (string id
)
172 hash_list
[h
] = ++next_sym_free
;
173 sym_table
[next_sym_free
].next
= ptr
;
174 sym_table
[next_sym_free
].val
= unused
;
175 sym_table
[next_sym_free
].id
= strings
+ next_string_free
;
176 sym_table
[next_sym_free
].var_formal
= false;
177 sym_table
[next_sym_free
].var_not_needed
= false;
178 strcpy (strings
+ next_string_free
, id
);
179 next_string_free
+= strlen (id
) + 1;
180 return next_sym_free
;
187 for (h
= 0; h
< hash_prime
; h
++)
189 next_sym_free
= mark_sym_free
;
190 next_string_free
= mark_string_free
;
192 while (ptr
> next_sym_free
)
193 ptr
= sym_table
[ptr
].next
;
202 mark_sym_free
= next_sym_free
;
203 mark_string_free
= next_string_free
;
213 for (i
= 0; i
< hash_prime
; hash_list
[i
++] = -1)
218 coerce
= xfopen (coerce_name
, FOPEN_W_MODE
);
227 main (int argc
, string
*argv
)
232 setmode(fileno(stdout
), _O_BINARY
);
234 for (i
= 1; i
< argc
; i
++)
235 if (argv
[i
][0] == '-')
248 std_header
= &argv
[i
][2];
254 program_name
= &argv
[i
][2];
255 sprintf (coerce_name
, "%s.h", program_name
);
258 fprintf (stderr
, "web2c: Unknown option %s, ignored\n", argv
[i
]);
263 fprintf (stderr
, "web2c: Unknown argument %s, ignored\n", argv
[i
]);
270 xfclose (coerce
, coerce_name
);
274 fprintf (stderr
, "%d symbols.\n", next_sym_free
);
275 fprintf (stderr
, "%d strings.\n", next_string_free
);