You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324
  1. typedef unsigned char byte;
  2. typedef unsigned short symbol;
  3. #define true 1
  4. #define false 0
  5. #define repeat while(true)
  6. #define unless(C) if(!(C))
  7. #define until(C) while(!(C))
  8. #define MALLOC check_malloc
  9. #define FREE check_free
  10. #define NEW(type, p) struct type * p = (struct type *) MALLOC(sizeof(struct type))
  11. #define NEWVEC(type, p, n) struct type * p = (struct type *) MALLOC(sizeof(struct type) * n)
  12. #define STARTSIZE 10
  13. #define SIZE(p) ((int *)(p))[-1]
  14. #define CAPACITY(p) ((int *)(p))[-2]
  15. extern symbol * create_b(int n);
  16. extern void report_b(FILE * out, symbol * p);
  17. extern void lose_b(symbol * p);
  18. extern symbol * increase_capacity(symbol * p, int n);
  19. extern symbol * move_to_b(symbol * p, int n, symbol * q);
  20. extern symbol * add_to_b(symbol * p, int n, symbol * q);
  21. extern symbol * copy_b(symbol * p);
  22. extern char * b_to_s(symbol * p);
  23. extern symbol * add_s_to_b(symbol * p, const char * s);
  24. struct str; /* defined in space.c */
  25. extern struct str * str_new(void);
  26. extern void str_delete(struct str * str);
  27. extern void str_append(struct str * str, struct str * add);
  28. extern void str_append_ch(struct str * str, char add);
  29. extern void str_append_b(struct str * str, symbol * q);
  30. extern void str_append_string(struct str * str, const char * s);
  31. extern void str_append_int(struct str * str, int i);
  32. extern void str_clear(struct str * str);
  33. extern void str_assign(struct str * str, char * s);
  34. extern struct str * str_copy(struct str * old);
  35. extern symbol * str_data(struct str * str);
  36. extern int str_len(struct str * str);
  37. extern int get_utf8(const symbol * p, int * slot);
  38. extern int put_utf8(int ch, symbol * p);
  39. struct m_pair {
  40. struct m_pair * next;
  41. symbol * name;
  42. symbol * value;
  43. };
  44. /* struct input must be a prefix of struct tokeniser. */
  45. struct input {
  46. struct input * next;
  47. symbol * p;
  48. int c;
  49. char * file;
  50. int line_number;
  51. };
  52. struct include {
  53. struct include * next;
  54. symbol * b;
  55. };
  56. /* struct input must be a prefix of struct tokeniser. */
  57. struct tokeniser {
  58. struct input * next;
  59. symbol * p;
  60. int c;
  61. char * file;
  62. int line_number;
  63. symbol * b;
  64. symbol * b2;
  65. int number;
  66. int m_start;
  67. int m_end;
  68. struct m_pair * m_pairs;
  69. int get_depth;
  70. int error_count;
  71. int token;
  72. int previous_token;
  73. byte token_held;
  74. byte widechars;
  75. byte utf8;
  76. int omission;
  77. struct include * includes;
  78. };
  79. extern symbol * get_input(symbol * p, char ** p_file);
  80. extern struct tokeniser * create_tokeniser(symbol * b, char * file);
  81. extern int read_token(struct tokeniser * t);
  82. extern const char * name_of_token(int code);
  83. extern void close_tokeniser(struct tokeniser * t);
  84. enum token_codes {
  85. #include "syswords2.h"
  86. c_mathassign,
  87. c_name,
  88. c_number,
  89. c_literalstring,
  90. c_neg,
  91. c_call,
  92. c_grouping,
  93. c_booltest
  94. };
  95. extern int space_count;
  96. extern void * check_malloc(int n);
  97. extern void check_free(void * p);
  98. struct node;
  99. struct name {
  100. struct name * next;
  101. symbol * b;
  102. int type; /* t_string etc */
  103. int mode; /* )_ for routines, externals */
  104. struct node * definition; /* ) */
  105. int count; /* 0, 1, 2 for each type */
  106. struct grouping * grouping; /* for grouping names */
  107. byte referenced;
  108. byte used;
  109. };
  110. struct literalstring {
  111. struct literalstring * next;
  112. symbol * b;
  113. };
  114. struct amongvec {
  115. symbol * b; /* the string giving the case */
  116. int size; /* - and its size */
  117. struct node * p; /* the corresponding command */
  118. int i; /* the amongvec index of the longest substring of b */
  119. int result; /* the numeric result for the case */
  120. struct name * function;
  121. };
  122. struct among {
  123. struct among * next;
  124. struct amongvec * b; /* pointer to the amongvec */
  125. int number; /* amongs are numbered 0, 1, 2 ... */
  126. int literalstring_count; /* in this among */
  127. int command_count; /* in this among */
  128. struct node * starter; /* i.e. among( (starter) 'string' ... ) */
  129. struct node * substring; /* i.e. substring ... among ( ... ) */
  130. };
  131. struct grouping {
  132. struct grouping * next;
  133. int number; /* groupings are numbered 0, 1, 2 ... */
  134. symbol * b; /* the characters of this group */
  135. int largest_ch; /* character with max code */
  136. int smallest_ch; /* character with min code */
  137. byte no_gaps; /* not used in generator.c after 11/5/05 */
  138. struct name * name; /* so g->name->grouping == g */
  139. };
  140. struct node {
  141. struct node * next;
  142. struct node * left;
  143. struct node * aux; /* used in setlimit */
  144. struct among * among; /* used in among */
  145. struct node * right;
  146. int type;
  147. int mode;
  148. struct node * AE;
  149. struct name * name;
  150. symbol * literalstring;
  151. int number;
  152. int line_number;
  153. int amongvar_needed; /* used in routine definitions */
  154. };
  155. enum name_types {
  156. t_size = 6,
  157. t_string = 0, t_boolean = 1, t_integer = 2, t_routine = 3, t_external = 4,
  158. t_grouping = 5
  159. /* If this list is extended, adjust wvn in generator.c */
  160. };
  161. /* In name_count[i] below, remember that
  162. type is
  163. ----+----
  164. 0 | string
  165. 1 | boolean
  166. 2 | integer
  167. 3 | routine
  168. 4 | external
  169. 5 | grouping
  170. */
  171. struct analyser {
  172. struct tokeniser * tokeniser;
  173. struct node * nodes;
  174. struct name * names;
  175. struct literalstring * literalstrings;
  176. int mode;
  177. byte modifyable; /* false inside reverse(...) */
  178. struct node * program;
  179. struct node * program_end;
  180. int name_count[t_size]; /* name_count[i] counts the number of names of type i */
  181. struct among * amongs;
  182. struct among * amongs_end;
  183. int among_count;
  184. int amongvar_needed; /* used in reading routine definitions */
  185. struct grouping * groupings;
  186. struct grouping * groupings_end;
  187. struct node * substring; /* pending 'substring' in current routine definition */
  188. byte utf8;
  189. };
  190. enum analyser_modes {
  191. m_forward = 0, m_backward /*, m_integer */
  192. };
  193. extern void print_program(struct analyser * a);
  194. extern struct analyser * create_analyser(struct tokeniser * t);
  195. extern void close_analyser(struct analyser * a);
  196. extern void read_program(struct analyser * a);
  197. struct generator {
  198. struct analyser * analyser;
  199. struct options * options;
  200. int unreachable; /* 0 if code can be reached, 1 if current code
  201. * is unreachable. */
  202. int var_number; /* Number of next variable to use. */
  203. struct str * outbuf; /* temporary str to store output */
  204. struct str * declarations; /* str storing variable declarations */
  205. int next_label;
  206. int margin;
  207. const char * failure_string; /* String to output in case of a failure. */
  208. #ifndef DISABLE_JAVA
  209. struct str * failure_str; /* This is used by the java generator instead of failure_string */
  210. #endif
  211. int label_used; /* Keep track of whether the failure label is used. */
  212. int failure_label;
  213. int debug_count;
  214. const char * S[10]; /* strings */
  215. symbol * B[10]; /* blocks */
  216. int I[10]; /* integers */
  217. struct name * V[5]; /* variables */
  218. symbol * L[5]; /* literals, used in formatted write */
  219. int line_count; /* counts number of lines output */
  220. int line_labelled; /* in ANSI C, will need extra ';' if it is a block end */
  221. int literalstring_count;
  222. int keep_count; /* used to number keep/restore pairs to avoid compiler warnings
  223. about shadowed variables */
  224. };
  225. struct options {
  226. /* for the command line: */
  227. char * output_file;
  228. char * name;
  229. FILE * output_c;
  230. FILE * output_h;
  231. #ifndef DISABLE_JAVA
  232. FILE * output_java;
  233. #endif
  234. byte syntax_tree;
  235. byte widechars;
  236. enum { LANG_JAVA, LANG_C, LANG_CPLUSPLUS } make_lang;
  237. char * externals_prefix;
  238. char * variables_prefix;
  239. char * runtime_path;
  240. char * parent_class_name;
  241. char * package;
  242. char * string_class;
  243. char * among_class;
  244. struct include * includes;
  245. struct include * includes_end;
  246. byte utf8;
  247. };
  248. /* Generator for C code. */
  249. extern struct generator * create_generator_c(struct analyser * a, struct options * o);
  250. extern void close_generator_c(struct generator * g);
  251. extern void generate_program_c(struct generator * g);
  252. #ifndef DISABLE_JAVA
  253. /* Generator for Java code. */
  254. extern struct generator * create_generator_java(struct analyser * a, struct options * o);
  255. extern void close_generator_java(struct generator * g);
  256. extern void generate_program_java(struct generator * g);
  257. #endif