Changeset 142 for branches/easy compiler/UCompiler.pas
- Timestamp:
- Jan 16, 2018, 3:19:23 PM (7 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
branches/easy compiler/UCompiler.pas
r141 r142 41 41 private 42 42 Tokenizer: TTokenizer; 43 procedure Parse(Tokens: TSourceTokens; Code: TSourceCode); 43 procedure Parse(Code: TSourceCode); 44 procedure ParseBeginEnd(BeginEnd: TSourceBeginEnd); 44 45 public 45 46 procedure Compile(Source: string; SourceCode: TSourceCode); … … 168 169 { TCompiler } 169 170 170 procedure TCompiler.Parse(Tokens: TSourceTokens; Code: TSourceCode); 171 procedure TCompiler.Parse(Code: TSourceCode); 172 begin 173 Tokenizer.TokenIndex := 0; 174 ParseBeginEnd(Code.Main); 175 end; 176 177 procedure TCompiler.ParseBeginEnd(BeginEnd: TSourceBeginEnd); 171 178 var 172 179 Token: TSourceToken; … … 186 193 NewReference := TSourceReferenceConstant.Create; 187 194 TSourceReferenceConstant(NewReference).Constant := 188 Code.Constants.AddNewString(Token.Text);195 BeginEnd.SourceCode.Constants.AddNewString(Token.Text); 189 196 end else 190 197 if Token.Kind = stIdentifier then begin 191 198 NewReference := TSourceReferenceVariable.Create; 192 199 TSourceReferenceVariable(NewReference).Variable := 193 Code.Variables.Search(Token.Text);200 BeginEnd.SourceCode.Variables.Search(Token.Text); 194 201 if TSourceReferenceVariable(NewReference).Variable = nil then 195 202 raise Exception.Create('Variable not found: ' + Token.Text); … … 198 205 NewReference := TSourceReferenceConstant.Create; 199 206 TSourceReferenceConstant(NewReference).Constant := 200 Code.Constants.AddNewInteger(StrToInt(Token.Text));207 BeginEnd.SourceCode.Constants.AddNewInteger(StrToInt(Token.Text)); 201 208 end else 202 209 raise Exception.Create('Unexpected parameter'); … … 210 217 NewReference := TSourceReferenceVariable.Create; 211 218 TSourceReferenceVariable(NewReference).Variable := 212 Code.Variables.Search(Token.Text);219 BeginEnd.SourceCode.Variables.Search(Token.Text); 213 220 if TSourceReferenceVariable(NewReference).Variable = nil then 214 221 raise Exception.Create('Variable not found: ' + Token.Text); … … 219 226 220 227 begin 221 Tokenizer.TokenIndex := 0; 222 while Tokenizer.TokenIndex < Length(Tokens.Tokens) do begin 228 while True do begin 223 229 Token := Tokenizer.GetNext; 230 if Token.Kind = stEof then Break 231 else 224 232 if Token.Kind = stIdentifier then begin 225 233 Keyword := LowerCase(Token.Text); … … 229 237 if Token2.Kind <> stIdentifier then 230 238 raise Exception.Create('Expected type parameter'); 231 Variable := Code.Variables.Search(Token.Text);239 Variable := BeginEnd.SourceCode.Variables.Search(Token.Text); 232 240 if not Assigned(Variable) then begin 233 ValueType := Code.Types.Search(Token2.Text);241 ValueType := BeginEnd.SourceCode.Types.Search(Token2.Text); 234 242 if not Assigned(ValueType) then 235 243 raise Exception.Create('Unsupported type: ' + Token2.Text); 236 Variable := Code.Variables.AddNew(Token.Text,244 Variable := BeginEnd.SourceCode.Variables.AddNew(Token.Text, 237 245 ValueType); 238 end else raise Exception.Create('Variable redefined'); 239 end else begin 240 Funct := Code.Functions.Search(Keyword); 246 end else raise Exception.Create('Variable redefined: ' + Token.Text); 247 end else 248 if Keyword = 'begin' then begin 249 Instruction := TSourceBeginEnd.Create; 250 BeginEnd.Instructions.Add(Instruction); 251 TSourceBeginEnd(Instruction).SourceCode := BeginEnd.SourceCode; 252 ParseBeginEnd(TSourceBeginEnd(Instruction)); 253 end else 254 if Keyword = 'end' then begin 255 Break; 256 end else 257 begin 258 Funct := BeginEnd.SourceCode.Functions.Search(Keyword); 241 259 if Assigned(Funct) then begin 242 Instruction := TSource InstructionFunction.Create;243 TSource InstructionFunction(Instruction).Name := Keyword;260 Instruction := TSourceFunctionCall.Create; 261 TSourceFunctionCall(Instruction).Name := Keyword; 244 262 for Param in Funct.Parameters do 245 263 if Param.Kind = pkString then begin 246 TSource InstructionFunction(Instruction).AddParameter(ParseReference)264 TSourceFunctionCall(Instruction).AddParameter(ParseReference) 247 265 end else 248 266 if Param.Kind = pkVariable then begin 249 TSource InstructionFunction(Instruction).AddParameter(ParseReferenceVariable(True));267 TSourceFunctionCall(Instruction).AddParameter(ParseReferenceVariable(True)); 250 268 end else 251 269 raise Exception.Create('Unsupported parameter type.'); 252 Code.Instructions.Add(Instruction);270 BeginEnd.Instructions.Add(Instruction); 253 271 end else raise Exception.Create('Unsupported keyword: ' + Token.Text); 254 272 end; … … 265 283 266 284 Tokenizer.Tokenize(Source, SourceTokens); 267 Parse(Source Tokens, SourceCode);285 Parse(SourceCode); 268 286 269 287 Tokenizer.Free; … … 277 295 TargetInstruction: TTargetInstruction; 278 296 begin 279 Target.Instructions.Count := 0;297 { Target.Instructions.Count := 0; 280 298 for I := 0 to SourceCode.Instructions.Count - 1 do begin 281 299 Instruction := TSourceInstruction(SourceCode.Instructions[I]); 282 if Instruction is TSource InstructionFunctionthen begin300 if Instruction is TSourceFunctionCall then begin 283 301 TargetInstruction := TTargetInstruction.Create; 284 302 TargetInstruction.Kind := tiFunction; 285 TargetInstruction.Name := TSource InstructionFunction(Instruction).Name;303 TargetInstruction.Name := TSourceFunctionCall(Instruction).Name; 286 304 Target.Instructions.Add(TargetInstruction) 287 305 end else raise Exception.Create('Unsupported source instruction'); 288 306 end; 307 } 289 308 end; 290 309
Note:
See TracChangeset
for help on using the changeset viewer.