Questo sito utilizza cookies solo per scopi di autenticazione sul sito e nient'altro. Nessuna informazione personale viene tracciata. Leggi l'informativa sui cookies.
Username: Password: oppure
Bonds Simple Basic Compiler  - solver.c

solver.c

Caricato da:
Scarica il programma completo

  1. /***************************************************************************
  2.  *   Copyright (C) 2008 by Mattia Bondanza                                 *
  3.  *   mattia.bond@gmail.com                                                 *
  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                         *
  17.  *   Free Software Foundation, Inc.,                                       *
  18.  *   59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.             *
  19.  ***************************************************************************/
  20.  
  21. #include "solver.h"
  22.  
  23. typedef struct stackN{//PILA DI CARATTERI USATA IN 'convert'
  24.         int val;
  25.         struct stackN *nextPrt;
  26. }stack_char;
  27. typedef stack_char *stack_charPrt;
  28.  
  29. typedef struct stack1{//PILA DI DOUBLE USATA IN 'solve'
  30.         int val;
  31.         struct stack1 *nextPrt;
  32. }stack;
  33. typedef stack *stackPrt;
  34.  
  35. void _push( stack_charPrt *stack, char value ){//PUSH PER PILA DI CARATTERI
  36.         stack_charPrt node = malloc( sizeof( stack_char ) );
  37.         if( node == NULL ){
  38.                 printf("NO MEMORY");
  39.                 exit( EXIT_FAILURE );
  40.         }
  41.         node->nextPrt = *stack;
  42.         node->val = value;
  43.         *stack = node;
  44. }
  45.  
  46. char _pop( stack_charPrt *stack ){//POP PER PILA DI CARATTERI
  47.         if( stack == NULL ){
  48.                 printf("Pila vuota");
  49.                 exit( EXIT_FAILURE );
  50.         }
  51.         stack_charPrt tmp = *stack;
  52.         char out = ( tmp )->val;
  53.         *stack = ( tmp )->nextPrt;
  54.         free( tmp );
  55.         return out;
  56. }
  57.  
  58. void push( stackPrt *stack, int value ){//PUSH PER PILA DI DOUBLE
  59.         stackPrt node = malloc( sizeof( stack ) );
  60.         if( node == NULL ){
  61.                 printf("NO MEMORY");
  62.                 exit( EXIT_FAILURE );
  63.         }
  64.         node->nextPrt = *stack;
  65.         node->val = value;
  66.         *stack = node;
  67. }
  68.  
  69. int pop( stackPrt *stack ){//POP PER PILA DI DOUBLE
  70.         if( stack == NULL ){
  71.                 printf("Pila vuota");
  72.                 exit( EXIT_FAILURE );
  73.         }    
  74.         stackPrt tmp = *stack;
  75.         int out = ( tmp )->val;
  76.         *stack = ( tmp )->nextPrt;
  77.         free( tmp );
  78.         return out;
  79. }
  80.  
  81.  
  82.  
  83. int isOperator( char c ){
  84.         if( c == '+'||c == '-'||c == '*'||c == '/'||c == '^'||c == '%' )return 1;
  85.         else return 0;
  86. }
  87.  
  88. int priority( char a, char b ){
  89.         int a1, b1;
  90.         switch( a ){
  91.                 case '+':
  92.                 case '-':
  93.                         a1 = 1;
  94.                         break;
  95.                 case '*':
  96.                 case '/':
  97.                 case '%':
  98.                         a1 = 2;
  99.                         break;
  100.                 case '^':
  101.                         a1 = 3;
  102.                         break;
  103.                 case '(':
  104.                         a1 = 0;
  105.         }
  106.         switch( b ){
  107.                 case '+':
  108.                 case '-':
  109.                         b1 = 1;
  110.                         break;
  111.                 case '*':
  112.                 case '/':
  113.                 case '%':
  114.                         b1 = 2;
  115.                         break;
  116.                 case '^':
  117.                         b1 = 3;
  118.                         break;
  119.                 case '(':
  120.                         b1 = 0;
  121.         }
  122.         return b1-a1;
  123. }
  124.  
  125. void convert( char input[], char output[] ){
  126.         int i, j;
  127.         stack_charPrt stack;
  128.         _push( &stack, '(' );
  129.         for( i = 0; input[ i ] != '\0'; i++ );
  130.         input[ i ] = ')';
  131.         input[ ++i ] = '\0';
  132.         for( i = 0, j = 0; input[ i ] != '\0'; i++){
  133.                 if( isdigit( input[ i ] ) || isalpha( input[i ] )){
  134.                         output[ j++ ] = input[ i ];
  135.                         while( isdigit( input[ i + 1 ] ) || isVariantch(input[i+1]) )
  136.                                 output[ j++ ] = input[ ++i ];
  137.                         output[ j++ ] = ' ';
  138.                 }
  139.                 else if( input[ i ] == '(' ){
  140.                         _push( &stack, '(' );
  141.                 }
  142.                 else if( isOperator( input[ i ] ) ){
  143.                         while( priority( input[ i ], stack->val ) >= 0 ){
  144.                                 output[ j++ ] = _pop( &stack );
  145.                                 output[ j++ ] = ' ' ;
  146.                         }
  147.                          _push( &stack, input[ i ] );
  148.                 }
  149.                 else if( input[ i ] == ')' ){
  150.                         while( stack->val != '(' ){
  151.                                 output[ j++ ] = _pop( &stack );
  152.                                 output[ j++ ] = ' ';
  153.                         }
  154.                         _pop( &stack );
  155.                 }
  156.         }
  157.         output[ j ] ='\0';
  158. }
  159.  
  160. int solve( char in[] ){
  161.         stackPrt stack;
  162.         int i, frst=0;
  163.         char input[500], tmp[500];
  164.         strcpy(tmp, in);
  165.         convert(tmp, input);
  166.         for( i = 0; input[ i ] != '\0'; i++ ){
  167.                 if(input[i]=='p'&&input[i+1]=='i'){
  168.                         push(&stack, M_PI);
  169.                         i+=2;
  170.                 }
  171.                 else if( isdigit( input[ i ] ) ){
  172.                         char numero[ 20 ];
  173.                         int k = 1;
  174.                         numero[ 0 ] = input[ i ];
  175.                         while( isdigit( input[ i + 1 ] ) )
  176.                                 numero[ k++ ] = input[ ++i ];
  177.                         numero[k]='\0';
  178.                         push( &stack, ricerca( 1, atoi( numero ), "", 0 ) );
  179.                 }
  180.                 else if( isalpha( input[ i ] ) ){
  181.                         char variant[50], indice[50];
  182.                         int k =0, cnt0=0, isarray=0;
  183.                         while( isVariantch( input[ i ] ) ){
  184.                                 variant[k++]=input[i++];
  185.                         }
  186.                         variant[k]='\0';
  187.                         push( &stack, ricerca( 0, 0, variant, 0 ) );
  188.                        
  189.                 }
  190.                 else if( isOperator( input[ i ] ) ){
  191.                         int ftc;
  192.                         int b;
  193.                         b = pop( &stack );
  194.                         int a = pop( &stack );
  195.                         fprintf( output, "%.5d 20%.5d\n", line_out++, a );
  196.                         switch( input[ i ] ){
  197.                                 case '+':
  198.                                         fprintf( output, "%.5d 30%.5d\n", line_out++, b );
  199.                                         fprintf( output, "%.5d 21%.5d\n", line_out++, location_cnt-- );
  200.                                         push( &stack, location_cnt+1);
  201.                                         break;
  202.                                 case '-':
  203.                                         fprintf( output, "%.5d 31%.5d\n", line_out++, b );
  204.                                         fprintf( output, "%.5d 21%.5d\n", line_out++, location_cnt-- );
  205.                                         push( &stack, location_cnt+1);
  206.                                         break;
  207.                                 case '*':
  208.                                         fprintf( output, "%.5d 33%.5d\n", line_out++, b );
  209.                                         fprintf( output, "%.5d 21%.5d\n", line_out++, location_cnt-- );
  210.                                         push( &stack, location_cnt+1);
  211.                                         break;
  212.                                 case '/':
  213.                                         fprintf( output, "%.5d 32%.5d\n", line_out++, b );
  214.                                         fprintf( output, "%.5d 21%.5d\n", line_out++, location_cnt-- );
  215.                                         push( &stack, location_cnt+1);;
  216.                                         break;  
  217.                                 case '^':
  218.                                         fprintf( output, "%.5d 35%.5d\n", line_out++, b );
  219.                                         fprintf( output, "%.5d 21%.5d\n", line_out++, location_cnt-- );
  220.                                         push( &stack, location_cnt+1);
  221.                                         break;
  222.                                 case '%':
  223.                                         fprintf( output, "%.5d 34%.5d\n", line_out++, b );
  224.                                         fprintf( output, "%.5d 21%.5d\n", line_out++, location_cnt-- );
  225.                                         push( &stack, location_cnt+1);
  226.                                         break;
  227.                         }
  228.                 }
  229.         }
  230.         return pop( &stack );
  231. }