pax_basic.pas

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

PAS
2,916 行
字号

  if StrEql(EndWord, 'Operator') then
    SubID := Parse_OverloadableOperator
  else
    SubID := Parse_Ident;

  SymbolTable.CallConv[SubID] := cc;

  S := Name[SubID];

  LevelStack.PushSub(SubID, CurrClassID, ml);

  ResID := NewVar;
  NewVar;
  ParamCount := 0;

  if IsCurrText('Lib') then
  begin
    IsDllDefined := true;
    DeclareSwitch := false;

    TempCard := SymbolTable.Card;

    Call_SCANNER;
    DeclareSwitch := true;
    DllName := Value[CurrToken.ID];
    ProcName := Name[SubID];
    Call_SCANNER;
    if IsCurrText('Alias') then
    begin
      Call_SCANNER;
      ProcName := CurrToken.Text;
      Call_SCANNER;
    end;

    SymbolTable.EraseTail(TempCard);
    SymbolTable.Card := TempCard;
  end
  else
    IsDllDefined := false;

  if IsCurrText('(') then
  begin
    DeclareSwitch := true;
    Call_SCANNER;
    if not IsCurrText(')') then
    begin
      ParamCount := 1;

      repeat
        if IsCurrText('ByVal') then
        begin
          Call_SCANNER;
          ParamID := Parse_Ident;
        end
        else if IsCurrText('ByRef') then
          ParamID := Parse_ByRef
        else
          ParamID := Parse_Ident;

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

          T := typeVARIANT;
          if NewID then
          begin
            SymbolTable.TypeNameIndex[ParamID] := CreateNameIndex(CurrToken.Text, Scripter);

            Gen(OP_SET_TYPE, ParamID, SymbolTable.TypeNameIndex[ParamID], 0);

            SymbolTable.DecCard;
            Call_SCANNER;
          end
          else
          begin
            if CurrToken.ID > PaxTypes.Count then
              SymbolTable.TypeNameIndex[ParamID] := CreateNameIndex(CurrToken.Text, Scripter);
            T := Parse_Ident;
          end;
          TypeID[ParamID] := T;

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

          SymbolTable.Rank[ParamID] := Rank;

        end;

        if IsCurrText('=') then
        begin
          TempCard := SymbolTable.Card;
          Call_SCANNER;

          if IsCurrText('-') then
          begin
            sign := -1;
            Call_SCANNER;
          end
          else
            sign := 1;

          ValID := Parse_Expression;
          with TPaxBaseScripter(Scripter) do
          begin
            if sign = - 1 then
              DefaultParameterList.AddParameter(SubID, ParamID, - SymbolTable.VariantValue[ValID])
            else
              DefaultParameterList.AddParameter(SubID, ParamID, SymbolTable.VariantValue[ValID]);
          end;
          SymbolTable.EraseTail(TempCard);
        end;

        if IsCurrText(',') then
        begin
          Inc(ParamCount);
          Call_SCANNER;
        end
        else
          Break;
      until false;
    end;
    Match(')');
    DeclareSwitch := false;

    Call_SCANNER;
  end;

  if StrEql(EndWord, 'Function') or StrEql(EndWord, 'Operator') then
  begin
    if IsCurrText('As') then
    begin
      DeclareSwitch := false;
      Call_SCANNER;
      DeclareSwitch := true;
      TypeID[SubID] := Parse_TypeID;
      TypeID[ResID] := TypeID[SubID];
    end;
  end
  else
    TypeID[SubID] := typeVOID;

  Match(':');

  Kind[SubID] := KindSUB;
  Count[SubID] := ParamCount;
  Next[SubID] := ResID;
  TypeSub[SubID] := ts;
  if StrEql(Name[SubID], 'New') then
    TypeSub[SubID] := tsConstructor;
  Value[SubID] := Code.Card;

  DeclareSwitch := false;

  if IsDllDefined then
  begin
    Name[NewVar] := DllName;
    Name[NewVar] := ProcName;

    LevelStack.Card := _SaveLevelCard;
    Next[SubID] := 0;
    Value[SubID] := 0;

    SetLabelHere(L);
    Exit;
  end;

  if CurrThisID > 0 then
  begin
    Name[CurrThisID] := 'Me';
    TypeID[CurrThisID] := CurrClassID;
    WithCount := GenBeginWith(CurrThisID);
  end
  else
    WithCount := GenBeginWith(CurrClassID);

  repeat
    Parse_Statement;
  until IsCurrText('end');
  IsExecutable := true;

  GenEndWith(WithCount);

  GenDestroyLocalVars;
  Gen(OP_RET, 0, 0, 0);
  SetLabelHere(L);

  LevelStack.Pop;

  LinkVariables(SubID, StrEql(EndWord, 'Function') or StrEql(EndWord, 'Operator'));

  Call_SCANNER;
  IsExecutable := false;
  Match(EndWord);
  Call_SCANNER;
