996
|
1 /* -*- mode: C; coding: utf-8; tab-width: 4; indent-tabs-mode: t; c-basic-offset: 4 -*-
|
|
2 *
|
|
3 * cfunge - A standard-conforming Befunge93/98/109 interpreter in C.
|
|
4 * Copyright (C) 2008-2009 Arvid Norlander <anmaster AT tele2 DOT se>
|
|
5 *
|
|
6 * This program is free software: you can redistribute it and/or modify
|
|
7 * it under the terms of the GNU General Public License as published by
|
|
8 * the Free Software Foundation, either version 3 of the License, or
|
|
9 * (at the proxy's option) any later version. Arvid Norlander is a
|
|
10 * proxy who can decide which future versions of the GNU General Public
|
|
11 * License can be used.
|
|
12 *
|
|
13 * This program is distributed in the hope that it will be useful,
|
|
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
16 * GNU General Public License for more details.
|
|
17 *
|
|
18 * You should have received a copy of the GNU General Public License
|
|
19 * along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
20 */
|
|
21
|
|
22 #include "../global.h"
|
|
23 #include "iterate.h"
|
|
24 #include "../interpreter.h"
|
|
25 #include "../funge-space/funge-space.h"
|
|
26 #include "../vector.h"
|
|
27 #include "../stack.h"
|
|
28 #include "../ip.h"
|
|
29 #include "../settings.h"
|
|
30
|
|
31 #ifdef CONCURRENT_FUNGE
|
|
32 # define RUNSELF() run_iterate(ip, IPList, threadindex, true)
|
|
33 # define RUNINSTR() execute_instruction(kInstr, ip, threadindex)
|
|
34 #else
|
|
35 # define RUNSELF() run_iterate(ip, true)
|
|
36 # define RUNINSTR() execute_instruction(kInstr, ip)
|
|
37 #endif
|
|
38
|
|
39 #ifndef DISABLE_TRACE
|
|
40 static inline void print_trace(funge_cell iters, funge_cell kInstr)
|
|
41 {
|
|
42 if (FUNGE_UNLIKELY(setting_trace_level > 5))
|
|
43 fprintf(stderr, " * In k: iteration: %" FUNGECELLPRI " instruction: %c (%" FUNGECELLPRI ")\n",
|
|
44 iters, (char)kInstr, kInstr);
|
|
45 }
|
|
46 #endif
|
|
47
|
|
48 /// This moves IP to next instruction, with respect to ;, space and current delta.
|
|
49 static inline funge_cell find_next_instr(instructionPointer * restrict ip, funge_cell kInstr)
|
|
50 {
|
|
51 bool injump = false;
|
|
52 if (kInstr == ';')
|
|
53 injump = true;
|
|
54 while (true) {
|
|
55 ip_forward(ip);
|
|
56 kInstr = fungespace_get(&ip->position);
|
|
57 if (kInstr == ';') {
|
|
58 injump = !injump;
|
|
59 continue;
|
|
60 } else if (kInstr == ' ') {
|
|
61 continue;
|
|
62 } else {
|
|
63 if (injump)
|
|
64 continue;
|
|
65 else
|
|
66 break;
|
|
67 }
|
|
68 }
|
|
69 return kInstr;
|
|
70 }
|
|
71
|
|
72 #ifdef CONCURRENT_FUNGE
|
|
73 FUNGE_ATTR_FAST void run_iterate(instructionPointer * restrict ip, ipList ** IPList, ssize_t * restrict threadindex, bool isRecursive)
|
|
74 #else
|
|
75 FUNGE_ATTR_FAST void run_iterate(instructionPointer * restrict ip, bool isRecursive)
|
|
76 #endif
|
|
77 {
|
|
78 funge_cell iters = stack_pop(ip->stack);
|
|
79 if (iters == 0) {
|
|
80 funge_cell kInstr;
|
|
81 // Skip past next instruction.
|
|
82 ip_forward(ip);
|
|
83 kInstr = fungespace_get(&ip->position);
|
|
84 if (kInstr == ' ' || kInstr == ';') {
|
|
85 find_next_instr(ip, kInstr);
|
|
86 }
|
|
87 } else if (iters < 0) {
|
|
88 ip_reverse(ip);
|
|
89 } else {
|
|
90 funge_cell kInstr;
|
|
91 // Note that:
|
|
92 // * Instruction executes *at* k
|
|
93 // * In Funge-109 we skip over the cell we executed
|
|
94 // (if position or delta didn't change).
|
|
95 // * In Funge-98 we don't do that.
|
|
96
|
|
97 // This is used in case of spaces and with Funge-109
|
|
98 funge_vector oldpos = ip->position;
|
|
99 // And this is for knowing where to move past (in 109)
|
|
100 funge_vector posinstr;
|
|
101 // Fetch instruction
|
|
102 ip_forward(ip);
|
|
103 kInstr = fungespace_get(&ip->position);
|
|
104
|
|
105 // We should reach past any spaces and ;; pairs and execute first
|
|
106 // instruction we find. This is unclear/undef in 98 but defined in 109.
|
|
107 if (kInstr == ' ' || kInstr == ';') {
|
|
108 kInstr = find_next_instr(ip, kInstr);
|
|
109 }
|
|
110
|
|
111 // First store pos where we got to restore to to "move past" instruction in Funge-109.
|
|
112 posinstr = ip->position;
|
|
113 // Then go back and execute it at k...
|
|
114 ip->position = oldpos;
|
|
115
|
|
116 // We special case some stuff here that breaks otherwise.
|
|
117 switch (kInstr) {
|
|
118 case 'z':
|
|
119 return;
|
|
120 case '@':
|
|
121 // Iterating over @ is insane, to avoid issues when doing
|
|
122 // concurrent execution lets just kill current IP.
|
|
123 // In other words, execute this once.
|
|
124 RUNINSTR();
|
|
125 break;
|
|
126 default: {
|
|
127 // Ok we got to execute it!
|
|
128 // Storing second part of the current IP state (for Funge-109)
|
|
129 funge_vector olddelta = ip->delta;
|
|
130
|
|
131 // This horrible kludge is needed because iplist_duplicate_ip
|
|
132 // calls realloc so IP pointer may end up invalid. A horrible
|
|
133 // hack yes.
|
|
134 #ifdef CONCURRENT_FUNGE
|
|
135 ssize_t oldindex = *threadindex;
|
|
136 #endif
|
|
137 while (iters--) {
|
|
138 #ifndef DISABLE_TRACE
|
|
139 print_trace(iters, kInstr);
|
|
140 #endif /* DISABLE_TRACE */
|
|
141
|
|
142 switch (kInstr) {
|
|
143 #ifdef CONCURRENT_FUNGE
|
|
144 case 't':
|
|
145 *threadindex = iplist_duplicate_ip(IPList, *threadindex);
|
|
146 break;
|
|
147 #endif
|
|
148 case 'k':
|
|
149 // I HATE this one...
|
|
150 ip->position = posinstr;
|
|
151 RUNSELF();
|
|
152 // Cludge for realloc again...
|
|
153 #ifdef CONCURRENT_FUNGE
|
|
154 ip = &((*IPList)->ips[oldindex]);
|
|
155 #endif
|
|
156 // Check position here.
|
|
157 if (posinstr.x == ip->position.x
|
|
158 && posinstr.y == ip->position.y)
|
|
159 ip->position = oldpos;
|
|
160 break;
|
|
161 default:
|
|
162 RUNINSTR();
|
|
163 break;
|
|
164 }
|
|
165 }
|
|
166 #ifdef CONCURRENT_FUNGE
|
|
167 if (kInstr == 't')
|
|
168 ip = &((*IPList)->ips[oldindex]);
|
|
169 #endif
|
|
170 // If delta and ip did not change, move forward in Funge-109.
|
|
171 // ...unless we are recursive, to ensure correct behaviour...
|
|
172 if (setting_current_standard == stdver109 && !isRecursive) {
|
|
173 if (olddelta.x == ip->delta.x
|
|
174 && olddelta.y == ip->delta.y
|
|
175 && oldpos.x == ip->position.x
|
|
176 && oldpos.y == ip->position.y)
|
|
177 ip->position = posinstr;
|
|
178 }
|
|
179 break;
|
|
180 }
|
|
181 }
|
|
182 }
|
|
183 }
|