Advertisement
Guest User

Untitled

a guest
Sep 20th, 2017
70
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 6.16 KB | None | 0 0
  1. //      snusp.c
  2. //      
  3. //      Copyright 2011 linkboss <linkboss@Bender>
  4. //      
  5. //      This program is free software; you can redistribute it and/or modify
  6. //      it under the terms of the GNU General Public License as published by
  7. //      the Free Software Foundation; either version 2 of the License, or
  8. //      (at your option) any later version.
  9. //      
  10. //      This program is distributed in the hope that it will be useful,
  11. //      but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. //      MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  13. //      GNU General Public License for more details.
  14. //      
  15. //      You should have received a copy of the GNU General Public License
  16. //      along with this program; if not, write to the Free Software
  17. //      Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
  18. //      MA 02110-1301, USA.
  19.  
  20. #include <stdlib.h>
  21. #include <stdio.h>
  22. #define MEMORY_SIZE 32768
  23.  
  24. typedef struct context_s
  25. {
  26.     int x;
  27.     int y;
  28.     int or;
  29.     struct context_s *prev;
  30. } context;
  31.  
  32. int execStep(char command, char *memory, int *mp, context *currentContext);
  33.  
  34. int main(int argc, char **argv)
  35. {
  36.     char *filename = argv[argc-1];
  37.     //char *filename = "test.snu";
  38.     int  i, j, w, h;
  39.     int px, py, or; //Pointeurs d'instruction (x/y), orientation
  40.     int mp; //Pointeur mémoire
  41.     int stop = 0;
  42.     char **program;
  43.     char *memory;
  44.     context *currentContext, *newContext;
  45.    
  46.     //Vérification de l'existence d'un ficher en entrée
  47.     if(argc == 0)
  48.     {
  49.         printf("Usage : snusp <file>\n");
  50.         return 1;
  51.     }
  52.    
  53.    
  54.     //Lecture de la taille du programme et allocation de la mémoire
  55.     getProgramSize(filename, &w, &h);
  56.     program = malloc(h * sizeof(char*));
  57.     for(i = 0; i < h; i++)
  58.         program[i] = malloc(w * sizeof(char));
  59.    
  60.     //Chargement du programme depuis le fichier
  61.     loadProgram(filename, program, w, h);
  62.     printf("Width:%d Height:%d\n", w, h);
  63.    
  64.     //Lecture du point de départ du programme et erreur si non trouvé.
  65.     if(getStart(program, w, h, &px, &py))
  66.     {
  67.         printf("Error : no usable char found.\n");
  68.         free(program);
  69.         return 1;
  70.     }
  71.    
  72.     printf("Start X:%d Y:%d\n", px, py);
  73.    
  74.     //Initialisation de la mémoire (32 Ko)
  75.     memory = malloc(MEMORY_SIZE * sizeof(char));
  76.    
  77.     //Création du contexte de base
  78.     currentContext = malloc(sizeof(context));
  79.     currentContext->x = px;
  80.     currentContext->y = py;
  81.     currentContext->or = 8;
  82.    
  83.     //Initialisation du memory pointer
  84.     mp = 0;
  85.    
  86.     //Exécution du programme
  87.     while(!stop)
  88.     {
  89.         execStep(program[currentContext->y][currentContext->x], memory, &mp, currentContext);
  90.         movePointer(currentContext);
  91.         if(currentContext->x == w || currentContext->y == h)
  92.             stop = 1;
  93.        
  94.         //Affichage de celui-ci (à supprimer)
  95.         /*for(j = 0; j < h;j++)
  96.         {
  97.             for (i = 0; i < w; i++)
  98.             {
  99.                 if(j == currentContext->y && i == currentContext->x)
  100.                     printf("0");
  101.                 else
  102.                     printf("%c", program[j][i]);
  103.             }
  104.             printf("\n");
  105.         }
  106.         printf("\033[H\033[2J");
  107.         usleep(50000);*/
  108.     }
  109.    
  110.     return 0;
  111. }
  112.  
  113. void clean_stdin(void)
  114. {
  115.     int c;
  116.    
  117.     do {
  118.         c = getchar();
  119.     } while (c != '\n' && c != EOF);
  120. }
  121.  
  122. int execStep(char command, char *memory, int *mp, context *currentContext)
  123. {
  124.     switch(command)
  125.     {
  126.         case '+':
  127.             memory[*mp]++;
  128.             break;
  129.         case '-':
  130.             memory[*mp]--;
  131.             break;
  132.         case '>':
  133.             *mp++;
  134.             break;
  135.         case '<':
  136.             *mp--;
  137.             break;
  138.         case ',':
  139.             printf("> ");
  140.             memory[*mp] = getchar();
  141.             clean_stdin();
  142.             break;
  143.         case '.':
  144.             printf("%c ", memory[*mp]);
  145.             break;
  146.         case '!':
  147.             movePointer(currentContext);
  148.             break;
  149.         case '?':
  150.             if(memory[*mp] == 0)
  151.             {
  152.                 movePointer(currentContext);
  153.             }
  154.             break;
  155.         case '/':
  156.             changeOrientation(currentContext, 1);
  157.             break;
  158.         case '\\':
  159.             changeOrientation(currentContext, 2);
  160.             break;
  161.     }
  162.    
  163.     return 0;
  164. }
  165.  
  166. int changeOrientation(context *currentContext, int type)
  167. {
  168.     int t;
  169.    
  170.     switch(currentContext->or)
  171.     {
  172.         case 1:
  173.             if(type == 1)
  174.                 currentContext->or = 2;
  175.             else
  176.                 currentContext->or = 8;
  177.             break;
  178.         case 2:
  179.             if(type == 1)
  180.                 currentContext->or = 1;
  181.             else
  182.                 currentContext->or = 4;
  183.             break;
  184.         case 4:
  185.             if(type == 1)
  186.                 currentContext->or = 8;
  187.             else
  188.                 currentContext->or = 2;
  189.             break;
  190.         case 8:
  191.             if(type == 1)
  192.                 currentContext->or = 4;
  193.             else
  194.                 currentContext->or = 1;
  195.             break;
  196.     }
  197. }
  198.  
  199. int movePointer(context *currentContext)
  200. {
  201.     switch(currentContext->or)
  202.     {
  203.         case 8:
  204.             currentContext->x++;
  205.             break;
  206.         case 2:
  207.             currentContext->x--;
  208.             break;
  209.         case 1:
  210.             currentContext->y++;
  211.             break;
  212.         case 4:
  213.             currentContext->y--;
  214.             break;
  215.     }
  216.    
  217.     return 0;
  218. }
  219.  
  220. //Lecture de la taille du programme
  221. int getProgramSize(char *filename, int *w, int *h)
  222. {
  223.     FILE *fp = fopen(filename, "r");
  224.     int i, j, k, c;
  225.    
  226.     i = 0;
  227.     j = 0;
  228.     k = 0;
  229.    
  230.     while(c != EOF)
  231.     {
  232.         c = fgetc(fp);
  233.         if(c == '\n')
  234.         {
  235.             j++;
  236.             i = -1;
  237.            
  238.         }
  239.        
  240.         i++;
  241.         if(k < i)
  242.             k = i;
  243.     }
  244.    
  245.     *w = k;
  246.     *h = j;
  247.     fclose(fp);
  248.    
  249.     return 0;
  250. }
  251.  
  252. //Chargement du programme en mémoire
  253. int loadProgram(char *filename, char **program, int w, int h)
  254. {
  255.     FILE *fp = fopen(filename, "r");
  256.     int i, j, k, c;
  257.    
  258.     for(j = 0; j < h;j++)
  259.     {
  260.        
  261.         for (i = 0; i < w; i++)
  262.         {
  263.             c = fgetc(fp);
  264.            
  265.             if(c == '\n')
  266.                 i = w;
  267.             else
  268.                 program[j][i] = (char)c;
  269.         }
  270.        
  271.         //Si on est arrivé au bout de la boucle (i = w), on saute l'\n
  272.         if(i == w)
  273.             fgetc(fp);
  274.     }
  275.    
  276.     fclose(fp);
  277.    
  278.     return 0;
  279. }
  280.  
  281. //Récupère l'endroit de départ du programme (i.e. le *, ou le caractère le plus en haut à gauche)
  282. int getStart(char **program, int x, int y, int *sx, int *sy)
  283. {
  284.     int fx, fy, first;
  285.     int i, j;
  286.     char c;
  287.    
  288.     for(j = 0; j < y;j++)
  289.     {
  290.         for (i = 0; i < x; i++)
  291.         {
  292.             c = program[j][i];
  293.            
  294.            
  295.             if(c == '$')
  296.             {
  297.                 *sx = i+1;
  298.                 *sy = j;
  299.                 return 0;
  300.             }
  301.            
  302.             //Recherche du premier caractère exécutable s'il n'a pas été rencontré
  303.             if(!first)
  304.             {
  305.                 if(c == '>' || c == '<' || c == '+' || c == '-' || c == ',' || c == '.' || c == '/' || c == '\\' || c == '?' || c == '!')
  306.                 {
  307.                     first = 1;
  308.                     fx = i;
  309.                     fy = j;
  310.                 }
  311.             }
  312.         }
  313.     }
  314.    
  315.     if(first)
  316.     {
  317.         *sx = fx;
  318.         *sy = fy;
  319.         return 0;
  320.     }
  321.    
  322.     return 1;
  323. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement