Changeset 97 for branches/interpreter/Parser3.pas
- Timestamp:
- Feb 2, 2017, 11:35:09 PM (8 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
branches/interpreter/Parser3.pas
r96 r97 24 24 25 25 function ParseIfThen(IfThenElse: PIfThenElse): Boolean; forward; 26 function ParseExecution: Boolean; forward; 26 function ParseWhileDo(WhileDo: PWhileDo): Boolean; forward; 27 function ParseExecution(Execution: PExecution): Boolean; forward; 28 function ParseBeginEnd(BeginEnd: PBeginEnd): Boolean; forward; 27 29 28 30 … … 35 37 begin 36 38 Result := (C = ';') or (C = '(') or (C = ')') or (C = ':') or (C = '=') or 37 (C = '+') or (C = '-') ;39 (C = '+') or (C = '-') or (C = ';') or (C = '.'); 38 40 end; 39 41 … … 142 144 function IsOperator(Text: string): Boolean; 143 145 begin 144 Result := (Text = '=') or (Text = '<>') or (Text = '>') or (Text = '<') or 146 Result := (Text = '+') or (Text = '-') or 147 (Text = '=') or (Text = '<>') or (Text = '>') or (Text = '<') or 145 148 (Text = '<=') or (Text = '>='); 146 149 end; … … 169 172 Variable: PVariable; 170 173 SubExpression: TExpression; 174 Execution: TExecution; 171 175 begin 172 176 Result := True; 177 if CheckNext('not') then begin 178 Expect('not'); 179 end; 173 180 if CheckNext('(') then begin 174 181 Expect('('); … … 177 184 end else 178 185 if ParseVariable(Variable) then begin 186 OldPos := InputTextPos; 179 187 Next := ReadNext; 180 188 if IsOperator(Next) then begin … … 183 191 184 192 //end else ShowError('Expected variable'); 185 end else ShowError('Exprected operator but found ' + ReadNext);193 end else InputTextPos := OldPos; 186 194 end else 187 195 if LastTokenType = ttString then begin 188 189 end else 190 if ParseExecution then begin196 ReadNext 197 end else 198 if ParseExecution(@Execution) then begin 191 199 end else 192 200 ShowError('Expected variable but found ' + ReadNext); … … 194 202 OldPos := InputTextPos; 195 203 Next := ReadNext; 196 if IsLogicOperator(Next) then begin204 if IsLogicOperator(Next) or IsOperator(Next) then begin 197 205 R := ParseExpression(@SubExpression); 198 206 end else InputTextPos := OldPos; … … 214 222 end; 215 223 216 function ParseExecution : Boolean;224 function ParseExecution(Execution: PExecution): Boolean; 217 225 var 218 226 OldPos: Integer; … … 220 228 Func: PFunction; 221 229 I: Integer; 230 Expression: TExpression; 222 231 begin 223 232 Result := True; … … 230 239 I := 0; 231 240 while I < Length(Func^.Parameters.Items) do begin 232 Next := ReadNext;241 ParseExpression(@Expression); 233 242 if I < (Length(Func^.Parameters.Items) - 1) then Expect(','); 234 243 I := I + 1; … … 245 254 var 246 255 IfThenElse: TIfThenElse; 256 WhileDo: TWhileDo; 257 BeginEnd: TBeginEnd; 258 Execution: TExecution; 247 259 begin 248 260 Result := True; 261 if ParseBeginEnd(@BeginEnd) then begin 262 end else 249 263 if ParseAssignment then begin 250 264 end else 251 if ParseExecution then begin 252 265 if ParseExecution(@Execution) then begin 253 266 end else 254 267 if ParseIfThen(@IfThenElse) then begin 255 268 end else 269 if ParseWhileDo(@WhileDo) then begin 256 270 end else Result := False; 257 271 end; … … 269 283 ParseCommand; 270 284 end; 285 end else Result := False; 286 end; 287 288 function ParseWhileDo(WhileDo: PWhileDo): Boolean; 289 begin 290 if CheckNext('while') then begin 291 Result := True; 292 Expect('while'); 293 ParseExpression(@WhileDo.Condition); 294 Expect('do'); 295 ParseCommand; 271 296 end else Result := False; 272 297 end; … … 364 389 ParseParams(@Func^.Parameters); 365 390 Expect(';'); 391 if ParseVariableSection then begin 392 end; 366 393 ParseBeginEnd(@Func^.BeginEnd); 367 394 Expect(';'); … … 396 423 ProgramCode^.Variables.Add(VariableCreate('Result', TypeString)); 397 424 ProgramCode^.Variables.Add(VariableCreate('C', TypeChar)); 425 ProgramCode^.Variables.Add(VariableCreate('Text', TypeString)); 398 426 399 427 SetLength(ProgramCode^.Functions.Items, 0);
Note:
See TracChangeset
for help on using the changeset viewer.