@@ -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+
36633694fun 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