polserver/pol-core/bscript/compiler/codegen/InstructionGenerator.cpp
Eric Swanson a77ddd028e
New compiler: Add optimized unary expressions (#244)
Adds:
- UnaryOperator: AST node for unary operators -, ++, --, and so forth
- UnaryOperatorOptimizer: optimizes a unary operator with its operand
  - now integer and float negation, and integer inversion
  - later x[y]++ to a single instruction

Also:
- automatically include basic.em, which includes some parameter defaults like -1.

This allows the compiler to generate output for a hello, world script with the exact same .ecl output as the legacy compiler.
2020-08-26 19:24:47 -07:00

100 lines
2.3 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/IntegerValue.h"
#include "compiler/ast/ModuleFunctionDeclaration.h"
#include "compiler/ast/StringValue.h"
#include "compiler/ast/UnaryOperator.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_integer_value( IntegerValue& 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_unary_operator( UnaryOperator& unary_operator )
{
visit_children( unary_operator );
emit.unary_operator( unary_operator.token_id );
}
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