2 * Empire - A multi-player, client/server Internet based war game.
3 * Copyright (C) 1986-2008, Dave Pare, Jeff Bailey, Thomas Ruschak,
4 * Ken Stevens, Steve McClure
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
22 * See files README, COPYING and CREDITS in the root of the source
23 * tree for related information and legal notices. It is expected
24 * that future projects/authors will amend these files as needed.
28 * nstreval.c: evaluate compiled values
30 * Known contributors to this file:
33 * Markus Armbruster, 2004-2008
46 * Initialize VAL to symbolic value for selector CA with index IDX.
50 nstr_mksymval(struct valstr *val, struct castr *ca, int idx)
52 val->val_type = ca->ca_type;
53 val->val_cat = NSC_OFF;
54 val->val_as.sym.off = ca->ca_off;
55 val->val_as.sym.len = ca->ca_len;
56 val->val_as.sym.idx = idx;
57 val->val_as.sym.get = ca->ca_get;
63 * If VAL is symbolic, evaluate it into a promoted value type.
64 * Use country CNUM's coordinate system and access control.
65 * PTR points to a context object of the type that was used to compile
67 * Unless WANT is NSC_NOTYPE, coerce the value to promoted value type
68 * WANT. VAL must be coercible. That's the case if a previous
69 * nstr_coerce_val(VAL, WANT, STR) succeeded.
72 nstr_exec_val(struct valstr *val, natid cnum, void *ptr, enum nsc_type want)
75 enum nsc_type valtype;
79 if (CANT_HAPPEN(want != NSC_NOTYPE && !NSC_IS_PROMOTED(want)))
80 want = nstr_promote(want);
82 switch (val->val_cat) {
84 valtype = val->val_type;
87 if (val->val_as.sym.get) {
89 ptr = val->val_as.sym.get(val, cnum, ptr);
90 } while (ptr && val->val_as.sym.get);
92 valtype = val->val_type;
93 val->val_cat = NSC_VAL;
100 memb_ptr += val->val_as.sym.off;
101 idx = val->val_as.sym.idx;
102 switch (val->val_type) {
104 val->val_as.lng = ((signed char *)memb_ptr)[idx];
107 val->val_as.lng = ((unsigned char *)memb_ptr)[idx];
110 val->val_as.lng = ((short *)memb_ptr)[idx];
113 val->val_as.lng = ((unsigned short *)memb_ptr)[idx];
116 val->val_as.lng = ((int *)memb_ptr)[idx];
119 val->val_as.lng = ((long *)memb_ptr)[idx];
122 val->val_as.lng = xrel(getnatp(cnum), ((short *)memb_ptr)[idx]);
125 val->val_as.lng = yrel(getnatp(cnum), ((short *)memb_ptr)[idx]);
128 val->val_as.lng = -1;
129 if (CANT_HAPPEN(((struct natstr *)ptr)->ef_type != EF_NATION))
131 natp = getnatp(cnum);
133 || natp->nat_stat == STAT_GOD
134 || (getcontact(natp, idx) && getcontact(ptr, idx)))
135 val->val_as.lng = ((unsigned char *)memb_ptr)[idx];
138 val->val_as.dbl = ((float *)memb_ptr)[idx];
139 valtype = NSC_DOUBLE;
142 val->val_as.dbl = ((double *)memb_ptr)[idx];
143 valtype = NSC_DOUBLE;
147 val->val_as.str.maxsz = val->val_as.sym.len;
148 val->val_as.str.base = (char *)memb_ptr;
149 valtype = NSC_STRING;
152 val->val_as.str.base = ((char **)memb_ptr)[idx];
153 val->val_as.str.maxsz = INT_MAX;
154 valtype = NSC_STRING;
157 val->val_as.lng = ((time_t *)memb_ptr)[idx];
163 val->val_cat = NSC_VAL;
167 valtype = NSC_NOTYPE;
172 else if (want == NSC_DOUBLE) {
173 if (valtype == NSC_LONG) {
175 val->val_as.dbl = val->val_as.lng;
177 } else if (want == NSC_STRING)
178 CANT_REACH(); /* FIXME implement */
180 if (CANT_HAPPEN(valtype != want && want != NSC_NOTYPE)) {
181 /* make up an error value */
183 memset(&val->val_as, 0, sizeof(val->val_as));
186 val->val_type = valtype;
192 * If VALTYPE is an integer type, return NSC_LONG.
193 * If VALTYPE is a floating-point type, return NSC_DOUBLE.
194 * If VALTYPE is a string type, return NSC_STRING.
197 nstr_promote(int valtype)
216 valtype = NSC_DOUBLE;
219 valtype = NSC_STRING;
223 valtype = NSC_NOTYPE;
229 symbol_by_value(int key, struct symbol *table)
233 for (i = 0; table[i].name; i++)
234 if (key == table[i].value)
235 return table[i].name;