pax_basic.pas

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

PAS
2,916 行
字号
begin
  ResID := 0;

  if CurrToken.ID = OP_NOT then
  begin
    result := NewVar;
    Call_SCANNER;
    Gen(OP_NOT, ToBoolean(Parse_EqualityExpression), 0, result);
  end
  else
    result := Parse_EqualityExpression;

  while
     (CurrToken.ID = OP_AND) or
     (CurrToken.ID = OP_OR) or
     (CurrToken.ID = OP_XOR) or
     (CurrToken.ID = SP_LOGICAL_IMP)
  do
  begin
    OP := CurrToken.ID;
    Call_SCANNER;
    if ResID = 0 then
    begin
      if OP = SP_LOGICAL_IMP then // A imp B --> (not A) or B
      begin
        A := NewVar;
        Gen(OP_NOT, ToBoolean(result), 0, A);
        ResID := NewVar;
        Gen(OP_OR, A, ToBoolean(Parse_EqualityExpression), ResID);
      end
      else
      begin
        if (OP = OP_OR) and ShortEvalSwitch then
        begin
          ResID := Parse_ShortEvalOR(result, Parse_EqualityExpression, nil);
        end
        else if (OP = OP_AND) and ShortEvalSwitch then
        begin
          ResID := Parse_ShortEvalAND(result, Parse_EqualityExpression, nil);
        end
        else
        begin
          ResID := NewVar;
          Gen(OP, ToBoolean(result), ToBoolean(Parse_EqualityExpression), ResID);
        end;
      end;
    end
    else
    begin
      if OP = SP_LOGICAL_IMP then
      begin
        A := NewVar;
        Gen(OP_NOT, result, 0, A);
        ResID := NewVar;
        Gen(OP_OR, A, ToBoolean(Parse_EqualityExpression), ResID);
      end
      else
      begin
        if (OP = OP_OR) and ShortEvalSwitch then
        begin
          ResID := Parse_ShortEvalOR(result, Parse_EqualityExpression, nil);
        end
        else if (OP = OP_AND) and ShortEvalSwitch then
        begin
          ResID := Parse_ShortEvalAND(result, Parse_EqualityExpression, nil);
        end
        else
        begin
          Gen(OP, result, ToBoolean(Parse_EqualityExpression), ResID);
        end;
      end;
    end;
    result := ResID;
  end;
end;

procedure TPAXBasicParser.Parse_Statement;
var
  ml: TPAXModifierList;
  Normal: Boolean;
  cc: Integer;
