mirror of
https://github.com/adamdunkels/ubasic.git
synced 2026-11-20 11:39:43 +00:00
Merge pull request #1 from dbohdan/master
Assignment bug fix, tests, loop speed improvement, REM and a couple other things.
This commit is contained in:
commit
cc07193c23
8 changed files with 440 additions and 57 deletions
24
LICENSE
Normal file
24
LICENSE
Normal file
|
|
@ -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.
|
||||
3
Makefile
3
Makefile
|
|
@ -1 +1,4 @@
|
|||
tests: tests.o ubasic.o tokenizer.o
|
||||
use-ubasic: use-ubasic.o ubasic.o tokenizer.o
|
||||
clean:
|
||||
rm *.o tests use-ubasic
|
||||
|
|
|
|||
134
tests.c
Normal file
134
tests.c
Normal file
|
|
@ -0,0 +1,134 @@
|
|||
/*
|
||||
* 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 <time.h>
|
||||
#include <stdio.h>
|
||||
#include <assert.h>
|
||||
#include "ubasic.h"
|
||||
|
||||
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\
|
||||
80 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";
|
||||
|
||||
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[]) {
|
||||
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_peek_poke(program, &peek, &poke);
|
||||
|
||||
do {
|
||||
ubasic_run();
|
||||
} while(!ubasic_finished());
|
||||
|
||||
end_t = clock();
|
||||
delta_t = (double)(end_t - start_t) / CLOCKS_PER_SEC;
|
||||
|
||||
printf("done. Run time: %.3f s\n", delta_t);
|
||||
}
|
||||
|
||||
/*---------------------------------------------------------------------------*/
|
||||
int
|
||||
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));
|
||||
|
||||
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;
|
||||
}
|
||||
/*---------------------------------------------------------------------------*/
|
||||
63
tokenizer.c
63
tokenizer.c
|
|
@ -43,7 +43,7 @@
|
|||
|
||||
static char const *ptr, *nextptr;
|
||||
|
||||
#define MAX_NUMLEN 5
|
||||
#define MAX_NUMLEN 6
|
||||
|
||||
struct keyword_token {
|
||||
char *keyword;
|
||||
|
|
@ -65,6 +65,9 @@ static const struct keyword_token keywords[] = {
|
|||
{"gosub", TOKENIZER_GOSUB},
|
||||
{"return", TOKENIZER_RETURN},
|
||||
{"call", TOKENIZER_CALL},
|
||||
{"rem", TOKENIZER_REM},
|
||||
{"peek", TOKENIZER_PEEK},
|
||||
{"poke", TOKENIZER_POKE},
|
||||
{"end", TOKENIZER_END},
|
||||
{NULL, TOKENIZER_ERROR}
|
||||
};
|
||||
|
|
@ -95,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 == '<') {
|
||||
|
|
@ -118,21 +123,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 +155,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,14 +166,21 @@ get_next_token(void)
|
|||
return TOKENIZER_VARIABLE;
|
||||
}
|
||||
|
||||
|
||||
|
||||
return TOKENIZER_ERROR;
|
||||
}
|
||||
/*---------------------------------------------------------------------------*/
|
||||
void
|
||||
tokenizer_goto(const char *program)
|
||||
{
|
||||
ptr = program;
|
||||
current_token = get_next_token();
|
||||
}
|
||||
/*---------------------------------------------------------------------------*/
|
||||
void
|
||||
tokenizer_init(const char *program)
|
||||
{
|
||||
ptr = program;
|
||||
tokenizer_goto(program);
|
||||
current_token = get_next_token();
|
||||
}
|
||||
/*---------------------------------------------------------------------------*/
|
||||
|
|
@ -188,15 +200,27 @@ 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;
|
||||
}
|
||||
/*---------------------------------------------------------------------------*/
|
||||
int
|
||||
VARIABLE_TYPE
|
||||
tokenizer_num(void)
|
||||
{
|
||||
return atoi(ptr);
|
||||
|
|
@ -207,7 +231,7 @@ tokenizer_string(char *dest, int len)
|
|||
{
|
||||
char *string_end;
|
||||
int string_len;
|
||||
|
||||
|
||||
if(tokenizer_token() != TOKENIZER_STRING) {
|
||||
return;
|
||||
}
|
||||
|
|
@ -241,3 +265,8 @@ tokenizer_variable_num(void)
|
|||
return *ptr - 'a';
|
||||
}
|
||||
/*---------------------------------------------------------------------------*/
|
||||
char const *
|
||||
tokenizer_pos(void)
|
||||
{
|
||||
return ptr;
|
||||
}
|
||||
|
|
|
|||
11
tokenizer.h
11
tokenizer.h
|
|
@ -30,6 +30,8 @@
|
|||
#ifndef __TOKENIZER_H__
|
||||
#define __TOKENIZER_H__
|
||||
|
||||
#include "vartype.h"
|
||||
|
||||
enum {
|
||||
TOKENIZER_ERROR,
|
||||
TOKENIZER_ENDOFINPUT,
|
||||
|
|
@ -48,6 +50,9 @@ enum {
|
|||
TOKENIZER_GOSUB,
|
||||
TOKENIZER_RETURN,
|
||||
TOKENIZER_CALL,
|
||||
TOKENIZER_REM,
|
||||
TOKENIZER_PEEK,
|
||||
TOKENIZER_POKE,
|
||||
TOKENIZER_END,
|
||||
TOKENIZER_COMMA,
|
||||
TOKENIZER_SEMICOLON,
|
||||
|
|
@ -58,6 +63,7 @@ enum {
|
|||
TOKENIZER_ASTR,
|
||||
TOKENIZER_SLASH,
|
||||
TOKENIZER_MOD,
|
||||
TOKENIZER_HASH,
|
||||
TOKENIZER_LEFTPAREN,
|
||||
TOKENIZER_RIGHTPAREN,
|
||||
TOKENIZER_LT,
|
||||
|
|
@ -66,14 +72,17 @@ enum {
|
|||
TOKENIZER_CR,
|
||||
};
|
||||
|
||||
void tokenizer_goto(const char *program);
|
||||
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);
|
||||
|
||||
int tokenizer_finished(void);
|
||||
void tokenizer_error_print(void);
|
||||
|
||||
char const *tokenizer_pos(void);
|
||||
|
||||
#endif /* __TOKENIZER_H__ */
|
||||
|
|
|
|||
217
ubasic.c
217
ubasic.c
|
|
@ -36,7 +36,6 @@
|
|||
#define DEBUG_PRINTF(...)
|
||||
#endif
|
||||
|
||||
|
||||
#include "ubasic.h"
|
||||
#include "tokenizer.h"
|
||||
|
||||
|
|
@ -60,20 +59,46 @@ 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 char variables[MAX_VARNUM];
|
||||
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);
|
||||
static void index_free(void);
|
||||
|
||||
peek_func peek_function = NULL;
|
||||
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;
|
||||
}
|
||||
/*---------------------------------------------------------------------------*/
|
||||
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;
|
||||
}
|
||||
|
|
@ -83,7 +108,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 +160,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);
|
||||
|
|
@ -157,19 +182,19 @@ term(void)
|
|||
return f1;
|
||||
}
|
||||
/*---------------------------------------------------------------------------*/
|
||||
static int
|
||||
static VARIABLE_TYPE
|
||||
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 +223,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);
|
||||
|
|
@ -225,20 +250,101 @@ 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) {
|
||||
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());
|
||||
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);
|
||||
}
|
||||
}
|
||||
/*---------------------------------------------------------------------------*/
|
||||
|
|
@ -265,13 +371,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 +387,7 @@ static void
|
|||
if_statement(void)
|
||||
{
|
||||
int r;
|
||||
|
||||
|
||||
accept(TOKENIZER_IF);
|
||||
|
||||
r = relation();
|
||||
|
|
@ -293,8 +399,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 +458,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 +483,7 @@ static void
|
|||
for_statement(void)
|
||||
{
|
||||
int for_variable, to;
|
||||
|
||||
|
||||
accept(TOKENIZER_FOR);
|
||||
for_variable = tokenizer_variable_num();
|
||||
accept(TOKENIZER_VARIABLE);
|
||||
|
|
@ -392,9 +498,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");
|
||||
|
|
@ -402,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);
|
||||
|
|
@ -412,9 +549,9 @@ static void
|
|||
statement(void)
|
||||
{
|
||||
int token;
|
||||
|
||||
|
||||
token = tokenizer_token();
|
||||
|
||||
|
||||
switch(token) {
|
||||
case TOKENIZER_PRINT:
|
||||
print_statement();
|
||||
|
|
@ -434,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;
|
||||
|
|
@ -456,7 +599,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;
|
||||
|
|
@ -480,17 +623,17 @@ 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) {
|
||||
if(varnum >= 0 && varnum <= MAX_VARNUM) {
|
||||
variables[varnum] = value;
|
||||
}
|
||||
}
|
||||
/*---------------------------------------------------------------------------*/
|
||||
int
|
||||
VARIABLE_TYPE
|
||||
ubasic_get_variable(int varnum)
|
||||
{
|
||||
if(varnum > 0 && varnum <= MAX_VARNUM) {
|
||||
if(varnum >= 0 && varnum <= MAX_VARNUM) {
|
||||
return variables[varnum];
|
||||
}
|
||||
return 0;
|
||||
|
|
|
|||
9
ubasic.h
9
ubasic.h
|
|
@ -30,11 +30,16 @@
|
|||
#ifndef __UBASIC_H__
|
||||
#define __UBASIC_H__
|
||||
|
||||
#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);
|
||||
|
||||
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__ */
|
||||
|
|
|
|||
36
vartype.h
Normal file
36
vartype.h
Normal file
|
|
@ -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__ */
|
||||
Loading…
Add table
Add a link
Reference in a new issue