Changeset 11 for branches/Void/UCompilator.pas
- Timestamp:
- Nov 9, 2009, 4:15:39 PM (15 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
branches/Void/UCompilator.pas
r10 r11 17 17 FOnError: TOnErrorEvent; 18 18 procedure DoError(Text: string); 19 procedure ParseBeginEnd; 20 procedure ParseProgram; 21 procedure ParseVariableDefinition; 19 22 public 20 23 Model: TModel; … … 23 26 Parser: TVoidParser; 24 27 procedure Compile; 25 procedure Process;26 28 constructor Create; 27 29 destructor Destroy; override; … … 42 44 end; 43 45 46 procedure TCompilator.ParseProgram; 47 begin 48 while SourceCode.Position < SourceCode.Size do 49 with Model, Module, BeginEnd do begin 50 if Parser.TokenType = ttWhiteSpace then begin 51 end else 52 if Parser.TokenType = ttIdentifier then begin 53 if Parser.TokenValue = 'program' then begin 54 Parser.ParseNextToken; 55 if Parser.TokenType <> ttWhiteSpace then DoError('Expect white space'); 56 Parser.ParseNextToken; 57 if Parser.TokenType <> ttString then DoError('Expect string'); 58 Parser.ParseNextToken; 59 if (Parser.TokenType <> ttSymbol) and (Parser.TokenValue <> ';') then DoError('Expect ;'); 60 Parser.ParseNextToken; 61 end else 62 if Parser.TokenValue = 'var' then begin 63 ParseVariableDefinition; 64 end else 65 if Parser.TokenValue = 'begin' then begin 66 ParseBeginEnd; 67 end; 68 end; 69 end; 70 end; 71 72 procedure TCompilator.ParseVariableDefinition; 73 var 74 VariableName: string; 75 VariableType: string; 76 Variable: TVariable; 77 begin 78 repeat 79 Parser.ParseNextToken; 80 if Parser.TokenType <> ttWhiteSpace then DoError('Expected white space'); 81 Parser.ParseNextToken; 82 if Parser.TokenType <> ttIdentifier then DoError('Expected identifier'); 83 VariableName := Parser.TokenValue; 84 Parser.ParseNextToken; 85 if Parser.TokenType = ttWhiteSpace then Parser.ParseNextToken; 86 if (Parser.TokenType <> ttSymbol) and (Parser.TokenValue = ':') then 87 DoError('Expected :'); 88 Parser.ParseNextToken; 89 if Parser.TokenType = ttWhiteSpace then Parser.ParseNextToken; 90 if Parser.TokenType <> ttIdentifier then DoError('Expected identifier'); 91 VariableType := Parser.TokenValue; 92 93 with Model.Module do begin 94 Variable := FindVariableByName(VariableName); 95 if Assigned(Variable) then DoError('Variable ' + VariableName + ' redefined') 96 else begin 97 Variable := TVariable.Create; 98 with Variable do begin 99 Name := VariableName; 100 VarType := VariableType; 101 end; 102 Variables.Add(Variable); 103 end; 104 end; 105 106 Parser.ParseNextToken; 107 if (Parser.TokenType <> ttSymbol) and (Parser.TokenValue = ';') then 108 DoError('Expected ;'); 109 Parser.ParseNextToken; 110 if Parser.TokenType <> ttWhiteSpace then DoError('Expected white space'); 111 Parser.ParseNextToken; 112 until (Parser.TokenType = ttIdentifier) and ((Parser.TokenValue = 'begin') or 113 (Parser.TokenValue = 'type')); 114 end; 115 44 116 procedure TCompilator.Compile; 45 117 var … … 49 121 SourceCode.Position := 0; 50 122 Parser.Open(SourceCode); 51 52 // Process source lines 53 while SourceCode.Position < SourceCode.Size do begin; 54 Process; 55 Parser.ParseNextToken; 56 end; 57 123 ParseProgram; 58 124 Generator.Generate(Model); 59 125 end; 60 126 61 procedure TCompilator.P rocess;127 procedure TCompilator.ParseBeginEnd; 62 128 var 63 129 CommandName: string; … … 67 133 Value: string; 68 134 begin 69 with Model, BeginEnd do begin 135 with Model, Module, BeginEnd do begin 136 if Parser.TokenType = ttWhiteSpace then begin 137 end else 70 138 if Parser.TokenType = ttIdentifier then begin 71 139 CommandName := Parser.TokenValue; 72 if CommandName = 'Define' then begin 73 Parser.ParseNextToken; 74 if Parser.TokenType <> ttWhiteSpace then DoError('Expected white space'); 75 Parser.ParseNextToken; 76 if Parser.TokenType <> ttIdentifier then DoError('Expected identifier'); 77 VariableName := Parser.TokenValue; 78 Variable := FindVariableByName(VariableName); 79 if Assigned(Variable) then DoError('Variable ' + VariableName + ' redefined') 80 else begin 81 Variable := TVariable.Create; 82 with Variable do begin 83 Name := VariableName; 84 VarType := 'string'; 85 end; 86 Variables.Add(Variable); 87 end; 88 end else begin 140 begin 89 141 Command := FindProcedureByName(CommandName); 90 142 if Assigned(Command) then begin … … 131 183 end; 132 184 end; 185 Parser.ParseNextToken; 186 if Parser.TokenType <> ttWhiteSpace then DoError('Expected white space'); 133 187 end else DoError('Expected identifier'); 134 Parser.ParseNextToken;135 if Parser.TokenType <> ttWhiteSpace then DoError('Expected white space');136 188 end; 137 189 end;
Note:
See TracChangeset
for help on using the changeset viewer.