From 93bb242eb096dea8f47d050b5e0b20eaf28ff788 Mon Sep 17 00:00:00 2001 From: Danyil Bohdan Date: Mon, 23 Dec 2013 18:46:25 +0200 Subject: [PATCH] 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);