Commit 99ec95d8 authored by Han Young's avatar Han Young
Browse files

merge KNumber branch

parents 94d41ca4 236f6be2
......@@ -20,7 +20,7 @@ public:
driver();
std::map<std::string, int> variables;
double result = 0;
QString result;
int parse(const std::string expr);
// Whether to generate parser debug traces.
......
......@@ -32,7 +32,7 @@
%code requires {
# include <string>
#include <cmath>
# include <knumber.h>
class driver;
}
......@@ -72,15 +72,15 @@
ABS "ABS"
;
%token <double> NUMBER "number"
%nterm <double> exp
%nterm <double> factor
%token <KNumber> NUMBER "number"
%nterm <KNumber> exp
%nterm <KNumber> factor
%printer { yyo << $$; } <*>;
%printer { yyo << $$.toQString().toStdString(); } <*>;
%%
%start unit;
unit: exp { drv.result = $1; };
unit: exp { drv.result = $1.toQString(); };
%left "+" "-";
%left "*" "/";
......@@ -99,8 +99,8 @@ exp:
| "COS" "(" exp ")" { $$ = cos($3); }
| "TAN" "(" exp { $$ = tan($3); }
| "TAN" "(" exp ")" { $$ = tan($3); }
| "LOG" "(" exp { $$ = log($3); }
| "LOG" "(" exp ")" { $$ = log($3); }
| "LOG" "(" exp { $$ = ln($3); }
| "LOG" "(" exp ")" { $$ = ln($3); }
| "LOG10" "(" exp { $$ = log10($3); }
| "LOG10" "(" exp ")" { $$ = log10($3); }
| "LOG2" "(" exp { $$ = log2($3); }
......@@ -122,7 +122,7 @@ factor: "(" exp ")" { $$ = $2; }
| "number"
| "-" "number" { $$ = -$2; }
| "+" "number" { $$ = $2; }
| factor "%" { $$ = $1 / 100; }
| factor "%" { $$ = $1 / KNumber(100); }
;
%%
......
......@@ -26,6 +26,8 @@
# include <cstdlib>
# include <cstring> // strerror
# include <string>
# include <knumber.h>
# include <QString>
# include "driver.hh"
# include "parser.hh"
%}
......@@ -101,7 +103,7 @@
make_NUMBER (const std::string &s, const yy::parser::location_type& loc);
%}
double [0-9]+|([0-9]+)?"."[0-9]+
knumber [0-9]+|([0-9]+)?"."[0-9]+
%{
// Code run each time a pattern is matched.
......@@ -129,8 +131,8 @@ double [0-9]+|([0-9]+)?"."[0-9]+
"log10" return yy::parser::make_LOG10 (loc);
"log2" return yy::parser::make_LOG2 (loc);
"√" return yy::parser::make_SQUAREROOT (loc);
"π" return yy::parser::make_NUMBER (3.14159265358, loc);
"e" return yy::parser::make_NUMBER (2.71828182845, loc);
"π" return yy::parser::make_NUMBER (KNumber::Pi(), loc);
"e" return yy::parser::make_NUMBER (KNumber::Euler(), loc);
"%" return yy::parser::make_PERCENTAGE (loc);
"=" loc.step ();
"asin" return yy::parser::make_ASIN (loc);
......@@ -138,7 +140,7 @@ double [0-9]+|([0-9]+)?"."[0-9]+
"atan" return yy::parser::make_ATAN (loc);
"abs" return yy::parser::make_ABS (loc);
{double} return make_NUMBER (yytext, loc);
{knumber} return make_NUMBER (yytext, loc);
<<EOF>> return yy::parser::symbol_type (0, loc);
%%
......@@ -146,8 +148,8 @@ yy::parser::symbol_type
make_NUMBER (const std::string &s, const yy::parser::location_type& loc)
{
errno = 0;
double n = strtod (s.c_str(), NULL);
return yy::parser::make_NUMBER ((double) n, loc);
KNumber n = KNumber(QString::fromStdString(s));
return yy::parser::make_NUMBER ((KNumber) n, loc);
}
void
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment