Ignore:
Timestamp:
Nov 9, 2010, 11:19:28 AM (14 years ago)
Author:
george
Message:
  • Added: Support for multiple combined sections var, type, const and functions.
  • Modified: Functions for parsing types redone to return Boolean result of success.
File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/Compiler/Produce/UProducerPascal.pas

    r18 r19  
    2222    procedure GenerateType(AType: TType; AssignSymbol: Char = ':');
    2323    procedure GenerateTypes(Types: TTypeList);
    24     procedure GenerateCommonBlock(CommonBlock: TCommonBlock;
     24    procedure GenerateCommonBlockInterface(CommonBlock: TCommonBlock;
     25      LabelPrefix: string);
     26    procedure GenerateCommonBlockImplementation(CommonBlock: TCommonBlock;
    2527      LabelPrefix: string);
    2628    procedure GenerateFunctions(Functions: TFunctionList);
     29    procedure GenerateFunction(AFunction: TFunction);
     30    procedure GenerateFunctionHead(AFunction: TFunction);
    2731    procedure GenerateConstants(Constants: TConstantList);
     32    procedure GenerateConstant(Constant: TConstant);
    2833    procedure GenerateBeginEnd(BeginEnd: TBeginEnd);
    2934    procedure GenerateVariableList(Variables: TVariableList);
     35    procedure GenerateVariable(Variable: TVariable);
    3036    procedure GenerateCommand(Command: TCommand);
    3137    procedure GenerateWhileDo(WhileDo: TWhileDo);
     
    7884  EmitLn(';');
    7985  Dec(Indetation);
     86  EmitLn;
    8087end;
    8188
     
    8794    Module.TargetFile := Module.Name + '.dpr';
    8895    EmitLn('program ' + Name + ';');
     96    EmitLn;
    8997    GenerateUses(UsedModules);
    90     GenerateCommonBlock(Body, '');
     98    GenerateCommonBlockImplementation(Body, '');
    9199    EmitLn('.');
    92100  end else
     
    102110  EmitLn('interface');
    103111  EmitLn;
    104   GenerateCommonBlock(TModuleUnit(Module).Body, '.');
     112  GenerateCommonBlockInterface(TModuleUnit(Module).Body, '.');
     113  EmitLn;
    105114  EmitLn('implementation');
    106115  EmitLn;
    107   EmitLn('end.');
     116  GenerateCommonBlockImplementation(TModuleUnit(Module).Body, '.');
     117  EmitLn('.');
    108118end;
    109119
     
    196206var
    197207  I: Integer;
     208begin
     209  for I := 0 to Functions.Count - 1 do
     210    GenerateFunction(TFunction(Functions[I]));
     211end;
     212
     213procedure TProducerPascal.GenerateFunction(AFunction: TFunction);
     214var
     215  I: Integer;
    198216  P: Integer;
    199217  Line: string;
    200218begin
    201   for I := 0 to Functions.Count - 1 do
    202   with TFunction(Functions[I]) do
     219  with AFunction do
    203220  if not Internal then
    204221  begin
     222    GenerateFunctionHead(AFunction);
     223    GenerateBeginEnd(Code);
     224    EmitLn(';');
     225    EmitLn;
     226  end;
     227end;
     228
     229procedure TProducerPascal.GenerateFunctionHead(AFunction: TFunction);
     230var
     231  Line: string;
     232  P: Integer;
     233begin
     234  with AFunction do begin
    205235    if FunctionType = ftFunction then
    206236      Line := 'function ' + Name
     
    218248      Line := Line + ': ' + ResultType.Name;
    219249    EmitLn(Line + ';');
    220     GenerateBeginEnd(Code);
    221     EmitLn(';');
    222     EmitLn;
    223250  end;
    224251end;
     
    232259    Inc(Indetation);
    233260    for I := 0 to Constants.Count - 1 do
    234     with TConstant(Constants[I]) do
    235     if not System then begin
    236       //Emit(Name + ': ');
    237       //if Assigned(ValueType) then Emit(ValueType.Name);
    238       //Emit(' = ' + Value + ';');
    239     end;
     261      GenerateConstant(Constants[I]);
    240262    Dec(Indetation);
    241263    Emit('');
     264  end;
     265end;
     266
     267procedure TProducerPascal.GenerateConstant(Constant: TConstant);
     268begin
     269  with Constant do begin
     270    Emit(Name);
     271    //if Assigned(ValueType) then Emit(': ' + ValueType.Name);
     272    EmitLn(' = ' + Value + ';');
    242273  end;
    243274end;
     
    266297  Inc(Indetation);
    267298  for I := 0 to Variables.Count - 1 do
    268   with TVariable(Variables[I]) do
     299    GenerateVariable(TVariable(Variables[I]));
     300  Dec(Indetation);
     301  EmitLn;
     302end;
     303
     304procedure TProducerPascal.GenerateVariable(Variable: TVariable);
     305begin
     306  with Variable do
    269307    EmitLn(Name + ': ' + ValueType.Name + ';');
    270   Dec(Indetation);
    271   EmitLn;
    272308end;
    273309
     
    362398end;
    363399
    364 procedure TProducerPascal.GenerateCommonBlock(CommonBlock: TCommonBlock; LabelPrefix: string);
     400procedure TProducerPascal.GenerateCommonBlockInterface(CommonBlock: TCommonBlock; LabelPrefix: string);
    365401var
    366402  I: Integer;
    367403  LabelName: string;
    368 begin
     404  Section: TCommonBlockSection;
     405begin
     406  Inc(Indetation);
    369407  with CommonBlock do begin
    370     GenerateTypes(Types);
    371     GenerateFunctions(Functions);
    372     GenerateConstants(Constants);
    373     GenerateVariableList(Variables);
     408    for I := 0 to Order.Count - 1 do begin
     409      if (Order[I] is TType) and (TType(Order[I]).Exported) then begin
     410        if Section <> cbsType then begin
     411          EmitLn;
     412          Dec(Indetation);
     413          EmitLn('type');
     414          Inc(Indetation);
     415        end;
     416        Emit(TType(Order[I]).Name + ' = ');
     417        GenerateType(TType(Order[I]));
     418        EmitLn(';');
     419        Section := cbsType;
     420      end else
     421      if (Order[I] is TVariable) and (TVariable(Order[I]).Exported) then begin
     422        if Section <> cbsVariable then begin
     423          EmitLn;
     424          Dec(Indetation);
     425          EmitLn('var');
     426          Inc(Indetation);
     427        end;
     428        GenerateVariable(TVariable(Order[I]));
     429        Section := cbsVariable;
     430      end else
     431      if (Order[I] is TConstant) and (TConstant(Order[I]).Exported) then begin
     432        if Section <> cbsConstant then begin
     433          EmitLn;
     434          Dec(Indetation);
     435          EmitLn('const');
     436          Inc(Indetation);
     437        end;
     438        GenerateConstant(TConstant(Order[I]));
     439        Section := cbsConstant;
     440      end else
     441      if (Order[I] is TFunction) and (TFunction(Order[I]).Exported) then begin
     442        GenerateFunctionHead(TFunction(Order[I]));
     443      end;
     444    end;
     445  end;
     446  Dec(Indetation);
     447end;
     448
     449procedure TProducerPascal.GenerateCommonBlockImplementation(
     450  CommonBlock: TCommonBlock; LabelPrefix: string);
     451var
     452  I: Integer;
     453  LabelName: string;
     454  Section: TCommonBlockSection;
     455begin
     456  with CommonBlock do begin
     457    for I := 0 to Order.Count - 1 do begin
     458      if (Order[I] is TType) and (not TType(Order[I]).Exported) then begin
     459        if Section <> cbsType then begin
     460          EmitLn;
     461          EmitLn('type');
     462        end;
     463        Emit(TType(Order[I]).Name + ' = ');
     464        GenerateType(TType(Order[I]));
     465        EmitLn(';');
     466        Section := cbsType;
     467      end else
     468      if (Order[I] is TVariable) and (not TVariable(Order[I]).Exported) then begin
     469        if Section <> cbsVariable then begin
     470          EmitLn;
     471          EmitLn('var');
     472        end;
     473        GenerateVariable(TVariable(Order[I]));
     474        Section := cbsVariable;
     475      end else
     476      if (Order[I] is TConstant) and (not TConstant(Order[I]).Exported) then begin
     477        if Section <> cbsConstant then begin
     478          EmitLn;
     479          EmitLn('const');
     480        end;
     481        GenerateConstant(TConstant(Order[I]));
     482        Section := cbsConstant;
     483      end else
     484      if Order[I] is TFunction then begin
     485        GenerateFunction(TFunction(Order[I]));
     486      end;
     487    end;
    374488    GenerateBeginEnd(Code);
    375489  end;
Note: See TracChangeset for help on using the changeset viewer.