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
|
#include <stdio.h>
#include <ctype.h>
#include "pic.h"
#include "picy.h"
YYSTYPE *getvar(s) /* return value of variable s (usually pointer) */
char *s;
{
struct symtab *p;
static YYSTYPE bug;
p = lookup(s);
if (p == NULL) {
if (islower(s[0]))
yyerror("no such variable as %s", s);
else
yyerror("no such place as %s", s);
return(&bug);
}
return(&(p->s_val));
}
float getfval(s) /* return float value of variable s */
char *s;
{
YYSTYPE *y;
y = getvar(s);
return y->f;
}
setfval(s, f) /* set variable s to f */
char *s;
float f;
{
struct symtab *p;
if ((p = lookup(s)) != NULL)
p->s_val.f = f;
}
struct symtab *makevar(s, t, v) /* make variable named s in table */
char *s; /* assumes s is static or from tostring */
int t;
YYSTYPE v;
{
struct symtab *p;
for (p = stack[nstack].p_symtab; p != NULL; p = p->s_next)
if (strcmp(s, p->s_name) == 0)
break;
if (p == NULL) { /* it's a new one */
p = (struct symtab *) malloc(sizeof(struct symtab));
if (p == NULL) {
yyerror("out of symtab space with %s", s);
exit(1);
}
p->s_next = stack[nstack].p_symtab;
stack[nstack].p_symtab = p; /* stick it at front */
}
p->s_name = s;
p->s_type = t;
p->s_val = v;
return(p);
}
struct symtab *lookup(s) /* find s in symtab */
char *s;
{
int i;
struct symtab *p;
for (i = nstack; i >= 0; i--) /* look in each active symtab */
for (p = stack[i].p_symtab; p != NULL; p = p->s_next)
if (strcmp(s, p->s_name) == 0)
return(p);
return(NULL);
}
freesymtab(p) /* free space used by symtab at p */
struct symtab *p;
{
for ( ; p != NULL; p = p->s_next) {
free(p->s_name); /* assumes done with tostring */
free((char *)p);
}
}
|