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.
Location:
branches/interpreter2/Generators
Files:
4 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
  • branches/interpreter2/Generators/UGeneratorPascal.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);
    3536    procedure GenerateValue(Value: TValue);
    3637  public
    37     Prog: TProgram;
    3838    procedure Generate; override;
     39    constructor Create; override;
    3940  end;
    4041
     
    134135  if Expression is TExpressionOperand then
    135136    GenerateExpressionOperand(Block, TExpressionOperand(Expression))
     137  else
     138  if Expression is TExpressionBrackets then
     139    GenerateExpressionBrackets(Block, TExpressionBrackets(Expression))
    136140  else raise Exception.Create('Unknown expression class.');
    137141end;
     
    145149    if I > 0 then begin
    146150      AddText(' ');
    147       if Expression.Operation = eoAdd then AddText('+')
    148       else if Expression.Operation = eoSub then AddText('-')
    149       else if Expression.Operation = eoEqual then AddText('=')
    150       else if Expression.Operation = eoNotEqual then AddText('<>');
     151      AddText(ExpressionOperatorText[Expression.Operation]);
    151152      AddText(' ');
    152153    end;
     
    165166    else raise Exception.Create('Unsupported exception operand type.');
    166167  end;
     168end;
     169
     170procedure TGeneratorPascal.GenerateExpressionBrackets(Block: TBlock;
     171  Expression: TExpressionBrackets);
     172begin
     173  AddText('(');
     174  GenerateExpression(Block, Expression.Expression);
     175  AddText(')');
    167176end;
    168177
     
    313322end;
    314323
     324constructor TGeneratorPascal.Create;
     325begin
     326  inherited;
     327  Name := 'Pascal';
     328  FileExt := '.pas';
     329end;
     330
    315331end.
    316332
  • branches/interpreter2/Generators/UGeneratorPhp.pas

    r221 r222  
    3030    procedure GenerateExpressionOperation(Block: TBlock; Expression: TExpressionOperation);
    3131    procedure GenerateExpressionOperand(Block: TBlock; Expression: TExpressionOperand);
     32    procedure GenerateExpressionBrackets(Block: TBlock; Expression: TExpressionBrackets);
    3233    procedure GenerateBreak(Block: TBlock; BreakCmd: TBreak);
    3334    procedure GenerateReturn(Block: TBlock; Return: TReturn);
     
    3536    procedure GenerateValue(Value: TValue);
    3637  public
    37     Prog: TProgram;
    3838    procedure Generate; override;
    39   end;
     39    constructor Create; override;
     40  end;
     41
    4042
    4143implementation
     44
     45const
     46  ExpressionOperatorTextPhp: array[TExpressionOperator] of string = ('', '+',
     47    '-', '*', '/', '/', '%', 'and', 'xor', 'or', '<<',
     48    '>>', '==', '!=', '<', '>', '<=','>=', '!');
    4249
    4350{ TGeneratorPhp }
     
    142149  if Expression is TExpressionOperand then
    143150    GenerateExpressionOperand(Block, TExpressionOperand(Expression))
     151  else
     152  if Expression is TExpressionBrackets then
     153    GenerateExpressionBrackets(Block, TExpressionBrackets(Expression))
    144154  else raise Exception.Create('Unknown expression class.');
    145155end;
     
    156166        else AddText(' + ');
    157167      end
    158       else if Expression.Operation = eoSub then AddText(' - ')
    159       else if Expression.Operation = eoEqual then AddText(' == ')
    160       else if Expression.Operation = eoNotEqual then AddText(' != ');
     168      else AddText(' ' + ExpressionOperatorTextPhp[Expression.Operation] + ' ');
    161169    end;
    162170    GenerateExpression(Block, TExpression(Expression.Items[I]));
     
    174182    else raise Exception.Create('Unsupported exception operand type.');
    175183  end;
     184end;
     185
     186procedure TGeneratorPhp.GenerateExpressionBrackets(Block: TBlock;
     187  Expression: TExpressionBrackets);
     188begin
     189  AddText('(');
     190  GenerateExpression(Block, Expression.Expression);
     191  AddText(')');
    176192end;
    177193
     
    290306end;
    291307
     308constructor TGeneratorPhp.Create;
     309begin
     310  inherited;
     311  Name := 'PHP';
     312  FileExt := '.php';
     313end;
     314
    292315end.
    293316
  • branches/interpreter2/Generators/UGeneratorXml.pas

    r221 r222  
    1616    procedure GenerateNode(SourceNode: TSourceNode);
    1717  public
    18     Prog: TProgram;
    1918    procedure Generate; override;
     19    constructor Create; override;
    2020  end;
    2121
     
    7373end;
    7474
     75constructor TGeneratorXml.Create;
     76begin
     77  inherited;
     78  Name := 'XML';
     79  FileExt := '.xml';
     80end;
     81
    7582end.
    7683
Note: See TracChangeset for help on using the changeset viewer.