Skip to content

Extending the language

Andreas AFENTAKIS edited this page Dec 18, 2025 · 6 revisions

Extending the language.

There are two ways to extend the FBASIC programming language. 

  1. To add a library with new statements and functions.
  2. To add statements, functions and variable values before invoke the interpreter via C# code.  
  3. To add new statements only in the main code of the interpreter (non recommended)

Adding a library 

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());

Use the FAST.FBasic.LibraryToolKit package

The FAST.FBasic Library Toolkit is your gateway to extending the FAST.FBasic programming language with your own libraries composed of new statements and functions. If you want to augment FAST.FBasic's capabilities and tailor the language environment to your specific needs, this Toolkit provides a streamlined, modular framework to author, package, and distribute your customized libraries.

With the FAST.FBasic Library Toolkit, you can implement and integrate your own language extensions—be it new text processing commands, data manipulation routines, decision logic components, or any specialized functionality—directly into the FAST.FBasic ecosystem. This allows you to elevate FAST.FBasic well beyond its core features, adapting it seamlessly to your projects and workflows.

The Toolkit is designed with developer convenience and flexibility in mind. It facilitates the creation of self-contained libraries that expose new statements and functions, making them available to all FAST.FBasic-powered environments once installed. This means you can write high-level, domain-specific language extensions without modifying the core interpreter or compiler code.

Getting started is straightforward. The Library Toolkit is distributed as a NuGet package, making installation into your development environment quick and simple. Once installed, you can begin authoring libraries immediately, leveraging comprehensive support tools and templates that guide you through defining syntax, implementing execution logic, and integrating with FAST.FBasic’s runtime.

Key features of the Toolkit include:

  • Modular Architecture: Author multiple independent libraries or combine them for layered functionality.
  • Custom Statement and Function Support: Easily define new language constructs with flexible syntax and semantics.
  • Runtime Integration: Plug your libraries directly into the FAST.FBasic interpreter for instant availability.
  • Extensibility: Encourage continual library improvement and expansion as your application demands evolve.
  • Documentation and Samples: Benefit from detailed guides and example code to accelerate development.

By using this Toolkit, developers create personalized language environments tailored exactly to their domain, enhancing productivity and reducing the need for external tooling or workaround scripts.

In essence, if you have the ambition to customize and grow the FAST.FBasic language beyond its initial scope, the FAST.FBasic Library Toolkit is your essential starting point. Download the NuGet package today, and start creating powerful, reusable libraries that empower you and your teams with bespoke programming capabilities in a well-supported, professional framework.

This Toolkit embodies the spirit of modularity and community-based language evolution, fostering an ecosystem where FAST.FBasic continuously adapts to new challenges, propelled by contributions from developers like you.

Embrace the opportunity to extend FAST.FBasic—use the FAST.FBasic Library Toolkit to bring your ideas to life in code and expand the horizon of what this language can do for you.

For more information about the ToolKit package


Adding before program interpretation

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}");

Adding new (in main code) statements.   

There are seven (7) steps to add a new statement to the FBASIC Core interpreter core code:

  1. Use Interpreter_Statement.cs or/and the partial class Interpreter
  2. Implement a method like:
    void MYSTATEMENT(){}
    to code how the statement works.
  3. Edit Token.cs (Project: FAST.FBasic.LibraryToolkit) and add a new value to enumeration: Token
  4. Edt Lexer_GetToken find the GetToken() and add a CASE to the long SWITCH converting the MYSTATEMENT to token.
  5. In the InterpreterHelper.cs (Project: FAST.FBasic.LibraryToolkit) find the isStatement() and add CASE to the SWITCH if the added token is statement.
  6. Edit code file  Interpreter_BuildStatement and at method BuildStatement() add a CASE to the long SWITCH to map the Token with the implementing method
  7. Add entry for the new statement to FBasicManual.md or to Any other Documentation Page, to replease news, to wiki and update any further documentation.

Clone this wiki locally