mirror of
https://github.com/nim-lang/Nim.git
synced 2026-06-09 21:28:13 +00:00
explicit indices in array literals
This commit is contained in:
@@ -94,6 +94,7 @@ type
|
||||
errXCannotBeInParamDecl, errPragmaOnlyInHeaderOfProc, errImplOfXNotAllowed,
|
||||
errImplOfXexpected, errNoSymbolToBorrowFromFound, errDiscardValue,
|
||||
errInvalidDiscard, errIllegalConvFromXtoY, errCannotBindXTwice,
|
||||
errInvalidOrderInArrayConstructor,
|
||||
errInvalidOrderInEnumX, errEnumXHasWholes, errExceptExpected, errInvalidTry,
|
||||
errOptionExpected, errXisNoLabel, errNotAllCasesCovered,
|
||||
errUnkownSubstitionVar, errComplexStmtRequiresInd, errXisNotCallable,
|
||||
@@ -214,6 +215,7 @@ const
|
||||
"value returned by statement has to be discarded",
|
||||
"statement returns no value that can be discarded",
|
||||
"conversion from $1 to $2 is invalid", "cannot bind parameter \'$1\' twice",
|
||||
"invalid order in array constructor",
|
||||
"invalid order in enum \'$1\'", "enum \'$1\' has wholes",
|
||||
"\'except\' or \'finally\' expected",
|
||||
"after catch all \'except\' or \'finally\' no section may follow",
|
||||
|
||||
@@ -260,11 +260,29 @@ proc semArrayConstr(c: PContext, n: PNode): PNode =
|
||||
if sonsLen(n) == 0:
|
||||
addSon(result.typ, newTypeS(tyEmpty, c)) # needs an empty basetype!
|
||||
else:
|
||||
addSon(result, semExprWithType(c, n.sons[0]))
|
||||
var x = n.sons[0]
|
||||
var lastIndex: biggestInt = 0
|
||||
var indexType = getSysType(tyInt)
|
||||
if x.kind == nkExprColonExpr:
|
||||
var idx = semConstExpr(c, x.sons[0])
|
||||
lastIndex = getOrdValue(idx)
|
||||
indexType = idx.typ
|
||||
x = x.sons[1]
|
||||
|
||||
addSon(result, semExprWithType(c, x))
|
||||
var typ = skipTypes(result.sons[0].typ, {tyGenericInst, tyVar, tyOrdinal})
|
||||
for i in countup(1, sonsLen(n) - 1):
|
||||
n.sons[i] = semExprWithType(c, n.sons[i])
|
||||
x = n.sons[i]
|
||||
if x.kind == nkExprColonExpr:
|
||||
var idx = semConstExpr(c, x.sons[0])
|
||||
idx = fitNode(c, indexType, idx)
|
||||
if lastIndex+1 != getOrdValue(idx):
|
||||
liMessage(x.info, errInvalidOrderInArrayConstructor)
|
||||
x = x.sons[1]
|
||||
|
||||
n.sons[i] = semExprWithType(c, x)
|
||||
addSon(result, fitNode(c, typ, n.sons[i]))
|
||||
inc(lastIndex)
|
||||
addSon(result.typ, typ)
|
||||
result.typ.sons[0] = makeRangeType(c, 0, sonsLen(result) - 1, n.info)
|
||||
|
||||
|
||||
@@ -2,6 +2,7 @@ tack.nim;125
|
||||
tambsym2.nim;7
|
||||
tambsys.nim;
|
||||
tarray.nim;10012
|
||||
tarraycons.nim;6
|
||||
tarray2.nim;[16, 25, 36]
|
||||
tarray3.nim;3
|
||||
tassert.nim;assertion failure!this shall be always written
|
||||
|
||||
|
17
tests/accept/run/tarraycons.nim
Normal file
17
tests/accept/run/tarraycons.nim
Normal file
@@ -0,0 +1,17 @@
|
||||
|
||||
type
|
||||
TEnum = enum
|
||||
eA, eB, eC, eD, eE, eF
|
||||
|
||||
const
|
||||
myMapping: array[TEnum, array[0..1, int]] = [
|
||||
eA: [1, 2],
|
||||
eB: [3, 4],
|
||||
[5, 6],
|
||||
eD: [0: 8, 1: 9],
|
||||
eE: [0: 8, 9],
|
||||
eF: [2, 1: 9]
|
||||
]
|
||||
|
||||
echo myMapping[eC][1]
|
||||
|
||||
@@ -3,6 +3,7 @@ tadrdisc.nim;15;for a 'var' type a variable needs to be passed
|
||||
tambsym.nim;6;ambiguous identifier
|
||||
tambsym2.nim;4;undeclared identifier: 'CreateRGBSurface'
|
||||
tambsym3.nim;6;ambiguous identifier
|
||||
tarraycons.nim;9;invalid order in array constructor
|
||||
tatomic.nim;2;identifier expected, but found 'atomic'
|
||||
tbind2.nim;7;ambiguous call
|
||||
tbind4.nim;4;undeclared identifier: 'lastId'
|
||||
|
||||
|
17
tests/reject/tarraycons.nim
Normal file
17
tests/reject/tarraycons.nim
Normal file
@@ -0,0 +1,17 @@
|
||||
|
||||
type
|
||||
TEnum = enum
|
||||
eA, eB, eC, eD, eE, eF
|
||||
|
||||
const
|
||||
myMapping: array[TEnum, array[0..1, int]] = [
|
||||
eA: [1, 2],
|
||||
eC: [3, 4],
|
||||
eB: [5, 6],
|
||||
eD: [0: 8, 1: 9],
|
||||
eE: [0: 8, 9],
|
||||
eF: [2, 1: 9]
|
||||
]
|
||||
|
||||
echo myMapping[eC][1]
|
||||
|
||||
1
todo.txt
1
todo.txt
@@ -2,7 +2,6 @@
|
||||
- deprecate ^ and make it available as operator
|
||||
- test branch coverage
|
||||
- checked exceptions
|
||||
- explicit indices for array literals
|
||||
- built-in serialization
|
||||
- do not ambiguity error for methods if amibiguity only affects the same
|
||||
dispatcher anyway
|
||||
|
||||
@@ -47,6 +47,8 @@ Additions
|
||||
- A field in an ``enum`` may be given an explicit string representation.
|
||||
This yields more maintainable code than using a constant
|
||||
``array[TMyEnum, string]`` mapping.
|
||||
- Indices in array literals may be explicitly given, enhancing readability:
|
||||
``[enumValueA: "a", enumValueB: "b"]``.
|
||||
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user