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',
flex.process('scanner.ll'),
bison.process('parser.yy'),
dependencies: [
bignum,
],
)

View File

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

View File

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

View File

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

View File

@ -1,4 +1,6 @@
%{
#include <sstream>
#include "parser-driver.hh"
#include "parser.hh"
%}
@ -41,7 +43,11 @@ int [0-9]+
"(" return yy::parser::make_LPAREN(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;