Open chharvey opened 4 years ago
Update: Only augmentation will be implemented. Punning will not be developed for variable reassignment. Further, the increment and related operators (++
, --
, **
, //
) will be removed.
a ^= b; % sugar for `a = a ^ b;`
a *= b; % sugar for `a = a * b;`
a /= b; % sugar for `a = a / b;`
a += b; % sugar for `a = a + b;`
a -= b; % sugar for `a = a - b;`
a &&= b; % sugar for `a = a && b;`
a !&= b; % sugar for `a = a !& b;`
a ||= b; % sugar for `a = a || b;`
a !|= b; % sugar for `a = a !| b;`
% no sugar for `a += 1;`
% no sugar for `a -= 1;`
% no sugar for `a *= 2;`
% no sugar for `a /= 2;`
This completely supercedes the previous.
Punctuator :::=
// statement
+ | "^=" | "*=" | "/=" | "+=" | "-="
+ | "&&=" | "!&=" | "||=" | "!|="
;
+AugmentOperator ::= "^=" | "*=" | "/=" | "+=" | "-=" | "&&=" | "||=";
+AugmentNegate ::= "!&=" | "!|=";
StatementAssignment
- ::= Assignee "=" Expression ";"
+ ::= Assignee (("=" | AugmentOperator | AugmentNegate) Expression) ";"
;
Decorate(StatementAssignment ::= Assignee "=" Expression) -> SemanticAssignment
:= (SemanticAssignment
Decorate(Assignee)
Decorate(Expression)
);
+Decorate(StatementAssignment ::= Assignee AugmentOperator Expression) -> SemanticAssignment
+ := (SemanticAssignment
+ Decorate(Assignee)
+ (SemanticOperation[operator=Decorate(AugmentOperator)]
+ Decorate(Assignee)
+ Decorate(Expression)
+ )
+ );
+Decorate(StatementAssignment ::= Assignee AugmentNegate Expression) -> SemanticAssignment
+ := (SemanticAssignment
+ Decorate(Assignee)
+ (SemanticOperation[operator=NOT]
+ (SemanticOperation[operator=Decorate(AugmentNegate)]
+ Decorate(Assignee)
+ Decorate(Expression)
+ )
+ )
+ );
+Decorate(AugmentOperator ::= "^=") -> Operator := EXP;
+Decorate(AugmentOperator ::= "*=") -> Operator := MUL;
+Decorate(AugmentOperator ::= "/=") -> Operator := DIV;
+Decorate(AugmentOperator ::= "+=") -> Operator := ADD;
+Decorate(AugmentOperator ::= "-=") -> Operator := SUB;
+Decorate(AugmentOperator ::= "&&=") -> Operator := AND;
+Decorate(AugmentOperator ::= "||=") -> Operator := OR;
+Decorate(AugmentNegate ::= "!&=") -> Operator := AND;
+Decorate(AugmentNegate ::= "!|=") -> Operator := OR;
New assignment statements and update statements are syntactic sugar for special cases of reassignment.
Description
Augment Assignment
In addition to
=
, new (binary) augment operators are added, as well as a few (unary) update operators. These new operators are only syntax sugar; they do not add new semantics. The examples below informally explain the formal semantics.Note on runtime short-circuiting: Because the boolean expressions
x || y
,x !| y
,x && y
, andx !& y
short-circuit (see #22), the boolean augment operators||=
,!|=
,&&=
, and!&=
short-circuit in the same way. For example, ifa
is “truthy” and‹b›
is some procedure (such as a function call), then the statementa ||= ‹b›;
will not execute‹b›
.However, unlike some languages, the reassignment is never short-circuited and will always occur. Thus in the case of
a ||= ‹b›;
, ifa
is “truthy”, thena
will be reassigned to the value ofa
. This is consistent with operations such asx += 0;
wherex
is still reassigned to the value ofx
. The reassignment should only negligibly impact performance.Punning
Using the symbol
$
instead of an assigned expression is equivalent to repeating the assignee.This isn’t particularly useful on its own, but it can be used with augment operators.
Specification
Lexical Grammar
Syntactic Grammar
Decorate