Ignore:
Timestamp:
Jan 16, 2018, 2:44:19 PM (7 years ago)
Author:
chronos
Message:
  • Added: Support for multiple value types. String and Integer are supported for the start.
  • Added: "var" keyword for compile time definition of variable and its type.
  • Added: Increment function for integer values.
File:
1 edited

Legend:

Unmodified
Added
Removed
  • branches/easy compiler/USourceCode.pas

    r140 r141  
    1818  end;
    1919
     20  { TSourceValue }
     21
     22  TSourceValue = class
     23    procedure Assign(Source: TSourceValue); virtual;
     24  end;
     25
     26  TSourceValueClass = class of TSourceValue;
     27
     28  { TSourceType }
     29
     30  TSourceType = class
     31    Name: string;
     32    ValueClass: TSourceValueClass;
     33  end;
     34
     35  { TSourceTypes }
     36
     37  TSourceTypes = class(TObjectList)
     38    function AddNew(Name: string; ClassType: TSourceValueClass): TSourceType;
     39    function Search(Name: string): TSourceType;
     40  end;
     41
     42  { TSourceVariable }
     43
    2044  TSourceVariable = class
    2145    Name: string;
     46    ValueType: TSourceType;
    2247  end;
    2348
     
    2550
    2651  TSourceVariables = class(TObjectList)
    27     function AddNew(Name: string = ''): TSourceVariable;
     52    function AddNew(Name: string; ValueType: TSourceType): TSourceVariable;
    2853    function Search(Name: string): TSourceVariable;
    2954  end;
     
    3358  end;
    3459
     60  { TSourceValueString }
     61
     62  TSourceValueString = class(TSourceValue)
     63    Value: string;
     64    procedure Assign(Source: TSourceValue); override;
     65  end;
     66
     67  { TSourceValueInteger }
     68
     69  TSourceValueInteger = class(TSourceValue)
     70    Value: Integer;
     71    procedure Assign(Source: TSourceValue); override;
     72  end;
     73
    3574  TSourceConstant = class
    3675    Name: string;
    37     Value: string;
    38   end;
    39 
    40   TSourceParameterKind = (pkString, pkVariable);
     76    Value: TSourceValue;
     77  end;
     78
     79  TSourceParameterKind = (pkString, pkVariable, pkType);
    4180
    4281  TSourceFunctionParameter = class
     
    68107
    69108  TSourceConstants = class(TObjectList)
    70     function AddNew(Value: string; Name: string = ''): TSourceConstant;
     109    function AddNewString(Value: string; Name: string = ''): TSourceConstant;
     110    function AddNewInteger(Value: Integer; Name: string = ''): TSourceConstant;
     111    function Search(Name: string): TSourceConstant;
    71112  end;
    72113
     
    89130    procedure InitFunctions;
    90131  public
     132    Types: TSourceTypes;
    91133    Variables: TSourceVariables;
    92134    Constants: TSourceConstants;
     
    100142implementation
    101143
    102 { TSourceFunctions }
    103 
    104 function TSourceFunctions.AddNew(Name: string): TSourceFunction;
    105 begin
    106   Result := TSourceFunction.Create;
     144{ TSourceTypes }
     145
     146function TSourceTypes.AddNew(Name: string; ClassType: TSourceValueClass): TSourceType;
     147begin
     148  Result := TSourceType.Create;
    107149  Result.Name := Name;
    108   Add(Result);
    109 end;
    110 
    111 function TSourceFunctions.Search(Name: string): TSourceFunction;
    112 var
    113   Item: TSourceFunction;
     150  Result.ValueClass := ClassType;
     151  Add(Result);
     152end;
     153
     154function TSourceTypes.Search(Name: string): TSourceType;
     155var
     156  Item: TSourceType;
    114157begin
    115158  Result := nil;
     
    121164end;
    122165
     166{ TSourceValue }
     167
     168procedure TSourceValue.Assign(Source: TSourceValue);
     169begin
     170  raise Exception.Create('Value assign not implemented');
     171end;
     172
     173{ TSourceValueInteger }
     174
     175procedure TSourceValueInteger.Assign(Source: TSourceValue);
     176begin
     177  if Source is TSourceValueInteger then
     178  Value := TSourceValueInteger(Source).Value
     179  else raise Exception.Create('Type for assignment not matches');
     180end;
     181
     182{ TSourceValueString }
     183
     184procedure TSourceValueString.Assign(Source: TSourceValue);
     185begin
     186  if Source is TSourceValueString then
     187  Value := TSourceValueString(Source).Value
     188  else raise Exception.Create('Type for assignment not matches');
     189end;
     190
     191{ TSourceFunctions }
     192
     193function TSourceFunctions.AddNew(Name: string): TSourceFunction;
     194begin
     195  Result := TSourceFunction.Create;
     196  Result.Name := Name;
     197  Add(Result);
     198end;
     199
     200function TSourceFunctions.Search(Name: string): TSourceFunction;
     201var
     202  Item: TSourceFunction;
     203begin
     204  Result := nil;
     205  for Item in Self do
     206  if Item.Name = Name then begin
     207    Result := Item;
     208    Break;
     209  end;
     210end;
     211
    123212{ TSourceFunction }
    124213
     
    147236{ TSourceVariables }
    148237
    149 function TSourceVariables.AddNew(Name: string): TSourceVariable;
     238function TSourceVariables.AddNew(Name: string;ValueType: TSourceType): TSourceVariable;
    150239begin
    151240  Result := TSourceVariable.Create;
    152241  Result.Name := Name;
     242  Result.ValueType := ValueType;
    153243  Add(Result);
    154244end;
     
    156246function TSourceVariables.Search(Name: string): TSourceVariable;
    157247var
    158   Variable: TSourceVariable;
     248  Item: TSourceVariable;
    159249begin
    160250  Result := nil;
    161   for Variable in Self do
    162   if Variable.Name = Name then begin
    163     Result := Variable;
     251  for Item in Self do
     252  if Item.Name = Name then begin
     253    Result := Item;
    164254    Break;
    165255  end;
     
    168258{ TSourceConstants }
    169259
    170 function TSourceConstants.AddNew(Value: string; Name: string): TSourceConstant;
     260function TSourceConstants.AddNewString(Value: string; Name: string
     261  ): TSourceConstant;
    171262begin
    172263  Result := TSourceConstant.Create;
    173   Result.Value := Value;
     264  Result.Value := TSourceValueString.Create;
     265  TSourceValueString(Result.Value).Value := Value;
    174266  Result.Name := '';
    175267  Add(Result);
     268end;
     269
     270function TSourceConstants.AddNewInteger(Value: Integer; Name: string
     271  ): TSourceConstant;
     272begin
     273  Result := TSourceConstant.Create;
     274  Result.Value := TSourceValueInteger.Create;
     275  TSourceValueInteger(Result.Value).Value := Value;
     276  Result.Name := '';
     277  Add(Result);
     278end;
     279
     280function TSourceConstants.Search(Name: string): TSourceConstant;
     281var
     282  Item: TSourceConstant;
     283begin
     284  Result := nil;
     285  for Item in Self do
     286  if Item.Name = Name then begin
     287    Result := Item;
     288    Break;
     289  end;
    176290end;
    177291
     
    191305begin
    192306  Functions.Clear;
     307
     308  // Init types
     309  Types.AddNew('Integer', TSourceValueInteger);
     310  Types.AddNew('String', TSourceValueString);
     311
     312  // Init functions
    193313  Funct := Functions.AddNew('print');
    194314  Funct.AddParameter('Text', pkString);
     315
    195316  Funct := Functions.AddNew('println');
    196317  Funct.AddParameter('Text', pkString);
     318
     319  Funct := Functions.AddNew('var');
     320  Funct.AddParameter('Variable', pkVariable);
     321  Funct.AddParameter('Type', pkType);
     322
    197323  Funct := Functions.AddNew('assign');
    198324  Funct.AddParameter('Destination', pkVariable);
    199325  Funct.AddParameter('Source', pkString);
     326
    200327  Funct := Functions.AddNew('inputln');
    201328  Funct.AddParameter('Text', pkVariable);
     329
     330  Funct := Functions.AddNew('increment');
     331  Funct.AddParameter('Variable', pkVariable);
     332  Funct.AddParameter('Addition', pkString);
    202333end;
    203334
    204335constructor TSourceCode.Create;
    205336begin
     337  Types := TSourceTypes.Create;
    206338  Variables := TSourceVariables.Create;
    207339  Constants := TSourceConstants.Create;
     340  Functions := TSourceFunctions.Create;
    208341  Instructions := TSourceInstructions.Create;
    209   Functions := TSourceFunctions.Create;
    210342  InitFunctions;
    211343end;
     
    213345destructor TSourceCode.Destroy;
    214346begin
     347  Instructions.Free;
    215348  Functions.Free;
    216349  Variables.Free;
    217350  Constants.Free;
    218   Instructions.Free;
     351  Types.Free;
    219352  inherited Destroy;
    220353end;
Note: See TracChangeset for help on using the changeset viewer.