vlmillet / Phantom.Code

Phantom extension allowing C++ evaluation at run-time, custom languages and AST building above reflection

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Phantom.Code

Phantom extension providing C++ evaluation at run-time, an experimental C++ Lite runtime language and the ability to create custom languages from AST built upon reflection.

Get Phantom.JIT to convert the slow AST evaluation into a highly performant asm JIT code and debug it with on-the-fly PDB files.

Installation

First download, clone or fork Phantom.

Then just copy (or mklink) every content of this repo to your Phantom folder.

Then run (again) the Premake-vs20XX.bat in the Phantom folder

C++ 'Lite'

This module comes with an experimental but already powerful and handy language called C++ lite.

It is a subset of C++ with an import-like system for run-time C++ compilation.

Here is an extract from the HelloWorld.Code sample provided with the sources (the code below is built and ran at run-time).

import phantom.lang.Class;
import phantom.lang.Application;
import phantom.lang.CppLite;
import phantom.String;

class MyClass
{
public:
    const char* HelloWorld()
    {
        helloWorldTriggered = true;
        return "HelloWorld !";
    }

protected:
    bool helloWorldTriggered = false;
};

int main(int argc, char** argv)
{
    // find the class by full qualified name through the Application singleton (which exists only during Main scope)
    auto metaClass = classof(MyClass);

    // use RAII + meta class to build a temporary instance on the stack

    auto instance = metaClass->allocate(); // alloc instance memory

    metaClass->construct(instance); // construct instance

    // get the value of the 'helloWorldTriggered' field

    bool result = false;

    while (!result)
    {
        const char* helloWorldTxt = nullptr;
         
        // invoke the 'HelloWorld()' method
        metaClass->getMethod("HelloWorld()")->invoke(instance, nullptr, &helloWorldTxt);

        // get the 'helloWorldTriggered' field value to 'result'
        metaClass->getField("helloWorldTriggered")->getValue(instance, &result);

        // use runtime expression evaluation to build some C++ expression ..
        auto cppexp = CppLite::Get()->expression(String("HelloWorldPrint(\"") + helloWorldTxt + "\")");
        
        // .. and evaluate it
        assert(cppexp);

        cppexp->eval();
    }

    metaClass->destroy(instance); // construct instance

    metaClass->deallocate(instance); // free instance memory

    return 0;
}

Disclaimers

  • At the moment, function templates are not supported, only class templates (there are generic alternatives which might be enough for pure scripting purpose, which the language was targeted for).
  • There is also no vararg function available yet.
  • Some modern C++ features are supported but not all (for each, auto, r values).
  • There is no real road-map yet as it evolves depending on the current needs of the game studio it is used in.

About

Phantom extension allowing C++ evaluation at run-time, custom languages and AST building above reflection

License:MIT License


Languages

Language:C++ 99.9%Language:C 0.0%Language:Lua 0.0%