Commit c83ac287 authored by Maxime Perrotin's avatar Maxime Perrotin

Refactor iterators of signed/unsigned integers

parent 7e44d06e
......@@ -8,14 +8,17 @@ generic
type Sort is range <>;
with function Init return Sort;
with function Is_Constraint_Valid (Val : Sort) return Boolean;
Max : Sort;
Increment : Sort := 1;
-- Increment cannot be of type Sort, because the range of Sort may
-- not include the value of the intcrement
Increment : Integer_64 := 1;
package ASN1_Iterators.Generic_Integer is
-- Iterators for signed integers
function Elem_Init return Sort is (Init);
function Has_Element (Value : Sort) return Boolean is (Value <= Max);
function Has_Element (Value : Sort) return Boolean is (Value <= Sort'Last);
function First return Sort is (Init);
function Next (Value : Sort) return Sort;
function Next (Value : Sort) return Sort
with Pre => Integer_64 (Value) <= Integer_64 (Sort'Last) - Increment
or else (raise Stop_Iteration);
package Signed_Integer is new SimpleTypes
(Sort => Sort,
......
......@@ -19,15 +19,15 @@ package ASN1_Iterators.Generic_SeqOf is
package P is new Generic_Fixed_SeqOF (P => Basic);
-- Create an integer type with a range constraint to iterate on
function Init_Len return Integer_64 is (Integer_64 (Min));
function Is_Constraint_Valid (Val : Integer_64) return Boolean is
(Val >= Integer_64 (Min) and Val <= Integer_64 (Max));
subtype Len_Sort is Natural range Min .. Max;
function Init_Len return Len_Sort is (Min);
function Is_Constraint_Valid (Val : Len_Sort) return Boolean is
(Val >= Min and Val <= Max);
package Length_ty is new Generic_Integer (Sort => Integer_64,
package Length_ty is new Generic_Integer (Sort => Len_Sort,
Init => Init_Len,
Is_Constraint_Valid =>
Is_Constraint_Valid,
Max => Integer_64 (Max));
Is_Constraint_Valid);
-- Instantiate the package to iterate on the integer for the length
package Length_Pkg renames Length_ty.It;
......
with ASN1_Iterators.SimpleTypes,
with Interfaces,
ASN1_Iterators.SimpleTypes,
ASN1_Iterators.Generic_Basic;
with Interfaces;
use Interfaces;
generic
Min : Interfaces.Unsigned_64;
Max : Interfaces.Unsigned_64;
type Sort is mod <>;
with function Init return Sort;
with function Is_Constraint_Valid (Val : Sort) return Boolean;
-- Increment cannot be of type Sort, because the range of Sort may
-- not include the value of the intcrement
Increment : Unsigned_64 := 1;
package ASN1_Iterators.Generic_Unsigned_Integer is
function Elem_Init return Interfaces.Unsigned_64 is (Min);
function Has_Elem (Value : Interfaces.Unsigned_64) return Boolean is
(Value <= Max);
function Elem_First return Interfaces.Unsigned_64 is (Min);
function Elem_Next (Value : Interfaces.Unsigned_64)
return Interfaces.Unsigned_64 is (Value + 1);
function Elem_Init return Sort is (Init);
function Has_Element (Value : Sort) return Boolean is (Value <= Sort'Last);
function First return Sort is (Init);
function Next (Value : Sort) return Sort
with Pre => Unsigned_64 (Value) <= Unsigned_64 (Sort'Last) - Increment
or else (raise Stop_Iteration);
package Unsigned_Type is new SimpleTypes
(Sort => Interfaces.Unsigned_64,
(Sort => Sort,
Elem_Init => Elem_Init,
Has_Elem => Has_Elem,
Elem_First => Elem_First,
Elem_Next => Elem_Next);
Has_Elem => Has_Element,
Elem_First => First,
Elem_Next => Next);
package It is new Generic_Basic (P => Unsigned_Type);
......
......@@ -55,40 +55,33 @@ end ASN1_Ada_Iterators.Iterators;
'''
def unsigned_int_adb_template (sort: str, minR: int, maxR: int) -> str:
''' Template for iterating on an unsigned integer '''
return f'''package body {sort}_Pkg is
procedure To_ASN1 (From : Interfaces.Unsigned_64;
To : out asn1Scc{sort}) is
begin
To := From;
end To_ASN1;
function To_ASN1 (From : Interfaces.Unsigned_64) return asn1Scc{sort} is
Result : asn1Scc{sort};
begin
To_ASN1 (From, Result);
return Result;
end To_ASN1;
end {sort}_Pkg;'''
def signed_int_adb_template (sort: str, minR: int, maxR: int) -> str:
''' Template for iterating on a signed integer '''
return f'''package body {sort}_Pkg is
procedure To_ASN1 (From : Interfaces.Integer_64;
To : out asn1Scc{sort}) is
begin
To := From;
end To_ASN1;
function To_ASN1 (From : Interfaces.Integer_64) return asn1Scc{sort} is
Result : asn1Scc{sort};
begin
To_ASN1 (From, Result);
return Result;
end To_ASN1;
end {sort}_Pkg;'''
#def unsigned_int_adb_template (sort: str, minR: int, maxR: int) -> str:
# ''' Template for iterating on an unsigned integer '''
# return f'''package body {sort}_Pkg is
#-- procedure To_ASN1 (From : Interfaces.Unsigned_64;
#-- To : out asn1Scc{sort}) is
#-- begin
#-- To := From;
#-- end To_ASN1;
#
# function To_ASN1 (From : Interfaces.Unsigned_64) return asn1Scc{sort} is
# Result : asn1Scc{sort};
# begin
# To_ASN1 (From, Result);
# return Result;
# end To_ASN1;
#end {sort}_Pkg;'''
#def signed_int_adb_template (sort: str, minR: int, maxR: int) -> str:
# ''' Template for iterating on a signed integer '''
# return f'''package body {sort}_Pkg is
# procedure To_ASN1 (From : Interfaces.Integer_64;
# To : out asn1Scc{sort}) is
# begin
# To := From;
# end To_ASN1;
#end {sort}_Pkg;'''
def seq_of_adb_template (sort: str, inner: str, minR: int, maxR: int) -> str:
......@@ -444,12 +437,15 @@ end ASN1_Ada_Iterators.Iterators;
def unsigned_int_ads_template (sort: str, minR: int, maxR: int) -> str:
''' Template for iterating on an unsigned integer '''
return f'''package {sort}_Pkg is
package Inner is new Generic_Unsigned_Integer (Min => {minR},
Max => {maxR});
function Is_Constraint_Valid (Val : asn1Scc{sort}) return Boolean is
(asn1Scc{sort}_IsConstraintValid (Val).Success);
package Inner is
new Generic_Unsigned_Integer (Sort => asn1Scc{sort},
Init => asn1Scc{sort}_Init,
Is_Constraint_Valid =>
Is_Constraint_Valid);
package It renames Inner.It;
procedure To_ASN1 (From : Interfaces.Unsigned_64;
To : out asn1Scc{sort});
function To_ASN1 (From : Interfaces.Unsigned_64) return asn1Scc{sort};
function To_ASN1 (From : asn1Scc{sort}) return asn1Scc{sort} is (From);
subtype Instance is Inner.Instance;
function Image (Elm : asn1Scc{sort}) return String is
(Ada.Strings.Fixed.Trim (Elm'Img, Both));
......@@ -464,12 +460,9 @@ def signed_int_ads_template (sort: str, minR: int, maxR: int) -> str:
package Inner is new Generic_Integer (Sort => asn1Scc{sort},
Init => asn1Scc{sort}_Init,
Is_Constraint_Valid =>
Is_Constraint_Valid,
Max => {maxR});
Is_Constraint_Valid);
package It renames Inner.It;
procedure To_ASN1 (From : Interfaces.Integer_64;
To : out asn1Scc{sort});
function To_ASN1 (From : Interfaces.Integer_64) return asn1Scc{sort};
function To_ASN1 (From : asn1Scc{sort}) return asn1Scc{sort} is (From);
subtype Instance is Inner.Instance;
function Image (Elm : asn1Scc{sort}) return String is
(Ada.Strings.Fixed.Trim (Elm'Img, Both));
......@@ -724,19 +717,11 @@ def run(options):
unsigned_int_ads_template(name,
int(basic.Min),
int(basic.Max)))
packages_adb.append(
unsigned_int_adb_template(name,
int(basic.Min),
int(basic.Max)))
elif basic.kind == "IntegerType" and int(basic.Min) < 0:
packages_ads.append(
signed_int_ads_template(name,
int(basic.Min),
int(basic.Max)))
packages_adb.append(
signed_int_adb_template(name,
int(basic.Min),
int(basic.Max)))
elif basic.kind in ("EnumeratedType", "BooleanType"):
packages_ads.append(enum_ads_template(name))
if basic.kind == "EnumeratedType":
......
......@@ -40,7 +40,7 @@ Boolean ::= BOOLEAN
MyReal ::= REAL (0.2 .. 1.5 | 10.5 .. 14.5)
MyInt2 ::= INTEGER (-5 .. 0 | 10 | 20 .. 25)
MyInt2 ::= INTEGER (-5 .. 0 | 10 | 20 .. 25 | 27 | 29)
END
......@@ -33,9 +33,9 @@ procedure test_generic is
use MySeq3_Pkg;
use MySeq4_Pkg;
use MyChoice_Pkg;
use Boolean_Pkg;
use MyReal_Pkg;
use MyInt2_Pkg;
--use Boolean_Pkg;
--use MyReal_Pkg;
--use MyInt2_Pkg;
-- Test cases
MyIt : MyInteger_Pkg.Instance;
......@@ -117,7 +117,7 @@ begin
count := 0;
for each of Seq2 loop
count := count + 1;
Put (Image (Each) & " ");
Put (Image (each) & " ");
i := (if i mod 6 = 0 then 1 else i + 1);
if i = 1 then
New_Line;
......@@ -132,14 +132,14 @@ begin
count := 0;
for each of Seq3 loop
count := count + 1;
Put (Image (Each) & " ");
Put (Image (each) & " ");
i := (if i mod 6 = 0 then 1 else i + 1);
if i = 1 then
New_Line;
end if;
end loop;
New_Line;
Put_Line ("total: " & count'img & " values were computed");
Put_Line ("total: " & count'Img & " values were computed");
New_Line;
Put_Line ("Bigger Sequence");
......@@ -148,19 +148,19 @@ begin
for each of Seq4 loop
count := count + 1;
if count < 30 or count > 1500 then
Put (Image (Each) & " ");
i := (if i mod 3 = 0 then 1 else i + 1);
if i = 1 then
New_Line;
end if;
Put (Image (each) & " ");
i := (if i mod 3 = 0 then 1 else i + 1);
if i = 1 then
New_Line;
end if;
end if;
if count = 30 then
New_Line;
Put_Line ("...");
New_Line;
Put_Line ("...");
end if;
end loop;
New_Line;
Put_Line ("total: " & count'img & " values were computed");
Put_Line ("total: " & count'Img & " values were computed");
New_Line;
Put_Line ("CHOICE with two options");
......@@ -168,31 +168,31 @@ begin
count := 0;
for each of Choice loop
count := count + 1;
Put (Image (Each) & " ");
Put (Image (each) & " ");
i := (if i mod 6 = 0 then 1 else i + 1);
if i = 1 then
New_Line;
end if;
end loop;
New_Line;
Put_Line ("total: " & count'img & " values were computed");
Put_Line ("total: " & count'Img & " values were computed");
New_Line;
Put_Line ("BOOLEAN");
for each of Bool loop
Put (Each'Img & " ");
Put (each'Img & " ");
end loop;
New_Line;
New_Line;
Put_Line ("REAL WITH DISJOINT SET OF VALUES");
for each of Real loop
Put (Each'Img & " ");
Put (each'Img & " ");
end loop;
New_Line;
New_Line;
Put_Line ("INTEGER WITH DISJOINT SET OF VALUES");
for each of MyInt2 loop
Put (Each'Img & " ");
Put (each'Img & " ");
end loop;
New_Line;
......
......@@ -7,7 +7,7 @@ project test_generic is
package Compiler is
for Default_Switches ("Ada") use
("-g",
"-O2",
"-O0",
"-gnatf",
"-gnaty",
"-gnatwa",
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment