Skip to content

Commit

Permalink
nixd/Sema: move lowering ctx into the Lowering class
Browse files Browse the repository at this point in the history
  • Loading branch information
inclyc committed Sep 19, 2023
1 parent f28cf6a commit 4011c13
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 19 deletions.
7 changes: 4 additions & 3 deletions nixd/include/nixd/Sema/Lowering.h
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,11 @@ struct Lowering {
nix::SymbolTable &STable;
nix::PosTable &PTable;
std::vector<syntax::Diagnostic> &Diags;
EvalContext &Ctx;

nix::Expr *lower(EvalContext &Ctx, syntax::Node *Root);
nix::ExprLambda *lowerFunction(EvalContext &Ctx, syntax::Function *Fn);
nix::Formal lowerFormal(EvalContext &Ctx, const syntax::Formal &Formal);
nix::Expr *lower(syntax::Node *Root);
nix::ExprLambda *lowerFunction(syntax::Function *Fn);
nix::Formal lowerFormal(const syntax::Formal &Formal);
};

} // namespace nixd
24 changes: 11 additions & 13 deletions nixd/lib/Sema/Lowering.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,8 @@

namespace nixd {

nix::Formal Lowering::lowerFormal(EvalContext &Ctx,
const syntax::Formal &Formal) {
auto *Def = Lowering::lower(Ctx, Formal.Default);
nix::Formal Lowering::lowerFormal(const syntax::Formal &Formal) {
auto *Def = Lowering::lower(Formal.Default);
nix::Formal F;

F.pos = Formal.Range.Begin;
Expand All @@ -16,8 +15,7 @@ nix::Formal Lowering::lowerFormal(EvalContext &Ctx,
return F;
}

nix::ExprLambda *Lowering::lowerFunction(EvalContext &Ctx,
syntax::Function *Fn) {
nix::ExprLambda *Lowering::lowerFunction(syntax::Function *Fn) {
// Implementation note:
// The official parser does this in the semantic action, and we deferred it
// here, as a part of the progressive lowering process.
Expand Down Expand Up @@ -92,12 +90,12 @@ nix::ExprLambda *Lowering::lowerFunction(EvalContext &Ctx,
auto *NixFormals = Ctx.FormalsPool.record(new nix::Formals);
NixFormals->ellipsis = Formals->Ellipsis;
for (auto [_, Formal] : Names) {
nix::Formal F = lowerFormal(Ctx, *Formal);
nix::Formal F = lowerFormal(*Formal);
NixFormals->formals.emplace_back(F);
}
}

auto *Body = lower(Ctx, Fn->Body);
auto *Body = lower(Fn->Body);

nix::ExprLambda *NewLambda;
if (Fn->Arg) {
Expand All @@ -111,7 +109,7 @@ nix::ExprLambda *Lowering::lowerFunction(EvalContext &Ctx,
return NewLambda;
}

nix::Expr *Lowering::lower(EvalContext &Ctx, nixd::syntax::Node *Root) {
nix::Expr *Lowering::lower(nixd::syntax::Node *Root) {
if (!Root)
return nullptr;

Expand All @@ -120,20 +118,20 @@ nix::Expr *Lowering::lower(EvalContext &Ctx, nixd::syntax::Node *Root) {
switch (Root->getKind()) {
case Node::NK_Function: {
auto *Fn = dynamic_cast<syntax::Function *>(Root);
return lowerFunction(Ctx, Fn);
return lowerFunction(Fn);
}
case Node::NK_Assert: {
auto *Assert = dynamic_cast<syntax::Assert *>(Root);
auto *Cond = lower(Ctx, Assert->Cond);
auto *Body = lower(Ctx, Assert->Body);
auto *Cond = lower(Assert->Cond);
auto *Body = lower(Assert->Body);
auto *NixAssert =
Ctx.Pool.record(new nix::ExprAssert(Assert->Range.Begin, Cond, Body));
return NixAssert;
}
case Node::NK_With: {
auto *With = dynamic_cast<syntax::With *>(Root);
auto *Attrs = lower(Ctx, With->Attrs);
auto *Body = lower(Ctx, With->Body);
auto *Attrs = lower(With->Attrs);
auto *Body = lower(With->Body);
auto *NixWith =
Ctx.Pool.record(new nix::ExprWith(With->Range.Begin, Attrs, Body));
return NixWith;
Expand Down
6 changes: 3 additions & 3 deletions nixd/tools/nixd-lint/nixd-lint.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -99,10 +99,10 @@ int main(int argc, char *argv[]) {
nixd::syntax::ParseData Data{.State = S, .Origin = Origin};
nixd::syntax::parse(Buffer, &Data);

nixd::Lowering Lowering{
.STable = *STable, .PTable = *PTable, .Diags = Data.Diags};
nixd::EvalContext Ctx;
Lowering.lower(Ctx, Data.Result);
nixd::Lowering Lowering{
.STable = *STable, .PTable = *PTable, .Diags = Data.Diags, .Ctx = Ctx};
Lowering.lower(Data.Result);

for (const auto &Diag : Data.Diags) {
auto BeginPos = (*PTable)[Diag.Range.Begin];
Expand Down

0 comments on commit 4011c13

Please sign in to comment.