polserver/pol-core/bscript/compiler/analyzer/Variables.cpp
Eric Swanson 9bbc0518f8
Compiler rewrite: add global variables (#241)
Adds support for var statements at the global level.

Adds:
- analyzer/Variables: keeps track of the variables in scope (either local or global).
- ast/Identifier: AST node for an identifier.
  - The optimizer will replace constant identifiers with their constant value (in a later commit).
  - The semantic analyzer will set the variable field for local or global variable identifiers.
- ast/VarStatement: AST node for a var statement.
  - A single var statement will generate one VarStatement per variable declared.
- model/Variable: Describes a variable, including its index within its scope.
2020-08-25 00:57:03 -07:00

38 lines
1.1 KiB
C++

#include "Variables.h"
#include "compiler/Report.h"
#include "compiler/file/SourceLocation.h"
#include "compiler/model/Variable.h"
namespace Pol::Bscript::Compiler
{
Variables::Variables( VariableScope scope, Report& report ) : scope( scope ), report( report ) {}
std::shared_ptr<Variable> Variables::create( const std::string& name, unsigned block_depth,
WarnOn warn_on, const SourceLocation& source_location )
{
auto index = names_by_index.size();
auto variable =
std::make_shared<Variable>( scope, name, block_depth, index, warn_on, source_location );
variables_by_name[name] = variable;
names_by_index.push_back( name );
return variable;
}
std::shared_ptr<Variable> Variables::find( const std::string& name ) const
{
auto itr = variables_by_name.find( name );
return ( itr != variables_by_name.end() ) ? ( *itr ).second : std::shared_ptr<Variable>();
}
const std::vector<std::string>& Variables::get_names() const
{
return names_by_index;
}
unsigned Variables::count() const
{
return names_by_index.size();
}
} // namespace Pol::Bscript::Compiler