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/Generators/UGeneratorCSharp.pas

    r221 r222  
    3131    procedure GenerateExpressionOperation(Block: TBlock; Expression: TExpressionOperation);
    3232    procedure GenerateExpressionOperand(Block: TBlock; Expression: TExpressionOperand);
     33    procedure GenerateExpressionBrackets(Block: TBlock; Expression: TExpressionBrackets);
    3334    procedure GenerateBreak(Block: TBlock; BreakCmd: TBreak);
    3435    procedure GenerateContinue(Block: TBlock; ContinueCmd: TContinue);
     
    3738    procedure GenerateValue(Value: TValue);
    3839  public
    39     Prog: TProgram;
    4040    procedure Generate; override;
    41   end;
     41    constructor Create; override;
     42  end;
     43
    4244
    4345implementation
     46
     47const
     48  ExpressionOperatorTextCSharp: array[TExpressionOperator] of string = ('', '+',
     49    '-', '*', '/', '/', '%', '&', '^', '|', '<<',
     50    '>>', '==', '!=', '<', '>', '<=','>=', '!');
    4451
    4552{ TGeneratorCSharp }
     
    144151  if Expression is TExpressionOperand then
    145152    GenerateExpressionOperand(Block, TExpressionOperand(Expression))
     153  else
     154  if Expression is TExpressionBrackets then
     155    GenerateExpressionBrackets(Block, TExpressionBrackets(Expression))
    146156  else raise Exception.Create('Unknown expression class.');
    147157end;
     
    154164  for I := 0 to Expression.Items.Count - 1 do begin
    155165    if I > 0 then begin
    156       if Expression.Operation = eoAdd then AddText(' + ')
    157       else if Expression.Operation = eoSub then AddText(' - ')
    158       else if Expression.Operation = eoEqual then AddText(' == ')
    159       else if Expression.Operation = eoNotEqual then AddText(' != ');
     166      AddText(' ' + ExpressionOperatorTextCSharp[Expression.Operation] + ' ');
    160167    end;
    161168    GenerateExpression(Block, TExpression(Expression.Items[I]));
     
    173180    else raise Exception.Create('Unsupported exception operand type.');
    174181  end;
     182end;
     183
     184procedure TGeneratorCSharp.GenerateExpressionBrackets(Block: TBlock;
     185  Expression: TExpressionBrackets);
     186begin
     187  AddText('(');
     188  GenerateExpression(Block, Expression.Expression);
     189  AddText(')');
    175190end;
    176191
     
    233248
    234249procedure TGeneratorCSharp.GenerateBlock(ParentBlock: TBlock; Block: TBlock);
    235 var
    236   I: Integer;
    237250begin
    238251  if Block.BeginEnd.Commands.Count > 0 then begin
     
    353366end;
    354367
     368constructor TGeneratorCSharp.Create;
     369begin
     370  inherited;
     371  Name := 'CSharp';
     372  FileExt := '.cs';
     373end;
     374
    355375end.
    356376
Note: See TracChangeset for help on using the changeset viewer.