G2Basic 0.1.1 (1e793fc)
BASIC interpreter for microcontrollers
Loading...
Searching...
No Matches
Execution

Entering program lines, running statements, and running programs. More...

Collaboration diagram for Execution:

Functions

int g2basic_parse (const char *input, double *result, const char **error)
 Process one line of input.
 

Detailed Description

Entering program lines, running statements, and running programs.

Function Documentation

◆ g2basic_parse()

int g2basic_parse ( const char *  input,
double *  result,
const char **  error 
)

Process one line of input.

The line is classified in this order:

  1. LIST, RUN, or NEW as the first word, in any letter case, runs that command. Any text after the command is ignored.
  2. A line starting with a digit begins with a line number from 0 to 65535. With nothing after it, the numbered line is deleted. Otherwise the rest of the line is stored, replacing any line with that number. Stored text is not checked until it runs.
  3. Anything else is executed immediately as one statement.

Because of rule 2, an immediate expression cannot start with a digit: 2 + 3 stores line 2. Write PRINT 2 + 3 or (2 + 3) instead.

RUN executes the stored lines in line-number order. A runtime error stops the program and is reported through the text callback as Error in line N: message; g2basic_parse() still returns 3.

Parameters
inputNUL-terminated line without a line terminator. It is not modified or retained.
[out]resultMust not be NULL. Receives the statement's value for return value 0 (the value of an expression or assignment, otherwise 0), the line number for 1 and 2, and 0 for 3. Unchanged for -1.
[out]errorMust not be NULL. Set only on failure, so initialize the pointed-to value to NULL before the call. The message is static and must not be freed. Some messages are rewritten by the next call. It is not set when the line number is out of range.
Return values
0An immediate statement ran successfully.
1A program line was deleted, or did not exist.
2A program line was stored.
3LIST, RUN, or NEW ran.
-1The immediate statement failed, or the line number is outside 0 to 65535.
double result;
const char* error = NULL;
g2basic_parse("10 FOR I = 1 TO 3", &result, &error); // 2: stored
g2basic_parse("20 PRINT I", &result, &error); // 2: stored
g2basic_parse("30 NEXT I", &result, &error); // 2: stored
g2basic_parse("RUN", &result, &error); // 3: prints 1, 2, 3
if (g2basic_parse("y = undefined_name", &result, &error) < 0) {
printf("Error: %s\n", error); // undefined variable 'undefined_name'
}
int g2basic_parse(const char *input, double *result, const char **error)
Process one line of input.
Examples
interactive/main.c.