Changeset 149 for branches/easy compiler/USourceExecutor.pas
- Timestamp:
- Jan 18, 2018, 1:30:58 PM (7 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
branches/easy compiler/USourceExecutor.pas
r148 r149 17 17 Variable: TSourceVariable; 18 18 Value: TSourceValue; 19 19 20 destructor Destroy; override; 20 21 end; … … 49 50 procedure ExecuteBreak(CommandBreak: TCommandBreak); 50 51 procedure ExecuteIfEqual(IfEqual: TCommandIfEqual); 52 procedure ExecuteIfNotEqual(IfNotEqual: TCommandIfNotEqual); 51 53 procedure ExecuteRepeat(CommandRepeat: TCommandRepeat); 52 54 function ReadValueReference(Reference: TSourceReference): TSourceValue; 53 function ReadVarReference(Reference: TSourceReference): TSourceVa riable;55 function ReadVarReference(Reference: TSourceReference): TSourceValue; 54 56 public 55 57 constructor Create; … … 141 143 end; 142 144 143 function TSourceExecutor.ReadVarReference(Reference: TSourceReference): TSourceVa riable;145 function TSourceExecutor.ReadVarReference(Reference: TSourceReference): TSourceValue; 144 146 var 145 147 ArrayIndex: TSourceValue; 148 Variable: TSourceVariable; 149 ExecutorVar: TExecutorVariable; 150 I: Integer; 146 151 begin 147 152 Result := nil; 148 153 if Reference is TSourceReferenceVariable then begin 149 Result := TSourceReferenceVariable(Reference).Variable; 154 Variable := TSourceReferenceVariable(Reference).Variable; 155 end else 156 if Reference is TSourceReferenceArray then begin 157 Variable := TSourceReferenceArray(Reference).ArrayRef; 158 end else 159 raise Exception.Create('Unsupported reference'); 160 161 ExecutorVar := Variables.Search(Variable); 162 if not Assigned(ExecutorVar) then begin 163 ExecutorVar := TExecutorVariable.Create; 164 ExecutorVar.Variable := Variable; 165 Variables.Add(ExecutorVar); 166 ExecutorVar.Value := Variable.ValueType.GetValueType.Create; 167 end; 168 if Reference is TSourceReferenceVariable then begin 169 Result := ExecutorVar.Value; 150 170 end else 151 171 if Reference is TSourceReferenceArray then begin … … 153 173 if not (ArrayIndex is TSourceValueInteger) then 154 174 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); 157 end else raise Exception.Create('Unsupported reference'); 175 I := TSourceValueInteger(ArrayIndex).Value; 176 while TSourceValueArray(ExecutorVar.Value).Items.Count < (I + 1) do begin 177 TSourceValueArray(ExecutorVar.Value).Items.Add(TSourceTypeArray(TSourceReferenceArray(Reference).ArrayRef.ValueType).ItemType.GetValueType.Create); 178 end; 179 Result := TSourceValue(TSourceValueArray(ExecutorVar.Value).Items[I]); 180 end else 181 raise Exception.Create('Unsupported reference'); 158 182 end; 159 183 160 184 procedure TSourceExecutor.ExecuteAssign(CommandAssign: TCommandFunctionCall); 161 185 var 162 Variable: TSourceVariable; 163 Value: TSourceValue; 164 ExecutorVar: TExecutorVariable; 186 Dest: TSourceValue; 187 Source: TSourceValue; 165 188 begin 166 189 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); 190 Dest := ReadVarReference(TSourceReference(Parameters[0])); 191 Source := ReadValueReference(TSourceReference(Parameters[1])); 192 Dest.Assign(Source); 177 193 end; 178 194 end; … … 230 246 end; 231 247 248 procedure TSourceExecutor.ExecuteIfNotEqual(IfNotEqual: TCommandIfNotEqual); 249 var 250 Value1: TSourceValue; 251 Value2: TSourceValue; 252 begin 253 Value1 := ReadValueReference(IfNotEqual.Reference1); 254 Value2 := ReadValueReference(IfNotEqual.Reference2); 255 if (Value1 is TSourceValueInteger) and (Value2 is TSourceValueInteger) then begin 256 if TSourceValueInteger(Value1).Value = TSourceValueInteger(Value2).Value then 257 SkipNext := True; 258 end else 259 if (Value1 is TSourceValueString) and (Value2 is TSourceValueString) then begin 260 if TSourceValueString(Value1).Value = TSourceValueString(Value2).Value then 261 SkipNext := True; 262 end else 263 raise Exception.Create('Unsupported types for comparison.'); 264 end; 265 232 266 procedure TSourceExecutor.ExecuteRepeat(CommandRepeat: TCommandRepeat); 233 267 var … … 251 285 Text: string; 252 286 IntValue: Integer; 287 Dest: TSourceValue; 253 288 begin 254 289 if Command is TCommandFunctionCall then … … 276 311 if Name = 'inputln' then begin 277 312 if Assigned(FOnInput) then begin 278 Variable := ReadVarReference(TSourceReference(Parameters[0])); 279 ExecutorVar := Variables.Search(Variable); 280 if ExecutorVar.Value is TSourceValueString then begin 281 TSourceValueString(ExecutorVar.Value).Value := FOnInput; 282 FOnOutput(TSourceValueString(ExecutorVar.Value).Value + LineEnding); 313 Value := ReadVarReference(TSourceReference(Parameters[0])); 314 if Value is TSourceValueString then begin 315 TSourceValueString(Value).Value := FOnInput; 316 FOnOutput(TSourceValueString(Value).Value + LineEnding); 283 317 end else 284 if ExecutorVar.Value is TSourceValueInteger then begin318 if Value is TSourceValueInteger then begin 285 319 Text := FOnInput; 286 320 if TryStrToInt(Text, IntValue) then 287 TSourceValueInteger( ExecutorVar.Value).Value := IntValue288 else TSourceValueInteger( ExecutorVar.Value).Value := 0;289 FOnOutput(IntToStr(TSourceValueInteger( ExecutorVar.Value).Value) + LineEnding);321 TSourceValueInteger(Value).Value := IntValue 322 else TSourceValueInteger(Value).Value := 0; 323 FOnOutput(IntToStr(TSourceValueInteger(Value).Value) + LineEnding); 290 324 end else 291 325 raise Exception.Create('Unsupported value type'); … … 296 330 end else 297 331 if Name = 'increment' then begin 298 Variable:= ReadVarReference(TSourceReference(Parameters[0]));332 Dest := ReadVarReference(TSourceReference(Parameters[0])); 299 333 Value := ReadValueReference(TSourceReference(Parameters[1])); 300 ExecutorVar := Variables.Search(Variable); 301 if not Assigned(ExecutorVar) then raise Exception.Create('Variable not found'); 302 if (ExecutorVar.Value is TSourceValueInteger) and (Value is TSourceValueInteger) then 303 Inc(TSourceValueInteger(ExecutorVar.Value).Value, TSourceValueInteger(Value).Value) 334 if (Dest is TSourceValueInteger) and (Value is TSourceValueInteger) then 335 Inc(TSourceValueInteger(Dest).Value, TSourceValueInteger(Value).Value) 304 336 else raise Exception.Create('Wrong type for increment'); 305 337 end else 306 338 if Name = 'decrement' then begin 307 Variable:= ReadVarReference(TSourceReference(Parameters[0]));339 Dest := ReadVarReference(TSourceReference(Parameters[0])); 308 340 Value := ReadValueReference(TSourceReference(Parameters[1])); 309 ExecutorVar := Variables.Search(Variable); 310 if not Assigned(ExecutorVar) then raise Exception.Create('Variable not found'); 311 if (ExecutorVar.Value is TSourceValueInteger) and (Value is TSourceValueInteger) then 312 Dec(TSourceValueInteger(ExecutorVar.Value).Value, TSourceValueInteger(Value).Value) 341 if (Dest is TSourceValueInteger) and (Value is TSourceValueInteger) then 342 Dec(TSourceValueInteger(Dest).Value, TSourceValueInteger(Value).Value) 313 343 else raise Exception.Create('Wrong type for increment'); 314 344 end else … … 324 354 ExecuteIfEqual(TCommandIfEqual(Command)); 325 355 end else 356 if Command is TCommandIfNotEqual then begin 357 ExecuteIfNotEqual(TCommandIfNotEqual(Command)); 358 end else 326 359 if Command is TCommandRepeat then begin 327 360 ExecuteRepeat(Command as TCommandRepeat);
Note:
See TracChangeset
for help on using the changeset viewer.