Functions

#include <stdio.h>
#include "commons.h"

void array_decl(_bool* is_arraydecl)
{
  static bool is_open_sqbr = FALSE;
    if(tok == '[')
    {
      is_open_sqbr = TRUE;
      nextToken();
    }
    else if(tok == ']')
    {
        if(is_open_sqbr == FALSE)
        {
           printf("expected [ \n");
           exit(0);
        }
        *is_arraydecl = TRUE;
        is_open_sqbr = FALSE;
        nextToken();
        return;
    }
    else if(is_open_sqbr == TRUE) // expression
    {
        nextToken();
    }

    if(tok == EOF)
      return;
    else if(tok != ID || tok != ';')
      array_decl(is_arraydecl); 
}


void declaration_specifiers(_bool* is_decl_sp)
{
    if(tok == TYPEDEF || 
       tok == EXTERN || 
       tok == STATIC || 
       tok == AUTO || 
       tok == REGISTER)
    {
      nextToken();   
    }
    if(tok == CONST ||
      tok == VOLATILE)
    {
        
       nextToken(); 
    }
    
    if(tok == SIGNED ||
       tok == UNSIGNED)
    {

      nextToken();
    }

    if(tok == VOID || 
       tok == CHAR ||
       tok == SHORT || 
       tok == INT ||
       tok == LONG ||
       tok == FLOAT ||
       tok == DOUBLE ||
       tok == SIGNED ||
       tok == UNSIGNED
       )
       {
          *is_decl_sp = TRUE;
          nextToken(); 
       }
       else if(tok == STRUCT ||
         tok == UNION)
      {
           nextToken();
           if(tok == ID)
           {
              *is_decl_sp = TRUE;
              nextToken();
           }
          //struct_or_union();
      }
      else {
        printf("Type missing in declaration\n");
        exit(0);
      }
     while(tok == '*')
     {
        nextToken();
     }
     if(tok == '[')
     {
        array_decl(&is_validarr);

     }   

 
      if(tok == ID && *is_decl_sp == TRUE)
      {
         *is_decl_sp = TRUE;
         nextToken();

      }
      else {
        printf("Identifier missing in declaration\n");
        exit(0);
      }

 
   return;
}
void args(_bool *is_args)
{
 static _bool is_comma = FALSE;
   if(tok == '(')
   {
     *is_args = TRUE;
     nextToken();
   }
   else if(tok == ',')
   {
      is_comma = TRUE;
      nextToken();

   }
   else if(tok == ')')
   {
      if(is_comma == TRUE) // for case " int id ,)"
      {
          printf("expected identifier between , and ) \n");
          exit(0);
      }

      if(*is_args == FALSE)
      {
          printf("expected \'( \' \n");
          exit(0);
      } 


      nextToken();
      return; 
   }
   else
   {
      if( is_args == FALSE && is_comma == FALSE)
      {
         printf("expected , or ( before  \n");
         exit(0);
      }
      _bool paramter_var = FALSE;
      declaration_specifiers( &paramter_var);
      if(paramter_var == FALSE)
      {
         printf("Function argument is invalid\n");
         exit(0);
      }
      else
      {
        is_comma = FALSE;
      }
   }

   if(tok == ';')
     return;
   else if(tok == EOF)
   {
     printf(" ; expected!\n");
     exit(0);
   }
   else if(tok != ';')
    args(&is_args); 
}

void args_caller(_bool is_args)
{
   if(tok == '(')
   {
     *is_args = TRUE;
     nextToken();
   }
   else if(tok == ID)
   {
      nextToken();

   }
   else if(tok == ',')
   {
      nextToken();

   }
   else if(tok == ')')
   {
      if(*is_args == FALSE)
      {

      }


      nextToken();
      return;
   }

   if(tok != EOF)
    args(&is_args);
}

void block_start_statement(_bool *is_blk )
{
    if(tok == '{')
    {
        //statement_list();
          block_level++;
          *is_blk = TRUE;
          nextToken(); 
    }
    
}
void block_end_statement()
{
    if(tok == '}')
    {
        //statement_list();
          block_level--;
          nextToken();
    }

}



void function_definition_or_declaration(_bool *is_func_def, _bool *is_func_decl)
{
    _bool is_decl, is_args, is_blk , is_stmt;
    declaration_specifiers( &is_decl );
    if(is_decl == FALSE)
    {
       *is_func_def = FALSE;
       *is_func_decl = FALSE;
        return;
    }
    args(&is_args);
    block_start_statement( &is_blk);
    if( is_blk == TRUE)
    {
      statement_list( &is_stmt);
      block_end_statement();
      *is_func_def = TRUE;
    }
    else if(tok == ';')
    {
        *is_func_decl = TRUE;
    }

    if( block_level > 1)
    {
         printf("nested function is not allowed!");
         exit(0);
    }

}

void multiple_declaration(_bool *is_mdecl)
{
   
   declaration_specifiers(); 
   if(tok == ',')
   {
      is_comma = TRUE;
      while(tok != ';' && tok != EOF)
      {
         switch(tok)
         {
           case ',':
             if(is_comma == TRUE)
             {
                printf("Multiple commas notices \n");
                exit(0);
             }
            nextToken();
           break;
           case ID:
             if(is_comma == FALSE)
             {
                printf("illegal declarations!\n");
                exit(0);
             }
            is_comma = FALSE;
            nextToken();
           break;

      
         }         
      }
      if(tok == ';')
      {
         is_mdecl = TRUE;
         return;
      }
      else if(tok == EOF)
      {
         printf("; expected ! \n");
         exit(0);
      }
   }
}

void global_scope()
{
   while(tok != EOF)
   {
     multiple_declaration();
     function_definition_or_declaration(); 
   }
}

int main()
{
    arr[0] = STATIC;
    arr[1] = INT;
    arr[2] = ID;
    arr[3] = '(';
    arr[4] = ')';
    arr[5] = '{';
    arr[6] = '}';
    arr[7] = EOF;
     global_scope();
    return 0;
}

Comments