这是我的问题here的后续行动。
我在程序中遇到了一个点,我觉得我无法继续使用当前的结构,所以我做了很多重写。Statement
类型不再是抽象的,每个子类型都创建Statement
自己Statement
的变量实例。我还从 Statements 包中删除了抽象execute
函数,因为编译器不喜欢这样(execute
无论如何,每个子类型仍然有自己的方法)。该execute
函数已更改为过程,因为必须修改传入的 Statement 类型。我将statementFactory
(以前的 createStatement)移动到Statement
包中。
这是我得到的错误:
statements-compoundstatements.adb:15:29: expected type "CompoundStatement" defined at statements-compoundstatements.ads:11
statements-compoundstatements.adb:15:29: found type "Statement'Class" defined at statements.ads:6
我是 Ada 的初学者,但我的直觉是,因为execute
程序在CompoundStatements
(它是 Statements 的“子类”)中,所以它永远无法看到execute
Statements 的另一个“子类”的方法。我能想到的唯一解决方案是将所有execute
调用过程的过程转储execute
到Statement
包中,但这似乎是不可取的。但这仍然不能解释为什么stmt.all
被用作类型Statement'Class
而不是在statementFactory
.
这是新代码:
package Statements is
type Statement is tagged private;
type Statement_Access is access all Statement'Class;
ParserException : Exception;
procedure createStatement(tokens : Vector; S : out Statement);
procedure statementFactory(S: in out Statement; stmt: out Statement_Access);
--.....A bunch of other procedures and functions.....
private
type Statement is tagged
record
tokens : Vector;
executedtokens : Vector;
end record;
end Statements;
procedure createStatement(tokens : Vector; S : out Statement) is
begin
S.tokens := tokens;
end createStatement;
procedure statementFactory(S: in out Statement; stmt: out Statement_Access) is
currenttoken : Unbounded_String;
C : CompoundStatement;
A : AssignmentStatement;
P : PrintStatement;
begin
currenttoken := getCurrentToken(S);
if currenttoken = "begin" then
createStatement(S.tokens, C);
stmt := new CompoundStatement;
stmt.all := Statement'Class(C);
elsif isVariable(To_String(currenttoken)) then
createStatement(S.tokens, A);
stmt := new AssignmentStatement;
stmt.all := Statement'Class(A);
elsif currenttoken = "print" then
createStatement(S.tokens, P);
stmt := new PrintStatement;
stmt.all := Statement'Class(P);
end statementFactory;
package body Statements.CompoundStatements is
procedure execute(skip: in Boolean; C: in out CompoundStatement; reset: out Integer) is
stmt: Statement_Access;
tokensexecuted: Integer;
currenttoken : Unbounded_String;
begin
match(C, "begin");
currenttoken := getCurrentToken(C);
while(currenttoken /= "end") loop
statementFactory(C, stmt);
execute(skip, stmt.all, tokensexecuted); //ERROR OCCURS HERE