pax_c.pas
来自「Delphi脚本控件」· PAS 代码 · 共 2,854 行 · 第 1/5 页
PAS
2,854 行
end;
function TPAXCParser.Parse_ClassStmt(ClassML: TPAXModifierList; ck: TPAXClassKind): Integer;
var
ClassID, AncestorClassID: Integer;
procedure Parse_PropertyStmt(ml: TPAXModifierList);
var
PropertyRec: TPAXMemberRec;
PropID, SubID, ResID, ParamID, ParamTypeID, ValueID, I, ID, T: Integer;
ParamIds: TPaxIds;
begin
// match "property"
DeclareSwitch := true;
Call_SCANNER;
T := typeVARIANT;
if NextToken.TokenClass = tcID then
begin
if NewID then
SymbolTable.DecCard
else
T := CurrToken.ID;
Call_SCANNER;
end;
PropID := CurrToken.ID;
DeclareSwitch := false;
TypeID[PropID] := T;
PropertyRec := CurrClassRec.AddProperty(Parse_Ident, ml);
Kind[PropID] := KindPROP;
if Name[PropID] = 'this' then
PropertyRec.ml := PropertyRec.ml + [modDEFAULT];
ParamIds := TPaxIds.Create(false);
if IsCurrText('[') then
begin
DeclareSwitch := true;
repeat
Call_SCANNER;
ParamTypeID := typeVARIANT;
if NextToken.TokenClass = tcID then
begin
if NewID then
SymbolTable.DecCard
else
ParamTypeID := CurrToken.ID;
Call_SCANNER;
end;
if IsCurrText('ref') or IsCurrText('out') then
ParamID := Parse_ByRef
else
ParamID := Parse_Ident;
ParamIds.Add(ParamID);
TypeID[ParamID] := ParamTypeID;
if not IsCurrText(',') then
Break;
until false;
DeclareSwitch := false;
Match(']');
Call_SCANNER;
end;
Match('{');
Call_SCANNER;
while IsCurrText('get') or
IsCurrText('set')
do
begin
if IsCurrText('get') then
begin
if PropertyRec.ReadID <> 0 then
Match('}');
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[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] := 'this';
TypeID[CurrThisID] := CurrClassID;
Gen(OP_BEGIN_WITH, WithStack.Push(CurrThisID), 0, 0);
Call_SCANNER;
Match('{');
Parse_Block;
Gen(OP_END_WITH, 0, 0, 0);
WithStack.Pop;
Gen(OP_RET, 0, 0, 0);
LevelStack.Pop;
LinkVariables(SubID, false);
Call_SCANNER;
end
else if IsCurrText('set') then
begin
if PropertyRec.WriteID <> 0 then
Match('}');
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[ParamIds[I]];
end;
ValueID := NewVar;
Kind[SubID] := KindSUB;
Count[SubID] := 1 + 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] := 'this';
TypeID[CurrThisID] := CurrClassID;
Gen(OP_BEGIN_WITH, WithStack.Push(CurrThisID), 0, 0);
Name[ValueID] := 'value';
Call_SCANNER;
Match('{');
Parse_Block;
Gen(OP_END_WITH, 0, 0, 0);
WithStack.Pop;
Gen(OP_RET, 0, 0, 0);
LevelStack.Pop;
LinkVariables(SubID, false);
Call_SCANNER;
end
end;
if PropertyRec.ReadID + PropertyRec.WriteID = 0 then
Match('get');
Call_SCANNER;
ParamIds.Free;
end;
var
L: Integer;
ml: TPAXModifierList;
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;
if IsCurrText(':') 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));
end
else
AncestorClassID := 0;
LevelStack.PushClass(ClassID, AncestorClassID, ClassML, ck, false);
Match('{');
Call_SCANNER;
repeat
ml := Parse_ModifierList;
if IsCurrText('function') then
Parse_FunctionStmt(ml)
else if IsCurrText('property') then
Parse_PropertyStmt(ml)
else if IsCurrText('var') then
begin
Parse_VarStmt(true, ml);
Match(';');
Call_SCANNER;
end
else if IsCurrText('const') then
begin
Parse_VarStmt(true, ml);
Match(';');
Call_SCANNER;
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 (CurrToken.TokenClass = tcID) and
((NextToken.TokenClass = tcId) or (NextToken.Text = '~') or
(NextToken.Text = 'operator')) then
begin
if IsNext2Text('(') or (NextToken.Text = '~') then
Parse_FunctionStmt(ml, CurrToken.ID)
else if NextToken.Text = 'operator' then
Parse_FunctionStmt(ml, CurrToken.ID)
else
begin
Parse_VarStmt(true, ml, CurrToken.ID);
Match(';');
Call_SCANNER;
end;
end
else if (CurrToken.TokenClass = tcID) and IsNextText('[') and
(IsNext2Text(']') or IsNext2Text(',')) then
begin
Parse_VarStmt(true, ml, CurrToken.ID);
Match(';');
Call_SCANNER;
end
else
Match('function');
if CurrToken.ID = SP_EOF then
Break;
if IsCurrText('}') then
Break;
until false;
LevelStack.Pop;
SetLabelHere(L);
Call_SCANNER;
end;
function TPAXCParser.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);
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('{');
Call_SCANNER;
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_AssignmentExpression, ValueID);
Gen(OP_ASSIGN, ID, ValueID, ID);
Gen(OP_PLUS, ValueID, NewConst(1), ValueID);
Gen(OP_HALT, 0, 0, 0);
if IsCurrText('}') then
Break;
Match(',');
Call_SCANNER;
end
else if IsCurrText(',') or 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);
if IsCurrText('}') then
Break;
Call_SCANNER;
end
else
Break;
if CurrToken.ID = SP_EOF then
Break;
until false;
Match('}');
LevelStack.Pop;
SetLabelHere(L);
Call_SCANNER;
end;
procedure TPaxCParser.Parse_SwitchStmt;
procedure Match(const S: String);
begin
Self.Match(S);
Call_SCANNER;
end;
var
lg, l_default, bool_id, expr_id, case_expr_id, lf, l_skip, I, N1, N2: Integer;
lt: TPaxStack;
n_skips: TPaxIds;
begin
lg := NewLabel();
l_default := NewLabel();
lt := TPaxStack.Create;
n_skips := TPaxIds.Create(true);
bool_id := NewVar();
Gen(OP_ASSIGN, bool_id, symboltable.IDTRUE, bool_id);
EntryStack.Push(lg, 0, StatementLabel);
Match('switch');
Match('(');
expr_id := Parse_Expression();
Match(')');
Match('{'); // parse switch block
repeat // parse switch sections
repeat // parse switch labels
if (IsCurrText('case')) then
begin
Match('case');
lt.Push(NewLabel());
case_expr_id := Parse_Expression();
Gen(OP_EQ, expr_id, case_expr_id, bool_id);
Gen(OP_GO_TRUE, lt.Top(), bool_id, 0);
end
else if (IsCurrText('default')) then
begin
Match('default');
SetLabelHere(l_default);
Gen(OP_ASSIGN, bool_id, symboltable.IDTRUE, bool_id);
end
else
break; //switch labels
Match(':');
until false;
while (lt.Card > 0) do
SetLabelHere(lt.Pop());
lf := NewLabel();
Gen(OP_GO_FALSE, lf, bool_id, 0);
// parse statement list
repeat
if (IsCurrText('case')) then
break;
if (IsCurrText('default')) then
break;
if (IsCurrText('}')) then
break;
l_skip := NewLabel();
SetLabelHere(l_skip);
Parse_Statement;
Gen(OP_GO, l_skip, 0, 0);
n_skips.Add(Code.Card);
until false;
SetLabelHere(lf);
if (IsCurrText('}')) then
break;
until false;
EntryStack.Pop();
SetLabelHere(lg);
Match('}');
if n_skips.Count >= 2 then
for I:=0 to n_skips.Count - 2 do
begin
N1 := n_skips[I];
N2 := n_skips[I+1];
Code.Prog[N1].Arg1 := Code.Prog[N2].Arg1;
end;
N1 := n_skips[n_skips.Count - 1];
Code.Prog[N1].Op := OP_NOP;
lt.Free;
n_skips.Free;
end;
procedure TPAXCParser.Parse_WithStmt;
var
ID, ID2: Integer;
begin
// match "with"
Call_SCANNER;
Match('(');
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);
Match(')');
Call_SCANNER;
Parse_Statement;
Gen(OP_END_WITH, 0, 0, 0);
WithStack.Pop;
end;
procedure TPAXCParser.Parse_ThrowStmt;
var
ID: Integer;
begin
// match "throw"
Call_SCANNER;
if IsCurrText(';') then
begin
ID := 0;
Call_SCANNER;
end
else
begin
ID := Parse_Expression;
end;
Gen(OP_THROW, ID, 0, 0);
end;
procedure TPAXCParser.Parse_TryStmt;
var
L, LTRY, ID: Integer;
begin
// match "try"
LTRY := NewLabel;
Gen(OP_TRY_ON, LTRY, 0, 0);
Call_SCANNER;
Parse_Block;
Call_SCANNER;
L := NewLabel;
Gen(OP_GO, L, 0, 0);
if not IsCurrText('catch') or IsCurrText('finally') then
Match('catch');
while IsCurrText('catch') do
begin
Call_SCANNER;
Match('(');
DeclareSwitch := true;
Call_SCANNER;
ID := Parse_Ident;
Gen(OP_CATCH, ID, 0, 0);
DeclareSwitch := false;
Match(')');
CurrClassRec.AddField(ID, [modSTATIC]);
Call_SCANNER;
Parse_Block;
Call_SCANNER;
Gen(OP_DISCARD_ERROR, 0,
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?