end;

procedure TPAXBasicParser.Parse_ReturnStmt;
var
  ResID: Integer;
begin
  // match "return"

  ResID := SymbolTable.GetResultID(CurrLevel);

  Call_SCANNER;

  if not IsCurrText(':') then
    Gen(OP_ASSIGN_RESULT, ResID, Parse_Expression, ResID);

  Gen(OP_EXIT, 0, 0, 0);
end;

procedure TPAXBasicParser.Parse_HaltStmt;
begin
  // match "halt"
  Call_SCANNER;
  Gen(OP_HALT_GLOBAL, 0, 0, 0);
end;

procedure TPAXBasicParser.Parse_NamespaceStmt(ml: TPAXModifierList);
var
  NamespaceID: Integer;
begin
  CurrClassRec.UsingInitList.Add(LastCodeLine);
  // match "namespace"
  Call_SCANNER;
  NamespaceID := Parse_Ident;

  TPAXBaseScripter(Scripter).Modules.Items[ModuleID].Namespaces.Add(NamespaceID);

  Kind[NamespaceID] := KindTYPE;

  LevelStack.PushClass(NamespaceID, 0, ml + [modSTATIC], ckClass, true);

  Gen(OP_USE_NAMESPACE, UsingList.PushUnique(NamespaceID), 0, 0);
  Gen(OP_HALT_OR_NOP, 0, 0, 0);

  SkipColons;

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

  Call_SCANNER;
  Match('namespace');
  Gen(OP_END_OF_NAMESPACE, NamespaceID, 0, 0);

  Call_SCANNER;

  LevelStack.Pop;
  UsingList.Delete(NamespaceID);
end;

function TPAXBasicParser.Parse_ClassStmt(ClassML: TPAXModifierList; ck: TPAXClassKind): Integer;
var
  ClassID, AncestorClassID: Integer;

procedure Parse_PropertyStmt(ml: TPAXModifierList);
var
  PropertyRec: TPAXMemberRec;
  PropID, ParamID, SubID, ResID, ValueID, I, ID: Integer;
  ParamIds: TPaxIds;
begin
  // match "property"
  DeclareSwitch := true;
  Call_SCANNER;
  PropID := CurrToken.ID;

  PropertyRec := CurrClassRec.AddProperty(Parse_Ident, ml);

  Kind[PropID] := KindPROP;
  if modDEFAULT in ml then
    PropertyRec.ml := PropertyRec.ml + [modDEFAULT];

  ParamIds := TPaxIds.Create(false);

  if IsCurrText('(') then
  begin
    DeclareSwitch := true;
    Call_SCANNER;
    repeat
      ParamID := Parse_Ident;
      ParamIds.Add(ParamID);

      if IsCurrText('As') then
      begin
        DeclareSwitch := false;
        Call_SCANNER;
        DeclareSwitch := true;
        TypeID[ParamID] := Parse_TypeID;
      end;

      if IsCurrText(',') then
        Call_SCANNER
      else
        break;
    until false;

    Match(')');
    DeclareSwitch := false;
    Call_SCANNER;
  end;

  if IsCurrText('As') then
  begin
    Call_SCANNER;
    TypeID[PropID] := Parse_TypeID;
  end;

  Match(':');
  Call_SCANNER;
  SkipColons;

  while IsCurrText('get') or
        IsCurrText('set')
  do
  begin
    if IsCurrText('get') then
    begin
      if PropertyRec.ReadID <> 0 then
        Match('end');

      SubID := NewVAR;
      PropertyRec.ReadID := SubID;

      Name[SubID] := Name[PropID] + '_get';

      LevelStack.PushSub(SubID, CurrClassID, ml);

      ResID := NewVar;
      NewVar;

      for I:=0 to ParamIds.Count - 1 do
      begin
        ID := NewVar;
        Name[ID] := Name[Integer(ParamIds[I])];
      end;

      Kind[SubID] := KindSUB;
      Count[SubID] := 0 + ParamIds.Count;
      Next[SubID] := ResID;
      TypeSub[SubID] := tsMethod;

      if Code.Prog[Code.Card].OP = OP_SEPARATOR then
        Value[SubID] := Code.Card
      else
        Value[SubID] := Code.Card + 1;

      Name[ResID] := 'result';
      Name[CurrThisID] := 'Me';
      TypeID[CurrThisID] := CurrClassID;

      Gen(OP_BEGIN_WITH, WithStack.Push(CurrThisID), 0, 0);

      Call_SCANNER;
      DeclareSwitch := false;
      repeat
        Parse_Statement;
      until IsCurrText('end');

      if CurrThisID > 0 then
      begin
        Gen(OP_END_WITH, 0, 0, 0);
        WithStack.Pop;
      end;

      Gen(OP_RET, 0, 0, 0);
      LevelStack.Pop;

      LinkVariables(SubID, true);

      Call_SCANNER;
      Match('get');

      Call_SCANNER;
      SkipColons;
    end
    else if IsCurrText('set') then
    begin
      if PropertyRec.WriteID <> 0 then
        Match('end');

      SubID := NewVAR;
      PropertyRec.WriteID := SubID;

      Name[SubID] := Name[PropID] + '_set';

      LevelStack.PushSub(SubID, CurrClassID, ml);

      ResID := NewVar;
      NewVar;

      for I:=0 to ParamIds.Count - 1 do
      begin
        ID := NewVar;
        Name[ID] := Name[Integer(ParamIds[I])];
      end;

      if IsNextText('(') then
      begin
        Call_SCANNER;

        Call_SCANNER;
        Parse_Ident;
        Match(')');
      end
      else
      begin
        ValueID := NewVar;
        Name[ValueID] := 'Value';
      end;

      Kind[SubID] := KindSUB;
      Count[SubID] := 1 + ParamIds.Count;
      Next[SubID] := ResID;
      TypeSub[SubID] := tsMethod;
      Value[SubID] := Code.Card + 1;
      Name[ResID] := 'result';
      Name[CurrThisID] := 'Me';
      TypeID[CurrThisID] := CurrClassID;

      Gen(OP_BEGIN_WITH, WithStack.Push(CurrThisID), 0, 0);

      Call_SCANNER;
      DeclareSwitch := false;
      repeat
        Parse_Statement;
      until IsCurrText('end');

      if CurrThisID > 0 then
      begin
        Gen(OP_END_WITH, 0, 0, 0);
        WithStack.Pop;
      end;

      Gen(OP_RET, 0, 0, 0);
      LevelStack.Pop;

      LinkVariables(SubID, false);

      Call_SCANNER;
      Match('set');

      Call_SCANNER;
      SkipColons;
    end;
  end;

  if PropertyRec.ReadID + PropertyRec.WriteID = 0 then
    Match('get');

  Match('end');

  Call_SCANNER;
  Match('property');

  Call_SCANNER;

  PropertyRec.NParams := ParamIds.Count;
  ParamIds.Free;
