996
|
1 // Action.h
|
|
2
|
|
3 #ifndef ACTION_H_RAIL_1
|
|
4 #define ACTION_H_RAIL_1
|
|
5
|
|
6 #include "Binding.h"
|
|
7 #include "Dir.h"
|
|
8 #include "NilVar.h"
|
|
9 #include "StringVar.h"
|
|
10 #include "ListVar.h"
|
|
11 #include "Closure.h"
|
|
12 #include "Board.h"
|
|
13 #include "ActivationRecord.h"
|
|
14 #include "Error.h"
|
|
15
|
|
16 class Action
|
|
17 {
|
|
18 public:
|
|
19 virtual void run(std::list<Binding> & dataStack,
|
|
20 std::list<ActivationRecord> & programStack,
|
|
21 std::map<std::string, Board> & globals)=0;
|
|
22 public:
|
|
23 // Helper functions. These are called from individual actions to do
|
|
24 // a common task.
|
|
25 static Action * charToAction(char current);
|
|
26 static void checkVarCount(std::list<Binding> const & theStack, int count);
|
|
27 static bool checkBool(Binding var);
|
|
28 static int checkNumeric(Binding var);
|
|
29 static ConsCell checkList(Binding var);
|
|
30 static std::string checkString(Binding var);
|
|
31
|
|
32 static bool areEqual(Binding left, Binding right);
|
|
33
|
|
34 static void runY(std::list<Binding> & dataStack,
|
|
35 std::list<ActivationRecord> & programStack,
|
|
36 Dir::t first, Dir::t second, Dir::t third);
|
|
37 static void callFunctionByName(std::string const & name,
|
|
38 std::list<ActivationRecord> & programStack,
|
|
39 std::map<std::string, Board> const & globals);
|
|
40 };
|
|
41
|
|
42 class Nop : public Action
|
|
43 {
|
|
44 public:
|
|
45 static Nop v;
|
|
46 virtual void run(std::list<Binding> &, std::list<ActivationRecord> &,
|
|
47 std::map<std::string, Board> &)
|
|
48 {
|
|
49 }
|
|
50 };
|
|
51
|
|
52 class True : public Action
|
|
53 {
|
|
54 public:
|
|
55 static True v;
|
|
56 virtual void run(std::list<Binding> & dataStack,
|
|
57 std::list<ActivationRecord> &,
|
|
58 std::map<std::string, Board> &)
|
|
59 {
|
|
60 dataStack.push_front(Binding(new StringVar("1")));
|
|
61 }
|
|
62 };
|
|
63
|
|
64 class False : public Action
|
|
65 {
|
|
66 public:
|
|
67 static False v;
|
|
68 virtual void run(std::list<Binding> & dataStack,
|
|
69 std::list<ActivationRecord> &,
|
|
70 std::map<std::string, Board> &)
|
|
71 {
|
|
72 dataStack.push_front(Binding(new StringVar("0")));
|
|
73 }
|
|
74 };
|
|
75
|
|
76 // The direction before the y junction is the direction which the
|
|
77 // 'arrow' points to. So '>' is an EastY.
|
|
78
|
|
79 // This is the '>' junction.
|
|
80 class EastY : public Action
|
|
81 {
|
|
82 public:
|
|
83 static EastY v;
|
|
84 virtual void run(std::list<Binding> & dataStack,
|
|
85 std::list<ActivationRecord> & programStack,
|
|
86 std::map<std::string, Board> &)
|
|
87 {
|
|
88 runY(dataStack, programStack, Dir::west, Dir::southeast, Dir::northeast);
|
|
89 }
|
|
90 };
|
|
91
|
|
92 // This is the '<' junction.
|
|
93 class WestY : public Action
|
|
94 {
|
|
95 public:
|
|
96 static WestY v;
|
|
97 virtual void run(std::list<Binding> & dataStack,
|
|
98 std::list<ActivationRecord> & programStack,
|
|
99 std::map<std::string, Board> &)
|
|
100 {
|
|
101 runY(dataStack, programStack, Dir::east, Dir::northwest, Dir::southwest);
|
|
102 }
|
|
103 };
|
|
104
|
|
105 // This is the '^' junction.
|
|
106 class NorthY : public Action
|
|
107 {
|
|
108 public:
|
|
109 static NorthY v;
|
|
110 virtual void run(std::list<Binding> & dataStack,
|
|
111 std::list<ActivationRecord> & programStack,
|
|
112 std::map<std::string, Board> &)
|
|
113 {
|
|
114 runY(dataStack, programStack, Dir::south, Dir::northeast, Dir::northwest);
|
|
115 }
|
|
116 };
|
|
117
|
|
118 // This is the 'v' junction.
|
|
119 class SouthY : public Action
|
|
120 {
|
|
121 public:
|
|
122 static SouthY v;
|
|
123 virtual void run(std::list<Binding> & dataStack,
|
|
124 std::list<ActivationRecord> & programStack,
|
|
125 std::map<std::string, Board> &)
|
|
126 {
|
|
127 runY(dataStack, programStack, Dir::north, Dir::southwest, Dir::southeast);
|
|
128 }
|
|
129 };
|
|
130
|
|
131 class Boom : public Action
|
|
132 {
|
|
133 public:
|
|
134 static Boom v;
|
|
135 virtual void run(std::list<Binding> & dataStack,
|
|
136 std::list<ActivationRecord> &,
|
|
137 std::map<std::string, Board> &)
|
|
138 {
|
|
139 checkVarCount(dataStack, 1);
|
|
140 std::string custom = checkString(dataStack.front());
|
|
141 throw CrashException(Error::custom, custom);
|
|
142 }
|
|
143 };
|
|
144
|
|
145 class EofCheck : public Action
|
|
146 {
|
|
147 public:
|
|
148 static EofCheck v;
|
|
149 virtual void run(std::list<Binding> & dataStack,
|
|
150 std::list<ActivationRecord> & programStack,
|
|
151 std::map<std::string, Board> & globals)
|
|
152 {
|
|
153 if (args::input->peek() == EOF)
|
|
154 {
|
|
155 True::v.run(dataStack, programStack, globals);
|
|
156 }
|
|
157 else
|
|
158 {
|
|
159 False::v.run(dataStack, programStack, globals);
|
|
160 }
|
|
161 }
|
|
162 };
|
|
163
|
|
164 class Input : public Action
|
|
165 {
|
|
166 public:
|
|
167 static Input v;
|
|
168 virtual void run(std::list<Binding> & dataStack,
|
|
169 std::list<ActivationRecord> &,
|
|
170 std::map<std::string, Board> &)
|
|
171 {
|
|
172 int val = args::input->get();
|
|
173 if (val == EOF)
|
|
174 {
|
|
175 throw CrashException(Error::noMoreInput);
|
|
176 }
|
|
177 else
|
|
178 {
|
|
179 dataStack.push_front(Binding(new StringVar(static_cast<char>(val)
|
|
180 + std::string())));
|
|
181 }
|
|
182 }
|
|
183 };
|
|
184
|
|
185 class Output : public Action
|
|
186 {
|
|
187 public:
|
|
188 static Output v;
|
|
189 virtual void run(std::list<Binding> & dataStack,
|
|
190 std::list<ActivationRecord> &,
|
|
191 std::map<std::string, Board> &)
|
|
192 {
|
|
193 checkVarCount(dataStack, 1);
|
|
194 std::string val = checkString(dataStack.front());
|
|
195 dataStack.pop_front();
|
|
196 *(args::output) << val;
|
|
197 }
|
|
198 };
|
|
199
|
|
200 class Underflow : public Action
|
|
201 {
|
|
202 public:
|
|
203 static Underflow v;
|
|
204 virtual void run(std::list<Binding> & dataStack,
|
|
205 std::list<ActivationRecord> &,
|
|
206 std::map<std::string, Board> &)
|
|
207 {
|
|
208 std::string count = intToString(static_cast<int>(dataStack.size()));
|
|
209 dataStack.push_front(Binding(new StringVar(count)));
|
|
210 }
|
|
211 };
|
|
212
|
|
213 class TypeP : public Action
|
|
214 {
|
|
215 public:
|
|
216 static TypeP v;
|
|
217 virtual void run(std::list<Binding> & dataStack,
|
|
218 std::list<ActivationRecord> &,
|
|
219 std::map<std::string, Board> &)
|
|
220 {
|
|
221 checkVarCount(dataStack, 1);
|
|
222 Binding top = dataStack.front();
|
|
223 dataStack.pop_front();
|
|
224 std::string type;
|
|
225 if (top->getString() != NULL)
|
|
226 {
|
|
227 type = "string";
|
|
228 }
|
|
229 else if (top->getList() != NULL)
|
|
230 {
|
|
231 type = "list";
|
|
232 }
|
|
233 else if (top->getNil() != NULL)
|
|
234 {
|
|
235 type = "nil";
|
|
236 }
|
|
237 else if (top->getClosure() != NULL)
|
|
238 {
|
|
239 type = "lambda";
|
|
240 }
|
|
241 else
|
|
242 {
|
|
243 throw InternalException("TypeP::run(): Unknown type");
|
|
244 }
|
|
245 dataStack.push_front(Binding(new StringVar(type)));
|
|
246 }
|
|
247 };
|
|
248
|
|
249 class PushConstant : public Action
|
|
250 {
|
|
251 public:
|
|
252 static PushConstant v;
|
|
253 virtual void run(std::list<Binding> & dataStack,
|
|
254 std::list<ActivationRecord> &,
|
|
255 std::map<std::string, Board> &)
|
|
256 {
|
|
257 // Our job is done. The system pushed the string in delimiters
|
|
258 // onto the stack. That is just what we want to do.
|
|
259 checkVarCount(dataStack, 1);
|
|
260 checkString(dataStack.front());
|
|
261 }
|
|
262 };
|
|
263
|
|
264 class UseVariable : public Action
|
|
265 {
|
|
266 public:
|
|
267 static UseVariable v;
|
|
268 virtual void run(std::list<Binding> & dataStack,
|
|
269 std::list<ActivationRecord> & programStack,
|
|
270 std::map<std::string, Board> &)
|
|
271 {
|
|
272 checkVarCount(dataStack, 1);
|
|
273 std::string varName = checkString(dataStack.front());
|
|
274 dataStack.pop_front();
|
|
275 std::map<std::string, Binding>::const_iterator pos
|
|
276 = programStack.front().getEnvironment().find(varName);
|
|
277 if (pos != programStack.front().getEnvironment().end())
|
|
278 {
|
|
279 dataStack.push_front(pos->second);
|
|
280 }
|
|
281 else
|
|
282 {
|
|
283 throw CrashException(Error::localBindingNotFound);
|
|
284 }
|
|
285 }
|
|
286 };
|
|
287
|
|
288 class BindVariable : public Action
|
|
289 {
|
|
290 public:
|
|
291 static BindVariable v;
|
|
292 virtual void run(std::list<Binding> & dataStack,
|
|
293 std::list<ActivationRecord> & programStack,
|
|
294 std::map<std::string, Board> &)
|
|
295 {
|
|
296 checkVarCount(dataStack, 2);
|
|
297 std::string varName = checkString(dataStack.front());
|
|
298 dataStack.pop_front();
|
|
299 programStack.front().bindVariable(varName, dataStack.front());
|
|
300 dataStack.pop_front();
|
|
301 }
|
|
302 };
|
|
303
|
|
304 class CallFunction : public Action
|
|
305 {
|
|
306 public:
|
|
307 static CallFunction v;
|
|
308 virtual void run(std::list<Binding> & dataStack,
|
|
309 std::list<ActivationRecord> & programStack,
|
|
310 std::map<std::string, Board> & globals)
|
|
311 {
|
|
312 checkVarCount(dataStack, 1);
|
|
313 std::string functionName = checkString(dataStack.front());
|
|
314 dataStack.pop_front();
|
|
315 if (functionName != "")
|
|
316 {
|
|
317 callFunctionByName(functionName, programStack, globals);
|
|
318 }
|
|
319 else
|
|
320 {
|
|
321 Binding function = dataStack.front();
|
|
322 if (function->getString() != NULL)
|
|
323 {
|
|
324 callFunctionByName(function->getString()->get(), programStack,
|
|
325 globals);
|
|
326 }
|
|
327 else if (function->getClosure() != NULL)
|
|
328 {
|
|
329 programStack.push_front(ActivationRecord(*(function->getClosure())));
|
|
330 }
|
|
331 else
|
|
332 {
|
|
333 throw CrashException(Error::typeMismatch);
|
|
334 }
|
|
335 dataStack.pop_front();
|
|
336 }
|
|
337 }
|
|
338 };
|
|
339
|
|
340 class Add : public Action
|
|
341 {
|
|
342 public:
|
|
343 static Add v;
|
|
344 virtual void run(std::list<Binding> & dataStack,
|
|
345 std::list<ActivationRecord> &,
|
|
346 std::map<std::string, Board> &)
|
|
347 {
|
|
348 checkVarCount(dataStack, 2);
|
|
349 int right = checkNumeric(dataStack.front());
|
|
350 dataStack.pop_front();
|
|
351 int left = checkNumeric(dataStack.front());
|
|
352 dataStack.pop_front();
|
|
353 dataStack.push_front(Binding(new StringVar(intToString(left + right))));
|
|
354 }
|
|
355 };
|
|
356
|
|
357 class Divide : public Action
|
|
358 {
|
|
359 public:
|
|
360 static Divide v;
|
|
361 virtual void run(std::list<Binding> & dataStack,
|
|
362 std::list<ActivationRecord> &,
|
|
363 std::map<std::string, Board> &)
|
|
364 {
|
|
365 checkVarCount(dataStack, 2);
|
|
366 int right = checkNumeric(dataStack.front());
|
|
367 dataStack.pop_front();
|
|
368 int left = checkNumeric(dataStack.front());
|
|
369 dataStack.pop_front();
|
|
370 dataStack.push_front(Binding(new StringVar(intToString(left / right))));
|
|
371 }
|
|
372 };
|
|
373
|
|
374 class Multiply : public Action
|
|
375 {
|
|
376 public:
|
|
377 static Multiply v;
|
|
378 virtual void run(std::list<Binding> & dataStack,
|
|
379 std::list<ActivationRecord> &,
|
|
380 std::map<std::string, Board> &)
|
|
381 {
|
|
382 checkVarCount(dataStack, 2);
|
|
383 int right = checkNumeric(dataStack.front());
|
|
384 dataStack.pop_front();
|
|
385 int left = checkNumeric(dataStack.front());
|
|
386 dataStack.pop_front();
|
|
387 dataStack.push_front(Binding(new StringVar(intToString(left * right))));
|
|
388 }
|
|
389 };
|
|
390
|
|
391 class Remainder : public Action
|
|
392 {
|
|
393 public:
|
|
394 static Remainder v;
|
|
395 virtual void run(std::list<Binding> & dataStack,
|
|
396 std::list<ActivationRecord> &,
|
|
397 std::map<std::string, Board> &)
|
|
398 {
|
|
399 checkVarCount(dataStack, 2);
|
|
400 int right = checkNumeric(dataStack.front());
|
|
401 dataStack.pop_front();
|
|
402 int left = checkNumeric(dataStack.front());
|
|
403 dataStack.pop_front();
|
|
404 dataStack.push_front(Binding(new StringVar(intToString(left % right))));
|
|
405 }
|
|
406 };
|
|
407
|
|
408 class Subtract : public Action
|
|
409 {
|
|
410 public:
|
|
411 static Subtract v;
|
|
412 virtual void run(std::list<Binding> & dataStack,
|
|
413 std::list<ActivationRecord> &,
|
|
414 std::map<std::string, Board> &)
|
|
415 {
|
|
416 checkVarCount(dataStack, 2);
|
|
417 int right = checkNumeric(dataStack.front());
|
|
418 dataStack.pop_front();
|
|
419 int left = checkNumeric(dataStack.front());
|
|
420 dataStack.pop_front();
|
|
421 dataStack.push_front(Binding(new StringVar(intToString(left - right))));
|
|
422 }
|
|
423 };
|
|
424
|
|
425 class Digit : public Action
|
|
426 {
|
|
427 public:
|
|
428 static Digit v0;
|
|
429 static Digit v1;
|
|
430 static Digit v2;
|
|
431 static Digit v3;
|
|
432 static Digit v4;
|
|
433 static Digit v5;
|
|
434 static Digit v6;
|
|
435 static Digit v7;
|
|
436 static Digit v8;
|
|
437 static Digit v9;
|
|
438 Digit(int newNum = 0) : num(newNum) {}
|
|
439 virtual void run(std::list<Binding> & dataStack,
|
|
440 std::list<ActivationRecord> &,
|
|
441 std::map<std::string, Board> &)
|
|
442 {
|
|
443 dataStack.push_front(Binding(new StringVar(intToString(num))));
|
|
444 }
|
|
445 private:
|
|
446 int num;
|
|
447 };
|
|
448
|
|
449 class Cut : public Action
|
|
450 {
|
|
451 public:
|
|
452 static Cut v;
|
|
453 virtual void run(std::list<Binding> & dataStack,
|
|
454 std::list<ActivationRecord> &,
|
|
455 std::map<std::string, Board> &)
|
|
456 {
|
|
457 checkVarCount(dataStack, 2);
|
|
458 int index = checkNumeric(dataStack.front());
|
|
459 dataStack.pop_front();
|
|
460 std::string str = checkString(dataStack.front());
|
|
461 dataStack.pop_front();
|
|
462 if (index < 0 || index > static_cast<int>(str.size()))
|
|
463 {
|
|
464 throw CrashException(Error::indexOutOfBounds);
|
|
465 }
|
|
466 std::string leftResult = str.substr(0, index);
|
|
467 std::string rightResult = str.substr(index);
|
|
468 dataStack.push_front(Binding(new StringVar(leftResult)));
|
|
469 dataStack.push_front(Binding(new StringVar(rightResult)));
|
|
470 }
|
|
471 };
|
|
472
|
|
473 class Append : public Action
|
|
474 {
|
|
475 public:
|
|
476 static Append v;
|
|
477 virtual void run(std::list<Binding> & dataStack,
|
|
478 std::list<ActivationRecord> &,
|
|
479 std::map<std::string, Board> &)
|
|
480 {
|
|
481 checkVarCount(dataStack, 2);
|
|
482 std::string right = checkString(dataStack.front());
|
|
483 dataStack.pop_front();
|
|
484 std::string left = checkString(dataStack.front());
|
|
485 dataStack.pop_front();
|
|
486 dataStack.push_front(Binding(new StringVar(left + right)));
|
|
487 }
|
|
488 };
|
|
489
|
|
490 class Size : public Action
|
|
491 {
|
|
492 public:
|
|
493 static Size v;
|
|
494 virtual void run(std::list<Binding> & dataStack,
|
|
495 std::list<ActivationRecord> &,
|
|
496 std::map<std::string, Board> &)
|
|
497 {
|
|
498 checkVarCount(dataStack, 1);
|
|
499 std::string str = checkString(dataStack.front());
|
|
500 dataStack.pop_front();
|
|
501 int size = static_cast<int>(str.size());
|
|
502 dataStack.push_front(Binding(new StringVar(intToString(size))));
|
|
503 }
|
|
504 };
|
|
505
|
|
506 class PushNIL : public Action
|
|
507 {
|
|
508 public:
|
|
509 static PushNIL v;
|
|
510 virtual void run(std::list<Binding> & dataStack,
|
|
511 std::list<ActivationRecord> &,
|
|
512 std::map<std::string, Board> &)
|
|
513 {
|
|
514 dataStack.push_front(NIL);
|
|
515 }
|
|
516 };
|
|
517
|
|
518 class Cons : public Action
|
|
519 {
|
|
520 public:
|
|
521 static Cons v;
|
|
522 virtual void run(std::list<Binding> & dataStack,
|
|
523 std::list<ActivationRecord> &,
|
|
524 std::map<std::string, Board> &)
|
|
525 {
|
|
526 checkVarCount(dataStack, 2);
|
|
527 Binding car = dataStack.front();
|
|
528 dataStack.pop_front();
|
|
529 Binding cdr = dataStack.front();
|
|
530 dataStack.pop_front();
|
|
531 dataStack.push_front(Binding(new ListVar(car, cdr)));
|
|
532 }
|
|
533 };
|
|
534
|
|
535 class ListBreakup : public Action
|
|
536 {
|
|
537 public:
|
|
538 static ListBreakup v;
|
|
539 virtual void run(std::list<Binding> & dataStack,
|
|
540 std::list<ActivationRecord> &,
|
|
541 std::map<std::string, Board> &)
|
|
542 {
|
|
543 checkVarCount(dataStack, 1);
|
|
544 ConsCell cell = checkList(dataStack.front());
|
|
545 dataStack.pop_front();
|
|
546 dataStack.push_front(cell.cdr);
|
|
547 dataStack.push_front(cell.car);
|
|
548 }
|
|
549 };
|
|
550
|
|
551 class GreaterThan : public Action
|
|
552 {
|
|
553 public:
|
|
554 static GreaterThan v;
|
|
555 virtual void run(std::list<Binding> & dataStack,
|
|
556 std::list<ActivationRecord> & programStack,
|
|
557 std::map<std::string, Board> & globals)
|
|
558 {
|
|
559 checkVarCount(dataStack, 2);
|
|
560 int right = checkNumeric(dataStack.front());
|
|
561 dataStack.pop_front();
|
|
562 int left = checkNumeric(dataStack.front());
|
|
563 dataStack.pop_front();
|
|
564 if (left > right)
|
|
565 {
|
|
566 True::v.run(dataStack, programStack, globals);
|
|
567 }
|
|
568 else
|
|
569 {
|
|
570 False::v.run(dataStack, programStack, globals);
|
|
571 }
|
|
572 }
|
|
573 };
|
|
574
|
|
575 class IsEqualTo : public Action
|
|
576 {
|
|
577 public:
|
|
578 static IsEqualTo v;
|
|
579 virtual void run(std::list<Binding> & dataStack,
|
|
580 std::list<ActivationRecord> & programStack,
|
|
581 std::map<std::string, Board> & globals)
|
|
582 {
|
|
583 checkVarCount(dataStack, 2);
|
|
584 Binding right = dataStack.front();
|
|
585 dataStack.pop_front();
|
|
586 Binding left = dataStack.front();
|
|
587 dataStack.pop_front();
|
|
588 if (areEqual(left, right))
|
|
589 {
|
|
590 True::v.run(dataStack, programStack, globals);
|
|
591 }
|
|
592 else
|
|
593 {
|
|
594 False::v.run(dataStack, programStack, globals);
|
|
595 }
|
|
596 }
|
|
597 };
|
|
598
|
|
599 class Reflector : public Action
|
|
600 {
|
|
601 public:
|
|
602 static Reflector v;
|
|
603 virtual void run(std::list<Binding> &,
|
|
604 std::list<ActivationRecord> & programStack,
|
|
605 std::map<std::string, Board> &)
|
|
606 {
|
|
607 ActivationRecord & record = programStack.front();
|
|
608 record.setDirection(Dir::back(record.getDirection()));
|
|
609 }
|
|
610 };
|
|
611
|
|
612 class Lambda : public Action
|
|
613 {
|
|
614 public:
|
|
615 static Lambda v;
|
|
616 virtual void run(std::list<Binding> & dataStack,
|
|
617 std::list<ActivationRecord> & programStack,
|
|
618 std::map<std::string, Board> & globals)
|
|
619 {
|
|
620 ActivationRecord & record = programStack.front();
|
|
621 if (record.getFunction() == NULL)
|
|
622 {
|
|
623 throw InternalException("Lambda: No function in the given record");
|
|
624 }
|
|
625 dataStack.push_front(Binding(new Closure(record.getEnvironment(),
|
|
626 *(record.getFunction()),
|
|
627 record.getPosition(),
|
|
628 record.getDirection())));
|
|
629 record.setDirection(Dir::back(record.getDirection()));
|
|
630 }
|
|
631 };
|
|
632
|
|
633 class End : public Action
|
|
634 {
|
|
635 public:
|
|
636 static End v;
|
|
637 virtual void run(std::list<Binding> & dataStack,
|
|
638 std::list<ActivationRecord> & programStack,
|
|
639 std::map<std::string, Board> &)
|
|
640 {
|
|
641 programStack.pop_front();
|
|
642 }
|
|
643 };
|
|
644
|
|
645 #endif
|