@@ -3,23 +3,31 @@ import compiler / [syntaxes, reorder, vmdef, msgs]
3
3
import compiler / passes {.all .}
4
4
5
5
{.warning [UnusedImport ]: off .}
6
- include compiler / [nimeval]
6
+ include compiler / [nimeval, pipelines ]
7
7
8
8
export Interpreter , VmArgs , PCtx , PStackFrame , TLineInfo
9
9
10
+ # NOTE: This file is mostly made up of modified functions pulled from the nim
11
+ # compiler, and must be updated occasionally to keep up with changes to the vm.
12
+ # To make diffing easier, the original casing has been preserved, so this file
13
+ # is in `camelCase` rather than `snake_case` like the rest of the project.
14
+
10
15
# adapted from
11
- # https://github.com/nim-lang/Nim/blob/version-1-6 /compiler/passes .nim#L120
12
- # Normal module loading procedure, but makes TPassContextArray a var param
13
- # so it can be passed to extend_module
16
+ # https://github.com/nim-lang/Nim/blob/v2.0.2 /compiler/pipelines .nim#L88
17
+ # Normal module loading procedure, but makes PContext a param so it can be
18
+ # passed to extend_module
14
19
proc processModule * (graph: ModuleGraph ; module: PSym ; idgen: IdGenerator ;
15
- stream: PLLStream , a: var TPassContextArray ): bool {.discardable .} =
20
+ stream: PLLStream , ctx: var PContext ): bool {.discardable .} =
21
+
16
22
if graph.stopCompile (): return true
23
+ let bModule = setupEvalGen (graph, module, idgen)
24
+
17
25
var
18
26
p: Parser
19
27
s: PLLStream
20
28
fileIdx = module.fileIdx
29
+
21
30
prepareConfigNotes (graph, module)
22
- openPasses (graph, a, module, idgen)
23
31
if stream == nil :
24
32
let filename = toFullPathConsiderDirty (graph.config, fileIdx)
25
33
s = llStreamOpen (filename, fmRead)
@@ -29,62 +37,42 @@ proc processModule*(graph: ModuleGraph; module: PSym; idgen: IdGenerator;
29
37
else :
30
38
s = stream
31
39
32
- when defined (nimsuggest):
33
- let filename = toFullPathConsiderDirty (graph.config, fileIdx).string
34
- msgs.setHash (graph.config, fileIdx, $ sha1.secureHashFile (filename))
35
-
36
40
while true :
37
- openParser (p, fileIdx, s, graph.cache, graph.config)
41
+ syntaxes. openParser (p, fileIdx, s, graph.cache, graph.config)
38
42
39
43
if not belongsToStdlib (graph, module) or (belongsToStdlib (graph, module) and module.name.s == " distros" ):
40
44
# XXX what about caching? no processing then? what if I change the
41
45
# modules to include between compilation runs? we'd need to track that
42
46
# in ROD files. I think we should enable this feature only
43
47
# for the interactive mode.
44
48
if module.name.s != " nimscriptapi" :
45
- processImplicits graph, graph.config.implicitImports, nkImportStmt, a, module
46
- processImplicits graph, graph.config.implicitIncludes, nkIncludeStmt, a, module
49
+ processImplicitImports graph, graph.config.implicitImports, nkImportStmt, module, ctx, bModule, idgen
50
+ processImplicitImports graph, graph.config.implicitIncludes, nkIncludeStmt, module, ctx, bModule, idgen
47
51
48
- while true :
49
- if graph.stopCompile (): break
52
+ checkFirstLineIndentation (p)
53
+ block processCode:
54
+ if graph.stopCompile (): break processCode
50
55
var n = parseTopLevelStmt (p)
51
- if n.kind == nkEmpty: break
52
- if (sfSystemModule notin module.flags and
53
- ({sfNoForward, sfReorder} * module.flags != {} or
54
- codeReordering in graph.config.features)):
55
- # read everything, no streaming possible
56
- var sl = newNodeI (nkStmtList, n.info)
56
+ if n.kind == nkEmpty: break processCode
57
+ # read everything, no streaming possible
58
+ var sl = newNodeI (nkStmtList, n.info)
59
+ sl.add n
60
+ while true :
61
+ var n = parseTopLevelStmt (p)
62
+ if n.kind == nkEmpty: break
57
63
sl.add n
58
- while true :
59
- var n = parseTopLevelStmt (p)
60
- if n.kind == nkEmpty: break
61
- sl.add n
62
- if sfReorder in module.flags or codeReordering in graph.config.features:
63
- sl = reorder (graph, sl, module)
64
- discard processTopLevelStmt (graph, sl, a)
65
- break
66
- elif n.kind in imperativeCode:
67
- # read everything until the next proc declaration etc.
68
- var sl = newNodeI (nkStmtList, n.info)
69
- sl.add n
70
- var rest: PNode = nil
71
- while true :
72
- var n = parseTopLevelStmt (p)
73
- if n.kind == nkEmpty or n.kind notin imperativeCode:
74
- rest = n
75
- break
76
- sl.add n
77
- # echo "-----\n", sl
78
- if not processTopLevelStmt (graph, sl, a): break
79
- if rest != nil :
80
- # echo "-----\n", rest
81
- if not processTopLevelStmt (graph, rest, a): break
82
- else :
83
- # echo "----- single\n", n
84
- if not processTopLevelStmt (graph, n, a): break
64
+
65
+ prePass (ctx, sl)
66
+ var semNode = semWithPContext (ctx, sl)
67
+ discard processPipeline (graph, semNode, bModule)
68
+
85
69
closeParser (p)
86
70
if s.kind != llsStdIn: break
87
- closePasses (graph, a)
71
+
72
+ assert graph.pipelinePass == EvalPass
73
+ let finalNode = closePContext (graph, ctx, nil )
74
+ discard interpreterCode (bModule, finalNode)
75
+
88
76
if graph.config.backend notin {backendC, backendCpp, backendObjc}:
89
77
# We only write rod files here if no C-like backend is active.
90
78
# The C-like backends have been patched to support the IC mechanism.
@@ -130,8 +118,8 @@ proc resetModule*(i: Interpreter, moduleName: string) =
130
118
iface.module.ast = nil
131
119
break
132
120
133
- proc loadModule * (i: Interpreter , fileName, code: string ,
134
- a: var TPassContextArray ) {.gcsafe .} =
121
+ proc loadModule * (i: Interpreter , fileName, code: string , ctx: var PContext
122
+ ) {.gcsafe .} =
135
123
136
124
assert i != nil
137
125
@@ -156,61 +144,53 @@ proc loadModule*(i: Interpreter, fileName, code: string,
156
144
# which causes "cannot evaluate at compile time" issues with some variables.
157
145
# Force things back to emRepl.
158
146
PCtx (i.graph.vm).mode = emRepl
147
+
148
+ ctx = preparePContext (i.graph, module, i.idgen)
149
+
159
150
{.gcsafe .}:
160
- discard processModule (i.graph, module, i.idgen, stream, a )
151
+ discard processModule (i.graph, module, i.idgen, stream, ctx )
161
152
162
153
# adapted from
163
- # https://github.com/nim-lang/Nim/blob/version-1-6 /compiler/passes .nim#L120
164
- proc extendModule (graph: ModuleGraph ; a: var TPassContextArray , module: PSym ;
165
- idgen: IdGenerator ; stream: PLLStream ): bool {.discardable .} =
154
+ # https://github.com/nim-lang/Nim/blob/v2.0.2 /compiler/pipelines .nim#L88
155
+ proc extendModule * (graph: ModuleGraph ; module: PSym ; idgen: IdGenerator ;
156
+ stream: PLLStream , ctx: var PContext ): bool {.discardable .} =
166
157
167
158
if graph.stopCompile (): return true
159
+ let bModule = setupEvalGen (graph, module, idgen)
160
+
168
161
var
169
162
p: Parser
170
163
s = stream
171
164
fileIdx = module.fileIdx
172
165
173
166
while true :
174
- openParser (p, fileIdx, s, graph.cache, graph.config)
167
+ syntaxes. openParser (p, fileIdx, s, graph.cache, graph.config)
175
168
176
- while true :
177
- if graph.stopCompile (): break
169
+ checkFirstLineIndentation (p)
170
+ assert graph.pipelinePass == EvalPass
171
+ block processCode:
172
+ if graph.stopCompile (): break processCode
178
173
var n = parseTopLevelStmt (p)
179
- if n.kind == nkEmpty: break
180
- if (sfSystemModule notin module.flags and
181
- ({sfNoForward, sfReorder} * module.flags != {} or
182
- codeReordering in graph.config.features)):
183
- # read everything, no streaming possible
184
- var sl = newNodeI (nkStmtList, n.info)
185
- sl.add n
186
- while true :
187
- var n = parseTopLevelStmt (p)
188
- if n.kind == nkEmpty: break
189
- sl.add n
190
-
191
- discard processTopLevelStmt (graph, sl, a)
192
- break
193
- elif n.kind in imperativeCode:
194
- # read everything until the next proc declaration etc.
195
- var sl = newNodeI (nkStmtList, n.info)
174
+ if n.kind == nkEmpty: break processCode
175
+ # read everything, no streaming possible
176
+ var sl = newNodeI (nkStmtList, n.info)
177
+ sl.add n
178
+ while true :
179
+ var n = parseTopLevelStmt (p)
180
+ if n.kind == nkEmpty: break
196
181
sl.add n
197
- var rest: PNode = nil
198
- while true :
199
- var n = parseTopLevelStmt (p)
200
- if n.kind == nkEmpty or n.kind notin imperativeCode:
201
- rest = n
202
- break
203
- sl.add n
204
- if not processTopLevelStmt (graph, sl, a): break
205
- if rest != nil :
206
- if not processTopLevelStmt (graph, rest, a): break
207
- else :
208
- if not processTopLevelStmt (graph, n, a): break
182
+
183
+ prePass (ctx, sl)
184
+
185
+ var semNode = semWithPContext (ctx, sl)
186
+ discard processPipeline (graph, semNode, bModule)
187
+
209
188
closeParser (p)
210
189
if s.kind != llsStdIn: break
190
+
211
191
result = true
212
192
213
- proc eval * (i: Interpreter , a : var TPassContextArray , fileName, code: string ) =
193
+ proc eval * (i: Interpreter , ctx : var PContext , fileName, code: string ) =
214
194
# # This can also be used to *reload* the script.
215
195
assert i != nil
216
196
var module: PSym
@@ -222,7 +202,7 @@ proc eval*(i: Interpreter, a: var TPassContextArray, fileName, code: string) =
222
202
223
203
assert module != nil , " no valid module selected"
224
204
let s = llStreamOpen (code)
225
- extendModule (i.graph, a, module, i.idgen, s)
205
+ extendModule (i.graph, module, i.idgen, s, ctx )
226
206
227
207
proc config * (i: Interpreter ): ConfigRef = i.graph.config
228
208
0 commit comments