Changeset 13


Ignore:
Timestamp:
Nov 10, 2009, 7:14:14 AM (14 years ago)
Author:
george
Message:
  • Přidáno: Odsazování řádků při generování výstupu.
  • Přidáno: Definování a modelování definic datových typů.
Location:
branches/Void
Files:
5 edited

Legend:

Unmodified
Added
Removed
  • branches/Void/Example.void

    r12 r13  
    1 var 
    2   Text: string;
     1var
     2  Text: String;
    33  A: Integer;
    44begin
  • branches/Void/UCompilator.pas

    r12 r13  
    7676  VariableType: string;
    7777  Variable: TVariable;
     78  VarType: TType;
    7879begin
    7980  Parser.ParseNextToken;
     
    8384  repeat
    8485    VariableName := Parser.TokenValue;
    85     Parser.ParseNextToken;
    86     if Parser.TokenType = ttWhiteSpace then Parser.ParseNextToken;
    87     if (Parser.TokenType <> ttSymbol) and (Parser.TokenValue = ':') then
    88       DoError('Expected :');
    89     Parser.ParseNextToken;
    90     if Parser.TokenType = ttWhiteSpace then Parser.ParseNextToken;
    91     if Parser.TokenType <> ttIdentifier then DoError('Expected identifier');
    92     VariableType := Parser.TokenValue;
    93 
    9486    with Model.Module do begin
    95     Variable := FindVariableByName(VariableName);
    96     if Assigned(Variable) then DoError('Variable ' + VariableName + ' redefined')
    97     else begin
    98       Variable := TVariable.Create;
    99       with Variable do begin
    100         Name := VariableName;
    101         VarType := VariableType;
     87      Variable := FindVariableByName(VariableName);
     88      if Assigned(Variable) then DoError('Variable ' + VariableName + ' redefined')
     89      else begin
     90        Parser.ParseNextToken;
     91        if Parser.TokenType = ttWhiteSpace then Parser.ParseNextToken;
     92        if (Parser.TokenType <> ttSymbol) and (Parser.TokenValue = ':') then
     93          DoError('Expected :');
     94        Parser.ParseNextToken;
     95        if Parser.TokenType = ttWhiteSpace then Parser.ParseNextToken;
     96        if Parser.TokenType <> ttIdentifier then DoError('Expected identifier');
     97        VariableType := Parser.TokenValue;
     98        VarType := FindTypeByName(VariableType);
     99        if not Assigned(VarType) then DoError('Undefined type ' + VariableType)
     100        else begin
     101          Variable := TVariable.Create;
     102          with Variable do begin
     103            Name := VariableName;
     104          end;
     105          Variable.VarType := VarType;
     106          Variables.Add(Variable);
     107        end;
    102108      end;
    103       Variables.Add(Variable);
    104     end;
    105109    end;
    106110
  • branches/Void/UModel.pas

    r11 r13  
    99
    1010type
     11  TType = class
     12    Name: string;
     13    BaseType: TType;
     14  end;
     15
    1116  TVariable = class
    1217    Name: string;
    13     VarType: string;
     18    VarType: TType;
    1419  end;
    1520
     
    3641  end;
    3742
     43  { TModule }
     44
    3845  TModule = class
    3946  public
     47    Types: TList;
    4048    Variables: TList;
    4149    Procedures: TList;
     
    4452    destructor Destroy; override;
    4553    procedure Clear;
     54    function FindTypeByName(AName: string): TType;
    4655    function FindProcedureByName(AName: string): TProcedure;
    4756    function FindVariableByName(AName: string): TVariable;
     
    136145constructor TModule.Create;
    137146begin
     147  Types := TList.Create;
    138148  Variables := TList.Create;
    139149  BeginEnd := TBeginEnd.Create;
     
    145155  I: Integer;
    146156begin
     157  for I := 0 to Types.Count - 1 do
     158    TType(Types[I]).Destroy;
     159  Types.Destroy;
    147160  for I := 0 to Variables.Count - 1 do
    148161    TVariable(Variables[I]).Destroy;
     
    178191procedure TModule.Init;
    179192begin
     193  // System types
     194  with TType(Types[Types.Add(TType.Create)]) do begin
     195    Name := 'Integer';
     196  end;
     197  with TType(Types[Types.Add(TType.Create)]) do begin
     198    Name := 'String';
     199  end;
     200  with TType(Types[Types.Add(TType.Create)]) do begin
     201    Name := 'Char';
     202  end;
     203  with TType(Types[Types.Add(TType.Create)]) do begin
     204    Name := 'Byte';
     205  end;
     206  with TType(Types[Types.Add(TType.Create)]) do begin
     207    Name := 'Word';
     208  end;
     209
     210  // System procedures
    180211  with TProcedure(Procedures[Procedures.Add(TProcedure.Create)]) do begin
    181212    Name := 'WriteLn';
    182213  end;
    183    with TProcedure(Procedures[Procedures.Add(TProcedure.Create)]) do begin
     214  with TProcedure(Procedures[Procedures.Add(TProcedure.Create)]) do begin
    184215    Name := 'Exit';
    185216  end;
     
    200231end;
    201232
     233function TModule.FindTypeByName(AName: string): TType;
     234var
     235  I: Integer;
     236begin
     237  I := 0;
     238  while (I < Types.Count) and (TType(Types[I]).Name <> AName) do
     239    Inc(I);
     240  if I < Types.Count then Result := Types[I] else Result := nil;
     241end;
     242
    202243end.
    203244
  • branches/Void/UOutputGenerator.pas

    r11 r13  
    66
    77uses
    8   Classes, SysUtils, UModel;
     8  Classes, SysUtils, UModel, StrUtils;
    99
    1010type
    11 
    12   { TOutputGenerator }
    13 
    1411  TOutputGenerator = class
    1512  private
     13    IndentCount: Integer;
    1614    Model: TModel;
     15    function Indent: string;
    1716  public
    1817    Output: TStringList;
     
    2221  end;
    2322
    24   { TPascalGenerator }
    25 
    2623  TPascalGenerator = class(TOutputGenerator)
    2724    procedure Generate(Model: TModel); override;
     
    2926  end;
    3027
    31   { TCGenerator }
    32 
    3328  TCGenerator = class(TOutputGenerator)
     29  private
     30    function ConvertType(Name: string): string;
     31  public
    3432    procedure Generate(Model: TModel); override;
    3533    procedure GenerateModule(Module: TModule);
     
    3937
    4038{ TOutputGenerator }
     39
     40function TOutputGenerator.Indent: string;
     41begin
     42  Result := DupeString('  ', IndentCount);
     43end;
    4144
    4245procedure TOutputGenerator.Generate(Model: TModel);
     
    7881    // var section
    7982    if Variables.Count > 0 then Output.Add('var');
     83    Inc(IndentCount);
    8084    for I := 0 to Variables.Count - 1 do
    8185      with TVariable(Variables[I]) do
    82         Output.Add('  ' + Name + ': ' + VarType + ';');
     86        Output.Add(Indent + Name + ': ' + VarType.Name + ';');
     87    Dec(IndentCount);
    8388
    8489    // Code block
    8590    Output.Add('begin');
     91    Inc(IndentCount);
    8692    for I := 0 to BeginEnd.Commands.Count - 1 do
    8793      with TCommand(BeginEnd.Commands[I]) do begin
    88         if Name = 'Assignment' then Output.Add(Parameters[0] + ' := ' + Parameters[1] + ';')
     94        if Name = 'Assignment' then Output.Add(Indent + Parameters[0] + ' := ' + Parameters[1] + ';')
    8995        else begin
    9096        Row := Name;
     
    95101          Row := Row + '(' + Copy(ParameterText, 1, Length(ParameterText) - 2) + ')';
    96102        end;
    97         Output.Add(Row + ';');
     103        Output.Add(Indent + Row + ';');
    98104        end;
    99105      end;
     106    Dec(IndentCount);
    100107    Output.Add('end.');
    101108  end;
     
    104111{ TCGenerator }
    105112
     113function TCGenerator.ConvertType(Name: string): string;
     114begin
     115  if Name = 'String' then Result := 'char*'
     116  else if Name = 'Byte' then Result := 'unsigned char'
     117  else if Name = 'ShortInt' then Result := 'signed char'
     118  else if Name = 'Word' then Result := 'unsigned int'
     119  else if Name = 'Integer' then Result := 'int'
     120  else if Name = 'Real' then Result := 'float'
     121  else if Name = 'Double' then Result := 'double'
     122  else if Name = 'Char' then Result := 'char'
     123  else Result := Name;
     124end;
     125
    106126procedure TCGenerator.Generate(Model: TModel);
    107127begin
    108128  inherited;
     129  IndentCount := 0;
    109130  GenerateModule(Model.Module);
    110131end;
     
    122143    Output.Add('int main()');
    123144    Output.Add('{');
     145    Inc(IndentCount);
    124146
    125147    // variable section
    126148    for I := 0 to Variables.Count - 1 do
    127149      with TVariable(Variables[I]) do
    128         Output.Add('  ' + VarType + ' ' + Name + ';');
     150        Output.Add(Indent + '  ' + ConvertType(VarType.Name) + ' ' + Name + ';');
     151    if Variables.Count > 0 then Output.Add('');
    129152
    130153    // Code block
    131154    for I := 0 to BeginEnd.Commands.Count - 1 do
    132155      with TCommand(BeginEnd.Commands[I]) do begin
    133         if Name = 'Assignment' then Output.Add(Parameters[0] + ' = ' + Parameters[1] + ';')
     156        if Name = 'Assignment' then Output.Add(Indent + Parameters[0] + ' = ' + Parameters[1] + ';')
    134157        else begin
    135158          if Name = 'WriteLn' then Row := 'printf'
     
    143166            Row := Row + Copy(ParameterText, 1, Length(ParameterText) - 2);
    144167          end;
    145           Output.Add(Row + ');');
     168          Output.Add(Indent + Row + ');');
    146169        end;
    147170      end;
     171    Dec(IndentCount);
    148172    Output.Add('}');
    149173  end;
  • branches/Void/project1.lpi

    r12 r13  
    99      <Icon Value="0"/>
    1010      <UseXPManifest Value="True"/>
    11       <ActiveEditorIndexAtStart Value="1"/>
     11      <ActiveEditorIndexAtStart Value="4"/>
    1212    </General>
    1313    <VersionInfo>
     
    3333      </Item1>
    3434    </RequiredPackages>
    35     <Units Count="12">
     35    <Units Count="14">
    3636      <Unit0>
    3737        <Filename Value="project1.lpr"/>
    3838        <IsPartOfProject Value="True"/>
    3939        <UnitName Value="project1"/>
    40         <CursorPos X="21" Y="16"/>
     40        <CursorPos X="37" Y="7"/>
    4141        <TopLine Value="1"/>
    42         <UsageCount Value="39"/>
     42        <EditorIndex Value="4"/>
     43        <UsageCount Value="40"/>
     44        <Loaded Value="True"/>
    4345      </Unit0>
    4446      <Unit1>
     
    4951        <UnitName Value="UMainForm"/>
    5052        <CursorPos X="46" Y="28"/>
    51         <TopLine Value="52"/>
     53        <TopLine Value="28"/>
    5254        <EditorIndex Value="0"/>
    53         <UsageCount Value="39"/>
     55        <UsageCount Value="40"/>
    5456        <Loaded Value="True"/>
    5557      </Unit1>
     
    5759        <Filename Value="UCompilator.pas"/>
    5860        <UnitName Value="UCompilator"/>
    59         <CursorPos X="24" Y="80"/>
    60         <TopLine Value="64"/>
     61        <CursorPos X="33" Y="91"/>
     62        <TopLine Value="82"/>
    6163        <EditorIndex Value="1"/>
    62         <UsageCount Value="19"/>
     64        <UsageCount Value="20"/>
    6365        <Loaded Value="True"/>
    6466      </Unit2>
     
    6769        <IsPartOfProject Value="True"/>
    6870        <UnitName Value="UOutputGenerator"/>
    69         <CursorPos X="12" Y="118"/>
    70         <TopLine Value="106"/>
     71        <CursorPos X="32" Y="151"/>
     72        <TopLine Value="143"/>
    7173        <EditorIndex Value="3"/>
    72         <UsageCount Value="39"/>
     74        <UsageCount Value="40"/>
    7375        <Loaded Value="True"/>
    7476      </Unit3>
     
    7678        <Filename Value="Example.void"/>
    7779        <IsPartOfProject Value="True"/>
    78         <CursorPos X="1" Y="1"/>
     80        <CursorPos X="4" Y="1"/>
    7981        <TopLine Value="1"/>
    80         <UsageCount Value="39"/>
     82        <UsageCount Value="40"/>
    8183        <SyntaxHighlighter Value="None"/>
    8284      </Unit4>
     
    8587        <IsPartOfProject Value="True"/>
    8688        <UnitName Value="UModel"/>
    87         <CursorPos X="24" Y="48"/>
    88         <TopLine Value="25"/>
    89         <EditorIndex Value="4"/>
    90         <UsageCount Value="39"/>
     89        <CursorPos X="19" Y="18"/>
     90        <TopLine Value="1"/>
     91        <EditorIndex Value="6"/>
     92        <UsageCount Value="40"/>
    9193        <Loaded Value="True"/>
    9294      </Unit5>
     
    120122        <IsPartOfProject Value="True"/>
    121123        <UnitName Value="UVoidParser"/>
    122         <CursorPos X="9" Y="69"/>
    123         <TopLine Value="7"/>
     124        <CursorPos X="5" Y="51"/>
     125        <TopLine Value="45"/>
    124126        <EditorIndex Value="2"/>
    125         <UsageCount Value="39"/>
     127        <UsageCount Value="40"/>
    126128        <Loaded Value="True"/>
    127129      </Unit10>
     
    132134        <UsageCount Value="9"/>
    133135      </Unit11>
     136      <Unit12>
     137        <Filename Value="..\..\..\..\..\Programy\Lazarus_0.9.29\fpc\2.3.1\source\rtl\inc\strings.pp"/>
     138        <UnitName Value="strings"/>
     139        <CursorPos X="1" Y="1"/>
     140        <TopLine Value="16"/>
     141        <UsageCount Value="10"/>
     142      </Unit12>
     143      <Unit13>
     144        <Filename Value="..\..\..\..\..\Programy\Lazarus_0.9.29\fpc\2.3.1\source\rtl\objpas\strutils.pp"/>
     145        <UnitName Value="strutils"/>
     146        <CursorPos X="1" Y="1"/>
     147        <TopLine Value="43"/>
     148        <EditorIndex Value="5"/>
     149        <UsageCount Value="10"/>
     150        <Loaded Value="True"/>
     151      </Unit13>
    134152    </Units>
    135153    <JumpHistory Count="30" HistoryIndex="29">
    136154      <Position1>
    137155        <Filename Value="UCompilator.pas"/>
    138         <Caret Line="110" Column="1" TopLine="94"/>
     156        <Caret Line="86" Column="1" TopLine="70"/>
    139157      </Position1>
    140158      <Position2>
    141159        <Filename Value="UCompilator.pas"/>
    142         <Caret Line="111" Column="1" TopLine="95"/>
     160        <Caret Line="87" Column="1" TopLine="71"/>
    143161      </Position2>
    144162      <Position3>
    145163        <Filename Value="UCompilator.pas"/>
    146         <Caret Line="112" Column="1" TopLine="96"/>
     164        <Caret Line="89" Column="1" TopLine="73"/>
    147165      </Position3>
    148166      <Position4>
    149167        <Filename Value="UCompilator.pas"/>
    150         <Caret Line="106" Column="24" TopLine="97"/>
     168        <Caret Line="90" Column="1" TopLine="74"/>
    151169      </Position4>
    152170      <Position5>
    153171        <Filename Value="UCompilator.pas"/>
    154         <Caret Line="80" Column="1" TopLine="64"/>
     172        <Caret Line="91" Column="1" TopLine="75"/>
    155173      </Position5>
    156174      <Position6>
    157175        <Filename Value="UCompilator.pas"/>
    158         <Caret Line="81" Column="1" TopLine="65"/>
     176        <Caret Line="92" Column="1" TopLine="76"/>
    159177      </Position6>
    160178      <Position7>
    161179        <Filename Value="UCompilator.pas"/>
    162         <Caret Line="82" Column="1" TopLine="66"/>
     180        <Caret Line="94" Column="1" TopLine="78"/>
    163181      </Position7>
    164182      <Position8>
    165183        <Filename Value="UCompilator.pas"/>
    166         <Caret Line="83" Column="1" TopLine="67"/>
     184        <Caret Line="95" Column="1" TopLine="79"/>
    167185      </Position8>
    168186      <Position9>
    169187        <Filename Value="UCompilator.pas"/>
    170         <Caret Line="84" Column="1" TopLine="68"/>
     188        <Caret Line="96" Column="1" TopLine="80"/>
    171189      </Position9>
    172190      <Position10>
    173191        <Filename Value="UCompilator.pas"/>
    174         <Caret Line="85" Column="1" TopLine="69"/>
     192        <Caret Line="98" Column="1" TopLine="82"/>
    175193      </Position10>
    176194      <Position11>
    177195        <Filename Value="UCompilator.pas"/>
    178         <Caret Line="86" Column="1" TopLine="70"/>
     196        <Caret Line="100" Column="1" TopLine="84"/>
    179197      </Position11>
    180198      <Position12>
    181199        <Filename Value="UCompilator.pas"/>
    182         <Caret Line="87" Column="1" TopLine="71"/>
     200        <Caret Line="101" Column="1" TopLine="85"/>
    183201      </Position12>
    184202      <Position13>
    185203        <Filename Value="UCompilator.pas"/>
    186         <Caret Line="89" Column="1" TopLine="73"/>
     204        <Caret Line="103" Column="1" TopLine="87"/>
    187205      </Position13>
    188206      <Position14>
    189207        <Filename Value="UCompilator.pas"/>
    190         <Caret Line="90" Column="1" TopLine="74"/>
     208        <Caret Line="107" Column="1" TopLine="91"/>
    191209      </Position14>
    192210      <Position15>
    193211        <Filename Value="UCompilator.pas"/>
    194         <Caret Line="91" Column="1" TopLine="75"/>
     212        <Caret Line="108" Column="1" TopLine="92"/>
    195213      </Position15>
    196214      <Position16>
    197215        <Filename Value="UCompilator.pas"/>
    198         <Caret Line="92" Column="1" TopLine="76"/>
     216        <Caret Line="110" Column="1" TopLine="94"/>
    199217      </Position16>
    200218      <Position17>
    201219        <Filename Value="UCompilator.pas"/>
    202         <Caret Line="94" Column="1" TopLine="78"/>
     220        <Caret Line="111" Column="1" TopLine="95"/>
    203221      </Position17>
    204222      <Position18>
    205223        <Filename Value="UCompilator.pas"/>
    206         <Caret Line="95" Column="1" TopLine="79"/>
     224        <Caret Line="112" Column="1" TopLine="96"/>
    207225      </Position18>
    208226      <Position19>
    209227        <Filename Value="UCompilator.pas"/>
    210         <Caret Line="96" Column="1" TopLine="80"/>
     228        <Caret Line="113" Column="1" TopLine="97"/>
    211229      </Position19>
    212230      <Position20>
    213231        <Filename Value="UCompilator.pas"/>
    214         <Caret Line="98" Column="1" TopLine="82"/>
     232        <Caret Line="114" Column="1" TopLine="98"/>
    215233      </Position20>
    216234      <Position21>
    217         <Filename Value="UCompilator.pas"/>
    218         <Caret Line="100" Column="1" TopLine="84"/>
     235        <Filename Value="UModel.pas"/>
     236        <Caret Line="54" Column="50" TopLine="40"/>
    219237      </Position21>
    220238      <Position22>
    221         <Filename Value="UCompilator.pas"/>
    222         <Caret Line="101" Column="1" TopLine="85"/>
     239        <Filename Value="UOutputGenerator.pas"/>
     240        <Caret Line="118" Column="12" TopLine="106"/>
    223241      </Position22>
    224242      <Position23>
    225         <Filename Value="UCompilator.pas"/>
    226         <Caret Line="103" Column="1" TopLine="87"/>
     243        <Filename Value="UOutputGenerator.pas"/>
     244        <Caret Line="82" Column="53" TopLine="61"/>
    227245      </Position23>
    228246      <Position24>
    229         <Filename Value="UCompilator.pas"/>
    230         <Caret Line="107" Column="1" TopLine="91"/>
     247        <Filename Value="UOutputGenerator.pas"/>
     248        <Caret Line="35" Column="48" TopLine="20"/>
    231249      </Position24>
    232250      <Position25>
    233         <Filename Value="UCompilator.pas"/>
    234         <Caret Line="108" Column="1" TopLine="92"/>
     251        <Filename Value="UOutputGenerator.pas"/>
     252        <Caret Line="122" Column="41" TopLine="109"/>
    235253      </Position25>
    236254      <Position26>
    237         <Filename Value="UCompilator.pas"/>
    238         <Caret Line="110" Column="1" TopLine="94"/>
     255        <Filename Value="UOutputGenerator.pas"/>
     256        <Caret Line="18" Column="20" TopLine="4"/>
    239257      </Position26>
    240258      <Position27>
    241         <Filename Value="UCompilator.pas"/>
    242         <Caret Line="111" Column="1" TopLine="95"/>
     259        <Filename Value="UOutputGenerator.pas"/>
     260        <Caret Line="45" Column="42" TopLine="43"/>
    243261      </Position27>
    244262      <Position28>
    245         <Filename Value="UCompilator.pas"/>
    246         <Caret Line="112" Column="1" TopLine="96"/>
     263        <Filename Value="UOutputGenerator.pas"/>
     264        <Caret Line="42" Column="28" TopLine="40"/>
    247265      </Position28>
    248266      <Position29>
    249         <Filename Value="UCompilator.pas"/>
    250         <Caret Line="113" Column="1" TopLine="97"/>
     267        <Filename Value="UOutputGenerator.pas"/>
     268        <Caret Line="8" Column="34" TopLine="1"/>
    251269      </Position29>
    252270      <Position30>
    253         <Filename Value="UCompilator.pas"/>
    254         <Caret Line="114" Column="1" TopLine="98"/>
     271        <Filename Value="project1.lpr"/>
     272        <Caret Line="16" Column="21" TopLine="1"/>
    255273      </Position30>
    256274    </JumpHistory>
     
    278296  </CompilerOptions>
    279297  <Debugging>
    280     <BreakPoints Count="1">
    281       <Item1>
    282         <Source Value="UCompilator.pas"/>
    283         <Line Value="84"/>
    284       </Item1>
    285     </BreakPoints>
    286298    <Exceptions Count="3">
    287299      <Item1>
Note: See TracChangeset for help on using the changeset viewer.