2020-08-10 01:37:07 -07:00
|
|
|
#include "SemanticAnalyzer.h"
|
|
|
|
|
|
|
|
|
|
#include "compiler/Report.h"
|
2020-08-25 00:57:03 -07:00
|
|
|
#include "compiler/ast/Argument.h"
|
|
|
|
|
#include "compiler/ast/FunctionCall.h"
|
|
|
|
|
#include "compiler/ast/FunctionParameterDeclaration.h"
|
|
|
|
|
#include "compiler/ast/FunctionParameterList.h"
|
|
|
|
|
#include "compiler/ast/Identifier.h"
|
|
|
|
|
#include "compiler/ast/ModuleFunctionDeclaration.h"
|
2020-08-17 22:53:03 -07:00
|
|
|
#include "compiler/ast/TopLevelStatements.h"
|
2020-08-25 00:57:03 -07:00
|
|
|
#include "compiler/ast/VarStatement.h"
|
2020-08-10 01:37:07 -07:00
|
|
|
#include "compiler/model/CompilerWorkspace.h"
|
2020-08-25 00:57:03 -07:00
|
|
|
#include "compiler/model/FunctionLink.h"
|
|
|
|
|
#include "compiler/model/Variable.h"
|
2020-08-10 01:37:07 -07:00
|
|
|
|
2020-08-10 03:20:29 -07:00
|
|
|
namespace Pol::Bscript::Compiler
|
2020-08-10 01:37:07 -07:00
|
|
|
{
|
|
|
|
|
SemanticAnalyzer::SemanticAnalyzer( Report& report )
|
2020-08-25 00:57:03 -07:00
|
|
|
: report( report ),
|
|
|
|
|
globals( VariableScope::Global, report )
|
2020-08-10 01:37:07 -07:00
|
|
|
{
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
SemanticAnalyzer::~SemanticAnalyzer() = default;
|
|
|
|
|
|
|
|
|
|
void SemanticAnalyzer::register_const_declarations( CompilerWorkspace& /*workspace*/ )
|
|
|
|
|
{
|
|
|
|
|
}
|
|
|
|
|
|
2020-08-17 22:53:03 -07:00
|
|
|
void SemanticAnalyzer::analyze( CompilerWorkspace& workspace )
|
2020-08-10 01:37:07 -07:00
|
|
|
{
|
2020-08-17 22:53:03 -07:00
|
|
|
workspace.top_level_statements->accept( *this );
|
2020-08-25 00:57:03 -07:00
|
|
|
|
|
|
|
|
workspace.global_variable_names = globals.get_names();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void SemanticAnalyzer::visit_identifier( Identifier& node )
|
|
|
|
|
{
|
|
|
|
|
if ( auto global = globals.find( node.name ) )
|
|
|
|
|
{
|
|
|
|
|
node.variable = global;
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
report.error( node, "Unknown identifier '", node.name, "'.\n" );
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void SemanticAnalyzer::visit_var_statement( VarStatement& node )
|
|
|
|
|
{
|
|
|
|
|
if ( auto existing = globals.find( node.name ) )
|
|
|
|
|
{
|
|
|
|
|
report.error( node, "Global variable '", node.name, "' already defined.\n",
|
|
|
|
|
" See also: ", existing->source_location, "\n" );
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
node.variable = globals.create( node.name, 0, WarnOn::Never, node.source_location );
|
|
|
|
|
|
|
|
|
|
visit_children( node );
|
2020-08-10 01:37:07 -07:00
|
|
|
}
|
|
|
|
|
|
2020-08-10 03:20:29 -07:00
|
|
|
} // namespace Pol::Bscript::Compiler
|