1 /* R E A D ( L N ) & W R I T E ( L N ) */
19 extern char *sprint();
22 register struct node *arg;
26 char *message, buff[80];
27 extern char *ChkAllowedVar();
30 assert(arg->nd_symb == ',');
32 if( arg->nd_left->nd_type->tp_fund == T_FILE ) {
36 error("\"%s\": variable-access expected", name);
41 else if( !(file = ChkStdInOut(name, 0)) )
45 assert(arg->nd_symb == ',');
47 if( file->nd_type != text_type ) {
48 /* real var & file of integer */
49 if( !TstAssCompat(arg->nd_left->nd_type,
50 BaseType(file->nd_type->next)) ) {
51 node_error(arg->nd_left,
52 "\"%s\": illegal parameter type",name);
55 else if( (BaseType(file->nd_type->next) == long_type
56 && arg->nd_left->nd_type == int_type)
58 (BaseType(file->nd_type->next) == int_type
59 && arg->nd_left->nd_type == long_type) ) {
60 if( int_size != long_size ) {
61 node_error(arg->nd_left,
62 "\"%s\": longs and integers have different sizes",name);
65 else node_warning(arg->nd_left,
66 "\"%s\": mixture of longs and integers", name);
69 else if( !(BaseType(arg->nd_left->nd_type)->tp_fund &
70 ( T_CHAR | T_NUMERIC )) ) {
71 node_error(arg->nd_left,
72 "\"%s\": illegal parameter type",name);
75 message = ChkAllowedVar(arg->nd_left, 1);
77 sprint(buff,"\"%%s\": %s can't be a variable parameter",
79 node_error(arg->nd_left, buff, name);
83 CodeRead(file, arg->nd_left);
89 register struct node *arg;
92 char *name = "readln";
93 char *message, buff[80];
94 extern char *ChkAllowedVar();
97 if( !(file = ChkStdInOut(name, 0)) )
105 assert(arg->nd_symb == ',');
107 if( arg->nd_left->nd_type->tp_fund == T_FILE ) {
108 if( arg->nd_left->nd_type != text_type ) {
109 node_error(arg->nd_left,
110 "\"%s\": textfile expected", name);
119 else if( !(file = ChkStdInOut(name, 0)) )
123 assert(arg->nd_symb == ',');
125 if( !(BaseType(arg->nd_left->nd_type)->tp_fund &
126 ( T_CHAR | T_NUMERIC )) ) {
127 node_error(arg->nd_left,
128 "\"%s\": illegal parameter type",name);
131 message = ChkAllowedVar(arg->nd_left, 1);
133 sprint(buff,"\"%%s\": %s can't be a variable parameter",
135 node_error(arg->nd_left, buff, name);
138 CodeRead(file, arg->nd_left);
145 register struct node *arg;
147 struct node *left, *expp, *file;
148 char *name = "write";
151 assert(arg->nd_symb == ',');
152 assert(arg->nd_left->nd_symb == ':');
155 expp = left->nd_left;
157 if( expp->nd_type->tp_fund == T_FILE ) {
158 if( left->nd_right ) {
160 "\"%s\": filevariable can't have a width",name);
167 error("\"%s\": expression expected", name);
171 else if( !(file = ChkStdInOut(name, 1)) )
175 assert(arg->nd_symb == ',');
177 if( !ChkWriteParameter(file->nd_type, arg->nd_left, name) )
180 CodeWrite(file, arg->nd_left);
186 register struct node *arg;
188 struct node *left, *expp, *file;
189 char *name = "writeln";
192 if( !(file = ChkStdInOut(name, 1)) )
200 assert(arg->nd_symb == ',');
201 assert(arg->nd_left->nd_symb == ':');
204 expp = left->nd_left;
206 if( expp->nd_type->tp_fund == T_FILE ) {
207 if( expp->nd_type != text_type ) {
208 node_error(expp, "\"%s\": textfile expected", name);
211 if( left->nd_right ) {
213 "\"%s\": filevariable can't have a width", name);
220 else if( !(file = ChkStdInOut(name, 1)) )
224 assert(arg->nd_symb == ',');
226 if( !ChkWriteParameter(text_type, arg->nd_left, name) )
229 CodeWrite(file, arg->nd_left);
235 ChkWriteParameter(filetype, arg, name)
236 struct type *filetype;
241 char *mess = "illegal write parameter";
243 assert(arg->nd_symb == ':');
245 tp = BaseType(arg->nd_left->nd_type);
247 if( filetype == text_type ) {
248 if( !(tp == bool_type ||
249 tp->tp_fund & (T_CHAR | T_NUMERIC | T_STRING) ||
251 node_error(arg->nd_left, "\"%s\": %s", name, mess);
256 if( !TstAssCompat(BaseType(filetype->next), tp) ) {
257 node_error(arg->nd_left, "\"%s\": %s", name, mess);
260 if( arg->nd_right ) {
261 node_error(arg->nd_left, "\"%s\": %s", name, mess);
268 /* Here we have a text-file */
270 if( arg = arg->nd_right ) {
273 assert(arg->nd_symb == ':');
274 if( BaseType(arg->nd_left->nd_type) != int_type ) {
275 node_error(arg->nd_left, "\"%s\": %s", name, mess);
282 if( arg = arg->nd_right ) {
283 /* Fractional Part */
285 assert(arg->nd_symb == ':');
286 if( tp != real_type ) {
287 node_error(arg->nd_left, "\"%s\": %s", name, mess);
290 if( BaseType(arg->nd_left->nd_type) != int_type ) {
291 node_error(arg->nd_left, "\"%s\": %s", name, mess);
299 ChkStdInOut(name, st_out)
302 register struct def *df;
303 register struct node *nd;
305 if( !(df = lookup(str2idf(st_out ? output : input, 0),
306 GlobalScope, D_INUSE)) ||
307 !(df->df_flags & D_PROGPAR) ) {
308 error("\"%s\": standard input/output not defined", name);
312 nd = MkLeaf(Def, &dot);
314 nd->nd_type = df->df_type;
315 df->df_flags |= D_USED;
321 register struct node *file, *arg;
323 struct type *tp = BaseType(arg->nd_type);
325 if( err_occurred ) return;
329 if( file->nd_type == text_type ) {
330 switch( tp->tp_fund ) {
353 RangeCheck(arg->nd_type, file->nd_type->next);
357 /* Keep the address of the file on the stack */
363 RangeCheck(arg->nd_type, file->nd_type->next);
365 C_loi(file->nd_type->next->tp_psize);
366 if( tp == real_type ) {
367 if( BaseType(file->nd_type->next) == int_type ||
368 BaseType(file->nd_type->next) == long_type )
369 Int2Real(file->nd_type->next->tp_psize);
381 if( err_occurred ) return;
389 register struct node *file, *arg;
392 register arith nbpars = pointer_size;
393 register struct node *expp = arg->nd_left;
394 struct node *right = arg->nd_right;
395 struct type *tp = BaseType(expp->nd_type);
397 if( err_occurred ) return;
402 if( file->nd_type == text_type ) {
403 if( tp->tp_fund & (T_ARRAY | T_STRINGCONST) ) {
405 nbpars += pointer_size + int_size;
407 else nbpars += tp->tp_size;
411 CodePExpr(right->nd_left);
413 right = right->nd_right;
416 switch( tp->tp_fund ) {
417 case T_ENUMERATION: /* boolean */
418 C_cal(width ? "_wsb" : "_wrb");
422 C_cal(width ? "_wsc" : "_wrc");
426 C_cal(width ? "_wsi" : "_wri");
430 C_cal(width ? "_wsl" : "_wrl");
435 CodePExpr(right->nd_left);
439 else C_cal(width ? "_wsr" : "_wrr");
444 C_cal(width ? "_wss" : "_wrs");
448 C_cal(width ? "_wsz" : "_wrz");
452 crash("(CodeWrite)");
458 if( file->nd_type->next == real_type && tp == int_type )
460 else if( file->nd_type->next == real_type && tp == long_type )
467 C_sti(file->nd_type->next->tp_psize);
475 register struct node *file;
477 if( err_occurred ) return;