feat: require parentheses around operator and setter method parameters in Wren

Enforce syntactic requirement for parentheses in operator method signatures (e.g., `+(other)` instead of `+ other`) and setter definitions (e.g., `bar=(value)` instead of `bar = value`). Update the compiler's `infixSignature`, `mixedSignature`, and `namedSignature` functions to consume explicit `(` and `)` tokens around parameter names. Migrate all built-in core library operator definitions and test fixtures to the new parenthesized syntax.
This commit is contained in:
Bob Nystrom
2014-04-09 00:54:37 +00:00
parent 25098cd181
commit fca506fdf1
19 changed files with 40 additions and 35 deletions
+7 -2
View File
@@ -1977,7 +1977,9 @@ void infixSignature(Compiler* compiler, char* name, int* length)
name[(*length)++] = ' ';
// Parse the parameter name.
consume(compiler, TOKEN_LEFT_PAREN, "Expect '(' after operator name.");
declareNamedVariable(compiler);
consume(compiler, TOKEN_RIGHT_PAREN, "Expect ')' after parameter name.");
}
// Compiles a method signature for an unary operator (i.e. "!").
@@ -1990,14 +1992,15 @@ void unarySignature(Compiler* compiler, char* name, int* length)
// infix (i.e. "-").
void mixedSignature(Compiler* compiler, char* name, int* length)
{
// If there is a parameter name, it's an infix operator, otherwise it's unary.
if (compiler->parser->current.type == TOKEN_NAME)
// If there is a parameter, it's an infix operator, otherwise it's unary.
if (match(compiler, TOKEN_LEFT_PAREN))
{
// Add a space for the RHS parameter.
name[(*length)++] = ' ';
// Parse the parameter name.
declareNamedVariable(compiler);
consume(compiler, TOKEN_RIGHT_PAREN, "Expect ')' after parameter name.");
}
}
@@ -2011,7 +2014,9 @@ void namedSignature(Compiler* compiler, char* name, int* length)
name[(*length)++] = ' ';
// Parse the value parameter.
consume(compiler, TOKEN_LEFT_PAREN, "Expect '(' after '='.");
declareNamedVariable(compiler);
consume(compiler, TOKEN_RIGHT_PAREN, "Expect ')' after parameter name.");
}
else
{
+2 -2
View File
@@ -77,9 +77,9 @@ static const char* libSource =
" return result\n"
" }\n"
"\n"
" + that {\n"
" +(other) {\n"
" var result = this[0..-1]\n"
" for (element in that) {\n"
" for (element in other) {\n"
" result.add(element)\n"
" }\n"
" return result\n"