2020-08-12 01:30:06 -07:00
|
|
|
#include "InstructionGenerator.h"
|
|
|
|
|
|
2020-08-19 10:43:08 -07:00
|
|
|
#include "compiler/ast/FloatValue.h"
|
2020-08-24 01:47:38 -07:00
|
|
|
#include "compiler/ast/FunctionCall.h"
|
2020-08-25 00:57:03 -07:00
|
|
|
#include "compiler/ast/FunctionParameterDeclaration.h"
|
|
|
|
|
#include "compiler/ast/FunctionParameterList.h"
|
|
|
|
|
#include "compiler/ast/Identifier.h"
|
2020-08-25 23:27:24 -07:00
|
|
|
#include "compiler/ast/IntegerValue.h"
|
2020-08-25 00:57:03 -07:00
|
|
|
#include "compiler/ast/ModuleFunctionDeclaration.h"
|
2020-08-20 01:27:35 -07:00
|
|
|
#include "compiler/ast/StringValue.h"
|
2020-08-26 19:24:47 -07:00
|
|
|
#include "compiler/ast/UnaryOperator.h"
|
2020-08-18 23:32:57 -07:00
|
|
|
#include "compiler/ast/ValueConsumer.h"
|
2020-08-25 00:57:03 -07:00
|
|
|
#include "compiler/ast/VarStatement.h"
|
2020-08-14 23:01:19 -07:00
|
|
|
#include "compiler/codegen/InstructionEmitter.h"
|
2020-08-18 23:32:57 -07:00
|
|
|
#include "compiler/file/SourceFileIdentifier.h"
|
2020-08-24 01:47:38 -07:00
|
|
|
#include "compiler/model/FunctionLink.h"
|
2020-08-25 00:57:03 -07:00
|
|
|
#include "compiler/model/Variable.h"
|
|
|
|
|
#include "symcont.h"
|
2020-08-12 01:30:06 -07:00
|
|
|
|
|
|
|
|
namespace Pol::Bscript::Compiler
|
|
|
|
|
{
|
|
|
|
|
InstructionGenerator::InstructionGenerator( InstructionEmitter& emitter )
|
|
|
|
|
: emitter( emitter ),
|
|
|
|
|
emit( emitter )
|
|
|
|
|
{
|
|
|
|
|
}
|
|
|
|
|
|
2020-08-25 00:57:03 -07:00
|
|
|
void InstructionGenerator::visit_identifier( Identifier& node )
|
|
|
|
|
{
|
|
|
|
|
if ( auto var = node.variable )
|
|
|
|
|
{
|
|
|
|
|
emit.access_variable( *var );
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
node.internal_error( "variable is not set" );
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2020-08-19 10:43:08 -07:00
|
|
|
void InstructionGenerator::visit_float_value( FloatValue& node )
|
|
|
|
|
{
|
|
|
|
|
emit.value( node.value );
|
|
|
|
|
}
|
|
|
|
|
|
2020-08-25 23:27:24 -07:00
|
|
|
void InstructionGenerator::visit_integer_value( IntegerValue& node )
|
|
|
|
|
{
|
|
|
|
|
emit.value( node.value );
|
|
|
|
|
}
|
|
|
|
|
|
2020-08-24 01:47:38 -07:00
|
|
|
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?" );
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2020-08-20 01:27:35 -07:00
|
|
|
void InstructionGenerator::visit_string_value( StringValue& lit )
|
|
|
|
|
{
|
|
|
|
|
emit.value( lit.value );
|
|
|
|
|
}
|
|
|
|
|
|
2020-08-26 19:24:47 -07:00
|
|
|
void InstructionGenerator::visit_unary_operator( UnaryOperator& unary_operator )
|
|
|
|
|
{
|
|
|
|
|
visit_children( unary_operator );
|
|
|
|
|
emit.unary_operator( unary_operator.token_id );
|
|
|
|
|
}
|
|
|
|
|
|
2020-08-18 23:32:57 -07:00
|
|
|
void InstructionGenerator::visit_value_consumer( ValueConsumer& node )
|
|
|
|
|
{
|
|
|
|
|
visit_children( node );
|
|
|
|
|
|
|
|
|
|
emit.consume();
|
|
|
|
|
}
|
|
|
|
|
|
2020-08-25 00:57:03 -07:00
|
|
|
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();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2020-08-12 01:30:06 -07:00
|
|
|
} // namespace Pol::Bscript::Compiler
|