1 //: Introduce a new transform to perform various checks in instructions before
  2 //: we start running them. It'll be extensible, so that we can add checks for
  3 //: new recipes as we extend 'run' to support them.
  4 //:
  5 //: Doing checking in a separate part complicates things, because the values
  6 //: of variables in memory and the processor (current_recipe_name,
  7 //: current_instruction) aren't available at checking time. If I had a more
  8 //: sophisticated layer system I'd introduce the simpler version first and
  9 //: transform it in a separate layer or set of layers.
 10 
 11 :(before "End Checks")
 12 Transform.push_back(check_instruction);  // idempotent
 13 
 14 :(code)
 15 void check_instruction(const recipe_ordinal r) {
 16   trace(9991, "transform") << "--- perform checks for recipe " << get(Recipe, r).name << end();
 17   map<string, vector<type_ordinal> > metadata;
 18   for (int i = 0;  i < SIZE(get(Recipe, r).steps);  ++i) {
 19   ¦ instruction& inst = get(Recipe, r).steps.at(i);
 20   ¦ if (inst.is_label) continue;
 21   ¦ switch (inst.operation) {
 22   ¦ ¦ // Primitive Recipe Checks
 23   ¦ ¦ case COPY: {
 24   ¦ ¦ ¦ if (SIZE(inst.products) > SIZE(inst.ingredients)) {
 25   ¦ ¦ ¦ ¦ raise << maybe(get(Recipe, r).name) << "too many products in '" << to_original_string(inst) << "'\n" << end();
 26   ¦ ¦ ¦ ¦ break;
 27   ¦ ¦ ¦ }
 28   ¦ ¦ ¦ for (int i = 0;  i < SIZE(inst.products);  ++i) {
 29   ¦ ¦ ¦ ¦ if (!types_coercible(inst.products.at(i), inst.ingredients.at(i))) {
 30   ¦ ¦ ¦ ¦ ¦ raise << maybe(get(Recipe, r).name) << "can't copy '" << inst.ingredients.at(i).original_string << "' to '" << inst.products.at(i).original_string << "'; types don't match\n" << end();
 31   ¦ ¦ ¦ ¦ ¦ goto finish_checking_instruction;
 32   ¦ ¦ ¦ ¦ }
 33   ¦ ¦ ¦ }
 34   ¦ ¦ ¦ break;
 35   ¦ ¦ }
 36   ¦ ¦ // End Primitive Recipe Checks
 37   ¦ ¦ default: {
 38   ¦ ¦ ¦ // Defined Recipe Checks
 39   ¦ ¦ ¦ // End Defined Recipe Checks
 40   ¦ ¦ }
 41   ¦ }
 42   ¦ finish_checking_instruction:;
 43   }
 44 }
 45 
 46 :(scenario copy_checks_reagent_count)
 47 % Hide_errors = true;
 48 def main [
 49   1:num, 2:num <- copy 34
 50 ]
 51 +error: main: too many products in '1:num, 2:num <- copy 34'
 52 
 53 :(scenario write_scalar_to_array_disallowed)
 54 % Hide_errors = true;
 55 def main [
 56   1:array:num <- copy 34
 57 ]
 58 +error: main: can't copy '34' to '1:array:num'; types don't match
 59 
 60 :(scenario write_scalar_to_array_disallowed_2)
 61 % Hide_errors = true;
 62 def main [
 63   1:num, 2:array:num <- copy 34, 35
 64 ]
 65 +error: main: can't copy '35' to '2:array:num'; types don't match
 66 
 67 :(scenario write_scalar_to_address_disallowed)
 68 % Hide_errors = true;
 69 def main [
 70   1:address:num <- copy 34
 71 ]
 72 +error: main: can't copy '34' to '1:address:num'; types don't match
 73 
 74 :(scenario write_address_to_number_allowed)
 75 def main [
 76   1:address:num <- copy 12/unsafe
 77   2:num <- copy 1:address:num
 78 ]
 79 +mem: storing 12 in location 2
 80 $error: 0
 81 
 82 :(scenario write_address_to_character_disallowed)
 83 % Hide_errors = true;
 84 def main [
 85   1:address:num <- copy 12/unsafe
 86   2:char <- copy 1:address:num
 87 ]
 88 +error: main: can't copy '1:address:num' to '2:char'; types don't match
 89 
 90 :(scenario write_boolean_to_number_allowed)
 91 def main [
 92   1:bool <- copy 1/true
 93   2:num <- copy 1:bool
 94 ]
 95 +mem: storing 1 in location 2
 96 $error: 0
 97 
 98 :(scenario write_number_to_boolean_disallowed)
 99 % Hide_errors = true;