begin
  ml := Parse_ModifierList;
  Normal := true;

  if CurrToken.ID = SP_COLON then // EmptyStatement
  begin end
  else if IsCurrText('class') then
    Parse_ClassStmt(ml, ckClass)
  else if IsCurrText('structure') then
    Parse_ClassStmt(ml, ckStructure)
  else if IsCurrText('enum') then
    Parse_EnumStmt
  else if IsCurrText('dim') then
  begin
    DeclareSwitch := true;
    Call_SCANNER;

    Parse_DimStmt(false, ml);
  end
  else if IsCurrText('namespace') then
    Parse_NamespaceStmt(ml)
  else if IsCurrText('declare') then
  begin
    cc := Parse_CallConv;
    if cc < 0 then
    begin
      cc := DefaultCallConv;
      Call_SCANNER;
    end;
    if IsCurrText('function') then
      Parse_FunctionStmt(tsGlobal, ml, cc)
    else if IsCurrText('sub') then
      Parse_FunctionStmt(tsGlobal, ml, cc)
    else
      Match('sub');
  end
  else if IsCurrText('function') then
    Parse_FunctionStmt(tsGlobal, ml)
  else if IsCurrText('sub') then
    Parse_FunctionStmt(tsGlobal, ml)
  else if IsCurrText('goto') then
  begin
    IsExecutable := true;
    Parse_GoToStmt;
    IsExecutable := false;
  end
  else if IsCurrText('label') then
  begin
    IsExecutable := true;
    Call_SCANNER;
    Parse_SetLabel;
    IsExecutable := false;
  end
  else if IsCurrText('if') then
  begin
    IsExecutable := true;
    Parse_IfStmt;
    IsExecutable := false;
  end
  else if IsCurrText('return') then
  begin
    IsExecutable := true;
    Parse_ReturnStmt;
    IsExecutable := false;
  end
  else if IsCurrText('select') then
  begin
    IsExecutable := true;
    Parse_SelectCaseStmt;
    IsExecutable := false;
  end
  else if IsCurrText('throw') then
  begin
    IsExecutable := true;
    Parse_ThrowStmt;
    IsExecutable := false;
  end
  else if IsCurrText('try') then
  begin
    IsExecutable := true;
    Parse_TryStmt;
    IsExecutable := false;
  end
  else if IsCurrText('exit') then
  begin
    IsExecutable := true;
    Parse_ExitStmt;
    IsExecutable := false;
  end
  else if IsCurrText('while') then
  begin
    IsExecutable := true;
    Parse_WhileStmt;
    IsExecutable := false;
  end
  else if IsCurrText('for') then
  begin
    IsExecutable := true;
    Normal := Parse_ForStmt;
    IsExecutable := false;
  end
  else if IsCurrText('do') then
  begin
    IsExecutable := true;
    Parse_DoStmt;
    IsExecutable := false;
  end
  else if IsCurrText('with') then
  begin
    IsExecutable := true;
    Parse_WithStmt;
    IsExecutable := false;
  end
  else if IsCurrText('imports') then
    Parse_ImportsStmt
  else
  begin
    IsExecutable := true;
    Parse_SimpleStatement;
    IsExecutable := false;
  end;

  if CurrToken.ID = SP_EOF then
    Exit;

  if Normal then
  begin
    if IsCurrText('print') or IsCurrText('println') then
    begin
      IsExecutable := true;
      Parse_Statement;
      IsExecutable := false;
      Exit;
    end;

    Match(':');
    Call_SCANNER;
    if IsCurrText(':') then
      SkipColons;
  end;

  GenDestroyArrayArgumentList;
end;

procedure TPAXBasicParser.Parse_DimStmt(IsField: Boolean; ml: TPAXModifierList);
var
  L: Integer;
begin
  L := 0;
  if not IsField then
  if (CurrClassID > 0) and (CurrSubID = 0) then
  begin
    L := NewLabel;
    Gen(OP_SKIP, 0, 0, 0);
    Gen(OP_GO, L, 0, 0);
  end;

  Parse_VariableDeclaration(IsField, ml);

  while IsCurrText(',') do
  begin
    DeclareSwitch := true;

    Call_SCANNER;
    Parse_VariableDeclaration(IsField, ml);
  end;

  DeclareSwitch := false;

  if not IsField then
  if (CurrClassID > 0) and (CurrSubID = 0) then
  begin
    SetLabelHere(L);
  end;
end;

function TPAXBasicParser.Parse_VariableDeclaration(IsField: Boolean; ml: TPAXModifierList): Integer;
var
  ArrID, T, Vars, ID, N, Rank, K: Integer;
  MemberRec: TPAXMemberRec;
  ObjectInit: Boolean;
