- Timestamp:
- Feb 2, 2017, 11:35:09 PM (8 years ago)
- Location:
- branches/interpreter
- Files:
-
- 5 edited
Legend:
- Unmodified
- Added
- Removed
-
branches/interpreter/Execute3.pas
r96 r97 11 11 implementation 12 12 13 procedure ExecuteCommand(Command: PCommand); forward; 14 15 procedure ExecuteBeginEnd(BeginEnd: PBeginEnd); 16 var 17 I: Integer; 18 begin 19 for I := 0 to Length(BeginEnd^.Commands) - 1 do 20 ExecuteCommand(@BeginEnd^.Commands[I]); 21 end; 22 23 function ExecuteExpression(Expression: PExpression): Boolean; 24 begin 25 26 end; 27 28 procedure ExecuteWhileDo(WhileDo: PWhileDo); 29 begin 30 if ExecuteExpression(@WhileDo^.Condition) then 31 ExecuteCommand(@WhileDo^.Command); 32 end; 33 34 procedure ExecuteIfThenElse(IfThenElse: PIfThenElse); 35 begin 36 if ExecuteExpression(@IfThenElse^.Condition) then 37 ExecuteCommand(@IfThenElse^.DoThen) 38 else ExecuteCommand(@IfThenElse^.DoElse); 39 end; 40 41 procedure ExecuteExecution(Execution: PExecution); 42 begin 43 // Set parameters 44 ExecuteBeginEnd(@Execution^.Func^.BeginEnd); 45 end; 46 47 procedure ExecuteAssignment(Assignment: PAssignment); 48 begin 49 50 end; 51 52 procedure ExecuteCommand(Command: PCommand); 53 begin 54 case Command^.CmdType of 55 ctBeginEnd: ExecuteBeginEnd(PBeginEnd(Command^.Ptr)); 56 ctWhileDo: ExecuteWhileDo(PWhileDo(Command^.Ptr)); 57 ctIfThenElse: ExecuteIfThenElse(PIfThenElse(Command^.Ptr)); 58 ctExecution: ExecuteExecution(PExecution(Command^.Ptr)); 59 ctAssignment: ExecuteAssignment(PAssignment(Command^.Ptr)); 60 end; 61 end; 62 13 63 procedure ExecuteProgram(ProgramCode: PProgramCode); 14 64 begin 15 65 ExecuteBeginEnd(@ProgramCode^.BeginEnd); 16 66 end; 17 67 -
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); -
branches/interpreter/Source3.pas
r96 r97 49 49 TFunctionCall = procedure ; 50 50 51 TCmdType = (ctWhileDo, ctIfThenElse, ctBeginEnd, ctAssignment, ctExecution); 51 52 TCommand = record 52 end; 53 CmdType: TCmdType; 54 Ptr: Pointer; 55 { case Integer of 56 0: (WhileDo: TWhileDo); 57 1: (IfThenElse: TIfThenElse); 58 2: (BeginEnd: TBeginEnd); 59 3: (Assignment: TAssignment); 60 4: (Execution: TExecution); 61 end; 62 } end; 53 63 PCommand = ^TCommand; 54 64 55 65 TBeginEnd = record 56 Commands: array of PCommand;66 Commands: array of TCommand; 57 67 end; 58 68 PBeginEnd = ^TBeginEnd; … … 66 76 Source: TExpression; 67 77 end; 78 PAssignment = ^TAssignment; 68 79 69 80 TIfThenElse = record … … 73 84 end; 74 85 PIfThenElse = ^TIfThenElse; 86 87 TWhileDo = record 88 Condition: TExpression; 89 Command: TCommand; 90 end; 91 PWhileDo = ^TWhileDo; 75 92 76 93 { TFunction } … … 93 110 end; 94 111 112 TExecutionParams = record 113 Items: array of TExpression; 114 end; 115 116 TExecution = record 117 Func: PFunction; 118 Parameters: TExecutionParams; 119 end; 120 PExecution = ^TExecution; 121 95 122 TProgramCode = record 96 123 Name: string; -
branches/interpreter/project3.lpr
r96 r97 9 9 ProgramCode: TProgramCode; 10 10 begin 11 WriteLn('Parsing started'); 11 12 ParseProgram(@ProgramCode); 13 WriteLn('Parsing finished'); 14 15 WriteLn('Execution started'); 12 16 ExecuteProgram(@ProgramCode); 17 WriteLn('Execution finished'); 13 18 end. 14 19 -
branches/interpreter/project3.lps
r96 r97 8 8 <Filename Value="project3.lpr"/> 9 9 <IsPartOfProject Value="True"/> 10 <CursorPos X=" 30" Y="12"/>11 <UsageCount Value=" 46"/>10 <CursorPos X="21" Y="17"/> 11 <UsageCount Value="95"/> 12 12 <Loaded Value="True"/> 13 13 <DefaultSyntaxHighlighter Value="Delphi"/> … … 18 18 <TopLine Value="698"/> 19 19 <CursorPos X="3" Y="724"/> 20 <UsageCount Value=" 22"/>20 <UsageCount Value="47"/> 21 21 <Loaded Value="True"/> 22 22 </Unit1> … … 24 24 <Filename Value="Execute3.pas"/> 25 25 <IsPartOfProject Value="True"/> 26 <IsVisibleTab Value="True"/> 26 27 <EditorIndex Value="2"/> 27 <CursorPos X="40" Y="8"/> 28 <UsageCount Value="26"/> 28 <TopLine Value="18"/> 29 <CursorPos X="20" Y="44"/> 30 <UsageCount Value="75"/> 29 31 <Loaded Value="True"/> 30 32 <DefaultSyntaxHighlighter Value="Delphi"/> … … 34 36 <IsPartOfProject Value="True"/> 35 37 <EditorIndex Value="3"/> 36 <TopLine Value=" 40"/>37 <CursorPos X=" 30" Y="74"/>38 <UsageCount Value=" 26"/>38 <TopLine Value="25"/> 39 <CursorPos X="40" Y="51"/> 40 <UsageCount Value="75"/> 39 41 <Loaded Value="True"/> 40 42 <DefaultSyntaxHighlighter Value="Delphi"/> … … 43 45 <Filename Value="Parser3.pas"/> 44 46 <IsPartOfProject Value="True"/> 45 <IsVisibleTab Value="True"/>46 47 <EditorIndex Value="1"/> 47 <TopLine Value=" 153"/>48 <CursorPos Y="188"/>49 <UsageCount Value=" 26"/>48 <TopLine Value="419"/> 49 <CursorPos X="18" Y="471"/> 50 <UsageCount Value="75"/> 50 51 <Loaded Value="True"/> 51 52 <DefaultSyntaxHighlighter Value="Delphi"/> 52 53 </Unit4> 53 54 </Units> 54 <JumpHistory Count=" 30" HistoryIndex="27">55 <JumpHistory Count="29" HistoryIndex="28"> 55 56 <Position1> 56 57 <Filename Value="Parser3.pas"/> 57 <Caret Line="2 06" Column="21" TopLine="176"/>58 <Caret Line="243" TopLine="216"/> 58 59 </Position1> 59 60 <Position2> 60 61 <Filename Value="Parser3.pas"/> 61 <Caret Line=" 176" TopLine="148"/>62 <Caret Line="240" TopLine="216"/> 62 63 </Position2> 63 64 <Position3> 64 65 <Filename Value="Parser3.pas"/> 65 <Caret Line=" 194" Column="40" TopLine="169"/>66 <Caret Line="245" TopLine="216"/> 66 67 </Position3> 67 68 <Position4> 68 69 <Filename Value="Parser3.pas"/> 69 <Caret Line="2 54" Column="45" TopLine="234"/>70 <Caret Line="240" TopLine="216"/> 70 71 </Position4> 71 72 <Position5> 72 73 <Filename Value="Parser3.pas"/> 73 <Caret Line=" 25" Column="22"/>74 <Caret Line="49" TopLine="23"/> 74 75 </Position5> 75 76 <Position6> 76 77 <Filename Value="Parser3.pas"/> 77 <Caret Line=" 254" Column="46" TopLine="254"/>78 <Caret Line="308" Column="24" TopLine="285"/> 78 79 </Position6> 79 80 <Position7> 80 81 <Filename Value="Parser3.pas"/> 81 <Caret Line=" 25" Column="46" TopLine="25"/>82 <Caret Line="119" Column="10" TopLine="93"/> 82 83 </Position7> 83 84 <Position8> 84 85 <Filename Value="Parser3.pas"/> 85 <Caret Line=" 243" Column="27" TopLine="223"/>86 <Caret Line="94" Column="10" TopLine="71"/> 86 87 </Position8> 87 88 <Position9> 88 <Filename Value=" Parser3.pas"/>89 <Caret Line=" 251" Column="19" TopLine="225"/>89 <Filename Value="Execute3.pas"/> 90 <Caret Line="13" Column="19"/> 90 91 </Position9> 91 92 <Position10> 92 <Filename Value=" Parser3.pas"/>93 <Caret Line="20 4" TopLine="178"/>93 <Filename Value="Execute3.pas"/> 94 <Caret Line="20" Column="20"/> 94 95 </Position10> 95 96 <Position11> 96 <Filename Value=" Parser3.pas"/>97 <Caret Line=" 316" TopLine="290"/>97 <Filename Value="Execute3.pas"/> 98 <Caret Line="15" Column="3"/> 98 99 </Position11> 99 100 <Position12> 100 <Filename Value=" Parser3.pas"/>101 <Caret Line=" 318" TopLine="290"/>101 <Filename Value="Execute3.pas"/> 102 <Caret Line="19" Column="10"/> 102 103 </Position12> 103 104 <Position13> 104 <Filename Value=" Parser3.pas"/>105 <Caret Line=" 319" TopLine="290"/>105 <Filename Value="Source3.pas"/> 106 <Caret Line="49" Column="16" TopLine="37"/> 106 107 </Position13> 107 108 <Position14> 108 <Filename Value=" Parser3.pas"/>109 <Caret Line=" 320" TopLine="290"/>109 <Filename Value="Source3.pas"/> 110 <Caret Line="78" Column="80" TopLine="37"/> 110 111 </Position14> 111 112 <Position15> 112 <Filename Value=" Parser3.pas"/>113 <Caret Line=" 204" TopLine="178"/>113 <Filename Value="Source3.pas"/> 114 <Caret Line="49" Column="16" TopLine="37"/> 114 115 </Position15> 115 116 <Position16> 116 <Filename Value=" Parser3.pas"/>117 <Caret Line=" 205" TopLine="178"/>117 <Filename Value="Source3.pas"/> 118 <Caret Line="115" Column="34" TopLine="67"/> 118 119 </Position16> 119 120 <Position17> 120 <Filename Value=" Parser3.pas"/>121 <Caret Line=" 206" TopLine="178"/>121 <Filename Value="Source3.pas"/> 122 <Caret Line="8" Column="30"/> 122 123 </Position17> 123 124 <Position18> 124 <Filename Value=" Parser3.pas"/>125 <Caret Line=" 204" TopLine="178"/>125 <Filename Value="Source3.pas"/> 126 <Caret Line="9" Column="3"/> 126 127 </Position18> 127 128 <Position19> 128 <Filename Value=" Parser3.pas"/>129 <Caret Line=" 207" TopLine="178"/>129 <Filename Value="Source3.pas"/> 130 <Caret Line="113" Column="11" TopLine="70"/> 130 131 </Position19> 131 132 <Position20> 132 <Filename Value=" Parser3.pas"/>133 <Caret Line=" 316" TopLine="290"/>133 <Filename Value="Source3.pas"/> 134 <Caret Line="8"/> 134 135 </Position20> 135 136 <Position21> 136 <Filename Value=" Parser3.pas"/>137 <Caret Line=" 207" TopLine="181"/>137 <Filename Value="Source3.pas"/> 138 <Caret Line="52" Column="25" TopLine="26"/> 138 139 </Position21> 139 140 <Position22> 140 <Filename Value=" Parser3.pas"/>141 <Caret Line="1 71" TopLine="145"/>141 <Filename Value="Execute3.pas"/> 142 <Caret Line="16" Column="42"/> 142 143 </Position22> 143 144 <Position23> 144 <Filename Value=" Parser3.pas"/>145 <Caret Line="1 72" TopLine="145"/>145 <Filename Value="Execute3.pas"/> 146 <Caret Line="13" Column="57"/> 146 147 </Position23> 147 148 <Position24> 148 <Filename Value=" Parser3.pas"/>149 <Caret Line="1 73" TopLine="145"/>149 <Filename Value="Execute3.pas"/> 150 <Caret Line="18" Column="52"/> 150 151 </Position24> 151 152 <Position25> 152 <Filename Value=" Parser3.pas"/>153 <Caret Line=" 178" TopLine="145"/>153 <Filename Value="Execute3.pas"/> 154 <Caret Line="26" Column="37"/> 154 155 </Position25> 155 156 <Position26> 156 <Filename Value=" Parser3.pas"/>157 <Caret Line=" 187" Column="41" TopLine="145"/>157 <Filename Value="Execute3.pas"/> 158 <Caret Line="25" Column="3"/> 158 159 </Position26> 159 160 <Position27> 160 <Filename Value=" Parser3.pas"/>161 <Caret Line=" 209" TopLine="183"/>161 <Filename Value="Execute3.pas"/> 162 <Caret Line="30" Column="25"/> 162 163 </Position27> 163 164 <Position28> 164 <Filename Value=" Parser3.pas"/>165 <Caret Line=" 318" TopLine="292"/>165 <Filename Value="Execute3.pas"/> 166 <Caret Line="46" Column="52" TopLine="17"/> 166 167 </Position28> 167 168 <Position29> 168 <Filename Value=" Parser3.pas"/>169 <Caret Line=" 209" Column="10" TopLine="183"/>169 <Filename Value="Execute3.pas"/> 170 <Caret Line="43" Column="20" TopLine="17"/> 170 171 </Position29> 171 <Position30>172 <Filename Value="Parser3.pas"/>173 <Caret Line="325" Column="11" TopLine="292"/>174 </Position30>175 172 </JumpHistory> 176 173 </ProjectSession> … … 182 179 <WatchKind Value="wpkWrite"/> 183 180 <Source Value="Parser3.pas"/> 184 <Line Value=" 188"/>181 <Line Value="49"/> 185 182 </Item1> 186 183 </BreakPoints>
Note:
See TracChangeset
for help on using the changeset viewer.