2012-03-30 41 views
2

我有一个名为Statements的包,其中包含一个名为Statement的抽象类型和一个名为execute()的抽象函数。在另一个包中,我有一个类型为CompoundStatement的类型为Statement的实现execute()函数。我有一个名为createStatement()的函数。它的目的是评估Unbounded_String类型的标记并确定它包含的关键字。然后基于这个关键字,它会根据这个关键字生成一个访问类型。在Ada中使用访问类型实现抽象函数

到目前为止这么好。

但我不知道如何做是调用正确的执行方法。我现在只有一个关键字编码,因为它还没有工作。

对不起,如果我的描述听起来很复杂。

package Statements is 

    type Statement is abstract tagged private; 
    type Statement_Access is access all Statement'Class; 

    function execute(skip: in Boolean; T: in TokenHandler; S: in Statement) return Integer is abstract; 

private 
    type Statement is abstract tagged 
     record 
     tokens: Vector; 
     end record; 

end Statements; 

procedure createStatement(T : in TokenHandler; stmt: out Statement_Access) is 
     currenttoken : Unbounded_String; 
     C   : CompoundStatement; 

    begin 
     currenttoken := To_Unbounded_String(TokenHandlers.getCurrentToken(T)); 
     if currenttoken = "begin" then 
     createCompoundStatement(T, C); 
     stmt := new CompoundStatement; 
     stmt.all := Statement'Class(C); 
     end if; 
    end createStatement; 

    procedure createCompoundStatement(T : in TokenHandler; C: out CompoundStatement) is 
    begin 
     C.tokens := T.tokens; 
    end createCompoundStatement; 

function execute(skip: in Boolean; T: in TokenHandler; C: in CompoundStatement) return Integer is 
     TK: TokenHandler := T; 
     stmt: Statement_Access; 
     tokensexecuted: Integer; 
     currenttoken : Unbounded_String; 
    begin 
     TokenHandlers.match("begin", TK); 
     currenttoken := To_Unbounded_String(TokenHandlers.getCurrentToken(TK)); 
     while(currenttoken /= "end") loop 
     Put(To_String(currenttoken)); 
     createStatement(T, stmt); 
     tokensexecuted := execute(skip, TK, stmt); //ERROR OCCURS HERE 
     TokenHandlers.moveAhead(tokensexecuted, TK); 
     currenttoken := To_Unbounded_String(TokenHandlers.getCurrentToken(TK)); 
     end loop; 
     TokenHandlers.match("end", TK); 
     return TokenHandlers.resetTokens(TK);  
    end execute; 

我编译时出现此错误:

statements-statementhandlers.adb:35:28: no candidate interpretations match the actuals: 
statements-statementhandlers.adb:35:46: expected type "CompoundStatement" defined at statements-statementhandlers.ads:14 
statements-statementhandlers.adb:35:46: found type "Statement_Access" defined at statements.ads:6 
statements-statementhandlers.adb:35:46: ==> in call to "execute" at statements-statementhandlers.ads:10 
statements-statementhandlers.adb:35:46: ==> in call to "execute" at statements.ads:8 

回答

4

execute的第三个参数预计Statement一(儿童),但你给它的是a指向(孩子)Statement。你可能想

tokensexecuted := execute(skip, TK, stmt.all); 

作为一个风格的问题,顺便说一下,通常是最好的调度参数的第一个;然后你可以(在2005年ADA)说

tokensexecuted := stmt.execute(skip, TK); 
+0

参见[* 2.3前缀符号*](http://www.adaic.org/resources/add_content/standards/05rat/html/Rat-2- 3.html)。 – trashgod 2012-03-30 21:22:46

+0

太棒了!这工作。谢谢,威尔先生。 – 2012-03-30 22:29:06

+0

@deuteros - 在这种情况下,我建议你通过点击该答案旁边的复选标记轮廓,让每个人都知道(并顺带奖励赖特先生)。这将“接受”这个答案。 – 2012-04-03 16:12:23