Thursday 14 July 2011

Regular source files as functions

Well, I came to the obvious conclusion that compile-time functions could be functions. But what about regular code too?

main.wc:
namespace bar {
    foo() {}
}
main() {
     bar::foo();
}


void ExportedMainWC(Compiler* comp) {
    static FunctionAST fooast;
    static CallAST callfooast(fooast);
    static FunctionAST main(fooast);
    namespace* ptr;
    if(comp->global_namespace["bar"] != comp->global_namespace.end())
        ptr = comp->global_namespace["bar"];
    else
        ptr = comp->global_namespace["bar"] = new namespace;
    comp->global_namespace.add_function("main", main);
}



The fastest code is machine code. It's kind of like pre-compiled AST. What I'd need to do is add one function for declarations and another for definitions, because I just realized that I completely don't support that in this quick example. But it's still genius.

No comments:

Post a Comment