aboutsummaryrefslogtreecommitdiffstats
path: root/src/mds-kbdc/variables.c
blob: b53569af34b92a77f61b846000d4e2e086332552 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
/**
 * mds — A micro-display server
 * Copyright © 2014  Mattias Andrée (maandree@member.fsf.org)
 * 
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 * 
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */
#include "variables.h"

#include <stdlib.h>
#include <string.h>



/**
 * The state of a variable
 */
typedef struct variable
{
  /**
   * The current value of the variable
   */
  mds_kbdc_tree_t* value;
  
  /**
   * The previous version of variable,
   * before it was shadowed
   */
  struct variable* restrict previous;
  
  /**
   * The original scope the current shadow
   * of the variable was created in
   */
  size_t scope;
  
} variable_t;



/**
 * Map (by index) of defined variables
 */
static variable_t** restrict variables = NULL;

/**
 * The size of `variables`
 */
static size_t variable_count = 0;

/**
 * The current scope, the number of
 * times the variable-stakc has been
 * pushed without being popped
 */
static size_t current_scope = 0;



/**
 * Destroy the variable storage
 */
void variables_terminate(void)
{
  size_t i;
  variable_t* old;
  for (i = 0; i < variable_count; i++)
    while (variables[i])
      {
	old = variables[i];
	variables[i] = variables[i]->previous;
	mds_kbdc_tree_free(old->value);
	free(old);
      }
  free(variables), variables = NULL;
  variable_count = current_scope = 0;
}


/**
 * Push the variable-stack, making it
 * possible to shadow all variables
 */
void variables_stack_push(void)
{
  current_scope++;
}


/**
 * Undo the actions of `variables_stack_push`
 * and all additions to the variable storage
 * since it was last called (without a
 * corresponding call to this function)
 */
void variables_stack_pop(void)
{
  size_t i;
  variable_t* old;
  for (i = 0; i < variable_count; i++)
    if (variables[i] && (variables[i]->scope == current_scope))
      {
	old = variables[i];
	variables[i] = variables[i]->previous;
	mds_kbdc_tree_free(old->value);
	free(old);
      }
  current_scope--;
}


/**
 * Check whether a let will override a variable
 * rather the define or shadow it
 * 
 * @param   variable  The variable index
 * @return            Whether a let will override the variable
 */
int variables_let_will_override(size_t variable)
{
  if (variable >= variable_count)   return 0;
  if (variables[variable] == NULL)  return 0;
  return variables[variable]->scope == current_scope;
}


/**
 * Assign a value to a variable, and define or shadow it in the process
 * 
 * @param   variable  The variable index
 * @param   value     The variable's new value
 * @return            Zero on success, -1 on error
 */
int variables_let(size_t variable, mds_kbdc_tree_t* restrict value)
{
  variable_t** new;
  variable_t* previous;
  
  /* Grow the table if necessary to fit the variable. */
  if (variable >= variable_count)
    {
      new = realloc(variables, (variable + 1) * sizeof(variable_t*));
      if (new == NULL)
	return -1;
      variables = new;
      memset(variables, 0, (variable + 1 - variable_count) * sizeof(variable_t*));
      variable_count = variable + 1;
    }
  
  if (variables_let_will_override(variable))
    {
      /* Override. */
      mds_kbdc_tree_free(variables[variable]->value);
      variables[variable]->value = value;
    }
  else
    {
      /* Shadow or define. */
      previous = variables[variable];
      variables[variable] = malloc(sizeof(variable_t));
      if (variables[variable] == NULL)
	return variables[variable] = previous, -1;
      variables[variable]->value = value;
      variables[variable]->previous = previous;
      variables[variable]->scope = current_scope;
    }
  
  return 0;
}


/**
 * Get the value currently assigned to a variable
 * 
 * The function cannot fail, `NULL` is however returned
 * if the variable is not defined
 * 
 * @param   variable  The variable index
 * @return            The variable's value, `NULL` if not defined
 */
mds_kbdc_tree_t* variables_get(size_t variable)
{
  if (variable >= variable_count)   return NULL;
  if (variables[variable] == NULL)  return NULL;
  return variables[variable]->value;
}