about summary refs log tree commit diff stats
path: root/cpp/017and-record
diff options
context:
space:
mode:
authorKartik K. Agaram <vc@akkartik.com>2015-02-20 20:25:45 -0800
committerKartik K. Agaram <vc@akkartik.com>2015-02-20 20:25:45 -0800
commita4559f7287599a981a3b38a82b741f57d50e85c4 (patch)
tree30e03f26c4559eadb8a5a715ec76b8f60c27dd4b /cpp/017and-record
parenteaa75c87c5b9ffb34ebd8c2ea0f1682dfcad336a (diff)
downloadmu-a4559f7287599a981a3b38a82b741f57d50e85c4.tar.gz
801
Diffstat (limited to 'cpp/017and-record')
-rw-r--r--cpp/017and-record39
1 files changed, 39 insertions, 0 deletions
diff --git a/cpp/017and-record b/cpp/017and-record
index 94febae0..f38b031f 100644
--- a/cpp/017and-record
+++ b/cpp/017and-record
@@ -74,3 +74,42 @@ recipe main [
 +mem: location 13 is 0
 +run: product 0 is 0
 +mem: storing in location 15
+
+:(before "End Globals")
+// To write to fields of records, you need their address.
+const int GET_ADDRESS = 19;
+:(before "End Primitive Recipe Numbers")
+Recipe_number["get-address"] = GET_ADDRESS;
+Next_recipe_number++;
+:(before "End Primitive Recipe Implementations")
+case GET_ADDRESS: {
+  trace("run") << "ingredient 0 is " << instructions[pc].ingredients[0].name;
+  int base_address = to_int(instructions[pc].ingredients[0].name);
+  trace("run") << "base address " << base_address;
+  int base_type = instructions[pc].ingredients[0].types[0];
+  trace("run") << "base type is " << base_type;
+  trace("run") << "ingredient 1 is " << instructions[pc].ingredients[1].name;
+  assert(instructions[pc].ingredients[1].types.size() == 1);
+  assert(instructions[pc].ingredients[1].types[0] == 0);  // must be literal
+  size_t offset = to_int(instructions[pc].ingredients[1].name);
+  int src = base_address+offset;
+  trace("run") << "address to copy is " << src;
+  vector<int> result;
+  result.push_back(src);
+  trace("run") << "product 0 is " << result[0];
+  write_memory(instructions[pc].products[0], result);
+  break;
+}
+
+:(scenario "get_address")
+recipe main [
+  12:integer <- copy 34:literal
+  13:boolean <- copy 0:literal
+  15:integer <- get-address 12:integer-boolean, 1:offset
+]
++run: instruction 2
++run: ingredient 0 is 12
++run: base address 12
++run: ingredient 1 is 1
++run: address to copy is 13
++mem: storing in location 15
e/cpp/013arithmetic?h=hlt&id=8d7839b99723553c08b33c30980a001e7a3557b6'>^
8d7839b9 ^




6f5d7864 ^
8d7839b9 ^










6f5d7864 ^
8d7839b9 ^






42b31beb ^

8d7839b9 ^
42b31beb ^
f7051fad ^
8d7839b9 ^

42b31beb ^
c7c822b2 ^

42b31beb ^
c7c822b2 ^

42b31beb ^

e2d57bfe ^
42b31beb ^
c7c822b2 ^
42b31beb ^

8d7839b9 ^




6f5d7864 ^
8d7839b9 ^










6f5d7864 ^
8d7839b9 ^






42b31beb ^

8d7839b9 ^
42b31beb ^
f7051fad ^
8d7839b9 ^

42b31beb ^
c7c822b2 ^

42b31beb ^
c7c822b2 ^

42b31beb ^


e2d57bfe ^
42b31beb ^
c7c822b2 ^
42b31beb ^

8d7839b9 ^




6f5d7864 ^
8d7839b9 ^










6f5d7864 ^
8d7839b9 ^






42b31beb ^

8d7839b9 ^
42b31beb ^
f7051fad ^
8d7839b9 ^

42b31beb ^
c7c822b2 ^

42b31beb ^
c7c822b2 ^

42b31beb ^


e2d57bfe ^
42b31beb ^
c7c822b2 ^
42b31beb ^

8d7839b9 ^




6f5d7864 ^
8d7839b9 ^










6f5d7864 ^
8d7839b9 ^






42b31beb ^

8d7839b9 ^
42b31beb ^
f7051fad ^
8d7839b9 ^

42b31beb ^
c7c822b2 ^

42b31beb ^
c7c822b2 ^

42b31beb ^

e2d57bfe ^
42b31beb ^
c7c822b2 ^
42b31beb ^
e2d57bfe ^
42b31beb ^
c7c822b2 ^
42b31beb ^

8d7839b9 ^




6f5d7864 ^
8d7839b9 ^












6f5d7864 ^
8d7839b9 ^







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
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235