main.c 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224
  1. /* M A I N P R O G R A M */
  2. #include "debug.h"
  3. #include <em.h>
  4. #include <em_mes.h>
  5. #include <system.h>
  6. #include "LLlex.h"
  7. #include "Lpars.h"
  8. #include "const.h"
  9. #include "def.h"
  10. #include "f_info.h"
  11. #include "idf.h"
  12. #include "input.h"
  13. #include "main.h"
  14. #include "node.h"
  15. #include "required.h"
  16. #include "tokenname.h"
  17. #include "type.h"
  18. char options[128];
  19. char *ProgName;
  20. char *input = "input";
  21. char *output = "output";
  22. label data_label;
  23. label text_label;
  24. struct def *program;
  25. extern int fp_used; /* set if floating point used */
  26. main(argc, argv)
  27. register char **argv;
  28. {
  29. register int Nargc = 1;
  30. register char **Nargv = &argv[0];
  31. ProgName = *argv++;
  32. while( --argc > 0 ) {
  33. if( **argv == '-' )
  34. DoOption((*argv++) + 1);
  35. else
  36. Nargv[Nargc++] = *argv++;
  37. }
  38. Nargv[Nargc] = 0; /* terminate the arg vector */
  39. if( Nargc < 2 ) {
  40. fprint(STDERR, "%s: Use a file argument\n", ProgName);
  41. exit(1);
  42. }
  43. exit(!Compile(Nargv[1], Nargv[2]));
  44. }
  45. Compile(src, dst)
  46. char *src, *dst;
  47. {
  48. extern struct tokenname tkidf[];
  49. extern struct tokenname tkstandard[];
  50. if( !InsertFile(src, (char **) 0, &src) ) {
  51. fprint(STDERR, "%s: cannot open %s\n", ProgName, src);
  52. return 0;
  53. }
  54. LineNumber = 1;
  55. FileName = src;
  56. init_idf();
  57. InitCst();
  58. reserve(tkidf);
  59. reserve(tkstandard);
  60. InitScope();
  61. InitTypes();
  62. AddRequired();
  63. #ifdef DEBUG
  64. if( options['l'] ) {
  65. LexScan();
  66. return 1;
  67. }
  68. #endif DEBUG
  69. C_init(word_size, pointer_size);
  70. if( !C_open(dst) )
  71. fatal("couldn't open output file");
  72. C_magic();
  73. C_ms_emx(word_size, pointer_size);
  74. C_df_dlb(++data_label);
  75. C_rom_scon(FileName, strlen(FileName) + 1);
  76. LLparse();
  77. C_ms_src((arith) (LineNumber - 1), FileName);
  78. if( fp_used ) C_ms_flt();
  79. C_close();
  80. #ifdef DEBUG
  81. if( options['I'] ) Info();
  82. #endif DEBUG
  83. return !err_occurred;
  84. }
  85. #ifdef DEBUG
  86. LexScan()
  87. {
  88. register struct token *tkp = &dot;
  89. extern char *symbol2str();
  90. while( LLlex() > 0 ) {
  91. print(">>> %s ", symbol2str(tkp->tk_symb));
  92. switch( tkp->tk_symb ) {
  93. case IDENT:
  94. print("%s\n", tkp->TOK_IDF->id_text);
  95. break;
  96. case INTEGER:
  97. print("%ld\n", tkp->TOK_INT);
  98. break;
  99. case REAL:
  100. print("%s\n", tkp->TOK_REL);
  101. break;
  102. case STRING:
  103. print("'%s'\n", tkp->TOK_STR);
  104. break;
  105. default:
  106. print("\n");
  107. }
  108. }
  109. }
  110. #endif
  111. AddRequired()
  112. {
  113. register struct def *df;
  114. extern struct def *Enter();
  115. static struct node maxintnode = { 0, 0, Value, 0, { INTEGER, 0 } };
  116. /* PROCEDURES */
  117. /* File handling procedures, Read(ln) & Write(ln) are handled
  118. * in the grammar
  119. */
  120. (void) Enter("rewrite", D_PROCEDURE, std_type, R_REWRITE);
  121. (void) Enter("put", D_PROCEDURE, std_type, R_PUT);
  122. (void) Enter("reset", D_PROCEDURE, std_type, R_RESET);
  123. (void) Enter("get", D_PROCEDURE, std_type, R_GET);
  124. (void) Enter("page", D_PROCEDURE, std_type, R_PAGE);
  125. /* DYNAMIC ALLOCATION PROCEDURES */
  126. (void) Enter("new", D_PROCEDURE, std_type, R_NEW);
  127. (void) Enter("dispose", D_PROCEDURE, std_type, R_DISPOSE);
  128. /* TRANSFER PROCEDURES */
  129. (void) Enter("pack", D_PROCEDURE, std_type, R_PACK);
  130. (void) Enter("unpack", D_PROCEDURE, std_type, R_UNPACK);
  131. /* FUNCTIONS */
  132. /* ARITHMETIC FUNCTIONS */
  133. (void) Enter("abs", D_FUNCTION, std_type, R_ABS);
  134. (void) Enter("sqr", D_FUNCTION, std_type, R_SQR);
  135. (void) Enter("sin", D_FUNCTION, std_type, R_SIN);
  136. (void) Enter("cos", D_FUNCTION, std_type, R_COS);
  137. (void) Enter("exp", D_FUNCTION, std_type, R_EXP);
  138. (void) Enter("ln", D_FUNCTION, std_type, R_LN);
  139. (void) Enter("sqrt", D_FUNCTION, std_type, R_SQRT);
  140. (void) Enter("arctan", D_FUNCTION, std_type, R_ARCTAN);
  141. /* TRANSFER FUNCTIONS */
  142. (void) Enter("trunc", D_FUNCTION, std_type, R_TRUNC);
  143. (void) Enter("round", D_FUNCTION, std_type, R_ROUND);
  144. /* ORDINAL FUNCTIONS */
  145. (void) Enter("ord", D_FUNCTION, std_type, R_ORD);
  146. (void) Enter("chr", D_FUNCTION, std_type, R_CHR);
  147. (void) Enter("succ", D_FUNCTION, std_type, R_SUCC);
  148. (void) Enter("pred", D_FUNCTION, std_type, R_PRED);
  149. /* BOOLEAN FUNCTIONS */
  150. (void) Enter("odd", D_FUNCTION, std_type, R_ODD);
  151. (void) Enter("eof", D_FUNCTION, std_type, R_EOF);
  152. (void) Enter("eoln", D_FUNCTION, std_type, R_EOLN);
  153. /* TYPES */
  154. (void) Enter("char", D_TYPE, char_type, 0);
  155. (void) Enter("integer", D_TYPE, int_type, 0);
  156. (void) Enter("real", D_TYPE, real_type, 0);
  157. (void) Enter("boolean", D_TYPE, bool_type, 0);
  158. (void) Enter("text", D_TYPE, text_type, 0);
  159. /* DIRECTIVES */
  160. (void) Enter("forward", D_FORWARD, NULLTYPE, 0);
  161. (void) Enter("extern", D_EXTERN, NULLTYPE, 0);
  162. /* CONSTANTS */
  163. /* nil is TOKEN and thus part of the grammar */
  164. df = Enter("maxint", D_CONST, int_type, 0);
  165. df->con_const = &maxintnode;
  166. maxintnode.nd_type = int_type;
  167. maxintnode.nd_INT = max_int; /* defined in cstoper.c */
  168. df = Enter("true", D_ENUM, bool_type, 0);
  169. df->enm_val = 1;
  170. df->enm_next = Enter("false", D_ENUM, bool_type, 0);
  171. df = df->enm_next;
  172. df->enm_val = 0;
  173. df->enm_next = NULLDEF;
  174. }
  175. #ifdef DEBUG
  176. int cntlines;
  177. Info()
  178. {
  179. extern int cnt_def, cnt_node, cnt_paramlist, cnt_type, cnt_scope,
  180. cnt_scopelist, cnt_tmpvar, cnt_withdesig,
  181. cnt_case_hdr, cnt_case_entry;
  182. print("\
  183. %6d def\n%6d node\n%6d paramlist\n%6d type\n%6d scope\n%6d scopelist\n\
  184. %6d lab\n%6d tmpvar\n%6d withdesig\n%6d casehdr\n%6d caseentry\n",
  185. cnt_def, cnt_node, cnt_paramlist, cnt_type, cnt_scope, cnt_scopelist, cnt_lab, cnt_tmpvar, cnt_withdesig, cnt_case_hdr, cnt_case_entry);
  186. print("\nNumber of lines read: %d\n", cntlines);
  187. }
  188. #endif