pax_pascal.pas

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

PAS
2,877 行
字号
  end
  else if IsCurrText('while') then
  begin
    IsExecutable := true;
    Parse_WhileStmt;
    IsExecutable := false;
  end
  else if IsCurrText('for') then
  begin
    IsExecutable := true;
    Parse_ForStmt;
    IsExecutable := false;
  end
  else if IsCurrText('break') then
  begin
    IsExecutable := true;
    Parse_BreakStmt;
    IsExecutable := false;
  end
  else if IsCurrText('continue') then
  begin
    IsExecutable := true;
    Parse_ContinueStmt;
    IsExecutable := false;
  end
  else if IsCurrText('exit') then
  begin
    IsExecutable := true;
    Parse_ExitStmt;
    IsExecutable := false;
  end
  else if IsCurrText('label') then
  begin
    IsExecutable := true;
    Parse_LabelStmt;
    IsExecutable := false;
  end
  else if IsCurrText('halt') then
  begin
    IsExecutable := true;
    Parse_HaltStmt;
    IsExecutable := false;
  end
  else if IsCurrText('with') then
  begin
    IsExecutable := true;
    Parse_WithStmt;
    IsExecutable := false;
  end
  else if IsCurrText('uses') then
    Parse_ImportsStmt
  else if IsCurrText('try') then
  begin
    IsExecutable := true;
    Parse_TryStmt;
    IsExecutable := false;
  end
  else if IsCurrText('raise') then
  begin
    IsExecutable := true;
    Parse_RaiseStmt;
    IsExecutable := false;
  end
  else if IsCurrText('program') then
    Parse_ProgramStmt
  else if IsCurrText('unit') then
    Parse_UnitStmt
  else
  begin
    IsExecutable := true;
    Parse_SimpleStatement;
    IsExecutable := false;
  end;

  GenDestroyArrayArgumentList;
end;

procedure TPAXPascalParser.Parse_DynamicArrayType(ClassID: Integer);
var
  ElTypeID: Integer;
  ClassDef: TPaxClassDefinition;
begin
  // match "of"
  Call_SCANNER();
  ElTypeID := Parse_TypeID();

  Gen(OP_CREATE_DYNAMIC_ARRAY_TYPE, ClassId, ElTypeId, 0);
  try
    _BeginWrite;
    ClassDef := DefinitionList.AddDynamicArrayType(Name[ClassID], Name[ElTypeID], nil);
    ClassDef.AddToScripter(Scripter);
  finally
    _EndWrite;
  end;
end;

procedure TPAXPascalParser.Parse_ArrayStmt(ClassID: Integer);

type
  TTypeRec = record
    B1, B2: String;
  end;

const
  BR = #13#10;

function L(J: Integer): String;
begin
  result := 'L' + IntToStr(J);
end;

function H(J: Integer): String;
begin
  result := 'H' + IntToStr(J);
end;

function I(J: Integer): String;
begin
  result := 'I' + IntToStr(J);
end;

var
  TypeRecords: array[1..20] of TTypeRec;
  K: Integer;
  J, P1, P2, TypeID: Integer;
  S, TypeName, ItemTypeName: String;

  TempScanner: TPAXScanner;
  temp: boolean;
begin
  // match "array"
  DeclareSwitch := false;

  K := 0;
  Call_SCANNER;

  if IsCurrText('of') then
  begin
    Parse_DynamicArrayType(ClassID);
    Exit;
  end;

  Match('[');

  TypeName := Name[ClassID];

  repeat
    Inc(K);

    Call_SCANNER;
    P1 := CurrToken.Position;
    Parse_Expression;
    P2 := CurrToken.Position;
    TypeRecords[K].B1 := Scanner.GetText(P1, P2 - 1);

    Match('..');
    Call_SCANNER;
    P1 := CurrToken.Position;
    Parse_Expression;
    P2 := CurrToken.Position;
    TypeRecords[K].B2 := Scanner.GetText(P1, P2 - 1);
  until not IsCurrText(',');

  DeclareSwitch := false;

  Match(']');
  Call_SCANNER;

  Match('of');
  Call_SCANNER;

  TypeID := Parse_TypeID;

  ItemTypeName := Name[TypeID];


  S := 'record ' + TypeName + BR +
       '  var' + BR;

  for J:=1 to K do
    S := S + L(J) + ',' + H(J) + ',';

  S := S + 'fItems;' + BR;

