Ignore:
Timestamp:
Apr 17, 2020, 10:16:25 PM (5 years ago)
Author:
chronos
Message:
  • Added: Simple pascal code generator.
File:
1 edited

Legend:

Unmodified
Added
Removed
  • branches/interpreter2/UExecutor.pas

    r202 r203  
    1010type
    1111  TExecutorFunctions = class;
     12
     13  { TExecutorVariable }
    1214
    1315  TExecutorVariable = class
    1416    Variable: TVariable;
    1517    Value: TValue;
     18    constructor Create;
     19    destructor Destroy; override;
    1620  end;
    1721
     
    99103    procedure ExecuteWhileDo(Block: TExecutorBlock; WhileDo: TWhileDo);
    100104    procedure ExecuteForToDo(Block: TExecutorBlock; ForToDo: TForToDo);
    101     procedure ExecuteBlock(ParentBlock: TExecutorBlock;Block: TBlock);
     105    procedure ExecuteBlock(ParentBlock: TExecutorBlock; Block: TBlock);
    102106    function ExecuteFunctionCall(Block: TExecutorBlock; FunctionCall: TFunctionCall): TValue;
    103107    procedure ExecuteAssignment(Block: TExecutorBlock; Assignment: TAssignment);
     
    112116
    113117implementation
     118
     119{ TExecutorVariable }
     120
     121constructor TExecutorVariable.Create;
     122begin
     123  Value := TValue.Create;
     124end;
     125
     126destructor TExecutorVariable.Destroy;
     127begin
     128  Value.Free;
     129  inherited Destroy;
     130end;
    114131
    115132{ TExecutorType }
     
    429446      else ExecuteCommand(Block, IfThenElse.CommandElse);
    430447  end else raise Exception.Create('Expected boolean value.');
     448  Value.Free;
    431449end;
    432450
     
    441459      ExecuteCommand(Block, WhileDo.Command);
    442460    end else raise Exception.Create('Expected boolean value.');
     461    Value.Free;
    443462  end;
    444463end;
     
    446465procedure TExecutor.ExecuteForToDo(Block: TExecutorBlock; ForToDo: TForToDo);
    447466var
    448   Value: TValue;
    449467  Variable: TExecutorVariable;
    450468  Limit: TValue;
    451469begin
    452470  Variable := Block.GetVariable(ForToDo.VariableRef);
     471  Variable.Value.Free;
    453472  Variable.Value := ExecuteExpression(Block, ForToDo.ExpressionFrom);
    454473  Limit := ExecuteExpression(Block, ForToDo.ExpressionTo);
     
    458477    if TValueInteger(Variable.Value).Value > TValueInteger(Limit).Value then Break;
    459478  end;
     479  Limit.Free;
    460480end;
    461481
     
    486506    end;
    487507    Result := ExecutorFunction.Callback(Params);
     508    for I := 0 to FunctionCall.Params.Count - 1 do begin
     509      Params[I].Free;
     510    end;
    488511  end else raise Exception.Create('No executor for ' + FunctionCall.FunctionDef.Name + ' function.');
    489512end;
     
    503526    SetLength(Params, 1);
    504527    Params[0] := Value;
     528    Variable.Value.Free;
    505529    Variable.Value := ExecutorFunction.Callback(Params);
    506530  end else raise Exception('Assignment result type is ' + Variable.Variable.TypeRef.Name +
    507531    ' but value is ' + Assignment.Expression.GetType.Name + '.');
     532  Value.Free;
    508533end;
    509534
     
    543568  end;
    544569  Result := ExecutorFunction.Callback(Params);
     570  for I := 0 to Expression.Items.Count - 1 do begin
     571    Params[I].Free;
     572  end;
    545573end;
    546574
     
    548576  Expression: TExpressionOperand): TValue;
    549577begin
    550   if Assigned(Expression.VariableRef) then begin
    551     Result := Block.Variables.SearchByVariable(Expression.VariableRef).Value;
    552   end else
    553   if Assigned(Expression.ConstantRef) then begin
    554     Result := Expression.ConstantRef.Value;
    555   end else
    556   if Assigned(Expression.FunctionCall) then begin
    557     Result := ExecuteFunctionCall(Block, Expression.FunctionCall);
    558   end else raise Exception.Create('Unsupported exception operand type.');
     578  case Expression.OperandType of
     579    otFunctionCall: Result := ExecuteFunctionCall(Block, Expression.FunctionCall);
     580    otConstantDirect: Result := Expression.ConstantDirect.Value.Clone;
     581    otConstantRef: Result := Expression.ConstantRef.Value.Clone;
     582    otVariableRef: Result := Block.Variables.SearchByVariable(Expression.VariableRef).Value.Clone;
     583    else raise Exception.Create('Unsupported exception operand type.');
     584  end;
    559585end;
    560586
Note: See TracChangeset for help on using the changeset viewer.