Skip to content

Commit

Permalink
nimony: fixes tuple types (#329)
Browse files Browse the repository at this point in the history
* nimony: fixes tuple types

* adds a test case

* adds a type
  • Loading branch information
ringabout authored Jan 7, 2025
1 parent e1716e8 commit e361ece
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 6 deletions.
29 changes: 23 additions & 6 deletions src/nimony/sem.nim
Original file line number Diff line number Diff line change
Expand Up @@ -1772,12 +1772,27 @@ proc semObjectType(c: var SemContext; n: var Cursor) =
semLocal(c, n, FldY)
wantParRi c, n

proc semTupleType(c: var SemContext; n: var Cursor) =
takeToken c, n
proc semTupleFieldType(c: var SemContext; elemType: var Cursor; context: TypeDeclContext; i: int) =
c.dest.add parLeToken(pool.tags.getOrIncl($FldS), elemType.info) # start field
c.dest.add identToken(pool.strings.getOrIncl("Field" & $i), elemType.info)
c.dest.addDotToken() # export marker
c.dest.addDotToken() # pragmas
semLocalTypeImpl c, elemType, context
c.dest.addDotToken() # value
c.dest.addParRi() # end field

proc semTupleType(c: var SemContext; n: var Cursor; context: TypeDeclContext) =
c.dest.add parLeToken(TupleT, n.info)
inc n
# tuple fields:
withNewScope c:
while n.substructureKind == FldS:
semLocal(c, n, FldY)
var i = 0
while n.kind != ParRi:
if n.substructureKind == FldS:
semLocal(c, n, FldY)
else:
semTupleFieldType(c, n, context, i)
inc i
wantParRi c, n

type
Expand Down Expand Up @@ -2216,6 +2231,8 @@ proc semLocalTypeImpl(c: var SemContext; n: var Cursor; context: TypeDeclContext
inc n
semLocalTypeImpl c, n, context
skipParRi n
elif exprKind(n) == TupleConstrX:
semTupleType(c, n, context)
elif isOrExpr(n):
c.dest.addParLe(OrT, info)
inc n # tag
Expand Down Expand Up @@ -2280,7 +2297,7 @@ proc semLocalTypeImpl(c: var SemContext; n: var Cursor; context: TypeDeclContext
of TupleT:
if tryTypeClass(c, n):
return
semTupleType c, n
semTupleType c, n, context
of ArrayT:
if tryTypeClass(c, n):
return
Expand Down Expand Up @@ -3529,7 +3546,7 @@ proc semTupleConstr(c: var SemContext, it: var Item) =
typ.addParRi()
let typeStart = c.dest.len
var t = typ.cursorAt(0)
semTupleType(c, t)
semTupleType(c, t, InLocalDecl)
it.typ = typeToCursor(c, typeStart)
c.dest.shrink typeStart
commonType c, it, exprStart, origExpected
Expand Down
7 changes: 7 additions & 0 deletions tests/nimony/sysbasics/ttuples.nim
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
type
Tuple0 = tuple[a: int, b: int, c: int]
Tuple = (int, int, int)

var x0: Tuple = (1, 2, 3)
var x1: Tuple0
var x2 = (1, 2, 3, 34, 5.6)

0 comments on commit e361ece

Please sign in to comment.