xo-reader: simplify f64 token handling

This commit is contained in:
Roland Conybeare 2024-08-09 20:28:58 -04:00
commit b8b9efd633
7 changed files with 40 additions and 36 deletions

View file

@ -19,6 +19,10 @@ namespace xo {
static std::unique_ptr<expect_expr_xs> expect_rhs_expression();
virtual void on_f64_token(const token_type & tk,
exprstatestack * p_stack,
rp<Expression> * p_emit_expr) override;
private:
};

View file

@ -96,8 +96,10 @@ namespace xo {
virtual bool admits_leftparen() const;
/** truee iff this parsing state admits a rightparen ')' as next token **/
virtual bool admits_rightparen() const;
#ifdef OBSOLETE
/** true iff this parsing state admits a 64-bit floating point literal token **/
virtual bool admits_f64() const;
#endif
/** update exprstate in response to incoming token @p tk,
* forward instructions to parent parser

View file

@ -31,16 +31,14 @@ namespace xo {
static std::unique_ptr<paren_xs> lparen_0();
#ifdef OBSOLETE
virtual bool admits_definition() const override;
#endif
bool admits_f64() const;
virtual bool admits_symbol() const override;
virtual bool admits_colon() const override;
virtual bool admits_semicolon() const override;
virtual bool admits_singleassign() const override;
virtual bool admits_leftparen() const override;
virtual bool admits_rightparen() const override;
virtual bool admits_f64() const override;
virtual void on_expr(ref::brw<Expression> expr,
exprstatestack * p_stack,

View file

@ -22,16 +22,14 @@ namespace xo {
static std::unique_ptr<progress_xs> make(rp<Expression> valex);
#ifdef OBSOLETE
virtual bool admits_definition() const override;
#endif
bool admits_f64() const;
virtual bool admits_symbol() const override;
virtual bool admits_colon() const override;
virtual bool admits_semicolon() const override;
virtual bool admits_singleassign() const override;
virtual bool admits_leftparen() const override;
virtual bool admits_rightparen() const override;
virtual bool admits_f64() const override;
virtual void on_expr(ref::brw<Expression> expr,
exprstatestack * p_stack,

View file

@ -425,12 +425,7 @@ namespace xo {
constexpr const char * self_name = "exprstate::on_f64";
if (!this->admits_f64())
{
this->illegal_input_error(self_name, tk);
}
assert(false);
this->illegal_input_error(self_name, tk);
}
void

View file

@ -4,8 +4,12 @@
*/
#include "expect_expr_xs.hpp"
#include "progress_xs.hpp"
#include "xo/expression/Constant.hpp"
namespace xo {
using xo::ast::Constant;
namespace scm {
std::unique_ptr<expect_expr_xs>
@ -18,6 +22,26 @@ namespace xo {
: exprstate(exprstatetype::expect_rhs_expression)
{}
void
expect_expr_xs::on_f64_token(const token_type & tk,
exprstatestack * p_stack,
rp<Expression> * /*p_emit_expr*/)
{
constexpr bool c_debug_flag = true;
scope log(XO_DEBUG(c_debug_flag));
//constexpr const char * self_name = "exprstate::on_f64_token";
/* e.g.
* def pi = 3.14159265;
* \---tk---/
*/
p_stack->push_exprstate
(progress_xs::make
(Constant<double>::make(tk.f64_value())));
}
} /*namespace scm*/
} /*namespace xo*/

View file

@ -257,19 +257,18 @@ namespace xo {
return false;
}
#ifdef OBSOLETE
bool
exprstate::admits_f64() const {
switch (exs_type_) {
case exprstatetype::expect_toplevel_expression_sequence:
case exprstatetype::defexpr:
case exprstatetype::parenexpr:
case exprstatetype::expect_rhs_expression:
/* unreachable - redirects to define_xs */
assert(false);
return false;
case exprstatetype::expect_rhs_expression:
return true;
case exprstatetype::expect_symbol:
case exprstatetype::expect_type:
return false;
@ -287,6 +286,7 @@ namespace xo {
return false;
}
#endif
void
exprstate::on_def_token(const token_type & tk,
@ -522,7 +522,7 @@ namespace xo {
void
exprstate::on_f64_token(const token_type & tk,
exprstatestack * p_stack,
exprstatestack * /*p_stack*/,
rp<Expression> * /*p_emit_expr*/)
{
constexpr bool c_debug_flag = true;
@ -530,24 +530,7 @@ namespace xo {
constexpr const char * self_name = "exprstate::on_f64";
if (!this->admits_f64())
{
throw std::runtime_error(tostr(self_name,
": unexpected floating-point literal for parsing state",
xtag("state", *this)));
}
if (this->exs_type_ == exprstatetype::expect_rhs_expression) {
/* e.g.
* def pi = 3.14159265;
* \---tk---/
*/
p_stack->push_exprstate
(progress_xs::make
(Constant<double>::make(tk.f64_value())));
} else {
assert(false);
}
this->illegal_input_error(self_name, tk);
}
void