Ignore:
Timestamp:
Feb 11, 2017, 4:35:08 PM (8 years ago)
Author:
chronos
Message:
  • Modified: Improved interpeter. Better handling of execution of functions and passing value of parameters.
File:
1 edited

Legend:

Unmodified
Added
Removed
  • branches/interpreter/Parser3.pas

    r100 r101  
    9090    N := N * 10;
    9191    I := I - 1;
     92  end;
     93end;
     94
     95function IntToStr(Value: Integer): string;
     96begin
     97  Result := '';
     98  while Value > 0 do begin
     99    Result := Chr(Ord('0') + Value mod 10) + Result;
     100    Value := Value div 10;
    92101  end;
    93102end;
     
    292301    if CheckNext('(') then begin
    293302      Expect('(');
     303      SetLength(SubExpression.Items, 0);
    294304      if ParseExpression(@SubExpression) then begin
    295305        SetLength(Expression^.Items, Length(Expression^.Items) + 1);
     
    316326    // Build expression tree using operator precedence
    317327    for II := 0 to Length(OperatorPrecedence) - 1 do begin
    318       I := 1;
     328      I := 0;
    319329      while (I < Length(Expression^.Items) - 1) do begin
    320330        if (TExpression(Expression^.Items[I]).NodeType = ntOperator) and
     
    338348      end;
    339349    end;
     350
     351   if Length(Expression^.Items) = 1 then begin
     352      Expression^.NodeType := Expression^.Items[0].NodeType;
     353      Expression^.OperatorType := Expression^.Items[0].OperatorType;
     354      Expression^.Value := Expression^.Items[0].Value;
     355      // Move subtitem one node up
     356      SetLength(Expression^.Items, Length(Expression^.Items[0].Items));
     357      I := Length(Expression^.Items) - 1;
     358      while I >= 0 do begin
     359        Expression^.Items[I] := Expression^.Items[0].Items[I];
     360        I := I - 1;
     361      end;
     362    end else ShowError('Expression error ' + IntToStr(Length(Expression^.Items)));
    340363  end;
    341364end;
     
    362385  Value: TConstant;
    363386begin
     387  FillChar(Expression, SizeOf(TExpression), 0);
     388  FillChar(FunctionCall, SizeOf(TFunctionCall), 0);
     389  FillChar(Value, SizeOf(TConstant), 0);
     390
    364391  Result := True;
    365392  if not NoExpression and ParseExpression(@Expression) then begin
     
    443470  Assignment: TAssignment;
    444471begin
     472  FillChar(IfThenElse, SizeOf(TIfThenElse), 0);
     473  FillChar(WhileDo, SizeOf(TWhileDo), 0);
     474  FillChar(BeginEnd, SizeOf(TBeginEnd), 0);
     475  FillChar(Execution, SizeOf(TExecution), 0);
     476  FillChar(Assignment, SizeOf(TAssignment), 0);
     477
    445478  Result := True;
    446479  if ParseBeginEnd(@BeginEnd) then begin
     
    632665begin
    633666  SetLength(ProgramCode^.Types.Items, 0);
    634   ProgramCode^.Types.Add(TypeCreate('string', btInteger));
     667  ProgramCode^.Types.Add(TypeCreate('string', btShortString));
    635668  TypeString := ProgramCode^.Types.GetLast;
    636669  ProgramCode^.Types.Add(TypeCreate('Boolean', btBoolean));
     
    650683  FuncWriteLn := ProgramCode^.Functions.GetLast;
    651684  FuncWriteLn^.Parameters.Add(FunctionParameterCreate('Text', TypeString));
     685  FuncWriteLn^.Variables.Add(VariableCreate('Text', TypeString));
    652686  ProgramCode^.Functions.Add(FunctionCreate('Read', nil));
    653687  FuncRead := ProgramCode^.Functions.GetLast;
     
    657691  FuncLength := ProgramCode^.Functions.GetLast;
    658692  FuncLength^.Parameters.Add(FunctionParameterCreate('Array', TypeArray));
     693  FuncLength^.Variables.Add(VariableCreate('Array', TypeArray));
    659694  ProgramCode^.Functions.Add(FunctionCreate('SetLength', nil));
    660695  FuncSetLength := ProgramCode^.Functions.GetLast;
    661696  FuncSetLength^.Parameters.Add(FunctionParameterCreate('Array', TypeArray));
     697  FuncSetLength^.Variables.Add(VariableCreate('Array', TypeArray));
    662698  FuncSetLength^.Parameters.Add(FunctionParameterCreate('Count', TypeInteger));
     699  FuncSetLength^.Variables.Add(VariableCreate('Count', TypeInteger));
    663700end;
    664701
Note: See TracChangeset for help on using the changeset viewer.