在Ada中使用访问类型实现抽象功能 [英] Implementing an abstract function with access types in Ada

查看:96
本文介绍了在Ada中使用访问类型实现抽象功能的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个名为Statements的程序包,其抽象类型为Statement,抽象函数为execute().在另一个包中,我有一个CompositeStatement类型,它是一个Statement类型,它实现了execute()函数.

I have a package called Statements with an abstract type called Statement and an abstract function called execute(). In another package I have a type CompoundStatement which is a type Statement and it implements the execute() function.

我有一个名为createStatement()的函数.目的是评估类型为Unbounded_String的令牌并确定其包含的关键字.然后,基于此关键字,它将基于该关键字生成访问类型.

I have a function called createStatement(). It's purpose is to evaluate a token of type Unbounded_String and determine what keyword it contains. Then based on this keyword it will generate an access type based on this keyword.

到目前为止一切都很好.

So far so good.

但是我不知道怎么做是调用正确的execute方法.我现在只编码了一个关键字,因为它尚无法使用.

But what I can't figure out how to do is call the correct execute method. I only have one keyword coded in right now because it's not working yet.

对不起,如果我的描述听起来很混乱.

Sorry if my description sounds convoluted.

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;

我在编译时收到此错误:

I get this error when I compile:

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

推荐答案

execute的第三个参数应该是Statement的(子),但是您给它的是一个指针(Statement的子元素).你可能想要

The third parameter to execute is expected to be a (child of) Statement, but what you’ve given it is a pointer to a (child of) Statement. You probably want

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

顺便说一下,顺便说一句,通常最好将调度参数设为第一个;然后,您可以(在Ada 2005中)说

As a matter of style, by the way, it’s usually best to make the dispatching parameter the first; you could then (in Ada 2005) say

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

这篇关于在Ada中使用访问类型实现抽象功能的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