forked from mirrors/ubasic
The uBASIC 0.1 source code
This commit is contained in:
parent
bcb458da42
commit
c299654d1c
6 changed files with 916 additions and 0 deletions
1
Makefile
Normal file
1
Makefile
Normal file
|
|
@ -0,0 +1 @@
|
|||
use-ubasic: use-ubasic.o ubasic.o tokenizer.o
|
||||
243
tokenizer.c
Normal file
243
tokenizer.c
Normal file
|
|
@ -0,0 +1,243 @@
|
|||
/*
|
||||
* 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. 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.
|
||||
*
|
||||
*/
|
||||
|
||||
#define DEBUG 0
|
||||
|
||||
#if DEBUG
|
||||
#define DEBUG_PRINTF(...) printf(__VA_ARGS__)
|
||||
#else
|
||||
#define DEBUG_PRINTF(...)
|
||||
#endif
|
||||
|
||||
#include "tokenizer.h"
|
||||
#include <string.h>
|
||||
#include <ctype.h>
|
||||
#include <stdlib.h>
|
||||
|
||||
static char const *ptr, *nextptr;
|
||||
|
||||
#define MAX_NUMLEN 5
|
||||
|
||||
struct keyword_token {
|
||||
char *keyword;
|
||||
int token;
|
||||
};
|
||||
|
||||
static int current_token = TOKENIZER_ERROR;
|
||||
|
||||
static const struct keyword_token keywords[] = {
|
||||
{"let", TOKENIZER_LET},
|
||||
{"print", TOKENIZER_PRINT},
|
||||
{"if", TOKENIZER_IF},
|
||||
{"then", TOKENIZER_THEN},
|
||||
{"else", TOKENIZER_ELSE},
|
||||
{"for", TOKENIZER_FOR},
|
||||
{"to", TOKENIZER_TO},
|
||||
{"next", TOKENIZER_NEXT},
|
||||
{"goto", TOKENIZER_GOTO},
|
||||
{"gosub", TOKENIZER_GOSUB},
|
||||
{"return", TOKENIZER_RETURN},
|
||||
{"call", TOKENIZER_CALL},
|
||||
{"end", TOKENIZER_END},
|
||||
{NULL, TOKENIZER_ERROR}
|
||||
};
|
||||
|
||||
/*---------------------------------------------------------------------------*/
|
||||
static int
|
||||
singlechar(void)
|
||||
{
|
||||
if(*ptr == '\n') {
|
||||
return TOKENIZER_CR;
|
||||
} else if(*ptr == ',') {
|
||||
return TOKENIZER_COMMA;
|
||||
} else if(*ptr == ';') {
|
||||
return TOKENIZER_SEMICOLON;
|
||||
} else if(*ptr == '+') {
|
||||
return TOKENIZER_PLUS;
|
||||
} else if(*ptr == '-') {
|
||||
return TOKENIZER_MINUS;
|
||||
} else if(*ptr == '&') {
|
||||
return TOKENIZER_AND;
|
||||
} else if(*ptr == '|') {
|
||||
return TOKENIZER_OR;
|
||||
} else if(*ptr == '*') {
|
||||
return TOKENIZER_ASTR;
|
||||
} else if(*ptr == '/') {
|
||||
return TOKENIZER_SLASH;
|
||||
} else if(*ptr == '%') {
|
||||
return TOKENIZER_MOD;
|
||||
} else if(*ptr == '(') {
|
||||
return TOKENIZER_LEFTPAREN;
|
||||
} else if(*ptr == ')') {
|
||||
return TOKENIZER_RIGHTPAREN;
|
||||
} else if(*ptr == '<') {
|
||||
return TOKENIZER_LT;
|
||||
} else if(*ptr == '>') {
|
||||
return TOKENIZER_GT;
|
||||
} else if(*ptr == '=') {
|
||||
return TOKENIZER_EQ;
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
/*---------------------------------------------------------------------------*/
|
||||
static int
|
||||
get_next_token(void)
|
||||
{
|
||||
struct keyword_token const *kt;
|
||||
int i;
|
||||
|
||||
DEBUG_PRINTF("get_next_token(): '%s'\n", ptr);
|
||||
|
||||
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(!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 too long number\n");
|
||||
return TOKENIZER_ERROR;
|
||||
} else if(singlechar()) {
|
||||
nextptr = ptr + 1;
|
||||
return singlechar();
|
||||
} else if(*ptr == '"') {
|
||||
nextptr = ptr;
|
||||
do {
|
||||
++nextptr;
|
||||
} while(*nextptr != '"');
|
||||
++nextptr;
|
||||
return TOKENIZER_STRING;
|
||||
} 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;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if(*ptr >= 'a' && *ptr <= 'z') {
|
||||
nextptr = ptr + 1;
|
||||
return TOKENIZER_VARIABLE;
|
||||
}
|
||||
|
||||
|
||||
return TOKENIZER_ERROR;
|
||||
}
|
||||
/*---------------------------------------------------------------------------*/
|
||||
void
|
||||
tokenizer_init(const char *program)
|
||||
{
|
||||
ptr = program;
|
||||
current_token = get_next_token();
|
||||
}
|
||||
/*---------------------------------------------------------------------------*/
|
||||
int
|
||||
tokenizer_token(void)
|
||||
{
|
||||
return current_token;
|
||||
}
|
||||
/*---------------------------------------------------------------------------*/
|
||||
void
|
||||
tokenizer_next(void)
|
||||
{
|
||||
|
||||
if(tokenizer_finished()) {
|
||||
return;
|
||||
}
|
||||
|
||||
DEBUG_PRINTF("tokenizer_next: %p\n", nextptr);
|
||||
ptr = nextptr;
|
||||
while(*ptr == ' ') {
|
||||
++ptr;
|
||||
}
|
||||
current_token = get_next_token();
|
||||
DEBUG_PRINTF("tokenizer_next: '%s' %d\n", ptr, current_token);
|
||||
return;
|
||||
}
|
||||
/*---------------------------------------------------------------------------*/
|
||||
int
|
||||
tokenizer_num(void)
|
||||
{
|
||||
return atoi(ptr);
|
||||
}
|
||||
/*---------------------------------------------------------------------------*/
|
||||
void
|
||||
tokenizer_string(char *dest, int len)
|
||||
{
|
||||
char *string_end;
|
||||
int string_len;
|
||||
|
||||
if(tokenizer_token() != TOKENIZER_STRING) {
|
||||
return;
|
||||
}
|
||||
string_end = strchr(ptr + 1, '"');
|
||||
if(string_end == NULL) {
|
||||
return;
|
||||
}
|
||||
string_len = string_end - ptr - 1;
|
||||
if(len < string_len) {
|
||||
string_len = len;
|
||||
}
|
||||
memcpy(dest, ptr + 1, string_len);
|
||||
dest[string_len] = 0;
|
||||
}
|
||||
/*---------------------------------------------------------------------------*/
|
||||
void
|
||||
tokenizer_error_print(void)
|
||||
{
|
||||
DEBUG_PRINTF("tokenizer_error_print: '%s'\n", ptr);
|
||||
}
|
||||
/*---------------------------------------------------------------------------*/
|
||||
int
|
||||
tokenizer_finished(void)
|
||||
{
|
||||
return *ptr == 0 || current_token == TOKENIZER_ENDOFINPUT;
|
||||
}
|
||||
/*---------------------------------------------------------------------------*/
|
||||
int
|
||||
tokenizer_variable_num(void)
|
||||
{
|
||||
return *ptr - 'a';
|
||||
}
|
||||
/*---------------------------------------------------------------------------*/
|
||||
79
tokenizer.h
Normal file
79
tokenizer.h
Normal file
|
|
@ -0,0 +1,79 @@
|
|||
/*
|
||||
* 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. 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 __TOKENIZER_H__
|
||||
#define __TOKENIZER_H__
|
||||
|
||||
enum {
|
||||
TOKENIZER_ERROR,
|
||||
TOKENIZER_ENDOFINPUT,
|
||||
TOKENIZER_NUMBER,
|
||||
TOKENIZER_STRING,
|
||||
TOKENIZER_VARIABLE,
|
||||
TOKENIZER_LET,
|
||||
TOKENIZER_PRINT,
|
||||
TOKENIZER_IF,
|
||||
TOKENIZER_THEN,
|
||||
TOKENIZER_ELSE,
|
||||
TOKENIZER_FOR,
|
||||
TOKENIZER_TO,
|
||||
TOKENIZER_NEXT,
|
||||
TOKENIZER_GOTO,
|
||||
TOKENIZER_GOSUB,
|
||||
TOKENIZER_RETURN,
|
||||
TOKENIZER_CALL,
|
||||
TOKENIZER_END,
|
||||
TOKENIZER_COMMA,
|
||||
TOKENIZER_SEMICOLON,
|
||||
TOKENIZER_PLUS,
|
||||
TOKENIZER_MINUS,
|
||||
TOKENIZER_AND,
|
||||
TOKENIZER_OR,
|
||||
TOKENIZER_ASTR,
|
||||
TOKENIZER_SLASH,
|
||||
TOKENIZER_MOD,
|
||||
TOKENIZER_LEFTPAREN,
|
||||
TOKENIZER_RIGHTPAREN,
|
||||
TOKENIZER_LT,
|
||||
TOKENIZER_GT,
|
||||
TOKENIZER_EQ,
|
||||
TOKENIZER_CR,
|
||||
};
|
||||
|
||||
void tokenizer_init(const char *program);
|
||||
void tokenizer_next(void);
|
||||
int tokenizer_token(void);
|
||||
int tokenizer_num(void);
|
||||
int tokenizer_variable_num(void);
|
||||
void tokenizer_string(char *dest, int len);
|
||||
|
||||
int tokenizer_finished(void);
|
||||
void tokenizer_error_print(void);
|
||||
|
||||
#endif /* __TOKENIZER_H__ */
|
||||
498
ubasic.c
Normal file
498
ubasic.c
Normal file
|
|
@ -0,0 +1,498 @@
|
|||
/*
|
||||
* 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. 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.
|
||||
*
|
||||
*/
|
||||
|
||||
#define DEBUG 0
|
||||
|
||||
#if DEBUG
|
||||
#define DEBUG_PRINTF(...) printf(__VA_ARGS__)
|
||||
#else
|
||||
#define DEBUG_PRINTF(...)
|
||||
#endif
|
||||
|
||||
|
||||
#include "ubasic.h"
|
||||
#include "tokenizer.h"
|
||||
|
||||
#include <stdio.h> /* printf() */
|
||||
#include <stdlib.h> /* exit() */
|
||||
|
||||
static char const *program_ptr;
|
||||
#define MAX_STRINGLEN 40
|
||||
static char string[MAX_STRINGLEN];
|
||||
|
||||
#define MAX_GOSUB_STACK_DEPTH 10
|
||||
static int gosub_stack[MAX_GOSUB_STACK_DEPTH];
|
||||
static int gosub_stack_ptr;
|
||||
|
||||
struct for_state {
|
||||
int line_after_for;
|
||||
int for_variable;
|
||||
int to;
|
||||
};
|
||||
#define MAX_FOR_STACK_DEPTH 4
|
||||
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 int ended;
|
||||
|
||||
static int expr(void);
|
||||
static void line_statement(void);
|
||||
static void statement(void);
|
||||
/*---------------------------------------------------------------------------*/
|
||||
void
|
||||
ubasic_init(const char *program)
|
||||
{
|
||||
program_ptr = program;
|
||||
for_stack_ptr = gosub_stack_ptr = 0;
|
||||
tokenizer_init(program);
|
||||
ended = 0;
|
||||
}
|
||||
/*---------------------------------------------------------------------------*/
|
||||
static void
|
||||
accept(int token)
|
||||
{
|
||||
if(token != tokenizer_token()) {
|
||||
DEBUG_PRINTF("Token not what was expected (expected %d, got %d)\n",
|
||||
token, tokenizer_token());
|
||||
tokenizer_error_print();
|
||||
exit(1);
|
||||
}
|
||||
DEBUG_PRINTF("Expected %d, got it\n", token);
|
||||
tokenizer_next();
|
||||
}
|
||||
/*---------------------------------------------------------------------------*/
|
||||
static int
|
||||
varfactor(void)
|
||||
{
|
||||
int r;
|
||||
DEBUG_PRINTF("varfactor: obtaining %d from variable %d\n", variables[tokenizer_variable_num()], tokenizer_variable_num());
|
||||
r = ubasic_get_variable(tokenizer_variable_num());
|
||||
accept(TOKENIZER_VARIABLE);
|
||||
return r;
|
||||
}
|
||||
/*---------------------------------------------------------------------------*/
|
||||
static int
|
||||
factor(void)
|
||||
{
|
||||
int r;
|
||||
|
||||
DEBUG_PRINTF("factor: token %d\n", tokenizer_token());
|
||||
switch(tokenizer_token()) {
|
||||
case TOKENIZER_NUMBER:
|
||||
r = tokenizer_num();
|
||||
DEBUG_PRINTF("factor: number %d\n", r);
|
||||
accept(TOKENIZER_NUMBER);
|
||||
break;
|
||||
case TOKENIZER_LEFTPAREN:
|
||||
accept(TOKENIZER_LEFTPAREN);
|
||||
r = expr();
|
||||
accept(TOKENIZER_RIGHTPAREN);
|
||||
break;
|
||||
default:
|
||||
r = varfactor();
|
||||
break;
|
||||
}
|
||||
return r;
|
||||
}
|
||||
/*---------------------------------------------------------------------------*/
|
||||
static int
|
||||
term(void)
|
||||
{
|
||||
int f1, f2;
|
||||
int op;
|
||||
|
||||
f1 = factor();
|
||||
op = tokenizer_token();
|
||||
DEBUG_PRINTF("term: token %d\n", op);
|
||||
while(op == TOKENIZER_ASTR ||
|
||||
op == TOKENIZER_SLASH ||
|
||||
op == TOKENIZER_MOD) {
|
||||
tokenizer_next();
|
||||
f2 = factor();
|
||||
DEBUG_PRINTF("term: %d %d %d\n", f1, op, f2);
|
||||
switch(op) {
|
||||
case TOKENIZER_ASTR:
|
||||
f1 = f1 * f2;
|
||||
break;
|
||||
case TOKENIZER_SLASH:
|
||||
f1 = f1 / f2;
|
||||
break;
|
||||
case TOKENIZER_MOD:
|
||||
f1 = f1 % f2;
|
||||
break;
|
||||
}
|
||||
op = tokenizer_token();
|
||||
}
|
||||
DEBUG_PRINTF("term: %d\n", f1);
|
||||
return f1;
|
||||
}
|
||||
/*---------------------------------------------------------------------------*/
|
||||
static int
|
||||
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) {
|
||||
tokenizer_next();
|
||||
t2 = term();
|
||||
DEBUG_PRINTF("expr: %d %d %d\n", t1, op, t2);
|
||||
switch(op) {
|
||||
case TOKENIZER_PLUS:
|
||||
t1 = t1 + t2;
|
||||
break;
|
||||
case TOKENIZER_MINUS:
|
||||
t1 = t1 - t2;
|
||||
break;
|
||||
case TOKENIZER_AND:
|
||||
t1 = t1 & t2;
|
||||
break;
|
||||
case TOKENIZER_OR:
|
||||
t1 = t1 | t2;
|
||||
break;
|
||||
}
|
||||
op = tokenizer_token();
|
||||
}
|
||||
DEBUG_PRINTF("expr: %d\n", t1);
|
||||
return t1;
|
||||
}
|
||||
/*---------------------------------------------------------------------------*/
|
||||
static int
|
||||
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) {
|
||||
tokenizer_next();
|
||||
r2 = expr();
|
||||
DEBUG_PRINTF("relation: %d %d %d\n", r1, op, r2);
|
||||
switch(op) {
|
||||
case TOKENIZER_LT:
|
||||
r1 = r1 < r2;
|
||||
break;
|
||||
case TOKENIZER_GT:
|
||||
r1 = r1 > r2;
|
||||
break;
|
||||
case TOKENIZER_EQ:
|
||||
r1 = r1 == r2;
|
||||
break;
|
||||
}
|
||||
op = tokenizer_token();
|
||||
}
|
||||
return r1;
|
||||
}
|
||||
/*---------------------------------------------------------------------------*/
|
||||
static void
|
||||
jump_linenum(int linenum)
|
||||
{
|
||||
tokenizer_init(program_ptr);
|
||||
while(tokenizer_num() != linenum) {
|
||||
do {
|
||||
do {
|
||||
tokenizer_next();
|
||||
} while(tokenizer_token() != TOKENIZER_CR &&
|
||||
tokenizer_token() != TOKENIZER_ENDOFINPUT);
|
||||
if(tokenizer_token() == TOKENIZER_CR) {
|
||||
tokenizer_next();
|
||||
}
|
||||
} while(tokenizer_token() != TOKENIZER_NUMBER);
|
||||
DEBUG_PRINTF("jump_linenum: Found line %d\n", tokenizer_num());
|
||||
}
|
||||
}
|
||||
/*---------------------------------------------------------------------------*/
|
||||
static void
|
||||
goto_statement(void)
|
||||
{
|
||||
accept(TOKENIZER_GOTO);
|
||||
jump_linenum(tokenizer_num());
|
||||
}
|
||||
/*---------------------------------------------------------------------------*/
|
||||
static void
|
||||
print_statement(void)
|
||||
{
|
||||
accept(TOKENIZER_PRINT);
|
||||
do {
|
||||
DEBUG_PRINTF("Print loop\n");
|
||||
if(tokenizer_token() == TOKENIZER_STRING) {
|
||||
tokenizer_string(string, sizeof(string));
|
||||
printf("%s", string);
|
||||
tokenizer_next();
|
||||
} else if(tokenizer_token() == TOKENIZER_COMMA) {
|
||||
printf(" ");
|
||||
tokenizer_next();
|
||||
} else if(tokenizer_token() == TOKENIZER_SEMICOLON) {
|
||||
tokenizer_next();
|
||||
} else if(tokenizer_token() == TOKENIZER_VARIABLE ||
|
||||
tokenizer_token() == TOKENIZER_NUMBER) {
|
||||
printf("%d", expr());
|
||||
} else {
|
||||
break;
|
||||
}
|
||||
} while(tokenizer_token() != TOKENIZER_CR &&
|
||||
tokenizer_token() != TOKENIZER_ENDOFINPUT);
|
||||
printf("\n");
|
||||
DEBUG_PRINTF("End of print\n");
|
||||
tokenizer_next();
|
||||
}
|
||||
/*---------------------------------------------------------------------------*/
|
||||
static void
|
||||
if_statement(void)
|
||||
{
|
||||
int r;
|
||||
|
||||
accept(TOKENIZER_IF);
|
||||
|
||||
r = relation();
|
||||
DEBUG_PRINTF("if_statement: relation %d\n", r);
|
||||
accept(TOKENIZER_THEN);
|
||||
if(r) {
|
||||
statement();
|
||||
} else {
|
||||
do {
|
||||
tokenizer_next();
|
||||
} while(tokenizer_token() != TOKENIZER_ELSE &&
|
||||
tokenizer_token() != TOKENIZER_CR &&
|
||||
tokenizer_token() != TOKENIZER_ENDOFINPUT);
|
||||
if(tokenizer_token() == TOKENIZER_ELSE) {
|
||||
tokenizer_next();
|
||||
statement();
|
||||
} else if(tokenizer_token() == TOKENIZER_CR) {
|
||||
tokenizer_next();
|
||||
}
|
||||
}
|
||||
}
|
||||
/*---------------------------------------------------------------------------*/
|
||||
static void
|
||||
let_statement(void)
|
||||
{
|
||||
int var;
|
||||
|
||||
var = tokenizer_variable_num();
|
||||
|
||||
accept(TOKENIZER_VARIABLE);
|
||||
accept(TOKENIZER_EQ);
|
||||
ubasic_set_variable(var, expr());
|
||||
DEBUG_PRINTF("let_statement: assign %d to %d\n", variables[var], var);
|
||||
accept(TOKENIZER_CR);
|
||||
|
||||
}
|
||||
/*---------------------------------------------------------------------------*/
|
||||
static void
|
||||
gosub_statement(void)
|
||||
{
|
||||
int linenum;
|
||||
accept(TOKENIZER_GOSUB);
|
||||
linenum = tokenizer_num();
|
||||
accept(TOKENIZER_NUMBER);
|
||||
accept(TOKENIZER_CR);
|
||||
if(gosub_stack_ptr < MAX_GOSUB_STACK_DEPTH) {
|
||||
gosub_stack[gosub_stack_ptr] = tokenizer_num();
|
||||
gosub_stack_ptr++;
|
||||
jump_linenum(linenum);
|
||||
} else {
|
||||
DEBUG_PRINTF("gosub_statement: gosub stack exhausted\n");
|
||||
}
|
||||
}
|
||||
/*---------------------------------------------------------------------------*/
|
||||
static void
|
||||
return_statement(void)
|
||||
{
|
||||
accept(TOKENIZER_RETURN);
|
||||
if(gosub_stack_ptr > 0) {
|
||||
gosub_stack_ptr--;
|
||||
jump_linenum(gosub_stack[gosub_stack_ptr]);
|
||||
} else {
|
||||
DEBUG_PRINTF("return_statement: non-matching return\n");
|
||||
}
|
||||
}
|
||||
/*---------------------------------------------------------------------------*/
|
||||
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);
|
||||
if(ubasic_get_variable(var) <= for_stack[for_stack_ptr - 1].to) {
|
||||
jump_linenum(for_stack[for_stack_ptr - 1].line_after_for);
|
||||
} else {
|
||||
for_stack_ptr--;
|
||||
accept(TOKENIZER_CR);
|
||||
}
|
||||
} else {
|
||||
DEBUG_PRINTF("next_statement: non-matching next (expected %d, found %d)\n", for_stack[for_stack_ptr - 1].for_variable, var);
|
||||
accept(TOKENIZER_CR);
|
||||
}
|
||||
|
||||
}
|
||||
/*---------------------------------------------------------------------------*/
|
||||
static void
|
||||
for_statement(void)
|
||||
{
|
||||
int for_variable, to;
|
||||
|
||||
accept(TOKENIZER_FOR);
|
||||
for_variable = tokenizer_variable_num();
|
||||
accept(TOKENIZER_VARIABLE);
|
||||
accept(TOKENIZER_EQ);
|
||||
ubasic_set_variable(for_variable, expr());
|
||||
accept(TOKENIZER_TO);
|
||||
to = expr();
|
||||
accept(TOKENIZER_CR);
|
||||
|
||||
if(for_stack_ptr < MAX_FOR_STACK_DEPTH) {
|
||||
for_stack[for_stack_ptr].line_after_for = tokenizer_num();
|
||||
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_ptr++;
|
||||
} else {
|
||||
DEBUG_PRINTF("for_statement: for stack depth exceeded\n");
|
||||
}
|
||||
}
|
||||
/*---------------------------------------------------------------------------*/
|
||||
static void
|
||||
end_statement(void)
|
||||
{
|
||||
accept(TOKENIZER_END);
|
||||
ended = 1;
|
||||
}
|
||||
/*---------------------------------------------------------------------------*/
|
||||
static void
|
||||
statement(void)
|
||||
{
|
||||
int token;
|
||||
|
||||
token = tokenizer_token();
|
||||
|
||||
switch(token) {
|
||||
case TOKENIZER_PRINT:
|
||||
print_statement();
|
||||
break;
|
||||
case TOKENIZER_IF:
|
||||
if_statement();
|
||||
break;
|
||||
case TOKENIZER_GOTO:
|
||||
goto_statement();
|
||||
break;
|
||||
case TOKENIZER_GOSUB:
|
||||
gosub_statement();
|
||||
break;
|
||||
case TOKENIZER_RETURN:
|
||||
return_statement();
|
||||
break;
|
||||
case TOKENIZER_FOR:
|
||||
for_statement();
|
||||
break;
|
||||
case TOKENIZER_NEXT:
|
||||
next_statement();
|
||||
break;
|
||||
case TOKENIZER_END:
|
||||
end_statement();
|
||||
break;
|
||||
case TOKENIZER_LET:
|
||||
accept(TOKENIZER_LET);
|
||||
/* Fall through. */
|
||||
case TOKENIZER_VARIABLE:
|
||||
let_statement();
|
||||
break;
|
||||
default:
|
||||
DEBUG_PRINTF("ubasic.c: statement(): not implemented %d\n", token);
|
||||
exit(1);
|
||||
}
|
||||
}
|
||||
/*---------------------------------------------------------------------------*/
|
||||
static void
|
||||
line_statement(void)
|
||||
{
|
||||
DEBUG_PRINTF("----------- Line number %d ---------\n", tokenizer_num());
|
||||
/* current_linenum = tokenizer_num();*/
|
||||
accept(TOKENIZER_NUMBER);
|
||||
statement();
|
||||
return;
|
||||
}
|
||||
/*---------------------------------------------------------------------------*/
|
||||
void
|
||||
ubasic_run(void)
|
||||
{
|
||||
if(tokenizer_finished()) {
|
||||
DEBUG_PRINTF("uBASIC program finished\n");
|
||||
return;
|
||||
}
|
||||
|
||||
line_statement();
|
||||
}
|
||||
/*---------------------------------------------------------------------------*/
|
||||
int
|
||||
ubasic_finished(void)
|
||||
{
|
||||
return ended || tokenizer_finished();
|
||||
}
|
||||
/*---------------------------------------------------------------------------*/
|
||||
void
|
||||
ubasic_set_variable(int varnum, int value)
|
||||
{
|
||||
if(varnum > 0 && varnum <= MAX_VARNUM) {
|
||||
variables[varnum] = value;
|
||||
}
|
||||
}
|
||||
/*---------------------------------------------------------------------------*/
|
||||
int
|
||||
ubasic_get_variable(int varnum)
|
||||
{
|
||||
if(varnum > 0 && varnum <= MAX_VARNUM) {
|
||||
return variables[varnum];
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
/*---------------------------------------------------------------------------*/
|
||||
40
ubasic.h
Normal file
40
ubasic.h
Normal file
|
|
@ -0,0 +1,40 @@
|
|||
/*
|
||||
* 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. 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 __UBASIC_H__
|
||||
#define __UBASIC_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);
|
||||
|
||||
#endif /* __UBASIC_H__ */
|
||||
55
use-ubasic.c
Normal file
55
use-ubasic.c
Normal file
|
|
@ -0,0 +1,55 @@
|
|||
/*
|
||||
* 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. 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 "ubasic.h"
|
||||
|
||||
static const char program[] =
|
||||
"10 gosub 100\n\
|
||||
20 for i = 1 to 10\n\
|
||||
30 print i\n\
|
||||
40 next i\n\
|
||||
50 print \"end\"\n\
|
||||
60 end\n\
|
||||
100 print \"subroutine\"\n\
|
||||
110 return\n";
|
||||
|
||||
/*---------------------------------------------------------------------------*/
|
||||
int
|
||||
main(void)
|
||||
{
|
||||
ubasic_init(program);
|
||||
|
||||
do {
|
||||
ubasic_run();
|
||||
} while(!ubasic_finished());
|
||||
|
||||
return 0;
|
||||
}
|
||||
/*---------------------------------------------------------------------------*/
|
||||
Loading…
Add table
Add a link
Reference in a new issue