abacus: parse: use BigNum instead of int

This commit is contained in:
Bruno BELANYI 2021-08-22 14:14:27 +02:00
parent f4f620436c
commit 60a0fbf5b0
5 changed files with 25 additions and 7 deletions

View file

@ -31,4 +31,7 @@ parser = library(
'parser-driver.hh', 'parser-driver.hh',
flex.process('scanner.ll'), flex.process('scanner.ll'),
bison.process('parser.yy'), bison.process('parser.yy'),
dependencies: [
bignum,
],
) )

View file

@ -28,4 +28,8 @@ yy::location const& ParserDriver::location() const {
return current_location_; return current_location_;
} }
ParserDriver::numeric_type const& ParserDriver::result() const {
return result_;
}
} // namespace abacus::parser } // namespace abacus::parser

View file

@ -4,10 +4,14 @@
#include "parser.hh" #include "parser.hh"
#include "bignum.hh" // FIXME: I would like `bignum/bignum.hh` path instead...
namespace abacus::parser { namespace abacus::parser {
class ParserDriver { class ParserDriver {
public: public:
using numeric_type = abacus::bignum::BigNum;
ParserDriver(); ParserDriver();
int parse(std::string filename); int parse(std::string filename);
@ -18,9 +22,10 @@ public:
yy::location& location(); yy::location& location();
yy::location const& location() const; yy::location const& location() const;
numeric_type const& result() const;
private: private:
// FIXME: will become BigNum numeric_type result_{0};
int result_ = 0;
std::string filename_{}; std::string filename_{};
yy::location current_location_{}; yy::location current_location_{};
bool parse_trace_p_ = false; bool parse_trace_p_ = false;

View file

@ -35,6 +35,8 @@
namespace abacus::parser { namespace abacus::parser {
class ParserDriver; class ParserDriver;
} // namespace abacus::parser } // namespace abacus::parser
#include "bignum.hh" // FIXME: I would like `bignum/bignum.hh` path instead...
} }
%code provides { %code provides {
@ -54,8 +56,7 @@ class ParserDriver;
%token EOF 0 "end-of-file" %token EOF 0 "end-of-file"
// FIXME:will become BigNum %token <abacus::bignum::BigNum> NUM "number"
%token <int> NUM "number"
// Use `<<` to print everything // Use `<<` to print everything
%printer { yyo << $$; } <*>; %printer { yyo << $$; } <*>;
@ -73,8 +74,7 @@ class ParserDriver;
%left TIMES DIVIDE %left TIMES DIVIDE
%precedence UNARY %precedence UNARY
// FIXME: will become BigNum %type <abacus::bignum::BigNum> input exp
%type <int> input exp
%% %%

View file

@ -1,4 +1,6 @@
%{ %{
#include <sstream>
#include "parser-driver.hh" #include "parser-driver.hh"
#include "parser.hh" #include "parser.hh"
%} %}
@ -41,7 +43,11 @@ int [0-9]+
"(" return yy::parser::make_LPAREN(loc); "(" return yy::parser::make_LPAREN(loc);
")" return yy::parser::make_RPAREN(loc); ")" return yy::parser::make_RPAREN(loc);
{int} return yy::parser::make_NUM(std::stoi(yytext), loc); {int} {
abacus::bignum::BigNum num;
std::stringstream(yytext) >> num;
return yy::parser::make_NUM(num, loc);
}
. { . {
using namespace yy; using namespace yy;