1
2
3
4 :(before "End Mu Types Initialization")
5 put(Type_abbreviations, "text", new_type_tree("address:array:character"));
6
7 :(scenario new_string)
8 def main [
9 1:text <- new [abc def]
10 2:char <- index *1:text, 5
11 ]
12
13 +mem: storing 101 in location 2
14
15 :(scenario new_string_handles_unicode)
16 def main [
17 1:text <- new [a«c]
18 2:num <- length *1:text
19 3:char <- index *1:text, 1
20 ]
21 +mem: storing 3 in location 2
22
23 +mem: storing 171 in location 3
24
25 :(before "End NEW Check Special-cases")
26 if (is_literal_text(inst.ingredients.at(0))) break;
27 :(before "Convert 'new' To 'allocate'")
28 if (inst.name == "new" && !inst.ingredients.empty() && is_literal_text(inst.ingredients.at(0))) continue;
29 :(after "case NEW" following "Primitive Recipe Implementations")
30 if (is_literal_text(current_instruction().ingredients.at(0))) {
31 ¦ products.resize(1);
32 ¦ products.at(0).push_back(new_mu_text(current_instruction().ingredients.at(0).name));
33 ¦ trace(9999, "mem") << "new string alloc: " << products.at(0).at(0) << end();
34 ¦ break;
35 }
36
37 :(code)
38 int new_mu_text(const string& contents) {
39
40 int string_length = unicode_length(contents);
41
42
43 int result = allocate(string_length+1);
44 trace(9999, "mem") << "storing string refcount 0 in location " << result << end();
45 put(Memory, result, 0);
46 int curr_address = result+1;
47 trace(9999, "mem") << "storing string length " << string_length << " in location " << curr_address << end();
48 put(Memory, curr_address, string_length);
49 ++curr_address;
50 int curr = 0;
51 const char* raw_contents = contents.c_str();
52 for (int i = 0; i < string_length; ++i) {
53 ¦ uint32_t curr_character;
54 ¦ assert(curr < SIZE(contents));
55 ¦ tb_utf8_char_to_unicode(&curr_character, &raw_contents[curr]);
56 ¦ trace(9999, "mem") << "storing string character " << curr_character << " in location " << curr_address << end();
57 ¦ put(Memory, curr_address, curr_character);
58 ¦ curr += tb_utf8_char_length(raw_contents[curr]);
59 ¦ ++curr_address;
60 }
61
62 return result;
63 }
64
65
66
67 :(scenario string_literal_without_instruction)
68 % Hide_errors = true;
69 def main [
70 [abc]
71 ]
72 +error: main: instruction '[abc]' has no recipe in '[abc]'
73
74
75
76 :(scenario stash_string)
77 def main [
78 1:text <- new [abc]
79 stash [foo:], 1:text
80 ]
81 +app: foo: abc
82
83 :(before "End inspect Special-cases(r, data)")
84 if (is_mu_text(r)) {
85 assert(scalar(data));
86 return read_mu_text(data.at(0));
87 }
88
89 :(before "End $print Special-cases")
90 else if (is_mu_text(current_instruction().ingredients.at(i))) {
91 cout << read_mu_text(ingredients.at(i).at(0));
92 }
93
94 :(scenario unicode_string)
95 def main [
96 1:text <- new [♠]
97 stash [foo:], 1:text
98 ]
99 +app: foo: ♠
100
101 :(scenario stash_space_after_string)
102 def main [
103 1:text <- new [abc]
104 stash 1:text, [foo]
105 ]
106 +app: abc foo
107
108 :(scenario stash_string_as_array)
109 def main [
110 1:text <- new [abc]
111 stash *1:text
112 ]
113 +app: 3 97 98 99
114
115
116 :(before "End Preprocess is_mu_text(reagent x)")
117 if (!canonize_type(x)) return false;
118
119
120 :(scenario new_string_overflow)
121 % Initial_memory_per_routine = 3;
122 def main [
123 1:address:num/raw <- new number:type
124 2:text/raw <- new [a]
125 ]
126 +new: routine allocated memory from 1000 to 1003
127 +new: routine allocated memory from 1003 to 1006
128
129
130 :(code)
131 int unicode_length(const string& s) {
132 const char* in = s.c_str();
133 int result = 0;
134 int curr = 0;
135 while (curr < SIZE(s)) {
136 ¦
137 ¦ ++result;
138 ¦ curr += tb_utf8_char_length(in[curr]);
139 }
140 return result;
141 }
142
143 string read_mu_text(int address) {
144 if (address == 0) return "";
145 ++address;
146 int size = get_or_insert(Memory, address);
147 if (size == 0) return "";
148 ostringstream tmp;
149 for (int curr = address+1; curr <= address+size; ++curr) {
150 ¦ tmp << to_unicode(static_cast<uint32_t>(get_or_insert(Memory, curr)));
151 }
152 return tmp.str();
153 }
154
155
156
157
158
159 :(scenario assert)
160 % Hide_errors = true; // '%' lines insert arbitrary C code into tests before calling 'run' with the lines below. Must be immediately after :(scenario) line.
161 def main [
162 assert 0, [this is an assert in Mu]
163 ]
164 +error: this is an assert in Mu
165
166 :(before "End Primitive Recipe Declarations")
167 ASSERT,
168 :(before "End Primitive Recipe Numbers")
169 put(Recipe_ordinal, "assert", ASSERT);
170 :(before "End Primitive Recipe Checks")
171 case ASSERT: {
172 if (SIZE(inst.ingredients) != 2) {
173 ¦ raise << maybe(get(Recipe, r).name) << "'assert' takes exactly two ingredients rather than '" << inst.original_string << "'\n" << end();
174 ¦ break;
175 }
176 if (!is_mu_scalar(inst.ingredients.at(0))) {
177 ¦ raise << maybe(get(Recipe, r).name) << "'assert' requires a boolean for its first ingredient, but got '" << inst.ingredients.at(0).original_string << "'\n" << end();
178 ¦ break;
179 }
180 if (!is_literal_text(inst.ingredients.at(1)) && !is_mu_text(inst.ingredients.at(1))) {
181 ¦ raise << maybe(get(Recipe, r).name) << "'assert' requires a text as its second ingredient, but got '" << inst.ingredients.at(1).original_string << "'\n" << end();
182 ¦ break;
183 }
184 break;
185 }
186 :(before "End Primitive Recipe Implementations")
187 case ASSERT: {
188 if (!ingredients.at(0).at(0)) {
189 ¦ if (is_literal_text(current_instruction().ingredients.at(1)))
190 ¦ ¦ raise << current_instruction().ingredients.at(1).name << '\n' << end();
191 ¦ else
192 ¦ ¦ raise << read_mu_text(ingredients.at(1).at(0)) << '\n' << end();
193 }
194 break;
195 }
196
197
198
199
200 :(before "End Primitive Recipe Declarations")
201 _READ,
202 :(before "End Primitive Recipe Numbers")
203 put(Recipe_ordinal, "$read", _READ);
204 :(before "End Primitive Recipe Checks")
205 case _READ: {
206 break;
207 }
208 :(before "End Primitive Recipe Implementations")
209 case _READ: {
210 skip_whitespace(cin);
211 string result;
212 if (has_data(cin))
213 ¦ cin >> result;
214 products.resize(1);
215 products.at(0).push_back(new_mu_text(result));
216 break;
217 }
218
219 :(code)
220 void skip_whitespace(istream& in) {
221 while (true) {
222 ¦ if (!has_data(in)) break;
223 ¦ if (isspace(in.peek())) in.get();
224 ¦ else break;
225 }
226 }