forked from mirrors/ubasic
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
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;
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue