diff --git a/lib/Syntax/Keyword/Assert.xs b/lib/Syntax/Keyword/Assert.xs index 805ebac..e330aa8 100644 --- a/lib/Syntax/Keyword/Assert.xs +++ b/lib/Syntax/Keyword/Assert.xs @@ -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); } @@ -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