100 def main [
101   1:num <- copy 34
102   2:bool <- copy 1:num
103 ]
104 +error: main: can't copy '1:num' to '2:bool'; types don't match
105 
106 :(code)
107 // types_match with some leniency
108 bool types_coercible(const reagent& to, const reagent& from) {
109   if (types_match(to, from)) return true;
110   if (is_mu_address(from) && is_real_mu_number(to)) return true;
111   if (is_mu_boolean(from) && is_real_mu_number(to)) return true;
112   // End types_coercible Special-cases
113   return false;
114 }
115 
116 bool types_match(const reagent& to, const reagent& from) {
117   // to sidestep type-checking, use /unsafe in the source.
118   // this will be highlighted in red inside vim. just for setting up some tests.
119   if (is_unsafe(from)) return true;
120   if (is_literal(from)) {
121   ¦ if (is_mu_array(to)) return false;
122   ¦ // End Matching Types For Literal(to)
123   ¦ // allow writing 0 to any address
124   ¦ if (is_mu_address(to)) return from.name == "0";
125   ¦ if (!to.type) return false;
126   ¦ if (to.type->atom && to.type->value == get(Type_ordinal, "boolean"))
127   ¦ ¦ return from.name == "0" || from.name == "1";
128   ¦ return size_of(to) == 1;  // literals are always scalars
129   }
130   return types_strictly_match(to, from);
131 }
132 
133 //: copy arguments for later layers
134 bool types_strictly_match(reagent/*copy*/ to, reagent/*copy*/ from) {
135   // End Preprocess types_strictly_match(reagent to, reagent from)
136   if (to.type == NULL) return false;  // error
137   if (is_literal(from) && to.type->value == get(Type_ordinal, "number")) return true;
138   // to sidestep type-checking, use /unsafe in the source.
139   // this will be highlighted in red inside vim. just for setting up some tests.
140   if (is_unsafe(from)) return true;
141   // '_' never raises type error
142   if (is_dummy(to)) return true;
143   if (!to.type) return !from.type;
144   return types_strictly_match(to.type, from.type);
145 }
146 
147 bool types_strictly_match(const type_tree* to, const type_tree* from) {
148   if (from == to) return true;
149   if (!to) return false;
150   if (!from) return to->atom && to->value == 0;
151   if (from->atom != to->atom) return false;
152   if (from->atom) {
153   ¦ if (from->value == -1) return from->name == to->name;
154   ¦ return from->value == to->value;
155   }
156   if (types_strictly_match(to->left, from->left) && types_strictly_match(to->right, from->right))
157   ¦ return true;
158   // fallback: (x) == x
159   if (to->right == NULL && types_strictly_match(to->left, from)) return true;
160   if (from->right == NULL && types_strictly_match(to, from->left)) return true;
161   return false;
162 }
163 
164 void test_unknown_type_does_not_match_unknown_type() {
165   reagent a("a:foo");
166   reagent b("b:bar");
167   CHECK(!types_strictly_match(a, b));
168 }
169 
170 void test_unknown_type_matches_itself() {
171   reagent a("a:foo");
172   reagent b("b:foo");
173   CHECK(types_strictly_match(a, b));
174 }
175 
176 void test_type_abbreviations_match_raw_types() {
177   put(Type_abbreviations, "text", new_type_tree("address:array:character"));
178   // a has type (address buffer (address array character))
179   reagent a("a:address:buffer:text");
180   expand_type_abbreviations(a.type);
181   // b has type (address buffer address array character)
182   reagent b("b:address:buffer:address:array:character");
183   CHECK(types_strictly_match(a, b));
184   delete Type_abbreviations["text"];
185   put(Type_abbreviations, "text", NULL);
186 }
187 
188 //: helpers
189 
190 bool is_unsafe(const reagent& r) {
191   return has_property(r, "unsafe");
192 }
193 
194 bool is_mu_array(reagent/*copy*/ r) {
195   // End Preprocess is_mu_array(reagent r)
196   return is_mu_array(r.type);
197 }
198 bool is_mu_array(const type_tree* type) {
199   if (!type) return false;
200   if (is_literal(type)) return false;
201   if (type->atom) return false;
202   if (!type->left->atom) {
203   ¦ raise << "invalid type " << to_string(type) << '\n' << end();
204   ¦ return false;
205   }
206   return type->left->value == get(Type_ordinal, "array");
207 }
208 
209 bool is_mu_address(reagent/*copy*/ r) {
210   // End Preprocess is_mu_address(reagent r)
211   return is_mu_address(r.type);
212 }
213 bool is_mu_address(const type_tree* type) {
214   if (!type) return false;
215   if (is_literal(type)) return false;
216   if (type->atom) return false;
217   if (!type->left->atom) {
218   ¦ raise << "invalid type " << to_string(type) << '\n' << end();
219   ¦ return false;
220   }
221   return type->left->value == get(Type_ordinal, "address");
222 }
223 
224 bool is_mu_boolean(reagent/*copy*/ r) {
225   // End Preprocess is_mu_boolean(reagent r)
226   if (!r.type) return false;
227   if (is_literal(r)) return false;
228   if (!r.type->atom) return false;
229   return r.type->value == get(Type_ordinal, "boolean");
230 }
231 
232 bool is_mu_number(reagent/*copy*/ r) {
233   if (is_mu_character(r.type)) return true;  // permit arithmetic on unicode code points
234   return is_real_mu_number(r);
235 }
236 
237 bool is_real_mu_number(reagent/*copy*/ r) {
238   // End Preprocess is_mu_number(reagent r)
239   if (!r.type) return false;
240   if (!r.type->atom) return false;
241   if (is_literal(r)) {
242   ¦ return r.type->name == "literal-fractional-number"
243   ¦ ¦ ¦ || r.type->name == "literal";
244   }
245   return r.type->value == get(Type_ordinal, "number");
246 }
247 
248 bool is_mu_character(reagent/*copy*/ r) {
249   // End Preprocess is_mu_character(reagent r)
250   return is_mu_character(r.type);
251 }
252 bool is_mu_character(const type_tree* type) {
253   if (!type) return false;
254   if (!type->atom) return false;
255   if (is_literal(type)) return false;
256   return type->value == get(Type_ordinal, "character");
257 }
258 
259 bool is_mu_scalar(reagent/*copy*/ r) {
260   return is_mu_scalar(r.type);
261 }
262 bool is_mu_scalar(const type_tree* type) {
263   if (!type) return false;
264   if (is_mu_address(type)) return true;
265   if (!type->atom) return false;
266   if (is_literal(type))
267   ¦ return type->name != "literal-string";
268   return size_of(type) == 1;
269 }