From: Mehdi Saada <00120260a@gmail.com>
Subject: Re: algorithm, two implementations that act the same, same type etc
Date: Wed, 17 Feb 2021 04:14:42 -0800 (PST) [thread overview]
Message-ID: <67a2f69e-669a-4b19-bba2-af025b8a440cn@googlegroups.com> (raw)
In-Reply-To: <8dc52127-3627-4bc1-b4d5-31935c3a4a37n@googlegroups.com>
>> I'll refactor stuff and you'll tell if that complies with your standard.
I hope that's better.
------------------
body part of "p_expression_a.a_moi")
function Computation (V: in T_Vect_Token) return integer is separate;
function Calcul(V: in T_Vect_Token) return Integer renames Computation;
----------------------
separate (P_Expression.A_Moi)
function Computation ( V : in T_Vect_Token) return Integer is
package P_stack is new P_Pile_4(T_Elem => Integer,
Max_Pile => 80);
-- call on stack package
subtype T_stack is P_stack.T_Pile;
subtype T_Token_Operand is T_Token_Operande;
subtype T_Token_Operator is T_Token_Operateur;
procedure Stack_Uppon (A_Stack : in out T_stack; Elem: Integer)
renames P_stack.Empiler;
procedure Pop (A_Stack: in out T_STACK; Elem: out Integer)
renames P_stack.Depiler;
function Is_Stack_Empty (A_Stack: T_stack) return Boolean
renames P_stack.Pile_Vide;
Exception_Wrong_Token : exception renames P_Expression.Exc_Erreur;
-- renamings for conveniance, avoids usage of "use"
Stack_for_Operands : T_stack(40);
Operand_1, Operand_2: Integer;
begin
-- Loop runs through the token vector, if it's an operand it's stacked,
-- if it's an operator the last two operands stacked are recovered and the computation made,
-- but in the inverse order of the recovered operands, or "/" and "-" would misbehave.
for I in V'Range loop
-- Tag comparisons to choose decision
if V(I).all'Tag = T_Token_Operand'Tag then
Stack_Uppon(Stack_for_operands,T_Token_Operand(v(I).all).Get_Elem);
-- Get the value to stack
elsif V(I).all'Tag = T_Token_Operateur'Tag then
Pop(Stack_for_operands,Operand_2);
Pop(Stack_for_operands,Operand_1);
-- decision over which operator to apply.
case T_Token_Operateur(v(I).all).L_Operateur is
when '*' => Stack_Uppon(Stack_for_operands,Operand_1 * Operand_2);
when '-' => Stack_Uppon(Stack_for_operands,Operand_1 - Operand_2);
when '+' => Stack_Uppon(Stack_for_operands,Operand_1 + Operand_2);
when '/' => Stack_Uppon(Stack_for_operands,Operand_1 / Operand_2);
end case;
else raise P_Expression.Exc_Erreur
with "wrong token inserted please review previous steps";
-- if any wrong token (parentheses, terminator) has still crept in
-- from the previous steps. Or if the vector is invalid and it has not been seen,
end if;
end loop;
declare
Result: INTEGER renames Operand_1;
-- clarity rules !
begin
Pop(A_Stack => Stack_for_Operands,
Elem => Result);
pragma Assert (Is_Stack_Empty(Stack_for_operands)
and (P_Expression.Calcul(V) = Result));
return Result;
end;
exception
when P_stack.Exc_Pile_Vide => Put_Line("Invalidity not recognized by previous subroutine");
return 0;
when E: others => Put_Line(Exception_Information(E)); return 0;
end Computation;
next prev parent reply other threads:[~2021-02-17 12:14 UTC|newest]
Thread overview: 9+ messages / expand[flat|nested] mbox.gz Atom feed top
2021-02-14 19:35 algorithm, two implementations that act the same, same type etc Mehdi Saada
2021-02-15 11:07 ` AdaMagica
2021-02-15 13:02 ` Niklas Holsti
2021-02-15 16:34 ` Mehdi Saada
2021-02-15 19:15 ` Niklas Holsti
2021-02-17 10:50 ` Mehdi Saada
2021-02-17 12:14 ` Mehdi Saada [this message]
2021-02-17 17:10 ` Niklas Holsti
2021-02-17 21:13 ` Mehdi Saada
replies disabled
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox