From bd3958e29e11795915fbfae98e44aac82352fd31 Mon Sep 17 00:00:00 2001 From: ringabout <43030857+ringabout@users.noreply.github.com> Date: Sun, 5 Jan 2025 19:45:56 +0800 Subject: [PATCH 1/3] nimony: fixes tuple types --- src/nimony/sem.nim | 29 +++++++++++++++++++++++------ 1 file changed, 23 insertions(+), 6 deletions(-) diff --git a/src/nimony/sem.nim b/src/nimony/sem.nim index dd015958..f63aa4c3 100644 --- a/src/nimony/sem.nim +++ b/src/nimony/sem.nim @@ -1764,12 +1764,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 @@ -2188,6 +2203,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 @@ -2252,7 +2269,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 @@ -3503,7 +3520,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 From 991ae2ed460a91645549eefbd92336565dc3ddd7 Mon Sep 17 00:00:00 2001 From: ringabout <43030857+ringabout@users.noreply.github.com> Date: Tue, 7 Jan 2025 18:29:59 +0800 Subject: [PATCH 2/3] adds a test case --- tests/nimony/sysbasics/ttuples.nim | 7 +++++++ 1 file changed, 7 insertions(+) create mode 100644 tests/nimony/sysbasics/ttuples.nim diff --git a/tests/nimony/sysbasics/ttuples.nim b/tests/nimony/sysbasics/ttuples.nim new file mode 100644 index 00000000..e8c506af --- /dev/null +++ b/tests/nimony/sysbasics/ttuples.nim @@ -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, "4", 5.6) From cc6e25909fe1ee70925e78b0810c886227ca5f19 Mon Sep 17 00:00:00 2001 From: ringabout <43030857+ringabout@users.noreply.github.com> Date: Tue, 7 Jan 2025 18:31:09 +0800 Subject: [PATCH 3/3] adds a type --- tests/nimony/sysbasics/ttuples.nim | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/nimony/sysbasics/ttuples.nim b/tests/nimony/sysbasics/ttuples.nim index e8c506af..07ed3a72 100644 --- a/tests/nimony/sysbasics/ttuples.nim +++ b/tests/nimony/sysbasics/ttuples.nim @@ -4,4 +4,4 @@ type var x0: Tuple = (1, 2, 3) var x1: Tuple0 -var x2 = (1, 2, 3, "4", 5.6) +var x2 = (1, 2, 3, 34, 5.6)