polserver/pol-core/bscript/compiler/codegen/InstructionGenerator.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

87 lines
2 KiB
C++

#include "InstructionGenerator.h"
#include "compiler/ast/FloatValue.h"
#include "compiler/ast/FunctionCall.h"
#include "compiler/ast/FunctionParameterDeclaration.h"
#include "compiler/ast/FunctionParameterList.h"
#include "compiler/ast/Identifier.h"
#include "compiler/ast/ModuleFunctionDeclaration.h"
#include "compiler/ast/StringValue.h"
#include "compiler/ast/ValueConsumer.h"
#include "compiler/ast/VarStatement.h"
#include "compiler/codegen/InstructionEmitter.h"
#include "compiler/file/SourceFileIdentifier.h"
#include "compiler/model/FunctionLink.h"
#include "compiler/model/Variable.h"
#include "symcont.h"
namespace Pol::Bscript::Compiler
{
InstructionGenerator::InstructionGenerator( InstructionEmitter& emitter )
: emitter( emitter ),
emit( emitter )
{
}
void InstructionGenerator::visit_identifier( Identifier& node )
{
if ( auto var = node.variable )
{
emit.access_variable( *var );
}
else
{
node.internal_error( "variable is not set" );
}
}
void InstructionGenerator::visit_float_value( FloatValue& node )
{
emit.value( node.value );
}
void InstructionGenerator::visit_function_call( FunctionCall& call )
{
visit_children( call );
if ( auto mf = call.function_link->module_function_declaration() )
{
emit.call_modulefunc( *mf );
}
else
{
call.internal_error( "neither a module function nor a user function?" );
}
}
void InstructionGenerator::visit_string_value( StringValue& lit )
{
emit.value( lit.value );
}
void InstructionGenerator::visit_value_consumer( ValueConsumer& node )
{
visit_children( node );
emit.consume();
}
void InstructionGenerator::visit_var_statement( VarStatement& node )
{
if ( !node.variable )
node.internal_error( "variable is not defined" );
emit.declare_variable( *node.variable );
if ( node.initialize_as_empty_array )
{
emit.array_declare();
}
else if ( !node.children.empty() )
{
visit_children( node );
emit.assign();
}
}
} // namespace Pol::Bscript::Compiler