Changeset 97 for branches


Ignore:
Timestamp:
Feb 2, 2017, 11:35:09 PM (8 years ago)
Author:
chronos
Message:
  • Modified: Improved parsin and prepared analyzed source execution.
Location:
branches/interpreter
Files:
5 edited

Legend:

Unmodified
Added
Removed
  • branches/interpreter/Execute3.pas

    r96 r97  
    1111implementation
    1212
     13procedure ExecuteCommand(Command: PCommand); forward;
     14
     15procedure ExecuteBeginEnd(BeginEnd: PBeginEnd);
     16var
     17  I: Integer;
     18begin
     19  for I := 0 to Length(BeginEnd^.Commands) - 1 do
     20    ExecuteCommand(@BeginEnd^.Commands[I]);
     21end;
     22
     23function ExecuteExpression(Expression: PExpression): Boolean;
     24begin
     25
     26end;
     27
     28procedure ExecuteWhileDo(WhileDo: PWhileDo);
     29begin
     30  if ExecuteExpression(@WhileDo^.Condition) then
     31    ExecuteCommand(@WhileDo^.Command);
     32end;
     33
     34procedure ExecuteIfThenElse(IfThenElse: PIfThenElse);
     35begin
     36  if ExecuteExpression(@IfThenElse^.Condition) then
     37    ExecuteCommand(@IfThenElse^.DoThen)
     38    else ExecuteCommand(@IfThenElse^.DoElse);
     39end;
     40
     41procedure ExecuteExecution(Execution: PExecution);
     42begin
     43  // Set parameters
     44  ExecuteBeginEnd(@Execution^.Func^.BeginEnd);
     45end;
     46
     47procedure ExecuteAssignment(Assignment: PAssignment);
     48begin
     49
     50end;
     51
     52procedure ExecuteCommand(Command: PCommand);
     53begin
     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;
     61end;
     62
    1363procedure ExecuteProgram(ProgramCode: PProgramCode);
    1464begin
    15 
     65  ExecuteBeginEnd(@ProgramCode^.BeginEnd);
    1666end;
    1767
  • branches/interpreter/Parser3.pas

    r96 r97  
    2424
    2525function ParseIfThen(IfThenElse: PIfThenElse): Boolean; forward;
    26 function ParseExecution: Boolean; forward;
     26function ParseWhileDo(WhileDo: PWhileDo): Boolean; forward;
     27function ParseExecution(Execution: PExecution): Boolean; forward;
     28function ParseBeginEnd(BeginEnd: PBeginEnd): Boolean; forward;
    2729
    2830
     
    3537begin
    3638  Result := (C = ';') or (C = '(') or (C = ')') or (C = ':') or (C = '=') or
    37     (C = '+') or (C = '-');
     39    (C = '+') or (C = '-') or (C = ';') or (C = '.');
    3840end;
    3941
     
    142144function IsOperator(Text: string): Boolean;
    143145begin
    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
    145148    (Text = '<=') or (Text = '>=');
    146149end;
     
    169172  Variable: PVariable;
    170173  SubExpression: TExpression;
     174  Execution: TExecution;
    171175begin
    172176  Result := True;
     177  if CheckNext('not') then begin
     178    Expect('not');
     179  end;
    173180  if CheckNext('(') then begin
    174181    Expect('(');
     
    177184  end else
    178185  if ParseVariable(Variable) then begin
     186    OldPos := InputTextPos;
    179187    Next := ReadNext;
    180188    if IsOperator(Next) then begin
     
    183191
    184192      //end else ShowError('Expected variable');
    185     end else ShowError('Exprected operator but found ' + ReadNext);
     193    end else InputTextPos := OldPos;
    186194  end else
    187195  if LastTokenType = ttString then begin
    188 
    189   end else
    190   if ParseExecution then begin
     196    ReadNext
     197  end else
     198  if ParseExecution(@Execution) then begin
    191199  end else
    192200  ShowError('Expected variable but found ' + ReadNext);
     
    194202  OldPos := InputTextPos;
    195203  Next := ReadNext;
    196   if IsLogicOperator(Next) then begin
     204  if IsLogicOperator(Next) or IsOperator(Next) then begin
    197205    R := ParseExpression(@SubExpression);
    198206  end else InputTextPos := OldPos;
     
    214222end;
    215223
    216 function ParseExecution: Boolean;
     224function ParseExecution(Execution: PExecution): Boolean;
    217225var
    218226  OldPos: Integer;
     
    220228  Func: PFunction;
    221229  I: Integer;
     230  Expression: TExpression;
    222231begin
    223232  Result := True;
     
    230239      I := 0;
    231240      while I < Length(Func^.Parameters.Items) do begin
    232         Next := ReadNext;
     241        ParseExpression(@Expression);
    233242        if I < (Length(Func^.Parameters.Items) - 1) then Expect(',');
    234243        I := I + 1;
     
    245254var
    246255  IfThenElse: TIfThenElse;
     256  WhileDo: TWhileDo;
     257  BeginEnd: TBeginEnd;
     258  Execution: TExecution;
    247259begin
    248260  Result := True;
     261  if ParseBeginEnd(@BeginEnd) then begin
     262  end else
    249263  if ParseAssignment then begin
    250264  end else
    251   if ParseExecution then begin
    252 
     265  if ParseExecution(@Execution) then begin
    253266  end else
    254267  if ParseIfThen(@IfThenElse) then begin
    255 
     268  end else
     269  if ParseWhileDo(@WhileDo) then begin
    256270  end else Result := False;
    257271end;
     
    269283      ParseCommand;
    270284    end;
     285  end else Result := False;
     286end;
     287
     288function ParseWhileDo(WhileDo: PWhileDo): Boolean;
     289begin
     290  if CheckNext('while') then begin
     291    Result := True;
     292    Expect('while');
     293    ParseExpression(@WhileDo.Condition);
     294    Expect('do');
     295    ParseCommand;
    271296  end else Result := False;
    272297end;
     
    364389    ParseParams(@Func^.Parameters);
    365390    Expect(';');
     391    if ParseVariableSection then begin
     392    end;
    366393    ParseBeginEnd(@Func^.BeginEnd);
    367394    Expect(';');
     
    396423  ProgramCode^.Variables.Add(VariableCreate('Result', TypeString));
    397424  ProgramCode^.Variables.Add(VariableCreate('C', TypeChar));
     425  ProgramCode^.Variables.Add(VariableCreate('Text', TypeString));
    398426
    399427  SetLength(ProgramCode^.Functions.Items, 0);
  • branches/interpreter/Source3.pas

    r96 r97  
    4949  TFunctionCall = procedure ;
    5050
     51  TCmdType = (ctWhileDo, ctIfThenElse, ctBeginEnd, ctAssignment, ctExecution);
    5152  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;
    5363  PCommand = ^TCommand;
    5464
    5565  TBeginEnd = record
    56     Commands: array of PCommand;
     66    Commands: array of TCommand;
    5767  end;
    5868  PBeginEnd = ^TBeginEnd;
     
    6676    Source: TExpression;
    6777  end;
     78  PAssignment = ^TAssignment;
    6879
    6980  TIfThenElse = record
     
    7384  end;
    7485  PIfThenElse = ^TIfThenElse;
     86
     87  TWhileDo = record
     88    Condition: TExpression;
     89    Command: TCommand;
     90  end;
     91  PWhileDo = ^TWhileDo;
    7592
    7693  { TFunction }
     
    93110  end;
    94111
     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
    95122  TProgramCode = record
    96123    Name: string;
  • branches/interpreter/project3.lpr

    r96 r97  
    99  ProgramCode: TProgramCode;
    1010begin
     11  WriteLn('Parsing started');
    1112  ParseProgram(@ProgramCode);
     13  WriteLn('Parsing finished');
     14
     15  WriteLn('Execution started');
    1216  ExecuteProgram(@ProgramCode);
     17  WriteLn('Execution finished');
    1318end.
    1419
  • branches/interpreter/project3.lps

    r96 r97  
    88        <Filename Value="project3.lpr"/>
    99        <IsPartOfProject Value="True"/>
    10         <CursorPos X="30" Y="12"/>
    11         <UsageCount Value="46"/>
     10        <CursorPos X="21" Y="17"/>
     11        <UsageCount Value="95"/>
    1212        <Loaded Value="True"/>
    1313        <DefaultSyntaxHighlighter Value="Delphi"/>
     
    1818        <TopLine Value="698"/>
    1919        <CursorPos X="3" Y="724"/>
    20         <UsageCount Value="22"/>
     20        <UsageCount Value="47"/>
    2121        <Loaded Value="True"/>
    2222      </Unit1>
     
    2424        <Filename Value="Execute3.pas"/>
    2525        <IsPartOfProject Value="True"/>
     26        <IsVisibleTab Value="True"/>
    2627        <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"/>
    2931        <Loaded Value="True"/>
    3032        <DefaultSyntaxHighlighter Value="Delphi"/>
     
    3436        <IsPartOfProject Value="True"/>
    3537        <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"/>
    3941        <Loaded Value="True"/>
    4042        <DefaultSyntaxHighlighter Value="Delphi"/>
     
    4345        <Filename Value="Parser3.pas"/>
    4446        <IsPartOfProject Value="True"/>
    45         <IsVisibleTab Value="True"/>
    4647        <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"/>
    5051        <Loaded Value="True"/>
    5152        <DefaultSyntaxHighlighter Value="Delphi"/>
    5253      </Unit4>
    5354    </Units>
    54     <JumpHistory Count="30" HistoryIndex="27">
     55    <JumpHistory Count="29" HistoryIndex="28">
    5556      <Position1>
    5657        <Filename Value="Parser3.pas"/>
    57         <Caret Line="206" Column="21" TopLine="176"/>
     58        <Caret Line="243" TopLine="216"/>
    5859      </Position1>
    5960      <Position2>
    6061        <Filename Value="Parser3.pas"/>
    61         <Caret Line="176" TopLine="148"/>
     62        <Caret Line="240" TopLine="216"/>
    6263      </Position2>
    6364      <Position3>
    6465        <Filename Value="Parser3.pas"/>
    65         <Caret Line="194" Column="40" TopLine="169"/>
     66        <Caret Line="245" TopLine="216"/>
    6667      </Position3>
    6768      <Position4>
    6869        <Filename Value="Parser3.pas"/>
    69         <Caret Line="254" Column="45" TopLine="234"/>
     70        <Caret Line="240" TopLine="216"/>
    7071      </Position4>
    7172      <Position5>
    7273        <Filename Value="Parser3.pas"/>
    73         <Caret Line="25" Column="22"/>
     74        <Caret Line="49" TopLine="23"/>
    7475      </Position5>
    7576      <Position6>
    7677        <Filename Value="Parser3.pas"/>
    77         <Caret Line="254" Column="46" TopLine="254"/>
     78        <Caret Line="308" Column="24" TopLine="285"/>
    7879      </Position6>
    7980      <Position7>
    8081        <Filename Value="Parser3.pas"/>
    81         <Caret Line="25" Column="46" TopLine="25"/>
     82        <Caret Line="119" Column="10" TopLine="93"/>
    8283      </Position7>
    8384      <Position8>
    8485        <Filename Value="Parser3.pas"/>
    85         <Caret Line="243" Column="27" TopLine="223"/>
     86        <Caret Line="94" Column="10" TopLine="71"/>
    8687      </Position8>
    8788      <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"/>
    9091      </Position9>
    9192      <Position10>
    92         <Filename Value="Parser3.pas"/>
    93         <Caret Line="204" TopLine="178"/>
     93        <Filename Value="Execute3.pas"/>
     94        <Caret Line="20" Column="20"/>
    9495      </Position10>
    9596      <Position11>
    96         <Filename Value="Parser3.pas"/>
    97         <Caret Line="316" TopLine="290"/>
     97        <Filename Value="Execute3.pas"/>
     98        <Caret Line="15" Column="3"/>
    9899      </Position11>
    99100      <Position12>
    100         <Filename Value="Parser3.pas"/>
    101         <Caret Line="318" TopLine="290"/>
     101        <Filename Value="Execute3.pas"/>
     102        <Caret Line="19" Column="10"/>
    102103      </Position12>
    103104      <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"/>
    106107      </Position13>
    107108      <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"/>
    110111      </Position14>
    111112      <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"/>
    114115      </Position15>
    115116      <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"/>
    118119      </Position16>
    119120      <Position17>
    120         <Filename Value="Parser3.pas"/>
    121         <Caret Line="206" TopLine="178"/>
     121        <Filename Value="Source3.pas"/>
     122        <Caret Line="8" Column="30"/>
    122123      </Position17>
    123124      <Position18>
    124         <Filename Value="Parser3.pas"/>
    125         <Caret Line="204" TopLine="178"/>
     125        <Filename Value="Source3.pas"/>
     126        <Caret Line="9" Column="3"/>
    126127      </Position18>
    127128      <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"/>
    130131      </Position19>
    131132      <Position20>
    132         <Filename Value="Parser3.pas"/>
    133         <Caret Line="316" TopLine="290"/>
     133        <Filename Value="Source3.pas"/>
     134        <Caret Line="8"/>
    134135      </Position20>
    135136      <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"/>
    138139      </Position21>
    139140      <Position22>
    140         <Filename Value="Parser3.pas"/>
    141         <Caret Line="171" TopLine="145"/>
     141        <Filename Value="Execute3.pas"/>
     142        <Caret Line="16" Column="42"/>
    142143      </Position22>
    143144      <Position23>
    144         <Filename Value="Parser3.pas"/>
    145         <Caret Line="172" TopLine="145"/>
     145        <Filename Value="Execute3.pas"/>
     146        <Caret Line="13" Column="57"/>
    146147      </Position23>
    147148      <Position24>
    148         <Filename Value="Parser3.pas"/>
    149         <Caret Line="173" TopLine="145"/>
     149        <Filename Value="Execute3.pas"/>
     150        <Caret Line="18" Column="52"/>
    150151      </Position24>
    151152      <Position25>
    152         <Filename Value="Parser3.pas"/>
    153         <Caret Line="178" TopLine="145"/>
     153        <Filename Value="Execute3.pas"/>
     154        <Caret Line="26" Column="37"/>
    154155      </Position25>
    155156      <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"/>
    158159      </Position26>
    159160      <Position27>
    160         <Filename Value="Parser3.pas"/>
    161         <Caret Line="209" TopLine="183"/>
     161        <Filename Value="Execute3.pas"/>
     162        <Caret Line="30" Column="25"/>
    162163      </Position27>
    163164      <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"/>
    166167      </Position28>
    167168      <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"/>
    170171      </Position29>
    171       <Position30>
    172         <Filename Value="Parser3.pas"/>
    173         <Caret Line="325" Column="11" TopLine="292"/>
    174       </Position30>
    175172    </JumpHistory>
    176173  </ProjectSession>
     
    182179        <WatchKind Value="wpkWrite"/>
    183180        <Source Value="Parser3.pas"/>
    184         <Line Value="188"/>
     181        <Line Value="49"/>
    185182      </Item1>
    186183    </BreakPoints>
Note: See TracChangeset for help on using the changeset viewer.