WIP : Eff it, I left the magic memcpy in. Printing mostly works but it's still broken. Calls to _cputs and _cputsf may mysteriously fail due to some kind of memory corruption I haven't tracked down yet. This commit leaves in a bunch of commented testing code, the next commit will rip them out.

This commit is contained in:
2024-05-12 21:28:27 -04:00
parent f2cab38cf2
commit 683b47f245
4 changed files with 108 additions and 53 deletions

Binary file not shown.

Before

Width:  |  Height:  |  Size: 15 KiB

After

Width:  |  Height:  |  Size: 15 KiB

View File

@@ -16,15 +16,14 @@ char _tokenizer_value[BASIC_TOKENIZER_MAX_LENGTH];
char *_tokenizer_prev;
char *_tokenizer_prev_next;
char basic_memory_lines[BASIC_MAX_LINES][BASIC_MAX_LINE_LENGTH];
char decimal[32];
char const_string_test[255];
void basic_report_error(char *prefix)
{
memset((char *)&decimal, 0x00, 32);
_cputs(prefix);
itoa(basic_errno, (char *)&decimal);
_cputs((char *)&decimal);
memset(decimal, 0x00, 32);
itoa(basic_errno, decimal);
_cputs(decimal);
_cputs("\n");
}
@@ -47,32 +46,25 @@ void basic_cmd_list(void *data)
char *lineptr = NULL;
i = 0;
/* I don't know why, but somehow, this makes the interspersed _cputs() calls below work.
* Take this out, and those lines stop working.
*
* WHAT. THE. FUCK.
*
*
* memcpy((char *)&const_string_test, (char *)" \0", strlen(" \0"));
* _cputs((char *)&const_string_test);
/* I don't know why, but somehow, this memcpy fixes the bug where _cputs and _cputsf stops properly processing
* constant character strings passed as the format string. I've tried lots and lots of things and I still don't
* know why this fixes the bug.
*/
/* 12 characters of nothing? This does the same thing as the cputs above.
* But cputs is still broken after this. It's the memcpy that is fixing whatever is wrong.
*/
/*for ( i = 0; i < 12; i++ ) {
_putch(' ');
advanceCursor();
}*/
memcpy(const_string_test, (char *)" \0", strlen(" \0"));
for ( i = 0; i <= limit ; i++ ) {
if ( basic_memory_lines[i][0] != 0 ) {
itoa(i+1, (char *)&decimal);
_cputs((char *)&decimal);
_cputsf("%d ", (char *)(i+1));
/*itoa(i+1, decimal);
_cputs(decimal);
_putch(' ');
advanceCursor();
_cputs(basic_memory_line_address(i));
advanceCursor();*/
_cputsf("%s\n", basic_memory_lines[i]);
/*_cputs(" ");
_cputs(basic_memory_lines[i]);
_putch('\n');
_cursor_x = 80;
advanceCursor();
_cputs("\n");*/
}
}
}
@@ -142,7 +134,7 @@ char *_tokenize(char *ptr, char *token)
orig = ptr;
numtokens = strlen(token);
memset(&_tokenizer_value, 0x00, BASIC_TOKENIZER_MAX_LENGTH);
memset(_tokenizer_value, 0x00, BASIC_TOKENIZER_MAX_LENGTH);
while ( *ptr != 0x0 ) {
/*for ( tokenptr = token ; *tokenptr != 0x00; tokenptr += 1) {
if ( *ptr == *tokenptr) {
@@ -163,14 +155,14 @@ _tokenize_copy:
basic_errno = BASIC_ERR_SYNTAX_TOKEN_LENGTH;
return NULL;
}
memcpy((void *)&_tokenizer_value, (void *)orig, len);
memcpy(_tokenizer_value, (void *)orig, len);
_tokenizer_prev_next = (ptr + 1);
return ptr;
}
char *tokenizer_token(void)
{
return (char *)&_tokenizer_value;
return _tokenizer_value;
}
/***************************************************************
@@ -179,11 +171,6 @@ char *tokenizer_token(void)
*
***************************************************************/
char *basic_memory_line_address(int lineno)
{
return ((char *)&basic_memory_lines) + (lineno * BASIC_MAX_LINE_LENGTH);
}
int basic_memory_line_store(char *content, int lineno)
{
int i = 0;
@@ -195,7 +182,7 @@ int basic_memory_line_store(char *content, int lineno)
return 1;
}
dest = basic_memory_line_address(lineno);
/*dest = basic_memory_line_address(lineno);*/
if ( lineno > basic_last_stored_lineno ) {
/*
* basic_last_stored_lineno only every grows upward (even if you go back and edit a line or insert one).
@@ -208,14 +195,14 @@ int basic_memory_line_store(char *content, int lineno)
/*ptr = basic_memory_line_address(i);*/
/*memset(ptr, 0x00, BASIC_MAX_LINE_LENGTH);*/
}
basic_last_stored_lineno = lineno;
}
lineno -= 1;
i = (int) memcpy(basic_memory_line_address(lineno), content, strlen(content));
i = (int) memcpy(basic_memory_lines[lineno], content, strlen(content));
if ( i == 0 ) {
basic_errno = BASIC_ERR_INTERNAL_NULLPOINTER;
return 1;
}
basic_last_stored_lineno = lineno;
}
int basic_run_line_v2(char *codeline, int repl_mode)
@@ -238,7 +225,7 @@ int basic_run_line_v2(char *codeline, int repl_mode)
/* is it a line number? If so, store it and do nothing else.*/
if ( isdigit(*token) ) {
memset((char *)&decimal, 0x00, 32);
memset(decimal, 0x00, 32);
lineno = atoi(token);
basic_memory_line_store(buffptr, lineno);
return 0;
@@ -258,8 +245,11 @@ int basic_run_line_v2(char *codeline, int repl_mode)
/* Report errors */
if ( basic_errno != 0 ) {
/*_cputs(token);*/
basic_report_error("Parsing error: ");
/*_cputs("Last parsed token: ");
_cputs(token);
_cputs("\n");*/
_cputsf("Parsing error: %d", basic_errno);
return 1;
}
/* Move to the next thing in the line and process it */
@@ -307,12 +297,13 @@ void basic_repl(void)
blankScreen();
setCursorPosition(0, 0);
basic_repl_mode = 1;
basic_memory_line_store("LIST", 10);
basic_memory_line_store("PRINT HELLO\n", 20);
basic_memory_line_store("PRINT WORLD\n", 30);
basic_memory_line_store("PRINT GOODBYE\n", 40);
/*
* basic_repl_mode = 1;
* basic_memory_line_store("LIST", 10);
* basic_memory_line_store("PRINT HELLO\\n", 20);
* basic_memory_line_store("PRINT WORLD\\n", 30);
* basic_memory_line_store("PRINT GOODBYE\\n", 40);
*/
_cputs("Piquant Basic v0.1\n");
_cputs("READY\n");
@@ -330,9 +321,9 @@ void basic_repl(void)
ptr = _tokenize((char *)&basic_memory_lines[39], " ");
token = tokenizer_token();
basic_cmd_print(ptr);
basic_run_line_v2((char *)&basic_memory_lines[19], 1);
basic_run_line_v2((char *)&basic_memory_lines[29], 1);
basic_run_line_v2((char *)&basic_memory_lines[39], 1);*/
basic_run_line_v2(basic_memory_lines[19], 1);
basic_run_line_v2(basic_memory_lines[29], 1);
basic_run_line_v2(basic_memory_lines[39], 1);*/
basic_repl_mode = 0;
}
_cputs("\n> ");

