readwrite.c 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482
  1. /* R E A D ( L N ) & W R I T E ( L N ) */
  2. #include "debug.h"
  3. #include <assert.h>
  4. #include <em.h>
  5. #include "LLlex.h"
  6. #include "def.h"
  7. #include "main.h"
  8. #include "misc.h"
  9. #include "node.h"
  10. #include "scope.h"
  11. #include "type.h"
  12. /* DEBUG */
  13. #include "idf.h"
  14. extern char *sprint();
  15. ChkRead(arg)
  16. register struct node *arg;
  17. {
  18. struct node *file;
  19. char *name = "read";
  20. char *message, buff[80];
  21. extern char *ChkAllowedVar();
  22. assert(arg);
  23. assert(arg->nd_symb == ',');
  24. if( arg->nd_left->nd_type->tp_fund == T_FILE ) {
  25. file = arg->nd_left;
  26. arg = arg->nd_right;
  27. if( !arg ) {
  28. error("\"%s\": variable-access expected", name);
  29. return;
  30. }
  31. MarkUsed(file);
  32. }
  33. else if( !(file = ChkStdInOut(name, 0)) )
  34. return;
  35. while( arg ) {
  36. assert(arg->nd_symb == ',');
  37. if( file->nd_type != text_type ) {
  38. /* real var & file of integer */
  39. if( !TstAssCompat(arg->nd_left->nd_type,
  40. BaseType(file->nd_type->next)) ) {
  41. node_error(arg->nd_left,
  42. "\"%s\": illegal parameter type",name);
  43. return;
  44. }
  45. else if( (BaseType(file->nd_type->next) == long_type
  46. && arg->nd_left->nd_type == int_type)
  47. ||
  48. (BaseType(file->nd_type->next) == int_type
  49. && arg->nd_left->nd_type == long_type) ) {
  50. if( int_size != long_size ) {
  51. node_error(arg->nd_left,
  52. "\"%s\": longs and integers have different sizes",name);
  53. return;
  54. }
  55. else node_warning(arg->nd_left,
  56. "\"%s\": mixture of longs and integers", name);
  57. }
  58. }
  59. else if( !(BaseType(arg->nd_left->nd_type)->tp_fund &
  60. ( T_CHAR | T_NUMERIC )) ) {
  61. node_error(arg->nd_left,
  62. "\"%s\": illegal parameter type",name);
  63. return;
  64. }
  65. message = ChkAllowedVar(arg->nd_left, 1);
  66. if( message ) {
  67. sprint(buff,"\"%%s\": %s can't be a variable parameter",
  68. message);
  69. node_error(arg->nd_left, buff, name);
  70. return;
  71. }
  72. CodeRead(file, arg->nd_left);
  73. arg = arg->nd_right;
  74. }
  75. }
  76. ChkReadln(arg)
  77. register struct node *arg;
  78. {
  79. struct node *file;
  80. char *name = "readln";
  81. char *message, buff[80];
  82. extern char *ChkAllowedVar();
  83. if( !arg ) {
  84. if( !(file = ChkStdInOut(name, 0)) )
  85. return;
  86. else {
  87. CodeReadln(file);
  88. return;
  89. }
  90. }
  91. assert(arg->nd_symb == ',');
  92. if( arg->nd_left->nd_type->tp_fund == T_FILE ) {
  93. if( arg->nd_left->nd_type != text_type ) {
  94. node_error(arg->nd_left,
  95. "\"%s\": textfile expected", name);
  96. return;
  97. }
  98. else {
  99. file = arg->nd_left;
  100. arg = arg->nd_right;
  101. MarkUsed(file);
  102. }
  103. }
  104. else if( !(file = ChkStdInOut(name, 0)) )
  105. return;
  106. while( arg ) {
  107. assert(arg->nd_symb == ',');
  108. if( !(BaseType(arg->nd_left->nd_type)->tp_fund &
  109. ( T_CHAR | T_NUMERIC )) ) {
  110. node_error(arg->nd_left,
  111. "\"%s\": illegal parameter type",name);
  112. return;
  113. }
  114. message = ChkAllowedVar(arg->nd_left, 1);
  115. if( message ) {
  116. sprint(buff,"\"%%s\": %s can't be a variable parameter",
  117. message);
  118. node_error(arg->nd_left, buff, name);
  119. return;
  120. }
  121. CodeRead(file, arg->nd_left);
  122. arg = arg->nd_right;
  123. }
  124. CodeReadln(file);
  125. }
  126. ChkWrite(arg)
  127. register struct node *arg;
  128. {
  129. struct node *left, *expp, *file;
  130. char *name = "write";
  131. assert(arg);
  132. assert(arg->nd_symb == ',');
  133. assert(arg->nd_left->nd_symb == ':');
  134. left = arg->nd_left;
  135. expp = left->nd_left;
  136. if( expp->nd_type->tp_fund == T_FILE ) {
  137. if( left->nd_right ) {
  138. node_error(expp,
  139. "\"%s\": filevariable can't have a width",name);
  140. return;
  141. }
  142. file = expp;
  143. MarkUsed(file);
  144. arg = arg->nd_right;
  145. if( !arg ) {
  146. error("\"%s\": expression expected", name);
  147. return;
  148. }
  149. }
  150. else if( !(file = ChkStdInOut(name, 1)) )
  151. return;
  152. while( arg ) {
  153. assert(arg->nd_symb == ',');
  154. if( !ChkWriteParameter(file->nd_type, arg->nd_left, name) )
  155. return;
  156. CodeWrite(file, arg->nd_left);
  157. arg = arg->nd_right;
  158. }
  159. }
  160. ChkWriteln(arg)
  161. register struct node *arg;
  162. {
  163. struct node *left, *expp, *file;
  164. char *name = "writeln";
  165. if( !arg ) {
  166. if( !(file = ChkStdInOut(name, 1)) )
  167. return;
  168. else {
  169. CodeWriteln(file);
  170. return;
  171. }
  172. }
  173. assert(arg->nd_symb == ',');
  174. assert(arg->nd_left->nd_symb == ':');
  175. left = arg->nd_left;
  176. expp = left->nd_left;
  177. if( expp->nd_type->tp_fund == T_FILE ) {
  178. if( expp->nd_type != text_type ) {
  179. node_error(expp, "\"%s\": textfile expected", name);
  180. return;
  181. }
  182. if( left->nd_right ) {
  183. node_error(expp,
  184. "\"%s\": filevariable can't have a width", name);
  185. return;
  186. }
  187. file = expp;
  188. MarkUsed(file);
  189. arg = arg->nd_right;
  190. }
  191. else if( !(file = ChkStdInOut(name, 1)) )
  192. return;
  193. while( arg ) {
  194. assert(arg->nd_symb == ',');
  195. if( !ChkWriteParameter(text_type, arg->nd_left, name) )
  196. return;
  197. CodeWrite(file, arg->nd_left);
  198. arg = arg->nd_right;
  199. }
  200. CodeWriteln(file);
  201. }
  202. ChkWriteParameter(filetype, arg, name)
  203. struct type *filetype;
  204. struct node *arg;
  205. char *name;
  206. {
  207. struct type *tp;
  208. char *mess = "illegal write parameter";
  209. assert(arg->nd_symb == ':');
  210. tp = BaseType(arg->nd_left->nd_type);
  211. if( filetype == text_type ) {
  212. if( !(tp == bool_type ||
  213. tp->tp_fund & (T_CHAR | T_NUMERIC | T_STRING) ||
  214. IsString(tp)) ) {
  215. node_error(arg->nd_left, "\"%s\": %s", name, mess);
  216. return 0;
  217. }
  218. }
  219. else {
  220. if( !TstAssCompat(BaseType(filetype->next), tp) ) {
  221. node_error(arg->nd_left, "\"%s\": %s", name, mess);
  222. return 0;
  223. }
  224. if( arg->nd_right ) {
  225. node_error(arg->nd_left, "\"%s\": %s", name, mess);
  226. return 0;
  227. }
  228. else
  229. return 1;
  230. }
  231. /* Here we have a text-file */
  232. if( arg = arg->nd_right ) {
  233. /* Total width */
  234. assert(arg->nd_symb == ':');
  235. if( BaseType(arg->nd_left->nd_type) != int_type ) {
  236. node_error(arg->nd_left, "\"%s\": %s", name, mess);
  237. return 0;
  238. }
  239. }
  240. else
  241. return 1;
  242. if( arg = arg->nd_right ) {
  243. /* Fractional Part */
  244. assert(arg->nd_symb == ':');
  245. if( tp != real_type ) {
  246. node_error(arg->nd_left, "\"%s\": %s", name, mess);
  247. return 0;
  248. }
  249. if( BaseType(arg->nd_left->nd_type) != int_type ) {
  250. node_error(arg->nd_left, "\"%s\": %s", name, mess);
  251. return 0;
  252. }
  253. }
  254. return 1;
  255. }
  256. struct node *
  257. ChkStdInOut(name, st_out)
  258. char *name;
  259. {
  260. register struct def *df;
  261. register struct node *nd;
  262. if( !(df = lookup(str2idf(st_out ? output : input, 0),
  263. GlobalScope, D_INUSE)) ||
  264. !(df->df_flags & D_PROGPAR) ) {
  265. error("\"%s\": standard input/output not defined", name);
  266. return NULLNODE;
  267. }
  268. nd = MkLeaf(Def, &dot);
  269. nd->nd_def = df;
  270. nd->nd_type = df->df_type;
  271. df->df_flags |= D_USED;
  272. return nd;
  273. }
  274. CodeRead(file, arg)
  275. register struct node *file, *arg;
  276. {
  277. struct type *tp = BaseType(arg->nd_type);
  278. if( err_occurred ) return;
  279. CodeDAddress(file);
  280. if( file->nd_type == text_type ) {
  281. switch( tp->tp_fund ) {
  282. case T_CHAR:
  283. C_cal("_rdc");
  284. break;
  285. case T_INTEGER:
  286. C_cal("_rdi");
  287. break;
  288. case T_LONG:
  289. C_cal("_rdl");
  290. break;
  291. case T_REAL:
  292. C_cal("_rdr");
  293. break;
  294. default:
  295. crash("(CodeRead)");
  296. /*NOTREACHED*/
  297. }
  298. C_asp(pointer_size);
  299. C_lfr(tp->tp_size);
  300. RangeCheck(arg->nd_type, file->nd_type->next);
  301. CodeDStore(arg);
  302. }
  303. else {
  304. /* Keep the address of the file on the stack */
  305. C_dup(pointer_size);
  306. C_cal("_wdw");
  307. C_asp(pointer_size);
  308. C_lfr(pointer_size);
  309. RangeCheck(arg->nd_type, file->nd_type->next);
  310. C_loi(file->nd_type->next->tp_psize);
  311. if( tp == real_type ) {
  312. if( BaseType(file->nd_type->next) == int_type ||
  313. BaseType(file->nd_type->next) == long_type )
  314. Int2Real(file->nd_type->next->tp_psize);
  315. }
  316. CodeDStore(arg);
  317. C_cal("_get");
  318. C_asp(pointer_size);
  319. }
  320. }
  321. CodeReadln(file)
  322. struct node *file;
  323. {
  324. if( err_occurred ) return;
  325. CodeDAddress(file);
  326. C_cal("_rln");
  327. C_asp(pointer_size);
  328. }
  329. CodeWrite(file, arg)
  330. register struct node *file, *arg;
  331. {
  332. int width = 0;
  333. register arith nbpars = pointer_size;
  334. register struct node *expp = arg->nd_left;
  335. struct node *right = arg->nd_right;
  336. struct type *tp = BaseType(expp->nd_type);
  337. if( err_occurred ) return;
  338. CodeDAddress(file);
  339. CodePExpr(expp);
  340. if( file->nd_type == text_type ) {
  341. if( tp->tp_fund & (T_ARRAY | T_STRINGCONST) ) {
  342. C_loc(IsString(tp));
  343. nbpars += pointer_size + int_size;
  344. }
  345. else nbpars += tp->tp_size;
  346. if( right ) {
  347. width = 1;
  348. CodePExpr(right->nd_left);
  349. nbpars += int_size;
  350. right = right->nd_right;
  351. }
  352. switch( tp->tp_fund ) {
  353. case T_ENUMERATION: /* boolean */
  354. C_cal(width ? "_wsb" : "_wrb");
  355. break;
  356. case T_CHAR:
  357. C_cal(width ? "_wsc" : "_wrc");
  358. break;
  359. case T_INTEGER:
  360. C_cal(width ? "_wsi" : "_wri");
  361. break;
  362. case T_LONG:
  363. C_cal(width ? "_wsl" : "_wrl");
  364. break;
  365. case T_REAL:
  366. if( right ) {
  367. CodePExpr(right->nd_left);
  368. nbpars += int_size;
  369. C_cal("_wrf");
  370. }
  371. else C_cal(width ? "_wsr" : "_wrr");
  372. break;
  373. case T_ARRAY:
  374. case T_STRINGCONST:
  375. C_cal(width ? "_wss" : "_wrs");
  376. break;
  377. case T_STRING:
  378. C_cal(width ? "_wsz" : "_wrz");
  379. break;
  380. default:
  381. crash("(CodeWrite)");
  382. /*NOTREACHED*/
  383. }
  384. C_asp(nbpars);
  385. }
  386. else {
  387. if( file->nd_type->next == real_type && tp == int_type )
  388. Int2Real(int_size);
  389. else if( file->nd_type->next == real_type && tp == long_type )
  390. Int2Real(long_size);
  391. CodeDAddress(file);
  392. C_cal("_wdw");
  393. C_asp(pointer_size);
  394. C_lfr(pointer_size);
  395. C_sti(file->nd_type->next->tp_psize);
  396. C_cal("_put");
  397. C_asp(pointer_size);
  398. }
  399. }
  400. CodeWriteln(file)
  401. register struct node *file;
  402. {
  403. if( err_occurred ) return;
  404. CodeDAddress(file);
  405. C_cal("_wln");
  406. C_asp(pointer_size);
  407. }