summaryrefslogtreecommitdiff
path: root/src/expr.l
blob: aa6f53840e692afdc9665234aac8fcc50507337a (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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
%{
/* $Id: expr.l,v 1.4 2006-11-05 04:42:43 ganzhorn Exp $
 * This file is part of lfhex.
 * Copyright (C) 2006 Salem Ganzhorn <eyekode@yahoo.com>
 *
 * This program 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 version 2.
 *
 * This program 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 this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
 */
#include <math.h>

#include <string>
#include <stdexcept>
#include <iostream>

using namespace std;

#include "expr.h"
#include "expr_yacc.h"

int exprparse();

extern off_t expr_value;

static off_t hex2l( const string &str );

#define yylval exprlval

%}

%option noyywrap

hexnum   [0-9a-fA-F]+

%%

[1-9][0-9]* {
               yylval = atol(yytext);
               return NUMBER;
            }
{hexnum} |
0x{hexnum}  {
               string hexnum = yytext;
               if( hexnum.substr(0,2) == "0x" ) {
                 hexnum = hexnum.substr(2);
               }
               yylval = hex2l(hexnum);
               return NUMBER;
            }
[-+*()]     {  return yytext[0]; }
[ \r\n\t]   {/*nop*/}
.           {  return yytext[0]; }

%%

static off_t 
hex2l( 
  const string &str
  )
{
  //
  off_t ret = 0;
  size_t scale = 1;
  int i = str.size()-1;
  while( i > -1 ) {
    switch( str[i] ) {
      case '0':
      break;
      case '1':
        ret += 1*scale;
      break;
      case '2':
        ret += 2*scale;
      break;
      case '3':
        ret += 3*scale;
      break;
      case '4':
        ret += 4*scale;
      break;
      case '5': 
        ret += 5*scale;
      break;
      case '6':
        ret += 6*scale;
      break;
      case '7':
        ret += 7*scale;
      break;
      case '8':
        ret += 8*scale;
      break;
      case '9':
        ret += 9*scale;
      break;
      case 'a': 
      case 'A':
        ret += 10*scale;
      break;
      case 'b':
      case 'B':
        ret += 11*scale;
      break;
      case 'c': 
      case 'C':
        ret += 12*scale;
      break;
      case 'd':
      case 'D':
        ret += 13*scale;
      break;
      case 'e':
      case 'E':
        ret += 14*scale;
      break;
      case 'f':
      case 'F':
        ret += 15*scale;
      break;
      default: 
        char buf[1024];
        sprintf(buf,"Unknown hex char: \"%s\"[%d]",str.c_str(),i);
        throw runtime_error(buf);
    }
    --i;
    scale *= 16;
  }
  return ret;
}

bool
expr_eval(
  const string &str,
  off_t     &retval
  )
{
//  exprrestart(NULL);
  expr_scan_string(str.c_str());
  expr_value = 0;
  bool status = !exprparse();
  retval = expr_value;
  return status;
}