View File

@@ -6,6 +6,8 @@
#include <stdio.h>
#endif
char decimal[32];
void _putch(char c)
{
#ifndef __GNUC__
@@ -32,12 +34,70 @@ void _cputs(char *ptr)
if ( ptr == NULL ) {
return;
}
while ((char)*ptr != 0x0) {
_putch((char)*ptr);
while (*ptr != 0x0) {
if (*ptr == '\\') {
ptr += 1;
switch ( *ptr ) {
case 'n':
_putch('\n');
_cursor_x = 80;
advanceCursor();
break;
}
} else {
_putch(*ptr);
if (*ptr == '\n') {
_cursor_x = 80;
}
advanceCursor();
}
ptr += 1;
}
return;
}
void _cputsf(char *ptr, char *data)
{
if ( ptr == NULL || data == NULL) {
return;
}
/*_cputs("_cputsf called with ptr : ");
memset(decimal, 0x00, 32);
itoa((int)ptr, decimal);
_cputs(decimal);
_cputs(" (");
_cputs(ptr);
_cputs(") length ");
memset(decimal, 0x00, 32);
itoa(strlen(ptr), decimal);
_cputs(decimal);
_cputs(" data ");
memset(decimal, 0x00, 32);
itoa((int)data, decimal);
_cputs(decimal);
_cputs("\n");*/
while (*ptr != 0) {
if ( *ptr == '%' ) {
ptr += 1;
switch ( *ptr ) {
case 'd':
memset(decimal, 0x00, 32);
itoa((int)data, decimal);
_cputs(decimal);
break;
case 's':
_cputs((char *)data);
break;
}
} else {
_putch(*ptr);
if (*ptr == '\n') {
_cursor_x = 80;
}
advanceCursor();
}
ptr += 1;
}
return;

View File

@@ -4,6 +4,10 @@
void _putch(char _c);
char *_cgets(char *d);
void _cputs(char *d);
void _cputsf(char *d, char *data);
char _getkey(char *dest);
extern char decimal[32];
#endif /* _CONIO_H_ */