1 /* Copyright 2015-2021 Free Software Foundation, Inc.
2 
3    This program is free software: you can redistribute it and/or modify
4    it under the terms of the GNU General Public License as published by
5    the Free Software Foundation, either version 3 of the License, or
6    (at your option) any later version.
7 
8    This program is distributed in the hope that it will be useful,
9    but WITHOUT ANY WARRANTY; without even the implied warranty of
10    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
11    GNU General Public License for more details.
12 
13    You should have received a copy of the GNU General Public License
14    along with this program.  If not, see <http://www.gnu.org/licenses/>. */
15 
16 #include <config.h>
17 
18 #include "counter.h"
19 #include "parser.h"
20 
21 
22 void
counter_push(COUNTER * c,ELEMENT * elt,int num)23 counter_push (COUNTER *c, ELEMENT *elt, int num)
24 {
25   if (c->nvalues >= c->space - 1)
26     {
27       c->space += 5;
28       c->values = realloc (c->values, c->space * sizeof (int));
29       c->elts = realloc (c->elts, c->space * sizeof (ELEMENT *));
30       if (!c->values)
31         fatal ("could not realloc");
32     }
33   c->values[c->nvalues] = num;
34   c->elts[c->nvalues] = elt;
35 
36   c->nvalues++;
37   c->values[c->nvalues] = 0;
38   c->elts[c->nvalues] = 0;
39 }
40 
41 void
counter_pop(COUNTER * c)42 counter_pop (COUNTER *c)
43 {
44   if (!c->nvalues)
45     fatal ("could not realloc");
46 
47   c->nvalues--;
48   c->values[c->nvalues] = 0;
49   c->elts[c->nvalues] = 0;
50 }
51 
52 void
counter_inc(COUNTER * c)53 counter_inc (COUNTER *c)
54 {
55   c->values[c->nvalues - 1]++;
56 }
57 
58 void
counter_dec(COUNTER * c)59 counter_dec (COUNTER *c)
60 {
61   c->values[c->nvalues - 1]--;
62 }
63 
64 /* Return value of counter if the top counter is for element ELT, otherwise
65    return -1. */
66 int
counter_value(COUNTER * c,ELEMENT * elt)67 counter_value (COUNTER *c, ELEMENT *elt)
68 {
69   if (c->nvalues > 0 && c->elts[c->nvalues - 1] == elt)
70     return c->values[c->nvalues - 1];
71   else
72     return -1;
73 }
74