]> git.pond.sub.org Git - empserver/blob - src/lib/subs/nstr.c
(nsc_type): New member NSC_HIDDEN.
[empserver] / src / lib / subs / nstr.c
1 /*
2  *  Empire - A multi-player, client/server Internet based war game.
3  *  Copyright (C) 1986-2006, Dave Pare, Jeff Bailey, Thomas Ruschak,
4  *                           Ken Stevens, Steve McClure
5  *
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.
10  *
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.
15  *
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
19  *
20  *  ---
21  *
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.
25  *
26  *  ---
27  *
28  *  nstr.c: compile and execute the item selections on sectors
29  * 
30  *  Known contributors to this file:
31  *     Dave Pare, 1989
32  *     Steve McClure, 1997
33  *     Markus Armbruster, 2004-2005
34  */
35
36 #include <config.h>
37
38 #include <limits.h>
39 #include "misc.h"
40 #include "file.h"
41 #include "match.h"
42 #include "nsc.h"
43 #include "optlist.h"
44 #include "prototypes.h"
45
46 static char *nstr_parse_val(char *, struct valstr *);
47 static int nstr_match_ca(struct valstr *, struct castr *);
48 static int nstr_match_val(struct valstr *, int, struct castr *, int);
49 static int nstr_string_ok(struct castr *ca, int idx);
50 static struct valstr *nstr_resolve_sel(struct valstr *, struct castr *);
51 static struct valstr *nstr_mkselval(struct valstr *, int, struct castr *);
52 static struct valstr *nstr_resolve_id(struct valstr *, struct castr *, int, int);
53 static int nstr_promote(int);
54
55
56 /*
57  * Compile conditions into array NP[LEN].
58  * Return number of conditions, or -1 on error.
59  * It is an error if there are more than LEN conditions.
60  * TYPE is the context type, a file type.
61  * STR is the condition string, in Empire syntax, without the leading
62  * '?'.
63  */
64 int
65 nstr_comp(struct nscstr *np, int len, int type, char *str)
66 {
67     struct castr *ca = ef_cadef(type);
68     char *cond;
69     char *tail;
70     int i;
71     struct nscstr dummy;
72     int lft_caidx, rgt_caidx;
73     int lft_val, rgt_val;
74     int lft_type, rgt_type;
75
76     cond = str;
77     for (i = 0; ; ++i, ++np) {
78         if (i >= len)
79             np = &dummy;
80
81         /* left operand */
82         tail = nstr_parse_val(cond, &np->lft);
83         lft_caidx = nstr_match_ca(&np->lft, ca);
84
85         /* operator */
86         if (*tail != '<' && *tail != '=' && *tail != '>' && *tail != '#') {
87             if (*tail)
88                 pr("%s -- expected condition operator\n", cond);
89             else
90                 pr("%s -- missing condition operator\n", cond);
91             return -1;
92         }
93         np->operator = *tail;
94         ++tail;
95
96         /* right operand */
97         tail = nstr_parse_val(tail, &np->rgt);
98         rgt_caidx = nstr_match_ca(&np->rgt, ca);
99
100         /*
101          * Resolve identifiers
102          *
103          * An identifier can name a selector or, if the other operand
104          * is a selector, a value for that.  The condition is
105          * ambiguous if both selector x value and value x selector are
106          * possible.  Example: n<n for sectors could mean newdes<n or
107          * n<newdes.
108          */
109         lft_val = nstr_match_val(&np->lft, type, ca, rgt_caidx);
110         rgt_val = nstr_match_val(&np->rgt, type, ca, lft_caidx);
111         /*
112          * if lft_val >= 0, then rhs names a selector and lhs names
113          * one of its values.  Likewise for rgt_val.
114          */
115         if (lft_val >= 0 && rgt_val >= 0) {
116             pr("%.*s -- condition ambiguous\n", (int)(tail-cond), cond);
117             return -1;
118         } else if (rgt_val >= 0) {
119             /* selector x value */
120             if (!nstr_resolve_sel(&np->lft, &ca[lft_caidx]))
121                 return -1;
122             nstr_mkselval(&np->rgt, rgt_val, &ca[lft_caidx]);
123         } else if (lft_val >= 0) {
124             /* value x selector */
125             nstr_mkselval(&np->lft, lft_val, &ca[rgt_caidx]);
126             if (!nstr_resolve_sel(&np->rgt, &ca[rgt_caidx]))
127                 return -1;
128         } else {
129             /*
130              * Neither side works as selector value; any identifiers
131              * must name selectors.
132              */
133             if (!nstr_resolve_id(&np->lft, ca, lft_caidx,
134                                  nstr_string_ok(ca, rgt_caidx)))
135                 return -1;
136             if (!nstr_resolve_id(&np->rgt, ca, rgt_caidx,
137                                  nstr_string_ok(ca, lft_caidx)))
138                 return -1;
139         }
140
141         /* find operator type, coerce operands */
142         lft_type = nstr_promote(np->lft.val_type);
143         rgt_type = nstr_promote(np->rgt.val_type);
144         np->optype = NSC_NOTYPE;
145         if (lft_type == NSC_TYPEID) {
146             if (!nstr_coerce_val(&np->rgt, NSC_TYPEID, str))
147                 np->optype = NSC_TYPEID;
148         } else if (rgt_type == NSC_TYPEID) {
149             if (!nstr_coerce_val(&np->lft, NSC_TYPEID, str))
150                 np->optype = NSC_TYPEID;
151         } else if (lft_type == NSC_STRING) {
152             if (!nstr_coerce_val(&np->rgt, NSC_STRING, str))
153                 np->optype = NSC_STRING;
154         } else if (rgt_type == NSC_STRING) {
155             if (!nstr_coerce_val(&np->lft, NSC_STRING, str))
156                 np->optype = NSC_STRING;
157         } else if (lft_type == NSC_DOUBLE) {
158             if (!nstr_coerce_val(&np->rgt, NSC_DOUBLE, str))
159                 np->optype = NSC_DOUBLE;
160         } else if (rgt_type == NSC_DOUBLE) {
161             if (!nstr_coerce_val(&np->lft, NSC_DOUBLE, str))
162                 np->optype = NSC_DOUBLE;
163         } else {
164             if (!nstr_coerce_val(&np->lft, NSC_LONG, str)
165                 && !nstr_coerce_val(&np->rgt, NSC_LONG, str))
166                 np->optype = NSC_LONG;
167         }
168         if (np->optype == NSC_NOTYPE)
169             return -1;
170
171         /* another condition? */
172         if (*tail == 0)
173             break;
174         if (*tail != '&') {
175             pr("%s -- expected `&'\n", cond);
176             return -1;
177         }
178         cond = tail + 1;
179     }
180
181     if (i >= len) {
182         /* could just return I and let caller gripe or enlarge buffer */
183         pr("%s -- too many conditions\n", str);
184         return -1;
185     }
186
187     return i + 1;
188 }
189
190 /* Like strcmp(S1, S2), but limit length of S1 to SZ1 and of S2 to SZ2.  */
191 static int
192 strnncmp(char *s1, size_t sz1, char *s2, size_t sz2)
193 {
194     int res;
195     if (sz1 == sz2) return strncmp(s1, s2, sz2);
196     if (sz1 < sz2) return -strnncmp(s2, sz2, s1, sz1);
197     res = strncmp(s1, s2, sz2);
198     return res ? res : s1[sz2];
199 }
200
201 #define EVAL(op, lft, rgt)                      \
202     ((op) == '<' ? (lft) < (rgt)                \
203      : (op) == '=' ? (lft) == (rgt)             \
204      : (op) == '>' ? (lft) > (rgt)              \
205      : (op) == '#' ? (lft) != (rgt)             \
206      : 0)
207
208 /*
209  * Evaluate compiled conditions in array NP[NCOND].
210  * Return non-zero iff they are all true.
211  * PTR points to a context object of the type that was used to compile
212  * the conditions.
213  */
214 int
215 nstr_exec(struct nscstr *np, int ncond, void *ptr)
216 {
217     int i, op, optype, cmp;
218     struct valstr lft, rgt;
219
220     for (i = 0; i < ncond; ++i) {
221         op = np[i].operator;
222         optype = np[i].optype;
223         if (np[i].lft.val_cat == NSC_NOCAT || np[i].rgt.val_cat == NSC_NOCAT)
224             return 0;
225         lft = np[i].lft;
226         nstr_exec_val(&lft, player->cnum, ptr, optype);
227         rgt = np[i].rgt;
228         nstr_exec_val(&rgt, player->cnum, ptr, optype);
229         switch (optype) {
230         case NSC_TYPEID:
231         case NSC_LONG:
232             if (!EVAL(op, lft.val_as.lng, rgt.val_as.lng))
233                 return 0;
234             break;
235         case NSC_DOUBLE:
236             if (!EVAL(op, lft.val_as.dbl, rgt.val_as.dbl))
237                 return 0;
238             break;
239         case NSC_STRING:
240             cmp = strnncmp(lft.val_as.str.base, lft.val_as.str.maxsz,
241                            rgt.val_as.str.base, rgt.val_as.str.maxsz);
242             if (!EVAL(op, cmp, 0))
243                 return 0;
244             break;
245         default:
246             CANT_REACH();
247             return 0;
248         }
249     }
250
251     return 1;
252 }
253
254 /*
255  * Parse a value in STR into VAL.
256  * Return a pointer to the first character after the value.
257  * Value is either evaluated (but not NSC_TYPEID) or an identifier.
258  */
259 static char *
260 nstr_parse_val(char *str, struct valstr *val)
261 {
262     long l;
263     double d;
264     char *tail, *tail2;
265
266     /* string */
267     if (str[0] == '\'') {
268         for (tail = str + 1; *tail && *tail != '\''; ++tail) ;
269         /* FIXME implement \ quoting */
270         val->val_type = NSC_STRING;
271         val->val_cat = NSC_VAL;
272         val->val_as.str.base = str + 1;
273         val->val_as.str.maxsz = tail - (str + 1);
274         if (*tail) ++tail;
275         return tail;
276     }
277
278     /* identifier */
279     if (isalpha(str[0])) {
280         for (tail = str+1; isalnum(*tail) || *tail == '_'; ++tail) ;
281         val->val_type = NSC_NOTYPE;
282         val->val_cat = NSC_ID;
283         val->val_as.str.base = str;
284         val->val_as.str.maxsz = tail - str;
285         return tail;
286     }
287
288     /* number */
289     l = strtol(str, &tail, 0);
290     d = strtod(str, &tail2);
291     if (tail2 > tail) {
292         val->val_type = NSC_DOUBLE;
293         val->val_cat = NSC_VAL;
294         val->val_as.dbl = d;
295         return tail2;
296     }
297     if (tail != str) {
298         val->val_type = NSC_LONG;
299         val->val_cat = NSC_VAL;
300         val->val_as.lng = l;
301         return tail;
302     }
303
304     /* funny character, interpret as identifier */
305     tail = str+1;
306     val->val_type = NSC_NOTYPE;
307     val->val_cat = NSC_ID;
308     val->val_as.str.base = str;
309     val->val_as.str.maxsz = tail - str;
310     return tail;
311 }
312
313 /*
314  * Match VAL in table of selector descriptors CA, return index.
315  * Return M_NOTFOUND if there are no matches, M_NOTUNIQUE if there are
316  * several.
317  * A VAL that is not an identifier doesn't match anything.  A null CA
318  * is considered empty.
319  */
320 static int
321 nstr_match_ca(struct valstr *val, struct castr *ca)
322 {
323     char id[32];
324
325     if (val->val_cat != NSC_ID || val->val_as.str.maxsz >= sizeof(id))
326         return M_NOTFOUND;
327
328     if (!ca)
329         return M_NOTFOUND;
330
331     memcpy(id, val->val_as.str.base, val->val_as.str.maxsz);
332     id[val->val_as.str.maxsz] = 0;
333
334     return stmtch(id, ca, offsetof(struct castr, ca_name),
335                   sizeof(struct castr));
336 }
337
338 /*
339  * Match VAL in a selector's values, return its (non-negative) value.
340  * TYPE is the context type, a file type.
341  * CA is ef_cadef(TYPE).  If it is null, then IDX must be negative.
342  * Match values of selector descriptor CA[IDX], provided IDX is not
343  * negative.
344  * Return M_NOTFOUND if there are no matches, M_NOTUNIQUE if there are
345  * several.
346  * TODO: This is just a stub and works only for NSC_TYPEID.
347  * Generalize: give struct castr enough info to find values, remove
348  * parameter `type'.
349  */
350 static int
351 nstr_match_val(struct valstr *val, int type, struct castr *ca, int idx)
352 {
353     char id[32];
354
355     if (val->val_cat != NSC_ID || val->val_as.str.maxsz >= sizeof(id))
356         return M_NOTFOUND;
357
358     if (idx < 0 || ca[idx].ca_type != NSC_TYPEID)
359         return M_NOTFOUND;
360
361     memcpy(id, val->val_as.str.base, val->val_as.str.maxsz);
362     id[val->val_as.str.maxsz] = 0;
363
364     return typematch(id, type);
365 }
366
367 /*
368  * Can CA[IDX] be compared to a string?
369  * Return 0 for negative IDX.
370  */
371 static int
372 nstr_string_ok(struct castr *ca, int idx)
373 {
374     return idx >= 0 && nstr_promote(ca[idx].ca_type) == NSC_STRING;
375 }
376
377 /*
378  * Change VAL to resolve identifier to selector or string.
379  * Return VAL on success, NULL on error.
380  * No change if VAL is not an identifier.  Otherwise, change it as
381  * follows.
382  * Error if IDX == M_NOTUNIQUE or IDX == M_NOTFOUND and !STRING_OK.
383  * Change into string if IDX == M_NOTFOUND and STRING_OK.
384  * Change into selector CA[IDX] if IDX >= 0.
385  */
386 static struct valstr *
387 nstr_resolve_id(struct valstr *val, struct castr *ca, int idx, int string_ok)
388 {
389     if (val->val_cat != NSC_ID)
390         return val;
391
392     if (idx == M_NOTUNIQUE) {
393         pr("%.*s -- ambiguous name\n",
394            (int)val->val_as.str.maxsz, val->val_as.str.base);
395         val->val_cat = NSC_NOCAT;
396         return NULL;
397     }
398
399     if (idx < 0) {
400         CANT_HAPPEN(idx != M_NOTFOUND);
401         if (!string_ok) {
402             pr("%.*s -- unknown name\n",
403                (int)val->val_as.str.maxsz, val->val_as.str.base);
404             val->val_cat = NSC_NOCAT;
405             return NULL;
406         }
407         /* interpret unbound identifier as string */
408         val->val_type = NSC_STRING;
409         val->val_cat = NSC_VAL;
410         return val;
411     }
412
413     return nstr_resolve_sel(val, &ca[idx]);
414 }
415
416 /*
417  * Change VAL to resolve identifier to selector CA.
418  * Return VAL on success, NULL if the player is denied access to the
419  * selector.
420  * VAL must be an identifier.
421  */
422 static struct valstr *
423 nstr_resolve_sel(struct valstr *val, struct castr *ca)
424 {
425     if (CANT_HAPPEN(val->val_cat != NSC_ID)) {
426         val->val_cat = NSC_NOCAT;
427         return val;
428     }
429
430     if ((ca->ca_flags & NSC_DEITY) && !player->god) {
431         pr("%.*s -- not accessible to mortals\n",
432            (int)val->val_as.str.maxsz, val->val_as.str.base);
433         val->val_cat = NSC_NOCAT;
434         return NULL;
435     }
436
437     val->val_type = ca->ca_type;
438     val->val_cat = NSC_OFF;
439     val->val_as.sym.off = ca->ca_off;
440     val->val_as.sym.len = ca->ca_len;
441     val->val_as.sym.idx = 0;
442     return val;
443 }
444
445 /*
446  * Initialize VAL to value SELVAL for selector CA, return VAL.
447  */
448 static struct valstr *
449 nstr_mkselval(struct valstr *val, int selval, struct castr *ca)
450 {
451     if (CANT_HAPPEN(ca->ca_type != NSC_TYPEID)) {
452         val->val_type = NSC_NOTYPE;
453         val->val_cat = NSC_NOCAT;
454         return val;
455     }
456
457     val->val_type = ca->ca_type;
458     val->val_cat = NSC_VAL;
459     val->val_as.lng = selval;
460     return val;
461 }
462
463 /*
464  * Compile a value in STR into VAL.
465  * Return a pointer to the first character after the value on success,
466  * NULL on error.
467  * TYPE is the context type, a file type.
468  * If STR names an array, VAL simply refers to the element with index
469  * zero.
470  */
471 char *
472 nstr_comp_val(char *str, struct valstr *val, int type)
473 {
474     struct castr *ca = ef_cadef(type);
475     char *tail = nstr_parse_val(str, val);
476     return nstr_resolve_id(val, ca, nstr_match_ca(val, ca), 0) ? tail : NULL;
477 }
478
479
480 /*
481  * Promote VALTYPE.
482  * If VALTYPE is an integer type, return NSC_LONG.
483  * If VALTYPE is a floating-point type, return NSC_DOUBLE.
484  * If VALTYPE is NSC_STRINGY, return NSC_STRING.
485  * If VALTYPE is NSC_NOTYPE, NSC_STRING or NSC_TYPEID, return VALTYPE.
486  */
487 static int
488 nstr_promote(int valtype)
489 {
490     switch (valtype) {
491     case NSC_NOTYPE:
492     case NSC_LONG:
493     case NSC_DOUBLE:
494     case NSC_STRING:
495     case NSC_TYPEID:
496         break;
497     case NSC_CHAR:
498     case NSC_UCHAR:
499     case NSC_SHORT:
500     case NSC_USHORT:
501     case NSC_INT:
502     case NSC_XCOORD:
503     case NSC_YCOORD:
504     case NSC_HIDDEN:
505     case NSC_TIME:
506         valtype = NSC_LONG;
507         break;
508     case NSC_FLOAT:
509         valtype = NSC_DOUBLE;
510         break;
511     case NSC_STRINGY:
512         valtype = NSC_STRING;
513         break;
514     default:
515         CANT_REACH();
516         valtype = NSC_NOTYPE;
517     }
518     return valtype;
519 }
520
521 static int
522 cond_type_mismatch(char *str)
523 {
524     if (str)
525         pr("%s -- condition operand type mismatch\n", str);
526     return -1;
527 }
528
529 /*
530  * Coerce VAL to promoted value type TO.
531  * Return 0 on success, -1 on error.
532  * If VAL is evaluated, convert it, else only check.
533  * STR is the condition text to be used for error messages.  Suppress
534  * messages if it is a null pointer.
535  */
536 int
537 nstr_coerce_val(struct valstr *val, nsc_type to, char *str)
538 {
539     /* FIXME get rid of promotion?  */
540     nsc_type from = nstr_promote(val->val_type);
541
542     if (from == NSC_NOTYPE)
543         return 0;
544
545     if (from != to) {
546         switch (to) {
547         case NSC_TYPEID:
548             return cond_type_mismatch(str);
549         case NSC_STRING:
550             return cond_type_mismatch(str); /* FIXME implement */
551         case NSC_DOUBLE:
552             if (from == NSC_LONG) {
553                 if (val->val_cat == NSC_VAL)
554                     val->val_as.dbl = val->val_as.lng;
555             } else
556                 return cond_type_mismatch(str);
557             break;
558         case NSC_LONG:
559             return cond_type_mismatch(str);
560         default:
561             CANT_REACH();
562             to = from;
563         }
564     }
565
566     if (val->val_cat == NSC_VAL) {
567         /* unimplemented conversions; don't currently occur here */
568         CANT_HAPPEN(val->val_type == NSC_XCOORD
569                     || val->val_type == NSC_YCOORD
570                     || val->val_type == NSC_HIDDEN);
571         val->val_type = to;
572     }
573
574     return 0;
575 }
576
577 /*
578  * Evaluate VAL.
579  * If VAL is symbolic, evaluate it into a promoted value type.
580  * Use coordinate system of country CNUM.
581  * PTR points to a context object of the type that was used to compile
582  * the value.
583  * Unless WANT is NSC_NOTYPE, coerce the value to promoted value type
584  * WANT.  VAL must be coercible.  That's the case if a previous
585  * nstr_coerce_val(VAL, WANT, STR) succeeded.
586  */
587 void
588 nstr_exec_val(struct valstr *val, natid cnum, void *ptr, nsc_type want)
589 {
590     char *memb_ptr;
591     nsc_type valtype;
592     int idx;
593     struct natstr *natp;
594
595     switch (val->val_cat) {
596     default:
597         CANT_REACH();
598         /* fall through */
599     case NSC_VAL:
600         valtype = val->val_type;
601         break;
602     case NSC_OFF:
603         valtype = NSC_LONG;
604         memb_ptr = ptr;
605         memb_ptr += val->val_as.sym.off;
606         idx = val->val_as.sym.idx;
607         switch (val->val_type) {
608         case NSC_CHAR:
609             val->val_as.lng = ((signed char *)memb_ptr)[idx];
610             break;
611         case NSC_UCHAR:
612             val->val_as.lng = ((unsigned char *)memb_ptr)[idx];
613             break;
614         case NSC_SHORT:
615             val->val_as.lng = ((short *)memb_ptr)[idx];
616             break;
617         case NSC_USHORT:
618             val->val_as.lng = ((unsigned short *)memb_ptr)[idx];
619             break;
620         case NSC_INT:
621             val->val_as.lng = ((int *)memb_ptr)[idx];
622             break;
623         case NSC_LONG:
624             val->val_as.lng = ((long *)memb_ptr)[idx];
625             break;
626         case NSC_XCOORD:
627             val->val_as.lng = xrel(getnatp(cnum), ((short *)memb_ptr)[idx]);
628             break;
629         case NSC_YCOORD:
630             val->val_as.lng = yrel(getnatp(cnum), ((short *)memb_ptr)[idx]);
631             break;
632         case NSC_HIDDEN:
633             val->val_as.lng = -1;
634             if (CANT_HAPPEN(((struct natstr *)ptr)->ef_type != EF_NATION))
635                 break;
636             natp = getnatp(cnum);
637             if (!opt_HIDDEN
638                 || natp->nat_stat == STAT_GOD
639                 || (getcontact(natp, idx) && getcontact(ptr, idx)))
640                 val->val_as.lng = ((unsigned char *)memb_ptr)[idx];
641             break;
642         case NSC_FLOAT:
643             val->val_as.dbl = ((float *)memb_ptr)[idx];
644             valtype = NSC_DOUBLE;
645             break;
646         case NSC_DOUBLE:
647             val->val_as.dbl = ((double *)memb_ptr)[idx];
648             valtype = NSC_DOUBLE;
649             break;
650         case NSC_STRINGY:
651             CANT_HAPPEN(idx);
652             val->val_as.str.maxsz = val->val_as.sym.len;
653             val->val_as.str.base = (char *)memb_ptr;
654             valtype = NSC_STRING;
655             break;
656         case NSC_STRING:
657             val->val_as.str.base = ((char **)memb_ptr)[idx];
658             val->val_as.str.maxsz = INT_MAX;
659             valtype = NSC_STRING;
660             break;
661         case NSC_TIME:
662             val->val_as.lng = ((time_t *)memb_ptr)[idx];
663             break;
664         case NSC_TYPEID:
665             val->val_as.lng = ((signed char *)memb_ptr)[idx];
666             valtype = NSC_TYPEID;
667             break;
668         default:
669             CANT_REACH();
670             val->val_as.lng = 0;
671         }
672         val->val_cat = NSC_VAL;
673     }
674
675     if (valtype == want)
676         ;
677     else if (want == NSC_DOUBLE) {
678         if (valtype == NSC_LONG) {
679             valtype = want;
680             val->val_as.dbl = val->val_as.lng;
681         }
682     } else if (want == NSC_STRING)
683         CANT_REACH();           /* FIXME implement */
684
685     if (CANT_HAPPEN(valtype != want && want != NSC_NOTYPE)) {
686         valtype = want;
687         switch (want) {
688         case NSC_TYPEID:
689         case NSC_LONG: val->val_as.lng = 0; break;
690         case NSC_DOUBLE: val->val_as.dbl = 0.0; break;
691         case NSC_STRING: val->val_as.str.base = NULL; break;
692         default:
693             CANT_REACH();
694         }
695     }
696
697     val->val_type = valtype;
698 }
699
700 char *
701 symbol_by_value(int key, struct symbol *table)
702 {
703     int i;
704
705     for (i = 0; table[i].name; i++)
706         if (key == table[i].value)
707             return table[i].name;
708
709     return NULL;
710 }