about summary refs log tree commit diff stats
path: root/048_check_type_by_name.cc
diff options
context:
space:
mode:
authorKartik K. Agaram <vc@akkartik.com>2015-09-28 23:36:39 -0700
committerKartik K. Agaram <vc@akkartik.com>2015-09-28 23:36:39 -0700
commitbb58e1797e8a813891d2ba8ceabcfb518c6f8050 (patch)
tree38ac76c87f12339766189621db31ec01017d9539 /048_check_type_by_name.cc
parent86a6c9cd8952b4600f648519a64a86156d1a3e10 (diff)
downloadmu-bb58e1797e8a813891d2ba8ceabcfb518c6f8050.tar.gz
2214
Diffstat (limited to '048_check_type_by_name.cc')
-rw-r--r--048_check_type_by_name.cc91
1 files changed, 91 insertions, 0 deletions
diff --git a/048_check_type_by_name.cc b/048_check_type_by_name.cc
new file mode 100644
index 00000000..8510d02c
--- /dev/null
+++ b/048_check_type_by_name.cc
@@ -0,0 +1,91 @@
+//: Some simple sanity checks for types, and also attempts to guess them where
+//: they aren't provided.
+//:
+//: You still have to provide the full type the first time you mention a
+//: variable in a recipe. You have to explicitly name :offset and :variant
+//: every single time. You can't use the same name with multiple types in a
+//: single recipe.
+
+:(scenario transform_warns_on_reusing_name_with_different_type)
+% Hide_warnings = true;
+recipe main [
+  x:number <- copy 1
+  x:boolean <- copy 1
+]
++warn: x used with multiple types in main
+
+:(after "int main")
+  Transform.push_back(check_types_by_name);
+
+:(code)
+void check_types_by_name(const recipe_ordinal r) {
+  map<string, vector<type_ordinal> > metadata;
+  for (long long int i = 0; i < SIZE(Recipe[r].steps); ++i) {
+    instruction& inst = Recipe[r].steps.at(i);
+    for (long long int in = 0; in < SIZE(inst.ingredients); ++in) {
+      deduce_missing_type(metadata, inst.ingredients.at(in));
+      check_metadata(metadata, inst.ingredients.at(in), r);
+    }
+    for (long long int out = 0; out < SIZE(inst.products); ++out) {
+      deduce_missing_type(metadata, inst.products.at(out));
+      check_metadata(metadata, inst.products.at(out), r);
+    }
+  }
+}
+
+void check_metadata(map<string, vector<type_ordinal> >& metadata, const reagent& x, const recipe_ordinal r) {
+  if (is_literal(x)) return;
+  if (is_raw(x)) return;
+  // if you use raw locations you're probably doing something unsafe
+  if (is_integer(x.name)) return;
+  if (x.types.empty()) return;  // will throw a more precise warning elsewhere
+  if (metadata.find(x.name) == metadata.end())
+    metadata[x.name] = x.types;
+  if (metadata[x.name] != x.types)
+    raise << x.name << " used with multiple types in " << Recipe[r].name << '\n' << end();
+}
+
+:(scenario transform_fills_in_missing_types)
+recipe main [
+  x:number <- copy 1
+  y:number <- add x, 1
+]
+
+:(code)
+void deduce_missing_type(map<string, vector<type_ordinal> >& metadata, reagent& x) {
+  if (!x.types.empty()) return;
+  if (metadata.find(x.name) == metadata.end()) return;
+  copy(metadata[x.name].begin(), metadata[x.name].end(), inserter(x.types, x.types.begin()));
+  assert(x.properties.at(0).second.empty());
+  x.properties.at(0).second.resize(metadata[x.name].size());
+  x.properties.push_back(pair<string, vector<string> >("as-before", vector<string>()));
+}
+
+:(scenario transform_fills_in_missing_types_in_product)
+recipe main [
+  x:number <- copy 1
+  x <- copy 2
+]
+
+:(scenario transform_fills_in_missing_types_in_product_and_ingredient)
+recipe main [
+  x:number <- copy 1
+  x <- add x, 1
+]
++mem: storing 2 in location 1
+
+:(scenario transform_warns_on_missing_types_in_first_mention)
+% Hide_warnings = true;
+recipe main [
+  x <- copy 1
+  x:number <- copy 2
+]
++warn: missing type in 'x <- copy 1'
+
+:(scenario typo_in_address_type_warns)
+% Hide_warnings = true;
+recipe main [
+  y:address:charcter <- new character:type
+  *y <- copy 67
+]
++warn: unknown type: charcter
all' href='/akspecs/ranger/commit/Makefile?id=b0a216f59a7770f5ce35f4a56b5055c51ceec284'>b0a216f5 ^
3a1e1f28 ^
b0a216f5 ^
e7758eba ^
b0a216f5 ^

a0077554 ^













88b4d374 ^

a0077554 ^

2c5ea01d ^
582f3519 ^
b06433bc ^
a65a2695 ^

582f3519 ^
94c5d83e ^
dee6cfa6 ^
e9e4b4ff ^
b0a216f5 ^
76612b05 ^
ececd03e ^
b0a216f5 ^
e9e4b4ff ^





612b8b8b ^
e9e4b4ff ^
52403c53 ^
ad51cca4 ^
08e43b29 ^


ad51cca4 ^






b3d031a9 ^
c5f776ed ^
b3d031a9 ^

bc2c5619 ^
c5f776ed ^

b3d031a9 ^

c5f776ed ^

c216901a ^
9d82571b ^

5fca2a0b ^
9cf43c17 ^
5fca2a0b ^
c5f776ed ^

a052151f ^
c5f776ed ^

88b4d374 ^
e416bfe7 ^
88b4d374 ^
e91ae587 ^

5f29f89a ^
e91ae587 ^
f2c8a7ff ^



e416bfe7 ^
185c022e ^
5fca2a0b ^
c7720fff ^

c2d7e36e ^
a6836d5d ^
c2d7e36e ^
c7720fff ^
8d21b83c ^


e9e4b4ff ^
ececd03e ^
e9e4b4ff ^
0c2c782d ^
636d9393 ^
b0a216f5 ^
c6afc196 ^

c0d63e78 ^


ececd03e ^
f2c8a7ff ^

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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150