diff --git a/tests.c b/tests.c index 96f60b8..bc5f736 100644 --- a/tests.c +++ b/tests.c @@ -76,6 +76,20 @@ static const char program_peek_poke[] = 50 end\n"; /*---------------------------------------------------------------------------*/ +static const char program_div_zero[] = +"10 let a = 1\n\ +20 let b = a / 0\n\ +30 let a = 2\n\ +40 end\n"; + +static const char program_bad_goto[] = +"10 goto 999\n\ +20 end\n"; + +static const char program_unterminated[] = +"10 print \"oops\n\ +20 end\n"; + VARIABLE_TYPE peek(VARIABLE_TYPE arg) { return arg; } @@ -128,6 +142,17 @@ main(void) run(program_peek_poke); assert(ubasic_get_variable(0) == 123); assert(ubasic_get_variable(25) == 123); + assert(ubasic_error() == UBASIC_OK); + + run(program_div_zero); + assert(ubasic_error() == UBASIC_ERR_DIV_ZERO); + assert(ubasic_get_variable(0) == 1); /* line 30 never ran */ + + run(program_bad_goto); + assert(ubasic_error() == UBASIC_ERR_NO_LINE); + + run(program_unterminated); + assert(ubasic_error() == UBASIC_ERR_SYNTAX); return 0; } diff --git a/tokenizer.c b/tokenizer.c index 3321079..f175404 100644 --- a/tokenizer.c +++ b/tokenizer.c @@ -149,7 +149,10 @@ get_next_token(void) nextptr = ptr; do { ++nextptr; - } while(*nextptr != '"'); + } while(*nextptr != '"' && *nextptr != 0); + if(*nextptr == 0) { + return TOKENIZER_ERROR; /* unterminated string */ + } ++nextptr; return TOKENIZER_STRING; } else { @@ -240,8 +243,8 @@ tokenizer_string(char *dest, int len) return; } string_len = string_end - ptr - 1; - if(len < string_len) { - string_len = len; + if(len - 1 < string_len) { + string_len = len - 1; } memcpy(dest, ptr + 1, string_len); dest[string_len] = 0; diff --git a/ubasic.c b/ubasic.c index 36eade4..f7eb447 100644 --- a/ubasic.c +++ b/ubasic.c @@ -40,7 +40,9 @@ #include "tokenizer.h" #include /* printf() */ -#include /* exit() */ +#include /* malloc(), free() */ +#include /* memset() */ +#include static char const *program_ptr; #define MAX_STRINGLEN 40 @@ -72,23 +74,40 @@ static VARIABLE_TYPE variables[MAX_VARNUM]; static int ended; +static jmp_buf error_jmp; +static int error_code; + +static const char *error_messages[] = { + "no error", + "syntax error", + "division by zero", + "stack overflow or underflow", + "undefined line number", + "PEEK/POKE callback not set" +}; + +/* Stop the program with an error; unwinds back into ubasic_run(). */ +static void +fail(int code) +{ + error_code = code; + ended = 1; + longjmp(error_jmp, 1); +} + static VARIABLE_TYPE expr(void); static void line_statement(void); static void statement(void); static void index_free(void); -peek_func peek_function = NULL; -poke_func poke_function = NULL; +static peek_func peek_function = NULL; +static poke_func poke_function = NULL; /*---------------------------------------------------------------------------*/ void ubasic_init(const char *program) { - program_ptr = program; - for_stack_ptr = gosub_stack_ptr = 0; - index_free(); - tokenizer_init(program); - ended = 0; + ubasic_init_peek_poke(program, NULL, NULL); } /*---------------------------------------------------------------------------*/ void @@ -99,6 +118,8 @@ ubasic_init_peek_poke(const char *program, peek_func peek, poke_func poke) index_free(); peek_function = peek; poke_function = poke; + memset(variables, 0, sizeof(variables)); + error_code = UBASIC_OK; tokenizer_init(program); ended = 0; } @@ -110,7 +131,7 @@ accept(int token) DEBUG_PRINTF("Token not what was expected (expected %d, got %d)\n", token, tokenizer_token()); tokenizer_error_print(); - exit(1); + fail(UBASIC_ERR_SYNTAX); } DEBUG_PRINTF("Expected %d, got it\n", token); tokenizer_next(); @@ -170,9 +191,11 @@ term(void) f1 = f1 * f2; break; case TOKENIZER_SLASH: + if(f2 == 0) fail(UBASIC_ERR_DIV_ZERO); f1 = f1 / f2; break; case TOKENIZER_MOD: + if(f2 == 0) fail(UBASIC_ERR_DIV_ZERO); f1 = f1 % f2; break; } @@ -329,6 +352,9 @@ jump_linenum_slow(int linenum) if(tokenizer_token() == TOKENIZER_CR) { tokenizer_next(); } + if(tokenizer_token() == TOKENIZER_ENDOFINPUT) { + fail(UBASIC_ERR_NO_LINE); + } } while(tokenizer_token() != TOKENIZER_NUMBER); DEBUG_PRINTF("jump_linenum_slow: Found line %d\n", tokenizer_num()); } @@ -371,13 +397,18 @@ print_statement(void) } else if(tokenizer_token() == TOKENIZER_SEMICOLON) { tokenizer_next(); } else if(tokenizer_token() == TOKENIZER_VARIABLE || - tokenizer_token() == TOKENIZER_NUMBER) { + tokenizer_token() == TOKENIZER_NUMBER || + tokenizer_token() == TOKENIZER_LEFTPAREN) { printf("%d", expr()); } else { break; } } while(tokenizer_token() != TOKENIZER_CR && tokenizer_token() != TOKENIZER_ENDOFINPUT); + if(tokenizer_token() != TOKENIZER_CR && + tokenizer_token() != TOKENIZER_ENDOFINPUT) { + fail(UBASIC_ERR_SYNTAX); + } printf("\n"); DEBUG_PRINTF("End of print\n"); tokenizer_next(); @@ -439,6 +470,7 @@ gosub_statement(void) jump_linenum(linenum); } else { DEBUG_PRINTF("gosub_statement: gosub stack exhausted\n"); + fail(UBASIC_ERR_STACK); } } /*---------------------------------------------------------------------------*/ @@ -451,6 +483,7 @@ return_statement(void) jump_linenum(gosub_stack[gosub_stack_ptr]); } else { DEBUG_PRINTF("return_statement: non-matching return\n"); + fail(UBASIC_ERR_STACK); } } /*---------------------------------------------------------------------------*/ @@ -504,6 +537,7 @@ for_statement(void) for_stack_ptr++; } else { DEBUG_PRINTF("for_statement: for stack depth exceeded\n"); + fail(UBASIC_ERR_STACK); } } /*---------------------------------------------------------------------------*/ @@ -520,6 +554,7 @@ peek_statement(void) accept(TOKENIZER_VARIABLE); accept(TOKENIZER_CR); + if(peek_function == NULL) fail(UBASIC_ERR_NO_CALLBACK); ubasic_set_variable(var, peek_function(peek_addr)); } /*---------------------------------------------------------------------------*/ @@ -535,6 +570,7 @@ poke_statement(void) value = expr(); accept(TOKENIZER_CR); + if(poke_function == NULL) fail(UBASIC_ERR_NO_CALLBACK); poke_function(poke_addr, value); } /*---------------------------------------------------------------------------*/ @@ -591,7 +627,7 @@ statement(void) break; default: DEBUG_PRINTF("ubasic.c: statement(): not implemented %d\n", token); - exit(1); + fail(UBASIC_ERR_SYNTAX); } } /*---------------------------------------------------------------------------*/ @@ -608,11 +644,14 @@ line_statement(void) void ubasic_run(void) { - if(tokenizer_finished()) { + if(ended || tokenizer_finished()) { DEBUG_PRINTF("uBASIC program finished\n"); return; } + if(setjmp(error_jmp) != 0) { + return; /* fail() was called */ + } line_statement(); } /*---------------------------------------------------------------------------*/ @@ -625,7 +664,7 @@ ubasic_finished(void) void ubasic_set_variable(int varnum, VARIABLE_TYPE value) { - if(varnum >= 0 && varnum <= MAX_VARNUM) { + if(varnum >= 0 && varnum < MAX_VARNUM) { variables[varnum] = value; } } @@ -633,9 +672,22 @@ ubasic_set_variable(int varnum, VARIABLE_TYPE value) VARIABLE_TYPE ubasic_get_variable(int varnum) { - if(varnum >= 0 && varnum <= MAX_VARNUM) { + if(varnum >= 0 && varnum < MAX_VARNUM) { return variables[varnum]; } return 0; } /*---------------------------------------------------------------------------*/ +/*---------------------------------------------------------------------------*/ +int +ubasic_error(void) +{ + return error_code; +} +/*---------------------------------------------------------------------------*/ +const char * +ubasic_error_message(void) +{ + return error_messages[error_code]; +} +/*---------------------------------------------------------------------------*/ diff --git a/ubasic.h b/ubasic.h index 43399c8..a2b3e9c 100644 --- a/ubasic.h +++ b/ubasic.h @@ -35,10 +35,23 @@ typedef VARIABLE_TYPE (*peek_func)(VARIABLE_TYPE); typedef void (*poke_func)(VARIABLE_TYPE, VARIABLE_TYPE); +enum { + UBASIC_OK = 0, + UBASIC_ERR_SYNTAX, + UBASIC_ERR_DIV_ZERO, + UBASIC_ERR_STACK, /* GOSUB/FOR overflow or RETURN without GOSUB */ + UBASIC_ERR_NO_LINE, /* GOTO/GOSUB to a nonexistent line */ + UBASIC_ERR_NO_CALLBACK /* PEEK/POKE without a registered callback */ +}; + void ubasic_init(const char *program); +void ubasic_init_peek_poke(const char *program, peek_func peek, poke_func poke); void ubasic_run(void); int ubasic_finished(void); +int ubasic_error(void); +const char *ubasic_error_message(void); + VARIABLE_TYPE ubasic_get_variable(int varnum); void ubasic_set_variable(int varum, VARIABLE_TYPE value); diff --git a/vartype.h b/vartype.h index bac8e81..afcec31 100644 --- a/vartype.h +++ b/vartype.h @@ -31,6 +31,9 @@ #ifndef __VARTYPE_H__ #define __VARTYPE_H__ -#define VARIABLE_TYPE char +#include + +/* Type of all BASIC values. Was "char" (-128..127). */ +#define VARIABLE_TYPE int32_t #endif /* __VARTYPE_H__ */