2020-08-18 03:07:51 -07:00
|
|
|
#include "SimpleStatementBuilder.h"
|
|
|
|
|
|
|
|
|
|
#include "compiler/Report.h"
|
2020-08-18 23:32:57 -07:00
|
|
|
#include "compiler/ast/Expression.h"
|
2020-08-25 00:57:03 -07:00
|
|
|
#include "compiler/ast/StringValue.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"
|
|
|
|
|
#include "compiler/astbuilder/BuilderWorkspace.h"
|
|
|
|
|
#include "compiler/model/CompilerWorkspace.h"
|
|
|
|
|
|
|
|
|
|
using EscriptGrammar::EscriptParser;
|
2020-08-18 03:07:51 -07:00
|
|
|
|
|
|
|
|
namespace Pol::Bscript::Compiler
|
|
|
|
|
{
|
|
|
|
|
SimpleStatementBuilder::SimpleStatementBuilder( const SourceFileIdentifier& source_file_identifier,
|
|
|
|
|
BuilderWorkspace& workspace )
|
|
|
|
|
: ExpressionBuilder( source_file_identifier, workspace )
|
|
|
|
|
{
|
|
|
|
|
}
|
|
|
|
|
|
2020-08-25 00:57:03 -07:00
|
|
|
void SimpleStatementBuilder::add_var_statements(
|
|
|
|
|
EscriptParser::VarStatementContext* ctx, std::vector<std::unique_ptr<Statement>>& statements )
|
|
|
|
|
{
|
|
|
|
|
if ( auto variable_declaration_list = ctx->variableDeclarationList() )
|
|
|
|
|
{
|
|
|
|
|
for ( auto decl : variable_declaration_list->variableDeclaration() )
|
|
|
|
|
{
|
|
|
|
|
auto loc = location_for( *decl );
|
|
|
|
|
std::string name = text( decl->IDENTIFIER() );
|
|
|
|
|
std::unique_ptr<VarStatement> var_ast;
|
|
|
|
|
|
|
|
|
|
if ( auto initializer_context = decl->variableDeclarationInitializer() )
|
|
|
|
|
{
|
|
|
|
|
if ( initializer_context->ARRAY() )
|
|
|
|
|
{
|
|
|
|
|
var_ast = std::make_unique<VarStatement>( loc, std::move( name ), true );
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
auto initializer = variable_initializer( initializer_context );
|
|
|
|
|
var_ast =
|
|
|
|
|
std::make_unique<VarStatement>( loc, std::move( name ), std::move( initializer ) );
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
var_ast = std::make_unique<VarStatement>( loc, std::move( name ) );
|
|
|
|
|
}
|
|
|
|
|
auto consumed = consume_statement_result( std::move( var_ast ) );
|
|
|
|
|
statements.push_back( std::move( consumed ) );
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2020-08-18 23:32:57 -07:00
|
|
|
std::unique_ptr<Statement> SimpleStatementBuilder::consume_statement_result(
|
|
|
|
|
std::unique_ptr<Statement> statement )
|
|
|
|
|
{
|
|
|
|
|
return std::make_unique<ValueConsumer>( statement->source_location, std::move( statement ) );
|
|
|
|
|
}
|
|
|
|
|
|
2020-08-25 00:57:03 -07:00
|
|
|
std::unique_ptr<Expression> SimpleStatementBuilder::variable_initializer(
|
|
|
|
|
EscriptParser::VariableDeclarationInitializerContext* ctx )
|
|
|
|
|
{
|
|
|
|
|
if ( auto expr = ctx->expression() )
|
|
|
|
|
return expression( expr );
|
|
|
|
|
else
|
|
|
|
|
return std::unique_ptr<Expression>( new StringValue( location_for( *ctx ), "" ) );
|
|
|
|
|
}
|
|
|
|
|
|
2020-08-18 03:07:51 -07:00
|
|
|
} // namespace Pol::Bscript::Compiler
|