Skip to content

Commit

Permalink
nimony: adds a type for arrayConstr
Browse files Browse the repository at this point in the history
  • Loading branch information
ringabout committed Jan 6, 2025
1 parent cdd94bb commit 7a0e538
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 1 deletion.
5 changes: 5 additions & 0 deletions src/gear3/expander.nim
Original file line number Diff line number Diff line change
Expand Up @@ -871,6 +871,11 @@ proc traverseExpr(e: var EContext; c: var Cursor) =
e.dest.shrink beforeType
traverseExpr(e, c)
skipParRi(e, c)
of AconstrX:
e.dest.add tagToken("aconstr", c.info)
inc c
traverseType(e, c)
inc nested
of OconstrX:
e.dest.add tagToken("oconstr", c.info)
inc c
Expand Down
6 changes: 5 additions & 1 deletion src/nimony/sem.nim
Original file line number Diff line number Diff line change
Expand Up @@ -3362,6 +3362,8 @@ proc semArrayConstr(c: var SemContext, it: var Item) =
buildErr c, it.n.info, "empty array needs a specified type"
wantParRi c, it.n
return

var elemBuf = createTokenBuf()
var elem = Item(n: it.n, typ: c.types.autoType)
case it.typ.typeKind
of ArrayT: # , SeqT, OpenArrayT
Expand All @@ -3372,13 +3374,15 @@ proc semArrayConstr(c: var SemContext, it: var Item) =
else:
buildErr c, it.n.info, "invalid expected type for array constructor: " & typeToString(it.typ)
# XXX index types, `index: value` etc not implemented
swap elemBuf, c.dest
semExpr c, elem
var count = 1
while elem.n.kind != ParRi:
semExpr c, elem
inc count
it.n = elem.n
wantParRi c, it.n
swap elemBuf, c.dest
let typeStart = c.dest.len
c.dest.buildTree ArrayT, it.n.info:
c.dest.addSubtree elem.typ
Expand All @@ -3389,7 +3393,7 @@ proc semArrayConstr(c: var SemContext, it: var Item) =
c.dest.addParRi()
let expected = it.typ
it.typ = typeToCursor(c, typeStart)
c.dest.shrink typeStart
c.dest.add elemBuf
commonType c, it, exprStart, expected

proc semSetConstr(c: var SemContext, it: var Item) =
Expand Down
32 changes: 32 additions & 0 deletions tests/nimony/sysbasics/tbasics.nif
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
(.nif24)
,1,tests/nimony/sysbasics/tbasics.nim(stmts 8,1
(type ~6 :Array.0.tbawx6nu81 . . . 7
(array ~11,5,lib/std/system.nim
(i -1)
(rangetype
(i -1) +0 +4))) 4,3
(var :s.0.tbawx6nu81 . .
(array
(i -1)
(rangetype
(i -1) +0 +2)) 4
(arr ~4
(array
(i -1)
(rangetype
(i -1) +0 +2)) 1 +1 4 +2 7 +3)) 4,4
(var :s2.0.tbawx6nu81 . . 11,~3
(array ~11,5,lib/std/system.nim
(i -1)
(rangetype
(i -1) +0 +4)) .) 4,5
(var :s3.0.tbawx6nu81 . . 11,~4
(array ~11,5,lib/std/system.nim
(i -1)
(rangetype
(i -1) +0 +4)) 12
(arr ~12
(array ,1,lib/std/system.nim
(i -1)
(rangetype
(i -1) +0 +4)) 1 +1 4 +2 7 +3 10 +4 13 +5)))
6 changes: 6 additions & 0 deletions tests/nimony/sysbasics/tbasics.nim
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
type
Array = array[5, int]

var s = [1, 2, 3]
var s2: Array
var s3: Array = [1, 2, 3, 4, 5]

0 comments on commit 7a0e538

Please sign in to comment.