Ignore:
Timestamp:
Feb 11, 2017, 6:55:42 PM (8 years ago)
Author:
chronos
Message:
  • Modified: Now project3 can interpret project2.
  • Modified: To support standard input and output inside interpreter, all lines read from input starting with | symbol is passed to interpreted code. Also text written to standard output by interpretted program is prefixed with | symbol.
File:
1 edited

Legend:

Unmodified
Added
Removed
  • branches/interpreter/Parser3.pas

    r101 r102  
    1010procedure ParseProgram(ProgramCode: PProgramCode);
    1111
     12
     13var
     14  InnerText: string;
     15  InnerTextPos: Integer;
    1216
    1317implementation
     
    6165procedure ReadInputAll;
    6266var
     67  LastC: Char;
    6368  C: Char;
    64 begin
     69  Inner: Boolean;
     70begin
     71  LastC := #0;
    6572  InputTextPos := 1;
    6673  InputText := '';
     74  InnerTextPos := 1;
     75  InnerText := '';
     76  Inner := False;
    6777  while not Eof do begin
    6878    Read(C);
    69     InputText := InputText + C;
     79    if ((LastC = #0) or (LastC = #13) or (LastC = #10)) and (C = '|') then begin
     80      Inner := True;
     81      LastC := C;
     82      Continue;
     83    end else
     84    if ((LastC = #0) or (LastC = #13) or (LastC = #10)) and (C <> '|') then begin
     85      Inner := False;
     86    end;
     87    if Inner then InnerText := InnerText + C
     88      else InputText := InputText + C;
     89    LastC := C;
    7090  end;
    7191end;
     
    296316  I: Integer;
    297317  II: Integer;
    298 begin
     318  FoundOperator: Boolean;
     319  OldPos: Integer;
     320begin
     321  OldPos := InputTextPos;
    299322  Result := True;
     323  FoundOperator := False;
    300324  repeat
    301325    if CheckNext('(') then begin
     
    312336      Expression^.Items[Length(Expression^.Items) - 1].NodeType := ntOperator;
    313337      Expression^.Items[Length(Expression^.Items) - 1].OperatorType := ExpOperator;
     338      FoundOperator := True;
    314339    end else
    315340    if ParseGetValue(@GetValue, True) then begin
     
    322347    end;
    323348  until False;
     349  if not FoundOperator then begin
     350    Result := False;
     351    InputTextPos := OldPos;
     352    Exit;
     353  end;
    324354
    325355  if Length(Expression^.Items) > 0 then begin
     
    340370            Expression^.Items[I].Associated := True;
    341371            SetLength(Expression^.Items[I].Items, 2);
    342             Expression^.Items[I].Items[1] := Expression^.Items[I - 1];
    343             Expression^.Items[I].Items[0] := Expression^.Items[I + 1];
     372            Expression^.Items[I].Items[0] := Expression^.Items[I - 1];
     373            Expression^.Items[I].Items[1] := Expression^.Items[I + 1];
    344374            DeleteExpressionItem(Expression, I + 1);
    345375            DeleteExpressionItem(Expression, I - 1);
     
    686716  ProgramCode^.Functions.Add(FunctionCreate('Read', nil));
    687717  FuncRead := ProgramCode^.Functions.GetLast;
    688   FuncRead^.Parameters.Add(FunctionParameterCreate('Output', TypeChar));
     718  FuncRead^.Parameters.Add(FunctionParameterCreate('Output', TypeChar, True));
     719  FuncRead^.Variables.Add(VariableCreate('Output', TypeChar));
    689720  ProgramCode^.Functions.Add(FunctionCreate('Eof', TypeBoolean));
    690721  ProgramCode^.Functions.Add(FunctionCreate('Length', TypeInteger));
Note: See TracChangeset for help on using the changeset viewer.