polserver/pol-core/bscript/compiler/optimizer/ConstantValidator.cpp
Eric Swanson 6ce9c38de3
Add support for const declarations (#264)
Adds:
- analyzer/Constants: holds constant name -> value for lookup
- ast/ConstDeclaration: AST node for a const declaration
- optimizer/ConstValidator: validates that an optimized expression is valid to use as a constant.

the Optimizer converts Identifiers that refer to a constant to the optimized constant value.
2020-09-01 00:54:24 -07:00

47 lines
1 KiB
C++

#include "ConstantValidator.h"
#include "compiler/ast/ConstDeclaration.h"
#include "compiler/ast/FloatValue.h"
#include "compiler/ast/FunctionCall.h"
#include "compiler/ast/Identifier.h"
#include "compiler/ast/IntegerValue.h"
#include "compiler/ast/StringValue.h"
#include "compiler/model/FunctionLink.h"
namespace Pol::Bscript::Compiler
{
ConstantValidator::ConstantValidator() : valid( false ) {}
bool ConstantValidator::validate( Node& node )
{
valid = false;
node.accept( *this );
return valid;
}
void ConstantValidator::visit_float_value( FloatValue& )
{
valid = true;
}
void ConstantValidator::visit_function_call( FunctionCall& fc )
{
// this was only allowed by accident.
valid = ( fc.function_link->module_function_declaration() && fc.children.empty() );
}
void ConstantValidator::visit_integer_value( IntegerValue& )
{
valid = true;
}
void ConstantValidator::visit_string_value( StringValue& )
{
valid = true;
}
void ConstantValidator::visit_children( Node& )
{
}
} // namespace Pol::Bscript::Compiler