Skip to content

Commit 575ac19

Browse files
committed
[skiplang/skc] Move #env() macro expansion to naming phase.
1 parent 1554a64 commit 575ac19

File tree

2 files changed

+34
-36
lines changed

2 files changed

+34
-36
lines changed

skiplang/compiler/src/skipExpand.sk

Lines changed: 1 addition & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -3419,39 +3419,7 @@ fun expr_(
34193419
functionName,
34203420
expr(context, env, body),
34213421
)
3422-
| A.EnvMacro(varName) ->
3423-
(pos2, name) = varName;
3424-
pkg_env = SKStore.EHandle(
3425-
SKStore.SID::keyType,
3426-
SKStore.StringFile::type,
3427-
// FIXME: Ensure package names cannot start with an underscore to avoid clash with `_default`.
3428-
SKStore.DirName::create(
3429-
`/packageEnv/${pos2.file.pkg_opt.default("_default")}/`,
3430-
),
3431-
);
3432-
value = pkg_env.maybeGet(context, SKStore.SID(name)) match {
3433-
| None() ->
3434-
SkipError.error(pos2, `Environment variable ${name} is not set.`)
3435-
| Some(v) -> v.value
3436-
};
3437-
// Record access to env variable.
3438-
cur_env_map = context.getGlobal(FileCache.kEnvAccessGlobal) match {
3439-
| None() -> SortedMap[]
3440-
| Some(envMapFile) -> FileCache.EnvMapFile::type(envMapFile).value
3441-
};
3442-
context.setGlobal(
3443-
FileCache.kEnvAccessGlobal,
3444-
FileCache.EnvMapFile(
3445-
cur_env_map.set(
3446-
pos2.file.pkg_opt,
3447-
cur_env_map
3448-
.maybeGet(pos2.file.pkg_opt)
3449-
.default(SortedSet[])
3450-
.set(name),
3451-
),
3452-
),
3453-
);
3454-
A.Literal(A.StringLiteral(value))
3422+
| A.EnvMacro(varName) -> A.EnvMacro(varName)
34553423
}
34563424
}
34573425

skiplang/compiler/src/skipNaming.sk

Lines changed: 33 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3660,6 +3660,37 @@ fun expandForEachMacro(
36603660
N.Bind(iteratorn, getIterator, flagged)
36613661
}
36623662

3663+
fun expandEnvMacro(context: mutable SKStore.Context, varName: A.Name): N.Expr_ {
3664+
(pos, name) = varName;
3665+
pkg_env = SKStore.EHandle(
3666+
SKStore.SID::keyType,
3667+
SKStore.StringFile::type,
3668+
// FIXME: Ensure package names cannot start with an underscore to avoid clash with `_default`.
3669+
SKStore.DirName::create(
3670+
`/packageEnv/${pos.file.pkg_opt.default("_default")}/`,
3671+
),
3672+
);
3673+
value = pkg_env.maybeGet(context, SKStore.SID(name)) match {
3674+
| None() -> SkipError.error(pos, `Environment variable ${name} is not set.`)
3675+
| Some(v) -> v.value
3676+
};
3677+
// Record access to env variable.
3678+
cur_env_map = context.getGlobal(FileCache.kEnvAccessGlobal) match {
3679+
| None() -> SortedMap[]
3680+
| Some(envMapFile) -> FileCache.EnvMapFile::type(envMapFile).value
3681+
};
3682+
context.setGlobal(
3683+
FileCache.kEnvAccessGlobal,
3684+
FileCache.EnvMapFile(
3685+
cur_env_map.set(
3686+
pos.file.pkg_opt,
3687+
cur_env_map.maybeGet(pos.file.pkg_opt).default(SortedSet[]).set(name),
3688+
),
3689+
),
3690+
);
3691+
N.Literal(A.StringLiteral(value))
3692+
}
3693+
36633694
fun reportDuplicateMacro(
36643695
pos: FileRange,
36653696
id: String,
@@ -3816,9 +3847,8 @@ fun expr_(
38163847
| A.Literal(l) -> N.Literal(l)
38173848
| A.Var(n) -> N.Var(n)
38183849
| A.MacroVar(n) -> expandExpressionMacro(env, n)
3819-
| A.Seq _
3820-
| A.EnvMacro _ ->
3821-
invariant_violation("assert false")
3850+
| A.Seq _ -> invariant_violation("assert false")
3851+
| A.EnvMacro(n) -> expandEnvMacro(context, n)
38223852
| A.If(e1, e2, e3) ->
38233853
N.If(
38243854
expr(context, acc, env, e1),

0 commit comments

Comments
 (0)