Changeset 141 for branches/easy compiler/USourceExecutor.pas
- Timestamp:
- Jan 16, 2018, 2:44:19 PM (7 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
branches/easy compiler/USourceExecutor.pas
r140 r141 6 6 7 7 uses 8 Classes, SysUtils, USourceCode ;8 Classes, SysUtils, USourceCode, Contnrs; 9 9 10 10 type 11 11 TOutputEvent = procedure (Text: string) of object; 12 12 TInputEvent = function: string of object; 13 14 TExecutorVariable = class 15 Variable: TSourceVariable; 16 Value: TSourceValue; 17 end; 18 19 TExecutorVariables = class(TObjectList) 20 function Search(Variable: TSourceVariable): TExecutorVariable; 21 end; 13 22 14 23 { TSourceExecutor } … … 18 27 FOnInput: TInputEvent; 19 28 FOnOutput: TOutputEvent; 20 Variables: T StringList;29 Variables: TExecutorVariables; 21 30 public 22 31 constructor Create; … … 30 39 implementation 31 40 41 42 { TExecutorVariables } 43 44 function TExecutorVariables.Search(Variable: TSourceVariable): TExecutorVariable; 45 var 46 Item: TExecutorVariable; 47 begin 48 Result := nil; 49 for Item in Self do 50 if Item.Variable = Variable then begin 51 Result := Item; 52 Break; 53 end; 54 end; 55 32 56 { TSourceExecutor } 33 57 34 58 constructor TSourceExecutor.Create; 35 59 begin 36 Variables := T StringList.Create;60 Variables := TExecutorVariables.Create; 37 61 end; 38 62 … … 48 72 Instruction: TSourceInstruction; 49 73 Variable: TSourceVariable; 74 Value: TSourceValue; 75 ExecutorVar: TExecutorVariable; 50 76 51 function Read StringReference(Reference: TSourceReference): string;77 function ReadValueReference(Reference: TSourceReference): TSourceValue; 52 78 begin 53 Result := '';79 Result := nil; 54 80 if Reference is TSourceReferenceConstant then begin 55 81 Result := TSourceReferenceConstant(Reference).Constant.Value; 56 82 end else 57 83 if Reference is TSourceReferenceVariable then begin 58 Result := Variables. Values[TSourceReferenceVariable(Reference).Variable.Name];84 Result := Variables.Search(TSourceReferenceVariable(Reference).Variable).Value; 59 85 end else raise Exception.Create('Unsupported reference'); 60 86 end; … … 75 101 with TSourceInstructionFunction(Instruction) do begin 76 102 if Name = 'print' then begin 77 if Assigned(FOnOutput) then FOnOutput(ReadStringReference(Parameters[0])); 103 if Assigned(FOnOutput) then begin 104 Value := ReadValueReference(Parameters[0]); 105 if Value is TSourceValueString then 106 FOnOutput(TSourceValueString(Value).Value) 107 else if Value is TSourceValueInteger then 108 FOnOutput(IntToStr(TSourceValueInteger(Value).Value)) 109 else raise Exception.Create('Unsupported value type'); 110 end; 78 111 end else 79 112 if Name = 'println' then begin 80 if Assigned(FOnOutput) then FOnOutput(ReadStringReference(Parameters[0]) + 81 LineEnding); 113 if Assigned(FOnOutput) then begin 114 Value := ReadValueReference(Parameters[0]); 115 if Value is TSourceValueString then 116 FOnOutput(TSourceValueString(Value).Value + LineEnding) 117 else if Value is TSourceValueInteger then 118 FOnOutput(IntToStr(TSourceValueInteger(Value).Value) + LineEnding) 119 else raise Exception.Create('Unsupported value type'); 120 end; 82 121 end else 83 122 if Name = 'inputln' then begin 84 123 if Assigned(FOnInput) then begin 85 124 Variable := ReadVarReference(Parameters[0]); 86 Variables.Values[Variable.Name] := FOnInput; 125 ExecutorVar := Variables.Search(Variable); 126 if ExecutorVar.Value is TSourceValueString then begin 127 TSourceValueString(ExecutorVar.Value).Value := FOnInput; 128 FOnOutput(TSourceValueString(ExecutorVar.Value).Value + LineEnding); 129 end else 130 if ExecutorVar.Value is TSourceValueInteger then begin 131 TSourceValueInteger(ExecutorVar.Value).Value := StrToInt(FOnInput); 132 FOnOutput(IntToStr(TSourceValueInteger(ExecutorVar.Value).Value) + LineEnding); 133 end else 134 raise Exception.Create('Unsupported value type'); 87 135 end; 88 136 end else 89 137 if Name = 'assign' then begin 90 138 Variable := ReadVarReference(Parameters[0]); 91 Variables.Values[Variable.Name] := ReadStringReference(Parameters[1]); 92 end else raise Exception.Create('Unsupported function: ' + TSourceInstructionFunction(Instruction).Name); 139 Value := ReadValueReference(Parameters[1]); 140 ExecutorVar := Variables.Search(Variable); 141 if not Assigned(ExecutorVar) then begin 142 ExecutorVar := TExecutorVariable.Create; 143 ExecutorVar.Variable := Variable; 144 Variables.Add(ExecutorVar); 145 ExecutorVar.Value := Variable.ValueType.ValueClass.Create; 146 end; 147 ExecutorVar.Value.Assign(Value); 148 end else 149 if Name = 'increment' then begin 150 Variable := ReadVarReference(Parameters[0]); 151 Value := ReadValueReference(Parameters[1]); 152 ExecutorVar := Variables.Search(Variable); 153 if not Assigned(ExecutorVar) then raise Exception.Create('Variable not found'); 154 if (ExecutorVar.Value is TSourceValueInteger) and (Value is TSourceValueInteger) then 155 Inc(TSourceValueInteger(ExecutorVar.Value).Value, TSourceValueInteger(Value).Value) 156 else raise Exception.Create('Wrong type for increment'); 157 end else 158 raise Exception.Create('Unsupported function: ' + TSourceInstructionFunction(Instruction).Name); 93 159 end else raise Exception.Create('Unsupported instruction'); 94 160 Inc(IP);
Note:
See TracChangeset
for help on using the changeset viewer.