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_boolean_to_number_allowed)
 83 def main [
 84   1:bool <- copy 1/true
 85   2:num <- copy 1:bool
 86 ]
 87 +mem: storing 1 in location 2
 88 $error: 0
 89 
 90 :(scenario write_number_to_boolean_allowed)
 91 def main [
 92   1:num <- copy 34
 93   2:bool <- copy 1:num
 94 ]
 95 +mem: storing 34 in location 2
 96 $error: 0
 97 
 98 :(code)
 99 // types_match with some leniency
100 bool types_coercible(const reagent& to, const reagent& from) {
101   if (types_match(to, from)) return true;
102   if (is_mu_address(from) && is_mu_number(to)) return true;
103   if (is_mu_boolean(from) && is_mu_number(to)) return true;
104   if (is_mu_number(from) && is_mu_boolean(to)) return true;
105   // End types_coercible Special-cases
106   return false;
107 }
108 
109 bool types_match(const reagent& to, const reagent& from) {
110   // to sidestep type-checking, use /unsafe in the source.
111   // this will be highlighted in red inside vim. just for setting up some tests.
112   if (is_unsafe(from)) return true;
113   if (is_literal(from)) {
114   ¦ if (is_mu_array(to)) return false;
115   ¦ // End Matching Types For Literal(to)
116   ¦ // allow writing 0 to any address
117   ¦ if (is_mu_address(to)) return from.name == "0";
118   ¦ if (!to.type) return false;
119   ¦ if (to.type->atom && to.type->value == get(Type_ordinal, "boolean"))
120   ¦ ¦ return from.name == "0" || from.name == "1";
121   ¦ return size_of(to) == 1;  // literals are always scalars
122   }
123   return types_strictly_match(to, from);
124 }
125 
126 //: copy arguments for later layers
127 bool types_strictly_match(reagent/*copy*/ to, reagent/*copy*/ from) {
128   // End Preprocess types_strictly_match(reagent to, reagent from)
129   if (to.type == NULL) return false;  // error
130   if (is_literal(from) && to.type->value == get(Type_ordinal, "number")) return true;
131   // to sidestep type-checking, use /unsafe in the source.
132   // this will be highlighted in red inside vim. just for setting up some tests.
133   if (is_unsafe(from)) return true;
134   // '_' never raises type error
135   if (is_dummy(to)) return true;
136   if (!to.type) return !from.type;
137   return types_strictly_match(to.type, from.type);
138 }
139 
140 bool types_strictly_match(const type_tree* to, const type_tree* from) {
141   if (from == to) return true;
142   if (!to) return false;
143   if (!from) return to->atom && to->value == 0;
144   if (from->atom != to->atom) return false;
145   if (from->atom) {
146   ¦ if (from->value == -1) return from->name == to->name;
147   ¦ return from->value == to->value;
148   }
149   if (types_strictly_match(to->left, from->left) && types_strictly_match(to->right, from->right))
150   ¦ return true;
151   // fallback: (x) == x
152   if (to->right == NULL && types_strictly_match(to->left, from)) return true;
153   if (from->right == NULL && types_strictly_match(to, from->left)) return true;
154   return false;
155 }
156 
157 void test_unknown_type_does_not_match_unknown_type() {
158   reagent a("a:foo");
159   reagent b("b:bar");
160   CHECK(!types_strictly_match(a, b));
161 }
162 
163 void test_unknown_type_matches_itself() {
164   reagent a("a:foo");
165   reagent b("b:foo");
166   CHECK(types_strictly_match(a, b));
167 }
168 
169 void test_type_abbreviations_match_raw_types() {
170   put(Type_abbreviations, "text", new_type_tree("address:array:character"));
171   // a has type (address buffer (address array character))
172   reagent a("a:address:buffer:text");
173   expand_type_abbreviations(a.type);
174   // b has type (address buffer address array character)
175   reagent b("b:address:buffer:address:array:character");
176   CHECK(types_strictly_match(a, b));
177   delete Type_abbreviations["text"];
178   put(Type_abbreviations, "text", NULL);
179 }
180 
181 //: helpers
182 
183 bool is_unsafe(const reagent& r) {
184   return has_property(r, "unsafe");
185 }
186 
187 bool is_mu_array(reagent/*copy*/ r) {
188   // End Preprocess is_mu_array(reagent r)
189   return is_mu_array(r.type);
190 }
191 bool is_mu_array(const type_tree* type) {
192   if (!type) return false;
193   if (is_literal(type)) return false;
194   if (type->atom) return false;
195   if (!type->left->atom) {
196   ¦ raise << "invalid type " << to_string(type) << '\n' << end();
197   ¦ return false;
198   }
199   return type->left->value == get(Type_ordinal, "array");
200 }
201 
202 bool is_mu_address(reagent/*copy*/ r) {
203   // End Preprocess is_mu_address(reagent r)
204   return is_mu_address(r.type);
205 }
206 bool is_mu_address(const type_tree* type) {
207   if (!type) return false;
208   if (is_literal(type)) return false;
209   if (type->atom) return false;
210   if (!type->left->atom) {
211   ¦ raise << "invalid type " << to_string(type) << '\n' << end();
212   ¦ return false;
213   }
214   return type->left->value == get(Type_ordinal, "address");
215 }
216 
217 bool is_mu_boolean(reagent/*copy*/ r) {
218   // End Preprocess is_mu_boolean(reagent r)
219   if (!r.type) return false;
220   if (is_literal(r)) return false;
221   if (!r.type->atom) return false;
222   return r.type->value == get(Type_ordinal, "boolean");
223 }
224 
225 bool is_mu_number(reagent/*copy*/ r) {
226   // End Preprocess is_mu_number(reagent r)
227   if (!r.type) return false;
228   if (!r.type->atom) return false;
229   if (is_literal(r)) {
230   ¦ return r.type->name == "literal-fractional-number"
231   ¦ ¦ ¦ || r.type->name == "literal";
232   }
233   if (r.type->value == get(Type_ordinal, "character")) return true;  // permit arithmetic on unicode code points
234   return r.type->value == get(Type_ordinal, "number");
235 }
236 
237 bool is_mu_character(reagent/*copy*/ r) {
238   // End Preprocess is_mu_character(reagent r)
239   return is_mu_character(r.type);
240 }
241 bool is_mu_character(const type_tree* type) {
242   if (!type) return false;
243   if (!type->atom) return false;
244   if (is_literal(type)) return false;
245   return type->value == get(Type_ordinal, "character");
246 }
247 
248 bool is_mu_scalar(reagent/*copy*/ r) {
249   return is_mu_scalar(r.type);
250 }
251 bool is_mu_scalar(const type_tree* type) {
252   if (!type) return false;
253   if (is_mu_address(type)) return true;
254   if (!type->atom) return false;
255   if (is_literal(type))
256   ¦ return type->name != "literal-string";
257   return size_of(type) == 1;
258 }