From 45e1e86c73ca3219ff9604e3696dfb677d0af459 Mon Sep 17 00:00:00 2001 From: Sainan Date: Fri, 21 Jul 2023 00:52:07 +0200 Subject: [PATCH] Add optional 'const' keyword --- src/llex.cpp | 2 +- src/llex.h | 2 +- src/lparser.cpp | 42 ++++++++++++++++++++++++++++++++++++++++++ src/luaconf.h | 3 +++ 4 files changed, 47 insertions(+), 2 deletions(-) diff --git a/src/llex.cpp b/src/llex.cpp index 57140b9c40..354ef1e9b7 100644 --- a/src/llex.cpp +++ b/src/llex.cpp @@ -56,7 +56,7 @@ static const char *const luaX_tokens [] = { "pluto_use", "pluto_switch", "pluto_continue", "pluto_enum", "pluto_new", "pluto_class", "pluto_parent", "pluto_export", "switch", "continue", "enum", "new", "class", "parent", "export", - "let", + "let", "const", "pluto_suggest_0", "pluto_suggest_1", "return", "then", "true", "until", "while", "//", "..", "...", "==", ">=", "<=", "~=", diff --git a/src/llex.h b/src/llex.h index cbb0515ca3..81430d259b 100644 --- a/src/llex.h +++ b/src/llex.h @@ -42,7 +42,7 @@ enum RESERVED { TK_PUSE, // New compatibility keywords. TK_PSWITCH, TK_PCONTINUE, TK_PENUM, TK_PNEW, TK_PCLASS, TK_PPARENT, TK_PEXPORT, TK_SWITCH, TK_CONTINUE, TK_ENUM, TK_NEW, TK_CLASS, TK_PARENT, TK_EXPORT, // New non-compatible keywords. - TK_LET, // New optional keywords. + TK_LET, TK_CONST, // New optional keywords. TK_SUGGEST_0, TK_SUGGEST_1, // New special keywords. TK_RETURN, TK_THEN, TK_TRUE, TK_UNTIL, TK_WHILE, /* other terminal symbols */ diff --git a/src/lparser.cpp b/src/lparser.cpp index 687a72d3f2..34dfd59d9f 100644 --- a/src/lparser.cpp +++ b/src/lparser.cpp @@ -3996,6 +3996,40 @@ static void localstat (LexState *ls) { checktoclose(fs, toclose); } +static void conststat (LexState *ls) { + FuncState *fs = ls->fs; + auto line = ls->getLineNumber(); /* in case we need to emit a warning */ + int vidx = new_localvar(ls, str_checkname(ls, N_OVERRIDABLE), line); + TypeHint hint = gettypehint(ls); + Vardesc *var = getlocalvardesc(fs, vidx); + var->vd.kind = RDKCONST; + *var->vd.hint = hint; + + expdesc e; + if (testnext(ls, '=')) { + ls->pushContext(PARCTX_CREATE_VAR); + TypeHint t; + expr_propagate(ls, &e, t); + ls->popContext(PARCTX_CREATE_VAR); + if (luaK_exp2const(fs, &e, &var->k)) { /* compile-time constant? */ + var->vd.kind = RDKCTC; /* variable is a compile-time constant */ + fs->nactvar++; /* don't adjustlocalvars, but count it */ + } + else { + exp_propagate(ls, e, t); + process_assign(ls, var, t, line); + adjust_assign(ls, 1, 1, &e); + adjustlocalvars(ls, 1); + } + } + else { + e.k = VVOID; + process_assign(ls, var, TypeHint{ VT_NIL }, line); + adjust_assign(ls, 1, 0, &e); + adjustlocalvars(ls, 1); + } +} + static int funcname (LexState *ls, expdesc *v) { /* funcname -> NAME {fieldsel} [':' NAME] */ @@ -4298,6 +4332,11 @@ static void statement (LexState *ls, TypeHint *prop) { localstat(ls); break; } + case TK_CONST: { + luaX_next(ls); /* skip CONST */ + conststat(ls); + break; + } case TK_EXPORT: case TK_PEXPORT: { if (ls->fs->bl->previous) @@ -4696,6 +4735,9 @@ LClosure *luaY_parser (lua_State *L, ZIO *z, Mbuffer *buff, #endif #ifndef PLUTO_USE_LET disablekeyword(&lexstate, TK_LET); +#endif +#ifndef PLUTO_USE_CONST + disablekeyword(&lexstate, TK_CONST); #endif mainfunc(&lexstate, &funcstate); lua_assert(!funcstate.prev && funcstate.nups == 1 && !lexstate.fs); diff --git a/src/luaconf.h b/src/luaconf.h index 18542f6bfb..557efbd30a 100644 --- a/src/luaconf.h +++ b/src/luaconf.h @@ -861,6 +861,9 @@ // If defined, Pluto will imply 'pluto_use let' at the beginning of every script. //#define PLUTO_USE_LET +// If defined, Pluto will imply 'pluto_use const' at the beginning of every script. +//#define PLUTO_USE_CONST + /* ** {==================================================================== ** Pluto configuration: Infinite Loop Prevention (ILP)