Ignore:
Timestamp:
Nov 9, 2009, 8:34:09 AM (15 years ago)
Author:
george
Message:
  • Přidáno: Podpora pro proměnné.
Location:
branches/Void
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • branches/Void

    • Property svn:ignore
      •  

        old new  
        55Example.void.bdsproj
        66project1.exe
         7*.dsk
  • branches/Void/UCompilator.pas

    r4 r5  
    99
    1010type
     11  TOnErrorEvent = procedure (Text: string; var Terminate: Boolean) of object;
    1112
    12   { TCompilator }
     13  TVariable = class
     14    Name: string;
     15    VarType: string;
     16  end;
     17
     18  TCommand = class
     19    Text: string;
     20  end;
    1321
    1422  TCompilator = class
     23  private
     24    FOnError: TOnErrorEvent;
     25    Variables: TList;
     26    Commands: TList;
     27    procedure DoError(Text: string);
     28    function FindVariableByName(AName: string): TVariable;
     29  public
    1530    Output: TStringList;
    1631    procedure Compile(FileName: string);
     
    1934    destructor Destroy; override;
    2035    function Parse(var Text: string; Separator: string = ' '): string;
    21     end;
     36    property OnError: TOnErrorEvent read FOnError write FOnError;
     37  end;
    2238
    2339implementation
    2440
    2541{ TCompilator }
     42
     43procedure TCompilator.DoError(Text: string);
     44var
     45  Terminate: Boolean;
     46begin
     47  Terminate := False;
     48  if Assigned(FOnError) then FOnError(Text, Terminate);
     49  if Terminate then raise Exception.Create('Compilation terminated');
     50end;
     51
     52function TCompilator.FindVariableByName(AName: string): TVariable;
     53var
     54  I: Integer;
     55begin
     56  I := 0;
     57  while (I < Variables.Count) and (TVariable(Variables[I]).Name <> AName) do
     58    Inc(I);
     59  if I < Variables.Count then Result := Variables[I] else Result := nil;
     60end;
    2661
    2762procedure TCompilator.Compile(FileName: string);
     
    3368  SourceCode.LoadFromFile(FileName);
    3469
     70  // Process source lines
     71  for I := 0 to SourceCode.Count - 1 do begin
     72    ProcessLine(SourceCode[I]);
     73  end;
     74
    3575  // Prepare output
    3676  Output.Clear;
    3777  Output.Add('program ' + ExtractFileName(FileName) + ';');
    3878  Output.Add('{$APPTYPE CONSOLE}');
     79
     80  // var section
     81  if Variables.Count > 0 then Output.Add('var');
     82  for I := 0 to Variables.Count - 1 do
     83    with TVariable(Variables[I]) do
     84      Output.Add('  ' + Name + ': ' + VarType + ';');
     85
     86  // Code block
    3987  Output.Add('begin');
    40 
    41   for I := 0 to SourceCode.Count - 1 do begin
    42     ProcessLine(SourceCode[I]);
    43   end;
    44   SourceCode.Destroy;
    45 
    46   // Finalize output code
     88  for I := 0 to Commands.Count - 1 do
     89    with TCommand(Commands[I]) do
     90      Output.Add(Text);
    4791  Output.Add('end.');
    4892  Output.SaveToFile(ExtractFileName(FileName) + '.dpr');
     93  SourceCode.Destroy;
    4994end;
    5095
     
    5297var
    5398  Command: string;
    54 begin
     99  Variable: TVariable;
     100  VariableName: string;
     101  begin
    55102  Command := Parse(Line);
    56103  WriteLn(Command);
    57   if Command = 'Write' then Output.Add('WriteLn(''' + Line + ''');')
    58   else if Command = 'Assign' then begin
    59     Output.Add('// Define' + Line)
    60   end else if Command = 'Pause' then Output.Add('ReadLn;')
    61   else if Command = 'Exit' then Output.Add('Exit;')
    62   else begin
    63     Output.Add('// Unknown command: ' + Command);
    64   end;
     104  if Command = 'Write' then
     105  with TCommand(Commands[Commands.Add(TCommand.Create)]) do begin
     106    Text := 'WriteLn(''' + Line + ''');';
     107  end else if Command = 'Define' then begin
     108    VariableName := Parse(Line);
     109    Variable := FindVariableByName(VariableName);
     110    if Assigned(Variable) then DoError('Variable ' + VariableName + ' redefined')
     111    else begin
     112      Variable := TVariable.Create;
     113      with Variable do begin
     114        Name := VariableName;
     115        VarType := 'string';
     116      end;
     117      Variables.Add(Variable);
     118    end;
     119  end else if Command = 'Assign' then with TCommand(Commands[Commands.Add(TCommand.Create)]) do begin
     120    VariableName := Parse(Line);
     121    Variable := FindVariableByName(VariableName);
     122    if not Assigned(Variable) then DoError('Undefined variable ' + VariableName)
     123    else begin
     124      Text := VariableName + ' := ''' + Parse(Line) + ''';';
     125    end;
     126  end else if Command = 'Pause' then with TCommand(Commands[Commands.Add(TCommand.Create)]) do begin
     127    Text := 'ReadLn;';
     128  end else if Command = 'Exit' then with TCommand(Commands[Commands.Add(TCommand.Create)]) do begin
     129    Text := 'Exit;';
     130  end else DoError('Unknown command: ' + Command);
    65131end;
    66132
     
    68134begin
    69135  Output := TStringList.Create;
     136  Variables := TList.Create;
     137  Commands := TList.Create;
    70138end;
    71139
    72140destructor TCompilator.Destroy;
     141var
     142  I: Integer;
    73143begin
    74144  Output.Destroy;
     145  for I := 0 to Variables.Count - 1 do
     146    TVariable(Variables[I]).Destroy;
     147  Variables.Destroy;
     148  for I := 0 to Commands.Count - 1 do
     149    TCommand(Commands[I]).Destroy;
     150  Commands.Destroy;
    75151  inherited Destroy;
    76152end;
Note: See TracChangeset for help on using the changeset viewer.