mirror of
https://github.com/polserver/polserver
synced 2026-08-13 08:23:08 -04:00
* initial poc of function expressions
- update grammar
- mock AST builder to return BBoolean(true) for a func expr
- update prettifier for skeleton implementation
* more skeleton work
create AST class FunctionExpression mimicking boolean value
* create ast UserFunction, add to workspace from functexpr
* can generate instructions
* reorg tests; add test for instructions
* can track FunctionDepth in Variable
* can get captures for funcexprs inside funcs.. tbd if this way of nesting works
* Implement create-functor instruction
- Move function depth from Variable to Variables
- Introduce stacking of `Variables` for function expresions via `FunctionVariableScope`
- Add `TOK_FUNCTOR` instruction for 'create-functor'
- Handle emitting a `FunctionExpression` AST node
- Update `emit.declare_variable` and `emit.access_variable` to account for function captures
- Remove `in_function` from instruction generator, as it is tracked via `UserFunction` stack
- Update tests
* Address Discord comments
- Swap pop param order
* Bubble up captured variables through nested functions
* Improve testing infrastructure; add some test cases
* Fix compilation error
* prepend captures to function parameters in funcref mth_call
* update tests
* move from function{} to @{}
* implementation fix
* some more tests
* update docs
* Some cleanup
* fix CI annotation warning
- 'argument': conversion from 'size_t' to 'VariableIndex', possible loss of data
* Address review comments
- Add `passert_always`
- Use better example in docs
* Some cleanup
- Remove unused functions
69 lines
2.1 KiB
C++
69 lines
2.1 KiB
C++
#include "LocalVariableScope.h"
|
|
|
|
#include "bscript/compiler/Report.h"
|
|
#include "bscript/compiler/analyzer/LocalVariableScopes.h"
|
|
#include "bscript/compiler/analyzer/Variables.h"
|
|
#include "bscript/compiler/file/SourceLocation.h"
|
|
#include "bscript/compiler/model/LocalVariableScopeInfo.h"
|
|
#include "bscript/compiler/model/Variable.h"
|
|
|
|
namespace Pol::Bscript::Compiler
|
|
{
|
|
LocalVariableScope::LocalVariableScope( LocalVariableScopes& scopes,
|
|
LocalVariableScopeInfo& local_variable_scope_info )
|
|
: scopes( scopes ),
|
|
report( scopes.report ),
|
|
block_depth( scopes.local_variable_scopes.size() ),
|
|
prev_locals( scopes.local_variables.count() ),
|
|
local_variable_scope_info( local_variable_scope_info )
|
|
{
|
|
local_variable_scope_info.base_index = prev_locals;
|
|
scopes.local_variable_scopes.push_back( this );
|
|
}
|
|
|
|
LocalVariableScope::~LocalVariableScope()
|
|
{
|
|
scopes.local_variables.remove_all_but( prev_locals );
|
|
|
|
for ( auto& variable : shadowing )
|
|
{
|
|
scopes.local_variables.restore_shadowed( variable );
|
|
}
|
|
|
|
scopes.local_variable_scopes.pop_back();
|
|
}
|
|
|
|
std::shared_ptr<Variable> LocalVariableScope::create( const std::string& name, WarnOn warn_on,
|
|
const SourceLocation& source_location )
|
|
{
|
|
if ( auto existing = scopes.local_variables.find( name ) )
|
|
{
|
|
if ( existing->block_depth == block_depth )
|
|
{
|
|
report.error( source_location,
|
|
"Variable '{}' is already in scope.\n"
|
|
" See previous definition at: {}",
|
|
name, existing->source_location );
|
|
return existing;
|
|
}
|
|
shadowing.push_back( existing );
|
|
}
|
|
auto local = scopes.local_variables.create( name, block_depth, warn_on, source_location );
|
|
|
|
local_variable_scope_info.variables.push_back( local );
|
|
|
|
return local;
|
|
}
|
|
|
|
std::shared_ptr<Variable> LocalVariableScope::capture( std::shared_ptr<Variable>& other )
|
|
{
|
|
auto local = scopes.local_variables.capture( other );
|
|
|
|
other->mark_used();
|
|
|
|
local_variable_scope_info.variables.push_back( local );
|
|
|
|
return local;
|
|
}
|
|
|
|
} // namespace Pol::Bscript::Compiler
|