Implemented line indexing for faster jumps/looping.

This required adding the ability to to get and set the
tokenizer's current position pointer from outside of it with
tokenizer_pos and tokenizer_goto (c.f. tokenizer_init).
The index itself is stored as a linked list, which makes it ripe
for further optimization. It is accessed with index_* routines.
The interpreter resorts to running the old, slower jump procedure
when jumping forward (to a line that hasn't been index).

New goto test.
This commit is contained in:
Danyil Bohdan 2013-12-22 16:15:40 +02:00
parent 9c45ed4588
commit f9ea3c6e6b
4 changed files with 123 additions and 4 deletions

13
tests.c
View file

@ -38,11 +38,21 @@ static const char program_let[] =
"10 let a = 42\n\
20 end\n";
static const char program_goto[] =
"10 goto 50\n\
20 goto 40\n\
30 goto 60\n\
40 goto 30\n\
50 goto 20\n\
60 let c = 108\n\
70 end\n";
static const char program_loop[] =
"10 for i = 0 to 126\n\
20 for j = 0 to 126\n\
30 for k = 0 to 10\n\
40 let a = i * j * k\n\
rem 45 print a, i, j, k\n\
50 next k\n\
60 next j\n\
70 next i\n\
@ -90,6 +100,9 @@ main(void)
run(program_let);
assert(ubasic_get_variable(0) == 42);
run(program_goto);
assert(ubasic_get_variable(2) == 108);
run(program_loop);
assert(ubasic_get_variable(0) == (VARIABLE_TYPE)(126 * 126 * 10));

View file

@ -167,12 +167,19 @@ get_next_token(void)
}
/*---------------------------------------------------------------------------*/
void
tokenizer_init(const char *program)
tokenizer_goto(const char *program)
{
ptr = program;
current_token = get_next_token();
}
/*---------------------------------------------------------------------------*/
void
tokenizer_init(const char *program)
{
tokenizer_goto(program);
current_token = get_next_token();
}
/*---------------------------------------------------------------------------*/
int
tokenizer_token(void)
{
@ -254,3 +261,8 @@ tokenizer_variable_num(void)
return *ptr - 'a';
}
/*---------------------------------------------------------------------------*/
char const *
tokenizer_pos(void)
{
return ptr;
}

View file

@ -69,6 +69,7 @@ enum {
TOKENIZER_CR,
};
void tokenizer_goto(const char *program);
void tokenizer_init(const char *program);
void tokenizer_next(void);
int tokenizer_token(void);
@ -79,4 +80,6 @@ void tokenizer_string(char *dest, int len);
int tokenizer_finished(void);
void tokenizer_error_print(void);
char const *tokenizer_pos(void);
#endif /* __TOKENIZER_H__ */

View file

@ -59,6 +59,14 @@ struct for_state {
static struct for_state for_stack[MAX_FOR_STACK_DEPTH];
static int for_stack_ptr;
struct line_index {
int line_number;
char const *program_text_position;
struct line_index *next;
};
struct line_index *line_index_head = NULL;
struct line_index *line_index_current = NULL;
#define MAX_VARNUM 26
static VARIABLE_TYPE variables[MAX_VARNUM];
@ -67,12 +75,14 @@ static int ended;
static VARIABLE_TYPE expr(void);
static void line_statement(void);
static void statement(void);
static void index_free(void);
/*---------------------------------------------------------------------------*/
void
ubasic_init(const char *program)
{
program_ptr = program;
for_stack_ptr = gosub_stack_ptr = 0;
index_free();
tokenizer_init(program);
ended = 0;
}
@ -224,7 +234,74 @@ relation(void)
}
/*---------------------------------------------------------------------------*/
static void
jump_linenum(int linenum)
index_free(void) {
if (line_index_head != NULL) {
line_index_current = line_index_head;
do {
DEBUG_PRINTF("Freeing index for line %d.\n", line_index_current);
line_index_head = line_index_current;
line_index_current = line_index_current->next;
free(line_index_head);
} while (line_index_current != NULL);
line_index_head = NULL;
}
}
/*---------------------------------------------------------------------------*/
static char const*
index_find(int linenum) {
struct line_index *lidx;
lidx = line_index_head;
#if DEBUG
int step = 0;
#endif
while(lidx != NULL && lidx->line_number != linenum) {
lidx = lidx->next;
#if DEBUG
if (lidx != NULL) {
DEBUG_PRINTF("index_find: Step %3d. Found index for line %d: %d.\n",
step, lidx->line_number,
lidx->program_text_position);
}
step++;
#endif
}
if (lidx != NULL && lidx->line_number == linenum) {
DEBUG_PRINTF("index_find: Returning index for line %d.\n", linenum);
return lidx->program_text_position;
}
DEBUG_PRINTF("index_find: Returning NULL.\n", linenum);
return NULL;
}
/*---------------------------------------------------------------------------*/
static void
index_add(int linenum, char const* sourcepos) {
if (line_index_head != NULL && index_find(linenum)) {
return;
}
struct line_index *new_lidx;
new_lidx = malloc(sizeof(struct line_index));
new_lidx->line_number = linenum;
new_lidx->program_text_position = sourcepos;
new_lidx->next = NULL;
if (line_index_head != NULL) {
line_index_current->next = new_lidx;
line_index_current = line_index_current->next;
} else {
line_index_current = new_lidx;
line_index_head = line_index_current;
}
DEBUG_PRINTF("index_add: Adding index for line %d: %d.\n", linenum,
sourcepos);
}
/*---------------------------------------------------------------------------*/
static void
jump_linenum_slow(int linenum)
{
tokenizer_init(program_ptr);
while(tokenizer_num() != linenum) {
@ -237,7 +314,21 @@ jump_linenum(int linenum)
tokenizer_next();
}
} while(tokenizer_token() != TOKENIZER_NUMBER);
DEBUG_PRINTF("jump_linenum: Found line %d\n", tokenizer_num());
DEBUG_PRINTF("jump_linenum_slow: Found line %d\n", tokenizer_num());
}
}
/*---------------------------------------------------------------------------*/
static void
jump_linenum(int linenum)
{
char const* pos = index_find(linenum);
if (pos != NULL) {
DEBUG_PRINTF("jump_linenum: Going to line %d.\n", linenum);
tokenizer_goto(pos);
} else {
/* We'll try to find a yet-unindexed line to jump to. */
DEBUG_PRINTF("jump_linenum: Calling jump_linenum_slow.\n", linenum);
jump_linenum_slow(linenum);
}
}
/*---------------------------------------------------------------------------*/
@ -455,7 +546,7 @@ static void
line_statement(void)
{
DEBUG_PRINTF("----------- Line number %d ---------\n", tokenizer_num());
/* current_linenum = tokenizer_num();*/
index_add(tokenizer_num(), tokenizer_pos());
accept(TOKENIZER_NUMBER);
statement();
return;