polserver/pol-core/bscript/compiler/ast/UserFunction.cpp
Kevin Eady e3512d001b Add scope handling for functions and variables (#690)
* Add Report.debug

* Implement function call scoping

* Implement variable scoping
- Rename `Identifier::scope` to `calling_scope`, and concatenate the identifier scope into `name` if necessary.
- Track current scope in SemanticAnalyzer, similar to UserFunctionVisitor.
- Use current scope in SemanticAnalyzer when visiting identifiers.

* Update grammar for `::identifier` global scoping

* Introduce ScopableName for function resolution

* Address review comments
- Check for display_debugs missing

* Add ScopableName for identifier resolution

* Some cleanup
- Remove `Identifier::calling_scope`, as it is tracked in the semantic
  analyzer
- Refactor `ScopeName::exists` to `global` for better clarification of
  its use
- Removed `X::maybe_scoped_string/name`

* Address self-review comments

* Rename `Function::module_name` to `scope`
2024-10-10 18:06:04 +02:00

46 lines
1.4 KiB
C++

#include "UserFunction.h"
#include <utility>
#include "bscript/compiler/ast/FunctionBody.h"
#include "bscript/compiler/ast/FunctionParameterList.h"
#include "bscript/compiler/ast/NodeVisitor.h"
namespace Pol::Bscript::Compiler
{
UserFunction::UserFunction( const SourceLocation& source_location, bool exported, bool expression,
UserFunctionType type, std::string scope, std::string name,
std::unique_ptr<FunctionParameterList> parameter_list,
std::unique_ptr<FunctionBody> body,
const SourceLocation& endfunction_location )
: Function( source_location, std::move( scope ), std::move( name ), std::move( parameter_list ),
std::move( body ) ),
exported( exported ),
expression( expression ),
type( type ),
endfunction_location( endfunction_location )
{
}
void UserFunction::accept( NodeVisitor& visitor )
{
visitor.visit_user_function( *this );
}
void UserFunction::describe_to( std::string& w ) const
{
fmt::format_to( std::back_inserter( w ), "user-function({})", name );
}
unsigned UserFunction::capture_count() const
{
return static_cast<unsigned>( capture_variable_scope_info.variables.size() );
}
FunctionBody& UserFunction::body()
{
return child<FunctionBody>( 1 );
}
} // namespace Pol::Bscript::Compiler