Changeset 149
- Timestamp:
- Jan 18, 2018, 1:30:58 PM (7 years ago)
- Location:
- branches/easy compiler
- Files:
-
- 6 edited
Legend:
- Unmodified
- Added
- Removed
-
branches/easy compiler/UCompiler.pas
r148 r149 50 50 ): Boolean; 51 51 function ParseFunctionCall(SourceCode: TSourceCode; out FunctionCall: TCommandFunctionCall): Boolean; 52 function ParseIfEqual(SourceCode: TSourceCode; out IfZero: TCommandIfEqual): Boolean; 52 function ParseIfEqual(SourceCode: TSourceCode; out IfEqual: TCommandIfEqual): Boolean; 53 function ParseIfNotEqual(SourceCode: TSourceCode; out IfNotEqual: TCommandIfNotEqual): Boolean; 53 54 function ParseReference(SourceCode: TSourceCode): TSourceReference; 54 55 function ParseReferenceVariable(SourceCode: TSourceCode): TSourceReference; … … 327 328 end; 328 329 329 function TCompiler.ParseIfEqual(SourceCode: TSourceCode; out If Zero: TCommandIfEqual): Boolean;330 function TCompiler.ParseIfEqual(SourceCode: TSourceCode; out IfEqual: TCommandIfEqual): Boolean; 330 331 var 331 332 Token: TSourceToken; … … 338 339 Keyword := LowerCase(Token.Text); 339 340 if Keyword = 'ifequal' then begin 340 IfZero := TCommandIfEqual.Create; 341 IfZero.Reference1 := ParseReference(SourceCode); 342 IfZero.Reference2 := ParseReference(SourceCode); 341 IfEqual := TCommandIfEqual.Create; 342 IfEqual.Reference1 := ParseReference(SourceCode); 343 IfEqual.Reference2 := ParseReference(SourceCode); 344 Result := True; 345 end; 346 if not Result then Tokenizer.TokenIndex := TokenIndex; 347 end; 348 349 function TCompiler.ParseIfNotEqual(SourceCode: TSourceCode; out 350 IfNotEqual: TCommandIfNotEqual): Boolean; 351 var 352 Token: TSourceToken; 353 TokenIndex: Integer; 354 Keyword: string; 355 begin 356 Result := False; 357 TokenIndex := Tokenizer.TokenIndex; 358 Token := Tokenizer.GetNext; 359 Keyword := LowerCase(Token.Text); 360 if Keyword = 'ifnotequal' then begin 361 IfNotEqual := TCommandIfNotEqual.Create; 362 IfNotEqual.Reference1 := ParseReference(SourceCode); 363 IfNotEqual.Reference2 := ParseReference(SourceCode); 343 364 Result := True; 344 365 end; … … 390 411 var 391 412 CommandBeginEnd: TCommandBeginEnd; 392 CommandIf Zero: TCommandIfEqual;413 CommandIfEqual: TCommandIfEqual; 393 414 CommandFunctionCall: TCommandFunctionCall; 394 415 CommandBreak: TCommandBreak; 395 416 CommandRepeat: TCommandRepeat; 417 CommandIfNotEqual: TCommandIfNotEqual; 396 418 begin 397 419 Command := nil; … … 401 423 Command := CommandBeginEnd; 402 424 end else 403 if ParseIfEqual(SourceCode, CommandIfZero) then begin 404 Command := CommandIfZero; 425 if ParseIfEqual(SourceCode, CommandIfEqual) then begin 426 Command := CommandIfEqual; 427 end else 428 if ParseIfNotEqual(SourceCode, CommandIfNotEqual) then begin 429 Command := CommandIfNotEqual; 405 430 end else 406 431 if ParseBreak(SourceCode, CommandBreak) then begin -
branches/easy compiler/UFormMain.lfm
r141 r149 14 14 object MemoOutput: TMemo 15 15 Left = 440 16 Height = 24016 Height = 464 17 17 Top = 32 18 Width = 400 18 Width = 448 19 ReadOnly = True 19 20 ScrollBars = ssAutoBoth 20 21 TabOrder = 0 … … 40 41 Left = 440 41 42 Height = 28 42 Top = 30443 Top = 526 43 44 Width = 200 44 45 OnKeyPress = Edit1KeyPress … … 48 49 Left = 656 49 50 Height = 31 50 Top = 30451 Top = 526 51 52 Width = 94 52 53 Caption = 'Send' … … 73 74 Left = 441 74 75 Height = 20 75 Top = 28276 Top = 504 76 77 Width = 98 77 78 Caption = 'Executor input:' -
branches/easy compiler/UFormMain.pas
r148 r149 7 7 uses 8 8 Classes, SysUtils, FileUtil, Forms, Controls, Graphics, Dialogs, StdCtrls, 9 UTargetCode, USourceCode, USourceExecutor, USourceGenerator ;9 UTargetCode, USourceCode, USourceExecutor, USourceGenerator, LCLIntf; 10 10 11 11 type … … 128 128 Add('IfEqual Answer ''y'''); 129 129 Add('Begin'); 130 Add('Print Ln''Thats clear. It is ''');130 Add('Print ''Thats clear. It is '''); 131 131 Add('PrintLn AnimalName[I]'); 132 132 Add('Break'); … … 136 136 Add('Break'); 137 137 Add('End'); 138 Add('PrintLn ''I am lost. What is the animal?'''); 139 Add('InputLn AnimalName[I]'); 140 Add('PrintLn ''Describe the animal for me. What is it like?'''); 141 Add('InputLn AnimalProperty[I]'); 142 Add('PrintLn ''Thank you. I will remember that animal.'''); 143 Add('Increment AnimalCount 1'); 138 Add('IfEqual I AnimalCount'); 139 Add('Begin'); 140 Add('PrintLn ''I am lost. What is the animal?'''); 141 Add('InputLn AnimalName[I]'); 142 Add('PrintLn ''Describe the animal for me. What is it like?'''); 143 Add('InputLn AnimalProperty[I]'); 144 Add('PrintLn ''Thank you. I will remember that animal.'''); 145 Add('Increment AnimalCount 1'); 146 Add('End'); 147 Add('Print ''Do you want to try again? (y/n)'''); 148 Add('InputLn Answer'); 149 Add('IfNotEqual Answer ''y'''); 150 Add('Break'); 144 151 Add('PrintLn '''''); 145 152 Add('End'); 153 Add('PrintLn ''Bye'''); 146 154 Add('End'); 147 155 end; … … 201 209 begin 202 210 MemoOutput.Text := MemoOutput.Text + Text; 211 MemoOutput.SelStart := Length(MemoOutput.Text); 212 //SendMessage(MemoOutput.Handle, EM_LINESCROLL, 0, MemoOutput.Lines.Count); 203 213 end; 204 214 -
branches/easy compiler/USourceCode.pas
r148 r149 26 26 TSourceValue = class 27 27 procedure Assign(Source: TSourceValue); virtual; 28 constructor Create; virtual; 28 29 end; 29 30 … … 113 114 Items: TSourceValues; 114 115 procedure Assign(Source: TSourceValue); override; 115 constructor Create; 116 constructor Create; override; 116 117 destructor Destroy; override; 117 118 end; … … 188 189 189 190 TCommandIfEqual = class(TSourceCommand) 191 Reference1: TSourceReference; 192 Reference2: TSourceReference; 193 destructor Destroy; override; 194 end; 195 196 { TCommandIfNotEqual } 197 198 TCommandIfNotEqual = class(TSourceCommand) 190 199 Reference1: TSourceReference; 191 200 Reference2: TSourceReference; … … 223 232 224 233 implementation 234 235 { TCommandIfNotEqual } 236 237 destructor TCommandIfNotEqual.Destroy; 238 begin 239 Reference1.Free; 240 Reference2.Free; 241 inherited Destroy; 242 end; 225 243 226 244 { TSourceType } … … 364 382 end; 365 383 384 constructor TSourceValue.Create; 385 begin 386 end; 387 366 388 { TSourceValueInteger } 367 389 -
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); -
branches/easy compiler/USourceGenerator.pas
r148 r149 120 120 Result := Result + IndentStr + 'repeat' + LineEnding; 121 121 Inc(Indent); 122 Result := Result + IndentStr + GenerateCommand(TCommandRepeat(Command).Command) + LineEnding;122 Result := Result + GenerateCommand(TCommandRepeat(Command).Command); 123 123 Dec(Indent); 124 124 Result := Result + IndentStr + 'until False;' + LineEnding; 125 125 end else 126 126 if Command is TCommandIfEqual then begin 127 Result := Result + IndentStr + 'if ' + GenerateRef(TCommandIfEqual(Command).Reference1) + 128 ' = ' + GenerateRef(TCommandIfEqual(Command).Reference2) + ' then '; 127 Result := Result + IndentStr + 'if ' + 128 GenerateRef(TCommandIfEqual(Command).Reference1) + ' = ' + 129 GenerateRef(TCommandIfEqual(Command).Reference2) + ' then ' + LineEnding; 130 end else 131 if Command is TCommandIfNotEqual then begin 132 Result := Result + IndentStr + 'if ' + 133 GenerateRef(TCommandIfNotEqual(Command).Reference1) + ' <> ' + 134 GenerateRef(TCommandIfNotEqual(Command).Reference2) + ' then ' + LineEnding; 129 135 end else 130 136 raise Exception.Create('Unsupported instruction');
Note:
See TracChangeset
for help on using the changeset viewer.