Changeset 148 for branches/easy compiler/USourceExecutor.pas
- Timestamp:
- Jan 17, 2018, 5:27:23 PM (7 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
branches/easy compiler/USourceExecutor.pas
r147 r148 44 44 RepeatBlocks: TExecutorRepeats; 45 45 SkipNext: Boolean; 46 procedure ExecuteAssign(CommandAssign: TCommandFunctionCall); 46 47 procedure ExecuteBeginEnd(BeginEnd: TCommandBeginEnd); 47 48 procedure ExecuteCommand(Command: TSourceCommand); 48 49 procedure ExecuteBreak(CommandBreak: TCommandBreak); 50 procedure ExecuteIfEqual(IfEqual: TCommandIfEqual); 49 51 procedure ExecuteRepeat(CommandRepeat: TCommandRepeat); 50 52 function ReadValueReference(Reference: TSourceReference): TSourceValue; … … 120 122 121 123 function TSourceExecutor.ReadValueReference(Reference: TSourceReference): TSourceValue; 124 var 125 ArrayIndex: TSourceValue; 122 126 begin 123 127 Result := nil; … … 127 131 if Reference is TSourceReferenceVariable then begin 128 132 Result := Variables.Search(TSourceReferenceVariable(Reference).Variable).Value; 129 end else raise Exception.Create('Unsupported reference'); 133 end else 134 if Reference is TSourceReferenceArray then begin 135 ArrayIndex := ReadValueReference(TSourceReferenceArray(Reference).Index); 136 if not (ArrayIndex is TSourceValueInteger) then 137 raise Exception.Create('Only integer array index supported'); 138 Result := TSourceValue(TSourceValueArray(Variables.Search(TSourceReferenceArray(Reference).ArrayRef).Value).Items[TSourceValueInteger(ArrayIndex).Value]); 139 end else 140 raise Exception.Create('Unsupported reference'); 130 141 end; 131 142 132 143 function TSourceExecutor.ReadVarReference(Reference: TSourceReference): TSourceVariable; 144 var 145 ArrayIndex: TSourceValue; 133 146 begin 134 147 Result := nil; 135 148 if Reference is TSourceReferenceVariable then begin 136 149 Result := TSourceReferenceVariable(Reference).Variable; 150 end else 151 if Reference is TSourceReferenceArray then begin 152 ArrayIndex := ReadValueReference(TSourceReferenceArray(Reference).Index); 153 if not (ArrayIndex is TSourceValueInteger) then 154 raise Exception.Create('Only integer array index supported'); 155 // Result := TSourceValue(TSourceValueArray(Variables.Search(TSourceReferenceArray(Reference).ArrayRef).Value).Items[TSourceValueInteger(ArrayIndex)]); 156 //TODO: Result := Variables.Search(TSourceReferenceArray(Reference).ArrayRef); 137 157 end else raise Exception.Create('Unsupported reference'); 158 end; 159 160 procedure TSourceExecutor.ExecuteAssign(CommandAssign: TCommandFunctionCall); 161 var 162 Variable: TSourceVariable; 163 Value: TSourceValue; 164 ExecutorVar: TExecutorVariable; 165 begin 166 with TCommandFunctionCall(CommandAssign) do begin 167 Variable := ReadVarReference(TSourceReference(Parameters[0])); 168 Value := ReadValueReference(TSourceReference(Parameters[1])); 169 ExecutorVar := Variables.Search(Variable); 170 if not Assigned(ExecutorVar) then begin 171 ExecutorVar := TExecutorVariable.Create; 172 ExecutorVar.Variable := Variable; 173 Variables.Add(ExecutorVar); 174 ExecutorVar.Value := Variable.ValueType.GetValueType.Create; 175 end; 176 ExecutorVar.Value.Assign(Value); 177 end; 138 178 end; 139 179 … … 170 210 end else 171 211 raise Exception.Create('Used break outside repeat loop'); 212 end; 213 214 procedure TSourceExecutor.ExecuteIfEqual(IfEqual: TCommandIfEqual); 215 var 216 Value1: TSourceValue; 217 Value2: TSourceValue; 218 begin 219 Value1 := ReadValueReference(IfEqual.Reference1); 220 Value2 := ReadValueReference(IfEqual.Reference2); 221 if (Value1 is TSourceValueInteger) and (Value2 is TSourceValueInteger) then begin 222 if TSourceValueInteger(Value1).Value <> TSourceValueInteger(Value2).Value then 223 SkipNext := True; 224 end else 225 if (Value1 is TSourceValueString) and (Value2 is TSourceValueString) then begin 226 if TSourceValueString(Value1).Value <> TSourceValueString(Value2).Value then 227 SkipNext := True; 228 end else 229 raise Exception.Create('Unsupported types for comparison.'); 172 230 end; 173 231 … … 235 293 end else 236 294 if Name = 'assign' then begin 237 Variable := ReadVarReference(TSourceReference(Parameters[0])); 238 Value := ReadValueReference(TSourceReference(Parameters[1])); 239 ExecutorVar := Variables.Search(Variable); 240 if not Assigned(ExecutorVar) then begin 241 ExecutorVar := TExecutorVariable.Create; 242 ExecutorVar.Variable := Variable; 243 Variables.Add(ExecutorVar); 244 ExecutorVar.Value := Variable.ValueType.ValueClass.Create; 245 end; 246 ExecutorVar.Value.Assign(Value); 295 ExecuteAssign(TCommandFunctionCall(Command)); 247 296 end else 248 297 if Name = 'increment' then begin … … 272 321 ExecuteBreak(TCommandBreak(Command)); 273 322 end else 274 if Command is TCommandIfZero then begin 275 ExecutorVar := Variables.Search(TCommandIfZero(Command).Variable.Variable); 276 if Assigned(ExecutorVar) then begin 277 if ExecutorVar.Variable.ValueType.Name = 'Integer' then begin 278 if TSourceValueInteger(ExecutorVar.Value).Value <> 0 then SkipNext := True; 279 end else 280 raise Exception.Create('Can compare only integers'); 281 end else 282 raise Exception.Create('Variable not found'); 323 if Command is TCommandIfEqual then begin 324 ExecuteIfEqual(TCommandIfEqual(Command)); 283 325 end else 284 326 if Command is TCommandRepeat then begin
Note:
See TracChangeset
for help on using the changeset viewer.