mu/archive/1.vm/099hardware_checks.cc

68 lines
2.5 KiB
C++
Raw Normal View History

2017-11-04 01:01:59 +00:00
//: Let's raise errors when students use real hardware in any recipes besides
//: 'main'. Part of the goal is to teach them testing hygiene and dependency
//: injection.
//:
//: This is easy to sidestep, it's for feedback rather than safety.
:(before "End Globals")
vector<type_tree*> Real_hardware_types;
:(before "Begin transform_all")
setup_real_hardware_types();
:(before "End transform_all")
teardown_real_hardware_types();
:(code)
void setup_real_hardware_types() {
Real_hardware_types.push_back(parse_type("address:screen"));
Real_hardware_types.push_back(parse_type("address:console"));
Real_hardware_types.push_back(parse_type("address:resources"));
}
type_tree* parse_type(string s) {
reagent x("x:"+s);
type_tree* result = x.type;
x.type = NULL; // don't deallocate on return
return result;
}
void teardown_real_hardware_types() {
for (int i = 0; i < SIZE(Real_hardware_types); ++i)
delete Real_hardware_types.at(i);
Real_hardware_types.clear();
}
:(before "End Checks")
Transform.push_back(check_for_misuse_of_real_hardware);
:(code)
void check_for_misuse_of_real_hardware(const recipe_ordinal r) {
const recipe& caller = get(Recipe, r);
if (caller.name == "main") return;
if (starts_with(caller.name, "scenario_")) return;
trace(101, "transform") << "--- check if recipe " << caller.name << " has any dependency-injection mistakes" << end();
for (int index = 0; index < SIZE(caller.steps); ++index) {
const instruction& inst = caller.steps.at(index);
if (is_primitive(inst.operation)) continue;
for (int i = 0; i < SIZE(inst.ingredients); ++i) {
const reagent& ing = inst.ingredients.at(i);
if (!is_literal(ing) || ing.name != "0") continue;
const recipe& callee = get(Recipe, inst.operation);
if (!callee.has_header) continue;
if (i >= SIZE(callee.ingredients)) continue;
const reagent& expected_ing = callee.ingredients.at(i);
for (int j = 0; j < SIZE(Real_hardware_types); ++j) {
if (*Real_hardware_types.at(j) == *expected_ing.type)
2017-05-26 23:43:18 +00:00
raise << maybe(caller.name) << "'" << to_original_string(inst) << "': only 'main' can pass 0 into a " << to_string(expected_ing.type) << '\n' << end();
}
}
}
}
5001 - drop the :(scenario) DSL I've been saying for a while[1][2][3] that adding extra abstractions makes things harder for newcomers, and adding new notations doubly so. And then I notice this DSL in my own backyard. Makes me feel like a hypocrite. [1] https://news.ycombinator.com/item?id=13565743#13570092 [2] https://lobste.rs/s/to8wpr/configuration_files_are_canary_warning [3] https://lobste.rs/s/mdmcdi/little_languages_by_jon_bentley_1986#c_3miuf2 The implementation of the DSL was also highly hacky: a) It was happening in the tangle/ tool, but was utterly unrelated to tangling layers. b) There were several persnickety constraints on the different kinds of lines and the specific order they were expected in. I kept finding bugs where the translator would silently do the wrong thing. Or the error messages sucked, and readers may be stuck looking at the generated code to figure out what happened. Fixing error messages would require a lot more code, which is one of my arguments against DSLs in the first place: they may be easy to implement, but they're hard to design to go with the grain of the underlying platform. They require lots of iteration. Is that effort worth prioritizing in this project? On the other hand, the DSL did make at least some readers' life easier, the ones who weren't immediately put off by having to learn a strange syntax. There were fewer quotes to parse, fewer backslash escapes. Anyway, since there are also people who dislike having to put up with strange syntaxes, we'll call that consideration a wash and tear this DSL out. --- This commit was sheer drudgery. Hopefully it won't need to be redone with a new DSL because I grow sick of backslashes.
2019-03-13 01:56:55 +00:00
void test_warn_on_using_real_screen_directly_in_non_main_recipe() {
Hide_errors = true;
transform(
"def foo [\n"
" print 0, 34\n"
"]\n"
);
CHECK_TRACE_CONTENTS(
"error: foo: 'print 0, 34': only 'main' can pass 0 into a (address screen)\n"
);
}