#include "builtins/builtins.h"
#include "parse/parse.h"
#include "execute/execute.h"
#include "compiler.h"
using namespace empathy;
namespace empathy::builtins
{
void SetupApiCompiler( Env& e )
{
weak_ptr< Env > pEnv = e.shared_from_this();
RegisterBuiltinFunc< Eager< void > ( bool ) >( e, "#DiagnosticsEnableTraces"_sid,
[pEnv]( bool enable )
{
DiagnosticsManager::GetInstance().setTraceMode( enable );
} );
RegisterBuiltinFunc< void ( bool ) >( e, "DiagnosticsForceColors"_sid,
[pEnv]( bool enable )
{
DiagnosticsManager::GetInstance().setForceColors( enable );
} );
RegisterBuiltinFunc< Eager< void > ( uint32_t ) >( e, "#SetExecutionBudget"_sid,
[pEnv]( uint32_t budget )
{
static bool used = false;
if( used )
{
DiagnosticsManager::GetInstance().emitErrorMessage( 0,
"SetExecutionBudget can only be used once." );
return;
}
used = true;
execute::VM::SetExecutionBudget( budget );
} );
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 ).setPoison();
return ToValue( BuildExternalFunc( *ft, symbol ) );
} );
RegisterBuiltinFunc< Intrinsic< uint32_t ( string ) > >( e, "ExecuteFile"_sid,
[pEnv]( const Value& fnameval ) -> Value
{
auto filename = *FromValue< string >( fnameval );
auto identity = InjectDomainIntoIdentity( RootIdentity(), DomainCompileTime() );
auto result = Compiler::LoadAndExecuteFile( pEnv.lock(), filename, identity, GetValueType< uint32_t >() );
if( !result )
return ToValue< uint32_t >( 1 );
return *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() )
{
DiagnosticsManager::GetInstance().emitErrorMessage( rt.locationId(),
"#CompileFileToFunction: type expected." );
return PoisonValue();
}
if( CheckParamListKind( params ) != ParamListKind::Regular )
{
DiagnosticsManager::GetInstance().emitErrorMessage( params.locationId(),
"#CompileFileToFunction: template parameter lists are not supported." );
return PoisonValue();
}
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
auto identity = sema::InjectDomainIntoIdentity( builtins::RootIdentity(), DomainRunTime() );
sema::Context c( pEnv.lock(), identity, ftype.returnType() );
auto funcIdentity = AppendToVectorTerm( sema::InjectDomainIntoIdentity( identity, DomainRunTime() ),
TERM( StringId( pEnv.lock()->GetUniqueId() ) ) );
c.env()->addVisibilityRule(
InjectDomainIntoIdentity( identity, ANYTERM( _ ) ),
InjectDomainIntoIdentity( funcIdentity, ANYTERM( _ ) ) );
auto func = BuildFunc( c, ftype, identity, params, nullptr, c );
const auto& pFuncLLR = func.llr();
auto cfg = Compiler::LoadAndParseFile( pEnv.lock(), filename, funcIdentity, ftype.returnType() );
if( cfg->isPoisoned() )
return PoisonValue();
if( c.returnType() != GetValueType< void >()
&& !cfg->areAllBBTerminated() )
{
pFuncLLR->setInvalid();
// TODO the return statement should always be optional at the top level of a file,
// so we should be passed a default return value as parameter and insert return
// statements automatically wherever they may be missing in the cfg.
DiagnosticsManager::GetInstance().emitErrorMessage( 0,
format( "{}: missing return statement in a function with non-void return type.", filename ) );
return ToValue( func ).setPoison();
}
pFuncLLR->body() = cfg;
return ToValue( func );
} );
}
}