polserver/pol-core/bscript/compiler/ast/VarStatement.cpp
Kevin Eady 4ae9519343 Update grammar, parser, semantic analyzer for classes; skeleton codegen and executor (#688)
* Update grammar

* WIP with tracking classes
- Refactor AvailableUserFunction to AvailableParseTree
- Track ClassDeclarationContexts similarly to user functions

* Skeleton AST + tracking

* Finish up AST building

* Implement AST building
- TODO decide better name for UserFunctionBuilder/Visitor,
since it does both user functions and classes

* Update Prettifier

* Crude, first-round semantic analysis + codegen
- Currently, no real difference from regular functions

* Move class var statements to top level statements

* Add semantic analysis for base class existence

* Introduce ClassInstance AST node for generating `this` parameter

* Skeleton create class instance instruction

* Default ctor; 'this' byref; Error if multiple same baseclass

* Update grammar for scoped funcrefs and scoped identifiers

* Update prettifier, builders for scoped funcref, identifiers

* Fix CI issues
- Styling
- Shadowing

* Add grammar tests for scope; Remove scope from switch label

* Final draft todos
- Rename `VarStatement::class_name` to `scope`
- Comment why identifiers dont have scopes in enum declarations
- Remove code comment
2024-10-10 18:06:04 +02:00

48 lines
1.5 KiB
C++

#include "VarStatement.h"
#include <utility>
#include "bscript/compiler/ast/Expression.h"
#include "bscript/compiler/ast/NodeVisitor.h"
namespace Pol::Bscript::Compiler
{
VarStatement::VarStatement( const SourceLocation& source_location, std::string scope,
std::string name, std::unique_ptr<Expression> initializer )
: Statement( source_location, std::move( initializer ) ),
scope( std::move( scope ) ),
name( std::move( name ) )
{
}
VarStatement::VarStatement( const SourceLocation& source_location, std::string scope,
std::string name )
: Statement( source_location ), scope( std::move( scope ) ), name( std::move( name ) )
{
}
VarStatement::VarStatement( const SourceLocation& source_location, std::string scope,
std::string name, bool initialize_as_empty_array )
: Statement( source_location ),
scope( std::move( scope ) ),
name( std::move( name ) ),
initialize_as_empty_array( initialize_as_empty_array )
{
}
void VarStatement::accept( NodeVisitor& visitor )
{
visitor.visit_var_statement( *this );
}
void VarStatement::describe_to( std::string& w ) const
{
auto scope_prefix = scope.empty() ? "" : fmt::format( "{}::", scope );
fmt::format_to( std::back_inserter( w ), "var-statement({}{}", scope_prefix, name );
if ( initialize_as_empty_array )
w += ", initialize-as-empty-array";
w += ")";
}
} // namespace Pol::Bscript::Compiler