feat: replace 'this' keyword with 'construct' for constructor definitions across core lib, compiler, docs, and tests

Add TOKEN_CONSTRUCT as a new reserved word in the Wren compiler lexer and parser, and update the grammar rule table so that 'construct' triggers a constructor signature instead of 'this'. Modify all built-in class definitions in core.wren, wren_core.c, benchmark files, and test fixtures to use 'construct new(...)' and 'construct named(...)' syntax. Update documentation in classes.markdown and syntax.markdown to reflect the new keyword, and remove the deprecated test files for 'new' and infix class expressions.
This commit is contained in:
Bob Nystrom
2015-07-21 14:24:53 +00:00
parent 847edd8e1e
commit 7d9e45e906
31 changed files with 68 additions and 81 deletions
+10 -4
View File
@@ -73,6 +73,7 @@ typedef enum
TOKEN_BREAK,
TOKEN_CLASS,
TOKEN_CONSTRUCT,
TOKEN_ELSE,
TOKEN_FALSE,
TOKEN_FOR,
@@ -628,6 +629,7 @@ static void readName(Parser* parser, TokenType type)
if (isKeyword(parser, "break")) type = TOKEN_BREAK;
else if (isKeyword(parser, "class")) type = TOKEN_CLASS;
else if (isKeyword(parser, "construct")) type = TOKEN_CONSTRUCT;
else if (isKeyword(parser, "else")) type = TOKEN_ELSE;
else if (isKeyword(parser, "false")) type = TOKEN_FALSE;
else if (isKeyword(parser, "for")) type = TOKEN_FOR;
@@ -2489,6 +2491,7 @@ GrammarRule rules[] =
/* TOKEN_BANGEQ */ INFIX_OPERATOR(PREC_EQUALITY, "!="),
/* TOKEN_BREAK */ UNUSED,
/* TOKEN_CLASS */ UNUSED,
/* TOKEN_CONSTRUCT */ { NULL, NULL, constructorSignature, PREC_NONE, NULL },
/* TOKEN_ELSE */ UNUSED,
/* TOKEN_FALSE */ PREFIX(boolean),
/* TOKEN_FOR */ UNUSED,
@@ -2501,7 +2504,7 @@ GrammarRule rules[] =
/* TOKEN_RETURN */ UNUSED,
/* TOKEN_STATIC */ UNUSED,
/* TOKEN_SUPER */ PREFIX(super_),
/* TOKEN_THIS */ { this_, NULL, constructorSignature, PREC_NONE, NULL },
/* TOKEN_THIS */ PREFIX(this_),
/* TOKEN_TRUE */ PREFIX(boolean),
/* TOKEN_VAR */ UNUSED,
/* TOKEN_WHILE */ UNUSED,
@@ -3239,17 +3242,20 @@ static void variableDefinition(Compiler* compiler)
// like the non-curly body of an if or while.
void definition(Compiler* compiler)
{
if (match(compiler, TOKEN_CLASS)) {
if (match(compiler, TOKEN_CLASS))
{
classDefinition(compiler);
return;
}
if (match(compiler, TOKEN_IMPORT)) {
if (match(compiler, TOKEN_IMPORT))
{
import(compiler);
return;
}
if (match(compiler, TOKEN_VAR)) {
if (match(compiler, TOKEN_VAR))
{
variableDefinition(compiler);
return;
}
+5 -5
View File
@@ -117,7 +117,7 @@ static const char* coreLibSource =
"}\n"
"\n"
"class MapSequence is Sequence {\n"
" this new(sequence, fn) {\n"
" construct new(sequence, fn) {\n"
" _sequence = sequence\n"
" _fn = fn\n"
" }\n"
@@ -127,7 +127,7 @@ static const char* coreLibSource =
"}\n"
"\n"
"class WhereSequence is Sequence {\n"
" this new(sequence, fn) {\n"
" construct new(sequence, fn) {\n"
" _sequence = sequence\n"
" _fn = fn\n"
" }\n"
@@ -147,7 +147,7 @@ static const char* coreLibSource =
"}\n"
"\n"
"class StringByteSequence is Sequence {\n"
" this new(string) {\n"
" construct new(string) {\n"
" _string = string\n"
" }\n"
"\n"
@@ -194,7 +194,7 @@ static const char* coreLibSource =
"}\n"
"\n"
"class MapKeySequence is Sequence {\n"
" this new(map) {\n"
" construct new(map) {\n"
" _map = map\n"
" }\n"
"\n"
@@ -203,7 +203,7 @@ static const char* coreLibSource =
"}\n"
"\n"
"class MapValueSequence is Sequence {\n"
" this new(map) {\n"
" construct new(map) {\n"
" _map = map\n"
" }\n"
"\n"