Changeset 99 for branches/interpreter/Parser3.pas
- Timestamp:
- Feb 6, 2017, 12:11:54 AM (8 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
branches/interpreter/Parser3.pas
r98 r99 27 27 function ParseExecution(Execution: PExecution): Boolean; forward; 28 28 function ParseBeginEnd(BeginEnd: PBeginEnd): Boolean; forward; 29 function ParseGetValue(GetValue: PGetValue): Boolean; forward; 29 30 30 31 … … 165 166 end; 166 167 168 function ParseConstant(out Constant: PConstant): Boolean; 169 var 170 OldPos: Integer; 171 Next: string; 172 begin 173 OldPos := InputTextPos; 174 Next := ReadNext; 175 Constant := MainProgram^.Constants.GetByName(Next); 176 if Constant <> nil then begin 177 Result := True; 178 end else begin 179 Result := False; 180 InputTextPos := OldPos; 181 end; 182 end; 183 167 184 function ParseExpression(Expression: PExpression): Boolean; 168 185 var … … 170 187 OldPos: Integer; 171 188 R: Boolean; 189 GetValue: TGetValue; 190 Execution: TExecution; 172 191 Variable: PVariable; 173 192 SubExpression: TExpression; 174 Execution: TExecution;175 193 begin 176 194 Result := True; … … 180 198 if CheckNext('(') then begin 181 199 Expect('('); 182 R := ParseExpression(@SubExpression); 200 if ParseGetValue(@GetValue) then begin 201 SetLength(Expression^.Items, Length(Expression^.Items) + 1); 202 Expression^.Items[Length(Expression^.Items) - 1] := GetValue; 203 end; 183 204 Expect(')'); 184 205 end else … … 207 228 end; 208 229 230 function ParseGetValue(GetValue: PGetValue): Boolean; 231 var 232 Variable: PVariable; 233 Constant: PConstant; 234 FunctionCall: TExecution; 235 Expression: TExpression; 236 begin 237 if ParseVariable(Variable) then begin 238 GetValue^.Variable := Variable; 239 GetValue^.ReadType := rtVariable; 240 end else 241 if ParseConstant(Constant) then begin 242 GetValue^.Constant := Constant; 243 GetValue^.ReadType := rtConstant; 244 end else 245 if ParseExecution(@FunctionCall) then begin 246 GetValue^.FunctionCall := GetMem(SizeOf(TExecution)); 247 GetValue^.FunctionCall^ := FunctionCall; 248 GetValue^.ReadType := rtFunctionCall; 249 end else 250 if ParseExpression(@Expression) then begin 251 GetValue^.Expression := GetMem(SizeOf(TExpression)); 252 GetValue^.Expression^ := Expression; 253 GetValue^.ReadType := rtExpression; 254 end else 255 ShowError('Expected value'); 256 end; 257 209 258 function ParseAssignment: Boolean; 210 259 var … … 216 265 Assignment.Destination := Variable; 217 266 Expect(':='); 218 Parse Expression(@Assignment.Source);267 ParseGetValue(@Assignment.Source); 219 268 end else begin 220 269 Result := False; … … 228 277 Func: PFunction; 229 278 I: Integer; 230 Expression: TExpression;231 279 begin 232 280 Result := True; … … 235 283 Func := MainProgram.Functions.GetByName(Next); 236 284 if Func <> nil then begin 285 SetLength(Execution^.Parameters.Items, Length(Func^.Parameters.Items)); 237 286 if Length(Func^.Parameters.Items) > 0 then begin 238 287 Expect('('); 239 288 I := 0; 240 289 while I < Length(Func^.Parameters.Items) do begin 241 Parse Expression(@Expression);290 ParseGetValue(@Execution^.Parameters.Items[I]); 242 291 if I < (Length(Func^.Parameters.Items) - 1) then Expect(','); 243 292 I := I + 1; … … 251 300 end; 252 301 253 function ParseCommand : Boolean;302 function ParseCommand(Command: PCommand): Boolean; 254 303 var 255 304 IfThenElse: TIfThenElse; … … 257 306 BeginEnd: TBeginEnd; 258 307 Execution: TExecution; 308 Assignment: TAssignment; 259 309 begin 260 310 Result := True; 261 311 if ParseBeginEnd(@BeginEnd) then begin 312 Command^.BeginEnd := GetMem(SizeOf(TBeginEnd)); 313 Command^.BeginEnd^ := BeginEnd; 314 Command^.CmdType := ctBeginEnd; 262 315 end else 263 316 if ParseAssignment then begin 317 Command^.Assignment := GetMem(SizeOf(TAssignment)); 318 Command^.Assignment^ := Assignment; 319 Command^.CmdType := ctAssignment; 264 320 end else 265 321 if ParseExecution(@Execution) then begin 322 Command^.Execution := GetMem(SizeOf(TExecution)); 323 Command^.Execution^ := Execution; 324 Command^.CmdType := ctExecution; 266 325 end else 267 326 if ParseIfThen(@IfThenElse) then begin 327 Command^.IfThenElse := GetMem(SizeOf(TIfThenElse)); 328 Command^.IfThenElse^ := IfThenElse; 329 Command^.CmdType := ctIfThenElse; 268 330 end else 269 331 if ParseWhileDo(@WhileDo) then begin 332 Command^.WhileDo := GetMem(SizeOf(TWhileDo)); 333 Command^.WhileDo^ := WhileDo; 334 Command^.CmdType := ctWhileDo; 270 335 end else Result := False; 271 336 end; … … 278 343 ParseExpression(@IfThenElse.Condition); 279 344 Expect('then'); 280 ParseCommand ;345 ParseCommand(@IfThenElse^.DoThen); 281 346 if CheckNext('else') then begin 282 347 Expect('else'); 283 ParseCommand ;348 ParseCommand(@IfThenElse^.DoElse); 284 349 end; 285 350 end else Result := False; … … 293 358 ParseExpression(@WhileDo.Condition); 294 359 Expect('do'); 295 ParseCommand ;360 ParseCommand(@WhileDo^.Command); 296 361 end else Result := False; 297 362 end; 298 363 299 364 function ParseBeginEnd(BeginEnd: PBeginEnd): Boolean; 365 var 366 Command: TCommand; 300 367 begin 301 368 if CheckNext('begin') then begin 302 369 Result := True; 303 370 Expect('begin'); 371 SetLength(BeginEnd^.Commands, 0); 304 372 repeat 305 if ParseCommand then begin 373 if ParseCommand(@Command) then begin 374 BeginEnd^.Add; 375 BeginEnd^.GetLast^ := Command; 306 376 Expect(';'); 307 377 end else
Note:
See TracChangeset
for help on using the changeset viewer.