Interactive interpreter: reads lines from standard input, passes each to g2basic_parse(), and prints error messages.
Interactive interpreter: reads lines from standard input, passes each to g2basic_parse(), and prints error messages. Build it with the default standalone CMake configuration and run build/examples/interactive/g2basic-interactive.
#include <ctype.h>
#include <stdio.h>
#include <string.h>
#define MAX_LINE_LENGTH 256
static void basic_print(const char* str) {
printf("%s", str);
fflush(stdout);
}
int main(int argc, char* argv[]) {
(void)argc;
(void)argv;
char line[MAX_LINE_LENGTH];
printf(
"G2BASIC Interpreter with line numbers. Ctrl-C/Ctrl-D/Ctrl-Z to "
"exit.\n\n");
while (1) {
printf("> ");
if (!fgets(line, sizeof(line), stdin))
break;
line[strcspn(line, "\r\n")] = 0;
if (strlen(line) == 0) {
continue;
}
double val;
const char* error = NULL;
if (error) {
printf("Error: %s\n", error);
}
}
return 0;
}
Public API of the G2Basic interpreter.
int g2basic_parse(const char *input, double *result, const char **error)
Process one line of input.
void g2basic_init(void(*print_func)(const char *str))
Reset the interpreter and set the text output callback.