summaryrefslogtreecommitdiffhomepage
path: root/mixlib/mix_eval.c
blob: 683b8ac613046cc565bab6731e07879e15c26fb5 (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
/* -*-c-*- -------------- mix_eval.c :
 * Implementation of the functions declared in mix_eval.h
 * ------------------------------------------------------------------
 *  Last change: Time-stamp: "00/12/02 23:13:47 jose"
 * ------------------------------------------------------------------
 * Copyright (C) 2000 jose antonio ortega ruiz <jaortega@acm.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 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 (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 (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 (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 (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);
}