more refactoring of input

git-svn-id: https://svn.code.sf.net/p/acme-crossass/code-0/trunk@386 4df02467-bbd4-4a76-a152-e7ce94205b78
This commit is contained in:
marcobaye 2024-07-31 12:36:47 +00:00
parent b11bdcded0
commit a53aa01a1e
5 changed files with 53 additions and 45 deletions

View File

@ -53,8 +53,9 @@ boolean check_ifdef_condition(void)
// parse a loop body (TODO - also use for macro body?) // parse a loop body (TODO - also use for macro body?)
static void parse_ram_block(struct block *block) static void parse_ram_block(struct block *block)
{ {
input_now->location.line_number = block->start; // set line number to loop start // set line number to loop start
input_now->src.ram_ptr = block->body; // set RAM read pointer to loop // set RAM read pointer to loop
inputchange_set_ram(block->start, block->body);
// parse block // parse block
parse_until_eob_or_eof(); parse_until_eob_or_eof();
if (GotByte != CHAR_EOB) if (GotByte != CHAR_EOB)
@ -118,20 +119,13 @@ static void iterating_for(struct for_loop *loop)
// back end function for "!for" pseudo opcode // back end function for "!for" pseudo opcode
void flow_forloop(struct for_loop *loop) void flow_forloop(struct for_loop *loop)
{ {
struct input loop_input, struct inputchange_buf icb;
*outer_input;
// switching input makes us lose GotByte. But we know it's '}' anyway! // remember input and set up new one:
// set up new input inputchange_new_ram(&icb);
loop_input = *input_now; // copy current input structure into new
loop_input.source = INPUTSRC_RAM; // set new byte source
// remember old input
outer_input = input_now;
// activate new input
// (not yet useable; pointer and line number are still missing)
input_now = &loop_input;
// fix line number (not for block, but in case symbol handling throws errors) // fix line number (not for block, but in case symbol handling throws errors)
input_now->location.line_number = loop->block.start; inputchange_set_ram(loop->block.start, NULL);
switch (loop->algorithm) { switch (loop->algorithm) {
case FORALGO_OLDCOUNT: case FORALGO_OLDCOUNT:
case FORALGO_NEWCOUNT: case FORALGO_NEWCOUNT:
@ -143,8 +137,9 @@ void flow_forloop(struct for_loop *loop)
default: default:
BUG("IllegalLoopAlgo", loop->algorithm); BUG("IllegalLoopAlgo", loop->algorithm);
} }
// restore previous input:
input_now = outer_input; // restore outer input
inputchange_back(&icb);
} }
@ -223,8 +218,8 @@ static boolean check_condition(struct condition *condition)
return TRUE; // non-existing conditions are always true return TRUE; // non-existing conditions are always true
// set up input for expression evaluation // set up input for expression evaluation
input_now->location.line_number = condition->line; inputchange_set_ram(condition->line, condition->body);
input_now->src.ram_ptr = condition->body;
GetByte(); // proceed with next char GetByte(); // proceed with next char
ALU_defined_int(&intresult); ALU_defined_int(&intresult);
if (GotByte) if (GotByte)
@ -236,17 +231,11 @@ static boolean check_condition(struct condition *condition)
// back end function for "!do" and "!while" pseudo opcodes // back end function for "!do" and "!while" pseudo opcodes
void flow_do_while(struct do_while *loop) void flow_do_while(struct do_while *loop)
{ {
struct input loop_input; struct inputchange_buf icb;
struct input *outer_input;
// remember input and prepare new one:
inputchange_new_ram(&icb);
// set up new input
loop_input = *input_now; // copy current input structure into new
loop_input.source = INPUTSRC_RAM; // set new byte source
// remember old input
outer_input = input_now;
// activate new input (not useable yet, as pointer and
// line number are not yet set up)
input_now = &loop_input;
for (;;) { for (;;) {
// check head condition // check head condition
if (!check_condition(&loop->head_cond)) if (!check_condition(&loop->head_cond))
@ -256,8 +245,10 @@ void flow_do_while(struct do_while *loop)
if (!check_condition(&loop->tail_cond)) if (!check_condition(&loop->tail_cond))
break; break;
} }
// restore previous input: // restore outer input
input_now = outer_input; inputchange_back(&icb);
// FIXME - the line above also restores GotByte, so this is no longer necessary,
// but check before removing:
GotByte = CHAR_EOS; // CAUTION! Very ugly kluge. GotByte = CHAR_EOS; // CAUTION! Very ugly kluge.
// But by switching input, we lost the outer input's GotByte. We know // But by switching input, we lost the outer input's GotByte. We know
// it was CHAR_EOS. We could just call GetByte() to get real input, but // it was CHAR_EOS. We could just call GetByte() to get real input, but

View File

@ -854,29 +854,41 @@ void inputchange_new_file(struct inputchange_buf *icb, FILE *fd, const char *ete
icb->new_input.src.fd = fd; icb->new_input.src.fd = fd;
// remember where outer input struct is // remember where outer input struct is
icb->outer_input = input_now; icb->outer_input = input_now;
// activate new input
icb->gb = GotByte; icb->gb = GotByte;
// activate new input
input_now = &icb->new_input; input_now = &icb->new_input;
} }
// save current input struct in buffer, then switch input to macro parameters // save current input struct in buffer, then switch to RAM
void inputchange_macro1_params(struct inputchange_buf *icb, struct location *def, char *params) void inputchange_new_ram(struct inputchange_buf *icb)
{ {
icb->new_input = *input_now; // copy current input structure into new icb->new_input = *input_now; // copy current input structure into new
icb->new_input.location = *def; icb->new_input.source = INPUTSRC_RAM; // set new byte source
icb->new_input.source = INPUTSRC_RAM; icb->new_input.src.ram_ptr = NULL; // force crash if used before setup is finished
icb->new_input.state = INPUTSTATE_NORMAL; // FIXME - fix others!
icb->new_input.src.ram_ptr = params;
// remember where outer input struct is // remember where outer input struct is
icb->outer_input = input_now; icb->outer_input = input_now;
// activate new input
icb->gb = GotByte; icb->gb = GotByte;
// activate new input (not useable yet, as pointer and line number are not yet set up)
input_now = &icb->new_input; input_now = &icb->new_input;
} }
// FIXME - merge these three functions into a single one (by always using a "location"):
// setup for reading from RAM (for parsing loop conditions etc.)
void inputchange_set_ram(int line_num, char *body)
{
input_now->location.line_number = line_num;
input_now->src.ram_ptr = body;
}
// switch input to macro parameters
void inputchange_macro1_params(struct location *def, char *params)
{
input_now->location = *def;
input_now->src.ram_ptr = params;
input_now->state = INPUTSTATE_NORMAL; // FIXME - fix others!
}
// switch from macro parameters to macro body // switch from macro parameters to macro body
void inputchange_macro2_body(char *macro_body) void inputchange_macro2_body(char *macro_body)
{ {
input_now->state = INPUTSTATE_NORMAL; // FIXME - fix others!
input_now->src.ram_ptr = macro_body; input_now->src.ram_ptr = macro_body;
input_now->state = INPUTSTATE_NORMAL; // FIXME - fix others!
} }
// restore input struct from buffer // restore input struct from buffer
void inputchange_back(const struct inputchange_buf *icb) void inputchange_back(const struct inputchange_buf *icb)

View File

@ -156,8 +156,12 @@ struct inputchange_buf {
}; };
// save current input struct in buffer, then switch input to new source code file // save current input struct in buffer, then switch input to new source code file
extern void inputchange_new_file(struct inputchange_buf *icb, FILE *fd, const char *eternal_plat_filename); extern void inputchange_new_file(struct inputchange_buf *icb, FILE *fd, const char *eternal_plat_filename);
// save current input struct in buffer, then switch input to macro parameters // save current input struct in buffer, then switch to RAM
extern void inputchange_macro1_params(struct inputchange_buf *icb, struct location *def, char *params); extern void inputchange_new_ram(struct inputchange_buf *icb);
// setup for reading from RAM (for parsing loop conditions etc.)
extern void inputchange_set_ram(int line_num, char *body);
// switch input to macro parameters
extern void inputchange_macro1_params(struct location *def, char *params);
// switch from macro parameters to macro body // switch from macro parameters to macro body
extern void inputchange_macro2_body(char *macro_body); extern void inputchange_macro2_body(char *macro_body);
// restore input struct from buffer // restore input struct from buffer

View File

@ -240,8 +240,9 @@ void macro_parse_call(void) // Now GotByte = first char of macro name
// make macro_node point to the macro struct // make macro_node point to the macro struct
actual_macro = macro_node->body; actual_macro = macro_node->body;
// set up new input // remember input and set up new one:
inputchange_macro1_params(&icb, &actual_macro->definition, actual_macro->parameter_list); inputchange_new_ram(&icb);
inputchange_macro1_params(&actual_macro->definition, actual_macro->parameter_list);
outer_msg_sum = pass.warning_count + pass.error_count; // remember for call stack decision outer_msg_sum = pass.warning_count + pass.error_count; // remember for call stack decision
@ -295,7 +296,7 @@ void macro_parse_call(void) // Now GotByte = first char of macro name
// restore previous section // restore previous section
section_now = outer_section; section_now = outer_section;
// restore previous input // restore outer input
inputchange_back(&icb); inputchange_back(&icb);
// if needed, dump call stack // if needed, dump call stack

View File

@ -9,7 +9,7 @@
#define RELEASE "0.97" // update before release FIXME #define RELEASE "0.97" // update before release FIXME
#define CODENAME "Zem" // update before release #define CODENAME "Zem" // update before release
#define CHANGE_DATE "22 Jul" // update before release FIXME #define CHANGE_DATE "23 Jul" // update before release FIXME
#define CHANGE_YEAR "2024" // update before release #define CHANGE_YEAR "2024" // update before release
//#define HOME_PAGE "http://home.pages.de/~mac_bacon/smorbrod/acme/" //#define HOME_PAGE "http://home.pages.de/~mac_bacon/smorbrod/acme/"
#define HOME_PAGE "http://sourceforge.net/p/acme-crossass/" // FIXME #define HOME_PAGE "http://sourceforge.net/p/acme-crossass/" // FIXME