Changeset 19 for branches/DelphiToC/UPascalParser.pas
- Timestamp:
- Apr 9, 2009, 2:08:56 PM (16 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
branches/DelphiToC/UPascalParser.pas
r14 r19 28 28 function IsOperator(Text: string): Boolean; 29 29 procedure ParseProgram(AProgram: TProgram); 30 procedure ParseFunctionList(FunctionList: TFunctionList);31 30 procedure ParseModule(Module: TModule); 32 31 procedure ParseModuleUnit(Module: TModule); 33 32 procedure ParseModuleProgram(Module: TModule); 34 procedure ParseFunction( AFunction: TFunction);33 procedure ParseFunction(FunctionList: TFunctionList); 35 34 procedure ParseVariableList(VariableList: TVariableList); 36 35 procedure ParseVariable(Variable: TVariable); … … 39 38 procedure ParseTypeList(TypeList: TTypeList); 40 39 procedure ParseType(AType: TType); 41 procedure ParseCommonBlockDefinitions(CommonBlock: TCommonBlock );40 procedure ParseCommonBlockDefinitions(CommonBlock: TCommonBlock; EndSymbol: string = ';'); 42 41 function ParseCommonBlockExpression(CommonBlock: TCommonBlock): TExpression; 43 42 procedure ParseCommonBlockProgramCode(CommonBlock: TCommonBlock); 44 43 procedure ParseCommonBlockOperation(CommonBlock: TCommonBlock); 45 procedure Parse;44 procedure Log(Text: string); 46 45 property OnErrorMessage: TOnErrorMessage read FOnErrorMessage write FOnErrorMessage; 47 46 end; … … 58 57 procedure TPascalParser.Expect(Code: string); 59 58 begin 59 Log('Expected: ' + Code + ' Readed: ' + NextCode); 60 60 if NextCode <> Code then begin 61 61 ErrorMessage('Expected ' + Code + ' but ' + NextCode + ' found.'); … … 112 112 begin 113 113 Result := (Character = ' ') or (Character = #13) or (Character = #10); 114 end; 115 116 procedure TPascalParser.Log(Text: string); 117 const 118 LogFileName = 'ParseLog.txt'; 119 var 120 LogFile: TextFile; 121 begin 122 AssignFile(LogFile, LogFileName); 123 if FileExists(LogFileName) then Append(LogFile) 124 else Rewrite(LogFile); 125 WriteLn(LogFile, Text); 126 CloseFile(LogFile); 114 127 end; 115 128 … … 189 202 begin 190 203 Result := NextCode(True); 191 end; 192 193 procedure TPascalParser.ParseFunction(AFunction: TFunction); 194 begin 195 with AFunction do begin 196 if NextCode = 'var' then ParseVariableList(TVariableList(Variables)) 197 else if NextCode = 'const' then ParseConstantList(TConstantList(Constants)) 198 else if NextCode = 'type' then ParseTypeList(TTypeList(Types)) 199 else ParseProgram(ProgramCode); 204 Log('Read: ' + Result); 205 end; 206 207 procedure TPascalParser.ParseFunction(FunctionList: TFunctionList); 208 begin 209 with FunctionList do begin 210 with TFunction(Items[Add(TFunction.Create)]) do begin 211 Expect('procedure'); 212 Name := ReadCode; 213 Expect(';'); 214 ParseCommonBlockDefinitions(Items[Count - 1]); 215 end; 200 216 end; 201 217 end; … … 205 221 I: Integer; 206 222 begin 223 Log('==== Parse start ===='); 207 224 with AProgram do begin 208 225 for I := 0 to Modules.Count - 1 do … … 227 244 228 245 procedure TPascalParser.ParseConstantList(ConstantList: TConstantList); 229 begin 230 // Compiler.Expect('const'); 231 // while Compiler.IsIdentificator(Compiler.NextCode) do 232 // TConstant(Items[Add(TConstant.Create)]).Parse(Compiler); 246 var 247 Identifiers: TStringList; 248 NewValueType: TType; 249 TypeName: string; 250 ConstantName: string; 251 Constant: TConstant; 252 I: Integer; 253 ConstantValue: string; 254 begin 255 Identifiers := TStringList.Create; 256 with ConstantList do begin 257 Expect('const'); 258 while IsIdentificator(NextCode) do begin 259 ConstantName := ReadCode; 260 Constant := Search(ConstantName); 261 if not Assigned(Constant) then begin 262 Identifiers.Add(ConstantName); 263 while NextCode = ',' do begin 264 Expect(','); 265 Identifiers.Add(ReadCode); 266 end; 267 end else ErrorMessage('Pøedefinování existující konstanty.'); 268 Expect(':'); 269 TypeName := ReadCode; 270 NewValueType := Parent.Types.Search(TypeName); 271 Expect('='); 272 ConstantValue := ReadCode; 273 Expect(';'); 274 275 if NewValueType = nil then ErrorMessage('Typ ' + TypeName + ' nebyl definován.') 276 else for I := 0 to Identifiers.Count - 1 do 277 with TConstant(Items[Add(TConstant.Create)]) do begin 278 Name := Identifiers[I]; 279 ValueType := NewValueType; 280 Value := ConstantValue; 281 end; 282 end; 283 end; 284 Identifiers.Destroy; 233 285 end; 234 286 … … 253 305 end; 254 306 end; 255 ParseCommonBlockDefinitions(Module );307 ParseCommonBlockDefinitions(Module, '.'); 256 308 end; 257 309 end; … … 266 318 end; 267 319 268 procedure TPascalParser.Parse; 269 begin 270 271 end; 272 273 procedure TPascalParser.ParseCommonBlockDefinitions(CommonBlock: TCommonBlock); 320 procedure TPascalParser.ParseCommonBlockDefinitions(CommonBlock: TCommonBlock; EndSymbol: string = ';'); 274 321 begin 275 322 with CommonBlock do begin 276 while NextCode <> '.'do begin323 while NextCode <> EndSymbol do begin 277 324 if NextCode = 'var' then ParseVariableList(TVariableList(Variables)) 278 325 else if NextCode = 'const' then ParseConstantList(TConstantList(Constants)) 279 326 else if NextCode = 'type' then ParseTypeList(TTypeList(Types)) 327 else if NextCode = 'procedure' then ParseFunction(Methods) 280 328 else begin 281 329 ParseCommonBlockProgramCode(CommonBlock); … … 283 331 end; 284 332 end; 333 Expect(EndSymbol); 285 334 end; 286 335 end; … … 371 420 372 421 if Identifier[1] = '''' then begin 373 SetLength(TExpression(SubItems[1]).Value, Length(Identifier)); 374 for I := 1 to Length(Identifier) do TExpression(SubItems[1]).Value[I - 1] := Byte(Identifier[I]); 422 TExpression(SubItems[1]).Value := Identifier; 423 //SetLength(TExpression(SubItems[1]).Value, Length(Identifier)); 424 //for I := 1 to Length(Identifier) do 425 // TExpression(SubItems[1]).Value[I - 1] := Byte(Identifier[I]); 375 426 end else begin 376 SetLength(TExpression(SubItems[1]).Value, 1);377 TExpression(SubItems[1]).Value[0] := StrToInt(Identifier);427 //SetLength(TExpression(SubItems[1]).Value, 1); 428 //TExpression(SubItems[1]).Value[0] := StrToInt(Identifier); 378 429 end; 379 430 end; … … 518 569 with TExpression(SubItems[1]) do begin 519 570 NodeType := ntConstant; 520 SetLength(Value, 1); 521 Value[0] := 1; 571 //SetLength(Value, 1); 572 //Value[0] := 1; 573 Value := 1; 522 574 end; 523 575 end; … … 617 669 end; 618 670 619 procedure TPascalParser.ParseFunctionList(FunctionList: TFunctionList);620 begin621 622 end;623 624 671 end.
Note:
See TracChangeset
for help on using the changeset viewer.