Blame src/cppvalue.cpp

Packit Service 50c9f2
/******************************************************************************
Packit Service 50c9f2
 *
Packit Service 50c9f2
 * 
Packit Service 50c9f2
 *
Packit Service 50c9f2
 *
Packit Service 50c9f2
 * Copyright (C) 1997-2015 by Dimitri van Heesch.
Packit Service 50c9f2
 *
Packit Service 50c9f2
 * Permission to use, copy, modify, and distribute this software and its
Packit Service 50c9f2
 * documentation under the terms of the GNU General Public License is hereby 
Packit Service 50c9f2
 * granted. No representations are made about the suitability of this software 
Packit Service 50c9f2
 * for any purpose. It is provided "as is" without express or implied warranty.
Packit Service 50c9f2
 * See the GNU General Public License for more details.
Packit Service 50c9f2
 *
Packit Service 50c9f2
 * Documents produced by Doxygen are derivative works derived from the
Packit Service 50c9f2
 * input used in their production; they are not affected by this license.
Packit Service 50c9f2
 *
Packit Service 50c9f2
 */
Packit Service 50c9f2
Packit Service 50c9f2
#include <stdlib.h>
Packit Service 50c9f2
Packit Service 50c9f2
#include "cppvalue.h"
Packit Service 50c9f2
#include "constexp.h"
Packit Service 50c9f2
Packit Service 50c9f2
CPPValue parseOctal()
Packit Service 50c9f2
{
Packit Service 50c9f2
  long val = 0;
Packit Service 50c9f2
  for (const char *p = g_strToken.data(); *p != 0; p++)
Packit Service 50c9f2
  {
Packit Service 50c9f2
    if (*p >= '0' && *p <= '7') val = val * 8 + *p - '0';
Packit Service 50c9f2
  }
Packit Service 50c9f2
  return CPPValue(val);
Packit Service 50c9f2
}
Packit Service 50c9f2
Packit Service 50c9f2
CPPValue parseDecimal()
Packit Service 50c9f2
{
Packit Service 50c9f2
  long val = 0;
Packit Service 50c9f2
  for (const char *p = g_strToken.data(); *p != 0; p++)
Packit Service 50c9f2
  {
Packit Service 50c9f2
    if (*p >= '0' && *p <= '9') val = val * 10 + *p - '0';
Packit Service 50c9f2
  }
Packit Service 50c9f2
  return CPPValue(val);
Packit Service 50c9f2
}
Packit Service 50c9f2
Packit Service 50c9f2
CPPValue parseHexadecimal()
Packit Service 50c9f2
{
Packit Service 50c9f2
  long val = 0;
Packit Service 50c9f2
  for (const char *p = g_strToken.data(); *p != 0; p++)
Packit Service 50c9f2
  {
Packit Service 50c9f2
    if      (*p >= '0' && *p <= '9') val = val * 16 + *p - '0';
Packit Service 50c9f2
    else if (*p >= 'a' && *p <= 'f') val = val * 16 + *p - 'a' + 10;
Packit Service 50c9f2
    else if (*p >= 'A' && *p <= 'F') val = val * 16 + *p - 'A' + 10;
Packit Service 50c9f2
  }
Packit Service 50c9f2
  //printf("parseHexadecimal %s->%x\n",g_strToken.data(),val);
Packit Service 50c9f2
  return CPPValue(val);
Packit Service 50c9f2
}
Packit Service 50c9f2
Packit Service 50c9f2
CPPValue parseCharacter() // does not work for '\n' and the alike 
Packit Service 50c9f2
{
Packit Service 50c9f2
  if (g_strToken[1]=='\\')
Packit Service 50c9f2
  {
Packit Service 50c9f2
    switch(g_strToken[2])
Packit Service 50c9f2
    {
Packit Service 50c9f2
      case 'n':  return CPPValue((long)'\n');
Packit Service 50c9f2
      case 't':  return CPPValue((long)'\t');
Packit Service 50c9f2
      case 'v':  return CPPValue((long)'\v');
Packit Service 50c9f2
      case 'b':  return CPPValue((long)'\b');
Packit Service 50c9f2
      case 'r':  return CPPValue((long)'\r');
Packit Service 50c9f2
      case 'f':  return CPPValue((long)'\f');
Packit Service 50c9f2
      case 'a':  return CPPValue((long)'\a');
Packit Service 50c9f2
      case '\\': return CPPValue((long)'\\');
Packit Service 50c9f2
      case '?':  return CPPValue((long)'\?');
Packit Service 50c9f2
      case '\'': return CPPValue((long)'\'');
Packit Service 50c9f2
      case '"':  return CPPValue((long)'"');
Packit Service 50c9f2
      case '0':  // fall through
Packit Service 50c9f2
      case '1':  // fall through
Packit Service 50c9f2
      case '2':  // fall through
Packit Service 50c9f2
      case '3':  // fall through
Packit Service 50c9f2
      case '4':  // fall through
Packit Service 50c9f2
      case '5':  // fall through
Packit Service 50c9f2
      case '6':  // fall through
Packit Service 50c9f2
      case '7':  // fall through
Packit Service 50c9f2
                 return parseOctal();
Packit Service 50c9f2
      case 'x': 
Packit Service 50c9f2
      case 'X':  return parseHexadecimal();
Packit Service 50c9f2
      default:   printf("Invalid escape sequence %s found!\n",g_strToken.data()); 
Packit Service 50c9f2
                 return CPPValue(0L); 
Packit Service 50c9f2
    }
Packit Service 50c9f2
  }
Packit Service 50c9f2
  return CPPValue((long)g_strToken[1]);
Packit Service 50c9f2
}
Packit Service 50c9f2
Packit Service 50c9f2
CPPValue parseFloat()
Packit Service 50c9f2
{
Packit Service 50c9f2
  return CPPValue(atof(g_strToken));
Packit Service 50c9f2
}