Goose  Artifact [b7ee83987c]

Artifact b7ee83987c94c9ea06c0c720c4d90952ca91b9db3af0a5ad00749705410bda74:

  • File bs/builtins/api/compiler.cpp — part of check-in [3c074f1b7d] at 2019-07-30 22:05:49 on branch trunk —
    • Builtin functions are now explicitely marked if they need to be evaluated eagerly.
    • Using errors out if the expression doesn't evaluate to a constant.
    (user: achavasse size: 5169)

#include "builtins/builtins.h"
#include "parse/parse.h"
#include "execute/execute.h"

using namespace empathy;

namespace empathy::builtins
{
    void SetupApiCompiler( Env& e )
    {
        weak_ptr< Env > pEnv = e.shared_from_this();

        RegisterBuiltinFunc< Eager< Value > ( Value, string ) >( e, "ExternalFunction"_sid,
            [pEnv]( const Value& f, const string& symbol )
            {
                auto ft = FromValue< FuncType >( f );
                if( !ft )
                    return ToValue( "error"s );

                return ToValue( BuildExternalFunc( *ft, symbol ) );
            } );


        RegisterBuiltinFunc< Eager< uint64_t > ( string ) >( e, "ExecuteFile"_sid,
            [pEnv]( const string& filename ) -> uint64_t
            {
                auto pFN = make_shared< string >( filename );

                ifstream sourcefile( pFN->c_str() );
                if( !sourcefile.good() )
                {
                    cout << "can't open '" << *pFN << "'\n";
                    return 0;
                }

                sema::Context c( pEnv.lock(), RootIdentity(), GetValueType< uint64_t >() );

                auto r = make_shared< parse::Resolver >( make_shared< lex::Lexer >( sourcefile, pFN ), c );
                parse::Parser p( r );

                auto cfgBuilder = make_shared< sema::CFGBuilder >();
                p.setCFGBuilder( cfgBuilder );
                p.setCurrentBB( cfgBuilder->entryBB() );

                p.parseSequence();

                if( !r->eos() )
                {
                    cout << filename << ": syntax error.\n";
                    return 0;
                }

                if( !cfgBuilder->entryBB()->canBeExecuted() )
                {
                    cout << filename << ": can not be executed.\n";
                    return 0;
                }

                execute::VM vm;
                auto result = vm.execute( cfgBuilder->entryBB() );
                if( !result )
                    return 1;

                return *FromValue< uint64_t >( *result );
            } );

        RegisterBuiltinFunc< Eager< Value > ( string, Value, Value ) >( e, "CompileFileToFunction"_sid,
            [pEnv]( const string& filename, const Value& rt, const Value& params ) -> Value
            {
                // Validate those generic value inputs
                if( !rt.isType() )
                {
                    // We don't have builtin optional types so return something completely unexpected instead.
                    // TODO: figure out something more elegant that doens't involve making the optional type builtin.
                    // Perhaps a simpler builtin version, just for the purpose of the builtin api.
                    return ToValue( "error"s );
                }

                if( CheckParamListKind( params ) != ParamListKind::Regular )
                {
                    // We only support compiling files into non-template functions for now.
                    return ToValue( "error"s );
                }

                auto ftype = BuildFuncType( sema::DomainAny(), rt, params );

                // TODO at some point we'll want to pass the base identity to use as a param but
                // let's wait and see how the module and namespace stuff pans out first
                sema::Context c( pEnv.lock(), RootIdentity(), ftype.returnType() );

                auto identity = AppendToVectorTerm( InjectDomainIntoIdentity( RootIdentity(), DomainRunTime() ),
                    TERM( StringId( pEnv.lock()->GetUniqueId() ) ) );
                auto func = BuildFunc( c, ftype, identity, params, make_shared< Vector >(), c );
                const auto& pFuncLLR = func.llr();

                auto pFN = make_shared< string >( filename );

                ifstream sourcefile( pFN->c_str() );
                if( !sourcefile.good() )
                {
                    pFuncLLR->setInvalid();
                    cout << "can't open '" << *pFN << "'\n";
                    return ToValue( func );
                }

                auto r = make_shared< parse::Resolver >( make_shared< lex::Lexer >( sourcefile, pFN ), c );
                parse::Parser p( r );

                auto cfgBuilder = make_shared< sema::CFGBuilder >();
                p.setCFGBuilder( cfgBuilder );
                p.setCurrentBB( cfgBuilder->entryBB() );

                p.parseSequence();

                if( !r->eos() )
                {
                    pFuncLLR->setInvalid();
                    cout << "syntax error.\n";
                    return ToValue( func );
                }

                if( c.returnType() != GetValueType< void >()
                    && !cfgBuilder->areAllBBTerminated() )
                {
                    pFuncLLR->setInvalid();

                    // TODO error management etc
                    cout << "missing return statement in a function with non-void return type.\n";
                    return ToValue( func );
                }

                pFuncLLR->body() = cfgBuilder->entryBB();
                return ToValue( func );
            } );
    }
}