- Timestamp:
- Apr 16, 2020, 7:40:38 PM (5 years ago)
- Location:
- branches/interpreter2
- Files:
-
- 9 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/UExecutor.pas
r200 r201 266 266 Expression: TExpression): string; 267 267 begin 268 if Assigned(Expression.Variable ) then begin269 Result := Block.Variables.SearchByVariable(Expression.Variable ).Value;268 if Assigned(Expression.VariableRef) then begin 269 Result := Block.Variables.SearchByVariable(Expression.VariableRef).Value; 270 270 end else 271 if Assigned(Expression.Constant ) then begin272 Result := Expression.Constant .Value;271 if Assigned(Expression.ConstantRef) then begin 272 Result := Expression.ConstantRef.Value; 273 273 end else 274 274 if Assigned(Expression.FunctionCall) then begin -
branches/interpreter2/UFormMain.lfm
r200 r201 9 9 DesignTimePPI = 144 10 10 OnActivate = FormActivate 11 OnClose = FormClose 12 OnDestroy = FormDestroy 11 13 OnShow = FormShow 12 14 LCLVersion = '2.0.2.0' -
branches/interpreter2/UFormMain.pas
r200 r201 23 23 procedure ButtonRunClick(Sender: TObject); 24 24 procedure FormActivate(Sender: TObject); 25 procedure FormClose(Sender: TObject; var CloseAction: TCloseAction); 26 procedure FormDestroy(Sender: TObject); 25 27 procedure FormShow(Sender: TObject); 26 28 private … … 56 58 end; 57 59 60 procedure TFormMain.FormClose(Sender: TObject; var CloseAction: TCloseAction); 61 begin 62 63 end; 64 65 procedure TFormMain.FormDestroy(Sender: TObject); 66 begin 67 if Assigned(Prog) then Prog.Free; 68 end; 69 58 70 procedure TFormMain.FormShow(Sender: TObject); 59 71 begin … … 85 97 Parser.Source := MemoSource.Lines.Text; 86 98 Parser.Parse; 99 if Assigned(Prog) then Prog.Free; 87 100 Prog := Parser.Prog; 88 101 Parser.Free; -
branches/interpreter2/UInterpreter.pas
r200 r201 18 18 public 19 19 Source: string; 20 procedure Compile; 20 21 procedure Run; 21 22 constructor Create; … … 26 27 27 28 { TInterpreter } 29 30 procedure TInterpreter.Compile; 31 begin 32 Parser.Source := Source; 33 Parser.Parse; 34 end; 28 35 29 36 procedure TInterpreter.Run; -
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'); -
branches/interpreter2/USource.pas
r200 r201 30 30 TConstants = class(TObjectList) 31 31 function SearchByName(Name: string): TConstant; 32 function AddNew(Name: string): TConstant; 32 33 end; 33 34 … … 66 67 67 68 TExpression = class 68 Variable : TVariable;69 Constant : TConstant;69 VariableRef: TVariable; 70 ConstantRef: TConstant; 70 71 FunctionCall: TFunctionCall; 71 72 end; … … 73 74 TExpressions = class(TObjectList) 74 75 end; 76 77 { TAssignment } 75 78 76 79 TAssignment = class(TCommand) 77 80 Variable: TVariable; 78 81 Expression: TExpression; 79 end; 82 constructor Create; 83 destructor Destroy; override; 84 end; 85 86 { TIfThenElse } 80 87 81 88 TIfThenElse = class(TCommand) … … 83 90 CommandThen: TCommand; 84 91 CommandElse: TCommand; 85 end; 92 constructor Create; 93 destructor Destroy; override; 94 end; 95 96 { TWhileDo } 86 97 87 98 TWhileDo = class(TCommand) 88 99 Expression: TExpression; 89 100 Command: TCommand; 101 constructor Create; 102 destructor Destroy; override; 90 103 end; 91 104 … … 98 111 Functions: TFunctions; 99 112 BeginEnd: TBeginEnd; 113 procedure Clear; 100 114 function GetFunction(Name: string): TFunction; 101 115 constructor Create; … … 116 130 implementation 117 131 132 { TAssignment } 133 134 constructor TAssignment.Create; 135 begin 136 Variable := nil; 137 Expression := TExpression.Create; 138 end; 139 140 destructor TAssignment.Destroy; 141 begin 142 Variable := nil; 143 Expression.Free; 144 inherited Destroy; 145 end; 146 147 { TIfThenElse } 148 149 constructor TIfThenElse.Create; 150 begin 151 Expression := TExpression.Create; 152 CommandThen := TCommand.Create; 153 CommandElse := TCommand.Create; 154 end; 155 156 destructor TIfThenElse.Destroy; 157 begin 158 Expression.Free; 159 CommandThen.Free; 160 CommandElse.Free; 161 inherited Destroy; 162 end; 163 164 { TWhileDo } 165 166 constructor TWhileDo.Create; 167 begin 168 Expression := TExpression.Create; 169 Command := TCommand.Create; 170 end; 171 172 destructor TWhileDo.Destroy; 173 begin 174 Expression.Free; 175 Command.Free; 176 inherited Destroy; 177 end; 178 118 179 { TFunctionCall } 119 180 … … 160 221 end; 161 222 223 function TConstants.AddNew(Name: string): TConstant; 224 begin 225 Result := TConstant.Create; 226 Result.Name := Name; 227 Add(Result); 228 end; 229 162 230 { TVariables } 163 231 … … 174 242 { TBlock } 175 243 244 procedure TBlock.Clear; 245 begin 246 Functions.Clear; 247 Constants.Clear; 248 Variables.Clear; 249 end; 250 176 251 function TBlock.GetFunction(Name: string): TFunction; 177 252 begin -
branches/interpreter2/interpreter.lpi
r200 r201 15 15 <Icon Value="0"/> 16 16 </General> 17 <BuildModes Count="1"> 18 <Item1 Name="Default" Default="True"/> 17 <BuildModes Count="2"> 18 <Item1 Name="Debug" Default="True"/> 19 <Item2 Name="Release"> 20 <CompilerOptions> 21 <Version Value="11"/> 22 <Target> 23 <Filename Value="interpreter"/> 24 </Target> 25 <SearchPaths> 26 <IncludeFiles Value="$(ProjOutDir)"/> 27 <UnitOutputDirectory Value="lib/$(TargetCPU)-$(TargetOS)-$(BuildMode)"/> 28 </SearchPaths> 29 <Parsing> 30 <SyntaxOptions> 31 <SyntaxMode Value="Delphi"/> 32 <CStyleOperator Value="False"/> 33 <AllowLabel Value="False"/> 34 <CPPInline Value="False"/> 35 </SyntaxOptions> 36 </Parsing> 37 <CodeGeneration> 38 <SmartLinkUnit Value="True"/> 39 <Optimizations> 40 <OptimizationLevel Value="3"/> 41 </Optimizations> 42 </CodeGeneration> 43 <Linking> 44 <Debugging> 45 <GenerateDebugInfo Value="False"/> 46 </Debugging> 47 <LinkSmart Value="True"/> 48 <Options> 49 <Win32> 50 <GraphicApplication Value="True"/> 51 </Win32> 52 </Options> 53 </Linking> 54 </CompilerOptions> 55 </Item2> 19 56 </BuildModes> 20 57 <PublishOptions> … … 72 109 <SearchPaths> 73 110 <IncludeFiles Value="$(ProjOutDir)"/> 74 <UnitOutputDirectory Value="lib/$(TargetCPU)-$(TargetOS) "/>111 <UnitOutputDirectory Value="lib/$(TargetCPU)-$(TargetOS)-$(BuildMode)"/> 75 112 </SearchPaths> 76 113 <Parsing> 77 114 <SyntaxOptions> 78 115 <SyntaxMode Value="Delphi"/> 116 <CStyleOperator Value="False"/> 117 <IncludeAssertionCode Value="True"/> 118 <AllowLabel Value="False"/> 119 <CPPInline Value="False"/> 79 120 </SyntaxOptions> 80 121 </Parsing> 122 <CodeGeneration> 123 <Checks> 124 <IOChecks Value="True"/> 125 <RangeChecks Value="True"/> 126 <OverflowChecks Value="True"/> 127 <StackChecks Value="True"/> 128 </Checks> 129 <VerifyObjMethodCallValidity Value="True"/> 130 </CodeGeneration> 81 131 <Linking> 132 <Debugging> 133 <UseHeaptrc Value="True"/> 134 </Debugging> 82 135 <Options> 83 136 <Win32> -
branches/interpreter2/interpreter.lpr
r200 r201 7 7 cthreads, 8 8 {$ENDIF}{$ENDIF} 9 Interfaces, // this includes the LCL widgetset9 Interfaces, SysUtils, // this includes the LCL widgetset 10 10 Forms, UFormMain, UParser, UTokenizer, USource, UExecutor, UInterpreter 11 11 { you can add units after this }; … … 13 13 {$R *.res} 14 14 15 {$if declared(UseHeapTrace)} 16 const 17 HeapTraceLog = 'heaptrclog.trc'; 18 {$ENDIF} 19 15 20 begin 21 {$if declared(UseHeapTrace)} 22 DeleteFile(ExtractFilePath(ParamStr(0)) + HeapTraceLog); 23 SetHeapTraceOutput(ExtractFilePath(ParamStr(0)) + HeapTraceLog); 24 {$ENDIF} 16 25 RequireDerivedFormResource:=True; 17 26 Application.Scaled:=True;
Note:
See TracChangeset
for help on using the changeset viewer.