Changeset 140 for branches/easy compiler/USourceCode.pas
- Timestamp:
- Jan 16, 2018, 10:38:33 AM (7 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
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;
Note:
See TracChangeset
for help on using the changeset viewer.