How to fix: 'Argument of Type Conversion Must Be Single Expression' in Ada 95 - procedure

I am writing a simple program in Ada95 to check to see if a chess board layout input is valid or not. I am quite new to Ada and I understand I have a lot to learn.
The error messages that I keep getting are:
35:31: argument of type conversion must be single expression
47:07: argument of type conversion must be single expression
47:22: illegal operand for array conversion
53:10: argument of type conversion must be single expression
58:10: argument of type conversion must be single expression
61:13: argument of type conversion must be single expression
64:13: argument of type conversion must be single expression
66:13: argument of type conversion must be single expression
68:13: argument of type conversion must be single expression
76:15: invalid use of subtype mark in expression or call
I have included my full code below so that if there is anything that could be causing an issue in a declaration, it is available for reference.
-- RULES:
-- Only black squares may have pieces on them
-- Black squares start at the bottom left hand corner
-- Total checker count for a color cannot exceed 12
-- A color may not have more than 7 kings
with Ada.Text_IO, Ada.Integer_Text_IO;
use Ada.Text_IO, Ada.Integer_Text_IO;
procedure Checker_Checker is
type Checkers_Piece is ("b", "BK", "RK", "BC", "RC");
type Checkers_Board is array (1..8, 1..8) of Checkers_Piece;
Checkers_String: String(1..64);
invalid,
BK_Count, -- black king checker
RK_Count, -- red king checker
RC_Count, -- red checker
BC_Count, -- black checker
b_Count, -- blank space
Char_Count : Integer := 0;
procedure Print_Board (Object: in Checkers_Board) is
Print_Divider: String(1..17);
Print_Squares: String(1..17);
begin
Print_Divider := "-----------------";
Print_Squares := "| | | | | | | | |";
for X in 1..8 loop
Put_Line(Print_Divider);
for Y in 1..8 loop
Print_Squares(Y+1) := Checkers_Board(X, Y);
end loop;
Put_Line(Print_Squares);
end loop;
Put_Line(Print_Divider);
end Print_Board;
begin
Get(Checkers_String);
for I in 1..8 loop
for J in 1..8 loop
Char_Count := Char_Count + 1;
Checkers_Board(I, J) := Checkers_String(Char_Count);
end loop;
end loop;
for Y in 1..8 loop
for X in 1..8 loop
if Checkers_Board(Y, X) = 'b' then
Put_Line("There is a piece on a white space.");
invalid := invalid + 1;
end if;
if Checkers_Board(Y, X) = "BK" then
BK_Count := BK_Count + 1;
BC_Count := BC_Count + 1;
elsif Checkers_Board(Y, X) = "RK" then
RK_Count := RK_Count + 1;
RC_Count := RC_Count + 1;
elsif Checkers_Board(Y, X) = "RC" then
RC_Count := RC_Count + 1;
elsif Checkers_Board(Y, X) = "BC" then
BC_Count := BC_Count + 1;
elsif Checkers_Board(Y, X) = "b" then
b_Count := b_Count + 1;
else
Put_Line("There is an unidentified character on the board.");
end if;
end loop;
end loop;
Print_Board;
if RK_Count > 7 then
Put_Line("There are more than 7 Red Kings on the board.");
end if;
if BK_Count > 7 then
Put_Line("There are more than 7 Black Kings on the board.");
end if;
if RC_Count > 12 then
Put_Line("There are more than 12 Red Checkers on the board.");
end if;
if BC_Count > 12 then
Put_Line("There are more than 12 Black Checkers on the board.");
end if;
if b_Count < 32 then
Put_Line("There are too many checkers on the board.");
end if;
if invalid = 0 then
Put_Line("This is a valid checker board.");
else
Put_Line("This is an invalid checker board.");
end if;
end Checker_Checker;
I know my code is a mess.. please don't be too critical of it.. I am trying my best at learning Ada.
Thank you for your help.

For a start,
type Checkers_Piece is ("b", "BK", "RK", "BC", "RC");
is trying to declare an enumeration; but enumeration literals are identifiers, not strings.
So that should be
type Checkers_Piece is (b, BK, RK, BC, RC);
Then, in
Print_Squares := "| | | | | | | | |";
for X in 1..8 loop
Put_Line(Print_Divider);
for Y in 1..8 loop
Print_Squares(Y+1) := Checkers_Board(X, Y);
end loop;
Put_Line(Print_Squares);
end loop;
you were clearly expecting Checkers_Board(X, Y) to be a single Character, but in your declaration you intended it to be a sort of String, with up to 2 Characters.
You have to decide what representation you want that lets you distinguish a red king from a black king in a single character. Or, better, accept that you need 2 characters to represent your pieces. Or perhaps you could use single Characters with the convention that lower-case values are uncrowned, upper-case are crowned, space means unoccupied.
The reason for your 'argument of type conversion' errors is that, in lines like
Checkers_Board(I, J) := Checkers_String(Char_Count);
Checkers_Board is a type; you need an object of that type here, not the type itself. The syntax you’re using is for a type conversion (ARM 4.6).

