pax_basic.pas

来自「Delphi脚本控件」· PAS 代码 · 共 2,916 行 · 第 1/5 页

PAS
2,916 行
字号
  L := NewLabel;
  Gen(OP_GO, L, 0, 0);

  DeclareSwitch := true;
  Call_SCANNER;
  DeclareSwitch := false;

  ClassID := Parse_Ident;
  result := ClassID;

  Kind[ClassID] := KindTYPE;

  AncestorClassID := 0;

  LevelStack.PushClass(ClassID, AncestorClassID, [modStatic], ckEnum, false);

  Match(':');
  SkipColons;

  repeat
    ml := Parse_ModifierList;

    ID := Parse_Ident;
    MemberRec := CurrClassRec.AddField(ID, [modSTATIC]);
    TypeID[ID] := ClassID;

    if IsCurrText('=') then
    begin
      Call_SCANNER;

      Gen(OP_NOP, 0, 0, 0);
      MemberRec.InitN := LastCodeLine;

      if ValueID = 0 then
        ValueID := NewVar;

      Gen(OP_ASSIGN, ValueID, Parse_Expression, ValueID);

      Gen(OP_ASSIGN, ID, ValueID, ID);
      Gen(OP_PLUS, ValueID, NewConst(1), ValueID);
      Gen(OP_HALT, 0, 0, 0);

      if IsCurrText(',') then
      begin
        Call_SCANNER;
        continue;
      end;
    end
    else if IsCurrText(',') then
    begin
      Call_SCANNER;

      Gen(OP_NOP, 0, 0, 0);
      MemberRec.InitN := LastCodeLine;

      if ValueID = 0 then
      begin
        ValueID := NewVar;
        Gen(OP_ASSIGN, ValueID, NewConst(0), ValueID);
      end;

      Gen(OP_ASSIGN, ID, ValueID, ID);
      Gen(OP_PLUS, ValueID, NewConst(1), ValueID);
      Gen(OP_HALT, 0, 0, 0);

      if not IsCurrText(':') then
        continue;
    end
    else if IsCurrText(':') then
    begin
      Gen(OP_NOP, 0, 0, 0);
      MemberRec.InitN := LastCodeLine;

      if ValueID = 0 then
      begin
        ValueID := NewVar;
        Gen(OP_ASSIGN, ValueID, NewConst(0), ValueID);
      end;

      Gen(OP_ASSIGN, ID, ValueID, ID);
      Gen(OP_PLUS, ValueID, NewConst(1), ValueID);
      Gen(OP_HALT, 0, 0, 0);
    end
    else
      Break;

    Match(':');
    SkipColons;

    if CurrToken.ID = SP_EOF then
      Break;

    if IsCurrText('end') then
      Break;
  until false;

  Match('end');
  Call_SCANNER;
  Match('enum');

  LevelStack.Pop;
  SetLabelHere(L);

  Call_SCANNER;
end;

procedure TPAXBasicParser.Parse_IfStmt;
label
  Again;
var
  L, LF, ExprID: Integer;
begin
  // match "if"

  L := NewLabel;

Again:

  IsExecutable := true;

  Call_SCANNER;
  LF := NewLabel;
  ExprID := Parse_Expression;
  GenDestroyArrayArgumentList;
  Gen(OP_GO_FALSE_EX, LF, ExprID, 0);

  Match('then');

  Call_SCANNER;
  repeat
    Parse_Statement;
  until IsCurrText('else') or IsCurrText('elseif') or IsCurrText('end');

  Gen(OP_GO, L, 0, 0);

  while IsCurrText('elseif') do
  begin
    SetLabelHere(LF);

    MoveUpSourceLine;

    goto Again;
  end;

  if IsCurrText('else') then
  begin
    SetLabelHere(LF);

    MoveUpSourceLine;

    Call_SCANNER;
    repeat
      Parse_Statement;
    until IsCurrText('end');
  end
  else
    SetLabelHere(LF);

  SetLabelHere(L);

  Call_SCANNER;
  Match('if');

  Call_SCANNER;
end;

procedure TPAXBasicParser.Parse_ThrowStmt;
var
  ID: Integer;
begin
  // match "throw"

  Call_SCANNER;

  if IsCurrText(':') then
  begin
    ID := 0;
    SkipColons;
    Call_SCANNER;
  end
  else
    ID := Parse_Expression;

  Gen(OP_THROW, ID, 0, 0);
end;

procedure TPAXBasicParser.Parse_TryStmt;
var
  L, LTRY, ID: Integer;