end;

var
  L: Integer;
  ml: TPAXModifierList;
  cc: Integer;
begin
  // match "class"
  Gen(OP_SKIP, 0, 0, 0);
  L := NewLabel;
  Gen(OP_GO, L, 0, 0);

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

  ClassID := Parse_Ident;
  result := ClassID;

  Kind[ClassID] := KindTYPE;

  SkipColons;
  if IsCurrText('inherits') then
  begin
    Call_SCANNER;
    AncestorClassID := Parse_Ident;
    SymbolTable.Level[AncestorClassID] := -1;

    if AncestorClassID > PaxTypes.Count then
      TPaxBaseScripter(Scripter).UnknownTypes.AddObject(AncestorClassID,
        TPaxIDRec.Create(AncestorClassID, Code.Card, Scanner.PosNumber));

    Match(':');
    Call_SCANNER;
  end
  else
    AncestorClassID := 0;

  SkipColons;

  LevelStack.PushClass(ClassID, AncestorClassID, ClassML, ck, true);

  repeat
    ml := Parse_ModifierList;

    if IsCurrText('function') then
      Parse_FunctionStmt(tsMethod, ml)
    else if IsCurrText('operator') then
      Parse_FunctionStmt(tsMethod, ml)
    else if IsCurrText('sub') then
      Parse_FunctionStmt(tsMethod, 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(tsMethod, ml, cc)
      else if IsCurrText('sub') then
        Parse_FunctionStmt(tsMethod, ml, cc)
      else
        Match('sub');
    end
    else if IsCurrText('property') then
      Parse_PropertyStmt(ml)
    else if IsCurrText('dim') then
    begin
      DeclareSwitch := true;
      Call_SCANNER;

      Parse_DimStmt(true, ml);
    end
    else if IsCurrText('class') then
      Parse_ClassStmt(ml, ckClass)
    else if IsCurrText('enum') then
      Parse_EnumStmt
    else if IsCurrText('structure') then
      Parse_ClassStmt(ml, ckStructure);

    Match(':');
    Call_SCANNER;

    if CurrToken.ID = SP_EOF then
      Break;
    if IsCurrText('end') then
      Break;
  until false;

  LevelStack.Pop;
  SetLabelHere(L);

  Call_SCANNER;
  case ck of
    ckClass: Match('class');
    ckStructure: Match('structure');
  end;
  Call_SCANNER;
end;

function TPAXBasicParser.Parse_EnumStmt: Integer;
var
  ClassID, AncestorClassID: Integer;
  L, ID, ValueID: Integer;
  ml: TPAXModifierList;
  MemberRec: TPAXMemberRec;
begin
  // match "enum"

  ValueID := 0;

  Gen(OP_SKIP, 0, 0, 0);

⌨️ 快捷键说明

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