今天是个好日子,
我目前正在尝试使用ASIS工具进行ADA计划.我想知道是否可以直接修改编译期间生成的抽象语法树(.adt).如果是这样,有人能指出我如何实现这一目标的正确方向吗?
非常感谢您的宝贵时间.
当我使用以127结尾的范围定义我自己的类型时,编译器不会执行上限检查,这允许变量环绕并在其定义的限制之下变为负数.如果我将范围定义为126,则抛出适当的异常.我在下面列出了程序及其输出.
with Ada.Text_IO, Ada.Integer_Text_IO;
use Ada.Text_IO, Ada.Integer_Text_IO;
procedure GoodType is
type GOOD_TYPE is range -1..126;
package GOOD_TYPE_IO is new Ada.Text_IO.Integer_IO(GOOD_TYPE);
use GOOD_TYPE_IO;
On_Both1 : GOOD_TYPE := 120;
Index : INTEGER := 0;
begin
for Index in 120..130 loop
On_Both1 := On_Both1 + 1;
Put(Index);
Put(": ");
Put(On_Both1);
New_line;
end loop;
end GoodType;
Run Code Online (Sandbox Code Playgroud)
输出:
gnatmake -f goodtype.adb && ./goodtype
120: 121
121: 122
122: 123
123: 124
124: 125
125: 126
raised CONSTRAINT_ERROR : goodtype.adb:16 range check failed
Run Code Online (Sandbox Code Playgroud)
.
with Ada.Text_IO, …Run Code Online (Sandbox Code Playgroud) 从面向对象的范式思考,人们如何倾向于为标记记录实现私有属性?
从我可以看到的那一刻起,唯一的方法是拥有一个私有类型的属性.
例如
type car is tagged record
i_am_a_public_attribute : Integer;
i_am_another_public_attribute : Integer;
my_private_attributes : t_private_attributes;
end record;
Run Code Online (Sandbox Code Playgroud)
其中t_private_attributes在包的私有部分中声明.
我想到的第二种方法是使用继承,例如
type car_public is tagged record
i_am_a_public_attribute : Integer;
i_am_another_public_attribute : Integer;
end record;
type car_private is new car_public with record
my_private_attributes : Integer;
end record;
Run Code Online (Sandbox Code Playgroud)
其中car_private在包的私有部分中声明.虽然我觉得这个实现会非常混乱.
人们如何倾向于这样做?
谢谢马特
我无论如何都无法在GPS中编译(不运行)我的Ada代码.我收到一个错误:
cannot generate code for file random.ads (package spec)
gprbuild: *** compilation phase failed
Run Code Online (Sandbox Code Playgroud)
random.ads文件如下所示:
with Ada.Numerics.Float_Random;
use Ada.Numerics.Float_Random;
package random is
protected randomOut is
procedure Inicializal;
entry Parcel(
randomout: out Positive;
from: in Positive;
to: in Positive := 1
);
private
G: Generator;
Inicializalt: Boolean := False;
end randomOut;
task print is
entry write(what: in String);
end print;
end random;
Run Code Online (Sandbox Code Playgroud)
.gpr文件如下所示:
project Default is
package Compiler is
for Default_Switches ("ada") use ("-g", "-O2");
end Compiler;
for Main use ("hunting.adb"); …Run Code Online (Sandbox Code Playgroud) 为什么伏特、安培和欧姆兼容?
with ada.text_io; use ada.text_io;
procedure main is
type Volts is delta 1.0 / 2.0 ** 12 range -45_000.0 .. 45_000.0;
type Amps is delta 1.0 / 2.0 ** 16 range -1_000.0 .. 1_000.0;
type Ohms is delta 0.125 range 0.0 .. 1.0E8;
V : Volts := 1.0;
A : Amps := 1.0;
R1 : Ohms := 1.0;
R2 : Ohms := 1.0;
begin
v := A * (R1 + R2);
put_line(V'Img);
end main;
Run Code Online (Sandbox Code Playgroud)
如果类型被定义为new Float我在编译期间收到以下异常:
main.adb:22:12: error: …Run Code Online (Sandbox Code Playgroud)