From mboxrd@z Thu Jan 1 00:00:00 1970 X-Spam-Checker-Version: SpamAssassin 3.4.5-pre1 (2020-06-20) on ip-172-31-74-118.ec2.internal X-Spam-Level: X-Spam-Status: No, score=0.7 required=3.0 tests=BAYES_20,FREEMAIL_FROM, FROM_STARTS_WITH_NUMS autolearn=no autolearn_force=no version=3.4.5-pre1 X-Received: by 2002:a37:444c:: with SMTP id r73mr9525212qka.24.1613564082457; Wed, 17 Feb 2021 04:14:42 -0800 (PST) X-Received: by 2002:a25:9902:: with SMTP id z2mr35234263ybn.339.1613564082191; Wed, 17 Feb 2021 04:14:42 -0800 (PST) Path: eternal-september.org!reader02.eternal-september.org!news.uzoreto.com!tr3.eu1.usenetexpress.com!feeder.usenetexpress.com!tr3.iad1.usenetexpress.com!border1.nntp.dca1.giganews.com!nntp.giganews.com!news-out.google.com!nntp.google.com!postnews.google.com!google-groups.googlegroups.com!not-for-mail Newsgroups: comp.lang.ada Date: Wed, 17 Feb 2021 04:14:42 -0800 (PST) In-Reply-To: <8dc52127-3627-4bc1-b4d5-31935c3a4a37n@googlegroups.com> Complaints-To: groups-abuse@google.com Injection-Info: google-groups.googlegroups.com; posting-host=85.240.211.137; posting-account=rhqvKAoAAABpikMmPHJSZh4400BboHwT NNTP-Posting-Host: 85.240.211.137 References: <0997fa9b-c607-4c61-a8cb-3d3dc3ce3904n@googlegroups.com> <8dc52127-3627-4bc1-b4d5-31935c3a4a37n@googlegroups.com> User-Agent: G2/1.0 MIME-Version: 1.0 Message-ID: <67a2f69e-669a-4b19-bba2-af025b8a440cn@googlegroups.com> Subject: Re: algorithm, two implementations that act the same, same type etc From: Mehdi Saada <00120260a@gmail.com> Injection-Date: Wed, 17 Feb 2021 12:14:42 +0000 Content-Type: text/plain; charset="UTF-8" Xref: reader02.eternal-september.org comp.lang.ada:61365 List-Id: >> 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;