Changeset 235 for branches/xpascal/Executor.pas
- Timestamp:
- Jun 27, 2023, 10:09:21 AM (17 months ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
branches/xpascal/Executor.pas
r233 r235 163 163 procedure ExecuteBlock(ParentBlock: TExecutorBlock; Block: TBlock; ExistingBlock: TExecutorBlock = nil); 164 164 function ExecuteFunctionCall(Block: TExecutorBlock; FunctionCall: TFunctionCall): TValue; 165 function ExecuteProcedureCall(Block: TExecutorBlock; ProcedureCall: TProcedureCall): TValue;165 procedure ExecuteProcedureCall(Block: TExecutorBlock; ProcedureCall: TProcedureCall); 166 166 procedure ExecuteAssignment(Block: TExecutorBlock; Assignment: TAssignment); 167 167 function ExecuteExpression(Block: TExecutorBlock; Expression: TExpression): TValue; … … 195 195 begin 196 196 I := 0; 197 while (I < Count) and ( TExecutorProcedure(Items[I]).ProcedureDef <> ProcedureDef) do Inc(I);198 if I < Count then Result := TExecutorProcedure(Items[I])197 while (I < Count) and (Items[I].ProcedureDef <> ProcedureDef) do Inc(I); 198 if I < Count then Result := Items[I] 199 199 else Result := nil; 200 200 end; … … 275 275 begin 276 276 I := 0; 277 while (I < Count) and ( TExecutorType(Items[I]).TypeRef <> TypeRef) do Inc(I);278 if I < Count then Result := TExecutorType(Items[I])277 while (I < Count) and (Items[I].TypeRef <> TypeRef) do Inc(I); 278 if I < Count then Result := Items[I] 279 279 else Result := nil; 280 280 end; … … 295 295 begin 296 296 I := 0; 297 while (I < Count) and ( TExecutorFunction(Items[I]).FunctionDef <> FunctionDef) do Inc(I);298 if I < Count then Result := TExecutorFunction(Items[I])297 while (I < Count) and (Items[I].FunctionDef <> FunctionDef) do Inc(I); 298 if I < Count then Result := Items[I] 299 299 else Result := nil; 300 300 end; … … 314 314 begin 315 315 I := 0; 316 while (I < Count) and ( TExecutorVariable(Items[I]).Variable <> Variable) do Inc(I);317 if I < Count then Result := TExecutorVariable(Items[I])316 while (I < Count) and (Items[I].Variable <> Variable) do Inc(I); 317 if I < Count then Result := Items[I] 318 318 else Result := nil; 319 319 end; … … 634 634 J: Integer; 635 635 ExecutorFunction: TExecutorFunction; 636 ExecutorProcedure: TExecutorProcedure; 636 637 ExecutorType: TExecutorType; 637 638 begin … … 723 724 end; 724 725 end; 725 for I := 0 to Block.Variables.Count - 1 do 726 727 for I := 0 to Block.Variables.Count - 1 do begin 726 728 ExecutorBlock.Variables.AddNew(TVariable(Block.Variables[I])); 729 end; 730 727 731 for I := 0 to Block.Functions.Count - 1 do begin 728 732 ExecutorFunction := ExecutorBlock.Functions.AddNew(TFunction(Block.Functions[I])); 729 if ExecutorFunction.FunctionDef.Name = 'Write' then begin730 ExecutorFunction.Callback := ExecuteWrite;731 end else732 if ExecutorFunction.FunctionDef.Name = 'WriteLn' then begin733 ExecutorFunction.Callback := ExecuteWriteLn;734 end else735 if ExecutorFunction.FunctionDef.Name = 'Read' then begin736 ExecutorFunction.Callback := ExecuteRead;737 end else738 if ExecutorFunction.FunctionDef.Name = 'ReadLn' then begin739 ExecutorFunction.Callback := ExecuteReadLn;740 end else741 733 if ExecutorFunction.FunctionDef.Name = 'IntToStr' then begin 742 734 ExecutorFunction.Callback := ExecuteIntToStr; … … 750 742 if ExecutorFunction.FunctionDef.Name = 'StrToBool' then begin 751 743 ExecutorFunction.Callback := ExecuteStrToBool; 744 end; 745 end; 746 747 for I := 0 to Block.Procedures.Count - 1 do begin 748 ExecutorProcedure := ExecutorBlock.Procedures.AddNew(TProcedure(Block.Procedures[I])); 749 if ExecutorProcedure.ProcedureDef.Name = 'Write' then begin 750 ExecutorProcedure.Callback := ExecuteWrite; 751 end else 752 if ExecutorProcedure.ProcedureDef.Name = 'WriteLn' then begin 753 ExecutorProcedure.Callback := ExecuteWriteLn; 754 end else 755 if ExecutorProcedure.ProcedureDef.Name = 'Read' then begin 756 ExecutorProcedure.Callback := ExecuteRead; 757 end else 758 if ExecutorProcedure.ProcedureDef.Name = 'ReadLn' then begin 759 ExecutorProcedure.Callback := ExecuteReadLn; 752 760 end; 753 761 end; … … 946 954 Result := ExecutorFunction.Callback(Params); 947 955 for I := 0 to FunctionCall.Params.Count - 1 do begin 948 //if FunctionCall.Params[I].949 956 Params[I].Free; 950 957 end; 951 958 end else begin 952 959 InitExecutorBlock(ExecutorFunction.Block, FunctionCall.FunctionDef.Block); 960 961 // Clean variables 953 962 for I := 0 to FunctionCall.Params.Count - 1 do begin 954 Variable := FunctionCall.FunctionDef.Block.Variables.SearchByName(TFunctionParameter(FunctionCall.FunctionDef.Params[I]).Name); 955 ExecutorVariable := ExecutorFunction.Block.Variables.SearchByVariable(Variable); 956 ExecutorVariable.Value.Free; 957 ExecutorVariable.Value := ExecuteExpression(Block, TExpression(FunctionCall.Params[I])); 963 if FunctionCall.FunctionDef.Params[I].Kind = pkVar then begin 964 Variable := TExpressionOperand(FunctionCall.Params[I]).VariableRef; 965 ExecutorVariable := Block.Variables.SearchByVariable(Variable); 966 ExecutorFunction.Block.Variables[I] := ExecutorVariable; 967 end else begin 968 Variable := FunctionCall.FunctionDef.Block.Variables.SearchByName( 969 TFunctionParameter(FunctionCall.FunctionDef.Params[I]).Name); 970 ExecutorVariable := ExecutorFunction.Block.Variables.SearchByVariable(Variable); 971 ExecutorVariable.Value.Free; 972 ExecutorVariable.Value := ExecuteExpression(Block, TExpression(FunctionCall.Params[I])); 973 end; 958 974 end; 975 959 976 ExecuteBlock(Block, FunctionCall.FunctionDef.Block, ExecutorFunction.Block); 960 977 ExecutorVariable := ExecutorFunction.Block.Variables.SearchByVariable(TVariable(FunctionCall.FunctionDef.Block.Variables.SearchByName('Result'))); … … 964 981 end; 965 982 966 functionTExecutor.ExecuteProcedureCall(Block: TExecutorBlock;967 ProcedureCall: TProcedureCall) : TValue;983 procedure TExecutor.ExecuteProcedureCall(Block: TExecutorBlock; 984 ProcedureCall: TProcedureCall); 968 985 var 969 986 ExecutorProcedure: TExecutorProcedure; … … 973 990 Variable: TVariable; 974 991 begin 975 Result := nil;976 992 ExecutorProcedure := Block.GetProcedure(ProcedureCall.ProcedureDef); 977 993 if Assigned(ExecutorProcedure) then begin … … 989 1005 else Params[I].Value := ExecuteExpression(Block, ProcedureCall.Params[I]); 990 1006 end; 991 Result :=ExecutorProcedure.Callback(Params);1007 ExecutorProcedure.Callback(Params); 992 1008 for I := 0 to ProcedureCall.Params.Count - 1 do begin 993 //if FunctionCall.Params[I].994 1009 Params[I].Free; 995 1010 end; 996 1011 end else begin 997 1012 InitExecutorBlock(ExecutorProcedure.Block, ProcedureCall.ProcedureDef.Block); 1013 1014 // Clean variables 998 1015 for I := 0 to ProcedureCall.Params.Count - 1 do begin 999 Variable := ProcedureCall.ProcedureDef.Block.Variables.SearchByName( 1000 TFunctionParameter(ProcedureCall.ProcedureDef.Params[I]).Name); 1001 ExecutorVariable := ExecutorProcedure.Block.Variables.SearchByVariable(Variable); 1002 ExecutorVariable.Value.Free; 1003 ExecutorVariable.Value := ExecuteExpression(Block, TExpression(ProcedureCall.Params[I])); 1016 if ProcedureCall.ProcedureDef.Params[I].Kind = pkVar then begin 1017 Variable := TExpressionOperand(ProcedureCall.Params[I]).VariableRef; 1018 ExecutorVariable := Block.Variables.SearchByVariable(Variable); 1019 ExecutorProcedure.Block.Variables[I].Variable := Variable; 1020 ExecutorProcedure.Block.Variables[I].Value := ExecutorVariable.Value; 1021 end else begin 1022 Variable := ProcedureCall.ProcedureDef.Block.Variables.SearchByName( 1023 TFunctionParameter(ProcedureCall.ProcedureDef.Params[I]).Name); 1024 ExecutorVariable := ExecutorProcedure.Block.Variables.SearchByVariable(Variable); 1025 ExecutorVariable.Value.Free; 1026 ExecutorVariable.Value := ExecuteExpression(Block, TExpression(ProcedureCall.Params[I])); 1027 end; 1004 1028 end; 1029 1005 1030 ExecuteBlock(Block, ProcedureCall.ProcedureDef.Block, ExecutorProcedure.Block); 1006 ExecutorVariable := ExecutorProcedure.Block.Variables.SearchByVariable(1007 TVariable(ProcedureCall.ProcedureDef.Block.Variables.SearchByName('Result')));1008 Result := ExecutorVariable.Value.Clone;1009 1031 end; 1010 1032 end else raise Exception.Create('No executor for ' + ProcedureCall.ProcedureDef.Name + ' function.'); … … 1022 1044 Variable := Block.GetVariable(Assignment.Variable); 1023 1045 ExecutorFunction := Block.GetTypeFunction(Assignment.Variable.TypeRef, '_Assign'); 1024 if Assignment.Variable.TypeRef = Assignment.Expression.GetType then begin ;1046 if Assignment.Variable.TypeRef = Assignment.Expression.GetType then begin 1025 1047 SetLength(Params, 1); 1026 1048 Params[0] := TExecutorFunctionCallbackParam.Create;
Note:
See TracChangeset
for help on using the changeset viewer.