about summary refs log tree commit diff stats
diff options
context:
space:
mode:
authorKartik K. Agaram <vc@akkartik.com>2015-08-19 22:13:15 -0700
committerKartik K. Agaram <vc@akkartik.com>2015-08-19 22:13:15 -0700
commit0a2026a6bdb43568422f368846130685b46a8074 (patch)
tree64d59aafedcaa234ea539c2ba4dd9e77311fbbee
parentcecb1b1b6b317425d6ff50a9a728c83e7f6f3b10 (diff)
downloadmu-0a2026a6bdb43568422f368846130685b46a8074.tar.gz
2039 - warn on unbalanced '['
-rw-r--r--011load.cc48
-rw-r--r--038scheduler.cc4
-rw-r--r--060string.mu72
-rw-r--r--081run_interactive.cc3
-rw-r--r--edit.mu29
5 files changed, 139 insertions, 17 deletions
diff --git a/011load.cc b/011load.cc
index 301dfdf2..8afe076f 100644
--- a/011load.cc
+++ b/011load.cc
@@ -63,7 +63,7 @@ recipe slurp_recipe(istream& in) {
   instruction curr;
   while (next_instruction(in, &curr)) {
     // End Rewrite Instruction(curr)
-//?     cerr << "instruction: " << curr.to_string() << '\n'; //? 2
+//?     cerr << "instruction: " << curr.to_string() << '\n'; //? 3
     result.steps.push_back(curr);
   }
   return result;
@@ -72,27 +72,39 @@ recipe slurp_recipe(istream& in) {
 bool next_instruction(istream& in, instruction* curr) {
   in >> std::noskipws;
   curr->clear();
-  if (in.eof()) return false;
+  if (in.eof()) {
+    raise << "0: unbalanced '[' for recipe\n" << end();
+    return false;
+  }
 //?   show_rest_of_stream(in); //? 1
-  skip_whitespace(in);  if (in.eof()) return false;
+  skip_whitespace(in);
+  if (in.eof()) {
+    raise << "1: unbalanced '[' for recipe\n" << end();
+    return false;
+  }
 //?   show_rest_of_stream(in); //? 1
-  skip_whitespace_and_comments(in);  if (in.eof()) return false;
+  skip_whitespace_and_comments(in);
+  if (in.eof()) {
+    raise << "2: unbalanced '[' for recipe\n" << end();
+    return false;
+  }
 
   vector<string> words;
 //?   show_rest_of_stream(in); //? 1
-  while (in.peek() != '\n') {
-    skip_whitespace(in);  if (in.eof()) return false;
+  while (in.peek() != '\n' && !in.eof()) {
+    skip_whitespace(in);
+    if (in.eof()) {
+      raise << "3: unbalanced '[' for recipe\n" << end();
+      return false;
+    }
 //?     show_rest_of_stream(in); //? 1
-    string word = next_word(in);  if (in.eof()) return false;
-//?     cerr << "AAA: " << word << '\n'; //? 1
+    string word = next_word(in);
     words.push_back(word);
-    skip_whitespace(in);  if (in.eof()) return false;
+    skip_whitespace(in);
   }
-  skip_whitespace_and_comments(in);  if (in.eof()) return false;
-
+  skip_whitespace_and_comments(in);
 //?   if (SIZE(words) == 1) cout << words.at(0) << ' ' << SIZE(words.at(0)) << '\n'; //? 1
   if (SIZE(words) == 1 && words.at(0) == "]") {
-//?     cout << "AAA\n"; //? 1
     return false;  // end of recipe
   }
 
@@ -100,7 +112,11 @@ bool next_instruction(istream& in, instruction* curr) {
     curr->is_label = true;
     curr->label = words.at(0);
     trace("parse") << "label: " << curr->label << end();
-    return !in.eof();
+    if (in.eof()) {
+      raise << "7: unbalanced '[' for recipe\n" << end();
+      return false;
+    }
+    return true;
   }
 
   vector<string>::iterator p = words.begin();
@@ -141,7 +157,11 @@ bool next_instruction(istream& in, instruction* curr) {
   for (vector<reagent>::iterator p = curr->products.begin(); p != curr->products.end(); ++p) {
     trace("parse") << "  product: " << p->to_string() << end();
   }
-  return !in.eof();
+  if (in.eof()) {
+    raise << "9: unbalanced '[' for recipe\n" << end();
+    return false;
+  }
+  return true;
 }
 
 string next_word(istream& in) {
diff --git a/038scheduler.cc b/038scheduler.cc
index dc4d56f1..1b68682d 100644
--- a/038scheduler.cc
+++ b/038scheduler.cc
@@ -212,8 +212,8 @@ recipe f2 [
 //: this scenario will require some careful setup in escaped C++
 //: (straining our tangle capabilities to near-breaking point)
 :(scenario scheduler_skips_completed_routines)
-% recipe_ordinal f1 = load("recipe f1 [\n1:number <- copy 0\n]").front();
-% recipe_ordinal f2 = load("recipe f2 [\n2:number <- copy 0\n]").front();
+% recipe_ordinal f1 = load("recipe f1 [\n1:number <- copy 0\n]\n").front();
+% recipe_ordinal f2 = load("recipe f2 [\n2:number <- copy 0\n]\n").front();
 % Routines.push_back(new routine(f1));  // f1 meant to run
 % Routines.push_back(new routine(f2));
 % Routines.back()->state = COMPLETED;  // f2 not meant to run
diff --git a/060string.mu b/060string.mu
index c7c8bf71..df82ee23 100644
--- a/060string.mu
+++ b/060string.mu
@@ -404,6 +404,78 @@ scenario string-append-1 [
   ]
 ]
 
+scenario replace-character-in-string [
+  run [
+    1:address:array:character/raw <- new [abc]
+    1:address:array:character/raw <- string-replace 1:address:array:character/raw, 98/b, 122/z
+    2:array:character/raw <- copy *1:address:array:character/raw
+  ]
+  memory-should-contain [
+    2:string <- [azc]
+  ]
+]
+
+recipe string-replace [
+  local-scope
+  s:address:array:character <- next-ingredient
+  oldc:character <- next-ingredient
+  newc:character <- next-ingredient
+  from:number <- next-ingredient
+  len:number <- length *s
+  i:number <- find-next s, oldc, from
+  done?:boolean <- greater-or-equal i, len
+  reply-if done?, s/same-as-ingredient:0
+  dest:address:character <- index-address *s, i
+  *dest <- copy newc
+  i <- add i, 1
+  s <- string-replace s, oldc, newc, i
+  reply s/same-as-ingredient:0
+]
+
+scenario replace-character-at-start [
+  run [
+    1:address:array:character/raw <- new [abc]
+    1:address:array:character/raw <- string-replace 1:address:array:character/raw, 97/a, 122/z
+    2:array:character/raw <- copy *1:address:array:character/raw
+  ]
+  memory-should-contain [
+    2:string <- [zbc]
+  ]
+]
+
+scenario replace-character-at-end [
+  run [
+    1:address:array:character/raw <- new [abc]
+    1:address:array:character/raw <- string-replace 1:address:array:character/raw, 99/c, 122/z
+    2:array:character/raw <- copy *1:address:array:character/raw
+  ]
+  memory-should-contain [
+    2:string <- [abz]
+  ]
+]
+
+scenario replace-character-missing [
+  run [
+    1:address:array:character/raw <- new [abc]
+    1:address:array:character/raw <- string-replace 1:address:array:character/raw, 100/d, 122/z
+    2:array:character/raw <- copy *1:address:array:character/raw
+  ]
+  memory-should-contain [
+    2:string <- [abc]
+  ]
+]
+
+scenario replace-all-characters [
+  run [
+    1:address:array:character/raw <- new [banana]
+    1:address:array:character/raw <- string-replace 1:address:array:character/raw, 97/a, 122/z
+    2:array:character/raw <- copy *1:address:array:character/raw
+  ]
+  memory-should-contain [
+    2:string <- [bznznz]
+  ]
+]
+
 # replace underscores in first with remaining args
 # result:address:array:character <- interpolate template:address:array:character, ...
 recipe interpolate [
diff --git a/081run_interactive.cc b/081run_interactive.cc
index e290024d..e97e2898 100644
--- a/081run_interactive.cc
+++ b/081run_interactive.cc
@@ -314,7 +314,8 @@ case RELOAD: {
   }
   Hide_warnings = true;
   Disable_redefine_warnings = true;
-  vector<recipe_ordinal> recipes_reloaded = load(read_mu_string(ingredients.at(0).at(0)));
+  string code = read_mu_string(ingredients.at(0).at(0));
+  vector<recipe_ordinal> recipes_reloaded = load(code);
   for (long long int i = 0; i < SIZE(recipes_reloaded); ++i) {
     Name.erase(recipes_reloaded.at(i));
   }
diff --git a/edit.mu b/edit.mu
index eb4ba61f..4d62e7c6 100644
--- a/edit.mu
+++ b/edit.mu
@@ -6068,6 +6068,35 @@ recipe foo [
   ]
 ]
 
+scenario run-shows-unbalanced-bracket-warnings [
+  $close-trace
+  assume-screen 100/width, 15/height
+  # recipe is incomplete (unbalanced '[')
+  1:address:array:character <- new [ 
+recipe foo «
+  x <- copy 0
+]
+  string-replace 1:address:array:character, 171/«, 91  # '['
+  2:address:array:character <- new [foo]
+  3:address:programming-environment-data <- new-programming-environment screen:address, 1:address:array:character, 2:address:array:character
+  assume-console [
+    press 65532  # F4
+  ]
+  run [
+    event-loop screen:address, console:address, 3:address:programming-environment-data
+  ]
+  screen-should-contain [
+    .  errors found                                                                   run (F4)           .
+    .                                                  ┊foo                                              .
+    .recipe foo \\\[                                      ┊━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━.
+    .  x <- copy 0                                     ┊                                                 .
+    .                                                  ┊                                                 .
+    .9: unbalanced '\\\[' for recipe                      ┊                                                 .
+    .┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┊                                                 .
+    .                                                  ┊                                                 .
+  ]
+]
+
 scenario run-shows-get-on-non-container-warnings [
   $close-trace
   assume-screen 100/width, 15/height
b5c76fa08357e33b6da0e'>^
94bcf188 ^
d239a127 ^
5505387c ^
d239a127 ^

94bcf188 ^
5505387c ^
94bcf188 ^
d239a127 ^
94bcf188 ^

dd11334b ^

3a22719b ^

235339e5 ^
3a22719b ^





d827abdd ^


4c78534b ^
0346fda0 ^






bbdc3413 ^
d827abdd ^

ec75b5e0 ^





94bcf188 ^
2cea2639 ^

94bcf188 ^

dd11334b ^

94bcf188 ^
70b923a7 ^
f5686cab ^
70b923a7 ^
94bcf188 ^
235339e5 ^
94bcf188 ^








94bcf188 ^



dd11334b ^

94bcf188 ^

5505387c ^
94bcf188 ^
4c78534b ^

5505387c ^

94bcf188 ^

94bcf188 ^
94bcf188 ^
5505387c ^

e263e00a ^




dd11334b ^
e263e00a ^






240f6a3f ^
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