From 0c8afc2cd71ab325e42d3d8458fad01c371c838b Mon Sep 17 00:00:00 2001 From: ringabout <43030857+ringabout@users.noreply.github.com> Date: Mon, 19 May 2025 22:00:38 +0800 Subject: [PATCH] wip: implemements `@` --- compiler/ast.nim | 4 ++-- compiler/ccgexprs.nim | 1 - compiler/semcall.nim | 7 +++++++ compiler/vmgen.nim | 2 +- lib/system.nim | 20 +++++--------------- 5 files changed, 15 insertions(+), 19 deletions(-) diff --git a/compiler/ast.nim b/compiler/ast.nim index 13f7890bcd2d..8efb89e800c5 100644 --- a/compiler/ast.nim +++ b/compiler/ast.nim @@ -563,9 +563,9 @@ const mEqStr, mLeStr, mLtStr, mEqSet, mLeSet, mLtSet, mMulSet, mPlusSet, mMinusSet, mXorSet, mConStrStr, mAppendStrCh, mAppendStrStr, mAppendSeqElem, - mInSet, mRepr, mOpenArrayToSeq} + mInSet, mRepr, mOpenArrayToSeq, mArrToSeq} - generatedMagics* = {mNone, mIsolate, mFinished, mOpenArrayToSeq} + generatedMagics* = {mNone, mIsolate, mFinished, mOpenArrayToSeq, mArrToSeq} ## magics that are generated as normal procs in the backend type diff --git a/compiler/ccgexprs.nim b/compiler/ccgexprs.nim index 26908a92eca8..7714d1ec0c9a 100644 --- a/compiler/ccgexprs.nim +++ b/compiler/ccgexprs.nim @@ -3012,7 +3012,6 @@ proc genMagicExpr(p: BProc, e: PNode, d: var TLoc, op: TMagic) = genCall(p, e, d) of mDefault, mZeroDefault: genDefault(p, e, d) of mEcho: genEcho(p, e[1].skipConv) - of mArrToSeq: genArrToSeq(p, e, d) of mNLen..mNError, mSlurp..mQuoteAst: localError(p.config, e.info, strutils.`%`(errXMustBeCompileTime, e[0].sym.name.s)) of mSpawn: diff --git a/compiler/semcall.nim b/compiler/semcall.nim index 866ddbe68f23..78a4a20c4fc8 100644 --- a/compiler/semcall.nim +++ b/compiler/semcall.nim @@ -851,6 +851,13 @@ proc semResolvedCall(c: PContext, x: var TCandidate, if x.calleeSym.kind notin {skMacro, skTemplate}: if x.calleeSym.magic in {mArrGet, mArrPut}: finalCallee = x.calleeSym + elif x.calleeSym.magic == mArrToSeq: + if expectedType != nil: + c.inheritBindings(x, expectedType) + x.bindings.put(x.callee.returnType.base, expectedType.base) + finalCallee = generateInstance(c, x.calleeSym, x.bindings, n.info) + else: + finalCallee = x.calleeSym else: c.inheritBindings(x, expectedType) finalCallee = generateInstance(c, x.calleeSym, x.bindings, n.info) diff --git a/compiler/vmgen.nim b/compiler/vmgen.nim index e8612000a3de..28122a3da347 100644 --- a/compiler/vmgen.nim +++ b/compiler/vmgen.nim @@ -1088,7 +1088,7 @@ proc genMagic(c: PCtx; n: PNode; dest: var TDest; flags: TGenFlags = {}, m: TMag c.genAsgnPatch(n[1], d) c.freeTemp(d) of mOrd, mChr, mArrToSeq, mUnown: c.gen(n[1], dest) - of generatedMagics: + of generatedMagics-{mArrToSeq}: genCall(c, n, dest) of mNew, mNewFinalize: unused(c, n, dest) diff --git a/lib/system.nim b/lib/system.nim index 128759ecf801..e7630889be16 100644 --- a/lib/system.nim +++ b/lib/system.nim @@ -897,21 +897,7 @@ proc cmp*(x, y: string): int {.noSideEffect.} ## **Note**: The precise result values depend on the used C runtime library and ## can differ between operating systems! -proc `@`* [IDX, T](a: sink array[IDX, T]): seq[T] {.magic: "ArrToSeq", noSideEffect.} - ## Turns an array into a sequence. - ## - ## This most often useful for constructing - ## sequences with the array constructor: `@[1, 2, 3]` has the type - ## `seq[int]`, while `[1, 2, 3]` has the type `array[0..2, int]`. - ## - ## ```nim - ## let - ## a = [1, 3, 5] - ## b = "foo" - ## - ## echo @a # => @[1, 3, 5] - ## echo @b # => @['f', 'o', 'o'] - ## ``` + proc default*[T](_: typedesc[T]): T {.magic: "Default", noSideEffect.} = ## Returns the default value of the type `T`. Contrary to `zeroDefault`, it takes default fields @@ -1449,6 +1435,10 @@ proc isNil*[T: proc | iterator {.closure.}](x: T): bool {.noSideEffect, magic: " ## Fast check whether `x` is nil. This is sometimes more efficient than ## `== nil`. +proc `@`* [IDX, T](a: array[IDX, T]): seq[T] {.magic: "ArrToSeq", noSideEffect.} = + newSeq(result, a.len) + for i in 0..a.len-1: result[i] = a[i] + when defined(nimHasTopDownInference): # magic used for seq type inference proc `@`*[T](a: openArray[T]): seq[T] {.magic: "OpenArrayToSeq".} =