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

38 lines
853 B
C++

#include "UnaryOperator.h"
#include <format/format.h>
#include <utility>
#include "compiler/ast/NodeVisitor.h"
namespace Pol::Bscript::Compiler
{
UnaryOperator::UnaryOperator( const SourceLocation& source_location, std::string op,
BTokenId token_id, std::unique_ptr<Expression> operand )
: Expression( source_location, std::move( operand ) ),
op( std::move( op ) ),
token_id( token_id )
{
}
void UnaryOperator::accept( NodeVisitor& visitor )
{
visitor.visit_unary_operator( *this );
}
void UnaryOperator::describe_to( fmt::Writer& w ) const
{
w << "unary-operator(" << op << ")";
}
Expression& UnaryOperator::operand()
{
return child<Expression>( 0 );
}
std::unique_ptr<Expression> UnaryOperator::take_operand()
{
return take_child<Expression>( 0 );
}
} // namespace Pol::Bscript::Compiler