// Initialize

  S := S + 'procedure Initialize;' + BR
         + 'var V: ' + ItemTypeName + ';';

  S := S + 'var ';

  for J:=1 to K do
  begin
    S := S + I(J);
    if J < K then
      S := S + ','
    else
      S := S + ';'
  end;

  S := S + BR;

  S := S + 'begin' + BR;

  if TypeID = typeINTEGER then
    S := S + 'V := 0;' + BR
  else if TypeID = typeDOUBLE then
    S := S + 'V := 0.0;' + BR
  else if TypeID = typeSINGLE then
    S := S + 'V := 0.0;' + BR
  else if TypeID = typeCURRENCY then
    S := S + 'V := 0.0;' + BR
  else if TypeID = typeSTRING then
    S := S + 'V := "";' + BR
  else if TypeID = typeBOOLEAN then
    S := S + 'V := false; ' + BR;

  for J:=1 to K do
  begin
    S := S + L(J) + ':=' + TypeRecords[J].B1 + ';' + BR;
    S := S + H(J) + ':=' + TypeRecords[J].B2 + ';' + BR;
  end;

  S := S + 'var temp[';
  for J:=1 to K do
  begin
    S := S + H(J) + '-' + L(J);
    if J < K then
      S := S + ','
    else
      S := S + '];'
  end;

  S := S + BR;

  S := S + 'fItems := temp;' + BR;

  if IsArrayInitialization then
  begin
    for J:=1 to K do
      S := S + 'for ' + I(J) + ':=' + L(J) + ' to ' + H(J) + ' do ' + BR;
    S := S + 'fItems[';
    for J:=1 to K do
    begin
      S := S + I(J) + '-' + L(J);
      if J < K then
        S := S + ','
      else
        S := S + '] := V;'
    end;
  end;


  S := S + 'end;' + BR;

// end of Initialize

// GetItem
  S := S + 'function GetItem(';
  for J:=1 to K do
  begin
    S := S + I(J);
    if J < K then
      S := S + ','
    else
      S := S + ');'
  end;

  S := S + 'begin' + BR;
  S := S + 'result := @ fItems[';
  for J:=1 to K do
  begin
    S := S + I(J) + '-' + L(J);
    if J < K then
      S := S + ','
    else
      S := S + '];'
  end;
  S := S + 'end;' + BR;
// End of GetItem

// SetItem
  S := S + 'procedure SetItem(';
  for J:=1 to K do
    S := S + I(J) + ',';
  S := S + 'Value);';
  S := S + 'begin' + BR;


  S := S + 'fItems[';
  for J:=1 to K do
  begin
    S := S + I(J) + '-' + L(J);
    if J < K then
      S := S + ','
    else
      S := S + '] := Value;'
  end;

  S := S + 'end;' + BR;
// End of SetItem

// property Items

  S := S + 'property Items[';


  for J:=1 to K do
  begin
    S := S + I(J);
    if J < K then
      S := S + ','
    else
      S := S + '] read GetItem write SetItem; default;';
  end;

  S := S + 'end;' + BR;
// end of record

//    AddExtraCode(TypeName, S);

  TempScanner := Scanner;
  try
    Scanner := TPAXPascalScanner.Create(Self);
    Scanner.SetScripter(Scripter);
    Scanner.SourceCode := S;

//    SaveStringToTextFile(S, '1.txt');

    Call_SCANNER;
    Call_SCANNER;

    temp := IsInterfaceSection;
    IsInterfaceSection := false;
    Parse_ClassStmt([], ckSTRUCTURE, ClassID);
    IsInterfaceSection := temp;

  finally
    Scanner.Free;
  end;

  Scanner := TempScanner;
end;

procedure TPAXPascalParser.Parse_TypeStmt;
var
  ml: TPAXModifierList;
  I, ClassID: Integer;
begin
// match "type"
  ml := [];

  ClassID := LookUpID(NextToken.Text);
  if ClassID > 0 then
  begin
    I := ForwardIds.IndexOf(ClassID);
    if I >= 0 then
    begin
      ForwardIds.Delete(I);
      ForwardPos.Delete(I);
      Call_SCANNER;
      Call_SCANNER;
    end
    else
    begin
      DeclareSwitch := true;
      Call_SCANNER;
      DeclareSwitch := false;
      ClassID := Parse_Ident;
//    raise TPAXScriptFailure.Create(Format(errIdentifierIsRedeclared,
//         [Name[ClassID]]));
    end;
  end
  else
  begin
    DeclareSwitch := true;
    Call_SCANNER;
    DeclareSwitch := false;
//    ClassID := Parse_Designator;
    ClassID := Parse_Ident;
  end;

  Kind[ClassID] := KindTYPE;

  Match('=');

  Call_SCANNER;

  if IsCurrText('class') then
  begin
    if IsNextText(';') then
    begin
      Call_SCANNER;
      ForwardPos.Add(Code.Card);
      ForwardIds.Add(ClassID);
    end
    else
      Parse_ClassStmt(ml, ckClass, ClassID);
  end
  else if IsCurrText('record') then
    Parse_ClassStmt(ml, ckStructure, ClassID)
  else if IsCurrText('(') then
    Parse_EnumStmt(ClassID)
  else if IsCurrText('array') then
    Parse_ArrayStmt(ClassID)
  else
  begin
    TPaxBaseScripter(Scripter).TypeAliasList.Add(ClassID, Parse_TypeID);
  end;

  Match(';');

  if IsNext2Text('=') then
    Parse_TypeStmt;
end;

