Closed chharvey closed 3 years ago
This comment was deleted and replaced with #40.
Update: Mapping collection literals will no longer allow comma-separated antecedents. The syntax is too confusing with them.
E.g., the mapping
[
'one', 'uno', 'eine' |-> 1,
'two', 'dos', 'zwei' |-> 2,
];
written inline, is very unreadable:
['one', 'uno', 'eine' |-> 1, 'two', 'dos', 'zwei' |-> 2];
It is better to keep cases as antecedent–consequent pairs only, repeating consequents as necessary.
[
'one' |-> 1,
'uno' |-> 1,
'eine' |-> 1,
'two' |-> 2,
'dos' |-> 2,
'zwei' |-> 2,
];
If any given consequent is a verbose expression, it may be saved as a variable first.
let what: [str] = ['what'];
[
1 |-> 'who',
2 |-> what,
'2nd' |-> what,
1 + 2 |-> ['i' |-> ['don’t' |-> 'know']],
];
This restriction keeps mappings parallel to records, where record keys also aren’t allowed to be grouped.
Syntax:
-Case ::= Expression# "|->" Expression;
+Case ::= Expression "|->" Expression;
Semantics:
SemanticCase
- ::= SemanticExpression SemanticExpression+;
+ ::= SemanticExpression SemanticExpression;
Decorate:
-Decorate(Case ::= Case__0__List "|->" Expression) -> SemanticCase
+Decorate(Case ::= Expression "|->" Expression) -> SemanticCase
:= (SemanticCase
- ...Decorate(Case__0__List)
+ Decorate(Expression)
Decorate(Expression)
);
-Decorate(ListLiteral ::= "[" ","? Case__0__List ","? "]") -> SemanticList
+Decorate(ListLiteral ::= "[" ","? ListLiteral__1__List ","? "]") -> SemanticList
:= (SemanticList
- ...Decorate(Case__0__List)
+ ...Decorate(ListLiteral__1__List)
);
- Decorate(Case__0__List ::= Expression) -> Sequence<SemanticExpression>
+ Decorate(ListLiteral__1__List ::= Expression) -> Sequence<SemanticExpression>
:= [Decorate(Expression)];
- Decorate(Case__0__List ::= Case__0__List "," Expression) -> Sequence<SemanticExpression>
+ Decorate(ListLiteral__1__List ::= ListLiteral__1__List "," Expression) -> Sequence<SemanticExpression>
:= [
- ...Decorate(Case__0__List),
+ ...Decorate(ListLiteral__1__List),
Decorate(Expression),
];
Update: Breaking change to Mapping collection literals delimiters.
The syntax of Mapping literals currently use square brackets for delimiters:
[a1 |-> c1, a2 |-> c2, a3 |-> c3];
They will now be replaced with curly braces:
{a1 |-> c1, a2 |-> c2, a3 |-> c3};
This change aligns with a future Set literal syntax:
{i1, i2, i3};
Update: Breaking change to Mapping collection literals separators.
The syntax of Mapping literals currently use the |->
arrow for separators:
{a1 |-> c1, a2 |-> c2, a3 |-> c3};
They will now be replaced with the thin arrow ->
:
{a1 -> c1, a2 -> c2, a3 -> c3};
This change aligns with the Mapping literal type syntax:
type T = {K -> V}; %== Mapping.<K, V>
Function types and lambdas (way down the road) will use the fat arrow =>
.
Collection literals include list literals, record literals, and mapping literals. This issue is for syntax only, and does not provide a specification for semantic analysis (including types).
Description
List Literals
List literals are syntax expressions that contain a list of comma-separated expressions. Nesting is allowed.
List literals can be of type Tuple. Tuple types are written as a list of type expressions. The list above has the following type:
We can declare a variable with a tuple type and then assign it a list literal.
Record Literals
Record literals are expressions that conatin a list of properties. Properties are key-value pairs. Keys are keywords or identifiers, and values are expressions. The key and value within a property are delimited by an equals sign. Properties are separated by commas. Nesting is allowed.
Keys may be reserved keywords, not just restricted to identifiers. This is because the record key will always be lexically bound to the record — it will never stand alone, so there’s no risk of syntax error.
Conventionally, whitespace is omitted between the key name and the equals sign delimiter
=
. This practice helps programmers differentiate between record properties and variable declarations/assignments.Records have literal types as well. They are similar to record values, except that the colon
:
is used as the key–value delimiter, and the property values are replaced with types. Notice how the properties may be written in any order.Mapping Literals
Mapping literals are expressions that conain a list of cases. Cases are correspondances from one or more antecedents to one consequent. Antecedents and consequents are expressions. The antecedent(s) and consequent within a case are delimited with the symbol
|->
. Antecedents within a case, and cases themselves, are separated by commas. Nesting is allowed.There is currently no syntax production for the types of mapping literals, so we must use
obj
instead.Specification
Lexical Grammar
Syntactic Grammar
Semantic Schema
Decorate