From c865e86b8e8cb214d576e6c760b8c62efdf09bad Mon Sep 17 00:00:00 2001 From: Danyil Bohdan Date: Sat, 21 Dec 2013 22:43:47 +0200 Subject: [PATCH 1/7] Fixed assignment to 'a'. Added tests and VARIABLE_TYPE. The VARIABLE_TYPE defines what type uBASIC's variables are. --- Makefile | 1 + tests.c | 87 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++ ubasic.c | 59 +++++++++++++++++++------------------- ubasic.h | 2 ++ 4 files changed, 119 insertions(+), 30 deletions(-) create mode 100644 tests.c diff --git a/Makefile b/Makefile index 3b69d7f..8434adf 100644 --- a/Makefile +++ b/Makefile @@ -1 +1,2 @@ use-ubasic: use-ubasic.o ubasic.o tokenizer.o +tests: tests.o ubasic.o tokenizer.o diff --git a/tests.c b/tests.c new file mode 100644 index 0000000..0ce7a1e --- /dev/null +++ b/tests.c @@ -0,0 +1,87 @@ +/* + * Copyright (c) 2006, Adam Dunkels + * Copyright (c) 2013, Danyil Bohdan + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * 3. Neither the name of the author nor the names of its contributors + * may be used to endorse or promote products derived from this software + * without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS + * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. + * + */ + +#include +#include +#include +#include "ubasic.h" + +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\ +37 let a = i * j * k\n\ +40 next k\n\ +50 next j\n\ +60 next i\n\ +70 end\n"; + +static const char program_let[] = +"10 let a = 42\n\ +20 end\n"; + + +/*---------------------------------------------------------------------------*/ +void run(const char program[]) { + static int test_num = 0; + test_num++; + printf("Running test #%u... ", test_num); + fflush(stdout); + + clock_t start_t, end_t; + double delta_t; + + start_t = clock(); + + ubasic_init(program); + + do { + ubasic_run(); + } while(!ubasic_finished()); + + end_t = clock(); + delta_t = (double)(end_t - start_t) / CLOCKS_PER_SEC; + + printf("done. Run time: %.3f\n", delta_t); +} + +/*---------------------------------------------------------------------------*/ +int +main(void) +{ + run(program_let); + assert(ubasic_get_variable(0) == 42); + run(program_loop); + assert(ubasic_get_variable(0) == (VARIABLE_TYPE)(126 * 126 * 10)); + + return 0; +} +/*---------------------------------------------------------------------------*/ diff --git a/ubasic.c b/ubasic.c index a6e65c4..3f5b8b1 100644 --- a/ubasic.c +++ b/ubasic.c @@ -36,7 +36,6 @@ #define DEBUG_PRINTF(...) #endif - #include "ubasic.h" #include "tokenizer.h" @@ -61,7 +60,7 @@ static struct for_state for_stack[MAX_FOR_STACK_DEPTH]; static int for_stack_ptr; #define MAX_VARNUM 26 -static char variables[MAX_VARNUM]; +static VARIABLE_TYPE variables[MAX_VARNUM]; static int ended; @@ -83,7 +82,7 @@ accept(int token) { if(token != tokenizer_token()) { DEBUG_PRINTF("Token not what was expected (expected %d, got %d)\n", - token, tokenizer_token()); + token, tokenizer_token()); tokenizer_error_print(); exit(1); } @@ -135,8 +134,8 @@ term(void) op = tokenizer_token(); DEBUG_PRINTF("term: token %d\n", op); while(op == TOKENIZER_ASTR || - op == TOKENIZER_SLASH || - op == TOKENIZER_MOD) { + op == TOKENIZER_SLASH || + op == TOKENIZER_MOD) { tokenizer_next(); f2 = factor(); DEBUG_PRINTF("term: %d %d %d\n", f1, op, f2); @@ -162,14 +161,14 @@ expr(void) { int t1, t2; int op; - + t1 = term(); op = tokenizer_token(); DEBUG_PRINTF("expr: token %d\n", op); while(op == TOKENIZER_PLUS || - op == TOKENIZER_MINUS || - op == TOKENIZER_AND || - op == TOKENIZER_OR) { + op == TOKENIZER_MINUS || + op == TOKENIZER_AND || + op == TOKENIZER_OR) { tokenizer_next(); t2 = term(); DEBUG_PRINTF("expr: %d %d %d\n", t1, op, t2); @@ -198,13 +197,13 @@ relation(void) { int r1, r2; int op; - + r1 = expr(); op = tokenizer_token(); DEBUG_PRINTF("relation: token %d\n", op); while(op == TOKENIZER_LT || - op == TOKENIZER_GT || - op == TOKENIZER_EQ) { + op == TOKENIZER_GT || + op == TOKENIZER_EQ) { tokenizer_next(); r2 = expr(); DEBUG_PRINTF("relation: %d %d %d\n", r1, op, r2); @@ -231,11 +230,11 @@ jump_linenum(int linenum) while(tokenizer_num() != linenum) { do { do { - tokenizer_next(); + tokenizer_next(); } while(tokenizer_token() != TOKENIZER_CR && - tokenizer_token() != TOKENIZER_ENDOFINPUT); + tokenizer_token() != TOKENIZER_ENDOFINPUT); if(tokenizer_token() == TOKENIZER_CR) { - tokenizer_next(); + tokenizer_next(); } } while(tokenizer_token() != TOKENIZER_NUMBER); DEBUG_PRINTF("jump_linenum: Found line %d\n", tokenizer_num()); @@ -265,13 +264,13 @@ 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) { printf("%d", expr()); } else { break; } } while(tokenizer_token() != TOKENIZER_CR && - tokenizer_token() != TOKENIZER_ENDOFINPUT); + tokenizer_token() != TOKENIZER_ENDOFINPUT); printf("\n"); DEBUG_PRINTF("End of print\n"); tokenizer_next(); @@ -281,7 +280,7 @@ static void if_statement(void) { int r; - + accept(TOKENIZER_IF); r = relation(); @@ -293,8 +292,8 @@ if_statement(void) do { tokenizer_next(); } while(tokenizer_token() != TOKENIZER_ELSE && - tokenizer_token() != TOKENIZER_CR && - tokenizer_token() != TOKENIZER_ENDOFINPUT); + tokenizer_token() != TOKENIZER_CR && + tokenizer_token() != TOKENIZER_ENDOFINPUT); if(tokenizer_token() == TOKENIZER_ELSE) { tokenizer_next(); statement(); @@ -352,14 +351,14 @@ static void next_statement(void) { int var; - + accept(TOKENIZER_NEXT); var = tokenizer_variable_num(); accept(TOKENIZER_VARIABLE); if(for_stack_ptr > 0 && var == for_stack[for_stack_ptr - 1].for_variable) { ubasic_set_variable(var, - ubasic_get_variable(var) + 1); + ubasic_get_variable(var) + 1); if(ubasic_get_variable(var) <= for_stack[for_stack_ptr - 1].to) { jump_linenum(for_stack[for_stack_ptr - 1].line_after_for); } else { @@ -377,7 +376,7 @@ static void for_statement(void) { int for_variable, to; - + accept(TOKENIZER_FOR); for_variable = tokenizer_variable_num(); accept(TOKENIZER_VARIABLE); @@ -392,9 +391,9 @@ for_statement(void) for_stack[for_stack_ptr].for_variable = for_variable; for_stack[for_stack_ptr].to = to; DEBUG_PRINTF("for_statement: new for, var %d to %d\n", - for_stack[for_stack_ptr].for_variable, - for_stack[for_stack_ptr].to); - + for_stack[for_stack_ptr].for_variable, + for_stack[for_stack_ptr].to); + for_stack_ptr++; } else { DEBUG_PRINTF("for_statement: for stack depth exceeded\n"); @@ -412,9 +411,9 @@ static void statement(void) { int token; - + token = tokenizer_token(); - + switch(token) { case TOKENIZER_PRINT: print_statement(); @@ -482,7 +481,7 @@ ubasic_finished(void) void ubasic_set_variable(int varnum, int value) { - if(varnum > 0 && varnum <= MAX_VARNUM) { + if(varnum >= 0 && varnum <= MAX_VARNUM) { variables[varnum] = value; } } @@ -490,7 +489,7 @@ ubasic_set_variable(int varnum, int value) int ubasic_get_variable(int varnum) { - if(varnum > 0 && varnum <= MAX_VARNUM) { + if(varnum >= 0 && varnum <= MAX_VARNUM) { return variables[varnum]; } return 0; diff --git a/ubasic.h b/ubasic.h index 4ee4e78..eee6ea9 100644 --- a/ubasic.h +++ b/ubasic.h @@ -30,6 +30,8 @@ #ifndef __UBASIC_H__ #define __UBASIC_H__ +#define VARIABLE_TYPE char + void ubasic_init(const char *program); void ubasic_run(void); int ubasic_finished(void); From a519c98e6c7f5703391fb10444819bd720453863 Mon Sep 17 00:00:00 2001 From: Danyil Bohdan Date: Sat, 21 Dec 2013 22:45:54 +0200 Subject: [PATCH 2/7] Added file LICENSE. License text taken from http://dunkels.com/adam/ubasic/license.html. --- LICENSE | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) create mode 100644 LICENSE diff --git a/LICENSE b/LICENSE new file mode 100644 index 0000000..266a485 --- /dev/null +++ b/LICENSE @@ -0,0 +1,24 @@ +Copyright (c) 2006, Adam Dunkels +All rights reserved. +Redistribution and use in source and binary forms, with or without +modification, are permitted provided that the following conditions +are met: +1. Redistributions of source code must retain the above copyright + notice, this list of conditions and the following disclaimer. +2. Redistributions in binary form must reproduce the above copyright + notice, this list of conditions and the following disclaimer in the + documentation and/or other materials provided with the distribution. +3. The name of the author may not be used to endorse or promote + products derived from this software without specific prior + written permission. +THIS SOFTWARE IS PROVIDED BY THE AUTHOR `AS IS' AND ANY EXPRESS +OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED +WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE +ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY +DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL +DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE +GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS +INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, +WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING +NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS +SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. From ea7a52e61f00c19685218ab526c9cdde7732848f Mon Sep 17 00:00:00 2001 From: Danyil Bohdan Date: Sat, 21 Dec 2013 23:55:48 +0200 Subject: [PATCH 3/7] Moved VARIABLE_TYPE to vartype.h. VARIABLE_TYPE functions. The functions in ubasic.c that used int for variable values and the function expr() in tokenizer.c now use VARIABLE_TYPE. Also added a new test in tests.c. --- Makefile | 2 ++ tests.c | 25 +++++++++++++++++++------ tokenizer.c | 32 ++++++++++++++++---------------- tokenizer.h | 4 +++- ubasic.c | 8 ++++---- ubasic.h | 6 +++--- vartype.h | 36 ++++++++++++++++++++++++++++++++++++ 7 files changed, 83 insertions(+), 30 deletions(-) create mode 100644 vartype.h diff --git a/Makefile b/Makefile index 8434adf..fa1e85d 100644 --- a/Makefile +++ b/Makefile @@ -1,2 +1,4 @@ use-ubasic: use-ubasic.o ubasic.o tokenizer.o tests: tests.o ubasic.o tokenizer.o +clean: + rm *.o tests use-ubasic diff --git a/tests.c b/tests.c index 0ce7a1e..bc26af6 100644 --- a/tests.c +++ b/tests.c @@ -38,16 +38,25 @@ 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\ -37 let a = i * j * k\n\ -40 next k\n\ -50 next j\n\ -60 next i\n\ -70 end\n"; +40 let a = i * j * k\n\ +50 next k\n\ +60 next j\n\ +70 next i\n\ +80 end\n"; static const char program_let[] = "10 let a = 42\n\ 20 end\n"; +static const char program_fibs[] = +"20 let a = 1\n\ +40 let b = 1\n\ +60 for i = 0 to 8\n\ +80 let b = a + b\n\ +100 let a = b - a\n\ +140 next i\n\ +160 end\n"; + /*---------------------------------------------------------------------------*/ void run(const char program[]) { @@ -70,7 +79,7 @@ void run(const char program[]) { end_t = clock(); delta_t = (double)(end_t - start_t) / CLOCKS_PER_SEC; - printf("done. Run time: %.3f\n", delta_t); + printf("done. Run time: %.3f s\n", delta_t); } /*---------------------------------------------------------------------------*/ @@ -79,9 +88,13 @@ main(void) { run(program_let); assert(ubasic_get_variable(0) == 42); + run(program_loop); assert(ubasic_get_variable(0) == (VARIABLE_TYPE)(126 * 126 * 10)); + run(program_fibs); + assert(ubasic_get_variable(1) == 89); + return 0; } /*---------------------------------------------------------------------------*/ diff --git a/tokenizer.c b/tokenizer.c index effb190..02a5dd5 100644 --- a/tokenizer.c +++ b/tokenizer.c @@ -43,7 +43,7 @@ static char const *ptr, *nextptr; -#define MAX_NUMLEN 5 +#define MAX_NUMLEN 6 struct keyword_token { char *keyword; @@ -118,21 +118,21 @@ get_next_token(void) if(*ptr == 0) { return TOKENIZER_ENDOFINPUT; } - + if(isdigit(*ptr)) { for(i = 0; i < MAX_NUMLEN; ++i) { if(!isdigit(ptr[i])) { - if(i > 0) { - nextptr = ptr + i; - return TOKENIZER_NUMBER; - } else { - DEBUG_PRINTF("get_next_token: error due to too short number\n"); - return TOKENIZER_ERROR; - } + if(i > 0) { + nextptr = ptr + i; + return TOKENIZER_NUMBER; + } else { + DEBUG_PRINTF("get_next_token: error due to too short number\n"); + return TOKENIZER_ERROR; + } } if(!isdigit(ptr[i])) { - DEBUG_PRINTF("get_next_token: error due to malformed number\n"); - return TOKENIZER_ERROR; + DEBUG_PRINTF("get_next_token: error due to malformed number\n"); + return TOKENIZER_ERROR; } } DEBUG_PRINTF("get_next_token: error due to too long number\n"); @@ -150,8 +150,8 @@ get_next_token(void) } else { for(kt = keywords; kt->keyword != NULL; ++kt) { if(strncmp(ptr, kt->keyword, strlen(kt->keyword)) == 0) { - nextptr = ptr + strlen(kt->keyword); - return kt->token; + nextptr = ptr + strlen(kt->keyword); + return kt->token; } } } @@ -161,7 +161,7 @@ get_next_token(void) return TOKENIZER_VARIABLE; } - + return TOKENIZER_ERROR; } /*---------------------------------------------------------------------------*/ @@ -196,7 +196,7 @@ tokenizer_next(void) return; } /*---------------------------------------------------------------------------*/ -int +VARIABLE_TYPE tokenizer_num(void) { return atoi(ptr); @@ -207,7 +207,7 @@ tokenizer_string(char *dest, int len) { char *string_end; int string_len; - + if(tokenizer_token() != TOKENIZER_STRING) { return; } diff --git a/tokenizer.h b/tokenizer.h index f51c48a..b038b62 100644 --- a/tokenizer.h +++ b/tokenizer.h @@ -30,6 +30,8 @@ #ifndef __TOKENIZER_H__ #define __TOKENIZER_H__ +#include "vartype.h" + enum { TOKENIZER_ERROR, TOKENIZER_ENDOFINPUT, @@ -69,7 +71,7 @@ enum { void tokenizer_init(const char *program); void tokenizer_next(void); int tokenizer_token(void); -int tokenizer_num(void); +VARIABLE_TYPE tokenizer_num(void); int tokenizer_variable_num(void); void tokenizer_string(char *dest, int len); diff --git a/ubasic.c b/ubasic.c index 3f5b8b1..93e1e6f 100644 --- a/ubasic.c +++ b/ubasic.c @@ -64,7 +64,7 @@ static VARIABLE_TYPE variables[MAX_VARNUM]; static int ended; -static int expr(void); +static VARIABLE_TYPE expr(void); static void line_statement(void); static void statement(void); /*---------------------------------------------------------------------------*/ @@ -156,7 +156,7 @@ term(void) return f1; } /*---------------------------------------------------------------------------*/ -static int +static VARIABLE_TYPE expr(void) { int t1, t2; @@ -479,14 +479,14 @@ ubasic_finished(void) } /*---------------------------------------------------------------------------*/ void -ubasic_set_variable(int varnum, int value) +ubasic_set_variable(int varnum, VARIABLE_TYPE value) { if(varnum >= 0 && varnum <= MAX_VARNUM) { variables[varnum] = value; } } /*---------------------------------------------------------------------------*/ -int +VARIABLE_TYPE ubasic_get_variable(int varnum) { if(varnum >= 0 && varnum <= MAX_VARNUM) { diff --git a/ubasic.h b/ubasic.h index eee6ea9..d21bfcd 100644 --- a/ubasic.h +++ b/ubasic.h @@ -30,13 +30,13 @@ #ifndef __UBASIC_H__ #define __UBASIC_H__ -#define VARIABLE_TYPE char +#include "vartype.h" void ubasic_init(const char *program); void ubasic_run(void); int ubasic_finished(void); -int ubasic_get_variable(int varnum); -void ubasic_set_variable(int varum, int value); +VARIABLE_TYPE ubasic_get_variable(int varnum); +void ubasic_set_variable(int varum, VARIABLE_TYPE value); #endif /* __UBASIC_H__ */ diff --git a/vartype.h b/vartype.h new file mode 100644 index 0000000..bac8e81 --- /dev/null +++ b/vartype.h @@ -0,0 +1,36 @@ +/* + * Copyright (c) 2006, Adam Dunkels + * Copyright (c) 2013, Danyil Bohdan + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * 3. Neither the name of the author nor the names of its contributors + * may be used to endorse or promote products derived from this software + * without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS + * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. + * + */ +#ifndef __VARTYPE_H__ +#define __VARTYPE_H__ + +#define VARIABLE_TYPE char + +#endif /* __VARTYPE_H__ */ From 9c45ed4588c17ebf97e14fee9341ed7aaa243576 Mon Sep 17 00:00:00 2001 From: Danyil Bohdan Date: Sun, 22 Dec 2013 14:23:14 +0200 Subject: [PATCH 4/7] Implemented comments with REM. --- tests.c | 9 +++++---- tokenizer.c | 13 +++++++++++++ tokenizer.h | 1 + 3 files changed, 19 insertions(+), 4 deletions(-) diff --git a/tests.c b/tests.c index bc26af6..8425d8f 100644 --- a/tests.c +++ b/tests.c @@ -34,6 +34,10 @@ #include #include "ubasic.h" +static const char program_let[] = +"10 let a = 42\n\ +20 end\n"; + static const char program_loop[] = "10 for i = 0 to 126\n\ 20 for j = 0 to 126\n\ @@ -44,16 +48,13 @@ static const char program_loop[] = 70 next i\n\ 80 end\n"; -static const char program_let[] = -"10 let a = 42\n\ -20 end\n"; - static const char program_fibs[] = "20 let a = 1\n\ 40 let b = 1\n\ 60 for i = 0 to 8\n\ 80 let b = a + b\n\ 100 let a = b - a\n\ +rem 120 print a, b\n\ 140 next i\n\ 160 end\n"; diff --git a/tokenizer.c b/tokenizer.c index 02a5dd5..2c093cd 100644 --- a/tokenizer.c +++ b/tokenizer.c @@ -65,6 +65,7 @@ static const struct keyword_token keywords[] = { {"gosub", TOKENIZER_GOSUB}, {"return", TOKENIZER_RETURN}, {"call", TOKENIZER_CALL}, + {"rem", TOKENIZER_REM}, {"end", TOKENIZER_END}, {NULL, TOKENIZER_ERROR} }; @@ -188,10 +189,22 @@ tokenizer_next(void) DEBUG_PRINTF("tokenizer_next: %p\n", nextptr); ptr = nextptr; + while(*ptr == ' ') { ++ptr; } current_token = get_next_token(); + + if (current_token == TOKENIZER_REM) { + while(!(*nextptr == '\n' || tokenizer_finished())) { + ++nextptr; + } + if (*nextptr == '\n') { + ++nextptr; + } + tokenizer_next(); + } + DEBUG_PRINTF("tokenizer_next: '%s' %d\n", ptr, current_token); return; } diff --git a/tokenizer.h b/tokenizer.h index b038b62..75cfe2e 100644 --- a/tokenizer.h +++ b/tokenizer.h @@ -50,6 +50,7 @@ enum { TOKENIZER_GOSUB, TOKENIZER_RETURN, TOKENIZER_CALL, + TOKENIZER_REM, TOKENIZER_END, TOKENIZER_COMMA, TOKENIZER_SEMICOLON, From f9ea3c6e6bec1ef1a6e5961da7b1c4f0b28e928b Mon Sep 17 00:00:00 2001 From: Danyil Bohdan Date: Sun, 22 Dec 2013 16:15:40 +0200 Subject: [PATCH 5/7] 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. --- tests.c | 13 +++++++ tokenizer.c | 14 +++++++- tokenizer.h | 3 ++ ubasic.c | 97 +++++++++++++++++++++++++++++++++++++++++++++++++++-- 4 files changed, 123 insertions(+), 4 deletions(-) diff --git a/tests.c b/tests.c index 8425d8f..429173e 100644 --- a/tests.c +++ b/tests.c @@ -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)); diff --git a/tokenizer.c b/tokenizer.c index 2c093cd..50fc6b9 100644 --- a/tokenizer.c +++ b/tokenizer.c @@ -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; +} diff --git a/tokenizer.h b/tokenizer.h index 75cfe2e..5248901 100644 --- a/tokenizer.h +++ b/tokenizer.h @@ -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__ */ diff --git a/ubasic.c b/ubasic.c index 93e1e6f..8e9c59f 100644 --- a/ubasic.c +++ b/ubasic.c @@ -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; From 93bb242eb096dea8f47d050b5e0b20eaf28ff788 Mon Sep 17 00:00:00 2001 From: Danyil Bohdan Date: Mon, 23 Dec 2013 18:46:25 +0200 Subject: [PATCH 6/7] New instructions PEEK and POKE to call outside routines. To use PEEK and POKE initialize uBASIC from your program with ubasic_init_peek_poke(program, &peek, &poke) where "peek" is a function peek(VARIABLE_TYPE addr) that returns VARIABLE_TYPE and "poke" is a procedure void poke(VARIABLE_TYPE addr, VARIABLE_TYPE value). Use "PEEK addr, var" to set the value of the variable var to the result of calling peek(addr). Use "POKE addr, value" to call poke(addr, value); The argument name "addr" is arbitrary; it doesn't mean you should necessarily use it to pass your routines a memory address. --- Makefile | 2 +- tests.c | 22 +++++++++++++++++++++- tokenizer.c | 4 ++++ tokenizer.h | 3 +++ ubasic.c | 53 +++++++++++++++++++++++++++++++++++++++++++++++++++++ ubasic.h | 3 +++ 6 files changed, 85 insertions(+), 2 deletions(-) diff --git a/Makefile b/Makefile index fa1e85d..50eedc9 100644 --- a/Makefile +++ b/Makefile @@ -1,4 +1,4 @@ -use-ubasic: use-ubasic.o ubasic.o tokenizer.o tests: tests.o ubasic.o tokenizer.o +use-ubasic: use-ubasic.o ubasic.o tokenizer.o clean: rm *.o tests use-ubasic diff --git a/tests.c b/tests.c index 429173e..96f60b8 100644 --- a/tests.c +++ b/tests.c @@ -68,6 +68,22 @@ rem 120 print a, b\n\ 140 next i\n\ 160 end\n"; +static const char program_peek_poke[] = +"10 peek 100 + 20 + 3, a\n\ +20 peek 123, z\n\ +30 poke 100 - 1, 99\n\ +40 poke 0, 0\n\ +50 end\n"; + +/*---------------------------------------------------------------------------*/ +VARIABLE_TYPE peek(VARIABLE_TYPE arg) { + return arg; +} + +/*---------------------------------------------------------------------------*/ +void poke(VARIABLE_TYPE arg, VARIABLE_TYPE value) { + assert(arg == value); +} /*---------------------------------------------------------------------------*/ void run(const char program[]) { @@ -81,7 +97,7 @@ void run(const char program[]) { start_t = clock(); - ubasic_init(program); + ubasic_init_peek_poke(program, &peek, &poke); do { ubasic_run(); @@ -109,6 +125,10 @@ main(void) run(program_fibs); assert(ubasic_get_variable(1) == 89); + run(program_peek_poke); + assert(ubasic_get_variable(0) == 123); + assert(ubasic_get_variable(25) == 123); + return 0; } /*---------------------------------------------------------------------------*/ diff --git a/tokenizer.c b/tokenizer.c index 50fc6b9..34d1906 100644 --- a/tokenizer.c +++ b/tokenizer.c @@ -66,6 +66,8 @@ static const struct keyword_token keywords[] = { {"return", TOKENIZER_RETURN}, {"call", TOKENIZER_CALL}, {"rem", TOKENIZER_REM}, + {"peek", TOKENIZER_PEEK}, + {"poke", TOKENIZER_POKE}, {"end", TOKENIZER_END}, {NULL, TOKENIZER_ERROR} }; @@ -96,6 +98,8 @@ singlechar(void) return TOKENIZER_MOD; } else if(*ptr == '(') { return TOKENIZER_LEFTPAREN; + } else if(*ptr == '#') { + return TOKENIZER_HASH; } else if(*ptr == ')') { return TOKENIZER_RIGHTPAREN; } else if(*ptr == '<') { diff --git a/tokenizer.h b/tokenizer.h index 5248901..d520ae8 100644 --- a/tokenizer.h +++ b/tokenizer.h @@ -51,6 +51,8 @@ enum { TOKENIZER_RETURN, TOKENIZER_CALL, TOKENIZER_REM, + TOKENIZER_PEEK, + TOKENIZER_POKE, TOKENIZER_END, TOKENIZER_COMMA, TOKENIZER_SEMICOLON, @@ -61,6 +63,7 @@ enum { TOKENIZER_ASTR, TOKENIZER_SLASH, TOKENIZER_MOD, + TOKENIZER_HASH, TOKENIZER_LEFTPAREN, TOKENIZER_RIGHTPAREN, TOKENIZER_LT, diff --git a/ubasic.c b/ubasic.c index 8e9c59f..6728885 100644 --- a/ubasic.c +++ b/ubasic.c @@ -76,6 +76,10 @@ 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; + /*---------------------------------------------------------------------------*/ void ubasic_init(const char *program) @@ -87,6 +91,18 @@ ubasic_init(const char *program) ended = 0; } /*---------------------------------------------------------------------------*/ +void +ubasic_init_peek_poke(const char *program, peek_func peek, poke_func poke) +{ + program_ptr = program; + for_stack_ptr = gosub_stack_ptr = 0; + index_free(); + peek_function = peek; + poke_function = poke; + tokenizer_init(program); + ended = 0; +} +/*---------------------------------------------------------------------------*/ static void accept(int token) { @@ -492,6 +508,37 @@ for_statement(void) } /*---------------------------------------------------------------------------*/ static void +peek_statement(void) +{ + VARIABLE_TYPE peek_addr; + int var; + + accept(TOKENIZER_PEEK); + peek_addr = expr(); + accept(TOKENIZER_COMMA); + var = tokenizer_variable_num(); + accept(TOKENIZER_VARIABLE); + accept(TOKENIZER_CR); + + ubasic_set_variable(var, peek_function(peek_addr)); +} +/*---------------------------------------------------------------------------*/ +static void +poke_statement(void) +{ + VARIABLE_TYPE poke_addr; + VARIABLE_TYPE value; + + accept(TOKENIZER_POKE); + poke_addr = expr(); + accept(TOKENIZER_COMMA); + value = expr(); + accept(TOKENIZER_CR); + + poke_function(poke_addr, value); +} +/*---------------------------------------------------------------------------*/ +static void end_statement(void) { accept(TOKENIZER_END); @@ -524,6 +571,12 @@ statement(void) case TOKENIZER_FOR: for_statement(); break; + case TOKENIZER_PEEK: + peek_statement(); + break; + case TOKENIZER_POKE: + poke_statement(); + break; case TOKENIZER_NEXT: next_statement(); break; diff --git a/ubasic.h b/ubasic.h index d21bfcd..43399c8 100644 --- a/ubasic.h +++ b/ubasic.h @@ -32,6 +32,9 @@ #include "vartype.h" +typedef VARIABLE_TYPE (*peek_func)(VARIABLE_TYPE); +typedef void (*poke_func)(VARIABLE_TYPE, VARIABLE_TYPE); + void ubasic_init(const char *program); void ubasic_run(void); int ubasic_finished(void); From cdbf58941d02adecad7e0df8f169b11ab5aaced5 Mon Sep 17 00:00:00 2001 From: Adam Dunkels Date: Wed, 16 Jul 2014 14:12:57 +0200 Subject: [PATCH 7/7] Slight code style fix --- tokenizer.c | 4 ++-- ubasic.c | 12 ++++++------ 2 files changed, 8 insertions(+), 8 deletions(-) diff --git a/tokenizer.c b/tokenizer.c index 34d1906..3321079 100644 --- a/tokenizer.c +++ b/tokenizer.c @@ -206,11 +206,11 @@ tokenizer_next(void) } current_token = get_next_token(); - if (current_token == TOKENIZER_REM) { + if(current_token == TOKENIZER_REM) { while(!(*nextptr == '\n' || tokenizer_finished())) { ++nextptr; } - if (*nextptr == '\n') { + if(*nextptr == '\n') { ++nextptr; } tokenizer_next(); diff --git a/ubasic.c b/ubasic.c index 6728885..36eade4 100644 --- a/ubasic.c +++ b/ubasic.c @@ -251,7 +251,7 @@ relation(void) /*---------------------------------------------------------------------------*/ static void index_free(void) { - if (line_index_head != NULL) { + if(line_index_head != NULL) { line_index_current = line_index_head; do { DEBUG_PRINTF("Freeing index for line %d.\n", line_index_current); @@ -276,7 +276,7 @@ index_find(int linenum) { lidx = lidx->next; #if DEBUG - if (lidx != NULL) { + if(lidx != NULL) { DEBUG_PRINTF("index_find: Step %3d. Found index for line %d: %d.\n", step, lidx->line_number, lidx->program_text_position); @@ -284,7 +284,7 @@ index_find(int linenum) { step++; #endif } - if (lidx != NULL && lidx->line_number == linenum) { + if(lidx != NULL && lidx->line_number == linenum) { DEBUG_PRINTF("index_find: Returning index for line %d.\n", linenum); return lidx->program_text_position; } @@ -294,7 +294,7 @@ index_find(int linenum) { /*---------------------------------------------------------------------------*/ static void index_add(int linenum, char const* sourcepos) { - if (line_index_head != NULL && index_find(linenum)) { + if(line_index_head != NULL && index_find(linenum)) { return; } @@ -305,7 +305,7 @@ index_add(int linenum, char const* sourcepos) { new_lidx->program_text_position = sourcepos; new_lidx->next = NULL; - if (line_index_head != NULL) { + if(line_index_head != NULL) { line_index_current->next = new_lidx; line_index_current = line_index_current->next; } else { @@ -338,7 +338,7 @@ static void jump_linenum(int linenum) { char const* pos = index_find(linenum); - if (pos != NULL) { + if(pos != NULL) { DEBUG_PRINTF("jump_linenum: Going to line %d.\n", linenum); tokenizer_goto(pos); } else {