Skip to content

Commit 2e207ca

Browse files
authored
import within when (#1158)
1 parent 3a027d7 commit 2e207ca

File tree

7 files changed

+48
-50
lines changed

7 files changed

+48
-50
lines changed

src/nifler/bridge.nim

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -116,6 +116,7 @@ type
116116
b, deps: Builder
117117
portablePaths: bool
118118
depsEnabled, lineInfoEnabled: bool
119+
inWhen: int
119120

120121
proc absLineInfo(i: TLineInfo; c: var TranslationContext) =
121122
var fp = toFullPath(c.conf, i.fileIndex)
@@ -175,7 +176,7 @@ proc splitIdentDefName(n: PNode): IdentDefName =
175176
else:
176177
result.name = n
177178

178-
proc toNif*(n, parent: PNode; c: var TranslationContext; allowEmpty = false)
179+
proc toNif*(n, parent: PNode; c: var TranslationContext; allowEmpty = false)
179180

180181
proc toVarTuple(v: PNode, n: PNode; c: var TranslationContext) =
181182
c.b.addTree(UnpacktupL)
@@ -621,7 +622,16 @@ proc toNif*(n, parent: PNode; c: var TranslationContext; allowEmpty = false) =
621622
let oldDepsEnabled = c.depsEnabled
622623
swap c.b, c.deps
623624
c.depsEnabled = false
624-
toNif(n, nil, c)
625+
626+
relLineInfo(n, nil, c)
627+
c.b.addTree(nodeKindTranslation(n.kind))
628+
if c.inWhen > 0:
629+
# mark it as a conditional dependency:
630+
c.b.addKeyw "when"
631+
for i in 0..<n.len:
632+
toNif(n[i], nil, c)
633+
c.b.endTree()
634+
625635
c.depsEnabled = oldDepsEnabled
626636
swap c.b, c.deps
627637
c.lineInfoEnabled = oldLineInfoEnabled
@@ -650,6 +660,14 @@ proc toNif*(n, parent: PNode; c: var TranslationContext; allowEmpty = false) =
650660
for i in 0..<n.len:
651661
toNif(n[i], n, c)
652662
c.b.endTree()
663+
of nkWhenStmt:
664+
inc c.inWhen
665+
relLineInfo(n, parent, c)
666+
c.b.addTree(nodeKindTranslation(n.kind))
667+
for i in 0..<n.len:
668+
toNif(n[i], n, c)
669+
c.b.endTree()
670+
dec c.inWhen
653671
else:
654672
relLineInfo(n, parent, c)
655673
c.b.addTree(nodeKindTranslation(n.kind))

src/nimony/deps.nim

Lines changed: 20 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -138,6 +138,8 @@ proc processImport(c: var DepContext; it: var Cursor; current: Node) =
138138
var x = it
139139
skip it
140140
inc x # skip the `import`
141+
# ignore conditional imports:
142+
if x.stmtKind == WhenS: return
141143
while x.kind != ParRi:
142144
if x.kind == ParLe and x.exprKind == PragmaxX:
143145
inc x
@@ -163,6 +165,8 @@ proc processSingleImport(c: var DepContext; it: var Cursor; current: Node) =
163165
var x = it
164166
skip it
165167
inc x # skip the tag
168+
# ignore conditional imports:
169+
if x.stmtKind == WhenS: return
166170
var files: seq[ImportedFilename] = @[]
167171
var hasError = false
168172
filenameVal(x, files, hasError, allowAs = true)
@@ -196,12 +200,15 @@ proc processDeps(c: var DepContext; n: Cursor; current: Node) =
196200
while n.kind != ParRi:
197201
processDep c, n, current
198202

199-
proc execNifler(c: var DepContext; input, output: string) =
200-
if not c.forceRebuild and semos.fileExists(output) and semos.fileExists(input) and
201-
getLastModificationTime(output) > getLastModificationTime(input):
203+
proc execNifler(c: var DepContext; f: FilePair) =
204+
let output = c.config.parsedFile(f)
205+
let depsFile = c.config.depsFile(f)
206+
if not c.forceRebuild and semos.fileExists(output) and
207+
semos.fileExists(f.nimFile) and getLastModificationTime(output) > getLastModificationTime(f.nimFile) and
208+
semos.fileExists(depsFile) and getLastModificationTime(depsFile) > getLastModificationTime(f.nimFile):
202209
discard "nothing to do"
203210
else:
204-
let cmd = quoteShell(c.nifler) & " --portablePaths --deps parse " & quoteShell(input) & " " &
211+
let cmd = quoteShell(c.nifler) & " --portablePaths --deps parse " & quoteShell(f.nimFile) & " " &
205212
quoteShell(output)
206213
exec cmd
207214

@@ -210,13 +217,13 @@ proc importSystem(c: var DepContext; current: Node) =
210217
current.deps.add p
211218
if not c.processedModules.containsOrIncl(p.modname):
212219
#echo "NIFLING ", p.nimFile, " -> ", c.config.parsedFile(p)
213-
execNifler c, p.nimFile, c.config.parsedFile(p)
220+
execNifler c, p
214221
var imported = Node(files: @[p], id: c.nodes.len, parent: current.id, isSystem: true)
215222
c.nodes.add imported
216223
parseDeps c, p, imported
217224

218225
proc parseDeps(c: var DepContext; p: FilePair; current: Node) =
219-
execNifler c, p.nimFile, c.config.parsedFile(p)
226+
execNifler c, p
220227

221228
let depsFile = if c.isGeneratingFinal: c.config.deps2File(p) else: c.config.depsFile(p)
222229
var stream = nifstreams.open(depsFile)
@@ -405,40 +412,7 @@ proc generateFrontendBuildFile(c: DepContext; commandLineArgs: string): string =
405412
if arg.len > 0:
406413
b.addStrLit arg
407414
b.addStrLit "m"
408-
b.withTree "input":
409-
b.addIntLit 0 # main parsed file
410-
b.withTree "output":
411-
b.addIntLit 0 # semmed file output
412-
b.withTree "output":
413-
b.addIntLit 1 # index file output
414-
415-
# Command for nimsem with system flag
416-
b.withTree "cmd":
417-
b.addSymbolDef "nimsem_system"
418-
b.addStrLit c.nimsem
419-
if commandLineArgs.len > 0:
420-
for arg in commandLineArgs.split(' '):
421-
if arg.len > 0:
422-
b.addStrLit arg
423-
b.addStrLit "--isSystem"
424-
b.addStrLit "m"
425-
b.withTree "input":
426-
b.addIntLit 0 # main parsed file
427-
b.withTree "output":
428-
b.addIntLit 0 # semmed file output
429-
b.withTree "output":
430-
b.addIntLit 1 # index file output
431-
432-
# Command for nimsem with main flag
433-
b.withTree "cmd":
434-
b.addSymbolDef "nimsem_main"
435-
b.addStrLit c.nimsem
436-
if commandLineArgs.len > 0:
437-
for arg in commandLineArgs.split(' '):
438-
if arg.len > 0:
439-
b.addStrLit arg
440-
b.addStrLit "--isMain"
441-
b.addStrLit "m"
415+
b.addKeyw "args"
442416
b.withTree "input":
443417
b.addIntLit 0 # main parsed file
444418
b.withTree "output":
@@ -451,12 +425,12 @@ proc generateFrontendBuildFile(c: DepContext; commandLineArgs: string): string =
451425
for v in c.nodes:
452426
b.withTree "do":
453427
# Choose the right command based on flags
454-
if v.isSystem:
455-
b.addIdent "nimsem_system"
456-
elif i == 0: # first node is main
457-
b.addIdent "nimsem_main"
458-
else:
459-
b.addIdent "nimsem"
428+
b.addIdent "nimsem"
429+
b.withTree "args":
430+
if v.isSystem:
431+
b.addStrLit "--isSystem"
432+
elif i == 0: # first node is main
433+
b.addStrLit "--isMain"
460434

461435
# Input: parsed file
462436
var seenDeps = initHashSet[string]()

src/nimony/nimony.nim

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -200,6 +200,7 @@ proc handleCmdLine() =
200200
requiresTool "nimsem", "src/nimony/nimsem.nim", fullRebuild
201201
requiresTool "hexer", "src/hexer/hexer.nim", fullRebuild
202202
requiresTool "nifc", "src/nifc/nifc.nim", fullRebuild
203+
requiresTool "nifmake", "src/nifmake/nifmake.nim", fullRebuild
203204
# compile full project modules
204205
buildGraph config, args[0], forceRebuild, silentMake,
205206
commandLineArgs, commandLineArgsNifc, moduleFlags, (if doRun: DoRun else: DoCompile),

src/nimony/sem.nim

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2104,7 +2104,9 @@ proc semWhen(c: var SemContext; it: var Item) =
21042104
of SemcheckSignatures, SemcheckBodies:
21052105
discard
21062106

2107+
inc c.inWhen
21072108
semWhenImpl(c, it, NormalWhen)
2109+
dec c.inWhen
21082110

21092111
proc semCaseOfValue(c: var SemContext; it: var Item; selectorType: TypeCursor;
21102112
seen: var seq[(xint, xint)]) =

src/nimony/semdata.nim

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,7 @@ type
9494
usedTypevars*: int
9595
phase*: SemPhase
9696
canSelfExec*: bool
97+
inWhen*: int
9798
templateInstCounter*: int
9899
commandLineArgs*: string # for IC we make nimony `exec` itself. Thus it is important
99100
# to forward command line args properly.

src/nimony/semimport.nim

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ proc importSingleFile(c: var SemContext; f1: ImportedFilename; origin: string;
5151
result = SymId(0)
5252
if not c.processedModules.contains(suffix):
5353
c.meta.importedFiles.add f2
54-
if c.canSelfExec and needsRecompile(f2, suffixToNif suffix):
54+
if (c.canSelfExec or c.inWhen > 0) and needsRecompile(f2, suffixToNif suffix):
5555
selfExec c, f2, (if f1.isSystem: " --isSystem" else: "")
5656

5757
let moduleName = pool.strings.getOrIncl(f1.name)

src/nimony/semos.nim

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -274,7 +274,9 @@ proc getFile*(info: PackedLineInfo): string =
274274
result = ""
275275

276276
proc selfExec*(c: var SemContext; file: string; moreArgs: string) =
277-
exec os.getAppFilename() & c.commandLineArgs & moreArgs & " --ischild m " & quoteShell(file)
277+
let nimonyExe = findTool("nimony")
278+
exec quoteShell(nimonyExe) & c.commandLineArgs & moreArgs & " --ischild m " & quoteShell(file)
279+
#exec os.getAppFilename() & c.commandLineArgs & moreArgs & " --ischild m " & quoteShell(file)
278280

279281
# ------------------ plugin handling --------------------------
280282

0 commit comments

Comments
 (0)