]> git.pond.sub.org Git - empserver/blob - src/lib/subs/nstr.c
(nstr_resolve_id): Resolve ambigous name into string if string_ok.
[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 && !string_ok) {
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 == M_NOTFOUND && !string_ok) {
400         pr("%.*s -- unknown name\n",
401            (int)val->val_as.str.maxsz, val->val_as.str.base);
402         val->val_cat = NSC_NOCAT;
403         return NULL;
404     }
405
406     if (idx < 0) {
407         CANT_HAPPEN(!string_ok);
408         /* interpret unresolvable identifier as string */
409         val->val_type = NSC_STRING;
410         val->val_cat = NSC_VAL;
411         return val;
412     }
413
414     return nstr_resolve_sel(val, &ca[idx]);
415 }
416
417 /*
418  * Change VAL to resolve identifier to selector CA.
419  * Return VAL on success, NULL if the player is denied access to the
420  * selector.
421  * VAL must be an identifier.
422  */
423 static struct valstr *
424 nstr_resolve_sel(struct valstr *val, struct castr *ca)
425 {
426     if (CANT_HAPPEN(val->val_cat != NSC_ID)) {
427         val->val_cat = NSC_NOCAT;
428         return val;
429     }
430
431     if ((ca->ca_flags & NSC_DEITY) && !player->god) {
432         pr("%.*s -- not accessible to mortals\n",
433            (int)val->val_as.str.maxsz, val->val_as.str.base);
434         val->val_cat = NSC_NOCAT;
435         return NULL;
436     }
437
438     val->val_type = ca->ca_type;
439     val->val_cat = NSC_OFF;
440     val->val_as.sym.off = ca->ca_off;
441     val->val_as.sym.len = ca->ca_len;
442     val->val_as.sym.idx = 0;
443     return val;
444 }
445
446 /*
447  * Initialize VAL to value SELVAL for selector CA, return VAL.
448  */
449 static struct valstr *
450 nstr_mkselval(struct valstr *val, int selval, struct castr *ca)
451 {
452     if (CANT_HAPPEN(ca->ca_type != NSC_TYPEID)) {
453         val->val_type = NSC_NOTYPE;
454         val->val_cat = NSC_NOCAT;
455         return val;
456     }
457
458     val->val_type = ca->ca_type;
459     val->val_cat = NSC_VAL;
460     val->val_as.lng = selval;
461     return val;
462 }
463
464 /*
465  * Compile a value in STR into VAL.
466  * Return a pointer to the first character after the value on success,
467  * NULL on error.
468  * TYPE is the context type, a file type.
469  * If STR names an array, VAL simply refers to the element with index
470  * zero.
471  */
472 char *
473 nstr_comp_val(char *str, struct valstr *val, int type)
474 {
475     struct castr *ca = ef_cadef(type);
476     char *tail = nstr_parse_val(str, val);
477     if (!nstr_resolve_id(val, ca, nstr_match_ca(val, ca), 0))
478         return NULL;
479     return tail;
480 }
481
482
483 /*
484  * Promote VALTYPE.
485  * If VALTYPE is an integer type, return NSC_LONG.
486  * If VALTYPE is a floating-point type, return NSC_DOUBLE.
487  * If VALTYPE is NSC_STRINGY, return NSC_STRING.
488  * If VALTYPE is NSC_NOTYPE, NSC_STRING or NSC_TYPEID, return VALTYPE.
489  */
490 static int
491 nstr_promote(int valtype)
492 {
493     switch (valtype) {
494     case NSC_NOTYPE:
495     case NSC_LONG:
496     case NSC_DOUBLE:
497     case NSC_STRING:
498     case NSC_TYPEID:
499         break;
500     case NSC_CHAR:
501     case NSC_UCHAR:
502     case NSC_SHORT:
503     case NSC_USHORT:
504     case NSC_INT:
505     case NSC_XCOORD:
506     case NSC_YCOORD:
507     case NSC_HIDDEN:
508     case NSC_TIME:
509         valtype = NSC_LONG;
510         break;
511     case NSC_FLOAT:
512         valtype = NSC_DOUBLE;
513         break;
514     case NSC_STRINGY:
515         valtype = NSC_STRING;
516         break;
517     default:
518         CANT_REACH();
519         valtype = NSC_NOTYPE;
520     }
521     return valtype;
522 }
523
524 static int
525 cond_type_mismatch(char *str)
526 {
527     if (str)
528         pr("%s -- condition operand type mismatch\n", str);
529     return -1;
530 }
531
532 /*
533  * Coerce VAL to promoted value type TO.
534  * Return 0 on success, -1 on error.
535  * If VAL is evaluated, convert it, else only check.
536  * STR is the condition text to be used for error messages.  Suppress
537  * messages if it is a null pointer.
538  */
539 int
540 nstr_coerce_val(struct valstr *val, nsc_type to, char *str)
541 {
542     /* FIXME get rid of promotion?  */
543     nsc_type from = nstr_promote(val->val_type);
544
545     if (from == NSC_NOTYPE)
546         return 0;
547
548     if (from != to) {
549         switch (to) {
550         case NSC_TYPEID:
551             return cond_type_mismatch(str);
552         case NSC_STRING:
553             return cond_type_mismatch(str); /* FIXME implement */
554         case NSC_DOUBLE:
555             if (from == NSC_LONG) {
556                 if (val->val_cat == NSC_VAL)
557                     val->val_as.dbl = val->val_as.lng;
558             } else
559                 return cond_type_mismatch(str);
560             break;
561         case NSC_LONG:
562             return cond_type_mismatch(str);
563         default:
564             CANT_REACH();
565             to = from;
566         }
567     }
568
569     if (val->val_cat == NSC_VAL) {
570         /* unimplemented conversions; don't currently occur here */
571         CANT_HAPPEN(val->val_type == NSC_XCOORD
572                     || val->val_type == NSC_YCOORD
573                     || val->val_type == NSC_HIDDEN);
574         val->val_type = to;
575     }
576
577     return 0;
578 }
579
580 /*
581  * Evaluate VAL.
582  * If VAL is symbolic, evaluate it into a promoted value type.
583  * Use coordinate system of country CNUM.
584  * PTR points to a context object of the type that was used to compile
585  * the value.
586  * Unless WANT is NSC_NOTYPE, coerce the value to promoted value type
587  * WANT.  VAL must be coercible.  That's the case if a previous
588  * nstr_coerce_val(VAL, WANT, STR) succeeded.
589  */
590 void
591 nstr_exec_val(struct valstr *val, natid cnum, void *ptr, nsc_type want)
592 {
593     char *memb_ptr;
594     nsc_type valtype;
595     int idx;
596     struct natstr *natp;
597
598     switch (val->val_cat) {
599     default:
600         CANT_REACH();
601         /* fall through */
602     case NSC_VAL:
603         valtype = val->val_type;
604         break;
605     case NSC_OFF:
606         valtype = NSC_LONG;
607         memb_ptr = ptr;
608         memb_ptr += val->val_as.sym.off;
609         idx = val->val_as.sym.idx;
610         switch (val->val_type) {
611         case NSC_CHAR:
612             val->val_as.lng = ((signed char *)memb_ptr)[idx];
613             break;
614         case NSC_UCHAR:
615             val->val_as.lng = ((unsigned char *)memb_ptr)[idx];
616             break;
617         case NSC_SHORT:
618             val->val_as.lng = ((short *)memb_ptr)[idx];
619             break;
620         case NSC_USHORT:
621             val->val_as.lng = ((unsigned short *)memb_ptr)[idx];
622             break;
623         case NSC_INT:
624             val->val_as.lng = ((int *)memb_ptr)[idx];
625             break;
626         case NSC_LONG:
627             val->val_as.lng = ((long *)memb_ptr)[idx];
628             break;
629         case NSC_XCOORD:
630             val->val_as.lng = xrel(getnatp(cnum), ((short *)memb_ptr)[idx]);
631             break;
632         case NSC_YCOORD:
633             val->val_as.lng = yrel(getnatp(cnum), ((short *)memb_ptr)[idx]);
634             break;
635         case NSC_HIDDEN:
636             val->val_as.lng = -1;
637             if (CANT_HAPPEN(((struct natstr *)ptr)->ef_type != EF_NATION))
638                 break;
639             natp = getnatp(cnum);
640             if (!opt_HIDDEN
641                 || natp->nat_stat == STAT_GOD
642                 || (getcontact(natp, idx) && getcontact(ptr, idx)))
643                 val->val_as.lng = ((unsigned char *)memb_ptr)[idx];
644             break;
645         case NSC_FLOAT:
646             val->val_as.dbl = ((float *)memb_ptr)[idx];
647             valtype = NSC_DOUBLE;
648             break;
649         case NSC_DOUBLE:
650             val->val_as.dbl = ((double *)memb_ptr)[idx];
651             valtype = NSC_DOUBLE;
652             break;
653         case NSC_STRINGY:
654             CANT_HAPPEN(idx);
655             val->val_as.str.maxsz = val->val_as.sym.len;
656             val->val_as.str.base = (char *)memb_ptr;
657             valtype = NSC_STRING;
658             break;
659         case NSC_STRING:
660             val->val_as.str.base = ((char **)memb_ptr)[idx];
661             val->val_as.str.maxsz = INT_MAX;
662             valtype = NSC_STRING;
663             break;
664         case NSC_TIME:
665             val->val_as.lng = ((time_t *)memb_ptr)[idx];
666             break;
667         case NSC_TYPEID:
668             val->val_as.lng = ((signed char *)memb_ptr)[idx];
669             valtype = NSC_TYPEID;
670             break;
671         default:
672             CANT_REACH();
673             val->val_as.lng = 0;
674         }
675         val->val_cat = NSC_VAL;
676     }
677
678     if (valtype == want)
679         ;
680     else if (want == NSC_DOUBLE) {
681         if (valtype == NSC_LONG) {
682             valtype = want;
683             val->val_as.dbl = val->val_as.lng;
684         }
685     } else if (want == NSC_STRING)
686         CANT_REACH();           /* FIXME implement */
687
688     if (CANT_HAPPEN(valtype != want && want != NSC_NOTYPE)) {
689         valtype = want;
690         switch (want) {
691         case NSC_TYPEID:
692         case NSC_LONG: val->val_as.lng = 0; break;
693         case NSC_DOUBLE: val->val_as.dbl = 0.0; break;
694         case NSC_STRING: val->val_as.str.base = NULL; break;
695         default:
696             CANT_REACH();
697         }
698     }
699
700     val->val_type = valtype;
701 }
702
703 char *
704 symbol_by_value(int key, struct symbol *table)
705 {
706     int i;
707
708     for (i = 0; table[i].name; i++)
709         if (key == table[i].value)
710             return table[i].name;
711
712     return NULL;
713 }