Added the ability to parse commands as well as expressions.
Added basic PRINT and REM commands. Added solver tests which had been around for a while and not committed. All execution is still immediate mode.
This commit is contained in:
190
src/basic.c
190
src/basic.c
@@ -11,6 +11,62 @@ char _tokenizer_value[BASIC_TOKENIZER_MAX_LENGTH];
|
||||
char *_tokenizer_prev;
|
||||
char *_tokenizer_prev_next;
|
||||
|
||||
void basic_report_error()
|
||||
{
|
||||
char decimal[2];
|
||||
decimal[0] = 0;
|
||||
decimal[1] = 0;
|
||||
_cputs("Error: ");
|
||||
decimal[0] = dtoa(basic_errno);
|
||||
_cputs((char *)&decimal);
|
||||
_cputs("\n");
|
||||
}
|
||||
|
||||
/***************************************************************
|
||||
*
|
||||
* Basic Commands
|
||||
*
|
||||
***************************************************************/
|
||||
|
||||
void basic_cmd_rem(void *expr)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
void basic_cmd_print(void *data)
|
||||
{
|
||||
struct basic_variable result;
|
||||
basic_expr *expr;
|
||||
if ( data == NULL ) {
|
||||
basic_errno = BASIC_ERR_INTERNAL_NULLPOINTER;
|
||||
return;
|
||||
}
|
||||
|
||||
_cputs("PRINT(");
|
||||
_cputs((char *)data);
|
||||
_cputs(")\n");
|
||||
|
||||
expr = basic_parse_expr((char *)data);
|
||||
if ( expr == NULL ) {
|
||||
basic_errno = BASIC_ERR_INTERNAL_NULLPOINTER;
|
||||
return;
|
||||
}
|
||||
|
||||
memset((void *)&result, 0x00, sizeof(struct basic_variable));
|
||||
basic_solve_expr(expr, &result);
|
||||
if ( basic_errno != 0 ) {
|
||||
return;
|
||||
} else {
|
||||
basic_print_var(&result);
|
||||
}
|
||||
}
|
||||
|
||||
/***************************************************************
|
||||
*
|
||||
* Basic Tokenizer
|
||||
*
|
||||
***************************************************************/
|
||||
|
||||
void _tokenizer_init(void)
|
||||
{
|
||||
_tokenizer_prev = NULL;
|
||||
@@ -64,11 +120,17 @@ _tokenize_copy:
|
||||
return ptr;
|
||||
}
|
||||
|
||||
char *_token_get(void)
|
||||
char *tokenizer_token(void)
|
||||
{
|
||||
return (char *)&_tokenizer_value;
|
||||
}
|
||||
|
||||
/***************************************************************
|
||||
*
|
||||
* Basic Interpreter
|
||||
*
|
||||
***************************************************************/
|
||||
|
||||
int basic_solve_expr(struct basic_expr *expr, struct basic_variable *result)
|
||||
{
|
||||
if ( expr == NULL || result == NULL ) {
|
||||
@@ -77,9 +139,18 @@ int basic_solve_expr(struct basic_expr *expr, struct basic_variable *result)
|
||||
}
|
||||
|
||||
switch (expr->type) {
|
||||
case BASIC_OPTP_NONE:
|
||||
if ( expr->lval_type == BASIC_LVAL_CONST_INT ) {
|
||||
result->flags = (result->flags | BASIC_VARFLAG_TINT | BASIC_VARFLAG_INIT);
|
||||
result->value.i = expr->lval.i;
|
||||
} else {
|
||||
basic_errno = BASIC_ERR_INTERNAL_UNIMPLEMENTED;
|
||||
return 0;
|
||||
}
|
||||
break;
|
||||
case BASIC_OPTP_ADD:
|
||||
if ( expr->lval_type != BASIC_LVAL_CONST ||
|
||||
( expr->rval_type != BASIC_RVAL_CONST ) ) {
|
||||
if ( expr->lval_type != BASIC_LVAL_CONST_INT ||
|
||||
( expr->rval_type != BASIC_RVAL_CONST_INT ) ) {
|
||||
basic_errno = BASIC_ERR_INVALID_ARGUMENTS;
|
||||
return 0;
|
||||
}
|
||||
@@ -87,8 +158,8 @@ int basic_solve_expr(struct basic_expr *expr, struct basic_variable *result)
|
||||
result->value.i = expr->lval.i + expr->rval.i;
|
||||
break;
|
||||
case BASIC_OPTP_SUB:
|
||||
if ( expr->lval_type != BASIC_LVAL_CONST ||
|
||||
( expr->rval_type != BASIC_RVAL_CONST ) ) {
|
||||
if ( expr->lval_type != BASIC_LVAL_CONST_INT ||
|
||||
( expr->rval_type != BASIC_RVAL_CONST_INT ) ) {
|
||||
basic_errno = BASIC_ERR_INVALID_ARGUMENTS;
|
||||
return 0;
|
||||
}
|
||||
@@ -96,8 +167,8 @@ int basic_solve_expr(struct basic_expr *expr, struct basic_variable *result)
|
||||
result->value.i = expr->lval.i - expr->rval.i;
|
||||
break;
|
||||
case BASIC_OPTP_MUL:
|
||||
if ( expr->lval_type != BASIC_LVAL_CONST ||
|
||||
( expr->rval_type != BASIC_RVAL_CONST ) ) {
|
||||
if ( expr->lval_type != BASIC_LVAL_CONST_INT ||
|
||||
( expr->rval_type != BASIC_RVAL_CONST_INT ) ) {
|
||||
basic_errno = BASIC_ERR_INVALID_ARGUMENTS;
|
||||
return 0;
|
||||
}
|
||||
@@ -105,8 +176,8 @@ int basic_solve_expr(struct basic_expr *expr, struct basic_variable *result)
|
||||
result->value.i = expr->lval.i * expr->rval.i;
|
||||
break;
|
||||
case BASIC_OPTP_DIV:
|
||||
if ( expr->lval_type != BASIC_LVAL_CONST ||
|
||||
( expr->rval_type != BASIC_RVAL_CONST ) ) {
|
||||
if ( expr->lval_type != BASIC_LVAL_CONST_INT ||
|
||||
( expr->rval_type != BASIC_RVAL_CONST_INT ) ) {
|
||||
basic_errno = BASIC_ERR_INVALID_ARGUMENTS;
|
||||
return 0;
|
||||
}
|
||||
@@ -118,8 +189,8 @@ int basic_solve_expr(struct basic_expr *expr, struct basic_variable *result)
|
||||
result->value.i = expr->lval.i / expr->rval.i;
|
||||
break;
|
||||
case BASIC_OPTP_MOD:
|
||||
if ( expr->lval_type != BASIC_LVAL_CONST ||
|
||||
( expr->rval_type != BASIC_RVAL_CONST ) ) {
|
||||
if ( expr->lval_type != BASIC_LVAL_CONST_INT ||
|
||||
( expr->rval_type != BASIC_RVAL_CONST_INT ) ) {
|
||||
basic_errno = BASIC_ERR_INVALID_ARGUMENTS;
|
||||
return 0;
|
||||
}
|
||||
@@ -131,8 +202,8 @@ int basic_solve_expr(struct basic_expr *expr, struct basic_variable *result)
|
||||
result->value.i = (expr->lval.i) - ((expr->lval.i / expr->rval.i)*expr->rval.i);
|
||||
break;
|
||||
case BASIC_OPTP_EQL:
|
||||
if ( expr->lval_type != BASIC_LVAL_CONST ||
|
||||
( expr->rval_type != BASIC_RVAL_CONST ) ) {
|
||||
if ( expr->lval_type != BASIC_LVAL_CONST_INT ||
|
||||
( expr->rval_type != BASIC_RVAL_CONST_INT ) ) {
|
||||
basic_errno = BASIC_ERR_INVALID_ARGUMENTS;
|
||||
return 0;
|
||||
}
|
||||
@@ -169,37 +240,60 @@ struct basic_expr *basic_parse_expr(char *expbuf)
|
||||
continue;
|
||||
}
|
||||
|
||||
expbuf = _tokenize(expbuf, BASIC_TOKENIZER_TOKENS);
|
||||
token = _token_get();
|
||||
expbuf = _tokenize(expbuf, " ");
|
||||
token = tokenizer_token();
|
||||
if ( isdigit(*token) == 1 ) {
|
||||
if ( (ret->type == 0) && (flags & BASIC_PARSE_FOUND_LVAL) == BASIC_PARSE_FOUND_LVAL ) {
|
||||
basic_errno = BASIC_ERR_SYNTAX_MULTIPLE_LVALUES;
|
||||
return NULL;
|
||||
} else if ( ret->type == 0x0 ) {
|
||||
ret->lval.i = atoi(token);
|
||||
ret->lval_type = BASIC_LVAL_CONST;
|
||||
ret->lval_type = BASIC_LVAL_CONST_INT;
|
||||
flags = (flags | BASIC_PARSE_FOUND_LVAL);
|
||||
} else if ( ret->type != 0x0 && ((flags & BASIC_PARSE_FOUND_RVAL) == BASIC_PARSE_FOUND_RVAL)) {
|
||||
basic_errno = BASIC_ERR_SYNTAX_MULTIPLE_RVALUES;
|
||||
return NULL;
|
||||
} else if ( ret->type != 0x0 ) {
|
||||
ret->rval.i = atoi(token);
|
||||
ret->rval_type = BASIC_RVAL_CONST;
|
||||
ret->rval_type = BASIC_RVAL_CONST_INT;
|
||||
}
|
||||
} else if ( token != NULL && ret->type == 0x0 ) {
|
||||
if ( *token == '+' ) {
|
||||
ret->type = BASIC_OPTP_ADD;
|
||||
} else if ( *token == '*' ) {
|
||||
ret->type = BASIC_OPTP_MUL;
|
||||
} else if ( *token == '-' ) {
|
||||
ret->type = BASIC_OPTP_SUB;
|
||||
} else if ( *token == '/' ) {
|
||||
ret->type = BASIC_OPTP_DIV;
|
||||
} else if ( *token == '%' ) {
|
||||
ret->type = BASIC_OPTP_MOD;
|
||||
} else {
|
||||
basic_errno = BASIC_ERR_SYNTAX_GENERAL;
|
||||
return NULL;
|
||||
switch ( *token ) {
|
||||
case '+':
|
||||
ret->type = BASIC_OPTP_ADD;
|
||||
break;
|
||||
case '*':
|
||||
ret->type = BASIC_OPTP_MUL;
|
||||
break;
|
||||
case '-':
|
||||
ret->type = BASIC_OPTP_SUB;
|
||||
break;
|
||||
case '/':
|
||||
ret->type = BASIC_OPTP_DIV;
|
||||
break;
|
||||
case '%':
|
||||
ret->type = BASIC_OPTP_MOD;
|
||||
break;
|
||||
default:
|
||||
/* see if a command will claim this token */
|
||||
if ( strcmp(token, "REM") == 0 ) {
|
||||
ret->type = BASIC_OPTP_CMD;
|
||||
ret->lval_type = BASIC_LVAL_CMDPTR;
|
||||
ret->lval.cmd_ptr = basic_cmd_rem;
|
||||
ret->rval_type = BASIC_RVAL_NONE;
|
||||
return ret;
|
||||
} else if ( strcmp(token, "PRINT") == 0 ) {
|
||||
ret->type = BASIC_OPTP_CMD;
|
||||
ret->lval_type = BASIC_LVAL_CMDPTR;
|
||||
ret->lval.cmd_ptr = basic_cmd_print;
|
||||
ret->rval_type = BASIC_RVAL_PTR;
|
||||
ret->rval.ptr = expbuf;
|
||||
return ret;
|
||||
} else {
|
||||
basic_errno = BASIC_ERR_SYNTAX_GENERAL;
|
||||
return NULL;
|
||||
break;
|
||||
}
|
||||
}
|
||||
} else {
|
||||
basic_errno = BASIC_ERR_SYNTAX_GENERAL;
|
||||
@@ -209,6 +303,12 @@ struct basic_expr *basic_parse_expr(char *expbuf)
|
||||
return ret;
|
||||
}
|
||||
|
||||
/***************************************************************
|
||||
*
|
||||
* Basic REPL
|
||||
*
|
||||
***************************************************************/
|
||||
|
||||
void basic_print_var(struct basic_variable *var)
|
||||
{
|
||||
char decimal[32];
|
||||
@@ -229,20 +329,14 @@ void basic_repl(basic_program *program)
|
||||
{
|
||||
char keybuff[512];
|
||||
char outbuff[128];
|
||||
char decimal[2];
|
||||
|
||||
struct basic_expr *expr;
|
||||
struct basic_variable result;
|
||||
|
||||
decimal[0] = 0;
|
||||
decimal[1] = 0;
|
||||
|
||||
blankScreen();
|
||||
setCursorPosition(0, 0);
|
||||
_cputs("Piquant Basic v0.1\n\n");
|
||||
|
||||
while ( 1 ) {
|
||||
memset((void *)&result, 0x00, sizeof(struct basic_variable));
|
||||
expr = NULL;
|
||||
_cputs("> ");
|
||||
|
||||
@@ -256,23 +350,17 @@ void basic_repl(basic_program *program)
|
||||
/* Evaluate */
|
||||
expr = basic_parse_expr((char *)&keybuff);
|
||||
if ( expr == NULL ) {
|
||||
_cputs("Error: ");
|
||||
decimal[0] = dtoa(basic_errno);
|
||||
_cputs((char *)&decimal);
|
||||
_cputs("\n");
|
||||
basic_report_error();
|
||||
basic_errno = 0;
|
||||
continue;
|
||||
}
|
||||
|
||||
basic_solve_expr(expr, &result);
|
||||
if ( basic_errno != 0 ) {
|
||||
_cputs("Error: ");
|
||||
decimal[0] = dtoa(basic_errno);
|
||||
_cputs((char *)&decimal);
|
||||
_cputs("\n");
|
||||
} else {
|
||||
/* Print */
|
||||
basic_print_var(&result);
|
||||
if ( expr->lval_type == BASIC_LVAL_CMDPTR ) {
|
||||
if ( expr->lval.cmd_ptr == NULL ) {
|
||||
basic_errno = BASIC_ERR_INTERNAL_NULLPOINTER;
|
||||
basic_report_error();
|
||||
basic_errno = 0;
|
||||
} else {
|
||||
(*expr->lval.cmd_ptr)(expr->rval.ptr);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
37
src/basic.h
37
src/basic.h
@@ -1,25 +1,30 @@
|
||||
#ifndef _BASIC_H_
|
||||
#define _BASIC_H_
|
||||
|
||||
#define BASIC_MAX_LINES 1000
|
||||
#define BASIC_MAX_LINE_LENGTH 256
|
||||
/* Per MS BASIC-80 ref page 1-2 */
|
||||
#define BASIC_MAX_LINES sizeof(int)
|
||||
#define BASIC_MAX_LINE_LENGTH 255
|
||||
|
||||
#define BASIC_OPTP_ADD 1 /* Add */
|
||||
#define BASIC_OPTP_SUB 2 /* Subtract */
|
||||
#define BASIC_OPTP_MUL 3 /* Multiply */
|
||||
#define BASIC_OPTP_DIV 4 /* Divide */
|
||||
#define BASIC_OPTP_MOD 5 /* Modulus */
|
||||
#define BASIC_OPTP_EQL 6 /* Equality test */
|
||||
#define BASIC_OPTP_ASN 7 /* Assignment */
|
||||
#define BASIC_OPTP_STOR 8 /* Store line for later */
|
||||
#define BASIC_OPTP_NONE 0 /* No operation (only valid when LVAL is CONST and no RVAL) */
|
||||
#define BASIC_OPTP_ADD 1 /* Add */
|
||||
#define BASIC_OPTP_SUB 2 /* Subtract */
|
||||
#define BASIC_OPTP_MUL 3 /* Multiply */
|
||||
#define BASIC_OPTP_DIV 4 /* Divide */
|
||||
#define BASIC_OPTP_MOD 5 /* Modulus */
|
||||
#define BASIC_OPTP_EQL 6 /* Equality test */
|
||||
#define BASIC_OPTP_ASN 7 /* Assignment */
|
||||
#define BASIC_OPTP_STOR 8 /* Store line for later */
|
||||
#define BASIC_OPTP_CMD 9 /* BASIC command (PRINT, FOR, REM, etc)*/
|
||||
|
||||
#define BASIC_LVAL_EXPR 0
|
||||
#define BASIC_LVAL_VAR 1
|
||||
#define BASIC_LVAL_CONST 2
|
||||
#define BASIC_LVAL_CONST_INT 2
|
||||
#define BASIC_LVAL_CMDPTR 3
|
||||
#define BASIC_RVAL_EXPR 3
|
||||
#define BASIC_RVAL_VAR 4
|
||||
#define BASIC_RVAL_CONST 5
|
||||
#define BASIC_RVAL_CONST_INT 5
|
||||
#define BASIC_RVAL_PTR 6 /* Only used internally */
|
||||
#define BASIC_RVAL_NONE 7
|
||||
|
||||
#define BASIC_PARSE_FOUND_LVAL 1
|
||||
#define BASIC_PARSE_FOUND_RVAL 2
|
||||
@@ -62,6 +67,7 @@ union basic_value {
|
||||
*/
|
||||
char *str;
|
||||
void *ptr;
|
||||
void (*cmd_ptr)(void *data);
|
||||
};
|
||||
typedef union basic_value basic_value;
|
||||
|
||||
@@ -94,5 +100,12 @@ extern int basic_errno;
|
||||
void basic_repl(basic_program *program);
|
||||
basic_expr *basic_parse_expr(char *);
|
||||
int basic_solve_expr(struct basic_expr *expr, struct basic_variable *result);
|
||||
char *tokenizer_token(void);
|
||||
|
||||
void basic_cmd_rem(void *data);
|
||||
void basic_cmd_print(void *data);
|
||||
|
||||
void basic_print_var(basic_variable *var);
|
||||
void basic_report_error();
|
||||
|
||||
#endif /* _BASIC_H_ */
|
||||
|
||||
Reference in New Issue
Block a user