I want something in between an interpreter and a pile of sscanf's.

Feel free to talk about any other retro stuff here including Commodore, Sinclair, Atari, Amstrad, Apple... the list goes on!
Post Reply
rje
Posts: 1263
Joined: Mon Apr 27, 2020 10:00 pm
Location: Dallas Area

I want something in between an interpreter and a pile of sscanf's.

Post by rje »


Good morning retro programmers.

I'm writing in C with CC65 on the X16.

In at least four projects, I've wanted to process user input.  In two cases, I went with a full tokenizer.  In the other two cases, there's only a limited set of interactions.  But they've got enough complexity that I'm doing sscanf and if/then's on string comparison.

I'm wondering if there's something in between that can process typed user input, but isn't a "full" tokenizer?

Maybe a little tokenizer can just be "directly" (?) connected to execution.  I dunno.  Thoughts?

SlithyMatt
Posts: 913
Joined: Tue Apr 28, 2020 2:45 am

I want something in between an interpreter and a pile of sscanf's.

Post by SlithyMatt »


I haven't tried cc65's scanf, but I'd imagine it's not terribly efficient. If you want to stick with clib, you gen always try gets and strtok and parse it out yourself, which is usually more efficient. Or you can just use the C wrappers for the Kernal GETIN (get characters as they are typed) and CHRIN (works like BASIC INPUT, waiting for Enter then reading characters from a buffer) routines, which will probably be even more efficient.

TomXP411
Posts: 1760
Joined: Tue May 19, 2020 8:49 pm

I want something in between an interpreter and a pile of sscanf's.

Post by TomXP411 »


It's hard to give great suggestions without a better idea of the context, but I'm going to lean toward tokenization as the best way to handle most scenarios. However, you don't want a pile of IF statements. Instead, you should build a list of function pointers. Each token value would have a pointer to a function that executes that token's code. 

So say you're writing a text adventure, and you have commands like OPEN, CLOSE, and TAKE

string token_text[3] = "OPEN", "CLOSE", 'TAKE";

void (*token_func[3])() = {cmd_open, cmd_close, cmd_take};


or something like that. 

Then you can execute the token functions with something like:

(*func_ptr[index])();

This is taken partially from this article on Stack Exchange:

https://stackoverflow.com/questions/252748/how-can-i-use-an-array-of-function-pointers

Snickers11001001
Posts: 140
Joined: Wed Jan 20, 2021 6:43 pm

I want something in between an interpreter and a pile of sscanf's.

Post by Snickers11001001 »


I seem to remember that The old C64 'type-in' word processor called Speedscript had something neat in the source code, which it treated as akin to the "ON GOTO" in BASIC, only implemented in assembler.   It used a command table to manage a count (e.g., found the key pressed on the 5th try so '5' is the command), to get addresses for the destination routines from another table, and stuck the necessary stuff on the stack and then did the RTS to simulate a return from a JSR. 

Post Reply