Changeset 140 for branches/easy compiler
- Timestamp:
- Jan 16, 2018, 10:38:33 AM (7 years ago)
- Location:
- branches/easy compiler
- Files:
-
- 3 added
- 6 edited
Legend:
- Unmodified
- Added
- Removed
-
branches/easy compiler/UCompiler.pas
r139 r140 159 159 Instruction: TSourceInstruction; 160 160 NewReference: TSourceReference; 161 Funct: TSourceFunction; 162 Param: TSourceFunctionParameter; 161 163 162 164 function ParseReference: TSourceReference; … … 171 173 NewReference := TSourceReferenceVariable.Create; 172 174 TSourceReferenceVariable(NewReference).Variable := 173 Code.Variables. Find(Token.Text);175 Code.Variables.Search(Token.Text); 174 176 if TSourceReferenceVariable(NewReference).Variable = nil then 175 177 raise Exception.Create('Variable not found: ' + Token.Text); … … 184 186 NewReference := TSourceReferenceVariable.Create; 185 187 TSourceReferenceVariable(NewReference).Variable := 186 Code.Variables. Find(Token.Text);188 Code.Variables.Search(Token.Text); 187 189 if TSourceReferenceVariable(NewReference).Variable = nil then begin 188 190 if Initialize then … … 201 203 if Token.Kind = stIdentifier then begin 202 204 Keyword := LowerCase(Token.Text); 203 if Keyword = 'print' then begin 205 Funct := Code.Functions.Search(Keyword); 206 if Assigned(Funct) then begin 204 207 Instruction := TSourceInstructionFunction.Create; 205 TSourceInstructionFunction(Instruction).Name := 'print'; 206 TSourceInstructionFunction(Instruction).AddParameter(ParseReference); 207 Code.Instructions.Add(Instruction); 208 end else 209 if Keyword = 'println' then begin 210 Instruction := TSourceInstructionFunction.Create; 211 TSourceInstructionFunction(Instruction).Name := 'println'; 212 TSourceInstructionFunction(Instruction).AddParameter(ParseReference); 213 Code.Instructions.Add(Instruction); 214 end else 215 if Keyword = 'assign' then begin 216 Instruction := TSourceInstructionFunction.Create; 217 TSourceInstructionFunction(Instruction).Name := 'assign'; 218 TSourceInstructionFunction(Instruction).AddParameter(ParseReferenceVariable(True)); 219 TSourceInstructionFunction(Instruction).AddParameter(ParseReference); 208 TSourceInstructionFunction(Instruction).Name := Keyword; 209 for Param in Funct.Parameters do 210 if Param.Kind = pkString then begin 211 TSourceInstructionFunction(Instruction).AddParameter(ParseReference) 212 end else 213 if Param.Kind = pkVariable then begin 214 TSourceInstructionFunction(Instruction).AddParameter(ParseReferenceVariable(True)); 215 end else 216 raise Exception.Create('Unsupported parameter type.'); 220 217 Code.Instructions.Add(Instruction); 221 218 end else raise Exception.Create('Unsupported keyword: ' + Token.Text); -
branches/easy compiler/UFormMain.lfm
r139 r140 1 1 object Form1: TForm1 2 Left = 6003 Height = 3944 Top = 4225 Width = 5172 Left = 326 3 Height = 572 4 Top = 267 5 Width = 909 6 6 Caption = 'Little compiler' 7 ClientHeight = 3948 ClientWidth = 5177 ClientHeight = 572 8 ClientWidth = 909 9 9 DesignTimePPI = 120 10 OnCreate = FormCreate 11 OnDestroy = FormDestroy 10 12 OnShow = FormShow 11 13 LCLVersion = '1.8.0.6' 12 14 object MemoOutput: TMemo 13 Left = 814 Height = 16015 Top = 22416 Width = 4 9615 Left = 440 16 Height = 240 17 Top = 32 18 Width = 400 17 19 ScrollBars = ssAutoBoth 18 20 TabOrder = 0 … … 20 22 object MemoSource: TMemo 21 23 Left = 8 22 Height = 16823 Top = 824 Width = 4 9624 Height = 240 25 Top = 32 26 Width = 416 25 27 ScrollBars = ssAutoBoth 26 28 TabOrder = 1 27 29 end 28 30 object ButtonBuild: TButton 29 Left = 1 8431 Left = 112 30 32 Height = 31 31 Top = 18433 Top = 280 32 34 Width = 166 33 35 Caption = 'Compile && Execute' … … 35 37 TabOrder = 2 36 38 end 39 object Edit1: TEdit 40 Left = 440 41 Height = 28 42 Top = 304 43 Width = 200 44 TabOrder = 3 45 end 46 object ButtonSend: TButton 47 Left = 656 48 Height = 31 49 Top = 304 50 Width = 94 51 Caption = 'Send' 52 OnClick = ButtonSendClick 53 TabOrder = 4 54 end 55 object Label1: TLabel 56 Left = 9 57 Height = 20 58 Top = 8 59 Width = 78 60 Caption = 'Text source:' 61 ParentColor = False 62 end 63 object Label2: TLabel 64 Left = 440 65 Height = 20 66 Top = 11 67 Width = 108 68 Caption = 'Executor output:' 69 ParentColor = False 70 end 71 object Label3: TLabel 72 Left = 441 73 Height = 20 74 Top = 282 75 Width = 98 76 Caption = 'Executor input:' 77 ParentColor = False 78 end 79 object MemoGenerator: TMemo 80 Left = 8 81 Height = 216 82 Top = 344 83 Width = 416 84 ScrollBars = ssAutoBoth 85 TabOrder = 5 86 end 87 object Label4: TLabel 88 Left = 8 89 Height = 20 90 Top = 315 91 Width = 117 92 Caption = 'Generator output:' 93 ParentColor = False 94 end 37 95 end -
branches/easy compiler/UFormMain.pas
r139 r140 7 7 uses 8 8 Classes, SysUtils, FileUtil, Forms, Controls, Graphics, Dialogs, StdCtrls, 9 UTargetCode, USourceCode ;9 UTargetCode, USourceCode, USourceExecutor, USourceGenerator; 10 10 11 11 type … … 14 14 15 15 TForm1 = class(TForm) 16 ButtonSend: TButton; 16 17 ButtonBuild: TButton; 18 Edit1: TEdit; 19 Label1: TLabel; 20 Label2: TLabel; 21 Label3: TLabel; 22 Label4: TLabel; 17 23 MemoOutput: TMemo; 18 24 MemoSource: TMemo; 25 MemoGenerator: TMemo; 19 26 procedure ButtonBuildClick(Sender: TObject); 27 procedure ButtonSendClick(Sender: TObject); 28 procedure FormCreate(Sender: TObject); 29 procedure FormDestroy(Sender: TObject); 20 30 procedure FormShow(Sender: TObject); 21 31 private 22 32 InputBuffer: TStringList; 33 procedure ExecutorOutput(Text: string); 34 function ExecutorInput: string; 23 35 public 24 procedure ExecutorOutput(Text: string);25 36 end; 26 37 … … 47 58 Add('PrintLn Text1'); 48 59 Add('PrintLn Text2'); 60 Add('InputLn Text3'); 61 Add('PrintLn Text3'); 49 62 end; 50 63 end; … … 64 77 Executor := TSourceExecutor.Create; 65 78 Executor.OnOutput := ExecutorOutput; 79 Executor.OnInput := ExecutorInput; 66 80 Generator := TSourceGenerator.Create; 67 81 68 82 Compiler.Compile(MemoSource.Text, SourceCode); 83 MemoGenerator.Text := Generator.Generate(SourceCode); 69 84 Executor.Execute(SourceCode); 70 Generator.Generate(SourceCode);71 85 72 86 Generator.Free; … … 77 91 end; 78 92 93 procedure TForm1.ButtonSendClick(Sender: TObject); 94 begin 95 InputBuffer.Add(Edit1.Text); 96 Edit1.Text := ''; 97 end; 98 99 procedure TForm1.FormCreate(Sender: TObject); 100 begin 101 InputBuffer := TStringList.Create; 102 end; 103 104 procedure TForm1.FormDestroy(Sender: TObject); 105 begin 106 InputBuffer.Free; 107 end; 108 79 109 procedure TForm1.ExecutorOutput(Text: string); 80 110 begin … … 82 112 end; 83 113 114 function TForm1.ExecutorInput: string; 115 begin 116 while InputBuffer.Count = 0 do begin 117 Sleep(50); 118 Application.ProcessMessages; 119 end; 120 Result := InputBuffer[0]; 121 InputBuffer.Delete(0); 122 end; 123 84 124 end. 85 125 -
branches/easy compiler/USourceCode.pas
r139 r140 26 26 TSourceVariables = class(TObjectList) 27 27 function AddNew(Name: string = ''): TSourceVariable; 28 function Find(Name: string): TSourceVariable;28 function Search(Name: string): TSourceVariable; 29 29 end; 30 30 … … 38 38 end; 39 39 40 TSourceParameterKind = (pkString, pkVariable); 41 42 TSourceFunctionParameter = class 43 Name: string; 44 Kind: TSourceParameterKind; 45 end; 46 47 TSourceFunctionParameters = class(TObjectList) 48 end; 49 50 { TSourceFunction } 51 52 TSourceFunction = class 53 Name: string; 54 Parameters: TSourceFunctionParameters; 55 procedure AddParameter(Name: string; Kind: TSourceParameterKind); 56 constructor Create; 57 destructor Destroy; override; 58 end; 59 60 { TSourceFunctions } 61 62 TSourceFunctions = class(TObjectList) 63 function AddNew(Name: string): TSourceFunction; 64 function Search(Name: string): TSourceFunction; 65 end; 66 40 67 { TSourceConstants } 41 68 … … 59 86 60 87 TSourceCode = class 88 private 89 procedure InitFunctions; 90 public 61 91 Variables: TSourceVariables; 62 92 Constants: TSourceConstants; 93 Functions: TSourceFunctions; 63 94 Instructions: TSourceInstructions; 64 95 constructor Create; … … 66 97 end; 67 98 68 TOutputEvent = procedure (Text: string) of object;69 70 { TSourceExecutor }71 72 TSourceExecutor = class73 private74 FOnOutput: TOutputEvent;75 Variables: TStringList;76 public77 constructor Create;78 destructor Destroy; override;79 procedure Execute(SourceCode: TSourceCode);80 property OnOutput: TOutputEvent read FOnOutput write FOnOutput;81 end;82 83 { TSourceGenerator }84 85 TSourceGenerator = class86 procedure Generate(SourceCode: TSourceCode);87 end;88 99 89 100 implementation 90 101 91 { TSourceGenerator } 92 93 procedure TSourceGenerator.Generate(SourceCode: TSourceCode); 94 var 95 F: TStringList; 96 Output: string; 97 Instruction: TSourceInstruction; 98 Parameter: TSourceReference; 99 I: Integer; 100 begin 101 Output := ''; 102 if SourceCode.Variables.Count > 0 then 103 Output := Output + 'var' + LineEnding; 104 with SourceCode do 105 for I := 0 to Variables.Count - 1 do 106 Output := Output + ' ' + TSourceVariable(Variables[I]).Name + ': string;' + LineEnding; 107 Output := Output + 'begin' + LineEnding; 108 with SourceCode do 109 for I := 0 to Instructions.Count - 1 do begin 110 Instruction := TSourceInstruction(Instructions[I]); 111 if Instruction is TSourceInstructionFunction then begin 112 if TSourceInstructionFunction(Instruction).Name = 'print' then begin 113 Output := Output + ' Write('; 114 Parameter := TSourceInstructionFunction(Instruction).Parameters[0]; 115 if Parameter is TSourceReferenceConstant then begin 116 Output := Output + '''' + TSourceReferenceConstant(Parameter).Constant.Value + ''''; 117 end else 118 if Parameter is TSourceReferenceVariable then begin 119 Output := Output + TSourceReferenceVariable(Parameter).Variable.Name; 120 end else raise Exception.Create('Unsupported parameter type'); 121 Output := Output + ');' + LineEnding; 122 end else 123 if TSourceInstructionFunction(Instruction).Name = 'println' then begin 124 Output := Output + ' WriteLn('; 125 Parameter := TSourceInstructionFunction(Instruction).Parameters[0]; 126 if Parameter is TSourceReferenceConstant then begin 127 Output := Output + '''' + TSourceReferenceConstant(Parameter).Constant.Value + ''''; 128 end else 129 if Parameter is TSourceReferenceVariable then begin 130 Output := Output + TSourceReferenceVariable(Parameter).Variable.Name; 131 end else raise Exception.Create('Unsupported parameter type'); 132 Output := Output + ');' + LineEnding; 133 end else 134 if TSourceInstructionFunction(Instruction).Name = 'assign' then begin 135 Output := Output + ' '; 136 Parameter := TSourceInstructionFunction(Instruction).Parameters[0]; 137 if Parameter is TSourceReferenceVariable then begin 138 Output := Output + TSourceReferenceVariable(Parameter).Variable.Name; 139 end else raise Exception.Create('Unsupported parameter type'); 140 Output := Output + ' := '; 141 Parameter := TSourceInstructionFunction(Instruction).Parameters[1]; 142 if Parameter is TSourceReferenceConstant then begin 143 Output := Output + '''' + TSourceReferenceConstant(Parameter).Constant.Value + ''''; 144 end else 145 if Parameter is TSourceReferenceVariable then begin 146 Output := Output + TSourceReferenceVariable(Parameter).Variable.Name; 147 end else raise Exception.Create('Unsupported parameter type'); 148 Output := Output + ';' + LineEnding; 149 end else raise Exception.Create('Unsupported instruction name.'); 150 end else raise Exception.Create('Unsupported instruction'); 151 end; 152 Output := Output + 'end.' + LineEnding; 153 154 F := TStringList.Create; 155 try 156 F.Text := Output; 157 F.SaveToFile('Output.pas'); 158 finally 159 F.Free; 160 end; 161 end; 162 163 { TSourceExecutor } 164 165 constructor TSourceExecutor.Create; 166 begin 167 Variables := TStringList.Create; 168 end; 169 170 destructor TSourceExecutor.Destroy; 171 begin 172 Variables.Free; 102 { TSourceFunctions } 103 104 function TSourceFunctions.AddNew(Name: string): TSourceFunction; 105 begin 106 Result := TSourceFunction.Create; 107 Result.Name := Name; 108 Add(Result); 109 end; 110 111 function TSourceFunctions.Search(Name: string): TSourceFunction; 112 var 113 Item: TSourceFunction; 114 begin 115 Result := nil; 116 for Item in Self do 117 if Item.Name = Name then begin 118 Result := Item; 119 Break; 120 end; 121 end; 122 123 { TSourceFunction } 124 125 procedure TSourceFunction.AddParameter(Name: string; Kind: TSourceParameterKind 126 ); 127 var 128 Parameter: TSourceFunctionParameter; 129 begin 130 Parameter := TSourceFunctionParameter.Create; 131 Parameter.Name := Name; 132 Parameter.Kind := Kind; 133 Parameters.Add(Parameter); 134 end; 135 136 constructor TSourceFunction.Create; 137 begin 138 Parameters := TSourceFunctionParameters.Create; 139 end; 140 141 destructor TSourceFunction.Destroy; 142 begin 143 Parameters.Free; 173 144 inherited Destroy; 174 end;175 176 procedure TSourceExecutor.Execute(SourceCode: TSourceCode);177 var178 IP: Integer;179 Instruction: TSourceInstruction;180 Text: string;181 Reference: TSourceReference;182 Variable: TSourceVariable;183 begin184 IP := 0;185 while IP < SourceCode.Instructions.Count do begin186 Instruction := TSourceInstruction(SourceCode.Instructions[IP]);187 if Instruction is TSourceInstructionFunction then begin188 if TSourceInstructionFunction(Instruction).Name = 'print' then begin189 Reference := TSourceInstructionFunction(Instruction).Parameters[0];190 if Reference is TSourceReferenceConstant then begin191 Text := TSourceReferenceConstant(Reference).Constant.Value;192 end else193 if Reference is TSourceReferenceVariable then begin194 Text := Variables.Values[TSourceReferenceVariable(Reference).Variable.Name];195 end else raise Exception.Create('Unsupported reference');196 if Assigned(FOnOutput) then FOnOutput(Text);197 end else198 if TSourceInstructionFunction(Instruction).Name = 'println' then begin199 Reference := TSourceInstructionFunction(Instruction).Parameters[0];200 if Reference is TSourceReferenceConstant then begin201 Text := TSourceReferenceConstant(Reference).Constant.Value;202 end else203 if Reference is TSourceReferenceVariable then begin204 Text := Variables.Values[TSourceReferenceVariable(Reference).Variable.Name];205 end else raise Exception.Create('Unsupported reference');206 if Assigned(FOnOutput) then FOnOutput(Text + LineEnding);207 end else208 if TSourceInstructionFunction(Instruction).Name = 'assign' then begin209 Variable := nil;210 Reference := TSourceInstructionFunction(Instruction).Parameters[0];211 if Reference is TSourceReferenceVariable then begin212 Variable := TSourceReferenceVariable(Reference).Variable;213 end else raise Exception.Create('Unsupported reference');214 Reference := TSourceInstructionFunction(Instruction).Parameters[1];215 if Reference is TSourceReferenceConstant then begin216 Text := TSourceReferenceConstant(Reference).Constant.Value;217 end else218 if Reference is TSourceReferenceVariable then begin219 Text := Variables.Values[TSourceReferenceVariable(Reference).Variable.Name];220 end else raise Exception.Create('Unsupported reference');221 Variables.Values[Variable.Name] := Text;222 end else raise Exception.Create('Unsupported function: ' + TSourceInstructionFunction(Instruction).Name);223 end else raise Exception.Create('Unsupported instruction');224 Inc(IP);225 end;226 145 end; 227 146 … … 235 154 end; 236 155 237 function TSourceVariables.Find(Name: string): TSourceVariable; 238 var 239 I: Integer; 156 function TSourceVariables.Search(Name: string): TSourceVariable; 157 var 240 158 Variable: TSourceVariable; 241 159 begin … … 268 186 { TSourceCode } 269 187 188 procedure TSourceCode.InitFunctions; 189 var 190 Funct: TSourceFunction; 191 begin 192 Functions.Clear; 193 Funct := Functions.AddNew('print'); 194 Funct.AddParameter('Text', pkString); 195 Funct := Functions.AddNew('println'); 196 Funct.AddParameter('Text', pkString); 197 Funct := Functions.AddNew('assign'); 198 Funct.AddParameter('Destination', pkVariable); 199 Funct.AddParameter('Source', pkString); 200 Funct := Functions.AddNew('inputln'); 201 Funct.AddParameter('Text', pkVariable); 202 end; 203 270 204 constructor TSourceCode.Create; 271 205 begin … … 273 207 Constants := TSourceConstants.Create; 274 208 Instructions := TSourceInstructions.Create; 209 Functions := TSourceFunctions.Create; 210 InitFunctions; 275 211 end; 276 212 277 213 destructor TSourceCode.Destroy; 278 214 begin 215 Functions.Free; 279 216 Variables.Free; 280 217 Constants.Free; -
branches/easy compiler/project1.lpi
r139 r140 28 28 </Item1> 29 29 </RequiredPackages> 30 <Units Count=" 5">30 <Units Count="7"> 31 31 <Unit0> 32 32 <Filename Value="project1.lpr"/> … … 52 52 <IsPartOfProject Value="True"/> 53 53 </Unit4> 54 <Unit5> 55 <Filename Value="USourceExecutor.pas"/> 56 <IsPartOfProject Value="True"/> 57 </Unit5> 58 <Unit6> 59 <Filename Value="USourceGenerator.pas"/> 60 <IsPartOfProject Value="True"/> 61 </Unit6> 54 62 </Units> 55 63 </ProjectOptions> … … 86 94 </Options> 87 95 </Linking> 96 <Other> 97 <CompilerMessages> 98 <IgnoredMessages idx5024="True"/> 99 </CompilerMessages> 100 </Other> 88 101 </CompilerOptions> 89 102 <Debugging> -
branches/easy compiler/project1.lpr
r139 r140 8 8 {$ENDIF}{$ENDIF} 9 9 Interfaces, // this includes the LCL widgetset 10 Forms, UFormMain, UCompiler, USourceCode, UTargetCode 10 Forms, UFormMain, UCompiler, USourceCode, UTargetCode, USourceExecutor, USourceGenerator 11 11 { you can add units after this }; 12 12
Note:
See TracChangeset
for help on using the changeset viewer.