Changeset 5


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:
5 edited

Legend:

Unmodified
Added
Removed
  • branches/Void

    • Property svn:ignore
      •  

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

    r4 r5  
     1Define Text
    12Write Leave me
     3Write Heal the world
    24Assign Text Hell
    35Pause
  • 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;
  • branches/Void/project1.lpi

    r4 r5  
    3737        <IsPartOfProject Value="True"/>
    3838        <UnitName Value="project1"/>
    39         <CursorPos X="1" Y="65"/>
    40         <TopLine Value="36"/>
     39        <CursorPos X="3" Y="45"/>
     40        <TopLine Value="26"/>
    4141        <EditorIndex Value="0"/>
    42         <UsageCount Value="21"/>
     42        <UsageCount Value="25"/>
    4343        <Loaded Value="True"/>
    4444      </Unit0>
     
    4747        <IsPartOfProject Value="True"/>
    4848        <UnitName Value="UCompilator"/>
    49         <CursorPos X="6" Y="59"/>
    50         <TopLine Value="47"/>
     49        <CursorPos X="58" Y="124"/>
     50        <TopLine Value="103"/>
    5151        <EditorIndex Value="1"/>
    52         <UsageCount Value="21"/>
     52        <UsageCount Value="25"/>
    5353        <Loaded Value="True"/>
    5454      </Unit1>
     
    5656        <Filename Value="Example.void"/>
    5757        <IsPartOfProject Value="True"/>
    58         <CursorPos X="17" Y="2"/>
     58        <CursorPos X="24" Y="4"/>
    5959        <TopLine Value="1"/>
    6060        <EditorIndex Value="2"/>
    61         <UsageCount Value="21"/>
     61        <UsageCount Value="25"/>
    6262        <Loaded Value="True"/>
    6363        <SyntaxHighlighter Value="None"/>
     
    6969        <TopLine Value="1"/>
    7070        <EditorIndex Value="3"/>
    71         <UsageCount Value="20"/>
     71        <UsageCount Value="24"/>
    7272        <Loaded Value="True"/>
    7373        <SyntaxHighlighter Value="None"/>
    7474      </Unit3>
    7575    </Units>
    76     <JumpHistory Count="15" HistoryIndex="14">
     76    <JumpHistory Count="30" HistoryIndex="29">
    7777      <Position1>
    7878        <Filename Value="UCompilator.pas"/>
    79         <Caret Line="15" Column="41" TopLine="1"/>
     79        <Caret Line="51" Column="68" TopLine="36"/>
    8080      </Position1>
    8181      <Position2>
    8282        <Filename Value="UCompilator.pas"/>
    83         <Caret Line="14" Column="1" TopLine="1"/>
     83        <Caret Line="52" Column="52" TopLine="37"/>
    8484      </Position2>
    8585      <Position3>
    8686        <Filename Value="UCompilator.pas"/>
    87         <Caret Line="24" Column="1" TopLine="1"/>
     87        <Caret Line="20" Column="24" TopLine="9"/>
    8888      </Position3>
    8989      <Position4>
    9090        <Filename Value="UCompilator.pas"/>
    91         <Caret Line="28" Column="20" TopLine="2"/>
     91        <Caret Line="57" Column="41" TopLine="42"/>
    9292      </Position4>
    9393      <Position5>
    9494        <Filename Value="UCompilator.pas"/>
    95         <Caret Line="32" Column="1" TopLine="2"/>
     95        <Caret Line="85" Column="1" TopLine="55"/>
    9696      </Position5>
    9797      <Position6>
    98         <Filename Value="UCompilator.pas"/>
    99         <Caret Line="16" Column="15" TopLine="1"/>
     98        <Filename Value="project1.lpr"/>
     99        <Caret Line="38" Column="5" TopLine="36"/>
    100100      </Position6>
    101101      <Position7>
    102102        <Filename Value="UCompilator.pas"/>
    103         <Caret Line="18" Column="1" TopLine="2"/>
     103        <Caret Line="28" Column="34" TopLine="10"/>
    104104      </Position7>
    105105      <Position8>
    106106        <Filename Value="UCompilator.pas"/>
    107         <Caret Line="55" Column="5" TopLine="26"/>
     107        <Caret Line="86" Column="21" TopLine="70"/>
    108108      </Position8>
    109109      <Position9>
    110110        <Filename Value="UCompilator.pas"/>
    111         <Caret Line="37" Column="1" TopLine="22"/>
     111        <Caret Line="11" Column="30" TopLine="3"/>
    112112      </Position9>
    113113      <Position10>
    114114        <Filename Value="UCompilator.pas"/>
    115         <Caret Line="51" Column="68" TopLine="36"/>
     115        <Caret Line="24" Column="59" TopLine="10"/>
    116116      </Position10>
    117117      <Position11>
    118118        <Filename Value="UCompilator.pas"/>
    119         <Caret Line="52" Column="52" TopLine="37"/>
     119        <Caret Line="41" Column="35" TopLine="39"/>
    120120      </Position11>
    121121      <Position12>
    122122        <Filename Value="UCompilator.pas"/>
    123         <Caret Line="20" Column="24" TopLine="9"/>
     123        <Caret Line="81" Column="9" TopLine="52"/>
    124124      </Position12>
    125125      <Position13>
    126126        <Filename Value="UCompilator.pas"/>
    127         <Caret Line="57" Column="41" TopLine="42"/>
     127        <Caret Line="24" Column="35" TopLine="9"/>
    128128      </Position13>
    129129      <Position14>
    130130        <Filename Value="UCompilator.pas"/>
    131         <Caret Line="85" Column="1" TopLine="55"/>
     131        <Caret Line="44" Column="57" TopLine="60"/>
    132132      </Position14>
    133133      <Position15>
    134         <Filename Value="project1.lpr"/>
    135         <Caret Line="38" Column="5" TopLine="36"/>
     134        <Filename Value="UCompilator.pas"/>
     135        <Caret Line="25" Column="1" TopLine="13"/>
    136136      </Position15>
     137      <Position16>
     138        <Filename Value="UCompilator.pas"/>
     139        <Caret Line="48" Column="55" TopLine="48"/>
     140      </Position16>
     141      <Position17>
     142        <Filename Value="UCompilator.pas"/>
     143        <Caret Line="89" Column="22" TopLine="70"/>
     144      </Position17>
     145      <Position18>
     146        <Filename Value="UCompilator.pas"/>
     147        <Caret Line="19" Column="9" TopLine="1"/>
     148      </Position18>
     149      <Position19>
     150        <Filename Value="UCompilator.pas"/>
     151        <Caret Line="89" Column="22" TopLine="74"/>
     152      </Position19>
     153      <Position20>
     154        <Filename Value="UCompilator.pas"/>
     155        <Caret Line="100" Column="24" TopLine="88"/>
     156      </Position20>
     157      <Position21>
     158        <Filename Value="UCompilator.pas"/>
     159        <Caret Line="114" Column="23" TopLine="99"/>
     160      </Position21>
     161      <Position22>
     162        <Filename Value="project1.lpr"/>
     163        <Caret Line="18" Column="15" TopLine="1"/>
     164      </Position22>
     165      <Position23>
     166        <Filename Value="project1.lpr"/>
     167        <Caret Line="48" Column="52" TopLine="45"/>
     168      </Position23>
     169      <Position24>
     170        <Filename Value="project1.lpr"/>
     171        <Caret Line="44" Column="7" TopLine="30"/>
     172      </Position24>
     173      <Position25>
     174        <Filename Value="project1.lpr"/>
     175        <Caret Line="1" Column="5" TopLine="1"/>
     176      </Position25>
     177      <Position26>
     178        <Filename Value="project1.lpr"/>
     179        <Caret Line="40" Column="27" TopLine="26"/>
     180      </Position26>
     181      <Position27>
     182        <Filename Value="project1.lpr"/>
     183        <Caret Line="18" Column="49" TopLine="1"/>
     184      </Position27>
     185      <Position28>
     186        <Filename Value="project1.lpr"/>
     187        <Caret Line="51" Column="58" TopLine="50"/>
     188      </Position28>
     189      <Position29>
     190        <Filename Value="project1.lpr"/>
     191        <Caret Line="41" Column="16" TopLine="26"/>
     192      </Position29>
     193      <Position30>
     194        <Filename Value="UCompilator.pas"/>
     195        <Caret Line="11" Column="77" TopLine="1"/>
     196      </Position30>
    137197    </JumpHistory>
    138198  </ProjectOptions>
  • branches/Void/project1.lpr

    r3 r5  
    1010
    1111type
     12
     13  { TApplication }
     14
    1215  TApplication = class(TCustomApplication)
    1316  protected
    1417    procedure DoRun; override;
     18    procedure CompilatorError(Text: string; var Terminate: Boolean);
    1519  public
    1620    Compilator: TCompilator;
     
    3438
    3539  Compilator := TCompilator.Create;
    36   Compilator.Compile('Example.void');
    37   Compilator.Destroy;
    38   //ReadLn;
     40  with Compilator do begin
     41    OnError := CompilatorError;
     42    Compile('Example.void');
     43    Destroy;
     44  end;
     45  ReadLn;
    3946
    4047  // stop program loop
    4148  Terminate;
     49end;
     50
     51procedure TApplication.CompilatorError(Text: string; var Terminate: Boolean);
     52begin
     53  Terminate := False;
     54  WriteLn('Error: ' + Text);
    4255end;
    4356
     
    4659  inherited Create(TheOwner);
    4760  StopOnException := True;
    48 end;
     61
     62  end;
    4963
    5064destructor TApplication.Destroy;
Note: See TracChangeset for help on using the changeset viewer.