Skip to content

Commit

Permalink
Add optional 'const' keyword
Browse files Browse the repository at this point in the history
  • Loading branch information
Sainan committed Jul 20, 2023
1 parent 316046a commit 45e1e86
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 2 deletions.
2 changes: 1 addition & 1 deletion src/llex.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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",
"//", "..", "...", "==", ">=", "<=", "~=",
Expand Down
2 changes: 1 addition & 1 deletion src/llex.h
Original file line number Diff line number Diff line change
Expand Up @@ -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 */
Expand Down
42 changes: 42 additions & 0 deletions src/lparser.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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] */
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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);
Expand Down
3 changes: 3 additions & 0 deletions src/luaconf.h
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down

0 comments on commit 45e1e86

Please sign in to comment.