polserver/pol-core/bscript/compiler/ast/FunctionCall.cpp
Eric Swanson cf42b03e3c
New compiler: Add AST nodes for function calls. Can compile hello world. (#240)
Adds:
- ast/Argument: AST node for an argument passed to a function.
- ast/FunctionCall: AST node for a function call.
- codegen/ModuleDeclarationRegistrar: The code generator registers module function declarations with this in order to determine module indexes and function indexes for instructions.
- model/FunctionLink: this is a reference either:
  - from: a FunctionCall or a FunctionReference
  - to: a ModuleFunctionDeclaration or a UserFunction
- optimizer/ReferencedFunctionGatherer: a visitor that determines which module functions and user functions are referenced, by looking at function calls and function references.

Also:
- CodeGenerator registers module functions
- InstructionGenerator generates code for function calls
- InstructionEmitter generates TOK_FUNC instructions
- StoredTokenDecoder decodes TOK_FUNC instructions

After all of this, the compiler can compile print("hello, world");
2020-08-24 01:47:38 -07:00

54 lines
1.5 KiB
C++

#include "FunctionCall.h"
#include <format/format.h>
#include <map>
#include <utility>
#include "compiler/ast/Argument.h"
#include "compiler/ast/FunctionParameterDeclaration.h"
#include "compiler/ast/ModuleFunctionDeclaration.h"
#include "compiler/ast/NodeVisitor.h"
#include "compiler/file/SourceLocation.h"
#include "compiler/model/FunctionLink.h"
namespace Pol::Bscript::Compiler
{
FunctionCall::FunctionCall( const SourceLocation& source_location, std::string scope,
std::string name, std::vector<std::unique_ptr<Argument>> children )
: Expression( source_location, std::move( children ) ),
function_link( std::make_shared<FunctionLink>( source_location ) ),
scope( std::move( scope ) ),
method_name( std::move( name ) )
{
}
void FunctionCall::accept( NodeVisitor& visitor )
{
visitor.visit_function_call( *this );
}
void FunctionCall::describe_to( fmt::Writer& w ) const
{
w << "function-call(" << method_name << ")";
}
std::vector<std::unique_ptr<Argument>> FunctionCall::take_arguments()
{
std::vector<std::unique_ptr<Argument>> args;
args.reserve( children.size() );
for ( auto& child : children )
{
args.emplace_back( static_unique_pointer_cast<Argument, Node>( std::move( child ) ) );
}
return args;
}
std::vector<std::reference_wrapper<FunctionParameterDeclaration>> FunctionCall::parameters() const
{
if ( auto fn = function_link->function() )
return fn->parameters();
else
internal_error( "function has not been resolved" );
}
} // namespace Pol::Bscript::Compiler