xo-reader: wip: parsing lambda expressions [wip, non-functional]

This commit is contained in:
Roland Conybeare 2024-08-17 01:09:17 -04:00
commit 0841fd7dbd
10 changed files with 289 additions and 5 deletions

View file

@ -11,7 +11,9 @@ set(SELF_SRCS
exprseq_xs.cpp
expect_expr_xs.cpp
expect_symbol_xs.cpp
expect_formal_xs.cpp
expect_formal_arglist_xs.cpp
expect_type_xs.cpp
lambda_xs.cpp)

View file

@ -4,8 +4,10 @@
*/
#include "expect_expr_xs.hpp"
#include "lambda_xs.hpp"
#include "paren_xs.hpp"
#include "progress_xs.hpp"
#include "xo/expression/Lambda.hpp"
#include "xo/expression/Constant.hpp"
namespace xo {
@ -23,6 +25,22 @@ namespace xo {
: exprstate(exprstatetype::expect_rhs_expression)
{}
void
expect_expr_xs::on_lambda_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_leftparen";
/* push lparen_0 to remember to look for subsequent rightparen. */
p_stack->push_exprstate(lambda_xs::make());
p_stack->top_exprstate().on_lambda_token(tk, p_stack, p_emit_expr);
//p_stack->push_exprstate(expect_expr_xs::expect_rhs_expression());
}
void
expect_expr_xs::on_leftparen_token(const token_type & /*tk*/,
exprstatestack * p_stack,

View file

@ -0,0 +1,37 @@
/* file expect_formal_arglist_xs.cpp
*
* author: Roland Conybeare
*/
#include "expect_formal_arglist_xs.hpp"
namespace xo {
namespace scm {
std::unique_ptr<expect_formal_arglist_xs>
expect_formal_arglist_xs::make() {
return std::make_unique<expect_formal_arglist_xs>
(expect_formal_arglist_xs());
}
expect_formal_arglist_xs::expect_formal_arglist_xs()
: farglxs_type_{formalarglstatetype::argl_0}
{}
void
expect_formal_arglist_xs::on_leftparen_token(const token_type & tk,
exprstatestack * p_stack,
rp<Expression> * p_emit_expr)
{
if (farglxs_type_ == formalarglstatetype::argl_0) {
this->farglxs_type_ = formalarglstatetype::argl_1;
return;
}
exprstate::on_leftparen_token(tk, p_stack, p_emit_expr);
}
} /*namespace scm*/
} /*namespace xo*/
/* end expect_formal_arglist_xs.cpp */

View file

@ -11,6 +11,7 @@ namespace xo {
using xo::reflect::Reflect;
namespace scm {
std::unique_ptr<expect_type_xs>
expect_type_xs::make() {
return std::make_unique<expect_type_xs>(expect_type_xs());

View file

@ -1,7 +1,9 @@
/* @file exprstate.cpp */
#include "exprstate.hpp"
#include "formal_arg.hpp"
//#include "formal_arg.hpp"
#include "xo/expression/Variable.hpp"
#include "xo/indentlog/print/vector.hpp"
#include <stdexcept>
//#include "define_xs.hpp"
//#include "progress_xs.hpp"
@ -49,6 +51,14 @@ namespace xo {
this->illegal_input_error("exprstate::on_def_token", tk);
}
void
exprstate::on_lambda_token(const token_type & tk,
exprstatestack * /*p_stack*/,
rp<Expression> * /*p_emit_expr*/)
{
this->illegal_input_error("exprstate::on_lambda_token", tk);
}
void
exprstate::on_symbol_token(const token_type & tk,
exprstatestack * p_stack,
@ -86,7 +96,7 @@ namespace xo {
}
void
exprstate::on_formal(const formal_arg & formal,
exprstate::on_formal(const rp<Variable> & formal,
exprstatestack * p_stack,
rp<Expression> * /*p_emit_expr*/)
{
@ -102,7 +112,28 @@ namespace xo {
throw std::runtime_error(tostr(c_self_name,
": unexpected formal-arg for parsing state",
xtag("formal", formal),
xtag("formal", formal.get()),
xtag("state", *this)));
}
void
exprstate::on_formal_arglist(const std::vector<rp<Variable>> & argl,
exprstatestack * p_stack,
rp<Expression> * /*p_emit_expr*/)
{
/* returning type description to something that wants it */
constexpr bool c_debug_flag = true;
scope log(XO_DEBUG(c_debug_flag));
log && log(xtag("exstype",
p_stack->top_exprstate().exs_type()));
constexpr const char * c_self_name = "exprstate::on_formal_arglist";
throw std::runtime_error(tostr(c_self_name,
": unexpected formal-arg for parsing state",
xtag("argl", argl),
xtag("state", *this)));
}
@ -223,6 +254,10 @@ namespace xo {
this->on_def_token(tk, p_stack);
return;
case tokentype::tk_lambda:
this->on_lambda_token(tk, p_stack, p_emit_expr);
return;
case tokentype::tk_i64:
assert(false);
return;
@ -289,7 +324,6 @@ namespace xo {
return;
case tokentype::tk_type:
case tokentype::tk_lambda:
case tokentype::tk_if:
case tokentype::tk_let:

View file

@ -1,11 +1,90 @@
/* @file lambda_xs.cpp */
#include "lambda_xs.hpp"
#include "expect_formal_arglist_xs.hpp"
#include "expect_expr_xs.hpp"
#include "xo/expression/Lambda.hpp"
namespace xo {
using xo::ast::Lambda;
namespace scm {
std::unique_ptr<lambda_xs>
lambda_xs::make() {
return std::make_unique<lambda_xs>(lambda_xs());
}
lambda_xs::lambda_xs() {}
void
lambda_xs::on_lambda_token(const token_type & tk,
exprstatestack * p_stack,
rp<Expression> * p_emit_expr)
{
if (lmxs_type_ == lambdastatetype::lm_0) {
this->lmxs_type_ = lambdastatetype::lm_1;
p_stack->push_exprstate(expect_formal_arglist_xs::make());
return;
}
exprstate::on_lambda_token(tk,
p_stack,
p_emit_expr);
}
void
lambda_xs::on_formal_arglist(const std::vector<rp<Variable>> & argl,
exprstatestack * p_stack,
rp<Expression> * p_emit_expr)
{
if (lmxs_type_ == lambdastatetype::lm_1) {
this->lmxs_type_ = lambdastatetype::lm_2;
this->argl_ = argl;
p_stack->push_exprstate(expect_expr_xs::expect_rhs_expression());
return;
}
exprstate::on_formal_arglist(argl,
p_stack,
p_emit_expr);
}
void
lambda_xs::on_expr(ref::brw<Expression> expr,
exprstatestack * p_stack,
rp<Expression> * p_emit_expr)
{
if (lmxs_type_ == lambdastatetype::lm_2) {
this->lmxs_type_ = lambdastatetype::lm_3;
this->body_ = expr.promote();
return;
}
exprstate::on_expr(expr, p_stack, p_emit_expr);
}
void
lambda_xs::on_semicolon_token(const token_type & tk,
exprstatestack * p_stack,
rp<Expression> * p_emit_expr)
{
if (lmxs_type_ == lambdastatetype::lm_3) {
/* done! */
std::unique_ptr<exprstate> self = p_stack->pop_exprstate();
std::string name = "fixmename";
rp<Lambda> lm = Lambda::make(name, argl_, body_);
p_stack->top_exprstate().on_expr(lm, p_stack, p_emit_expr);
p_stack->top_exprstate().on_semicolon_token(tk, p_stack, p_emit_expr);
return;
}
exprstate::on_semicolon_token(tk, p_stack, p_emit_expr);
}
} /*namespace scm*/
} /*namespace xo*/