mirror of
https://github.com/polserver/polserver
synced 2026-08-13 08:23:08 -04:00
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.
62 lines
1.3 KiB
C++
62 lines
1.3 KiB
C++
#include "UnaryOperatorOptimizer.h"
|
|
|
|
#include "compiler/ast/FloatValue.h"
|
|
#include "compiler/ast/IntegerValue.h"
|
|
#include "compiler/ast/UnaryOperator.h"
|
|
|
|
namespace Pol::Bscript::Compiler
|
|
{
|
|
UnaryOperatorOptimizer::UnaryOperatorOptimizer( UnaryOperator& unary_operator )
|
|
: unary_operator( unary_operator )
|
|
{
|
|
}
|
|
|
|
std::unique_ptr<Expression> UnaryOperatorOptimizer::optimize()
|
|
{
|
|
unary_operator.operand().accept( *this );
|
|
return std::move( optimized_result );
|
|
}
|
|
|
|
void UnaryOperatorOptimizer::visit_children( Node& ) {}
|
|
|
|
void UnaryOperatorOptimizer::visit_float_value( FloatValue& fv )
|
|
{
|
|
double value;
|
|
|
|
switch ( unary_operator.token_id )
|
|
{
|
|
case TOK_UNMINUS:
|
|
value = -fv.value;
|
|
break;
|
|
|
|
default:
|
|
return;
|
|
}
|
|
|
|
optimized_result = std::make_unique<FloatValue>( fv.source_location, value );
|
|
}
|
|
|
|
void UnaryOperatorOptimizer::visit_integer_value( IntegerValue& iv )
|
|
{
|
|
int value;
|
|
|
|
switch ( unary_operator.token_id )
|
|
{
|
|
case TOK_UNMINUS:
|
|
value = -iv.value;
|
|
break;
|
|
case TOK_LOG_NOT:
|
|
value = !iv.value;
|
|
break;
|
|
case TOK_BITWISE_NOT:
|
|
value = static_cast<int>( ~static_cast<unsigned>( iv.value ) );
|
|
break;
|
|
|
|
default:
|
|
return;
|
|
}
|
|
|
|
optimized_result = std::make_unique<IntegerValue>( iv.source_location, value );
|
|
}
|
|
|
|
} // namespace Pol::Bscript::Compiler
|