xtfc / mold

A fresh (ironic? maybe!) approach to project chores.
https://xtfc.org/mold
MIT License
7 stars 0 forks source link

Add "default value" statements #129

Closed scizzorz closed 4 years ago

scizzorz commented 4 years ago

The implementation was a little trickier than I intended because the behavior was a little trickier than I expected. The current details for handling variable assignments are roughly as follows:

So, in short, the variable priority is roughly: Recipe variables > Moldfile variables > environment variables. Because of this ordering, it's not possible for Mold to provide a default value for a variable that can be overridden by the environment; any default values will have to be handled in the script rather than in Mold.

The open issue #128 is intended to address this, and I was expecting that I could simply parse the default assignment and apply it if the variable map and environment turned up empty. Because of the multi-layered variable system (Mold, Moldfile, and Recipe), my initial assumptions don't work out well without breaking some boundaries: a Moldfile would need to inspect the variable map of the Mold that's opening it, and a Recipe would need to inspect the variable map of the Moldfile that defines it, etc. Unfortunately, I don't think that's feasible and would significantly impact the interfaces between those concepts.

The approach I've taken here is to add a secondary variable map, defaults, for all three structures. This variable map takes the opposite prioritization approach: the first definition takes priority, rather than the last. That is:

var foo = "one"
var foo = "two"

$foo would evaluate to "two" here (last assignment takes priority), while:

default foo = "one"
default foo = "two"

$foo would evaluate to "one" here (first assignment takes priority).

Following the way that Recipes are treated as happening "after" the Moldfile, allowing their variables override the file's, but respecting the inversion of priority for default variables, the file's defaults override the recipe's defaults. Default values are appended to the priority list after environment variables, making the final priority list look something like this: Recipe variables > Moldfile variables > environment variables > Moldfile defaults > Recipe defaults.

scizzorz commented 4 years ago

The PR is updated with some more behavioral changes. Notably: