|
|
1.1 ! root 1: #include <stdio.h> ! 2: #include <ctype.h> ! 3: #include "pic.h" ! 4: #include "y.tab.h" ! 5: ! 6: YYSTYPE getvar(s) /* return value of variable s (usually pointer) */ ! 7: char *s; ! 8: { ! 9: struct symtab *p; ! 10: static YYSTYPE bug; ! 11: ! 12: p = lookup(s); ! 13: if (p == NULL) { ! 14: if (islower(s[0])) ! 15: yyerror("no such variable as %s", s); ! 16: else ! 17: yyerror("no such place as %s", s); ! 18: return(bug); ! 19: } ! 20: return(p->s_val); ! 21: } ! 22: ! 23: float getfval(s) /* return float value of variable s */ ! 24: char *s; ! 25: { ! 26: YYSTYPE y; ! 27: ! 28: y = getvar(s); ! 29: return y.f; ! 30: } ! 31: ! 32: setfval(s, f) /* set variable s to f */ ! 33: char *s; ! 34: float f; ! 35: { ! 36: struct symtab *p; ! 37: ! 38: if ((p = lookup(s)) != NULL) ! 39: p->s_val.f = f; ! 40: } ! 41: ! 42: struct symtab *makevar(s, t, v) /* make variable named s in table */ ! 43: char *s; /* assumes s is static or from tostring */ ! 44: int t; ! 45: YYSTYPE v; ! 46: { ! 47: int i; ! 48: struct symtab *p; ! 49: ! 50: for (p = stack[nstack].p_symtab; p != NULL; p = p->s_next) ! 51: if (strcmp(s, p->s_name) == 0) ! 52: break; ! 53: if (p == NULL) { /* it's a new one */ ! 54: p = (struct symtab *) malloc(sizeof(struct symtab)); ! 55: if (p == NULL) { ! 56: yyerror("out of symtab space with %s", s); ! 57: exit(1); ! 58: } ! 59: p->s_next = stack[nstack].p_symtab; ! 60: stack[nstack].p_symtab = p; /* stick it at front */ ! 61: } ! 62: p->s_name = s; ! 63: p->s_type = t; ! 64: p->s_val = v; ! 65: return(p); ! 66: } ! 67: ! 68: struct symtab *lookup(s) /* find s in symtab */ ! 69: char *s; ! 70: { ! 71: int i; ! 72: struct symtab *p; ! 73: ! 74: for (i = nstack; i >= 0; i--) /* look in each active symtab */ ! 75: for (p = stack[i].p_symtab; p != NULL; p = p->s_next) ! 76: if (strcmp(s, p->s_name) == 0) ! 77: return(p); ! 78: return(NULL); ! 79: } ! 80: ! 81: freesymtab(p) /* free space used by symtab at p */ ! 82: struct symtab *p; ! 83: { ! 84: struct symtab *q; ! 85: ! 86: for ( ; p != NULL; p = q) { ! 87: q = p->s_next; ! 88: free(p->s_name); /* assumes done with tostring */ ! 89: free(p); ! 90: } ! 91: } ! 92: ! 93: freedef(s) /* free definition for string s */ ! 94: char *s; ! 95: { ! 96: struct symtab *p, *q, *op; ! 97: ! 98: for (p = op = q = stack[nstack].p_symtab; p != NULL; p = p->s_next) { ! 99: if (strcmp(s, p->s_name) == 0) { /* got it */ ! 100: if (p->s_type != DEFNAME) ! 101: break; ! 102: if (p == op) /* 1st elem */ ! 103: stack[nstack].p_symtab = p->s_next; ! 104: else ! 105: q->s_next = p->s_next; ! 106: free(p->s_name); ! 107: free(p->s_val.p); ! 108: free(p); ! 109: return; ! 110: } ! 111: q = p; ! 112: } ! 113: yyerror("%s is not defined at this point", s); ! 114: }
This archive runs on limited infrastructure. Preserving old code on modern bandwidth. Automated agents are requested to crawl responsibly.