| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
#include "flow.h" |
| 15 |
#include <string.h> |
| 16 |
#include "alu.h" |
| 17 |
#include "config.h" |
| 18 |
#include "dynabuf.h" |
| 19 |
#include "global.h" |
| 20 |
#include "input.h" |
| 21 |
#include "mnemo.h" |
| 22 |
#include "symbol.h" |
| 23 |
#include "tree.h" |
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
boolean check_ifdef_condition(void) |
| 31 |
{ |
| 32 |
scope_t scope; |
| 33 |
struct rwnode *node; |
| 34 |
struct symbol *symbol; |
| 35 |
|
| 36 |
|
| 37 |
if (input_read_scope_and_symbol_name(&scope)) |
| 38 |
return FALSE; |
| 39 |
|
| 40 |
|
| 41 |
tree_hard_scan(&node, symbols_forest, scope, FALSE); |
| 42 |
if (!node) |
| 43 |
return FALSE; |
| 44 |
|
| 45 |
symbol = (struct symbol *) node->body; |
| 46 |
symbol->has_been_read = TRUE; |
| 47 |
if (symbol->object.type == NULL) |
| 48 |
BUG("ObjectHasNullType", 0); |
| 49 |
return symbol->object.type->is_defined(&symbol->object); |
| 50 |
} |
| 51 |
|
| 52 |
|
| 53 |
|
| 54 |
static void parse_ram_block(struct block *block) |
| 55 |
{ |
| 56 |
|
| 57 |
|
| 58 |
inputchange_set_ram(block->line_number, block->body); |
| 59 |
|
| 60 |
parse_until_eob_or_eof(); |
| 61 |
if (GotByte != CHAR_EOB) |
| 62 |
BUG("IllegalBlockTerminator", GotByte); |
| 63 |
} |
| 64 |
|
| 65 |
|
| 66 |
|
| 67 |
static void counting_for(struct for_loop *loop) |
| 68 |
{ |
| 69 |
struct object loop_var; |
| 70 |
enum shortcut shortcut; |
| 71 |
|
| 72 |
|
| 73 |
loop_var.type = &type_number; |
| 74 |
loop_var.u.number.ntype = NUMTYPE_INT; |
| 75 |
loop_var.u.number.flags = 0; |
| 76 |
loop_var.u.number.val.intval = 0; |
| 77 |
loop_var.u.number.addr_refs = loop->u.counter.addr_refs; |
| 78 |
|
| 79 |
|
| 80 |
|
| 81 |
symbol_set_object(loop->symbol, &loop_var, POWER_CHANGE_VALUE); |
| 82 |
|
| 83 |
|
| 84 |
|
| 85 |
|
| 86 |
|
| 87 |
|
| 88 |
|
| 89 |
if (loop->u.counter.force_bit) |
| 90 |
symbol_set_force_bit(loop->symbol, loop->u.counter.force_bit); |
| 91 |
loop_var = loop->symbol->object; |
| 92 |
loop->symbol->has_been_read = TRUE; |
| 93 |
loop_var.u.number.val.intval = loop->u.counter.first; |
| 94 |
while (loop->iterations_left) { |
| 95 |
loop->symbol->object = loop_var; |
| 96 |
parse_ram_block(&loop->block); |
| 97 |
loop_var.u.number.val.intval += loop->u.counter.increment; |
| 98 |
loop->iterations_left--; |
| 99 |
|
| 100 |
|
| 101 |
shortcut = parser_get_shortcut(); |
| 102 |
if (shortcut == SHORTCUT_BREAK) { |
| 103 |
parser_set_shortcut(SHORTCUT_NONE); |
| 104 |
break; |
| 105 |
} else if (shortcut == SHORTCUT_CONT) { |
| 106 |
parser_set_shortcut(SHORTCUT_NONE); |
| 107 |
} |
| 108 |
} |
| 109 |
|
| 110 |
if (loop->algorithm == FORALGO_NEWCOUNT) |
| 111 |
loop->symbol->object = loop_var; |
| 112 |
} |
| 113 |
|
| 114 |
|
| 115 |
static void iterating_for(struct for_loop *loop) |
| 116 |
{ |
| 117 |
intval_t index = 0; |
| 118 |
struct object obj; |
| 119 |
enum shortcut shortcut; |
| 120 |
|
| 121 |
while (loop->iterations_left) { |
| 122 |
loop->u.iter.obj.type->at(&loop->u.iter.obj, &obj, index++); |
| 123 |
symbol_set_object(loop->symbol, &obj, POWER_CHANGE_VALUE | POWER_CHANGE_OBJTYPE); |
| 124 |
parse_ram_block(&loop->block); |
| 125 |
loop->iterations_left--; |
| 126 |
|
| 127 |
|
| 128 |
shortcut = parser_get_shortcut(); |
| 129 |
if (shortcut == SHORTCUT_BREAK) { |
| 130 |
parser_set_shortcut(SHORTCUT_NONE); |
| 131 |
break; |
| 132 |
} else if (shortcut == SHORTCUT_CONT) { |
| 133 |
parser_set_shortcut(SHORTCUT_NONE); |
| 134 |
} |
| 135 |
} |
| 136 |
} |
| 137 |
|
| 138 |
|
| 139 |
|
| 140 |
void flow_forloop(struct for_loop *loop) |
| 141 |
{ |
| 142 |
struct inputchange_buf icb; |
| 143 |
boolean break_cont_buf; |
| 144 |
|
| 145 |
|
| 146 |
inputchange_new_ram(&icb); |
| 147 |
|
| 148 |
inputchange_set_ram(loop->block.line_number, NULL); |
| 149 |
|
| 150 |
break_cont_buf = parser_allow_break_cont(TRUE); |
| 151 |
|
| 152 |
switch (loop->algorithm) { |
| 153 |
case FORALGO_OLDCOUNT: |
| 154 |
case FORALGO_NEWCOUNT: |
| 155 |
counting_for(loop); |
| 156 |
break; |
| 157 |
case FORALGO_ITERATE: |
| 158 |
iterating_for(loop); |
| 159 |
break; |
| 160 |
default: |
| 161 |
BUG("IllegalLoopAlgo", loop->algorithm); |
| 162 |
} |
| 163 |
|
| 164 |
|
| 165 |
parser_allow_break_cont(break_cont_buf); |
| 166 |
|
| 167 |
inputchange_back(&icb); |
| 168 |
} |
| 169 |
|
| 170 |
|
| 171 |
|
| 172 |
|
| 173 |
static void copy_condition(struct condition *condition, char terminator) |
| 174 |
{ |
| 175 |
input_read_statement(terminator); |
| 176 |
condition->block.body = dynabuf_get_copy(GlobalDynaBuf); |
| 177 |
} |
| 178 |
|
| 179 |
|
| 180 |
|
| 181 |
|
| 182 |
|
| 183 |
void flow_store_doloop_condition(struct condition *condition, char terminator) |
| 184 |
{ |
| 185 |
struct location loc; |
| 186 |
|
| 187 |
|
| 188 |
input_get_location(&loc); |
| 189 |
condition->block.line_number = loc.line_number; |
| 190 |
|
| 191 |
condition->invert = FALSE; |
| 192 |
condition->block.body = NULL; |
| 193 |
|
| 194 |
if (GotByte == terminator) |
| 195 |
return; |
| 196 |
|
| 197 |
|
| 198 |
if (parser_read_and_lower_keyword()) { |
| 199 |
if (strcmp(GlobalDynaBuf->buffer, "while") == 0) { |
| 200 |
|
| 201 |
} else if (strcmp(GlobalDynaBuf->buffer, "until") == 0) { |
| 202 |
condition->invert = TRUE; |
| 203 |
} else { |
| 204 |
throw_error("Expected WHILE or UNTIL keyword, or an empty loop condition."); |
| 205 |
return; |
| 206 |
} |
| 207 |
|
| 208 |
copy_condition(condition, terminator); |
| 209 |
} |
| 210 |
} |
| 211 |
|
| 212 |
|
| 213 |
|
| 214 |
|
| 215 |
|
| 216 |
void flow_store_while_condition(struct condition *condition) |
| 217 |
{ |
| 218 |
struct location loc; |
| 219 |
|
| 220 |
input_get_location(&loc); |
| 221 |
condition->block.line_number = loc.line_number; |
| 222 |
condition->invert = FALSE; |
| 223 |
copy_condition(condition, CHAR_SOB); |
| 224 |
} |
| 225 |
|
| 226 |
|
| 227 |
|
| 228 |
static boolean check_condition(struct condition *condition) |
| 229 |
{ |
| 230 |
struct number intresult; |
| 231 |
|
| 232 |
|
| 233 |
if (condition->block.body == NULL) |
| 234 |
return TRUE; |
| 235 |
|
| 236 |
|
| 237 |
inputchange_set_ram(condition->block.line_number, condition->block.body); |
| 238 |
|
| 239 |
GetByte(); |
| 240 |
ALU_defined_int(&intresult); |
| 241 |
if (GotByte) |
| 242 |
throw_serious_error("Unexpected char when evaluating loop condition."); |
| 243 |
return condition->invert ? !intresult.val.intval : !!intresult.val.intval; |
| 244 |
} |
| 245 |
|
| 246 |
|
| 247 |
|
| 248 |
void flow_do_while(struct do_while *loop) |
| 249 |
{ |
| 250 |
struct inputchange_buf icb; |
| 251 |
boolean break_cont_buf; |
| 252 |
enum shortcut shortcut; |
| 253 |
|
| 254 |
|
| 255 |
inputchange_new_ram(&icb); |
| 256 |
|
| 257 |
break_cont_buf = parser_allow_break_cont(TRUE); |
| 258 |
|
| 259 |
for (;;) { |
| 260 |
|
| 261 |
if (!check_condition(&loop->head_cond)) |
| 262 |
break; |
| 263 |
|
| 264 |
parse_ram_block(&loop->block); |
| 265 |
|
| 266 |
|
| 267 |
shortcut = parser_get_shortcut(); |
| 268 |
if (shortcut == SHORTCUT_BREAK) { |
| 269 |
parser_set_shortcut(SHORTCUT_NONE); |
| 270 |
break; |
| 271 |
} else if (shortcut == SHORTCUT_CONT) { |
| 272 |
parser_set_shortcut(SHORTCUT_NONE); |
| 273 |
} |
| 274 |
|
| 275 |
|
| 276 |
if (!check_condition(&loop->tail_cond)) |
| 277 |
break; |
| 278 |
} |
| 279 |
|
| 280 |
|
| 281 |
parser_allow_break_cont(break_cont_buf); |
| 282 |
|
| 283 |
inputchange_back(&icb); |
| 284 |
} |