polserver/pol-core/bscript/compiler/ast/VarStatement.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

43 lines
1.2 KiB
C++

#include "VarStatement.h"
#include <format/format.h>
#include <utility>
#include "compiler/ast/Expression.h"
#include "compiler/ast/NodeVisitor.h"
namespace Pol::Bscript::Compiler
{
VarStatement::VarStatement( const SourceLocation& source_location, std::string name,
std::unique_ptr<Expression> initializer )
: Statement( source_location, std::move( initializer ) ), name( std::move( name ) )
{
}
VarStatement::VarStatement( const SourceLocation& source_location, std::string name )
: Statement( source_location ), name( std::move( name ) )
{
}
VarStatement::VarStatement( const SourceLocation& source_location, std::string name,
bool initialize_as_empty_array )
: Statement( source_location ),
name( std::move( name ) ),
initialize_as_empty_array( initialize_as_empty_array )
{
}
void VarStatement::accept( NodeVisitor& visitor )
{
visitor.visit_var_statement( *this );
}
void VarStatement::describe_to( fmt::Writer& w ) const
{
w << "var-statement(" << name;
if ( initialize_as_empty_array )
w << ", initialize-as-empty-array";
w << ")";
}
} // namespace Pol::Bscript::Compiler