Skip to content

Commit f2a6660

Browse files
some more refactoring
1 parent b220635 commit f2a6660

File tree

3 files changed

+50
-43
lines changed

3 files changed

+50
-43
lines changed

src/main.c

Lines changed: 8 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,8 @@
2222
#include "statistics.h"
2323
#include "parse.h"
2424

25+
#include <getopt.h>
26+
2527
/* simple version info for --version command line option */
2628
static void print_version()
2729
{
@@ -162,13 +164,13 @@ int main(int argc, char *argv[])
162164
#if YYDEBUG
163165
yydebug = 1;
164166
#endif
165-
166-
// call setup to create the state needed to parse the file
167-
setup();
168167

169168
// parse the options and make sure we got a filename somewhere
170169
parse_options(argc, argv);
171170

171+
// call the interpreter's setup to create the state needed to parse the file
172+
interpreter_setup();
173+
172174
// open the file...
173175
yyin = fopen(source_file, "r");
174176
// and see if it exists
@@ -184,40 +186,8 @@ int main(int argc, char *argv[])
184186
// if we were able to open the file, parse it
185187
yyparse();
186188

187-
// run all the lines together into a single continuous list
188-
// by pointing the ->next for each line to the head of the next
189-
// non-empty line. that way we don't have to search through the line
190-
// array for the next non-null entry during the run loop, we just
191-
// keep stepping through the ->next until we fall off the end
192-
{
193-
// look for the first entry in the lines array with a non-empty statement list
194-
int first_line = 0;
195-
while ((first_line < MAXLINE - 1) && (interpreter_state.lines[first_line] == NULL))
196-
first_line++;
197-
198-
// that statement is going to be the head of the list when we're done
199-
GList *first_statement = interpreter_state.lines[first_line];
200-
201-
// now find the next non-null line and concat it to the first one, and repeat
202-
for (int i = first_line + 1; (i < MAXLINE); i++) {
203-
if (interpreter_state.lines[i])
204-
first_statement = g_list_concat(first_statement, interpreter_state.lines[i]);
205-
}
206-
207-
// and set the resulting list back into the first line
208-
// NOTE: do we need to do this? isn't this already there?
209-
interpreter_state.lines[first_line] = first_statement;
210-
// and keep track of this for posterity
211-
interpreter_state.first_line = first_line;
212-
213-
// a program runs from the first line, so...
214-
interpreter_state.current_statement = first_statement; // the first statement
215-
interpreter_state.current_data_statement = first_statement; // the DATA can point anywhere
216-
interpreter_state.current_data_element = NULL; // the element within the DATA is nothing
217-
}
218-
219-
// the cursor starts in col 0
220-
interpreter_state.cursor_column = 0;
189+
// prepare the code
190+
interpreter_post_parse();
221191

222192
// seed the random with the provided number or randomize it
223193
if (random_seed > -1)
@@ -227,7 +197,7 @@ int main(int argc, char *argv[])
227197

228198
// and go!
229199
if (run_program)
230-
run();
200+
interpreter_run();
231201

232202
// we're done, print/write desired stats
233203
if (print_stats || write_stats)

src/retrobasic.c

Lines changed: 37 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,6 @@
2424
#include "retrobasic.h"
2525
#include "parse.h"
2626

27-
#include <getopt.h>
2827
#include <sys/time.h>
2928

3029
/* here's the actual definition of the interpreter state which is extern in the header */
@@ -1585,15 +1584,50 @@ static void delete_lines() {
15851584
}
15861585

15871586
/* set up empty trees to store variables and user functions as we find them */
1588-
void setup()
1587+
void interpreter_setup()
15891588
{
15901589
interpreter_state.variable_values = g_tree_new(symbol_compare);
15911590
interpreter_state.functions = g_tree_new(symbol_compare);
15921591
}
1592+
// run all the lines together into a single continuous list
1593+
// by pointing the ->next for each line to the head of the next
1594+
// non-empty line. that way we don't have to search through the line
1595+
// array for the next non-null entry during the run loop, we just
1596+
// keep stepping through the ->next until we fall off the end
1597+
void interpreter_post_parse(void)
1598+
{
1599+
// look for the first entry in the lines array with a non-empty statement list
1600+
int first_line = 0;
1601+
while ((first_line < MAXLINE - 1) && (interpreter_state.lines[first_line] == NULL))
1602+
first_line++;
1603+
1604+
// that statement is going to be the head of the list when we're done
1605+
GList *first_statement = interpreter_state.lines[first_line];
1606+
1607+
// now find the next non-null line and concat it to the first one, and repeat
1608+
for (int i = first_line + 1; (i < MAXLINE); i++) {
1609+
if (interpreter_state.lines[i])
1610+
first_statement = g_list_concat(first_statement, interpreter_state.lines[i]);
1611+
}
1612+
1613+
// and set the resulting list back into the first line
1614+
// NOTE: do we need to do this? isn't this already there?
1615+
interpreter_state.lines[first_line] = first_statement;
1616+
// and keep track of this for posterity
1617+
interpreter_state.first_line = first_line;
1618+
1619+
// a program runs from the first line, so...
1620+
interpreter_state.current_statement = first_statement; // the first statement
1621+
interpreter_state.current_data_statement = first_statement; // the DATA can point anywhere
1622+
interpreter_state.current_data_element = NULL; // the element within the DATA is nothing
1623+
}
15931624

15941625
/* the main loop for the program */
1595-
void run(void)
1626+
void interpreter_run(void)
15961627
{
1628+
// the cursor starts in col 0
1629+
interpreter_state.cursor_column = 0;
1630+
15971631
// start the clock and mark us as running
15981632
start_ticks = clock();
15991633
gettimeofday(&start_time, NULL);

src/retrobasic.h

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -199,9 +199,12 @@ extern interpreterstate_t interpreter_state;
199199
void insert_variable(variable_t *variable);
200200

201201
/* called by main to set up the interpreter state */
202-
void setup(void);
202+
void interpreter_setup(void);
203+
204+
/* perform post-read setup */
205+
void interpreter_post_parse(void);
203206

204207
/* the interpreter entry point */
205-
void run(void);
208+
void interpreter_run(void);
206209

207210
#endif

0 commit comments

Comments
 (0)