Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Custom error messages #11

Draft
wants to merge 2 commits into
base: main
Choose a base branch
from
Draft
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
48 changes: 37 additions & 11 deletions lib/Syntax/Keyword/Assert.xs
Original file line number Diff line number Diff line change
Expand Up @@ -213,24 +213,43 @@ ok:
RETURN;
}

static int build_assert(pTHX_ OP **out, XSParseKeywordPiece *arg0, void *hookdata)
static int build_assert(pTHX_ OP **out, XSParseKeywordPiece *args[], size_t nargs, void *hookdata)
{
OP *argop = arg0->op;
// assert(EXPR, EXPR)
//
// assert($x == 1)
// assert($x == 1, "x is not 1");
// assert($x == 1, sub { "x is not 1: x=$x" });
//
// first EXPR is the condition, second is the message.
// error meeage is optional.
// if the condition is false, the message is printed and the program dies.
//
// TODO implement the error message.
// [ ] First, implement the message as a string.
// [ ] Then, implement the message as a code block.
OP *condop = args[0]->op;
OP *msgop = args[2] ? args[2]->op : NULL;

if (assert_enabled) {
enum BinopType binoptype = classify_binop(argop->op_type);
enum BinopType binoptype = classify_binop(condop->op_type);
if (binoptype) {
argop->op_type = OP_CUSTOM;
argop->op_ppaddr = &pp_assertbin;
argop->op_private = binoptype;
*out = argop;
condop->op_type = OP_CUSTOM;
condop->op_ppaddr = &pp_assertbin;
condop->op_private = binoptype;

*out = condop;
}
else {
*out = newUNOP_CUSTOM(&pp_assert, 0, argop);
*out = newUNOP_CUSTOM(&pp_assert, 0, condop);
}
}
else {
// do nothing.
op_free(argop);
op_free(condop);
if (msgop) {
op_free(msgop);
}
*out = newOP(OP_NULL, 0);
}

Expand All @@ -239,8 +258,15 @@ static int build_assert(pTHX_ OP **out, XSParseKeywordPiece *arg0, void *hookdat

static const struct XSParseKeywordHooks hooks_assert = {
.permit_hintkey = "Syntax::Keyword::Assert/assert",
.piece1 = XPK_TERMEXPR_SCALARCTX,
.build1 = &build_assert,
.pieces = (const struct XSParseKeywordPieceType[]) {
XPK_ARGS(
XPK_TERMEXPR_SCALARCTX,
XPK_OPTIONAL(XPK_COMMA),
XPK_TERMEXPR_SCALARCTX_OPT
),
{0}
},
.build = &build_assert,
};

MODULE = Syntax::Keyword::Assert PACKAGE = Syntax::Keyword::Assert
Expand Down
Loading