summaryrefslogtreecommitdiffhomepage
path: root/mixlib/mix_eval.c
blob: b6298523aeef5b20b77328b9e42543414592ff8f (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
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
/* -*-c-*- -------------- mix_eval.c :
 * Implementation of the functions declared in mix_eval.h
 * ------------------------------------------------------------------
 *  Last change: Time-stamp: "01/02/20 00:23:58 jose"
 * ------------------------------------------------------------------
 * Copyright (C) 2000 Free Software Foundation, Inc.
 *  
 * 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 2 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, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
 *  
 */


#include "xmix_eval.h"


static const gchar *errors_[] = {
  N_("Successful evaluation"),
  N_("Syntax error in expression"),
  N_("Out of range F-specification"),
  N_("Mismatched parenthesis"),
  N_("Undefined symbol"),
  N_("Internal error")
};


/* create a new evaluator */
mix_eval_t *
mix_eval_new (void) 
{
  mix_eval_t *result = g_new (mix_eval_t, 1);
  result->table = mix_symbol_table_new ();
  if (result->table == NULL) {
    g_free (result);
    return NULL;
  }
  result->towner = TRUE;
  result->value = MIX_WORD_ZERO;
  result->errpos = 0;
  return result;
}

/* create a new evaluator with an external symbol table */
mix_eval_t *
mix_eval_new_with_table (mix_symbol_table_t *table)
{
  mix_eval_t *result = g_new (mix_eval_t, 1);
  result->table = table;
  result->towner = FALSE;
  result->value = MIX_WORD_ZERO;
  result->errpos = 0;
  return result;
}

/* delete */
void
mix_eval_delete (mix_eval_t *eval)
{
  g_return_if_fail (eval);
  if (eval->table && eval->towner) {
    mix_symbol_table_delete (eval->table);
  }
  g_free (eval);
}

/* eval an expression */
mix_eval_result_t
mix_eval_expression_with_loc (mix_eval_t *eval, const gchar *expr,
			      mix_short_t loc)
{
  mix_eval_data_ data;
  
  if (expr == NULL || eval == NULL)
    return MIX_EVAL_INTERN;
  data.expr = g_strdup_printf ("%s\n", expr);
  data.table = eval->table;
  data.errpos = eval->errpos;
  data.value = eval->value;
  data.loc = loc;
  eval->result = mix_eval_expr (&data);
  if (eval->result == MIX_EVAL_OK) {
    eval->value = data.value;
    eval->errpos = -1;
  } else {
    eval->errpos = data.errpos;
  }
  g_free (data.expr);
    
  return eval->result;
}

/* get the result of the last evaluation */
mix_word_t
mix_eval_value (const mix_eval_t *eval) 
{
  g_return_val_if_fail (eval != NULL, MIX_WORD_ZERO);
  return eval->value;
}

/* get the last eval result code */
mix_eval_result_t 
mix_eval_last_error (const mix_eval_t *eval) 
{
  g_return_val_if_fail (eval != NULL, MIX_EVAL_INTERN);
  return eval->result;
}

/* get the last error string */
const gchar*
mix_eval_last_error_string (const mix_eval_t *eval) 
{
  g_return_val_if_fail (eval != NULL, errors_[MIX_EVAL_INTERN]);
  return errors_[eval->result];
}

/* get the position of last error */
guint
mix_eval_last_error_pos (const mix_eval_t *eval)
{
  g_return_val_if_fail (eval != NULL, 0);
  return eval->errpos;
}

/* add, or redefine, a symbol. see mix_symbol_table.h for
   possible outcomes. */
gint
mix_eval_set_symbol (mix_eval_t *eval, const gchar *symbol,
		     mix_word_t value)
{
  g_return_val_if_fail (eval != NULL && eval->table != NULL,
			MIX_SYM_FAIL);
  return mix_symbol_table_insert (eval->table, symbol, value);
}

void
mix_eval_remove_symbol (mix_eval_t *eval, const gchar *symbol)
{
  g_return_if_fail (eval != NULL && eval->table != NULL);
  mix_symbol_table_remove (eval->table, symbol);
}

void
mix_eval_use_symbol_table (mix_eval_t *eval,
			   mix_symbol_table_t *table)
{
  g_return_if_fail (eval != NULL);
  if (eval->table != NULL && eval->towner)
    mix_symbol_table_delete (eval->table);
  eval->table = table;
  eval->towner = FALSE;
}

const mix_symbol_table_t *
mix_eval_symbol_table (const mix_eval_t *eval)
{
  g_return_val_if_fail (eval != NULL, NULL);
  return eval->table;
}

gboolean
mix_eval_set_symbols_from_table (mix_eval_t *eval,
				 const mix_symbol_table_t *table)
{
  g_return_val_if_fail (eval != NULL, FALSE);
  if (eval->table != NULL)
    return mix_symbol_table_merge_table (eval->table, table);
  else
    return FALSE;
}

gboolean
mix_eval_remove_symbols_from_table (mix_eval_t *eval,
				    const mix_symbol_table_t *table)
{
  g_return_val_if_fail (eval != NULL, FALSE);
  if (eval->table != NULL)
    return mix_symbol_table_substract_table (eval->table, table);
  else
    return FALSE;
}