begin
  // match "try"

  LTRY := NewLabel;
  Gen(OP_TRY_ON, LTRY, 0, 0);

  Call_SCANNER;
  repeat
    Parse_Statement;
  until IsCurrText('catch') or IsCurrText('finally');

  L := NewLabel;
  Gen(OP_GO, L, 0, 0);

  while IsCurrText('catch') do
  begin
    DeclareSwitch := true;
    Call_SCANNER;
    ID := Parse_Ident;
    Gen(OP_CATCH, ID, 0, 0);
    DeclareSwitch := false;

    CurrClassRec.AddField(ID, [modSTATIC]);

    repeat
      Parse_Statement;
    until IsCurrText('end') or IsCurrText('finally');

    Gen(OP_DISCARD_ERROR, 0, 0, 0);
  end;

  SetLabelHere(L);

  if IsCurrText('finally') then
  begin
    Gen(OP_FINALLY, 0, 0, 0);
    Call_SCANNER;
    repeat
      Parse_Statement;
    until IsCurrText('end');
    Gen(OP_EXIT_ON_ERROR, 0, 0, 0);
  end;

  SetLabelHere(LTRY);
  Gen(OP_TRY_OFF, 0, 0, 0);

  Match('end');
  Call_SCANNER;
  Match('try');
  Call_SCANNER;
end;

procedure TPAXBasicParser.Parse_ExitStmt;
var
  EntryStack: TPAXEntryStack;
begin
  // match "exit"
  Call_SCANNER;

  EntryStack := nil;
  if IsCurrText('do') then
    EntryStack := EntryStackDO
  else if IsCurrText('for') then
    EntryStack := EntryStackFOR
  else if IsCurrText('sub') then
  begin
    if CurrSubID = 0 then
      raise TPAXScriptFailure.Create(errStatementIsNotAllowedHere);
    Gen(OP_EXIT, 0, 0, 0);
    Call_SCANNER;
    Exit;
  end
  else if IsCurrText('function') then
  begin
    if CurrSubID = 0 then
      raise TPAXScriptFailure.Create(errStatementIsNotAllowedHere);
    Gen(OP_EXIT, 0, 0, 0);
    Call_SCANNER;
    Exit;
  end
  else if IsCurrText('property') then
  begin
    if CurrSubID = 0 then
      raise TPAXScriptFailure.Create(errStatementIsNotAllowedHere);
    Gen(OP_EXIT, 0, 0, 0);
    Call_SCANNER;
    Exit;
  end
  else
    Match('do');

  if EntryStack.Count = 0 then
    raise TPAXScriptFailure.Create(errStatementIsNotAllowedHere);

  Gen(OP_EXIT, EntryStack.TopBreakLabel, 0, 0);
  Call_SCANNER;
end;

procedure TPAXBasicParser.Parse_WhileStmt;
var
  L, LF: Integer;
begin
  // match "while"
  L := NewLabel;
  LF := NewLabel;
  SetLabelHere(L);

  Call_SCANNER;
  Gen(OP_GO_FALSE_EX, LF, Parse_Expression, 0);

  repeat
    Parse_Statement;
  until IsCurrText('end');

  Gen(OP_GO, L, 0, 0);
  SetLabelHere(LF);

  Call_SCANNER;
  Match('while');
  Call_SCANNER;
end;

function TPAXBasicParser.Parse_ForStmt: Boolean;
var
  StepDirID, Expr1ID, Expr2ID, StepID, RelExprID, L, LF, L1, L2, LC, ID: Integer;
  R: TPAXForLoopRec;
begin
  // match "for"

  result := true;

  L := NewLabel;
  LF := NewLabel;
  LC := NewLabel;

  RelExprID := NewVar;

  Call_SCANNER;
  Expr1ID := Parse_Ident;
  Match('=');
  Call_SCANNER;
  Gen(OP_ASSIGN, Expr1ID, Parse_Expression, Expr1ID);

  Match('to');
  Call_SCANNER;
  Expr2ID := Parse_Expression;

  if IsCurrText('step') then
  begin
    L1 := NewLabel;
    L2 := NewLabel;

    Call_SCANNER;
    SetLabelHere(L);

    StepID := Parse_Expression;

    StepDirID := NewVar;
    Gen(OP_GT, StepID, NewConst(0), StepDirID);

    Gen(OP_GO_FALSE_EX, L1, StepDirID, 0);

    Gen(OP_LE, Expr1ID, Expr2ID, RelExprID);

    Gen(OP_GO, L2, 0, 0);
    SetLabelHere(L1);

    Gen(OP_GE, Expr1ID, Expr2ID, RelExprID);

    SetLabelHere(L2);
  end
  else
  begin
    StepID := NewConst(1);
    SetLabelHere(L);
    Gen(OP_LE, Expr1ID, Expr2ID, RelExprID);
  end;

  Gen(OP_GO_FALSE_EX, LF, RelExprID, 0);

  EntryStackFOR.Push(LF, LC, StatementLabel);
  ForLoopStack.Push(Expr1ID, StepID, L, LC, LF);
  repeat
    Parse_Statement;
  until IsCurrText('next') or (ForLoopStack.Count = 0);
  IsExecutable := true;

  EntryStackFOR.Pop;

  if ForLoopStack.Count = 0 then
  begin
    result := false;
    Exit;
  end;

  Call_SCANNER;

  if IsCurrText(':') then
  begin
    SetLabelHere(LC);
    Gen(OP_PLUS, Expr1ID, StepID, Expr1ID);
    Gen(OP_GO, L, 0, 0);
    ForLoopStack.Pop;
    SetLabelHere(LF);
  end
  else
  begin
    ID := Parse_Ident;
    R := ForLoopStack.Top;
    if R.ID <> ID then
      raise TPAXScriptFailure.Create(Format(errIncorrectIdentifier,
         [Name[ID]]));
    SetLabelHere(R.LC);
    Gen(OP_PLUS, R.ID, R.StepID, R.ID);
    Gen(OP_GO, R.L, 0, 0);
    SetLabelHere(R.LF);

    ForLoopStack.Pop;
    while IsCurrText(',') do
    begin
      Call_SCANNER;
      ID := Parse_Ident;
      R := ForLoopStack.Top;
      if R.ID <> ID then
        raise TPAXScriptFailure.Create(Format(errIncorrectIdentifier,
           [Name[ID]]));
      SetLabelHere(R.LC);
      Gen(OP_PLUS, R.ID, R.StepID, R.ID);
      Gen(OP_GO, R.L, 0, 0);
      SetLabelHere(R.LF);
      ForLoopStack.Pop;
    end;
  end;
