|
|
1.1 ! root 1: #include <stdio.h> ! 2: #include "grap.h" ! 3: #include "y.tab.h" ! 4: ! 5: typedef struct { ! 6: Obj *var; /* index variable */ ! 7: double to; /* limit */ ! 8: double by; ! 9: int op; /* operator */ ! 10: char *str; /* string to push back */ ! 11: } For; ! 12: ! 13: #define MAXFOR 10 ! 14: ! 15: For forstk[MAXFOR]; /* stack of for loops */ ! 16: For *forp = forstk; /* pointer to current top */ ! 17: ! 18: forloop(var, from, to, op, by, str) /* set up a for loop */ ! 19: Obj *var; ! 20: double from, to, by; ! 21: int op; ! 22: char *str; ! 23: { ! 24: fprintf(tfd, "# for %s from %g to %g by %c %g \n", ! 25: var->name, from, to, op, by); ! 26: if (++forp >= forstk+MAXFOR) ! 27: fatal("for loop nested too deep"); ! 28: forp->var = var; ! 29: forp->to = to; ! 30: forp->op = op; ! 31: forp->by = by; ! 32: forp->str = str; ! 33: setvar(var, from); ! 34: nextfor(); ! 35: unput('\n'); ! 36: } ! 37: ! 38: nextfor() /* do one iteration of a for loop */ ! 39: { ! 40: /* BUG: this should depend on op and direction */ ! 41: if (forp->var->fval > SLOP * forp->to) { /* loop is done */ ! 42: free(forp->str); ! 43: if (--forp < forstk) ! 44: fatal("forstk popped too far"); ! 45: } else { /* another iteration */ ! 46: pushsrc(String, "\nEndfor\n"); ! 47: pushsrc(String, forp->str); ! 48: } ! 49: } ! 50: ! 51: endfor() /* end one iteration of for loop */ ! 52: { ! 53: switch (forp->op) { ! 54: case '+': ! 55: case ' ': ! 56: forp->var->fval += forp->by; ! 57: break; ! 58: case '-': ! 59: forp->var->fval -= forp->by; ! 60: break; ! 61: case '*': ! 62: forp->var->fval *= forp->by; ! 63: break; ! 64: case '/': ! 65: forp->var->fval /= forp->by; ! 66: break; ! 67: } ! 68: nextfor(); ! 69: } ! 70: ! 71: char *ifstat(expr, thenpart, elsepart) ! 72: double expr; ! 73: char *thenpart, *elsepart; ! 74: { ! 75: dprintf("if %g then <%s> else <%s>\n", expr, thenpart, elsepart? elsepart : ""); ! 76: if (expr) { ! 77: unput('\n'); ! 78: pushsrc(Free, thenpart); ! 79: pushsrc(String, thenpart); ! 80: unput('\n'); ! 81: if (elsepart) ! 82: free(elsepart); ! 83: return thenpart; /* to be freed later */ ! 84: } else { ! 85: free(thenpart); ! 86: if (elsepart) { ! 87: unput('\n'); ! 88: pushsrc(Free, elsepart); ! 89: pushsrc(String, elsepart); ! 90: unput('\n'); ! 91: } ! 92: return elsepart; ! 93: } ! 94: }
This archive runs on limited infrastructure. Preserving old code on modern bandwidth. Automated agents are requested to crawl responsibly.