mirror of
https://github.com/nim-lang/Nim.git
synced 2026-05-28 15:55:14 +00:00
fixes #25650 This pull request refactors and improves the dependency resolution logic in the Nim compiler, The most important changes are grouped below: ### Dependency Resolution Refactor * Replaced the `resolveFile` procedure with two more specialized procedures: `resolveImport` (which uses the compiler's module lookup rules for imports) and `resolveInclude` (which resolves includes relative to the including file or search paths). Updated all usages accordingly, improving clarity and correctness of dependency handling. [[1]](diffhunk://#diff-1203947eecb9ef641ce7ee029677f875eb983de050b82c65ca286517feed00e6L82-R94) [[2]](diffhunk://#diff-1203947eecb9ef641ce7ee029677f875eb983de050b82c65ca286517feed00e6L106-R103) [[3]](diffhunk://#diff-1203947eecb9ef641ce7ee029677f875eb983de050b82c65ca286517feed00e6L121-R118) * Removed the unused `strutils` import from `compiler/deps.nim` for cleaner dependencies. ### Testing Improvements * Added `import std/strbasics` to `tests/ic/tmiscs.nim` to ensure required symbols are available for tests. I tried to improve `resolveFile`, which is harder because either we need to add `lib/std` to search path and all of other nested directory to `--path` in `config/nim.cfg`. So I choose toi reuse `findModule` for imports
95 lines
1.5 KiB
Nim
95 lines
1.5 KiB
Nim
discard """
|
|
output: '''
|
|
42
|
|
5
|
|
3
|
|
2
|
|
1.0
|
|
2.0
|
|
55
|
|
@[1, 2]
|
|
'''
|
|
"""
|
|
import std/strbasics
|
|
|
|
# Object variant / case object
|
|
type
|
|
NodeKind = enum
|
|
nkInt, nkStr, nkAdd
|
|
|
|
Node = object
|
|
case kind: NodeKind
|
|
of nkInt: intVal: int
|
|
of nkStr: strVal: string
|
|
of nkAdd: left, right: ref Node
|
|
|
|
proc newInt(v: int): ref Node =
|
|
new(result)
|
|
result[] = Node(kind: nkInt, intVal: v)
|
|
|
|
let n = newInt(42)
|
|
echo n.intVal
|
|
|
|
# Sink and move semantics
|
|
type
|
|
BigObj = object
|
|
data: seq[int]
|
|
|
|
proc consume(x: sink BigObj) =
|
|
echo x.data.len
|
|
|
|
var b = BigObj(data: @[1, 2, 3, 4, 5])
|
|
consume(move b)
|
|
|
|
proc divmod(a, b: int): (int, int) =
|
|
(a div b, a mod b)
|
|
|
|
|
|
let (q, r) = divmod(17, 5)
|
|
echo q
|
|
echo r
|
|
|
|
|
|
# Shallow object with seq (trigger GC interaction)
|
|
type
|
|
Matrix = object
|
|
rows, cols: int
|
|
data: seq[float]
|
|
|
|
proc newMatrix(r, c: int): Matrix =
|
|
Matrix(rows: r, cols: c, data: newSeq[float](r * c))
|
|
|
|
proc `[]`(m: Matrix, r, c: int): float =
|
|
m.data[r * m.cols + c]
|
|
|
|
proc `[]=`(m: var Matrix, r, c: int, v: float) =
|
|
m.data[r * m.cols + c] = v
|
|
|
|
var m = newMatrix(2, 2)
|
|
m[0, 0] = 1.0
|
|
m[1, 1] = 2.0
|
|
echo m[0, 0]
|
|
echo m[1, 1]
|
|
|
|
template compute(body: untyped): int =
|
|
block:
|
|
body
|
|
let x = compute:
|
|
var sum = 0
|
|
for i in 1..10: sum += i
|
|
sum
|
|
|
|
echo x
|
|
|
|
# Crash: bridge.nim(206, 5) `allowEmpty` unexpected nkEmpty [AssertionDefect]
|
|
# Bare closure iterator type alias
|
|
type IntIter = iterator(): int {.closure.}
|
|
proc run(it: IntIter): seq[int] =
|
|
result = @[]
|
|
for x in it():
|
|
result.add(x)
|
|
let gen: IntIter = iterator(): int {.closure.} =
|
|
yield 1
|
|
yield 2
|
|
echo run(gen)
|