- Timestamp:
- Feb 2, 2017, 7:49:02 AM (8 years ago)
- Location:
- branches/interpreter
- Files:
-
- 3 added
- 3 edited
Legend:
- Unmodified
- Added
- Removed
-
branches/interpreter/project3.lpi
r95 r96 32 32 </local> 33 33 </RunParams> 34 <Units Count=" 1">34 <Units Count="4"> 35 35 <Unit0> 36 36 <Filename Value="project3.lpr"/> 37 37 <IsPartOfProject Value="True"/> 38 38 </Unit0> 39 <Unit1> 40 <Filename Value="Execute3.pas"/> 41 <IsPartOfProject Value="True"/> 42 </Unit1> 43 <Unit2> 44 <Filename Value="Source3.pas"/> 45 <IsPartOfProject Value="True"/> 46 </Unit2> 47 <Unit3> 48 <Filename Value="Parser3.pas"/> 49 <IsPartOfProject Value="True"/> 50 </Unit3> 39 51 </Units> 40 52 </ProjectOptions> -
branches/interpreter/project3.lpr
r95 r96 1 1 program project3; 2 2 3 type 4 TTokenType = (ttNormal, ttSpecialSymbol, ttString, ttConstant); 5 var 6 InputText: string; 7 InputTextPos: Integer; 8 LastTokenType: TTokenType; 3 {$mode delphi} 9 4 10 function IsWhiteSpace(C: Char): Boolean; 11 begin 12 Result := (C = ' ') or (C = #13) or (C = #10) or (C = #9); 13 end; 14 15 function IsSpecialSymbol(C: Char): Boolean; 16 begin 17 Result := (C = ';') or (C = '(') or (C = ')') or (C = ':') or (C = '=') or 18 (C = '+') or (C = '-'); 19 end; 20 21 function IsSpecialSymbolLong(Text: string): Boolean; 22 begin 23 Result := (Text = ':=') or (Text = '<>') or (Text = '>=') or (Text = '<='); 24 end; 25 26 procedure ShowError(Text: string); 27 begin 28 WriteLn(Text); 29 Halt; 30 end; 31 32 procedure ReadInputAll; 33 var 34 C: Char; 35 begin 36 InputTextPos := 1; 37 InputText := ''; 38 while not Eof do begin 39 Read(C); 40 InputText := InputText + C; 41 end; 42 end; 43 44 function ReadChar: Char; 45 begin 46 if InputTextPos >= Length(InputText) then ShowError('Premature end of source'); 47 Result := InputText[InputTextPos]; 48 InputTextPos := InputTextPos + 1; 49 end; 50 51 function ReadNext: string; 52 var 53 C: Char; 54 IsString: Boolean; 55 begin 56 Result := ''; 57 IsString := False; 58 LastTokenType := ttNormal; 59 repeat 60 C := ReadChar; 61 if IsString then begin 62 if C = '''' then begin 63 Break; 64 end else Result := Result + C; 65 end else begin 66 if IsWhiteSpace(C) then begin 67 if Result = '' then Continue 68 else begin 69 Break; 70 end; 71 end else 72 if IsSpecialSymbol(C) then begin 73 if Result = '' then begin 74 LastTokenType := ttSpecialSymbol; 75 Result := Result + C; 76 C := ReadChar; 77 if IsSpecialSymbolLong(Result + C) then begin 78 Result := Result + C; 79 Break; 80 end else InputTextPos := InputTextPos - 1; 81 Break; 82 end else begin 83 InputTextPos := InputTextPos - 1; 84 Break; 85 end; 86 end else 87 if C = '''' then begin 88 LastTokenType := ttString; 89 IsString := True; 90 end else begin 91 Result := Result + C; 92 end; 93 end; 94 until False; 95 end; 96 97 function CheckNext(Text: string): Boolean; 98 var 99 Next: string; 100 OldPos: Integer; 101 begin 102 OldPos := InputTextPos; 103 Next := ReadNext; 104 Result := Next = Text; 105 InputTextPos := OldPos; 106 end; 107 108 procedure Expect(Text: string); 109 var 110 Next: string; 111 begin 112 Next := ReadNext; 113 if Next <> Text then 114 ShowError('Expected ' + Text + ' but found ' + Next); 115 end; 116 117 function IsVariable(Text: string): Boolean; 118 begin 119 Result := (Text = 'Result') or (Text = 'Text') or (Text = 'C'); 120 end; 121 122 function IsLogicOperator(Text: string): Boolean; 123 begin 124 Result := (Text = 'or') or (Text = 'and'); 125 end; 126 127 function IsOperator(Text: string): Boolean; 128 begin 129 Result := (Text = '=') or (Text = '<>') or (Text = '>') or (Text = '<') or 130 (Text = '<=') or (Text = '>='); 131 end; 132 133 function ParseExpression: Boolean; 134 var 135 Next: string; 136 OldPos: Integer; 137 R: Boolean; 138 begin 139 Result := True; 140 Next := ReadNext; 141 if Next = '(' then begin 142 R := ParseExpression; 143 Expect(')'); 144 end else 145 if IsVariable(Next) then begin 146 Next := ReadNext; 147 if IsOperator(Next) then begin 148 Next := ReadNext; 149 //if IsVariable(Next) then begin 150 151 //end else ShowError('Expected variable'); 152 end else ShowError('Exprected operator but found ' + ReadNext); 153 end else 154 ShowError('Expected variable but found ' + ReadNext); 155 156 OldPos := InputTextPos; 157 Next := ReadNext; 158 if IsLogicOperator(Next) then begin 159 R := ParseExpression; 160 end else InputTextPos := OldPos; 161 end; 162 163 function ParseAssignment: Boolean; 164 var 165 Next: string; 166 OldPos: Integer; 167 begin 168 Result := True; 169 OldPos := InputTextPos; 170 Next := ReadNext; 171 if IsVariable(Next) then begin 172 Expect(':='); 173 ParseExpression; 174 Expect(';'); 175 end else begin 176 InputTextPos := OldPos; 177 Result := False; 178 end; 179 end; 180 181 function ParseBeginEnd(Top: Boolean = False): Boolean; 182 begin 183 if CheckNext('begin') then begin 184 Result := True; 185 Expect('begin'); 186 repeat 187 if ParseAssignment then begin 188 end else 189 if CheckNext('end') then begin 190 Expect('end'); 191 Break; 192 end else ShowError('Expected command but found ' + ReadNext); 193 until False; 194 if Top then Expect('.') 195 else Expect(';'); 196 end else Result := False; 197 end; 198 199 function ParseFunction: Boolean; 200 var 201 Name: string; 202 ParamName: string; 203 ParamType: string; 204 ReturnType: string; 205 begin 206 if CheckNext('function') then begin 207 Result := True; 208 Expect('function'); 209 Name := ReadNext; 210 if CheckNext('(') then begin 211 Expect('('); 212 ParamName := ReadNext; 213 Expect(':'); 214 ParamType := ReadNext; 215 Expect(')'); 216 end; 217 Expect(':'); 218 ReturnType := ReadNext; 219 Expect(';'); 220 ParseBeginEnd; 221 end else Result := False; 222 end; 223 224 function ParseProcedure: Boolean; 225 var 226 Name: string; 227 ParamName: string; 228 ParamType: string; 229 begin 230 if CheckNext('procedure') then begin 231 Result := True; 232 Expect('procedure'); 233 Name := ReadNext; 234 if CheckNext('(') then begin 235 Expect('('); 236 ParamName := ReadNext; 237 Expect(':'); 238 ParamType := ReadNext; 239 Expect(')'); 240 end; 241 Expect(';'); 242 ParseBeginEnd; 243 end else Result := False; 244 end; 5 uses 6 Execute3, Source3, Parser3; 245 7 246 8 var 247 Program Name: string;9 ProgramCode: TProgramCode; 248 10 begin 249 WriteLn('Start'); 250 251 ReadInputAll; 252 if CheckNext('program') then begin 253 Expect('program'); 254 ProgramName := ReadNext; 255 Expect(';'); 256 end; 257 repeat 258 if not ParseFunction then 259 else if not ParseProcedure then 260 else Break; 261 until False; 262 263 WriteLn('Finished'); 11 ParseProgram(@ProgramCode); 12 ExecuteProgram(@ProgramCode); 264 13 end. 265 14 -
branches/interpreter/project3.lps
r95 r96 4 4 <Version Value="9"/> 5 5 <BuildModes Active="Default"/> 6 <Units Count=" 2">6 <Units Count="5"> 7 7 <Unit0> 8 8 <Filename Value="project3.lpr"/> 9 9 <IsPartOfProject Value="True"/> 10 <IsVisibleTab Value="True"/> 11 <TopLine Value="161"/> 12 <CursorPos X="10" Y="188"/> 13 <UsageCount Value="39"/> 10 <CursorPos X="30" Y="12"/> 11 <UsageCount Value="46"/> 14 12 <Loaded Value="True"/> 15 13 <DefaultSyntaxHighlighter Value="Delphi"/> … … 17 15 <Unit1> 18 16 <Filename Value="/usr/share/fpcsrc/3.0.0/rtl/inc/systemh.inc"/> 19 <EditorIndex Value=" 1"/>17 <EditorIndex Value="4"/> 20 18 <TopLine Value="698"/> 21 19 <CursorPos X="3" Y="724"/> 22 <UsageCount Value=" 19"/>20 <UsageCount Value="22"/> 23 21 <Loaded Value="True"/> 24 22 </Unit1> 23 <Unit2> 24 <Filename Value="Execute3.pas"/> 25 <IsPartOfProject Value="True"/> 26 <EditorIndex Value="2"/> 27 <CursorPos X="40" Y="8"/> 28 <UsageCount Value="26"/> 29 <Loaded Value="True"/> 30 <DefaultSyntaxHighlighter Value="Delphi"/> 31 </Unit2> 32 <Unit3> 33 <Filename Value="Source3.pas"/> 34 <IsPartOfProject Value="True"/> 35 <EditorIndex Value="3"/> 36 <TopLine Value="40"/> 37 <CursorPos X="30" Y="74"/> 38 <UsageCount Value="26"/> 39 <Loaded Value="True"/> 40 <DefaultSyntaxHighlighter Value="Delphi"/> 41 </Unit3> 42 <Unit4> 43 <Filename Value="Parser3.pas"/> 44 <IsPartOfProject Value="True"/> 45 <IsVisibleTab Value="True"/> 46 <EditorIndex Value="1"/> 47 <TopLine Value="153"/> 48 <CursorPos Y="188"/> 49 <UsageCount Value="26"/> 50 <Loaded Value="True"/> 51 <DefaultSyntaxHighlighter Value="Delphi"/> 52 </Unit4> 25 53 </Units> 26 <JumpHistory Count="30" HistoryIndex="2 9">54 <JumpHistory Count="30" HistoryIndex="27"> 27 55 <Position1> 28 <Filename Value=" project3.lpr"/>29 <Caret Line=" 188" Column="11" TopLine="155"/>56 <Filename Value="Parser3.pas"/> 57 <Caret Line="206" Column="21" TopLine="176"/> 30 58 </Position1> 31 59 <Position2> 32 <Filename Value=" project3.lpr"/>33 <Caret Line="1 87" TopLine="155"/>60 <Filename Value="Parser3.pas"/> 61 <Caret Line="176" TopLine="148"/> 34 62 </Position2> 35 63 <Position3> 36 <Filename Value=" project3.lpr"/>37 <Caret Line="1 88" TopLine="155"/>64 <Filename Value="Parser3.pas"/> 65 <Caret Line="194" Column="40" TopLine="169"/> 38 66 </Position3> 39 67 <Position4> 40 <Filename Value=" project3.lpr"/>41 <Caret Line=" 189" TopLine="155"/>68 <Filename Value="Parser3.pas"/> 69 <Caret Line="254" Column="45" TopLine="234"/> 42 70 </Position4> 43 71 <Position5> 44 <Filename Value=" project3.lpr"/>45 <Caret Line=" 190" TopLine="155"/>72 <Filename Value="Parser3.pas"/> 73 <Caret Line="25" Column="22"/> 46 74 </Position5> 47 75 <Position6> 48 <Filename Value=" project3.lpr"/>49 <Caret Line=" 187" TopLine="155"/>76 <Filename Value="Parser3.pas"/> 77 <Caret Line="254" Column="46" TopLine="254"/> 50 78 </Position6> 51 79 <Position7> 52 <Filename Value=" project3.lpr"/>53 <Caret Line=" 167" TopLine="155"/>80 <Filename Value="Parser3.pas"/> 81 <Caret Line="25" Column="46" TopLine="25"/> 54 82 </Position7> 55 83 <Position8> 56 <Filename Value=" project3.lpr"/>57 <Caret Line=" 168" TopLine="155"/>84 <Filename Value="Parser3.pas"/> 85 <Caret Line="243" Column="27" TopLine="223"/> 58 86 </Position8> 59 87 <Position9> 60 <Filename Value=" project3.lpr"/>61 <Caret Line=" 169" TopLine="155"/>88 <Filename Value="Parser3.pas"/> 89 <Caret Line="251" Column="19" TopLine="225"/> 62 90 </Position9> 63 91 <Position10> 64 <Filename Value=" project3.lpr"/>65 <Caret Line=" 170" TopLine="155"/>92 <Filename Value="Parser3.pas"/> 93 <Caret Line="204" TopLine="178"/> 66 94 </Position10> 67 95 <Position11> 68 <Filename Value=" project3.lpr"/>69 <Caret Line=" 171" TopLine="155"/>96 <Filename Value="Parser3.pas"/> 97 <Caret Line="316" TopLine="290"/> 70 98 </Position11> 71 99 <Position12> 72 <Filename Value=" project3.lpr"/>73 <Caret Line=" 172" TopLine="155"/>100 <Filename Value="Parser3.pas"/> 101 <Caret Line="318" TopLine="290"/> 74 102 </Position12> 75 103 <Position13> 76 <Filename Value=" project3.lpr"/>77 <Caret Line=" 173" TopLine="155"/>104 <Filename Value="Parser3.pas"/> 105 <Caret Line="319" TopLine="290"/> 78 106 </Position13> 79 107 <Position14> 80 <Filename Value=" project3.lpr"/>81 <Caret Line=" 174" TopLine="155"/>108 <Filename Value="Parser3.pas"/> 109 <Caret Line="320" TopLine="290"/> 82 110 </Position14> 83 111 <Position15> 84 <Filename Value=" project3.lpr"/>85 <Caret Line=" 179" TopLine="155"/>112 <Filename Value="Parser3.pas"/> 113 <Caret Line="204" TopLine="178"/> 86 114 </Position15> 87 115 <Position16> 88 <Filename Value=" project3.lpr"/>89 <Caret Line=" 187" TopLine="155"/>116 <Filename Value="Parser3.pas"/> 117 <Caret Line="205" TopLine="178"/> 90 118 </Position16> 91 119 <Position17> 92 <Filename Value=" project3.lpr"/>93 <Caret Line=" 188" Column="24" TopLine="155"/>120 <Filename Value="Parser3.pas"/> 121 <Caret Line="206" TopLine="178"/> 94 122 </Position17> 95 123 <Position18> 96 <Filename Value=" project3.lpr"/>97 <Caret Line=" 187" TopLine="155"/>124 <Filename Value="Parser3.pas"/> 125 <Caret Line="204" TopLine="178"/> 98 126 </Position18> 99 127 <Position19> 100 <Filename Value=" project3.lpr"/>101 <Caret Line=" 188" TopLine="155"/>128 <Filename Value="Parser3.pas"/> 129 <Caret Line="207" TopLine="178"/> 102 130 </Position19> 103 131 <Position20> 104 <Filename Value=" project3.lpr"/>105 <Caret Line=" 187" TopLine="155"/>132 <Filename Value="Parser3.pas"/> 133 <Caret Line="316" TopLine="290"/> 106 134 </Position20> 107 135 <Position21> 108 <Filename Value=" project3.lpr"/>109 <Caret Line=" 190" TopLine="155"/>136 <Filename Value="Parser3.pas"/> 137 <Caret Line="207" TopLine="181"/> 110 138 </Position21> 111 139 <Position22> 112 <Filename Value=" project3.lpr"/>113 <Caret Line="1 87" TopLine="155"/>140 <Filename Value="Parser3.pas"/> 141 <Caret Line="171" TopLine="145"/> 114 142 </Position22> 115 143 <Position23> 116 <Filename Value=" project3.lpr"/>117 <Caret Line="1 89" TopLine="155"/>144 <Filename Value="Parser3.pas"/> 145 <Caret Line="172" TopLine="145"/> 118 146 </Position23> 119 147 <Position24> 120 <Filename Value=" project3.lpr"/>121 <Caret Line="1 90" TopLine="155"/>148 <Filename Value="Parser3.pas"/> 149 <Caret Line="173" TopLine="145"/> 122 150 </Position24> 123 151 <Position25> 124 <Filename Value=" project3.lpr"/>125 <Caret Line="1 91" TopLine="155"/>152 <Filename Value="Parser3.pas"/> 153 <Caret Line="178" TopLine="145"/> 126 154 </Position25> 127 155 <Position26> 128 <Filename Value=" project3.lpr"/>129 <Caret Line="1 94" TopLine="155"/>156 <Filename Value="Parser3.pas"/> 157 <Caret Line="187" Column="41" TopLine="145"/> 130 158 </Position26> 131 159 <Position27> 132 <Filename Value=" project3.lpr"/>133 <Caret Line=" 195" TopLine="155"/>160 <Filename Value="Parser3.pas"/> 161 <Caret Line="209" TopLine="183"/> 134 162 </Position27> 135 163 <Position28> 136 <Filename Value=" project3.lpr"/>137 <Caret Line=" 197" TopLine="155"/>164 <Filename Value="Parser3.pas"/> 165 <Caret Line="318" TopLine="292"/> 138 166 </Position28> 139 167 <Position29> 140 <Filename Value=" project3.lpr"/>141 <Caret Line="2 22" TopLine="196"/>168 <Filename Value="Parser3.pas"/> 169 <Caret Line="209" Column="10" TopLine="183"/> 142 170 </Position29> 143 171 <Position30> 144 <Filename Value=" project3.lpr"/>145 <Caret Line=" 259" TopLine="214"/>172 <Filename Value="Parser3.pas"/> 173 <Caret Line="325" Column="11" TopLine="292"/> 146 174 </Position30> 147 175 </JumpHistory> 148 176 </ProjectSession> 177 <Debugging> 178 <BreakPoints Count="1"> 179 <Item1> 180 <Kind Value="bpkSource"/> 181 <WatchScope Value="wpsLocal"/> 182 <WatchKind Value="wpkWrite"/> 183 <Source Value="Parser3.pas"/> 184 <Line Value="188"/> 185 </Item1> 186 </BreakPoints> 187 </Debugging> 149 188 </CONFIG>
Note:
See TracChangeset
for help on using the changeset viewer.