polserver/pol-core/bscript/compiler/analyzer/Variables.h
Kevin Eady f381f46dc3
Add compiler support for function expressions (#671)
* 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
2024-07-29 22:30:52 +02:00

66 lines
1.8 KiB
C++

#ifndef POLSERVER_VARIABLES_H
#define POLSERVER_VARIABLES_H
#include <map>
#include <memory>
#include <string>
#include <vector>
#include "bscript/compiler/model/SimpleTypes.h"
#include "bscript/compiler/model/VariableScope.h"
#include "bscript/compiler/model/WarnOn.h"
#include "clib/maputil.h"
namespace Pol::Bscript::Compiler
{
class Report;
class SourceLocation;
class Variable;
class FunctionVariableScope;
class Variables
{
public:
Variables( VariableScope, Report& );
std::shared_ptr<Variable> create( const std::string& name, BlockDepth, WarnOn,
const SourceLocation& );
std::shared_ptr<Variable> capture( std::shared_ptr<Variable>& );
// Looks in parent and all ancestor function variable scopes
[[nodiscard]] std::shared_ptr<Variable> find_in_ancestors( const std::string& name ) const;
// Only looks in current function variable scope
[[nodiscard]] std::shared_ptr<Variable> find( const std::string& name ) const;
void restore_shadowed( std::shared_ptr<Variable> );
void remove_all_but( unsigned count );
[[nodiscard]] const std::vector<std::string>& get_names() const;
[[nodiscard]] unsigned count() const;
private:
const VariableScope scope;
Report& report;
// Encapsulates the current variable stack across function expresions
struct VariablesInfo
{
typedef std::map<std::string, std::shared_ptr<Variable>, Clib::ci_cmp_pred> VariableMap;
VariableMap variables_by_name;
std::vector<std::string> names_by_index;
};
std::vector<VariablesInfo> variable_info_stack;
VariablesInfo& current();
// Used to reset the variable indexs for new function expressions
friend class FunctionVariableScope;
};
} // namespace Pol::Bscript::Compiler
#endif // POLSERVER_SCRIPTVARIABLES_H