calc: process(egal, temp_res, q_front) is
variable q_front_v: integer := q_front;
variable top_res_v: integer := top_res;
variable crt_el: std_logic_vector(17 downto 0) := (others => '0');
begin
if egal = '1' and finish = '0' then
if q_front_v = q_last - 1 then -- the queue is empty => we calculated the whole expression
finish <= '1';
else
if previous_element(17) = '1' then --if the previous element was an operator => we have a temporary result we have to add to the stack
top_res_v := top_res_v - 1;
res_stack(top_res_v) <= temp_res;
else --the previous element was a number => we don't have to push anything into the stack
null;
end if;
crt_el := queue(q_front_v); --the first element from the queue
q_front_v := q_front_v - 1; --dequeue
if crt_el(17) = '0' then --we took out an operand from the queue =>we push it into the stack
top_res_v := top_res_v - 1;
res_stack(top_res_v) <= crt_el(16 downto 0);
else --am scos un operator
oper_crt <= crt_el(4 downto 0);
-- the first two numbers are poped from the stack
op2 <= res_stack(top_res_v);
top_res_v := top_res_v + 1;
op1 <= res_stack(top_res_v);
top_res_v := top_res_v + 1;
end if;
end if;
q_front <= q_front_v;
top_res <= top_res_v;
previous_element <= crt_el;
elsif egal = '1' and finish = '1' then
final_result <= temp_res;
top_res_v := top_res - 1;
res_stack(top_res_v) <= temp_res;
end if;
end process;
calculate: ALU port map(op1, op2, oper_crt, temp_res);
afis: BCD_7seg port map(final_result(15 downto 0), clk, clr, a_to_g, an);
negative <= final_result(16);