Skip to content

Commit

Permalink
Bind the standard library to a $std variable and use it in desugare…
Browse files Browse the repository at this point in the history
…d expressions. (#1158)

This keeps redefining `std` from breaking expressions that desugar to calls to standard library functions by redefining `std`.
  • Loading branch information
eduardosm authored Jun 23, 2024
1 parent 1e6cdc0 commit 03a3a39
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 15 deletions.
18 changes: 13 additions & 5 deletions core/desugarer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,7 @@ static constexpr char STD_CODE[] = {
*/
class Desugarer {
Allocator *alloc;
bool isStdlib;

template <class T, class... Args>
T *make(Args &&... args)
Expand Down Expand Up @@ -139,7 +140,10 @@ class Desugarer {

Var *std(void)
{
return var(id(U"std"));
// In most places, there is a "$std" variable inserted by
// the desugarer. On the standard library itself there isn't,
// so use "std" instead.
return var(id(isStdlib ? U"std" : U"$std"));
}

Local::Bind bind(const Identifier *id, AST *body)
Expand Down Expand Up @@ -219,7 +223,7 @@ class Desugarer {
}

public:
Desugarer(Allocator *alloc) : alloc(alloc) {}
Desugarer(Allocator *alloc, bool isStdlib = false) : alloc(alloc), isStdlib(isStdlib) {}

void desugarParams(ArgParams &params, unsigned obj_level)
{
Expand Down Expand Up @@ -999,13 +1003,17 @@ class Desugarer {
make<Var>(E, line_end, body)));
}

// local std = (std.jsonnet stuff); ast
ast = make<Local>(ast->location, EF, singleBind(id(U"std"), std_obj), ast);
// local $std = (std.jsonnet stuff); std = $std; ast
// The standard library is bound to $std, which cannot be overriden,
// so redefining std won't break expressions that desugar to calls
// to standard library functions.
ast = make<Local>(ast->location, EF, singleBind(id(U"std"), std()), ast);
ast = make<Local>(ast->location, EF, singleBind(id(U"$std"), std_obj), ast);
}
};

DesugaredObject *makeStdlibAST(Allocator *alloc, std::string filename) {
Desugarer desugarer(alloc);
Desugarer desugarer(alloc, true);
return desugarer.stdlibAST(filename);
}

Expand Down
20 changes: 10 additions & 10 deletions doc/ref/spec.html
Original file line number Diff line number Diff line change
Expand Up @@ -1125,7 +1125,7 @@ <h3 id="desugaring">Desugaring</h3>
</p>
<div class="desugar-rule">
\[
desugar(e) = desugar_{expr}(\local{\texttt{std} = e_{std}}{e}, false)
desugar(e) = desugar_{expr}(\local{\texttt{\$std} = e_{std}}{\local{\texttt{std} = \texttt{\$std}}{e}}, false)
\]
</div>
</div>
Expand Down Expand Up @@ -1287,7 +1287,7 @@ <h3 id="desugaring">Desugaring</h3>
<div class="desugar-rule">
\[
desugar_{expr}(e[e':e'':e'''], b) =
desugar_{expr}(\texttt{std.slice}(e, e', e'', e'''), b)
desugar_{expr}(\texttt{\$std.slice}(e, e', e'', e'''), b)
\]
</div>

Expand Down Expand Up @@ -1318,27 +1318,27 @@ <h3 id="desugaring">Desugaring</h3>

<div class="desugar-rule">
\[
desugar_{expr}(e \mathop{==} e', b) = desugar_{expr}(\texttt{std.equals}(e, e'), b)
desugar_{expr}(e \mathop{==} e', b) = desugar_{expr}(\texttt{\$std.equals}(e, e'), b)
\]
</div>

<div class="desugar-rule">
\[
desugar_{expr}(e \mathop{\%} e', b) = desugar_{expr}(\texttt{std.mod}(e, e'), b)
desugar_{expr}(e \mathop{\%} e', b) = desugar_{expr}(\texttt{\$std.mod}(e, e'), b)
\]
</div>

<div class="desugar-rule">
\[
desugar_{expr}(e \mathop{\texttt{in}} e', b) =
desugar_{expr}(\texttt{std.objectHasEx}(e', e, \texttt{true}), b)
desugar_{expr}(\texttt{\$std.objectHasEx}(e', e, \texttt{true}), b)
\]
</div>

<div class="desugar-rule">
\[
desugar_{expr}(e \mathop{\texttt{in}} \texttt{super}, b) =
desugar_{expr}(\texttt{std.objectHasEx}(\texttt{super}, e, \texttt{true}), b)
desugar_{expr}(\texttt{\$std.objectHasEx}(\texttt{super}, e, \texttt{true}), b)
\]
</div>
</div>
Expand Down Expand Up @@ -1484,8 +1484,8 @@ <h3 id="desugaring">Desugaring</h3>
\hspace{10mm}\textrm{Let }arr, i\textrm{ fresh} \\
\hspace{10mm}desugar_{expr}(
\local{arr = e'}{
\texttt{std.join}(\\\hspace{20mm}[\ ], \texttt{std.makeArray}(
\texttt{std.length}(arr),
\texttt{\$std.join}(\\\hspace{20mm}[\ ], \texttt{\$std.makeArray}(
\texttt{\$std.length}(arr),
\function{i}{\local{x = arr[i]}{desugar_{arrcomp}(e, compspec, b)}}
))
},
Expand All @@ -1499,8 +1499,8 @@ <h3 id="desugaring">Desugaring</h3>
\hspace{10mm}\textrm{Let }arr, i\textrm{ fresh} \\
\hspace{10mm}desugar_{expr}(
\local{arr = e'}{
\texttt{std.join}(\\\hspace{20mm}[\ ], \texttt{std.makeArray}(
\texttt{std.length}(arr),
\texttt{\$std.join}(\\\hspace{20mm}[\ ], \texttt{\$std.makeArray}(
\texttt{\$std.length}(arr),
\function{i}{\local{x = arr[i]}{[e]}}
))
},
Expand Down

0 comments on commit 03a3a39

Please sign in to comment.