Changeset 148 for branches/easy compiler/UCompiler.pas
- Timestamp:
- Jan 17, 2018, 5:27:23 PM (7 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
branches/easy compiler/UCompiler.pas
r147 r148 9 9 10 10 type 11 TSourceTokenKind = (stNone, stString, stIdentifier, stEof, stInteger); 11 TSourceTokenKind = (stNone, stString, stIdentifier, stEof, stInteger, 12 stSpecialSymbol); 12 13 TSourceToken = record 13 14 Text: string; … … 49 50 ): Boolean; 50 51 function ParseFunctionCall(SourceCode: TSourceCode; out FunctionCall: TCommandFunctionCall): Boolean; 51 function ParseIf Zero(SourceCode: TSourceCode; out IfZero: TCommandIfZero): Boolean;52 function ParseIfEqual(SourceCode: TSourceCode; out IfZero: TCommandIfEqual): Boolean; 52 53 function ParseReference(SourceCode: TSourceCode): TSourceReference; 53 54 function ParseReferenceVariable(SourceCode: TSourceCode): TSourceReference; … … 126 127 procedure TTokenizer.Tokenize(Text: string; Tokens: TSourceTokens); 127 128 type 128 TTokenizerState = (tsNone, tsIdentifier, tsString, tsInteger );129 TTokenizerState = (tsNone, tsIdentifier, tsString, tsInteger, tsSpecialSymbol); 129 130 var 130 131 State: TTokenizerState; … … 153 154 Token := ''; 154 155 end else 155 raise Exception.Create('Unexpected character'); 156 if (Text[I] = '[') or (Text[I] = ']') then begin 157 Tokens.AddToken(stSpecialSymbol, Text[I]); 158 end else 159 raise Exception.Create('Unexpected character: ' + Text[I]); 156 160 end else 157 161 if State = tsIdentifier then begin 158 162 if IsAlphaNumeric(Text[I]) then begin 159 163 Token := Token + Text[I]; 160 end else 161 if IsWhiteSpace(Text[I]) then begin 164 end else begin 162 165 Tokens.AddToken(stIdentifier, Token); 163 166 State := tsNone; 164 167 Dec(I); 165 end else 166 raise Exception.Create('Unexpected character'); 168 end; 167 169 end else 168 170 if State = tsInteger then begin 169 171 if IsDigit(Text[I]) then begin 170 172 Token := Token + Text[I]; 171 end else 172 if IsWhiteSpace(Text[I]) then begin 173 end else begin 173 174 Tokens.AddToken(stInteger, Token); 174 175 State := tsNone; 175 176 Dec(I); 176 end else 177 raise Exception.Create('Unexpected character'); 177 end; 178 178 end else 179 179 if State = tsString then begin … … 230 230 var 231 231 Token: TSourceToken; 232 Token2: TSourceToken; 232 233 NewReference: TSourceReference; 233 234 begin … … 239 240 end else 240 241 if Token.Kind = stIdentifier then begin 241 NewReference := TSourceReferenceVariable.Create; 242 TSourceReferenceVariable(NewReference).Variable := 243 SourceCode.Variables.Search(Token.Text); 244 if TSourceReferenceVariable(NewReference).Variable = nil then 245 raise Exception.Create('Variable not found: ' + Token.Text); 242 if Tokenizer.CheckNext('[', stSpecialSymbol) then begin 243 Token2 := Tokenizer.GetNext; 244 NewReference := TSourceReferenceArray.Create; 245 TSourceReferenceArray(NewReference).ArrayRef := SourceCode.Variables.Search(Token.Text); 246 if TSourceReferenceArray(NewReference).ArrayRef = nil then 247 raise Exception.Create('Variable not found: ' + Token.Text); 248 TSourceReferenceArray(NewReference).Index := ParseReference(SourceCode); 249 250 Token2 := Tokenizer.GetNext; 251 if (Token2.Text <> ']') or (Token2.Kind <> stSpecialSymbol) then 252 raise Exception.Create('Expected ]'); 253 end else begin 254 NewReference := TSourceReferenceVariable.Create; 255 TSourceReferenceVariable(NewReference).Variable := 256 SourceCode.Variables.Search(Token.Text); 257 if TSourceReferenceVariable(NewReference).Variable = nil then 258 raise Exception.Create('Variable not found: ' + Token.Text); 259 end; 246 260 end else 247 261 if Token.Kind = stInteger then begin … … 257 271 var 258 272 Token: TSourceToken; 273 Token2: TSourceToken; 259 274 NewReference: TSourceReference; 260 275 begin 261 276 Token := Tokenizer.GetNext; 262 277 if Token.Kind = stIdentifier then begin 263 NewReference := TSourceReferenceVariable.Create; 264 TSourceReferenceVariable(NewReference).Variable := 265 SourceCode.Variables.Search(Token.Text); 266 if TSourceReferenceVariable(NewReference).Variable = nil then 267 raise Exception.Create('Variable not found: ' + Token.Text); 268 278 if Tokenizer.CheckNext('[', stSpecialSymbol) then begin 279 Token2 := Tokenizer.GetNext; 280 NewReference := TSourceReferenceArray.Create; 281 TSourceReferenceArray(NewReference).ArrayRef := SourceCode.Variables.Search(Token.Text); 282 if TSourceReferenceArray(NewReference).ArrayRef = nil then 283 raise Exception.Create('Variable not found: ' + Token.Text); 284 TSourceReferenceArray(NewReference).Index := ParseReference(SourceCode); 285 286 Token2 := Tokenizer.GetNext; 287 if (Token2.Text <> ']') or (Token2.Kind <> stSpecialSymbol) then 288 raise Exception.Create('Expected ]'); 289 end else begin 290 NewReference := TSourceReferenceVariable.Create; 291 TSourceReferenceVariable(NewReference).Variable := 292 SourceCode.Variables.Search(Token.Text); 293 if TSourceReferenceVariable(NewReference).Variable = nil then 294 raise Exception.Create('Variable not found: ' + Token.Text); 295 end; 269 296 end else raise Exception.Create('Unexpected parameter'); 270 297 Result := NewReference; … … 300 327 end; 301 328 302 function TCompiler.ParseIf Zero(SourceCode: TSourceCode; out IfZero: TCommandIfZero): Boolean;329 function TCompiler.ParseIfEqual(SourceCode: TSourceCode; out IfZero: TCommandIfEqual): Boolean; 303 330 var 304 331 Token: TSourceToken; … … 310 337 Token := Tokenizer.GetNext; 311 338 Keyword := LowerCase(Token.Text); 312 if Keyword = 'ifzero' then begin 313 IfZero := TCommandIfZero.Create; 314 IfZero.Variable := TSourceReferenceVariable(ParseReferenceVariable(SourceCode)); 339 if Keyword = 'ifequal' then begin 340 IfZero := TCommandIfEqual.Create; 341 IfZero.Reference1 := ParseReference(SourceCode); 342 IfZero.Reference2 := ParseReference(SourceCode); 315 343 Result := True; 316 344 end; … … 362 390 var 363 391 CommandBeginEnd: TCommandBeginEnd; 364 CommandIfZero: TCommandIf Zero;392 CommandIfZero: TCommandIfEqual; 365 393 CommandFunctionCall: TCommandFunctionCall; 366 394 CommandBreak: TCommandBreak; … … 373 401 Command := CommandBeginEnd; 374 402 end else 375 if ParseIf Zero(SourceCode, CommandIfZero) then begin403 if ParseIfEqual(SourceCode, CommandIfZero) then begin 376 404 Command := CommandIfZero; 377 405 end else
Note:
See TracChangeset
for help on using the changeset viewer.