Advertisement
Guest User

Segfault in intepreter

a guest
Jan 14th, 2011
131
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 6.56 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3.  
  4. #define INCREASE 30
  5.  
  6. FILE *source;
  7. char *prgnm;
  8.  
  9. struct size {
  10.     int used;
  11.     int available;
  12. } portsize;
  13.  
  14. char *stack;
  15. unsigned long sp, stacksize;
  16.  
  17. struct prgpnt {
  18.     unsigned long x;
  19.     unsigned long y;
  20.     unsigned long dirx;
  21.     unsigned long diry;
  22. } pc;
  23.  
  24. char mode;
  25.  
  26. struct portal {
  27.     int x;
  28.     int y;
  29. } *portals;
  30.  
  31. int sc;
  32.  
  33. void die (char *err) {
  34.     fprintf(stderr, "%s: %s\n", prgnm, err);
  35.     exit(1);
  36. }
  37.  
  38. char **playfield;
  39.  
  40. struct playfieldsize {
  41.     unsigned long y;
  42.     unsigned long x;
  43. } playfieldsize;
  44.  
  45. int isLet (int y, int x) {
  46.     if (playfield[y][x] > 64 && playfield[y][x] < 91 || playfield[y][x] > 96 && playfield[y][x] < 123) {
  47.         return 1;
  48.     } else {
  49.         return 0;
  50.     }
  51. }
  52.  
  53. void nextstack () {
  54.     if (++sp == stacksize) {
  55.         char *tmp;
  56.         if (tmp = realloc(stack, sizeof(char) * (stacksize += INCREASE)))
  57.             stack = tmp;
  58.         else
  59.             die("Not enough memory");
  60.     }
  61. }
  62.  
  63. int main (int argc, char *argv[]) {
  64.     prgnm = argv[0];
  65.  
  66.     if (argc == 2) {
  67.         if (!(source = fopen(argv[1], "r"))) {
  68.             perror(prgnm);
  69.             exit(1);
  70.         }
  71.  
  72.     } else if (argc == 1) {
  73.         source = stdin;
  74.     } else {
  75.         die("Command line error");
  76.     }
  77.  
  78.     unsigned long x, y;
  79.  
  80.     playfieldsize.y = playfieldsize.x = y = x = 0;
  81.  
  82.     struct portal *tmp;
  83.     if (tmp = malloc(sizeof(struct portal) * 50)) {
  84.         portals = tmp;
  85.         portsize.available = 50;
  86.         portsize.used = 0;
  87.     }
  88.  
  89.     char c;
  90.     while ((c = getc(source))!=EOF) {
  91.         if (!isascii(c) && !isprint(c)) {
  92.             die("Illegal character in program. The program must only contian printable ASCII characters.");
  93.         } else if (c == '$') {
  94.             pc.x = x;
  95.             pc.y = y;
  96.             pc.dirx = 1;
  97.             pc.diry = 0;
  98.         } else if (c == '@') {
  99.             if (portsize.used >= portsize.available) {
  100.                 if (tmp = realloc(portals, sizeof(struct portal) * (portsize.available += INCREASE)))
  101.                     portals = tmp;
  102.                 else
  103.                     die("Not enough initial memory");
  104.             }
  105.             portals[portsize.used].x = x;
  106.             portals[portsize.used].y = y;
  107.             portsize.used++;
  108.         }
  109.  
  110.         if (c == '\n') {
  111.             x=0;
  112.             y++;
  113.         } else {
  114.             unsigned long cnt;
  115.             if (y >= playfieldsize.y) { // if the array isn't large enough
  116.                 char **tmp;
  117.                 unsigned long cnt;
  118.                 cnt = playfieldsize.y; // stores the old size of the array
  119.                 playfieldsize.y += (y - playfieldsize.y) + INCREASE; // the new array size
  120.                 if (tmp = realloc(playfield, playfieldsize.y * sizeof(char *))) { // if storage can be reallocated for the new size
  121.                     playfield = tmp;
  122.                     for (; cnt<playfieldsize.y; cnt++) { // for every new column a row is added
  123.                         char *tmp;
  124.                         printf("cnt=%lisize=%li\n", cnt, playfieldsize.y); // added for debugging purpose
  125.                         if (tmp = realloc(playfield[cnt], sizeof(char) * playfieldsize.x)) // segfault happens at this line
  126.                             playfield[cnt] = tmp;
  127.                         else
  128.                             die("Not enough initial memory");
  129.                     }
  130.                 } else // if storage could not be reallocated
  131.                     die("Not enough initial memory");
  132.             }
  133.             if (x >= playfieldsize.x) {
  134.                 char *tmp;
  135.                 playfieldsize.x += x - playfieldsize.x + INCREASE;
  136.                 for (cnt = 0; cnt < playfieldsize.y; cnt++) {
  137.                     if (tmp = realloc(playfield[cnt], sizeof(char) * playfieldsize.x))
  138.                         playfield[cnt] = tmp;
  139.                     else
  140.                         die("Not enough initial memory");
  141.                 }
  142.             }
  143.             playfield[y][x] = c;
  144.  
  145.             x++;
  146.         }
  147.     }
  148.  
  149.     char *tmp2;
  150.     if (tmp2 = malloc(sizeof(char) * 50)) {
  151.         stack = tmp2;
  152.         stacksize = 50;
  153.         sp = 0;
  154.     }
  155.  
  156.     while (playfield[pc.y][pc.x] != '#') {
  157.         playfield[pc.y+=pc.diry][pc.x+=pc.dirx];
  158.  
  159.         if (mode == 's') { /* if string mode */
  160.             char string[1000];
  161.  
  162.             if (playfield[pc.y][pc.x] == '"') {
  163.                 nextstack();
  164.                 stack[sp] = 0;
  165.                 while (!(sc < 0)) {
  166.                     nextstack();
  167.                     stack[sp] = string[sc];
  168.                     --sc;
  169.                 }
  170.                 mode = 0;
  171.             } else {
  172.                 string[sc]=playfield[pc.y][pc.x];
  173.                 sc++;
  174.             }
  175.         } else {
  176.             switch (playfield[pc.y][pc.x]) {
  177.                 case '/':
  178.                     if (pc.dirx) {
  179.                         pc.diry -= pc.dirx;
  180.                         pc.dirx = 0;
  181.                     } else {
  182.                         pc.dirx -= pc.diry;
  183.                         pc.diry = 0;
  184.                     }
  185.                     break;
  186.                 case '\\': ;
  187.                     unsigned long tmp;
  188.                     tmp = pc.diry;
  189.                     pc.diry = pc.dirx;
  190.                     pc.dirx = tmp;
  191.                     break;
  192.                 case '>':
  193.                     nextstack();
  194.                     break;
  195.                 case '<':
  196.                     --sp;
  197.                     break;
  198.                 case '+':
  199.                     ++stack[sp];
  200.                     break;
  201.                 case '-':
  202.                     --stack[sp];
  203.                     break;
  204.                 case '.':
  205.                     printf("[%i]",stack[sp]);
  206.                     putchar(stack[sp]);
  207.                     break;
  208.                 case ',':
  209.                     stack[sp] = getchar();
  210.                     break;
  211.                 case '!':
  212.                     playfield[pc.y+=pc.diry][pc.x+=pc.dirx];
  213.                     break;
  214.                 case '|':
  215.                     pc.y = -(pc.y);
  216.                     pc.x = -(pc.x);
  217.                     break;
  218.                 case '"':
  219.                     mode = 's';
  220.                     break;
  221.             }
  222.         }
  223.     }
  224.  
  225. /*    x = y = 0;
  226.     for (y=0; y<playfieldsize.y; y++) {
  227.         for (x=0; x<playfieldsize.x; x++) {
  228.             if (playfield[y][x])
  229.                 printf("[%c]",playfield[y][x]);
  230.             else
  231.                 printf("[ ]");
  232.         }
  233.         printf("\n");
  234.     }*/
  235. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement