Changeset 14 for branches/DelphiToC


Ignore:
Timestamp:
Apr 9, 2009, 11:04:41 AM (16 years ago)
Author:
george
Message:
  • Opraveno: Chyby v uvolňování paměti.
  • Opraveno: Program po rozdělení do více souborů opět funguje jako dříve.
Location:
branches/DelphiToC
Files:
8 edited

Legend:

Unmodified
Added
Removed
  • branches/DelphiToC/Example.pas

    r12 r14  
    44  B: Byte;
    55begin
    6   A := 'a' + (10 + 2 * 3 + 2) xor 10 / 2;
    7   B := 20;
    8   while A < 10 do A := A + 1;
    9 
     6  A := 1;
    107end;
  • branches/DelphiToC/UAssemblerSource.pas

    r13 r14  
    2525
    2626  TAssemblerProducer = class(TCodeProducer)
    27     AssemblyCode: TList; // TList<TAssemblerLine>
    28     destructor Destroy; override;
    2927  private
    3028    procedure AddInstruction(LabelName, Instruction, Operand1,
     
    3331    procedure GenerateExpression(Expression: TExpression; LabelPrefix: string);
    3432    procedure GenerateProgram(ProgramBlock: TProgram);
     33  public
     34    AssemblyCode: TList; // TList<TAssemblerLine>
     35    procedure Produce; override;
     36    constructor Create;
     37    destructor Destroy; override;
    3538  end;
    3639
     
    9598end;
    9699*)
     100
     101constructor TAssemblerProducer.Create;
     102begin
     103  AssemblyCode := TList.Create;
     104end;
    97105
    98106destructor TAssemblerProducer.Destroy;
     
    177185end;
    178186
    179 
    180 
     187procedure TAssemblerProducer.Produce;
     188begin
     189  inherited;
     190  GenerateProgram(ProgramCode);
     191end;
    181192
    182193end.
  • branches/DelphiToC/UCSource.pas

    r12 r14  
    33interface
    44
     5uses
     6  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
     7  Dialogs, StdCtrls, UPascalSource;
     8
     9type
     10  TCSource = class
     11    TextSource: TStringList;
     12    PascalSource: TStringList;
     13    procedure Generate;
     14    constructor Create;
     15    destructor Destroy; override;
     16  end;
     17
    518implementation
    619
     20{ TCSource }
     21
     22constructor TCSource.Create;
     23begin
     24  TextSource := TStringList.Create;
     25end;
     26
     27destructor TCSource.Destroy;
     28begin
     29  TextSource.Free;
     30  inherited;
     31end;
     32
     33procedure TCSource.Generate;
     34begin
     35
     36end;
     37
    738end.
  • branches/DelphiToC/UCodeProducer.pas

    r13 r14  
    33interface
    44
     5uses
     6  UPascalSource;
     7
    58type
    69  TCodeProducer = class
     10    ProgramCode: TProgram;
    711    procedure Produce; virtual; abstract;
    812  end;
  • branches/DelphiToC/UMainForm.pas

    r12 r14  
    55uses
    66  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
    7   Dialogs, StdCtrls, UPascalSource;
     7  Dialogs, StdCtrls, UPascalSource, UPascalCompiler, UAssemblerSource;
    88
    99type
     
    3838  I: Integer;
    3939begin
    40   Compiler.Compile(Memo1.Lines);
     40  MainForm.Memo3.Clear;
     41  Compiler.SourceCode.Assign(Memo1.Lines);
     42  Compiler.Compile;
    4143  Memo2.Clear;
    42   for I := 0 to Compiler.AssemblyCode.Count - 1 do
    43     Memo2.Lines.Add(TAssemblerLine(Compiler.AssemblyCode[I]).AsString);
     44  with TAssemblerProducer(Compiler.Producer) do
     45  for I := 0 to AssemblyCode.Count - 1 do
     46    Memo2.Lines.Add(TAssemblerLine(AssemblyCode[I]).AsString);
    4447end;
    4548
     
    6669
    6770procedure TMainForm.FormShow(Sender: TObject);
    68 var
    69   I: Integer;
    7071begin
    7172  Memo1.Lines.LoadFromFile('Example.pas');
    72   MainForm.Memo3.Clear;
    73   Compiler.Compile(Memo1.Lines);
    74   Memo2.Clear;
    75   for I := 0 to Compiler.AssemblyCode.Count - 1 do
    76     Memo2.Lines.Add(TAssemblerLine(Compiler.AssemblyCode[I]).AsString);
     73  Button1Click(Self);
    7774end;
    7875
  • branches/DelphiToC/UPascalCompiler.pas

    r13 r14  
    1010  TCompiler = class
    1111  private
    12     SourceCode: TStringList;
    13     CodePosition: Integer;
    1412    ProgramCode: TProgram;
    1513    FOnErrorMessage: TOnErrorMessage;
    1614    procedure ErrorMessage(Text: string);
    1715  public
     16    SourceCode: TStringList;
    1817    Parser: TPascalParser;
    1918    Producer: TCodeProducer;
    2019    constructor Create;
    21     procedure Compile(SourceCode: TStrings);
     20    procedure Compile;
    2221    destructor Destroy; override;
    2322    property OnErrorMessage: TOnErrorMessage read FOnErrorMessage write FOnErrorMessage;
     
    2827{ TCompiler }
    2928
    30 procedure TCompiler.Compile(SourceCode: TStrings);
     29procedure TCompiler.Compile;
    3130begin
    32   Self.SourceCode.Assign(SourceCode);
    33   CodePosition := 1;
     31  Parser.CodePosition := 1;
    3432  Parser.ParseProgram(ProgramCode);
    3533  Producer.Produce;
     
    3937begin
    4038  SourceCode := TStringList.Create;
     39  ProgramCode := TProgram.Create;
    4140  Producer := TAssemblerProducer.Create;
    42   ProgramCode := TProgram.Create;
     41  Producer.ProgramCode := ProgramCode;
     42  Parser := TPascalParser.Create;
     43  Parser.SourceCode := SourceCode;
    4344end;
    4445
    4546destructor TCompiler.Destroy;
    46 var
    47   I: Integer;
    4847begin
    4948  ProgramCode.Free;
    5049  SourceCode.Free;
     50  Parser.Free;
     51  Producer.Free;
    5152end;
    5253
  • branches/DelphiToC/UPascalParser.pas

    r13 r14  
    1212  TPascalParser = class
    1313  private
    14     CodePosition: Integer;
    1514    ProgramCode: TProgram;
    1615    FOnErrorMessage: TOnErrorMessage;
    1716    procedure ErrorMessage(Text: string);
    1817  public
     18    CodePosition: Integer;
    1919    SourceCode: TStringList;
    2020    function IsAlphanumeric(Character: Char): Boolean;
     
    3232    procedure ParseModuleUnit(Module: TModule);
    3333    procedure ParseModuleProgram(Module: TModule);
    34 
    3534    procedure ParseFunction(AFunction: TFunction);
    3635    procedure ParseVariableList(VariableList: TVariableList);
     
    207206begin
    208207  with AProgram do begin
    209   for I := 0 to Modules.Count - 1 do
    210     TModule(Modules[I]).Clear;
    211   Modules.Clear;
    212   with TModule(Modules[Modules.Add(TModule.Create)]) do begin
    213     Name := 'main';
    214     with TType(Types[Types.Add(TType.Create)]) do begin
    215       Name := 'byte';
    216       Size := 1;
    217       UsedType := nil;
    218     end;
    219   end;
     208    for I := 0 to Modules.Count - 1 do
     209      TModule(Modules[I]).Clear;
     210    Modules.Clear;
     211    with TModule(Modules[Modules.Add(TModule.Create)]) do begin
     212      Name := 'main';
     213      with TType(Types[Types.Add(TType.Create)]) do begin
     214        Name := 'byte';
     215        Size := 1;
     216        UsedType := nil;
     217      end;
     218    end;
    220219    ParseModule(TModule(Modules[0]));
    221220  end;
     
    294293  Constant: TConstant;
    295294//  Brackets: Integer;
    296   Expressions: TList; // TList<TExpression>;
     295  Expressions: TExpressionList;
    297296  I: Integer;
    298297  II: Integer;
    299298begin
    300   Expressions := TList.Create;
     299  Expressions := TExpressionList.Create;
    301300  Expressions.Add(TExpression.Create);
    302301  with CommonBlock do begin
     
    367366        end else
    368367        begin
    369                 with TExpression(Expressions[Expressions.Count - 1]) do begin
    370                   SubItems[1] := TExpression.Create;
    371                   TExpression(SubItems[1]).NodeType := ntConstant;
    372 
    373                   if Identifier[1] = '''' then begin
    374                     SetLength(TExpression(SubItems[1]).Value, Length(Identifier));
    375                     for I := 1 to Length(Identifier) do TExpression(SubItems[1]).Value[I - 1] := Byte(Identifier[I]);
    376                   end else begin
    377                     SetLength(TExpression(SubItems[1]).Value, 1);
    378                     TExpression(SubItems[1]).Value[0] := StrToInt(Identifier);
    379                   end;
    380                 end;
    381                 with TExpression(Expressions.Items[Expressions.Add(TExpression.Create)]) do begin
    382                   SubItems[0] := TExpression(Expressions[Expressions.Count - 2]).SubItems[1];
    383                 end;
     368          with TExpression(Expressions[Expressions.Count - 1]) do begin
     369            SubItems[1] := TExpression.Create;
     370            TExpression(SubItems[1]).NodeType := ntConstant;
     371
     372            if Identifier[1] = '''' then begin
     373              SetLength(TExpression(SubItems[1]).Value, Length(Identifier));
     374              for I := 1 to Length(Identifier) do TExpression(SubItems[1]).Value[I - 1] := Byte(Identifier[I]);
     375            end else begin
     376              SetLength(TExpression(SubItems[1]).Value, 1);
     377              TExpression(SubItems[1]).Value[0] := StrToInt(Identifier);
     378            end;
     379          end;
     380          with TExpression(Expressions.Items[Expressions.Add(TExpression.Create)]) do begin
     381            SubItems[0] := TExpression(Expressions[Expressions.Count - 2]).SubItems[1];
     382          end;
    384383        end;
    385384    end;
     
    400399  end;
    401400  Result := TExpression(Expressions[0]).SubItems[1];
    402   TExpression(Expressions[0]).Destroy;
    403   TExpression(Expressions[1]).Destroy;
     401  TExpression(Expressions[0]).SubItems[1] := nil;
     402  TExpression(Expressions[1]).SubItems[0] := nil;
    404403  Expressions.Destroy;
    405404end;
     
    623622end;
    624623
    625 
    626 
    627 
    628 
    629 
    630 
    631624end.
  • branches/DelphiToC/UPascalSource.pas

    r13 r14  
    2424  TVariableList = class;
    2525  TFunctionList = class;
     26  TExpressionList = class;
    2627  TExpression = class;
    2728  TOperationList = class;
     
    5657    Parent: TCommonBlock;
    5758    function Search(Name: string): TType;
     59    destructor Destroy; override;
    5860  end;
    5961
     
    6769    Parent: TCommonBlock;
    6870    function Search(Name: string): TConstant;
     71    destructor Destroy; override;
    6972  end;
    7073
     
    7881    Parent: TCommonBlock;
    7982    function Search(Name: string): TVariable;
     83    destructor Destroy; override;
    8084  end;
    8185
     
    8690    Value: TValue;
    8791    OperatorName: string;
    88     SubItems: TList; // TList<TExpression>
     92    SubItems: TExpressionList;
    8993    Associated: Boolean;
    9094    constructor Create;
    9195    destructor Destroy; override;
    9296  end;
     97
     98  TExpressionList = class(TList)
     99    destructor Destroy; override;
     100  end;
     101
    93102
    94103  TOperation = class
     
    177186  for I := 0 to Modules.Count - 1 do
    178187    TModule(Modules[I]).Free;
     188  Modules.Free;
    179189  Device.Free;
    180190end;
     
    184194
    185195{ TConstantList }
     196
     197destructor TConstantList.Destroy;
     198var
     199  I: Integer;
     200begin
     201  for I := 0 to Count - 1 do
     202    TConstant(Items[I]).Free;
     203  inherited;
     204end;
    186205
    187206function TConstantList.Search(Name: string): TConstant;
     
    257276{ TTypeList }
    258277
     278destructor TTypeList.Destroy;
     279var
     280  I: Integer;
     281begin
     282  for I := 0 to Count - 1 do
     283    TType(Items[I]).Free;
     284  inherited;
     285end;
     286
    259287function TTypeList.Search(Name: string): TType;
    260288var
     
    273301{ TVariableList }
    274302
     303destructor TVariableList.Destroy;
     304var
     305  I: Integer;
     306begin
     307  for I := 0 to Count - 1 do
     308    TVariable(Items[I]).Free;
     309  inherited;
     310end;
     311
    275312function TVariableList.Search(Name: string): TVariable;
    276313var
     
    312349constructor TExpression.Create;
    313350begin
    314   SubItems := TList.Create;
     351  SubItems := TExpressionList.Create;
    315352  SubItems.Count := 2;
    316353end;
     
    318355destructor TExpression.Destroy;
    319356begin
    320   SubItems.Destroy;
     357  SubItems.Free;
    321358  inherited;
    322359end;
     
    341378end;
    342379
     380{ TExpressionList }
     381
     382destructor TExpressionList.Destroy;
     383var
     384  I: Integer;
     385begin
     386  for I := 0 to Count - 1 do
     387    TExpression(Items[I]).Free;
     388  inherited;
     389end;
     390
    343391end.
    344392
Note: See TracChangeset for help on using the changeset viewer.