Develop and Download Open Source Software

Browse Subversion Repository

Contents of /io.pas

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1 - (show annotations) (download) (as text)
Mon Nov 7 12:03:00 2011 UTC (12 years, 4 months ago) by shiraishikazuo
File MIME type: text/x-pascal
File size: 18581 byte(s)


1 unit io;
2 {$IFDEF FPC}
3 {$MODE DELPHI}{$H+}
4 {$ENDIF}
5 (***************************************)
6 (* Copyright (C) 2006, SHIRAISHI Kazuo *)
7 (***************************************)
8
9 {$X+}
10
11 interface
12
13 uses base,listcoll,variabl,struct,express;
14
15 function MATREADst(prev,eld:TStatement):TStatement;
16 function MATINPUTst(prev,eld:TStatement):TStatement;
17 function MATLINEINPUTst(prev,eld:TStatement):TStatement;
18 function IORecovery(prev:TStatement):TStatement;
19 procedure RecordSetterClause(var RecordSetter:tpRecordSetter);
20
21 {**********}
22 {TReadInput}
23 {**********}
24 type
25 InputFunction=procedure of object; //現状,ただの目印
26
27
28 type
29 TReadInput=class(TStatement)
30 input:inputfunction; {nilのとき Varilen}
31 chn:TPrincipal;
32 recovery:TStatement;
33 prompt:TPrincipal;
34 timeout:TPrincipal;
35 elapsed:TVariable;
36 vars : TListCollection; {collection of TVariable}
37 option:IOoptions;
38 RecordSetter:tpRecordSetter;
39 OnlyStringVars:boolean;
40 MSAppendQuestionMark:boolean;
41 constructor create(prev,eld:TStatement; f:inputfunction; StrOnly:boolean);
42 constructor createREAD(prev,eld:TStatement);
43 constructor createINPUT(prev,eld:TStatement);
44 constructor createLINEINPUT(prev,eld:TStatement);
45 constructor createCHARACTERINPUT(prev,eld:TStatement);
46 constructor createVariLen(prev,eld:TStatement);
47 function item:TObject;virtual;
48 function itemVarilen:TObject;virtual;abstract;
49 function Code:AnsiString;override;
50 destructor destroy;override;
51 private
52 defaultPrompt:string[2];
53 function MsPrompt:TPrincipal;
54 function ControlItem1:boolean;
55 procedure ControlItem2;
56 procedure RegularRead;
57 procedure RegularInput;
58 procedure LineInput;
59 procedure CharacterInput;
60 procedure varileninput;
61 end;
62
63
64 implementation
65
66 uses Classes, Dialogs,SysUtils,Controls,
67 base0,texthand,helpctex,
68 mat,control,optina,charinp,graphic,sconsts;
69
70
71 type
72 TMatRead=class(TReadInput)
73 function item:TObject;override;
74 function itemVarilen:TObject;override;
75 function Code:AnsiString;override;
76 end;
77
78
79 {**************}
80 {READ statement}
81 {**************}
82
83 function IORecovery(prev:TStatement):TStatement;
84 begin
85 result:=nil;
86 if (token='IF') and (nexttoken='MISSING') then
87 begin
88 gettoken;
89 gettoken;
90 check('THEN',IDH_FILE);
91 if tokenspec=NRep then
92 begin
93 result:=GOTOst(prev,nil);
94 end
95 else
96 begin
97 check('EXIT',IDH_FILE);
98 result:=EXITst(prev,nil);
99 end;
100 end;
101 end;
102
103
104 {*********}
105 {INPUT statement}
106 {********}
107
108
109 function TReadInput.MsPrompt:TPrincipal;
110 begin
111 result:=nil ;{default}
112 if (tokenspec=SCon) and ((NextToken=';') or (NextToken=',')) then
113 begin
114 if permitMicrosoft then
115 begin
116 if NextToken=';' then MSAppendQuestionMark:=true;
117 result:=SExpression;
118 gettoken;
119 end
120 else if AutoCorrect[ac_input] or
121 confirm('INPUT PROMPT '+token+': '+EOL+
122 s_IsCorectAskConvert,
123 IDH_MICROSOFT_IO ) then
124 begin
125 insertText(' PROMPT');
126 result:=SExpression;
127 replacetoken(':');
128 gettoken;
129 end;
130 end;
131 end;
132
133
134 destructor TReadInput.destroy;
135 begin
136 vars.free;
137 prompt.free;
138 recovery.free;
139 chn.free;
140 inherited destroy
141 end;
142
143
144 function TReadInput.ControlItem1:boolean;
145 var
146 CharInput:InputFunction; // input変数の値を比較するために使用する
147 begin
148 CharInput:=Characterinput;
149 result:=true;
150 if (token='#') then
151 chn:=channelExpression
152 else if (token='IF') and (nexttoken='MISSING') then
153 recovery:=IORecovery(self)
154 else if (token='PROMPT') and (nextToken<>',') then
155 begin
156 gettoken;
157 prompt:=SExpression
158 end
159 else if (token='TIMEOUT') and (nextToken<>',') then
160 begin
161 gettoken;
162 timeout:=NExpression
163 end
164 else if (token='ELAPSED') and (nextToken<>',') then
165 begin
166 gettoken;
167 elapsed:=NVariable;
168 if elapsed is TSubstance then
169 TSubstance(elapsed).AddQueryInteger(nil); //Integer不適格
170 end
171 else if (token='CLEAR') and (@input=@charInput) then
172 begin
173 gettoken;
174 Option:=option + [ioClear]
175 end
176 else if (token='NOWAIT') and (@input=@charInput) then
177 begin
178 gettoken;
179 Option:=option + [ioNoWait]
180 end
181 else
182 result:=false;
183
184
185 end;
186
187 procedure TReadInput.ControlItem2;
188 var
189 CharInput:InputFunction; // input変数の値を比較するために使用する
190 begin
191 CharInput:=Characterinput;
192
193 if (token='IF') and (nexttoken='MISSING') and (recovery=nil) then
194 recovery:=IORecovery(self)
195 else if (token='PROMPT') and (prompt=nil) then
196 begin
197 gettoken;
198 prompt:=SExpression
199 end
200 else if (token='TIMEOUT') and (timeout=nil) then
201 begin
202 gettoken;
203 timeout:=NExpression
204 end
205 else if (token='ELAPSED') and (elapsed=nil) then
206 begin
207 gettoken;
208 elapsed:=NVariable;
209 if elapsed is TSubstance then
210 TSubstance(elapsed).AddQueryInteger(nil); //Integer不適格
211 end
212 else if (token='CLEAR') and (@input=@charInput) then
213 begin
214 gettoken;
215 Option:=option + [ioClear]
216 end
217 else if (token='NOWAIT') and (@input=@charInput) then
218 begin
219 gettoken;
220 Option:=option + [ioNoWait]
221 end
222 else
223 RecordSetterClause(RecordSetter);
224 end;
225
226 procedure RecordSetterClause(var RecordSetter:tpRecordSetter);
227 begin
228 if (token='BEGIN') then
229 begin
230 gettoken;
231 RecordSetter:=rsBegin;
232 end
233 else if (token='END') then
234 begin
235 gettoken;
236 RecordSetter:=rsEnd;
237 end
238 else if (token='NEXT') then
239 begin
240 gettoken;
241 RecordSetter:=rsNext;
242 end
243 else if (token='SAME') then
244 begin
245 gettoken;
246 RecordSetter:=rsSAME;
247 end
248 end;
249
250 constructor TReadInput.create(prev,eld:TStatement; f:inputfunction; StrOnly:boolean);
251 var
252 p:TObject;
253 begin
254 inherited create(prev,eld);
255 OnlyStringVars:=StrOnly;
256 RecordSetter:=rsNone;
257 if InsideOfWhen then option:=[ioWhenInside];
258 input:=f;
259 defaultprompt:='? ';
260 prompt:=MsPrompt;
261 if prompt=nil then
262 begin
263 if ControlItem1 then
264 begin
265 while test(',') do
266 ControlItem2;
267 check(':',IDH_INPUT_PROMPT);
268 end;
269 end;
270 vars:=TListCollection.create;
271 if (self is TMatRead)
272 and (@input=@TReadInput.regularinput)
273 and (nextnexttoken='?') then
274 begin
275 input:=nil;
276 Vars.insert(itemVarilen);
277 end
278 else
279 repeat
280 if (prevtoken=',') and (token='SKIP') and (nexttoken='REST') then
281 begin
282 gettoken;
283 gettoken;
284 option:=option+[ioSkipRest];
285 break;
286 end;
287 if StrOnly and (TokenSpec<>SIdf) then
288 seterrexpected(s_StringIdentifier,IDH_MAT_INPUT);
289 p:=item;
290 vars.insert(p);
291 until (@input=@TreadInput.CharacterInput) or (test(',')=false) ;
292 if ProgramUnit.CharacterByte then option:=option+[ioCharacterByte];
293 //if chn=nil then TextMode:=true;
294 end;
295
296
297 function TReadInput.item;
298 begin
299 result:=Inputvari(OnlyStringVars)
300 end;
301
302
303 constructor TReadInput.createREAD(prev,eld:TStatement);
304 begin
305 Create(prev,eld,regularRead,false);
306 option:=option+[ioReadWrite];
307 end;
308
309 constructor TReadInput.createINPUT(prev,eld:TStatement);
310 begin
311 Create(prev,eld,regularInput,false);
312 end;
313
314 constructor TReadInput.createLINEINPUT(prev,eld:TStatement);
315 begin
316 Create(prev,eld,LineInput,true);
317 end;
318
319 constructor TReadInput.createCHARACTERINPUT(prev,eld:TStatement);
320 begin
321 Create(prev,eld,CharacterInput,true);
322 defaultprompt:='';
323 if chn=nil then
324 useCharInput:=true;
325 end;
326
327 constructor TReadInput.createVariLen(prev,eld:TStatement);
328 begin
329 Create(prev,eld,nil{VariLenInput},false);
330 end;
331
332 procedure TReadInput.RegularRead;
333 begin
334 end;
335
336 procedure TReadInput.RegularInput;
337 begin
338 end;
339
340
341 procedure TReadInput.LineInput;
342 begin
343 end;
344
345
346 procedure TReadInput.CharacterInput;
347 begin
348 end;
349
350
351
352
353 function INPUTst(prev,eld:TStatement):TStatement;far;
354 begin
355 INPUTst:=TReadInput.createINPUT(prev,eld)
356 end;
357
358 function READst(prev,eld:TStatement):TStatement;far;
359 begin
360 READst:=TReadInput.createREAD(prev,eld)
361 end;
362
363 function LINEst(prev,eld:TStatement):TStatement;far;
364 begin
365 if permitMicrosoft and ((token='(') or (token='-')) then
366 LINEst:=MSLINEst(prev,eld)
367 else
368 begin
369 check('INPUT',IDH_LINE_INPUT);
370 LINEst:=TREADINPUT.createLINEINPUT(prev,eld)
371 end;
372 end;
373
374 function CHARACTERst(prev,eld:TStatement):TStatement;far;
375 begin
376 check('INPUT',IDH_CHARACTER_INPUT);
377 CHARACTERst:=TREADINPUT.createCHARACTERINPUT(prev,eld)
378 end;
379
380
381
382
383 {*********}
384 {Mat Read }
385 {Mat Input}
386 {*********}
387 type TRedimArray=class
388 mat:TMatrix;
389 redim:TMatRedim;
390 constructor create(mat1:TMatrix);
391 destructor destroy;override;
392 end;
393
394 constructor TRedimArray.create;
395 begin
396 inherited create;
397 mat:=mat1;
398 redim:=Matredim(mat,false);
399 end;
400
401 destructor TRedimArray.destroy;
402 begin
403 mat.free;
404 redim.free;
405 inherited destroy;
406 end;
407 procedure TReadInput.varileninput;
408 begin
409 end;
410
411
412 function TMatRead.item:Tobject;
413 var
414 mat1:TMatrix;
415 begin
416 mat1:=Matrix;
417 result:=TRedimArray.create(mat1);
418 end;
419
420 function TMatRead.itemVariLen:Tobject;
421 var
422 mat1:TMatrix;
423 begin
424 mat1:=Matrix;
425 if mat1.idr.dim<>1 then
426 seterrDimension(IDH_MAT_INPUT);
427 check('(',IDH_MAT_INPUT);
428 check('?',IDH_MAT_INPUT);
429 check(')',IDH_MAT_INPUT);
430 result:=TRedimArray.create(mat1);
431 end;
432
433 function MATREADst(prev,eld:TStatement):TStatement;
434 begin
435 result:=TMatRead.createREAD(prev,eld)
436 end;
437
438 function MATINPUTst(prev,eld:TStatement):TStatement;
439 begin
440 result:=TMatRead.createINPUT(prev,eld)
441 end;
442
443 function MATLINEINPUTst(prev,eld:TStatement):TStatement;
444 begin
445 result:=TMatRead.createLINEINPUT(prev,eld)
446 end;
447
448 {*************}
449 {Generate Code}
450 {*************}
451
452 function TReadInput.Code:AnsiString;
453 var
454 kindlist:Ansistring;
455 InputFunctionName:Ansistring;
456 i:integer;
457 optionCode:Ansistring;
458 promptCode:Ansistring;
459 timelimitCode:Ansistring;
460 insideofwhencode:Ansistring;
461 ChannelCode:Ansistring;
462 ElapsedCode:AnsiString;
463 p:TObject;
464 begin
465 proc.HaveMissing:=True;
466 result:='';
467 optionCode:=IOOptionsCode(option);
468
469 if prompt<>nil then
470 promptCode:=Prompt.Code
471 else
472 promptCode:='''' + DefaultPrompt + '''';
473
474 if timeout<>nil then
475 timelimitCode:=timeout.code
476 else
477 timeLimitCode:='MaxNumberDouble';
478
479 if InsideOfWhen then
480 InsideOfWhenCode:='true'
481 else
482 InsideOfWhenCode:='false' ;
483
484 if elapsed<>nil then
485 elapsedCode:='@' + elapsed.code
486 else
487 elapsedCode:='nil';
488
489 // InputFunctionName
490 InputFunctionName:='';
491 if @input=@TReadInput.RegularInput then
492 InputFunctionName:='InputData'
493 else if @input=@TReadInput.RegularRead then
494 InputFunctionName:='ReadData'
495 else if @input=@TReadInput.LineInput then
496 InputFunctionName:='LineInput'
497 else if @input=@TReadInput.CharacterInput then
498 InputFunctionName:='CharacterInput'
499 else
500 ;
501
502 // kindlist
503 kindlist:='';
504 for i:=0 to vars.count-1 do
505 begin
506 p:=TObject(vars.items[i]);
507 kindlist:=kindlist+ (p as TInputvari).vari.kind
508 end;
509 // ch
510
511 if chn=nil then
512 if ioReadWrite in option then
513 ChannelCode:='DataSeq'
514 else
515 ChannelCode:='console'
516 else
517 ChannelCode:='ChannelList.channel('+ chn.code + ')' ;
518
519 // Code 生成
520 result:=result
521 + 'with TDataList.create do ' +EOL
522 + 'try' +EOL
523 + ' Missing:=false;'+EOL
524 + ' if '+InputFunctionName +'(' + channelCode+ ','''+kindlist+''','
525 + optionCode +',' + promptCode +','
526 + timelimitcode + ',' + elapsedcode + ','
527 + insideofWhenCode + ',' + RecordSetterCode[RecordSetter] + ','
528 + Strint(LineNumb)+ ') then' +EOL
529 + 'begin'+EOL;
530
531 // 代入文の生成
532 for i:=0 to vars.count-1 do
533 if (TObject(vars.items[i]) is TStrVari)
534 and (TStrVari(vars.items[i]).index1<>nil) then
535 begin
536 if PUnit.characterbyte then
537 result:=result+' SubstSubstringByte('
538 else
539 result:=result+' SubstSubstring(';
540 result:=result
541 +TInputVari(vars.items[i]).vari.code + ','
542 +TStrVari(vars.items[i]).index1.code + ','
543 +TStrVari(vars.items[i]).index2.code + ','
544 +' strings[' + Strint(i) + '] ) ;' +EOL ;
545 end
546 else if TObject(vars.items[i]) is TInputvari then
547 begin
548 result:=result
549 +' '+(TObject(vars.items[i]) as TInputvari).vari.code + ' := ' ;
550 if (TObject(vars.items[i]) as TInputvari).vari.kind = 'n' then
551 result:=result + 'FloatVal( strings[' + Strint(i) + ']) ;' +EOL
552 else
553 result:=result + ' strings[' + Strint(i) + '] ;' +EOL ;
554 end;
555
556
557 result:=result
558 + 'end'+EOL
559 + 'else' +EOL
560 + ' Missing:=true;'+EOL
561 + 'finally' + EOL
562 + ' free;' +EOL
563 + 'end;' +EOL;
564
565 if recovery=nil then
566 result:=result + 'if Missing then setexception(base.extype);' +EOL
567 else
568 result:=result + 'if Missing then begin base.extype:=0; ' + recovery.code + 'end;' +EOL;
569
570 end;
571
572 function TMatRead.Code:AnsiString;
573 var
574 kindlist:Ansistring;
575 InputFunctionName:Ansistring;
576 i:integer;
577 optionCode:Ansistring;
578 promptCode:Ansistring;
579 timelimitCode:Ansistring;
580 insideofwhencode:Ansistring;
581 ChannelCode:Ansistring;
582 ElapsedCode:AnsiString;
583 p:TObject;
584 begin
585 proc.HaveMissing:=True;
586 result:='';
587 optionCode:=IOOptionsCode(option);
588
589 if prompt<>nil then
590 promptCode:=Prompt.Code
591 else
592 promptCode:='''' + DefaultPrompt + '''';
593
594 if timeout<>nil then
595 timelimitCode:=timeout.code
596 else
597 timeLimitCode:='MaxNumberDouble';
598
599 if InsideOfWhen then
600 InsideOfWhenCode:='true'
601 else
602 InsideOfWhenCode:='false' ;
603
604 if elapsed<>nil then
605 elapsedCode:='@' + elapsed.code
606 else
607 elapsedCode:='nil';
608
609 // InputFunctionName
610 InputFunctionName:='';
611 if @input=@TReadInput.RegularInput then
612 InputFunctionName:='InputData'
613 else if @input=@TReadInput.RegularRead then
614 InputFunctionName:='ReadData'
615 else if @input=@TReadInput.LineInput then
616 InputFunctionName:='LineInput'
617 //else if @input=@TReadInput.CharacterInput then
618 // InputFunctionName:='CharacterInput'
619 else if @input=nil then
620 InputFunctionName:='InputVarilen';
621
622 // kindlist
623 kindlist:='';
624 for i:=0 to vars.count-1 do
625 begin
626 p:=TObject(vars.items[i]);
627 if p is TRedimArray then
628 begin
629 if (p as TRedimArray).redim<>nil then
630 result:=result+(p as TRedimArray).redim.Code;
631 if i=0 then
632 KindList:=(p as TRedimArray).mat.Code+'.KindList'
633 else
634 KindList:=KindList+'+'+(p as TRedimArray).mat.Code+'.KindList'
635 end;
636 end;
637 // ch
638
639 if chn=nil then
640 if ioReadWrite in option then
641 ChannelCode:='DataSeq'
642 else
643 ChannelCode:='console'
644 else
645 ChannelCode:='ChannelList.channel('+ chn.code + ')' ;
646
647 // Code 生成
648 result:=result
649 + 'with TDataList.create do ' +EOL
650 + 'try' +EOL
651 + ' Missing:=false;'+EOL
652 + ' if '+InputFunctionName +'(' + channelCode+ ','+kindlist+','
653 + optionCode +',' + promptCode +','
654 + timelimitcode + ',' + elapsedcode + ','
655 + insideofWhenCode + ',' + RecordSetterCode[RecordSetter] + ','
656 + Strint(LineNumb)+ ') then' +EOL
657 + 'begin'+EOL;
658
659
660 // 代入文の生成
661 if @input<>nil then
662 begin
663 result:=result +' AssignData([';
664 for i:=0 to vars.count-1 do
665 begin
666 if i>0 then result:=result+',';
667 result:=result + (TObject(vars.items[i]) as TRedimArray).mat.Code;
668 end;
669 result:=result+']);'+EOL;
670 end
671 else
672 result:=result +' '
673 +'AssignVarilen(' +(TObject(vars.items[0]) as TRedimArray).mat.Code + ');'+EOL;
674
675
676 result:=result
677 + 'end'+EOL
678 + 'else' +EOL
679 + ' Missing:=true;'+EOL
680 + 'finally' + EOL
681 + ' free;' +EOL
682 + 'end;' +EOL;
683
684 if recovery=nil then
685 result:=result + 'if Missing then setexception(base.extype);' +EOL
686 else
687 result:=result + 'if Missing then begin base.extype:=0; ' + recovery.code + 'end;' +EOL;
688
689 end;
690
691
692
693
694
695
696 procedure statementTableinit;far;
697 begin
698 StatementTableInitImperative('INPUT',INPUTst);
699 StatementTableInitImperative('LINE',LINEst);
700 StatementTableInitImperative('CHARACTER',CHARACTERst);
701 StatementTableInitImperative('READ',READst);
702 end;
703
704 procedure functiontableInit;far;
705 begin
706 end;
707
708
709 begin
710 tableInitProcs.accept(statementTableinit);
711 tableInitProcs.accept(FunctionTableInit);
712 end.
713

Back to OSDN">Back to OSDN
ViewVC Help
Powered by ViewVC 1.1.26