Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

nimony: fixes tuple types #329

Merged
merged 6 commits into from
Jan 7, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 @@ -3528,7 +3545,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)
Loading