Skip to content

Commit

Permalink
nixd/Sema: lowering for strings
Browse files Browse the repository at this point in the history
  • Loading branch information
inclyc committed Sep 24, 2023
1 parent 6e1270d commit 06f93ef
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 0 deletions.
23 changes: 23 additions & 0 deletions nixd/lib/Sema/Lowering.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -615,6 +615,29 @@ nix::Expr *Lowering::lower(const syntax::Node *Root) {
return Ctx.Pool.record(Ret);
}

case Node::NK_String: {
const auto *String = dynamic_cast<const syntax::String *>(Root);
auto *NixString = new nix::ExprString(std::string(String->S));
return Ctx.Pool.record(NixString);
}

case Node::NK_ConcatStrings: {
const auto &CS = dynamic_cast<const syntax::ConcatStrings *>(Root);
nix::PosIdx P = CS->Range.Begin;
auto *ES = Ctx.ESPool.record(new EvalContext::ES);
for (Node *SubStr : CS->SubStrings) {
auto *NixSubStr = lower(SubStr);
ES->emplace_back(std::pair{SubStr->Range.Begin, NixSubStr});
}
auto *NixConcatStrings = new nix::ExprConcatStrings(P, CS->ForceString, ES);
return Ctx.Pool.record(NixConcatStrings);
}

case Node::NK_InterpExpr: {
const auto &IE = dynamic_cast<const syntax::InterpExpr *>(Root);
return lower(IE->Body);
}

} // switch

return nullptr;
Expand Down
8 changes: 8 additions & 0 deletions nixd/tools/nixd-lint/test/lowering-string.nix
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
# RUN: nixd-lint -dump-nix-ast %s | FileCheck %s
{
# CHECK: ExprConcatStrings: (aa + "bbb" + cc)
s1 = "${aa}bbb${cc}";

# CHECK: ExprString: "aabbcc"
s2 = "aabbcc";
}

0 comments on commit 06f93ef

Please sign in to comment.