#include "builtins/builtins.h"
#include "parse/parse.h"
#include "verify/verify.h"
#include "execute/execute.h"
#include "compile/compiler.h"
using namespace goose;
using namespace goose::compile;
namespace goose::builtins
{
void SetupApiCompiler( Env& e )
{
weak_ptr< Env > pEnv = e.shared_from_this();
RegisterBuiltinFunc< Intrinsic< void ( bool ) > >( e, "#DiagnosticsEnableTraces"_sid,
[pEnv]( auto&& c, const Value& enable )
{
if( !enable.isConstant() )
{
DiagnosticsManager::GetInstance().emitErrorMessage( enable.locationId(),
"#DiagnosticsEnableTraces: the expression doesn't evaluate to a constant." );
return;
}
DiagnosticsManager::GetInstance().setTraceMode( *FromValue< bool >( enable ) );
} );
RegisterBuiltinFunc< void ( bool ) >( e, "DiagnosticsForceColors"_sid,
[pEnv]( bool enable )
{
DiagnosticsManager::GetInstance().setForceColors( enable );
} );
RegisterBuiltinFunc< Intrinsic< void ( bool ) > >( e, "#VerificationTraceMode"_sid,
[pEnv]( auto&& c, const Value& enable )
{
if( !enable.isConstant() )
{
DiagnosticsManager::GetInstance().emitErrorMessage( enable.locationId(),
"#VerificationTraceMode: the expression doesn't evaluate to a constant." );
return;
}
verify::Func::SetTraceMode( *FromValue< bool >( enable ) );
} );
RegisterBuiltinFunc< Intrinsic< void ( bool ) > >( e, "#VerificationDumpSolverOnFailure"_sid,
[pEnv]( auto&& c, const Value& enable )
{
if( !enable.isConstant() )
{
DiagnosticsManager::GetInstance().emitErrorMessage( enable.locationId(),
"#VerificationDumpSolverOnFailure: the expression doesn't evaluate to a constant." );
return;
}
verify::Func::SetDumpSolverOnFailure( *FromValue< bool >( enable ) );
} );
RegisterBuiltinFunc< Intrinsic< void ( bool ) > >( e, "#VerificationDumpSolverOnSuccess"_sid,
[pEnv]( auto&& c, const Value& enable )
{
if( !enable.isConstant() )
{
DiagnosticsManager::GetInstance().emitErrorMessage( enable.locationId(),
"#VerificationDumpSolverOnSuccess: the expression doesn't evaluate to a constant." );
return;
}
verify::Func::SetDumpSolverOnSuccess( *FromValue< bool >( enable ) );
} );
RegisterBuiltinFunc< Intrinsic< void ( uint32_t ) > >( e, "#SetExecutionBudget"_sid,
[pEnv]( auto&& c, const Value& budget )
{
static bool used = false;
if( !budget.isConstant() )
{
DiagnosticsManager::GetInstance().emitErrorMessage( budget.locationId(),
"#SetExecutionBudget: the expression doesn't evaluate to a constant." );
return;
}
if( used )
{
DiagnosticsManager::GetInstance().emitErrorMessage( 0,
"#SetExecutionBudget can only be used once." );
return;
}
used = true;
execute::VM::SetExecutionBudget( *FromValue< uint32_t >( budget ) );
} );
RegisterBuiltinFunc< Eager< Value > ( Value, string ) >( e, "ExternalFunction"_sid,
[pEnv]( const Value& f, const string& symbol )
{
auto ft = FromValue< FuncType >( f );
if( !ft )
return PoisonValue();
return ToValue( BuildExternalFunc( *ft, symbol ) );
} );
RegisterBuiltinFunc< void ( string, Value ) >( e, "CreateConstant"_sid,
[pEnv]( const string& name, const Value& v )
{
if( !v.isConstant() )
{
DiagnosticsManager::GetInstance().emitErrorMessage( v.locationId(),
"CreateConstant: the expression doesn't evaluate to a constant." );
return;
}
pEnv.lock()->storeValue(
AppendToVectorTerm( RootIdentity(), StringId( name.c_str() ) ),
ANYTERM( _ ), ValueToIRExpr( v ) );
} );
RegisterBuiltinFunc< Intrinsic< uint32_t ( string ) > >( e, "#Include"_sid,
[pEnv]( auto&& c, 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 >(), ToValue< uint32_t >( 0 ) );
if( !result )
return ToValue< uint32_t >( 0 );
return *result;
} );
RegisterBuiltinFunc< uint32_t ( string ) >( e, "ExecuteFile"_sid,
[pEnv]( const string& filename ) -> Value
{
auto identity = InjectDomainIntoIdentity( RootIdentity(), DomainCompileTime() );
auto result = Compiler::LoadAndExecuteFile( pEnv.lock(), filename, identity,
GetValueType< uint32_t >(), ToValue< uint32_t >( 0 ) );
if( !result )
return ToValue< uint32_t >( 0 );
return *result;
} );
RegisterBuiltinFunc< Intrinsic< Value ( string, Value, Value, Value ) > >( e, "#CompileFileToFunction"_sid,
[pEnv]( auto&& c, const Value& filenameVal, const Value& rt, const Value& defReturnValue, const Value& params ) -> Value
{
if( !filenameVal.isConstant() )
{
DiagnosticsManager::GetInstance().emitErrorMessage( filenameVal.locationId(),
"#CompileFileToFunction: the expression doesn't evaluate to a constant." );
return PoisonValue();
}
auto filename = *FromValue< string >( filenameVal );
// Validate those generic value inputs
if( !IsType( rt ) )
{
DiagnosticsManager::GetInstance().emitErrorMessage( rt.locationId(),
"#CompileFileToFunction: type expected.", 0 );
return PoisonValue();
}
if( CheckParamListKind( params ) != ParamListKind::Regular )
{
DiagnosticsManager::GetInstance().emitErrorMessage( params.locationId(),
"#CompileFileToFunction: template parameter lists are not supported.", 0 );
return PoisonValue();
}
// 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 localC( pEnv.lock(), identity, ValueToIRExpr( rt ) );
auto ftype = BuildFuncType( localC, sema::DomainAny(), rt, params );
auto funcIdentity = AppendToVectorTerm( identity,
TERM( StringId( Env::NewUniqueId() ) ) );
c.env()->addVisibilityRule(
InjectDomainIntoIdentity( identity, ANYTERM( _ ) ),
InjectDomainIntoIdentity( funcIdentity, ANYTERM( _ ) ) );
auto func = BuildFunc( localC, ftype, identity, params, nullptr, c );
const auto& pFuncLLR = func.llr();
auto cfg = Compiler::LoadAndParseFile( pEnv.lock(), filename, funcIdentity,
ftype.returnType(), defReturnValue );
if( !cfg )
return PoisonValue();
if( cfg->isPoisoned() )
return PoisonValue();
pFuncLLR->body() = cfg;
return ToValue( func );
} );
}
}