Refactored the name of some defined basic parser codes and exposed basic_solve_expr for tests
This commit is contained in:
2
Makefile
2
Makefile
@@ -36,5 +36,5 @@ test:
|
||||
|
||||
.PHONY: clean
|
||||
clean:
|
||||
rm -f boot.bin asm/*o src/*o
|
||||
rm -f boot.bin boot.img kernel.bin asm/*o src/*o
|
||||
cd tests && make clean
|
||||
|
||||
46
src/basic.c
46
src/basic.c
@@ -6,25 +6,11 @@
|
||||
|
||||
struct basic_expr math_expressions[32];
|
||||
int basic_errno;
|
||||
basic_line basic_memory_lines[BASIC_MAX_LINES];
|
||||
|
||||
char _tokenizer_value[BASIC_TOKENIZER_MAX_LENGTH];
|
||||
char *_tokenizer_prev;
|
||||
char *_tokenizer_prev_next;
|
||||
|
||||
basic_line *_basic_store_line(char *line, int lineno)
|
||||
{
|
||||
basic_line *unused = NULL;
|
||||
int i = 0;
|
||||
for ( i = 0; i < BASIC_MAX_LINES; i++ ) {
|
||||
if ( basic_memory_lines[i].content[0] == 0x00 ) {
|
||||
unused = (basic_line *)&basic_memory_lines[i];
|
||||
memcpy(unused->content, line, strlen(line));
|
||||
return unused;
|
||||
}
|
||||
}
|
||||
return NULL;
|
||||
}
|
||||
|
||||
void _tokenizer_init(void)
|
||||
{
|
||||
_tokenizer_prev = NULL;
|
||||
@@ -157,10 +143,6 @@ int basic_solve_expr(struct basic_expr *expr, struct basic_variable *result)
|
||||
result->value.i = BASIC_CONST_FALSE;
|
||||
}
|
||||
break;
|
||||
case BASIC_OPTP_STOR:
|
||||
result->flags = (result->flags | BASIC_VARFLAG_TSTR | BASIC_VARFLAG_INIT);
|
||||
result->value.str = "OK\n";
|
||||
return 0;
|
||||
case BASIC_OPTP_ASN:
|
||||
basic_errno = BASIC_ERR_INTERNAL_UNIMPLEMENTED;
|
||||
return 0;
|
||||
@@ -175,23 +157,21 @@ struct basic_expr *basic_parse_expr(char *expbuf)
|
||||
{
|
||||
struct basic_expr *ret = &math_expressions[0];
|
||||
char *token = NULL;
|
||||
char flags = BASIC_PARSE_FIRSTTOKEN;
|
||||
char flags = 0;
|
||||
/*char *subptr = 0;*/
|
||||
|
||||
_tokenizer_init();
|
||||
memset(ret, 0x0, sizeof(struct basic_expr));
|
||||
|
||||
while ( *expbuf != '\0' ) {
|
||||
if ( *expbuf == ' ' ) {
|
||||
expbuf += 1;
|
||||
continue;
|
||||
}
|
||||
|
||||
expbuf = _tokenize(expbuf, BASIC_TOKENIZER_TOKENS);
|
||||
token = _token_get();
|
||||
if ( isdigit(*token) == 1 ) {
|
||||
if ( flags == BASIC_PARSE_FIRSTTOKEN ) {
|
||||
ret->lval.i = atoi(token);
|
||||
ret->lval_type = BASIC_LVAL_CONST;
|
||||
ret->type = BASIC_OPTP_STOR;
|
||||
ret->rval.ptr = _basic_store_line(expbuf, ret->lval.i);
|
||||
ret->rval_type = BASIC_RVAL_PTR;
|
||||
break;
|
||||
}
|
||||
if ( (ret->type == 0) && (flags & BASIC_PARSE_FOUND_LVAL) == BASIC_PARSE_FOUND_LVAL ) {
|
||||
basic_errno = BASIC_ERR_SYNTAX_MULTIPLE_LVALUES;
|
||||
return NULL;
|
||||
@@ -217,15 +197,6 @@ struct basic_expr *basic_parse_expr(char *expbuf)
|
||||
ret->type = BASIC_OPTP_DIV;
|
||||
} else if ( *token == '%' ) {
|
||||
ret->type = BASIC_OPTP_MOD;
|
||||
} else if ( *token == '=' ) {
|
||||
if ( flags == BASIC_PARSE_FIRSTTOKEN ) {
|
||||
/* A statement with = as the first character is processed in
|
||||
* immediate mode even if the expression begins with a digit and
|
||||
* only performs arithmetic. aka calculator mode.
|
||||
* This logic is only here to count the = as the first token so
|
||||
* proceeding digits don't get counted as line numbers. */
|
||||
flags = 0;
|
||||
}
|
||||
} else {
|
||||
basic_errno = BASIC_ERR_SYNTAX_GENERAL;
|
||||
return NULL;
|
||||
@@ -266,7 +237,6 @@ void basic_repl(basic_program *program)
|
||||
decimal[0] = 0;
|
||||
decimal[1] = 0;
|
||||
|
||||
memset((char *)&basic_memory_lines, 0x00, BASIC_MAX_LINES*BASIC_MAX_LINE_LENGTH);
|
||||
blankScreen();
|
||||
setCursorPosition(0, 0);
|
||||
_cputs("Piquant Basic v0.1\n\n");
|
||||
|
||||
21
src/basic.h
21
src/basic.h
@@ -25,16 +25,16 @@
|
||||
#define BASIC_PARSE_FOUND_RVAL 2
|
||||
#define BASIC_PARSE_FIRSTTOKEN 4
|
||||
|
||||
#define BASIC_ERR_SYNTAX_MULTIPLE_LVALUES 1
|
||||
#define BASIC_ERR_SYNTAX_TOKEN_LENGTH 2
|
||||
#define BASIC_ERR_SYNTAX_GENERAL 3
|
||||
#define BASIC_ERR_SYNTAX_MULTIPLE_RVALUES 4
|
||||
#define BASIC_ERR_INVALID_ARGUMENTS 5
|
||||
#define BASIC_ERR_INTERNAL_NULLPOINTER 6
|
||||
#define BASIC_ERR_INTERNAL_MEMORY 7
|
||||
#define BASIC_ERR_INTERNAL_UNDEFINED_BEHAVIOR 8
|
||||
#define BASIC_ERR_INTERNAL_UNIMPLEMENTED 9
|
||||
#define BASIC_ERR_MATH_DBZ 10
|
||||
#define BASIC_ERR_SYNTAX_MULTIPLE_LVALUES 1
|
||||
#define BASIC_ERR_SYNTAX_TOKEN_LENGTH 2
|
||||
#define BASIC_ERR_SYNTAX_GENERAL 3
|
||||
#define BASIC_ERR_SYNTAX_MULTIPLE_RVALUES 4
|
||||
#define BASIC_ERR_INVALID_ARGUMENTS 5
|
||||
#define BASIC_ERR_INTERNAL_NULLPOINTER 6
|
||||
#define BASIC_ERR_INTERNAL_MEMORY 7
|
||||
#define BASIC_ERR_INTERNAL_UNDEFINED_BEHAVIOR 8
|
||||
#define BASIC_ERR_INTERNAL_UNIMPLEMENTED 9
|
||||
#define BASIC_ERR_MATH_DBZ 10
|
||||
|
||||
#define BASIC_TOKENIZER_TOKENS "+-/%*="
|
||||
#define BASIC_TOKENIZER_MAX_LENGTH 512
|
||||
@@ -93,5 +93,6 @@ 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);
|
||||
|
||||
#endif /* _BASIC_H_ */
|
||||
|
||||
@@ -1,29 +1,43 @@
|
||||
#include "types.h"
|
||||
#include "basic.h"
|
||||
#include "string.h"
|
||||
#include <stdio.h>
|
||||
|
||||
/* extern basic_line basic_memory_lines[BASIC_MAX_LINES]; */
|
||||
|
||||
int main(void)
|
||||
{
|
||||
struct basic_expr *expr;
|
||||
struct basic_line *line;
|
||||
/*struct basic_line *retline;
|
||||
struct basic_line *arrayline;*/
|
||||
|
||||
/* Store a line */
|
||||
expr = basic_parse_expr("10 =1 + 1");
|
||||
/* expr = basic_parse_expr("10 =1 + 1");
|
||||
if ( expr == NULL ) return 1;
|
||||
if ( expr->lval_type != BASIC_LVAL_CONST ) return 2;
|
||||
if ( expr->rval_type != BASIC_RVAL_PTR ) return 3;
|
||||
if ( expr->lval.i != 10 ) return 4;
|
||||
if ( expr->rval.ptr == NULL ) return 5;
|
||||
if ( expr->type != BASIC_OPTP_STOR ) return 6;
|
||||
|
||||
*/
|
||||
/* Parse the stored line */
|
||||
line = (basic_line *)expr->rval.ptr;
|
||||
expr = basic_parse_expr(line->content);
|
||||
if ( expr == NULL ) return 1;
|
||||
if ( expr->lval_type != BASIC_LVAL_CONST ) return 2;
|
||||
if ( expr->rval_type != BASIC_RVAL_CONST ) return 3;
|
||||
if ( expr->lval.i != 1 ) return 4;
|
||||
if ( expr->rval.i != 1 ) return 5;
|
||||
if ( expr->type != BASIC_OPTP_ADD ) return 6;
|
||||
|
||||
/* retline = (basic_line *)expr->rval.ptr;
|
||||
expr = basic_parse_expr(retline->content);*/
|
||||
expr = basic_parse_expr("1 + 1");
|
||||
if ( expr == NULL ) return 7;
|
||||
if ( expr->lval_type != BASIC_LVAL_CONST ) return 8;
|
||||
if ( expr->rval_type != BASIC_RVAL_CONST ) return 9;
|
||||
if ( expr->lval.i != 1 ) return 10;
|
||||
if ( expr->rval.i != 1 ) return 11;
|
||||
if ( expr->type != BASIC_OPTP_ADD ) return 12;
|
||||
|
||||
/*
|
||||
arrayline = &basic_memory_lines[0];
|
||||
if ( retline != arrayline) return 13;
|
||||
printf("Line 10 in memory is %d : %s (%p)\n", arrayline->lineno, (char *)&arrayline->content, (void *)arrayline->nextline);
|
||||
if ( arrayline->lineno != 10 ) return 14;
|
||||
if ( strcmp((char *)&arrayline->content, "=1 + 1") != 0) return 15;
|
||||
if ( arrayline->nextline != NULL ) return 16;
|
||||
*/
|
||||
return 0;
|
||||
}
|
||||
Reference in New Issue
Block a user