summaryrefslogtreecommitdiff
path: root/src/tibvar.c
blob: f25061a5b478b85dde194b7626c4a901165df857 (plain)
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
/*
 *  libtib - Read, write, and evaluate TI BASIC programs
 *  Copyright (C) 2015 Delwink, LLC
 *
 *  This program is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU Affero General Public License as published by
 *  the Free Software Foundation, version 3 only.
 *
 *  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 Affero General Public License for more details.
 *
 *  You should have received a copy of the GNU Affero General Public License
 *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

#include <math.h>
#include <stdlib.h>

#include "tibchar.h"
#include "tiberr.h"
#include "tibvar.h"

struct varlist
{
  tib_Variable *vars;
  int len;
};

static struct varlist varlist =
  {
    .len = 0,
    .vars = NULL
  };

int
tib_var_init ()
{
  int rc;
  TIB *t;

#define ADD(K,V)                                \
  {                                             \
    t = (V);                                    \
    if (t)                                      \
      {                                         \
        rc = tib_var_set ((K), t);              \
        tib_decref (t);                         \
      }                                         \
    else                                        \
      {                                         \
        rc = tib_errno;                         \
      }                                         \
                                                \
    if (rc)                                     \
      goto end;                                 \
  }

  ADD ('e', tib_new_complex (2.718281828459045235360287471352662498, 0));
  ADD (TIB_CHAR_PI,
       tib_new_complex (3.141592653589793238462643383279502884, 0));

#undef ADD

 end:
  return rc;
}

void
tib_var_free ()
{
  for (int i = 0; i < varlist.len; ++i)
    tib_decref (varlist.vars[i].value);

  free (varlist.vars);

  varlist.len = 0;
  varlist.vars = NULL;
}

static int
add_var (int key, const TIB *value)
{
  tib_Variable *old = varlist.vars;

  ++varlist.len;
  varlist.vars = realloc (varlist.vars, varlist.len * sizeof (tib_Variable));
  if (NULL == varlist.vars)
    {
      varlist.vars = old;
      --varlist.len;
      return TIB_EALLOC;
    }

  tib_errno = 0;
  tib_Variable *new = varlist.vars + varlist.len - 1;
  new->key = key;
  new->value = tib_copy (value);
  if (tib_errno)
    --varlist.len;

  return tib_errno;
}

int
tib_var_set (int key, const TIB *value)
{
  if (!tib_is_var (key))
    return add_var (key, value);

  for (int i = 0; i < varlist.len; ++i)
    {
      if (key == varlist.vars[i].key)
        {
          TIB *old = varlist.vars[i].value;
          varlist.vars[i].value = tib_copy (value);
          if (tib_errno)
            {
              varlist.vars[i].value = old;
              return tib_errno;
            }

          tib_decref (old);
          return 0;
        }
    }

  return TIB_EINDEX; /* should be unreachable */
}

TIB *
tib_var_get (int key)
{
  for (int i = 0; i < varlist.len; ++i)
    if (key == varlist.vars[i].key)
      return tib_copy (varlist.vars[i].value);

  return tib_new_complex (0, 0);
}

bool
tib_is_var (int key)
{
  for (int i = 0; i < varlist.len; ++i)
    if (key == varlist.vars[i].key)
      return true;

  return false;
}