summaryrefslogtreecommitdiffstats
path: root/src/cppvalue.cpp
blob: df404b901a32f4ed02e7a08dc2c42e7ab07a608f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
/******************************************************************************
 *
 * $Id$
 *
 *
 * Copyright (C) 1997-2000 by Dimitri van Heesch.
 *
 * Permission to use, copy, modify, and distribute this software and its
 * documentation under the terms of the GNU General Public License is hereby 
 * granted. No representations are made about the suitability of this software 
 * for any purpose. It is provided "as is" without express or implied warranty.
 * See the GNU General Public License for more details.
 *
 * All output generated with Doxygen is not covered by this license.
 *
 */

#include <stdlib.h>

#include "cppvalue.h"
#include "constexp.h"

CPPValue parseOctal()
{
  long val = 0;
  for (const char *p = strToken.data(); *p != 0; p++)
  {
    if (*p >= '0' && *p <= '7') val = val * 8 + *p - '0';
  }
  return CPPValue(val);
}

CPPValue parseDecimal()
{
  long val = 0;
  for (const char *p = strToken.data(); *p != 0; p++)
  {
    if (*p >= '0' && *p <= '9') val = val * 10 + *p - '0';
  }
  return CPPValue(val);
}

CPPValue parseHexadecimal()
{
  long val = 0;
  for (const char *p = strToken.data(); *p != 0; p++)
  {
    if      (*p >= '0' && *p <= '9') val = val * 16 + *p - '0';
    else if (*p >= 'a' && *p <= 'f') val = val * 16 + *p - 'a' + 10;
    else if (*p >= 'A' && *p <= 'F') val = val * 16 + *p - 'A' + 10;
  }
  return CPPValue(val);
}

CPPValue parseCharacter() // does not work for '\n' and the alike 
{
  if (strToken[1]=='\\')
  {
    switch(strToken[2])
    {
      case 'n':  return CPPValue((long)'\n');
      case 't':  return CPPValue((long)'\t');
      case 'v':  return CPPValue((long)'\v');
      case 'b':  return CPPValue((long)'\b');
      case 'r':  return CPPValue((long)'\r');
      case 'f':  return CPPValue((long)'\f');
      case 'a':  return CPPValue((long)'\a');
      case '\\': return CPPValue((long)'\\');
      case '?':  return CPPValue((long)'\?');
      case '\'': return CPPValue((long)'\'');
      case '"':  return CPPValue((long)'"');
      case '0':  return parseOctal();
      case 'x': 
      case 'X':  return parseHexadecimal();
      default:   printf("Invalid escape sequence %s found!\n",strToken.data()); 
                 return CPPValue(0L); 
    }
  }
  return CPPValue((long)strToken[1]);
}

CPPValue parseFloat()
{
  return CPPValue(atof(strToken));
}