polserver/pol-core/bscript/compiler/optimizer/ReferencedFunctionGatherer.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

46 lines
1.3 KiB
C++

#include "ReferencedFunctionGatherer.h"
#include "compiler/ast/FunctionCall.h"
#include "compiler/model/FunctionLink.h"
namespace Pol::Bscript::Compiler
{
ReferencedFunctionGatherer::ReferencedFunctionGatherer(
std::vector<std::unique_ptr<ModuleFunctionDeclaration>>& all_module_function_declarations )
{
for ( auto& mfd : all_module_function_declarations )
{
unreferenced_module_function_declarations.insert( mfd.get() );
}
}
void ReferencedFunctionGatherer::visit_function_call( FunctionCall& fc )
{
visit_children( fc );
reference( *fc.function_link );
}
void ReferencedFunctionGatherer::reference( FunctionLink& link )
{
if ( auto mfd = link.module_function_declaration() )
reference( mfd );
}
void ReferencedFunctionGatherer::reference( ModuleFunctionDeclaration* mfd )
{
auto itr = unreferenced_module_function_declarations.find( mfd );
if ( itr != unreferenced_module_function_declarations.end() )
{
referenced_module_function_declarations.push_back( mfd );
unreferenced_module_function_declarations.erase( itr );
}
}
std::vector<ModuleFunctionDeclaration*>
ReferencedFunctionGatherer::take_referenced_module_function_declarations()
{
return std::move( referenced_module_function_declarations );
}
} // namespace Pol::Bscript::Compiler