From fec6ad0f5f47c1bea6f4ecc06c1b28c1e3461cd6 Mon Sep 17 00:00:00 2001 From: Sainan Date: Thu, 23 Jan 2025 05:46:31 +0100 Subject: [PATCH] Add $assert --- src/lbaselib.cpp | 2 +- src/lparser.cpp | 53 +++++++++++++++++++++++----------------- testes/pluto/basic.pluto | 5 ++++ 3 files changed, 37 insertions(+), 23 deletions(-) diff --git a/src/lbaselib.cpp b/src/lbaselib.cpp index 5ce87fe69..a550341f4 100644 --- a/src/lbaselib.cpp +++ b/src/lbaselib.cpp @@ -499,7 +499,7 @@ static int luaB_dofile (lua_State *L) { } -static int luaB_assert (lua_State *L) { +int luaB_assert (lua_State *L) { if (l_likely(lua_toboolean(L, 1))) /* condition is true? */ return lua_gettop(L); /* return all arguments */ else { /* error */ diff --git a/src/lparser.cpp b/src/lparser.cpp index 0b3d6abe6..4b55f4fe1 100644 --- a/src/lparser.cpp +++ b/src/lparser.cpp @@ -2675,6 +2675,13 @@ static void safe_navigation (LexState *ls, expdesc *v) { static void top_to_expdesc (LexState *ls, expdesc *v) { lua_State *L = ls->L; switch (lua_type(L, -1)) { + case LUA_TNONE: + case LUA_TNIL: + init_exp(v, VNIL, 0); + break; + case LUA_TBOOLEAN: + init_exp(v, lua_istrue(L, -1) ? VTRUE : VFALSE, 0); + break; case LUA_TNUMBER: { if (lua_isinteger(L, -1)) { init_exp(v, VKINT, 0); @@ -2777,26 +2784,37 @@ int luaB_utonumber (lua_State *L); int luaB_tostring (lua_State *L); int luaB_utostring (lua_State *L); int luaB_type (lua_State *L); +int luaB_assert (lua_State *L); static void const_expr (LexState *ls, expdesc *v) { switch (ls->t.token) { case TK_NAME: { - const Pluto::ConstexprLibrary* lib = nullptr; - for (const auto& library : Pluto::all_preloaded) { - if (strcmp(library->name, getstr(ls->t.seminfo.ts)) == 0) { - lib = library; - break; - } - } - if (lib == nullptr) { - for (const auto& library : Pluto::all_constexpr) { + if (!check_constexpr_call(ls, v, "tonumber", luaB_tonumber) + && !check_constexpr_call(ls, v, "utonumber", luaB_utonumber) + && !check_constexpr_call(ls, v, "tostring", luaB_tostring) + && !check_constexpr_call(ls, v, "utostring", luaB_utostring) + && !check_constexpr_call(ls, v, "type", luaB_type) + && !check_constexpr_call(ls, v, "assert", luaB_assert) + ) + { + const Pluto::ConstexprLibrary* lib = nullptr; + for (const auto& library : Pluto::all_preloaded) { if (strcmp(library->name, getstr(ls->t.seminfo.ts)) == 0) { lib = library; break; } } - } - if (lib != nullptr) { + if (lib == nullptr) { + for (const auto& library : Pluto::all_constexpr) { + if (strcmp(library->name, getstr(ls->t.seminfo.ts)) == 0) { + lib = library; + break; + } + } + if (lib == nullptr) { + throwerr(ls, luaO_fmt(ls->L, "%s is not available in constant expression", getstr(ls->t.seminfo.ts)), "unrecognized name."); + } + } luaX_next(ls); /* skip TK_NAME */ checknext(ls, '.'); check(ls, TK_NAME); @@ -2815,15 +2833,6 @@ static void const_expr (LexState *ls, expdesc *v) { constexpr_call(ls, v, f); } } - else if (!check_constexpr_call(ls, v, "tonumber", luaB_tonumber) - && !check_constexpr_call(ls, v, "utonumber", luaB_utonumber) - && !check_constexpr_call(ls, v, "tostring", luaB_tostring) - && !check_constexpr_call(ls, v, "utostring", luaB_utostring) - && !check_constexpr_call(ls, v, "type", luaB_type) - ) - { - throwerr(ls, luaO_fmt(ls->L, "%s is not available in constant expression", getstr(ls->t.seminfo.ts)), "unrecognized name."); - } return; } default: { @@ -4669,8 +4678,8 @@ static void constexprstat (LexState *ls, int line) { constexprdefinestat(ls, line); } else { - const char *token = luaX_token2str(ls, ls->t); - throwerr(ls, luaO_fmt(ls->L, "unexpected symbol near %s", token), "unexpected symbol."); + expdesc v; + const_expr(ls, &v); } } diff --git a/testes/pluto/basic.pluto b/testes/pluto/basic.pluto index 862d6da86..b998f4021 100644 --- a/testes/pluto/basic.pluto +++ b/testes/pluto/basic.pluto @@ -3416,6 +3416,11 @@ do $end end assert(elseif_test() == "branch 2") + + $assert(true) + $define COND = true + $assert(COND) + assert(not load[[$assert(false)]]) end print "Testing preprocessor."