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

Add $assert #1088

Merged
merged 1 commit into from
Jan 23, 2025
Merged
Show file tree
Hide file tree
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
2 changes: 1 addition & 1 deletion src/lbaselib.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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 */
Expand Down
53 changes: 31 additions & 22 deletions src/lparser.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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);
Expand All @@ -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: {
Expand Down Expand Up @@ -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);
}
}

Expand Down
5 changes: 5 additions & 0 deletions testes/pluto/basic.pluto
Original file line number Diff line number Diff line change
Expand Up @@ -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."
Expand Down
Loading