-
Notifications
You must be signed in to change notification settings - Fork 0
Extending the language
Andreas AFENTAKIS edited this page Oct 5, 2025
·
6 revisions
There are two ways to extend the FBASIC programming language.
- To add a library with new statements and functions.
- To add statements, functions and variable values before invoke the interpreter via C# code.
- To add new statements only in the main code of the interpreter (non recommended)
Library is a set of statements and/or functions. To create a new library, create a new class that implements the IFBasicLibrary interface. The are only one method to implement like the following example:
public class MyFBasicLibrary : IFBasicLibrary
{
public void InstallAll(Interpreter interpreter)
{
interpreter.AddFunction("FUN", Fun);
interpreter.AddStatement("STATE", State);
interpreter.SetVar("PI",3.14159);
}
public static Value Fun(Interpreter interpreter, List<Value> args)
{
...
}
public static void State(Interpreter interpreter)
{
...
}
}Note: The implementation of the functions and the statements are static methods.
To add the library to the interpreter use the AddLibrary() method:
Interpreter interp = new();
interp.AddLibrary(new FBasicStringFunctions());Before a program execution, the developer has the ability to add Variables with values, Statements and Functions.
Example 1:
Interpreter interp = new();
interp.AddFunction("FUN", MyClass.Fun);
interp.AddStatement("STATE", MyClass.State);
interp.SetVar("PI",3.14159);
...
... code to run (interprate) a program
...Example 2:
executionResult result;
string basProgramFile = Directory.GetFiles(programsFolder, startupName).FirstOrDefault();
result = fBasicHelper.run(env, basProgramFile, (interp) =>
{
interp.SetVar("table.column", new Value("myColumn1"));
interp.AddLibrary(new FBasicStringFunctions());
});
if (result.hasError)
{
Console.WriteLine(result.errorText);
if (!string.IsNullOrEmpty(result.errorSourceLine)) Console.WriteLine(result.errorSourceLine);
}
else Console.WriteLine($"Result: {result.value}");There are seven (7) steps to add a new statement to the FBASIC Core interpreter core code:
- Use Interpreter_Statement.cs or/and the partial class Interpreter
- Implement a method like:
void MYSTATEMENT(){}
to code how the statement works. - Edit Token.cs and add a new value to enumeration: Token
- Edt Lexer.cs find the GetToken() and add a CASE to the long SWITCH converting the MYSTATEMENT to token.
- In the Lexer.cs find the isStatement() and add CASE to the SWITCH if the added token is statement.
- Edit code file interpreter_Elements.cs and at method Statement() add a CASE to the long SWITCH to map the Token with the implementing method
- Add entry for the new statement to FBasicManual.md