Related

Inferring latches in Verilog/SystemVerilog

The statements in procedural blocks execute seqeuntially so why aren't any of the block1, block2 or block3 inferring a latch?
module testing(
input logic a, b, c,
output logic x, y, z, v
);
logic tmp_ref, tmp1, tmp2, tmp3;
//reference
always_comb begin: ref_block
tmp_ref = a & b;
x = tmp_ref ^ c;
end
always_comb begin: block1
y = tmp1 ^ c;
tmp1 = a & b;
end
always #(*) begin: block2
tmp2 <= a & b;
z = tmp2 ^ c;
end
always #(c) begin: block3
tmp3 = a & b;
v = tmp3 ^ c;
end
endmodule: testing
In block1 y is calculated using the blocking assignment before the new value of tmp1 is available.
In block2 tmp2 is calculated using a non-blocking assignment, which should postpone the assignment for when the always block finishes. Meanwhile, z is calculated using the blocking assignment and the new value of tmp2 is not yet available.
In block3 there is an incomplete sensitivity list and still no latch.
Here is the synthesis result from Quartus II 14.1:
Only when I add this block a latch is inferred:
//infers a latch
always #(*) begin: block4
if (c == 1'b1) begin
tmp4 = a & b;
w = tmp4 ^ c;
end
end
Can someone please explain why incomplete sensitivity list or using a variable before the value is updated does not infer a latch in a combinatorial block?
The type of assignment used in a combinatorial block will not effect synthesis. The use of non-blocking (<=) may result in RTL (pre-synthesis) to gates (post-synthesis) simulator mismatches.
The same is true for sensitivity lists, synthesis will give the behaviour of auto generated or complete list.
In a clocked process (#(posedge clk)) use non-blocking (<=) to get the simulation behaviour of a flip-flop. It is possible to use blocking (=) as well to have combinatorial code inside the clocked process but mixing styles is considered a bad coding practice. The combinatorial part code just be moved to a separate combinatorial block (always #*).
A latch is a basic memory element, if the circuit does not need memory then it will not be inferred.
For example:
always #* begin:
v = (a & b) ^ c;
end
v is completely defined by inputs, there is no memory involved. In comparison to :
always #* begin
if (c == 1'b1) begin
w = (a & b) ^ c;
end
end
When c is 0 w must hold its value, therefore a latch is inferred.
It is worth noting that while latches are not bad, care must be taken with the timing of when the open and close to ensure they capture the correct data. Therefore inferred latch are typically seen as bad and are from poor coding.
SystemVerilog has the following syntax for semantically implying design intent:
always_latch begin
if (c == 1'b1) begin
w = (a & b) ^ c;
end
end

Translate VHDL to Verilog

I have a problem with translating VHDL to Verilog.
It's part of my source code on VHDL.
With I/O I somehow understood, but have some problems to translate this string
ib1 <= std_logic_vector(to_unsigned(i,ib1'length));
to verilog?
COMPONENT GenerateModel
PORT(
ib1 : IN std_logic_vector(3 downto 0);
);
END COMPONENT;
--Inputs
signal ib1 : std_logic_vector(3 downto 0) := (others => '0');
BEGIN
uut: GenerateModel PORT MAP (
ib1 => ib1,
);
process
begin
for i in 0 to 15 loop
ib1 <= std_logic_vector(to_unsigned(i,ib1'length));
wait for 10 ns;
end loop;
end process;
end;
To extend into Verilog from Paebbels' comment, the line you are looking at does an explicit conversion from the type of the loop variable i to the port variable ib1. In Verilog, that explicit conversion is not needed, you can just assign the port variable directly. So, for example (in Verilog IEEE 1364-1995 compatible):
integer i;
...
for (i = 0; i < 16; i = i + 1) begin
ib1 = i; // <-- The line
#10; // -- Assume 1 step is 1 ns, can specific timescale if needed
end
If you want, you can even loop through the variable directly if its of type reg (ie, not a net):
for (ib1 = 0; ib1 < 15; ib1 = ib1 + 1) begin
#10;
end
#10;
[Note that as Greg mentioned, you need to be sure you dont create an infinite loop as if ib1 is 4-bits wide, it will always be less than 16, thus I fixed the example above to loop until ib1 is 15 (4'b1111)]

How do I use lhs and rhs to define a function?

In the Maxima session below, how come f(1) is not 0?
(%i1) eq: 2 * x + 1 = 3;
(%o1) 2 x + 1 = 3
(%i2) f(x) := lhs(eq) - rhs(eq);
(%o2) f(x) := lhs(eq) - rhs(eq)
(%i3) f(1);
(%o3) 2 x - 2
the process of function calling in maxima here binds x to 1 in the function
definition, lhs(eq)-rhs(eq). That has no x in it, so that binding does nothing.
Next, lhs(eq) is evaluated to 2*x+1. rhs(eq) is evaluated to 3. etc.
Do you always want the same equation eq? perhaps you want to do
define(f(x),lhs(eq)-rhs(eq));
to check what the definition is, try
grind(f);
If you want to vary the equation maybe something like
g(val, eq) := subst(val,x, lhs(eq)-rhs(eq)) ;
would do.

How to split a string using an integer array?

I am trying to split a string using an integer array as mask.
The task is simple but I am not accustomed to ADA (which is a constraint).
Here is my code. It works exept that I have an one character offset when testing against a file. Can someone help me remove this offset. it is drinving me nuts.
generic_functions.adb :
package body Generic_Functions is
-- | Sums up the elements of an array of Integers
function Sum_Arr_Int(Arr_To_Sum: Int_Array) return Integer is
Sum: Integer;
begin
Sum := 0;
for I in Arr_To_Sum'Range loop
Sum := Sum + Arr_To_Sum(I);
end loop;
return Sum;
end Sum_Arr_Int;
-- | Split up a String into a array of Unbounded_String following pattern from an Int_Array
function Take_Enregistrements(Decoup_Tab: Int_Array; Str_To_Read: String) return Str_Array is
Previous, Next : Integer;
Arr_To_Return : Str_Array(Decoup_Tab'Range);
begin
if Sum_Arr_Int(Decoup_Tab) > Str_To_Read'Length then
raise Constraint_Error;
else
Previous := Decoup_Tab'First;
Next := Decoup_Tab(Decoup_Tab'First);
for I in Decoup_Tab'Range loop
if I /= Decoup_Tab'First then
Previous := Next + 1;
Next := (Previous - 1) + Decoup_Tab(I);
end if;
Arr_To_Return(I) := To_Unbounded_String(Str_To_Read(Previous..Next));
end loop;
return Arr_To_Return;
end if;
end Take_Enregistrements;
end Generic_Functions;
generic_functions.ads :
with Ada.Strings.Unbounded; use Ada.Strings.Unbounded;
package Generic_Functions is
-- | Types
type Int_Array is array(Positive range <>) of Integer;
type Str_Array is array(Positive range <>) of Unbounded_String;
-- | end of Types
-- | Functions
function Sum_Arr_Int(Arr_To_Sum: Int_Array) return Integer;
function Take_Enregistrements(Decoup_Tab: Int_Array; Str_To_Read: String) return Str_Array;
-- | end of Functions
end Generic_Functions;
generic_functions_tests.adb :
with Ada.Text_IO; use Ada.Text_IO;
with Ada.Strings.Unbounded; use Ada.Strings.Unbounded;
with Generic_Functions; use Generic_Functions;
procedure Generic_Functions_Tests is
-- | Variables
Decoup_Test : constant Int_Array(1..8) := (11, 19, 60, 24, 255, 10, 50, 255);
Test_Str_Arr : Str_Array(Decoup_Test'Range);
Test_Str_Arr2 : Str_Array(Decoup_Test'Range);
Test_Str_Arr3 : Str_Array(Decoup_Test'Range);
--Test_Int : Integer;
Test_Handle : File_Type;
-- | end of Variables
begin
Open(Test_Handle, In_File, "EXPORTFINAL.DAT");
Test_Str_Arr := Take_Enregistrements(Decoup_Test, Get_Line(Test_Handle));
Test_Str_Arr2 := Take_Enregistrements(Decoup_Test, Get_Line(Test_Handle));
Test_Str_Arr3 := Take_Enregistrements(Decoup_Test, Get_Line(Test_Handle));
for I in Test_Str_Arr'Range loop
Put_Line(To_String(Test_Str_Arr(I)));
end loop;
for I in Test_Str_Arr2'Range loop
Put_Line(To_String(Test_Str_Arr2(I)));
end loop;
for I in Test_Str_Arr3'Range loop
Put_Line(To_String(Test_Str_Arr3(I)));
end loop;
-- for I in Test_Str_Arr'Range loop
-- Test_Int := To_String(Test_Str_Arr(I))'Length;
-- Put_Line(Integer'Image(Test_Int));
-- end loop;
-- for I in Test_Str_Arr2'Range loop
-- Test_Int := To_String(Test_Str_Arr2(I))'Length;
-- Put_Line(Integer'Image(Test_Int));
-- end loop;
-- for I in Test_Str_Arr3'Range loop
-- Test_Int := To_String(Test_Str_Arr3(I))'Length;
-- Put_Line(Integer'Image(Test_Int));
-- end loop;
Close(Test_Handle);
end Generic_Functions_Tests;
and finaly the file:
000000000012012-01-01 10:00:00 IBM IBM COMPAGNIE IBM FRANCE 17 AVENUE DE l'EUROPE 92275 BOIS-COLOMBES CEDEX CONFIGURATION COMPLETE SERVEUR000000000000000000000019 .6000000000001000000000000000000001000.00000000000000000000000000000196.00000000000000000000000000001196.00000000
000000000022012-01-01 11:00:00 MICROSOFT MSC 39 QUAI DU PRESIDENT ROOSEVELT 92130 ISSY-LES-MOULINEAUX AMENAGEMENT SALLE INFORMATIQUE000000000000000000000019.6000000000001000000000000000000001000.00000000000000000000000000000196.00000000000000000000000000001196.00000000
000000000032012-01-01 12:00:00 MICROSOFT MSC 39 QUAI DU PRESIDENT ROOSEVELT 92130 ISSY-LES-MOULINEAUX TESTS SUR SITE000000000000000000000019.6000000000001000000000000000000003226.52000000000000000000000000000632.39792000000000000000000000003858.91792000 DELEGATION TECHNICIEN HARD000000000000000000000019.60000000000000000000000000000001.00000000000000000000000000001000.00000000000000000000000000000196.00000000000000000000000000001196.00000000
These lines:
if I = Decoup_Tab'Last then
Arr_To_Return(I) := To_Unbounded_String(Str_To_Read(Previous..Next));
end if;
will overwrite the last element in your array.
Also, are you sure that the line number (00000000001, 00000000002, etc) is one of the strings you want to split based on the integer mask? As your code is right now, you use '11' twice, once for the line number and once for the date-field. If you skip the line number, the other numbers seem to make more sense.

How do I Save[] definitions associated to a symbol in Mathematica without saving subsidiary definitions?

The built-in Mathematica command Save[file, symbol] uses FullDefinition[] to look up the definition symbol and all of the subsidiary definitions.
For example, the commands
a:=b
c:=2a+b
Save[ToFileName[NotebookDirectory[],"test.dat"],c]
produces the file test.dat containing
c := 2*a + b
a := b
I have a program with a lot of prettifying MakeBoxes type definitions that I do not want to be saved when I Save[] the many separate results.
In terms of the simple example above, I do not want the a := b definition saved to the file. Does anyone know a neat way to make this happen?
According to the documentation, Save uses FullDefinition while what you want is for it to use Definition. Using a Block we can override the global definition of any symbol, and in particular replace FullDefinition with Definition while running Save:
Block[{FullDefinition},
FullDefinition = Definition;
Save[filename, c]
];
FilePrint[filename]
DeleteFile[filename]
The magic works:
c := 2*a + b
EDIT. Wrapping things up with the right attributes:
SetAttributes[truncatedSave, HoldRest]
truncatedSave[filename_, args__] := Block[{FullDefinition},
FullDefinition = Definition;
Save[filename, args]];
I think
DumpSave["test1", c]
Does that.
Sample code:
a := b;
c := 2 a + b;
DumpSave["test1", c];
Clear[a, c];
<< test1
?a
?c
Out
_____________________
Global`a
_____________________
Global`c
c:=2 a+b
Warning - Warning - I don't know what I am doing
Just found this browsing the help system randomly.
Never before used RunThrough ... anyway seems to do what you want.
Clear["Global`*"];
a := b;
c := 2 a + b;
mathcommand = StringReplace[First[$CommandLine], "MathKernel" -> "math"];
outputfile = "c:\\rtout";
RunThrough[mathcommand <> " -noprompt", Unevaluated[Put[Definition[c], "c:\\rtout"]]]
FilePrint[outputfile]
Clear[a, c];
<< "c:\\rtout"
DeleteFile[outputfile]
?c
Out
c := 2*a + b
_______________________________
Global`c
c:=2 a+b
Edit.. Works on lists with a little Hold-Fu
Clear["Global`*"];
(*Trick here *)
f[l_] := Definition ## HoldPattern /# Unevaluated#l;
SetAttributes[f, HoldFirst];
a := b;
c := 2 a + b;
d := 3 a + b;
mathcommand = StringReplace[First[$CommandLine], "MathKernel" -> "math"];
outputfile = "c:\\rtout";
RunThrough[mathcommand <> " -noprompt",Unevaluated[Put[Evaluate[f#{c, d}], "c:\\rtout"]]]
(* test *)
FilePrint[outputfile]
Clear[a, c, d];
<< "c:\\rtout"
DeleteFile[outputfile]
?c
?d

Resources