begin
  T := 0;
  MemberRec := nil;
  ObjectInit := false;

  result := Parse_Ident;

  if IsField then
  begin
    MemberRec := CurrClassRec.FindMember(NameIndex[result], maAny, true);
    if MemberRec <> nil then
      raise TPAXScriptFailure.Create(Format(errIdentifierIsRedeclared, [Name[result]]));

    MemberRec := CurrClassRec.AddField(result, ml);
  end
  else if (CurrClassID > 0) and (CurrSubID = 0) then
  begin
    MemberRec := CurrClassRec.FindMember(NameIndex[result], maAny, true);
    if MemberRec <> nil then
      raise TPAXScriptFailure.Create(Format(errIdentifierIsRedeclared, [Name[result]]));
      
    MemberRec := CurrClassRec.AddField(result, ml + [modSTATIC]);
  end
  else if CurrSubID > 0 then
  begin
    SymbolTable.DecCard;
    if SymbolTable.LookUpID(Name[result], CurrSubID, true) > 0 then
      raise TPAXScriptFailure.Create(Format(errIdentifierIsRedeclared,
                                     [Name[result]]));
    SymbolTable.IncCard;
    SymbolTable.SetLocal(result);

    TestDupLocalVars(result);

    LocalVars.Add(result);
  end;

  if IsCurrText('As') then
  begin
    DeclareSwitch := false;
    Call_SCANNER;
    DeclareSwitch := true;

    T := Parse_TypeID;

    if (T > PAXTypes.Count) or (T < 0) then
    begin
      TPaxBaseScripter(Scripter).UnknownTypes.AddObject(T, TPaxIDRec.Create(T, Code.Card, Scanner.PosNumber));

      if MemberRec <> nil then
         MemberRec.InitN := LastCodeLine;
      T := GenEvalWith(T);
      Gen(OP_CHECK_CLASS, T, 0, result);
      Gen(OP_CREATE_OBJECT, T, 0, result);
      if MemberRec <> nil then
      begin
        if IsCurrText('=') and IsNextText('(') then
          ObjectInit := true
        else
          Gen(OP_HALT, 0, 0, 0);
      end;
    end;

    TypeID[result] := T;
  end;

  if IsCurrText('(') then
  begin
    if MemberRec <> nil then
      MemberRec.InitN := LastCodeLine;

    if VBArrays then
      Gen(OP_VBARRAYS_ON, 0, 0, 0)
    else
      Gen(OP_VBARRAYS_OFF, 0, 0, 0);

    DeclareSwitch := false;
    Call_SCANNER;

    ArrID := 0;
    K := Parse_ArgumentList(ArrID, Vars);
    N := Gen(OP_CREATE_ARRAY, result, K, 0);

    if MemberRec <> nil then
       Gen(OP_HALT, 0, 0, 0);

    Match(')');
    Call_SCANNER;

    if IsCurrText('As') then
    begin
      DeclareSwitch := false;
      Call_SCANNER;
      DeclareSwitch := true;
      T := Parse_TypeID;

      Code.Prog[N].Res := T;

      Rank := 0;
      if IsCurrText('[') then
        Rank := Parse_Rank
      else
        if (T <= PaxTypes.Count) and (T > 0) and (T <> typeVARIANT) then
           Rank := -1;

      SymbolTable.Rank[result] := Rank;
      if (K <> Rank) then
         raise TPaxScriptFailure.Create(errRankMismatch);
    end;
  end
  else if IsCurrText('[') then
  begin
    if MemberRec <> nil then
      MemberRec.InitN := LastCodeLine;

    if VBArrays then
      Gen(OP_VBARRAYS_ON, 0, 0, 0)
    else
      Gen(OP_VBARRAYS_OFF, 0, 0, 0);

    DeclareSwitch := false;
    Call_SCANNER;

    ArrID := 0;
    K := Parse_ArgumentList(ArrID, Vars);
    N := Gen(OP_CREATE_ARRAY, result, K, 0);

    if MemberRec <> nil then
       Gen(OP_HALT, 0, 0, 0);

    Match(']');
    Call_SCANNER;

    if IsCurrText('As') then
    begin
      DeclareSwitch := false;
      Call_SCANNER;
      DeclareSwitch := true;
      T := Parse_TypeID;

      Code.Prog[N].Res := T;

      Rank := 0;
      if IsCurrText('[') then
        Rank := Parse_Rank
      else
        if (T <= PaxTypes.Count) and (T > 0) and (T <> typeVARIANT) then
           Rank := -1;

      SymbolTable.Rank[result] := Rank;
      if (K <> Rank) and (Rank > 0) then
         raise TPaxScriptFailure.Create(errRankMismatch);
    end;
  end
  else if IsCurrText('=') then
  begin
    Gen(OP_NOP, 0, 0, 0);

    if (MemberRec <> nil) and (not ObjectInit) then
      MemberRec.InitN := LastCodeLine;

    DeclareSwitch := false;
    Call_SCANNER;

    Gen(OP_SKIP, 0, 0, 0);
    if IsCurrText('(') then
    begin
      Parse_ObjectInitializer(result);
      if MemberRec <> nil then
        Gen(OP_HALT, 0, 0, 0);
    end
    else
    begin
      ID := Parse_Expression;

      if ID = UndefinedID then
        Gen(OP_DESTROY_INTF, result, 0, 0);
      Gen(OP_ASSIGN, result, ID, result);

      if MemberRec <> nil then
      begin
  //      if Kind[ID] = KindCONST then
          Value[MemberRec.ID] := Value[ID];
        Gen(OP_HALT, 0, 0, 0);
      end;
    end;

    TempObjectList.Clear;
  end
  else if T in [typeINTEGER, typeCARDINAL, typeBYTE, typeWORD, typeSHORTINT, typeSMALLINT, typeINT64, typeBOOLEAN, typeDOUBLE, typeSTRING] then
  if modSTATIC in CurrClassRec.ml then
  begin
    if T in [typeINTEGER, typeCARDINAL, typeBYTE, typeWORD, typeSHORTINT, typeSMALLINT, typeINT64] then
    begin
      Gen(OP_NOP, 0, 0, 0);
      if (MemberRec <> nil) and (not ObjectInit) then
        MemberRec.InitN := LastCodeLine;
      Gen(OP_SKIP, 0, 0, 0);
      ID := NewConst(0);
      Gen(OP_ASSIGN, result, ID, result);
      if MemberRec <> nil then
        Gen(OP_HALT, 0, 0, 0);
    end
    else if T = typeBOOLEAN then
    begin
      Gen(OP_NOP, 0, 0, 0);
      if (MemberRec <> nil) and (not ObjectInit) then
        MemberRec.InitN := LastCodeLine;
      Gen(OP_SKIP, 0, 0, 0);
      ID := NewConst(false);
      Gen(OP_ASSIGN, result, ID, result);
      if MemberRec <> nil then
        Gen(OP_HALT, 0, 0, 0);
    end
    else if T = typeDOUBLE then
    begin
      Gen(OP_NOP, 0, 0, 0);
      if (MemberRec <> nil) and (not ObjectInit) then
        MemberRec.InitN := LastCodeLine;
      Gen(OP_SKIP, 0, 0, 0);
      ID := NewConst(0.0);
      Gen(OP_ASSIGN, result, ID, result);
      if MemberRec <> nil then
        Gen(OP_HALT, 0, 0, 0);
    end
    else if T = typeSTRING then
    begin
      Gen(OP_NOP, 0, 0, 0);
      if (MemberRec <> nil) and (not ObjectInit) then
        MemberRec.InitN := LastCodeLine;
      Gen(OP_SKIP, 0, 0, 0);
      ID := NewConst('');
      Gen(OP_ASSIGN, result, ID, result);
      if MemberRec <> nil then
        Gen(OP_HALT, 0, 0, 0);
    end;
  end;
end;

function TPAXBasicParser.Parse_ModifierList: TPAXModifierList;
label
  Again;
begin
  result := [];

Again:

  if IsCurrText('default') then
  begin
    result := result + [modDEFAULT];
    Call_SCANNER;
    Goto Again;
  end;
  if IsCurrText('public') then
  begin
    result := result + [modPUBLIC];
    Call_SCANNER;
    Goto Again;
  end;
  if IsCurrText('private') then
  begin
    result := result + [modPRIVATE];

    if NextToken.TokenClass <> tcKeyword then
    begin
      CurrToken.Text := NextToken.Text;
      CurrToken.Text := 'Dim';
      Exit;
    end;

    Call_SCANNER;
    Goto Again;
  end;
  if IsCurrText('protected') then
  begin
    result := result + [modPROTECTED];
    Call_SCANNER;
    Goto Again;
  end;
  if IsCurrText('shared') then
  begin
    result := result + [modSTATIC];
    Call_SCANNER;
    Goto Again;
  end;
  if IsCurrText('virtual') then
  begin
    result := result + [modVIRTUAL];
    Call_SCANNER;
    Goto Again;
  end;
  if IsCurrText('override') then
  begin
    result := result + [modVIRTUAL];
    Call_SCANNER;
    Goto Again;
  end;
end;

procedure TPAXBasicParser.Parse_FunctionStmt(ts: TPAXTypeSub;
                                             ml: TPAXModifierList;
                                             cc: Integer = _ccStdCall);
var
  L, SubID, ResID, ParamID, ParamCount, WithCount, T,
  _SaveLevelCard, TempCard, ValID: Integer;
  S, EndWord: String;
  IsDllDefined: Boolean;
  DllName, ProcName: String;
  Rank: Integer;
  sign: Integer;
begin
  // match "function" or "sub"
  _SaveLevelCard := LevelStack.Card;

  EndWord := CurrToken.Text;

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

  DeclareSwitch := true;
  Call_SCANNER;

⌨️ 快捷键说明

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