summaryrefslogtreecommitdiff
path: root/expr.l
diff options
context:
space:
mode:
authorTobias Klauser <tklauser@distanz.ch>2008-07-01 16:45:28 +0200
committerTobias Klauser <tklauser@xenon.tklauser.home>2008-07-01 16:45:28 +0200
commit2ada69fe971ecc8881d5eb01dfbf337996472c40 (patch)
treecfb5e62fb3b48399770c292ab73e0b16698de366 /expr.l
Initial import
Diffstat (limited to 'expr.l')
-rw-r--r--expr.l154
1 files changed, 154 insertions, 0 deletions
diff --git a/expr.l b/expr.l
new file mode 100644
index 0000000..a94cfe3
--- /dev/null
+++ b/expr.l
@@ -0,0 +1,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_yacc.h"
+#include "expr.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;
+}
+
+