#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 );
} );
}
}