Changeset 201 for branches/interpreter2/UParser.pas
- Timestamp:
- Apr 16, 2020, 7:40:38 PM (5 years ago)
- Location:
- branches/interpreter2
- Files:
-
- 2 edited
Legend:
- Unmodified
- Added
- Removed
-
branches/interpreter2
-
Property svn:ignore
set to
lib
interpreter
interpreter.lps
interpreter.res
heaptrclog.trc
-
Property svn:ignore
set to
-
branches/interpreter2/UParser.pas
r200 r201 16 16 FOnError: TErrorEvent; 17 17 Tokenizer: TTokenizer; 18 function ParseBeginEnd(Block: TBlock; var BeginEnd: TBeginEnd): Boolean; 19 function ParseFunctionCall(Block: TBlock; var FunctionCall: TFunctionCall): Boolean; 20 function ParseCommand(Block: TBlock; var Command: TCommand): Boolean; 21 function ParseProgram(SystemBlock: TBlock; var Prog: TProgram): Boolean; 22 function ParseBlock(ParentBlock: TBlock; var Block: TBlock): Boolean; 23 function ParseAssignment(Block: TBlock; var Assignment: TAssignment): Boolean; 24 function ParseExpression(Block: TBlock; var Expression: TExpression): Boolean; 25 function ParseIfThenElse(Block: TBlock; var IfThenElse: TIfThenElse): Boolean; 26 function ParseWhileDo(Block: TBlock; var WhileDo: TWhileDo): Boolean; 18 function ParseBeginEnd(Block: TBlock; out BeginEnd: TBeginEnd): Boolean; 19 function ParseFunctionCall(Block: TBlock; out FunctionCall: TFunctionCall): Boolean; 20 function ParseCommand(Block: TBlock; out Command: TCommand): Boolean; 21 function ParseProgram(SystemBlock: TBlock; out Prog: TProgram): Boolean; 22 function ParseBlock(ParentBlock: TBlock; out Block: TBlock): Boolean; 23 function ParseVarBlock(Block: TBlock): Boolean; 24 function ParseConstBlock(Block: TBlock): Boolean; 25 function ParseAssignment(Block: TBlock; out Assignment: TAssignment): Boolean; 26 function ParseExpression(Block: TBlock; out Expression: TExpression): Boolean; 27 function ParseIfThenElse(Block: TBlock; out IfThenElse: TIfThenElse): Boolean; 28 function ParseWhileDo(Block: TBlock; out WhileDo: TWhileDo): Boolean; 27 29 procedure TokenizerError(Pos: TPoint; Text: string); 28 30 procedure InitSystemBlock(Block: TBlock); … … 42 44 { TParser } 43 45 44 function TParser.ParseBeginEnd(Block: TBlock; varBeginEnd: TBeginEnd): Boolean;46 function TParser.ParseBeginEnd(Block: TBlock; out BeginEnd: TBeginEnd): Boolean; 45 47 var 46 48 Command: TCommand; … … 64 66 end; 65 67 66 function TParser.ParseFunctionCall(Block: TBlock; varFunctionCall: TFunctionCall68 function TParser.ParseFunctionCall(Block: TBlock; out FunctionCall: TFunctionCall 67 69 ): Boolean; 68 70 var … … 97 99 end; 98 100 99 function TParser.ParseCommand(Block: TBlock; varCommand: TCommand): Boolean;101 function TParser.ParseCommand(Block: TBlock; out Command: TCommand): Boolean; 100 102 var 101 103 BeginEnd: TBeginEnd; … … 127 129 end; 128 130 129 function TParser.ParseProgram(SystemBlock: TBlock; varProg: TProgram): Boolean;131 function TParser.ParseProgram(SystemBlock: TBlock; out Prog: TProgram): Boolean; 130 132 var 131 133 Block: TBlock; … … 134 136 Result := False; 135 137 Prog := TProgram.Create; 138 Prog.SystemBlock.Free; 136 139 Prog.SystemBlock := SystemBlock; 137 140 if Tokenizer.CheckNext('program', tkKeyword) then begin … … 144 147 if ParseBlock(SystemBlock, Block) then begin 145 148 Result := True; 149 Prog.Block.Free; 146 150 Prog.Block := Block; 147 151 Tokenizer.Expect('.', tkSpecialSymbol); … … 152 156 end; 153 157 154 function TParser.ParseBlock(ParentBlock: TBlock; var Block: TBlock): Boolean; 155 var 156 Token: TToken; 157 Variable: TVariable; 158 Constant: TConstant; 159 begin 158 function TParser.ParseBlock(ParentBlock: TBlock; out Block: TBlock): Boolean; 159 var 160 BeginEnd: TBeginEnd; 161 begin 162 Result := False; 160 163 Block := TBlock.Create; 161 164 Block.Parent := ParentBlock; 165 ParseVarBlock(Block); 166 ParseConstBlock(Block); 167 if ParseBeginEnd(Block, BeginEnd) then begin 168 Result := True; 169 Block.BeginEnd.Free; 170 Block.BeginEnd := BeginEnd; 171 end else Block.Free; 172 end; 173 174 function TParser.ParseVarBlock(Block: TBlock): Boolean; 175 var 176 Token: TToken; 177 Variable: TVariable; 178 begin 162 179 if Tokenizer.CheckNext('var', tkKeyword) then begin 180 Result := True; 163 181 Tokenizer.Expect('var', tkKeyword); 164 182 while Tokenizer.CheckNextKind(tkIdentifier) do begin … … 177 195 end; 178 196 end; 179 end; 197 end else Result := False; 198 end; 199 200 function TParser.ParseConstBlock(Block: TBlock): Boolean; 201 var 202 Token: TToken; 203 Constant: TConstant; 204 begin 180 205 if Tokenizer.CheckNext('const', tkKeyword) then begin 206 Result := True; 181 207 Tokenizer.Expect('const', tkKeyword); 182 208 while Tokenizer.CheckNextKind(tkIdentifier) do begin … … 200 226 end; 201 227 end; 202 end; 203 Result := ParseBeginEnd(Block, Block.BeginEnd); 204 //if not Result then Block.Free; 205 end; 206 207 function TParser.ParseAssignment(Block: TBlock; var Assignment: TAssignment): Boolean; 228 end else Result := False; 229 end; 230 231 function TParser.ParseAssignment(Block: TBlock; out Assignment: TAssignment): Boolean; 208 232 var 209 233 Token: TToken; 210 234 Variable: TVariable; 235 Expression: TExpression; 211 236 begin 212 237 Result := False; … … 220 245 Assignment.Variable := Variable; 221 246 Tokenizer.Expect(':=', tkSpecialSymbol); 222 ParseExpression(Block, Assignment.Expression); 247 if ParseExpression(Block, Expression) then begin 248 Assignment.Expression.Free; 249 Assignment.Expression := Expression; 250 end; 223 251 end else Error('Variable ' + Token.Text + ' not defined.'); 224 252 end; 225 253 end; 226 254 227 function TParser.ParseExpression(Block: TBlock; varExpression: TExpression255 function TParser.ParseExpression(Block: TBlock; out Expression: TExpression 228 256 ): Boolean; 229 257 var … … 239 267 Result := True; 240 268 Expression := TExpression.Create; 241 Expression.Variable := Variable;269 Expression.VariableRef := Variable; 242 270 end else begin 243 271 Constant := Block.Constants.SearchByName(Token.Text); … … 245 273 Result := True; 246 274 Expression := TExpression.Create; 247 Expression.Constant := Constant;275 Expression.ConstantRef := Constant; 248 276 end; 249 277 end; … … 251 279 if Token.Kind = tkNumber then begin 252 280 Result := True; 281 Constant := Block.Constants.AddNew('_C' + IntToStr(Block.Constants.Count)); 282 Constant.Value := Token.Text; 253 283 Expression := TExpression.Create; 254 Expression.Constant := TConstant.Create; 255 Expression.Constant.Value := Token.Text; 284 Expression.ConstantRef := Constant; 256 285 end else 257 286 if Token.Kind = tkString then begin 258 287 Result := True; 288 Constant := Block.Constants.AddNew('_C' + IntToStr(Block.Constants.Count)); 289 Constant.Value := Token.Text; 259 290 Expression := TExpression.Create; 260 Expression.Constant := TConstant.Create; 261 Expression.Constant.Value := Token.Text; 262 end; 263 end; 264 265 function TParser.ParseIfThenElse(Block: TBlock; var IfThenElse: TIfThenElse 291 Expression.ConstantRef := Constant; 292 end; 293 end; 294 295 function TParser.ParseIfThenElse(Block: TBlock; out IfThenElse: TIfThenElse 266 296 ): Boolean; 267 297 var … … 275 305 IfThenElse := TIfThenElse.Create; 276 306 if ParseExpression(Block, Expression) then begin 307 IfThenElse.Expression.Free; 277 308 IfThenElse.Expression := Expression; 278 309 Tokenizer.Expect('then', tkKeyword); 279 310 if ParseCommand(Block, Command) then begin 311 IfThenElse.CommandThen.Free; 280 312 IfThenElse.CommandThen := Command; 281 313 if Tokenizer.CheckNext('else', tkKeyword) then begin 282 314 Tokenizer.Expect('else', tkKeyword); 283 315 if ParseCommand(Block, Command) then begin 316 IfThenElse.CommandElse.Free; 284 317 IfThenElse.CommandElse := Command; 285 318 end else Error('Expected command'); … … 290 323 end; 291 324 292 function TParser.ParseWhileDo(Block: TBlock; varWhileDo: TWhileDo): Boolean;325 function TParser.ParseWhileDo(Block: TBlock; out WhileDo: TWhileDo): Boolean; 293 326 var 294 327 Expression: TExpression; … … 301 334 WhileDo := TWhileDo.Create; 302 335 if ParseExpression(Block, Expression) then begin 336 WhileDo.Expression.Free; 303 337 WhileDo.Expression := Expression; 304 338 Tokenizer.Expect('do', tkKeyword); 305 339 if ParseCommand(Block, Command) then begin 340 WhileDo.Command.Free; 306 341 WhileDo.Command := Command; 307 342 end else Error('Expected command');
Note:
See TracChangeset
for help on using the changeset viewer.