Ignore:
Timestamp:
Nov 10, 2009, 10:27:21 AM (15 years ago)
Author:
george
Message:
  • Upraveno: Rozdělení jednotek pro generování výstupů do samostatných souborů.
  • Přidáno: Generátor kódu pro procesor Z80.
  • Přidáno: Parsování celých čísel.
File:
1 edited

Legend:

Unmodified
Added
Removed
  • branches/Void/UOutputGenerator.pas

    r14 r16  
    1111  TOutputGenerator = class
    1212  private
     13  public
     14    Model: TModel;
    1315    IndentCount: Integer;
    14     Model: TModel;
     16    Output: TStringList;
    1517    function Indent: string;
    16   public
    17     Output: TStringList;
    1818    procedure Generate(Model: TModel); virtual;
    1919    constructor Create;
    2020    destructor Destroy; override;
    21   end;
    22 
    23   { TPascalGenerator }
    24 
    25   TPascalGenerator = class(TOutputGenerator)
    26   private
    27     function GenerateVariableValue(Value: TVariableValue): string;
    28   public
    29     procedure Generate(Model: TModel); override;
    30     procedure GenerateModule(Module: TModule);
    31   end;
    32 
    33   { TCGenerator }
    34 
    35   TCGenerator = class(TOutputGenerator)
    36   private
    37     function ConvertType(Name: string): string;
    38     function GenerateVariableValue(Value: TVariableValue): string;
    39   public
    40     procedure Generate(Model: TModel); override;
    41     procedure GenerateModule(Module: TModule);
    4221  end;
    4322
     
    6746end;
    6847
    69 { TPascalGenerator }
    70 
    71 function TPascalGenerator.GenerateVariableValue(Value: TVariableValue): string;
    72 begin
    73   case Value.ValueType of
    74     vtVariable: if Assigned(Value.VariableDef) then Result := Value.VariableDef.Name
    75       else Result := '';
    76     vtFloat: Result := FloatToStr(Value.FloatConstant);
    77     vtNumber: Result := IntToStr(Value.NumberConstant);
    78     vtString: Result := '''' + Value.StringConstant + '''';
    79     vtChar: Result := '''' + Value.CharConstant + '''';
    80   end;
    81 end;
    82 
    83 procedure TPascalGenerator.Generate(Model: TModel);
    84 begin
    85   inherited;
    86   GenerateModule(Model.Module);
    87 end;
    88 
    89 procedure TPascalGenerator.GenerateModule(Module: TModule);
    90 var
    91   I, P: Integer;
    92   ParameterText: string;
    93   Row: string;
    94 begin
    95   with Module do begin
    96     // Prepare output
    97     Output.Clear;
    98     Output.Add('program Test;');
    99     Output.Add('{$APPTYPE CONSOLE}');
    100 
    101     // var section
    102     if Variables.Count > 0 then Output.Add('var');
    103     Inc(IndentCount);
    104     for I := 0 to Variables.Count - 1 do
    105       with TVariable(Variables[I]) do
    106         Output.Add(Indent + Name + ': ' + VarType.Name + ';');
    107     Dec(IndentCount);
    108 
    109     // Code block
    110     Output.Add('begin');
    111     Inc(IndentCount);
    112     for I := 0 to BeginEnd.Commands.Count - 1 do
    113       with TCommand(BeginEnd.Commands[I]) do begin
    114         if Name = 'Assignment' then Output.Add(Indent +
    115           TVariableValue(Parameters[0]).VariableDef.Name + ' := ' +
    116           GenerateVariableValue(TVariableValue(Parameters[1])) + ';')
    117         else begin
    118         Row := Name;
    119         if Parameters.Count > 0 then begin
    120           ParameterText := '';
    121           for P := 0 to Parameters.Count - 1 do
    122             ParameterText := ParameterText + GenerateVariableValue(Parameters[P]) + ', ';
    123           Row := Row + '(' + Copy(ParameterText, 1, Length(ParameterText) - 2) + ')';
    124         end;
    125         Output.Add(Indent + Row + ';');
    126         end;
    127       end;
    128     Dec(IndentCount);
    129     Output.Add('end.');
    130   end;
    131 end;
    132 
    133 { TCGenerator }
    134 
    135 function TCGenerator.ConvertType(Name: string): string;
    136 begin
    137   if Name = 'String' then Result := 'char*'
    138   else if Name = 'Byte' then Result := 'unsigned char'
    139   else if Name = 'ShortInt' then Result := 'signed char'
    140   else if Name = 'Word' then Result := 'unsigned int'
    141   else if Name = 'Integer' then Result := 'int'
    142   else if Name = 'Real' then Result := 'float'
    143   else if Name = 'Double' then Result := 'double'
    144   else if Name = 'Char' then Result := 'char'
    145   else Result := Name;
    146 end;
    147 
    148 function TCGenerator.GenerateVariableValue(Value: TVariableValue): string;
    149 begin
    150   case Value.ValueType of
    151     vtVariable: if Assigned(Value.VariableDef) then Result := Value.VariableDef.Name
    152       else Result := '';
    153     vtFloat: Result := FloatToStr(Value.FloatConstant);
    154     vtNumber: Result := IntToStr(Value.NumberConstant);
    155     vtString: Result := '"' + Value.StringConstant + '"';
    156     vtChar: Result := '''' + Value.CharConstant + '''';
    157   end;
    158 end;
    159 
    160 procedure TCGenerator.Generate(Model: TModel);
    161 begin
    162   inherited;
    163   IndentCount := 0;
    164   GenerateModule(Model.Module);
    165 end;
    166 
    167 procedure TCGenerator.GenerateModule(Module: TModule);
    168 var
    169   I, P: Integer;
    170   Row: string;
    171   ParameterText: string;
    172 begin
    173   with Module do begin
    174     // Prepare output
    175     Output.Clear;
    176 
    177     Output.Add('int main()');
    178     Output.Add('{');
    179     Inc(IndentCount);
    180 
    181     // Variable section
    182     for I := 0 to Variables.Count - 1 do
    183       with TVariable(Variables[I]) do
    184         Output.Add(Indent + ConvertType(VarType.Name) + ' ' + Name + ';');
    185     if Variables.Count > 0 then Output.Add('');
    186 
    187     // Code block
    188     for I := 0 to BeginEnd.Commands.Count - 1 do
    189       with TCommand(BeginEnd.Commands[I]) do begin
    190         if Name = 'Assignment' then Output.Add(Indent +
    191           TVariableValue(Parameters[0]).VariableDef.Name + ' = ' +
    192           GenerateVariableValue(TVariableValue(Parameters[1])) + ';')
    193         else begin
    194           if Name = 'WriteLn' then Row := 'printf'
    195           else if Name = 'ReadLn' then Row := 'scanf'
    196           else if Name = 'Exit' then Row := 'exit';
    197           Row := Row + '(';
    198           if Parameters.Count > 0 then begin
    199             ParameterText := '';
    200             for P := 0 to Parameters.Count - 1 do
    201               ParameterText := ParameterText + GenerateVariableValue(Parameters[P]) + ', ';
    202             Row := Row + Copy(ParameterText, 1, Length(ParameterText) - 2);
    203           end;
    204           Output.Add(Indent + Row + ');');
    205         end;
    206       end;
    207     Dec(IndentCount);
    208     Output.Add('}');
    209   end;
    210 end;
    211 
    21248end.
    21349
Note: See TracChangeset for help on using the changeset viewer.