enable syntax for anonymous tuples.

Turns out to be slightly problematic as `(int, int)` could be logically
thought of as an expression evaluating to a tuple value containing
two typedesc[int]s.

To disambiguate, the zero-tuple's type must still be written as
`tuple[]`, and what would be tuple value expressions containing only
typedescs are interpreted as types.

() # value of type `tuple[]`
(int, int) # tuple type
(int, int, ()) # value of type `(typedesc[int], typedesc[int], tuple[])`
This commit is contained in:
Max Zerzouri 2015-03-04 11:52:48 +13:00
commit dba25d2622
3 changed files with 31 additions and 7 deletions

View file

@ -344,7 +344,14 @@ proc semTypeIdent(c: PContext, n: PNode): PSym =
else:
localError(n.info, errIdentifierExpected)
result = errorSym(c, n)
proc semAnonTuple(c: PContext, n: PNode, prev: PType): PType =
if sonsLen(n) == 0:
localError(n.info, errTypeExpected)
result = newOrPrevType(tyTuple, prev, c)
for i in countup(0, sonsLen(n) - 1):
addSonSkipIntLit(result, semTypeNode(c, n.sons[i], nil))
proc semTuple(c: PContext, n: PNode, prev: PType): PType =
var typ: PType
result = newOrPrevType(tyTuple, prev, c)
@ -1116,9 +1123,7 @@ proc semTypeNode(c: PContext, n: PNode, prev: PType): PType =
of nkPar:
if sonsLen(n) == 1: result = semTypeNode(c, n.sons[0], prev)
else:
# XXX support anon tuple here
localError(n.info, errTypeExpected)
result = newOrPrevType(tyError, prev, c)
result = semAnonTuple(c, n, prev)
of nkCallKinds:
if isRange(n):
result = semRangeAux(c, n, prev)