/**************************************************************************************
 * Chaga compiler                                                                     *
 * Copyright (C) 2026 Robert James Bruce                                              *
 *                                                                                    *
 * This file is part of the Chaga compiler.                                           *
 *                                                                                    *
 * The Chaga compiler is free software: you can redistribute it and/or modify it      *
 * under the terms of the GNU General Public License as published by the              *
 * Free Software Foundation, either version 3 of the License, or (at your option) any *
 * later version.                                                                     *
 *                                                                                    *
 * The Chaga compiler is distributed in the hope that it will be useful, but WITHOUT  *
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or              *
 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License               *
 * for more details.                                                                  *
 *                                                                                    *
 * You should have received a copy of the GNU General Public License                  *
 * along with the Chaga compiler. If not, see <https://www.gnu.org/licenses/>.        *
 **************************************************************************************/
#include "include/chaga_compiler-purge_comments.h"

void purge_comments (char *file_buffer, size_t file_buffer_size)
{
  size_t i, j, begin_comment, end_comment;
  int state;
  char byte;

  if (file_buffer != NULL)
  {
    i = 0;
    state = 0;
    while ((i < file_buffer_size) && (state < 999))
    {
      byte = file_buffer[i];
      switch (state)
      {
        case 0:
          if (byte == '\\')
          {
            state = 7;
          }
          else
          {
            if (byte == '"')
            {
              state = 5;
            }
            else
            {
              if (byte == '/')
              {
                begin_comment = i;
                state = 1;
              }
            }
          }
        break;
        case 1:
          if (byte == '*')
          {
            state = 2;
          }
          else
          {
            if (byte == '/')
            {
              state = 4;
            }
            else
            {
              state = 0;
            }
          } 
        break;
        case 2:
          if (byte == '*')
          {
             state = 3;
          }
        break;
        case 3:
          if (byte == '/')
          {
             end_comment = i;
             for (j = begin_comment; j <= end_comment; j++)
             {
               if (file_buffer[j] != '\n') // be sure to keep newline since they affect line numbering of source code. 
               {
                 file_buffer[j] = ' ';
               }
             }
             state = 0;
          }
          else
          {
            if (byte != '*')
            {
              state = 2;
            }
          }
        break;
        case 4:
          if (byte == '\n')
          {
            end_comment = i;
            for (j = begin_comment; j < end_comment; j++)
            {
              file_buffer[j] = ' ';
            }
            state = 0;
          }
        break;
        case 5:
          if (byte == '\\')
          {
            state = 6;
          }
          else
          {
            if (byte == '"')
            {
              state = 0;
            }
          }
        break;
        case 6:
          state = 5;
        break;
        case 7:
          state = 0;
        break;
        default:
        break;
      }
      i++;
    }
  }
}
