Ignore:
Timestamp:
Nov 25, 2020, 12:18:45 AM (4 years ago)
Author:
chronos
Message:
  • Added: Support for more expression operators.
  • Added: Support for brackets in expressions.
File:
1 edited

Legend:

Unmodified
Added
Removed
  • branches/interpreter2/USource.pas

    r221 r222  
    242242  end;
    243243
    244   TExpressionOperator = (eoAdd, eoSub, eoMultiply, eoDivide, eoModulo, eoAnd, eoXor,
    245     eoOr, eoShl, eoShr, eoEqual, eoNotEqual);
     244  TExpressionOperator = (eoNone, eoAdd, eoSub, eoMultiply, eoDivide, eoIntDivide,
     245    eoModulo, eoAnd, eoXor, eoOr, eoShl, eoShr, eoEqual, eoNotEqual, eoLesser,
     246    eoHigher, eoLesserOrEqual, eoHigherOrEqual, eoNot);
    246247
    247248  { TExpression }
     
    270271  end;
    271272
    272   TExpressionOperandType = (otVariableRef, otConstantRef, otConstantDirect, otFunctionCall);
     273  TExpressionOperandType = (otVariableRef, otConstantRef, otConstantDirect,
     274    otFunctionCall);
    273275
    274276  { TExpressionOperand }
     
    288290    function GetType: TType; override;
    289291    constructor Create;
     292    destructor Destroy; override;
     293  end;
     294
     295  { TExpressionBrackets }
     296
     297  TExpressionBrackets = class(TExpression)
     298    Expression: TExpression;
     299    procedure GetValue(Index: Integer; out Value); override;
     300    function GetField(Index: Integer): TField; override;
     301    procedure SetValue(Index: Integer; var Value); override;
     302    function GetType: TType; override;
    290303    destructor Destroy; override;
    291304  end;
     
    442455      'Integer', 'Float', 'Color', 'Time', 'Date', 'DateTime', 'Enumeration',
    443456      'Reference');
     457    ExpressionOperatorText: array[TExpressionOperator] of string = ('', '+',
     458      '-', '*', '/', 'div', 'mod', 'and', 'xor', 'or', 'shl',
     459      'shr', '=', '<>', '<', '>', '<=','>=', 'not');
     460    ExpressionOperatorFuncText: array[TExpressionOperator] of string = ('', '_Add',
     461      '_Sub', '_Mul', '_Div', '_IntDiv', '_Mod', '_And', '_Xor', '_Or', '_Shl',
     462      '_Shr', '_Equal', '_NotEqual', '_Lesser', '_Higher', '_LesserOrEqual',
     463      '_HigherOrEqual', '_Not');
     464
     465  function GetOperatorByName(Name: string): TExpressionOperator;
    444466
    445467
     
    452474  SYes = 'Yes';
    453475  SNo = 'No';
     476
     477function GetOperatorByName(Name: string): TExpressionOperator;
     478var
     479  I: TExpressionOperator;
     480begin
     481  Result := eoNone;
     482  for I := Succ(Low(TExpressionOperator)) to High(TExpressionOperator) do begin
     483    if ExpressionOperatorText[I] = Name then begin
     484      Result := I;
     485      Break;
     486    end;
     487  end;
     488end;
     489
     490{ TExpressionBrackets }
     491
     492procedure TExpressionBrackets.GetValue(Index: Integer; out Value);
     493begin
     494  if Index = 0 then begin
     495    TExpression(Value) := Expression;
     496  end
     497  else inherited;
     498end;
     499
     500function TExpressionBrackets.GetField(Index: Integer): TField;
     501begin
     502  if Index = 0 then Result := TField.Create(dtObject, 'Expression')
     503  else inherited;
     504end;
     505
     506procedure TExpressionBrackets.SetValue(Index: Integer; var Value);
     507begin
     508  if Index = 0 then begin
     509    Expression := TExpression(Value);
     510  end
     511  else inherited;
     512end;
     513
     514function TExpressionBrackets.GetType: TType;
     515begin
     516  Result := Expression.GetType;
     517end;
     518
     519destructor TExpressionBrackets.Destroy;
     520begin
     521  FreeAndNil(Expression);
     522  inherited;
     523end;
    454524
    455525{ TReturn }
     
    10171087function TExpressionOperation.GetFunctionName: string;
    10181088begin
    1019   if Operation = eoAdd then Result := '_Add'
    1020   else if Operation = eoSub then Result := '_Sub'
    1021   else if Operation = eoEqual then Result := '_Equal'
    1022   else if Operation = eoNotEqual then Result := '_NotEqual'
    1023   else raise Exception.Create('Unsupported operation type.');
     1089  Result := ExpressionOperatorFuncText[Operation];
    10241090end;
    10251091
Note: See TracChangeset for help on using the changeset viewer.