procedure TPAXPascalParser.Parse_StmtList;
begin
  repeat
    Parse_Statement;

    if CurrToken.ID = SP_EOF then
      Exit;
    if CurrToken.ID = SP_POINT then
      Exit;
    Match(';');

    Call_SCANNER;

    if CurrToken.ID = SP_EOF then
      Exit;
    if CurrToken.Text[1] = '.' then
      Exit;
    if IsCurrText('end') then
      Exit;
    if IsCurrText('except') then
      Exit;
    if IsCurrText('finally') then
      Exit;
  until false;
end;

procedure TPAXPascalParser.Parse_SimpleStatement;
var
  I, ID, LeftID, RightID, NP, Arg1, Arg2, Res, Vars, T: Integer;
  IsreducedAssignment: Boolean;
  CallRec: TPaxCallRec;
label
  Again;
begin
  if IsCurrText('inherited') then
  begin
    if CurrClassID = 0 then
      raise TPAXScriptFailure.Create(errStatementIsNotAllowedHere);
    if CurrSubID <> CurrMethodID then
      raise TPAXScriptFailure.Create(errStatementIsNotAllowedHere);
    if IsNextText(';') then
    begin
      ID := CurrMethodID;
      NP := Count[ID];
      Res := NewRef;
      Name[Res] := Name[ID];
      GenRef(CurrThisID, maMyBase, Res);
      for I:=1 to NP do
        Gen(OP_PUSH, SymbolTable.GetParamID(ID, I), 0, 0);
      if StrEql('Create', Name[ID]) then
        Gen(OP_CALL, Res, NP, CurrThisID)
      else
        Gen(OP_CALL, Res, NP, 0);

      CallRec := TPaxCallRec.Create;
      CallRec.CallP := Scanner.PosNumber;
      CallRec.CallN := Code.Card;
      TPaxBaseScripter(Scripter).CallRecList.AddObject(CallRec.CallN, CallRec);

      Call_SCANNER;
    end
    else
    begin
      FieldSwitch := true;
      Call_SCANNER;
      Gen(OP_NOP, 0, 0, 0);
      LeftID := Parse_Ident;
      GenRef(CurrThisID, maMyBase, LeftID);
      if IsCurrText('(') then
      begin
        Call_SCANNER;
        if not IsCurrText(')') then
          NP := Parse_ArgumentList(LeftID, Vars)
        else
        begin
          NP := 0;
          Vars := 0;

          CallRec := TPaxCallRec.Create;
          CallRec.CallP := Scanner.PosNumber;
          CallRec.CallN := Code.Card + 1;
          TPaxBaseScripter(Scripter).CallRecList.AddObject(CallRec.CallN, CallRec);
        end;
        Match(')');
        if StrEql('Create', Name[LeftID]) then
          Gen(OP_CALL, LeftID, NP, CurrThisID)
        else
          Gen(OP_CALL, LeftID, NP, 0);
        SetVars(Vars);

        Call_SCANNER;
      end
      else
      begin
        Match(';');
        if StrEql('Create', Name[LeftID]) then
          Gen(OP_CALL, LeftID, 0, CurrThisID)
        else
          Gen(OP_CALL, LeftID, 0, 0);

          CallRec := TPaxCallRec.Create;
          CallRec.CallP := Scanner.PosNumber;
          CallRec.CallN := Code.Card;
          TPaxBaseScripter(Scripter).CallRecList.AddObject(CallRec.CallN, CallRec);
      end;
    end;
  end
  else if IsCurrText('print') then
    Parse_PrintList
  else if IsCurrText('println') then
    Parse_PrintlnList
  else if IsCurrText('goto') then
    Parse_GoToStmt
  else if IsCurrText('delete') and (not IsNextText('(')) then
  begin
    Call_SCANNER;
    LeftID := Parse_Designator(0);
    Gen(OP_RELEASE, LeftID, 0, 0);
  end
  else if IsCurrText('abort') and (not IsNextText('(')) then
  begin
    LeftID := Parse_Ident;
    Gen(OP_CALL, LeftID, 0, 0);
    CallRec := TPaxCallRec.Create;
    CallRec.CallP := Scanner.PosNumber;
    CallRec.CallN := Code.Card;
    TPaxBaseScripter(Scripter).CallRecList.AddObject(CallRec.CallN, CallRec);
  end
  else
  begin
    IsreducedAssignment := IsCurrText('reduced');
    if IsreducedAssignment then
      Call_SCANNER;

    Res := 0;

    if IsCurrText('(') then
    begin
      Call_SCANNER;
      ID := Parse_Designator(0);
      Match('as');
      Call_SCANNER;
      T := Parse_Ident;
      Gen(OP_PUSH, ID, 0, 0);
      res := ID;
      Gen(OP_CALL, T, 1, res);

      CallRec := TPaxCallRec.Create;
      CallRec.CallP := Scanner.PosNumber;
      CallRec.CallN := Code.Card;
      TPaxBaseScripter(Scripter).CallRecList.AddObject(CallRec.CallN, CallRec);

      Match(')');
      Call_SCANNER;
    end;

Again:
    Gen(OP_NOP, 0, 0, 0);
    LeftID := Parse_Designator(Res);

⌨️ 快捷键说明

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