about summary refs log tree commit diff stats
path: root/087file.cc
Commit message (Expand)AuthorAgeFilesLines
* 3877Kartik K. Agaram2017-05-261-11/+11
* 3433Kartik K. Agaram2016-10-011-1/+1
* 3374Kartik K. Agaram2016-09-161-4/+4
* 3238Kartik K. Agaram2016-08-211-3/+14
* 3237Kartik K. Agaram2016-08-211-0/+36
* 3236Kartik K. Agaram2016-08-211-5/+0
* 3225 - testable interface for writing filesKartik K. Agaram2016-08-181-2/+2
* 3195Kartik K. Agaram2016-08-161-0/+3
* 3185Kartik K. Agaram2016-08-141-1/+1
* 3184Kartik K. Agaram2016-08-141-0/+2
* 3183 - cleanupKartik K. Agaram2016-08-131-33/+33
* 3182 - primitives for manipulating the file systemKartik K. Agaram2016-08-131-9/+160
* 2788Kartik K. Agaram2016-03-161-0/+27
1504c2b5c472266e86a38af'>4a943d4e ^
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67


                                                                             

































                                                                   
                                                                                                                        

                                                              
                                               








                                                             
                                                                                                                                                                 




       










                                                                                   
//: 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)
          raise << maybe(caller.name) << "'" << to_original_string(inst) << "': only 'main' can pass 0 into a " << to_string(expected_ing.type) << '\n' << end();
      }
    }
  }
}

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"
  );
}