end;

procedure TPAXBasicParser.Parse_DoStmt;
var
  L, LF, ExprID, ID: Integer;
  SignWHILE: boolean;
begin
// match "do"

  L := NewLabel;
  LF := NewLabel;

  Call_SCANNER;
  if IsCurrText('while') or IsCurrText('until') then
  begin
    if IsCurrText('while') then
      SignWHILE := true
    else
      SignWHILE := false;

    SetLabelHere(L);

    Call_SCANNER;
    ExprID := Parse_Expression;

    if not SignWHILE then
    begin
      ID := NewVar;
      Gen(OP_NOT, ExprID, 0, ID);
    end
    else
      ID := ExprID;

    Gen(OP_GO_FALSE_EX, LF, ID, 0);

    EntryStackDO.Push(LF, L, StatementLabel);
    repeat
      Parse_Statement;
    until IsCurrText('loop');
    EntryStackDO.Pop;

    Gen(OP_GO, L, 0, 0);

    SetLabelHere(LF);

    Call_SCANNER;
    Exit;
  end;

  Match(':');

  SetLabelHere(LF);

  EntryStackDO.Push(L, LF, StatementLabel);
  repeat
    Parse_Statement;
  until IsCurrText('loop');

  Call_SCANNER;

  SignWHILE := false;
  if IsCurrText('while') then
    SignWHILE := true
  else
    Match('until');

  Call_SCANNER;
  ExprID := Parse_Expression;

  if SignWHILE then
  begin
    ID := NewVar;
    Gen(OP_NOT, ExprID, 0, ID);
  end
  else
    ID := ExprID;

  Gen(OP_GO_FALSE_EX, LF, ID, 0);

  SetLabelHere(L);
end;

procedure TPAXBasicParser.Parse_SelectCaseStmt;
var
  L, LF, ExprID, RelExprID: Integer;
begin
  // match "select"

  L := NewLabel;

  Call_SCANNER;
  Match('case');

  Call_SCANNER;
  ExprID := Parse_Expression;

  SkipColons;

  while IsCurrText('case') do
  begin
    LF := NewLabel;
    SetLabelHere(LF);

    Call_SCANNER;
    if IsCurrText('else') then
      Call_SCANNER
    else
    begin
      RelExprID := NewVar;
      Gen(OP_EQ, ExprID, Parse_Expression, RelExprID);
      Gen(OP_GO_FALSE_EX, LF, RelExprID, 0);
    end;

    repeat
      Parse_Statement;
    until IsCurrText('case') or IsCurrText('end');

    Gen(OP_GO, L, 0, 0);

    SetLabelHere(LF);
    if IsCurrText('end') then
      Break;
  end;

  Call_SCANNER;
  Match('select');
  Call_SCANNER;

  SetLabelHere(L);
end;

procedure TPAXBasicParser.Parse_WithStmt;
var
  I, K, ID, ID2: Integer;
begin
  // match "with"

  Call_SCANNER;
  ID := Parse_Expression;
  if IsCallOperator then
  begin
    ID2 := NewVar;
    Gen(OP_ASSIGN, ID2, ID, ID2);
    Gen(OP_BEGIN_WITH, WithStack.Push(ID2), 0, 0);
  end
  else
    Gen(OP_BEGIN_WITH, WithStack.Push(ID), 0, 0);
  K := 1;
  while IsCurrText(',') do
  begin
    Inc(K);
    Call_SCANNER;
    ID := Parse_Expression;
    if IsCallOperator then
    begin
      ID2 := NewVar;
      Gen(OP_ASSIGN, ID2, ID, ID2);
      Gen(OP_BEGIN_WITH, WithStack.Pus

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?