C++ Integration guide

Lean how to embed & extend with rookie.

Rookie provides C++ APIs to access entire language features such as AST and runtime-debugging.

Compling

Compile with default compiler
auto src = "a = 1;";
auto b = binding::default_binding();
auto c = compiler::default_compiler(b);

auto out = c.compile(src);
if (out.errors.empty()) {
    printf("Success without error");
}
else {
    for (auto &err : out.errors)
        printf("%s\n", err.message.c_str());
}

Extending your code

Global function

myadd.cpp
b.function(L"myadd", [](value_cref a, value_cref b){
    return a + b;
});
Call `myadd` in rookie
# 5
print( myadd(1, 4) );

Please remember that value_cref means const value &. Every parameter is immutable.

Class

mymath.cpp
class mymath : rkobject<mymath> {
public:
    static void import(binding &b) {
        auto type = type_builder(L"mymath");

        method(type, L"add", &mymath::add);

        b.add_type(type);
    }

    value add(value_cref a, value_cref b) {
        return a + b;
    }
}

b.import<mymath>();
Call `mymath::add` in rookie
m = mymath.new();

# 5
print( m.add(1, 5) );

Conversion between Rookie and C++

Primitive type conversion
int   cpp_v = rk2int(v);
value rk_v  = int2rk(1234);
Object conversion
rkstring cpp_v = rk2obj(v, rkstring*);
value    rk_v  = obj2rk(cpp_v, string);
TYPE Rookie → C++ C++ → Rookie
integer rk2int int2rk
std::string rk2str str2rk
wchar_t* rk2cstr cstr2rk
object rk2ptr ptr2rk

Embedding

Run
runner(*out.p, b).execute();

Reflection

Compile time reflection

astr class provides AST level reflection methods.

Reflection
for (auto &method : astr::all_methods(root))
    printf("%S\n", method.name.c_str());

Debugging

Supported debugging features

  • Pause and REPL (TODO)
  • Step-by-step (Instruction level)
  • Step-by-step (Code level, line)
debugger dbger(pdb);
runner r(p)
    .attach_debugger(dbger);