1

Is there any way to generate functions at compile-time in Ada? I want to generate an opcode table that stores function pointers. My current solution is to store objects containing a procedure that can be executed, but I'd rather not allocate if possible.

Arcterus
  • 13
  • 2
  • Maybe you should show us a short, but representative example of what you are doing at the moment. – Jacob Sparre Andersen Jul 27 '18 at 19:19
  • Are you sure you mean to *generate* functions at compile-time? – Jacob Sparre Andersen Jul 27 '18 at 19:21
  • I’m away from my computer right now, but I may post something when I get back. Essentially I have a bunch of functions that are generally very similar that I don’t want to maintain separately (for example, a bunch of LD instructions). In Rust, I would use a macro here. – Arcterus Jul 27 '18 at 23:22

2 Answers2

3

No.

But there's nothing wrong with having a domain-specific language (DSL) from which you generate Ada. That happens regularly.

Jacob Sparre Andersen
  • 6,733
  • 17
  • 22
  • I had a feeling that was the case. I’ll guess I’ll just write a script to generate them (or use some preprocessor). – Arcterus Jul 27 '18 at 23:29
3

You can use a generic procedure to have a generic body and just supply whatever unique parameters you need to it

with Ada.Text_IO; use Ada.Text_IO;

procedure Hello is
    generic
        -- some sort of parameters
        Value : Integer;
    procedure Do_Op;

    procedure Do_Op is
    begin
        Put_Line(Integer'Image(Value));
    end Do_Op;

    procedure Op1 is new Do_Op(1);
    procedure Op2 is new Do_Op(2);
    procedure Op3 is new Do_Op(3);


begin
  Put_Line("Hello, world!");
  Op1;
  Op2;
  Op3;
end Hello;
Jere
  • 3,124
  • 7
  • 15