mirror of
https://github.com/adamdunkels/ubasic.git
synced 2026-11-20 11:39:43 +00:00
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.
This commit is contained in:
parent
f9ea3c6e6b
commit
93bb242eb0
6 changed files with 85 additions and 2 deletions
2
Makefile
2
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
|
||||
|
|
|
|||
22
tests.c
22
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;
|
||||
}
|
||||
/*---------------------------------------------------------------------------*/
|
||||
|
|
|
|||
|
|
@ -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 == '<') {
|
||||
|
|
|
|||
|
|
@ -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,
|
||||
|
|
|
|||
53
ubasic.c
53
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;
|
||||
|
|
|
|||
3
ubasic.h
3
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);
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue