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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
|
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
<meta http-equiv="content-type" content="text/html; charset=UTF-8">
<title>Mu - 045closure_name.cc</title>
<meta name="Generator" content="Vim/7.4">
<meta name="plugin-version" content="vim7.4_v1">
<meta name="syntax" content="cpp">
<meta name="settings" content="use_css,pre_wrap,no_foldcolumn,expand_tabs,prevent_copy=">
<meta name="colorscheme" content="minimal">
<style type="text/css">
<!--
pre { white-space: pre-wrap; font-family: monospace; color: #d0d0d0; background-color: #000000; }
body { font-family: monospace; color: #d0d0d0; background-color: #000000; }
* { font-size: 1em; }
.cSpecial { color: #008000; }
.Identifier { color: #008080; }
.Constant { color: #008080; }
.Comment { color: #8080ff; }
.Delimiter { color: #c000c0; }
.Special { color: #ff6060; }
.CommentedCode { color: #6c6c6c; }
.traceContains { color: #008000; }
-->
</style>
<script type='text/javascript'>
<!--
-->
</script>
</head>
<body>
<pre id='vimCodeElement'>
<span class="Comment">//: Writing to a literal (not computed) address of 0 in a recipe chains two</span>
<span class="Comment">//: spaces together. When a variable has a property of /space:1, it looks up</span>
<span class="Comment">//: the variable in the chained/surrounding space. /space:2 looks up the</span>
<span class="Comment">//: surrounding space of the surrounding space, etc.</span>
<span class="Delimiter">:(scenario closure)</span>
recipe main [
default-space:address:array:location<span class="Special"> <- </span>new location:type<span class="Delimiter">,</span> <span class="Constant">30</span>:literal
<span class="Constant">1</span>:address:array:location/names:init-counter<span class="Special"> <- </span>init-counter
<span class="CommentedCode">#? $print [AAAAAAAAAAAAAAAA]</span>
<span class="CommentedCode">#? $print 1:address:array:location</span>
<span class="Constant">2</span>:number/<span class="Special">raw <- </span>increment-counter <span class="Constant">1</span>:address:array:location/names:init-counter
<span class="Constant">3</span>:number/<span class="Special">raw <- </span>increment-counter <span class="Constant">1</span>:address:array:location/names:init-counter
]
recipe init-counter [
default-space:address:array:location<span class="Special"> <- </span>new location:type<span class="Delimiter">,</span> <span class="Constant">30</span>:literal
x:number<span class="Special"> <- </span>copy <span class="Constant">23</span>:literal
y:number<span class="Special"> <- </span>copy <span class="Constant">3</span>:literal <span class="Comment"># variable that will be incremented</span>
reply default-space:address:array:location
]
recipe increment-counter [
default-space:address:array:location<span class="Special"> <- </span>new location:type<span class="Delimiter">,</span> <span class="Constant">30</span>:literal
<span class="Constant">0</span>:address:array:location/names:init-counter<span class="Special"> <- </span>next-ingredient <span class="Comment"># outer space must be created by 'init-counter' above</span>
y:number/space:<span class="Constant">1</span><span class="Special"> <- </span>add y:number/space:<span class="Constant">1</span><span class="Delimiter">,</span> <span class="Constant">1</span>:literal <span class="Comment"># increment</span>
y:number<span class="Special"> <- </span>copy <span class="Constant">234</span>:literal <span class="Comment"># dummy</span>
reply y:number/space:<span class="Constant">1</span>
]
<span class="traceContains">+name: recipe increment-counter is surrounded by init-counter</span>
<span class="traceContains">+mem: storing 5 in location 3</span>
<span class="Comment">//: To make this work, compute the recipe that provides names for the</span>
<span class="Comment">//: surrounding space of each recipe. This must happen before transform_names.</span>
<span class="Delimiter">:(before "End Globals")</span>
map<recipe_number<span class="Delimiter">,</span> recipe_number> Surrounding_space<span class="Delimiter">;</span>
<span class="Delimiter">:(after "int main")</span>
Transform<span class="Delimiter">.</span>push_back<span class="Delimiter">(</span>collect_surrounding_spaces<span class="Delimiter">);</span>
<span class="Delimiter">:(code)</span>
void collect_surrounding_spaces<span class="Delimiter">(</span>const recipe_number r<span class="Delimiter">)</span> <span class="Delimiter">{</span>
for <span class="Delimiter">(</span>long long int i = <span class="Constant">0</span><span class="Delimiter">;</span> i < SIZE<span class="Delimiter">(</span>Recipe[r]<span class="Delimiter">.</span>steps<span class="Delimiter">);</span> ++i<span class="Delimiter">)</span> <span class="Delimiter">{</span>
const instruction& inst = Recipe[r]<span class="Delimiter">.</span>steps<span class="Delimiter">.</span>at<span class="Delimiter">(</span>i<span class="Delimiter">);</span>
if <span class="Delimiter">(</span>inst<span class="Delimiter">.</span>is_label<span class="Delimiter">)</span> <span class="Identifier">continue</span><span class="Delimiter">;</span>
for <span class="Delimiter">(</span>long long int j = <span class="Constant">0</span><span class="Delimiter">;</span> j < SIZE<span class="Delimiter">(</span>inst<span class="Delimiter">.</span>products<span class="Delimiter">);</span> ++j<span class="Delimiter">)</span> <span class="Delimiter">{</span>
if <span class="Delimiter">(</span>isa_literal<span class="Delimiter">(</span>inst<span class="Delimiter">.</span>products<span class="Delimiter">.</span>at<span class="Delimiter">(</span>j<span class="Delimiter">)))</span> <span class="Identifier">continue</span><span class="Delimiter">;</span>
if <span class="Delimiter">(</span>inst<span class="Delimiter">.</span>products<span class="Delimiter">.</span>at<span class="Delimiter">(</span>j<span class="Delimiter">).</span>name != <span class="Constant">"0"</span><span class="Delimiter">)</span> <span class="Identifier">continue</span><span class="Delimiter">;</span>
if <span class="Delimiter">(</span>SIZE<span class="Delimiter">(</span>inst<span class="Delimiter">.</span>products<span class="Delimiter">.</span>at<span class="Delimiter">(</span>j<span class="Delimiter">).</span>types<span class="Delimiter">)</span> != <span class="Constant">3</span>
|| inst<span class="Delimiter">.</span>products<span class="Delimiter">.</span>at<span class="Delimiter">(</span>j<span class="Delimiter">).</span>types<span class="Delimiter">.</span>at<span class="Delimiter">(</span><span class="Constant">0</span><span class="Delimiter">)</span> != Type_number[<span class="Constant">"address"</span>]
|| inst<span class="Delimiter">.</span>products<span class="Delimiter">.</span>at<span class="Delimiter">(</span>j<span class="Delimiter">).</span>types<span class="Delimiter">.</span>at<span class="Delimiter">(</span><span class="Constant">1</span><span class="Delimiter">)</span> != Type_number[<span class="Constant">"array"</span>]
|| inst<span class="Delimiter">.</span>products<span class="Delimiter">.</span>at<span class="Delimiter">(</span>j<span class="Delimiter">).</span>types<span class="Delimiter">.</span>at<span class="Delimiter">(</span><span class="Constant">2</span><span class="Delimiter">)</span> != Type_number[<span class="Constant">"location"</span>]<span class="Delimiter">)</span> <span class="Delimiter">{</span>
raise << <span class="Constant">"slot 0 should always have type address:array:location, but is "</span> << inst<span class="Delimiter">.</span>products<span class="Delimiter">.</span>at<span class="Delimiter">(</span>j<span class="Delimiter">).</span>to_string<span class="Delimiter">()</span> << <span class="cSpecial">'\n'</span><span class="Delimiter">;</span>
<span class="Identifier">continue</span><span class="Delimiter">;</span>
<span class="Delimiter">}</span>
vector<string> s = property<span class="Delimiter">(</span>inst<span class="Delimiter">.</span>products<span class="Delimiter">.</span>at<span class="Delimiter">(</span>j<span class="Delimiter">),</span> <span class="Constant">"names"</span><span class="Delimiter">);</span>
if <span class="Delimiter">(</span>s<span class="Delimiter">.</span>empty<span class="Delimiter">())</span>
raise << <span class="Constant">"slot 0 requires a /names property in recipe "</span> << Recipe[r]<span class="Delimiter">.</span>name << die<span class="Delimiter">();</span>
if <span class="Delimiter">(</span>SIZE<span class="Delimiter">(</span>s<span class="Delimiter">)</span> > <span class="Constant">1</span><span class="Delimiter">)</span> raise << <span class="Constant">"slot 0 should have a single value in /names, got "</span> << inst<span class="Delimiter">.</span>products<span class="Delimiter">.</span>at<span class="Delimiter">(</span>j<span class="Delimiter">).</span>to_string<span class="Delimiter">()</span> << <span class="cSpecial">'\n'</span><span class="Delimiter">;</span>
string surrounding_recipe_name = s<span class="Delimiter">.</span>at<span class="Delimiter">(</span><span class="Constant">0</span><span class="Delimiter">);</span>
if <span class="Delimiter">(</span>Surrounding_space<span class="Delimiter">.</span>find<span class="Delimiter">(</span>r<span class="Delimiter">)</span> != Surrounding_space<span class="Delimiter">.</span>end<span class="Delimiter">()</span>
&& Surrounding_space[r] != Recipe_number[surrounding_recipe_name]<span class="Delimiter">)</span> <span class="Delimiter">{</span>
raise << <span class="Constant">"recipe "</span> << Recipe[r]<span class="Delimiter">.</span>name << <span class="Constant">" can have only one 'surrounding' recipe but has "</span> << Recipe[Surrounding_space[r]]<span class="Delimiter">.</span>name << <span class="Constant">" and "</span> << surrounding_recipe_name << <span class="cSpecial">'\n'</span><span class="Delimiter">;</span>
<span class="Identifier">continue</span><span class="Delimiter">;</span>
<span class="Delimiter">}</span>
trace<span class="Delimiter">(</span><span class="Constant">"name"</span><span class="Delimiter">)</span> << <span class="Constant">"recipe "</span> << Recipe[r]<span class="Delimiter">.</span>name << <span class="Constant">" is surrounded by "</span> << surrounding_recipe_name<span class="Delimiter">;</span>
Surrounding_space[r] = Recipe_number[surrounding_recipe_name]<span class="Delimiter">;</span>
<span class="Delimiter">}</span>
<span class="Delimiter">}</span>
<span class="Delimiter">}</span>
<span class="Comment">//: Once surrounding spaces are available, transform_names uses them to handle</span>
<span class="Comment">//: /space properties.</span>
<span class="Delimiter">:(replace{} "long long int lookup_name(const reagent& r, const recipe_number default_recipe)")</span>
long long int lookup_name<span class="Delimiter">(</span>const reagent& x<span class="Delimiter">,</span> const recipe_number default_recipe<span class="Delimiter">)</span> <span class="Delimiter">{</span>
<span class="CommentedCode">//? cout << "AAA " << default_recipe << " " << Recipe[default_recipe].name << '\n'; //? 2</span>
<span class="CommentedCode">//? cout << "AAA " << x.to_string() << '\n'; //? 1</span>
if <span class="Delimiter">(</span>!has_property<span class="Delimiter">(</span>x<span class="Delimiter">,</span> <span class="Constant">"space"</span><span class="Delimiter">))</span> <span class="Delimiter">{</span>
if <span class="Delimiter">(</span>Name[default_recipe]<span class="Delimiter">.</span>empty<span class="Delimiter">())</span> raise << <span class="Constant">"name not found: "</span> << x<span class="Delimiter">.</span>name << <span class="cSpecial">'\n'</span> << die<span class="Delimiter">();</span>
<span class="Identifier">return</span> Name[default_recipe][x<span class="Delimiter">.</span>name]<span class="Delimiter">;</span>
<span class="Delimiter">}</span>
vector<string> p = property<span class="Delimiter">(</span>x<span class="Delimiter">,</span> <span class="Constant">"space"</span><span class="Delimiter">);</span>
if <span class="Delimiter">(</span>SIZE<span class="Delimiter">(</span>p<span class="Delimiter">)</span> != <span class="Constant">1</span><span class="Delimiter">)</span> raise << <span class="Constant">"/space property should have exactly one (non-negative integer) value</span><span class="cSpecial">\n</span><span class="Constant">"</span><span class="Delimiter">;</span>
long long int n = to_integer<span class="Delimiter">(</span>p<span class="Delimiter">.</span>at<span class="Delimiter">(</span><span class="Constant">0</span><span class="Delimiter">));</span>
assert<span class="Delimiter">(</span>n >= <span class="Constant">0</span><span class="Delimiter">);</span>
recipe_number surrounding_recipe = lookup_surrounding_recipe<span class="Delimiter">(</span>default_recipe<span class="Delimiter">,</span> n<span class="Delimiter">);</span>
set<recipe_number> done<span class="Delimiter">;</span>
vector<recipe_number> path<span class="Delimiter">;</span>
<span class="Identifier">return</span> lookup_name<span class="Delimiter">(</span>x<span class="Delimiter">,</span> surrounding_recipe<span class="Delimiter">,</span> done<span class="Delimiter">,</span> path<span class="Delimiter">);</span>
<span class="Delimiter">}</span>
<span class="Comment">// If the recipe we need to lookup this name in doesn't have names done yet,</span>
<span class="Comment">// recursively call transform_names on it.</span>
long long int lookup_name<span class="Delimiter">(</span>const reagent& x<span class="Delimiter">,</span> const recipe_number r<span class="Delimiter">,</span> set<recipe_number>& done<span class="Delimiter">,</span> vector<recipe_number>& path<span class="Delimiter">)</span> <span class="Delimiter">{</span>
if <span class="Delimiter">(</span>!Name[r]<span class="Delimiter">.</span>empty<span class="Delimiter">())</span> <span class="Identifier">return</span> Name[r][x<span class="Delimiter">.</span>name]<span class="Delimiter">;</span>
if <span class="Delimiter">(</span>done<span class="Delimiter">.</span>find<span class="Delimiter">(</span>r<span class="Delimiter">)</span> != done<span class="Delimiter">.</span>end<span class="Delimiter">())</span> <span class="Delimiter">{</span>
raise << <span class="Constant">"can't compute address of "</span> << x<span class="Delimiter">.</span>to_string<span class="Delimiter">()</span> << <span class="Constant">" because "</span><span class="Delimiter">;</span>
for <span class="Delimiter">(</span>long long int i = <span class="Constant">1</span><span class="Delimiter">;</span> i < SIZE<span class="Delimiter">(</span>path<span class="Delimiter">);</span> ++i<span class="Delimiter">)</span> <span class="Delimiter">{</span>
raise << path<span class="Delimiter">.</span>at<span class="Delimiter">(</span>i-<span class="Constant">1</span><span class="Delimiter">)</span> << <span class="Constant">" requires computing names of "</span> << path<span class="Delimiter">.</span>at<span class="Delimiter">(</span>i<span class="Delimiter">)</span> << <span class="cSpecial">'\n'</span><span class="Delimiter">;</span>
<span class="Delimiter">}</span>
raise << path<span class="Delimiter">.</span>at<span class="Delimiter">(</span>SIZE<span class="Delimiter">(</span>path<span class="Delimiter">)</span>-<span class="Constant">1</span><span class="Delimiter">)</span> << <span class="Constant">" requires computing names of "</span> << r << <span class="Constant">"..ad infinitum</span><span class="cSpecial">\n</span><span class="Constant">"</span> << die<span class="Delimiter">();</span>
<span class="Identifier">return</span> <span class="Constant">0</span><span class="Delimiter">;</span>
<span class="Delimiter">}</span>
done<span class="Delimiter">.</span>insert<span class="Delimiter">(</span>r<span class="Delimiter">);</span>
path<span class="Delimiter">.</span>push_back<span class="Delimiter">(</span>r<span class="Delimiter">);</span>
transform_names<span class="Delimiter">(</span>r<span class="Delimiter">);</span> <span class="Comment">// Not passing 'done' through. Might this somehow cause an infinite loop?</span>
assert<span class="Delimiter">(</span>!Name[r]<span class="Delimiter">.</span>empty<span class="Delimiter">());</span>
<span class="Identifier">return</span> Name[r][x<span class="Delimiter">.</span>name]<span class="Delimiter">;</span>
<span class="Delimiter">}</span>
recipe_number lookup_surrounding_recipe<span class="Delimiter">(</span>const recipe_number r<span class="Delimiter">,</span> long long int n<span class="Delimiter">)</span> <span class="Delimiter">{</span>
if <span class="Delimiter">(</span>n == <span class="Constant">0</span><span class="Delimiter">)</span> <span class="Identifier">return</span> r<span class="Delimiter">;</span>
if <span class="Delimiter">(</span>Surrounding_space<span class="Delimiter">.</span>find<span class="Delimiter">(</span>r<span class="Delimiter">)</span> == Surrounding_space<span class="Delimiter">.</span>end<span class="Delimiter">())</span> <span class="Delimiter">{</span>
raise << <span class="Constant">"don't know surrounding recipe of "</span> << Recipe[r]<span class="Delimiter">.</span>name << <span class="cSpecial">'\n'</span><span class="Delimiter">;</span>
<span class="Identifier">return</span> <span class="Constant">0</span><span class="Delimiter">;</span>
<span class="Delimiter">}</span>
assert<span class="Delimiter">(</span>Surrounding_space[r]<span class="Delimiter">);</span>
<span class="Identifier">return</span> lookup_surrounding_recipe<span class="Delimiter">(</span>Surrounding_space[r]<span class="Delimiter">,</span> n-<span class="Constant">1</span><span class="Delimiter">);</span>
<span class="Delimiter">}</span>
<span class="Comment">//: weaken use-before-set warnings just a tad</span>
<span class="Delimiter">:(replace{} "bool already_transformed(const reagent& r, const map<string, long long int>& names)")</span>
bool already_transformed<span class="Delimiter">(</span>const reagent& r<span class="Delimiter">,</span> const map<string<span class="Delimiter">,</span> long long int>& names<span class="Delimiter">)</span> <span class="Delimiter">{</span>
if <span class="Delimiter">(</span>has_property<span class="Delimiter">(</span>r<span class="Delimiter">,</span> <span class="Constant">"space"</span><span class="Delimiter">))</span> <span class="Delimiter">{</span>
vector<string> p = property<span class="Delimiter">(</span>r<span class="Delimiter">,</span> <span class="Constant">"space"</span><span class="Delimiter">);</span>
assert<span class="Delimiter">(</span>SIZE<span class="Delimiter">(</span>p<span class="Delimiter">)</span> == <span class="Constant">1</span><span class="Delimiter">);</span>
if <span class="Delimiter">(</span>p<span class="Delimiter">.</span>at<span class="Delimiter">(</span><span class="Constant">0</span><span class="Delimiter">)</span> != <span class="Constant">"0"</span><span class="Delimiter">)</span> <span class="Identifier">return</span> <span class="Constant">true</span><span class="Delimiter">;</span>
<span class="Delimiter">}</span>
<span class="Identifier">return</span> names<span class="Delimiter">.</span>find<span class="Delimiter">(</span>r<span class="Delimiter">.</span>name<span class="Delimiter">)</span> != names<span class="Delimiter">.</span>end<span class="Delimiter">();</span>
<span class="Delimiter">}</span>
</pre>
</body>
</html>
<!-- vim: set foldmethod=manual : -->
|