Ignore:
Timestamp:
Feb 6, 2017, 12:11:54 AM (8 years ago)
Author:
chronos
Message:
  • Modified: Better parsing of expression and commands.
File:
1 edited

Legend:

Unmodified
Added
Removed
  • branches/interpreter/Source3.pas

    r98 r99  
    66
    77type
     8  PBeginEnd = ^TBeginEnd;
     9  PAssignment = ^TAssignment;
     10  PVariable = ^TVariable;
     11  PConstant = ^TConstant;
     12  PIfThenElse = ^TIfThenElse;
     13  PWhileDo = ^TWhileDo;
     14  PExpression = ^TExpression;
     15  PExecution = ^TExecution;
     16
    817  TBaseType = (btBoolean, btInteger, btChar, btShortString, btArray);
    918
     
    2635    Name: string;
    2736    DataType: PType;
    28     case Integer of
    29       0: (ValueChar: Char);
    30       1: (ValueInteger: Integer);
    31       2: (ValueString: ShortString);
    32       3: (ValueBoolean: Boolean);
    33   end;
    34   PVariable = ^TVariable;
     37    Index: Integer;
     38  end;
    3539
    3640  { TVariables }
     
    4145    function GetByName(Name: string): PVariable;
    4246  end;
     47  PVariables = ^TVariables;
    4348
    4449  TConstant = record
    4550    Name: string;
    4651    DataType: PType;
    47     case Integer of
    48       0: (ValueChar: Char);
    49       1: (ValueInteger: Integer);
    50       2: (ValueString: ShortString);
    51       3: (ValueBoolean: Boolean);
    52   end;
    53   PConstant = ^TConstant;
     52    Index: Integer;
     53    case TBaseType of
     54      btChar: (ValueChar: Char);
     55      btInteger: (ValueInteger: Integer);
     56      btShortString: (ValueString: ShortString);
     57      btBoolean: (ValueBoolean: Boolean);
     58  end;
     59
     60  { TConstants }
     61
     62  TConstants = record
     63    Items: array of TConstant;
     64    procedure Add(Constant: TConstant);
     65    function GetByName(Name: string): PConstant;
     66  end;
     67  PConstants = ^TConstants;
    5468
    5569  TFunctionParameter = record
     
    7286    CmdType: TCmdType;
    7387    Ptr: Pointer;
    74 {    case Integer of
    75       0: (WhileDo: TWhileDo);
    76       1: (IfThenElse: TIfThenElse);
    77       2: (BeginEnd: TBeginEnd);
    78       3: (Assignment: TAssignment);
    79       4: (Execution: TExecution);
    80  } end;
     88    case Integer of
     89      0: (WhileDo: PWhileDo);
     90      1: (IfThenElse: PIfThenElse);
     91      2: (BeginEnd: PBeginEnd);
     92      3: (Assignment: PAssignment);
     93      4: (Execution: PExecution);
     94  end;
    8195  PCommand = ^TCommand;
     96
     97  { TBeginEnd }
    8298
    8399  TBeginEnd = record
    84100    Commands: array of TCommand;
    85   end;
    86   PBeginEnd = ^TBeginEnd;
     101    procedure Add;
     102    function GetLast: PCommand;
     103  end;
     104
     105  TReadType = (rtVariable, rtConstant, rtExpression, rtFunctionCall);
     106  TGetValue = record
     107    ReadType: TReadType;
     108    case TReadType of
     109      rtVariable: (Variable: PVariable);
     110      rtConstant: (Constant: PConstant);
     111      rtExpression: (Expression: PExpression);
     112      rtFunctionCall: (FunctionCall: PExecution);
     113  end;
     114  PGetValue = ^TGetValue;
     115
     116  TExpOperand = (eoNone, eoAdd, eoSubtract, eoAnd, eoOr, eoNot);
    87117
    88118  TExpression = record
    89   end;
    90   PExpression = ^TExpression;
     119    Operand: TExpOperand;
     120    Items: array of TGetValue;
     121  end;
    91122
    92123  TAssignment = record
    93124    Destination: PVariable;
    94     Source: TExpression;
    95   end;
    96   PAssignment = ^TAssignment;
     125    Source: TGetValue;
     126  end;
    97127
    98128  TIfThenElse = record
    99     Condition: TExpression;
     129    Condition: TGetValue;
    100130    DoThen: TCommand;
    101131    DoElse: TCommand;
    102132  end;
    103   PIfThenElse = ^TIfThenElse;
    104133
    105134  TWhileDo = record
    106     Condition: TExpression;
     135    Condition: TGetValue;
    107136    Command: TCommand;
    108137  end;
    109   PWhileDo = ^TWhileDo;
    110138
    111139  { TFunction }
     
    130158
    131159  TExecutionParams = record
    132     Items: array of TExpression;
     160    Items: array of TGetValue;
    133161  end;
    134162
     
    137165    Parameters: TExecutionParams;
    138166  end;
    139   PExecution = ^TExecution;
    140167
    141168  TProgramCode = record
    142169    Name: string;
    143170    Variables: TVariables;
     171    Constants: TConstants;
    144172    Types: TTypes;
    145173    Functions: TFunctions;
     
    180208end;
    181209
    182 { TVariables }
    183 
    184 procedure TVariables.Add(Variable: TVariable);
    185 begin
    186   SetLength(Items, Length(Items) + 1);
    187   Items[Length(Items) - 1] := Variable;
    188 end;
    189 
    190 function TVariables.GetByName(Name: string): PVariable;
     210{ TBeginEnd }
     211
     212procedure TBeginEnd.Add;
     213begin
     214  SetLength(Commands, Length(Commands) + 1);
     215end;
     216
     217function TBeginEnd.GetLast: PCommand;
     218begin
     219  Result := @Commands[Length(Commands) - 1];
     220end;
     221
     222{ TConstants }
     223
     224procedure TConstants.Add(Constant: TConstant);
     225begin
     226  SetLength(Items, Length(Items) + 1);
     227  Items[Length(Items) - 1] := Constant;
     228  Items[Length(Items) - 1].Index := Length(Items) - 1;
     229end;
     230
     231function TConstants.GetByName(Name: string): PConstant;
    191232var
    192233  I: Integer;
     
    198239end;
    199240
    200 { TFunctionParameters }
    201 
    202 procedure TFunctionParameters.Add(Param: TFunctionParameter);
    203 begin
    204   SetLength(Items, Length(Items) + 1);
    205   Items[Length(Items) - 1] := Param;
    206 end;
    207 
    208 { TTypes }
    209 
    210 procedure TTypes.Add(DataType: TType);
    211 begin
    212   SetLength(Items, Length(Items) + 1);
    213   Items[Length(Items) - 1] := DataType;
    214 end;
    215 
    216 function TTypes.GetByName(Name: string): PType;
     241{ TVariables }
     242
     243procedure TVariables.Add(Variable: TVariable);
     244begin
     245  SetLength(Items, Length(Items) + 1);
     246  Items[Length(Items) - 1] := Variable;
     247  Items[Length(Items) - 1].Index := Length(Items) - 1;
     248end;
     249
     250function TVariables.GetByName(Name: string): PVariable;
    217251var
    218252  I: Integer;
     
    224258end;
    225259
    226 function TTypes.GetLast: PType;
    227 begin
    228   Result := @Items[Length(Items) - 1];
    229 end;
    230 
    231 { TFunctions }
    232 
    233 procedure TFunctions.Add(Func: TFunction);
    234 begin
    235   SetLength(Items, Length(Items) + 1);
    236   Items[Length(Items) - 1] := Func;
    237 end;
    238 
    239 function TFunctions.GetByName(Name: string): PFunction;
     260{ TFunctionParameters }
     261
     262procedure TFunctionParameters.Add(Param: TFunctionParameter);
     263begin
     264  SetLength(Items, Length(Items) + 1);
     265  Items[Length(Items) - 1] := Param;
     266end;
     267
     268{ TTypes }
     269
     270procedure TTypes.Add(DataType: TType);
     271begin
     272  SetLength(Items, Length(Items) + 1);
     273  Items[Length(Items) - 1] := DataType;
     274end;
     275
     276function TTypes.GetByName(Name: string): PType;
    240277var
    241278  I: Integer;
     
    247284end;
    248285
     286function TTypes.GetLast: PType;
     287begin
     288  Result := @Items[Length(Items) - 1];
     289end;
     290
     291{ TFunctions }
     292
     293procedure TFunctions.Add(Func: TFunction);
     294begin
     295  SetLength(Items, Length(Items) + 1);
     296  Items[Length(Items) - 1] := Func;
     297end;
     298
     299function TFunctions.GetByName(Name: string): PFunction;
     300var
     301  I: Integer;
     302begin
     303  I := 0;
     304  while (I < Length(Items)) and (Items[I].Name <> Name) do I := I + 1;
     305  if I < Length(Items) then Result := @Items[I]
     306    else Result := nil;
     307end;
     308
    249309function TFunctions.GetLast: PFunction;
    250310begin
Note: See TracChangeset for help on using the changeset viewer.