polserver/pol-core/bscript/compiler/ast/NodeVisitor.cpp

458 lines
11 KiB
C++
Raw Permalink Normal View History

#include "bscript/compiler/ast/NodeVisitor.h"
#include "bscript/compiler/ast/Argument.h"
#include "bscript/compiler/ast/ArrayInitializer.h"
#include "bscript/compiler/ast/BasicForLoop.h"
#include "bscript/compiler/ast/BinaryOperator.h"
#include "bscript/compiler/ast/BinaryOperatorShortCircuit.h"
#include "bscript/compiler/ast/BindingStatement.h"
#include "bscript/compiler/ast/Block.h"
#include "bscript/compiler/ast/BooleanValue.h"
#include "bscript/compiler/ast/BranchSelector.h"
#include "bscript/compiler/ast/CaseDispatchDefaultSelector.h"
#include "bscript/compiler/ast/CaseDispatchGroup.h"
#include "bscript/compiler/ast/CaseDispatchGroups.h"
#include "bscript/compiler/ast/CaseDispatchSelectors.h"
#include "bscript/compiler/ast/CaseStatement.h"
#include "bscript/compiler/ast/ClassBody.h"
#include "bscript/compiler/ast/ClassDeclaration.h"
#include "bscript/compiler/ast/ClassInstance.h"
#include "bscript/compiler/ast/ClassParameterDeclaration.h"
#include "bscript/compiler/ast/ClassParameterList.h"
#include "bscript/compiler/ast/ConditionalOperator.h"
#include "bscript/compiler/ast/ConstDeclaration.h"
#include "bscript/compiler/ast/ConstantPredicateLoop.h"
#include "bscript/compiler/ast/CstyleForLoop.h"
#include "bscript/compiler/ast/DebugStatementMarker.h"
#include "bscript/compiler/ast/DictionaryEntry.h"
#include "bscript/compiler/ast/DictionaryInitializer.h"
#include "bscript/compiler/ast/DoWhileLoop.h"
#include "bscript/compiler/ast/ElementAccess.h"
#include "bscript/compiler/ast/ElementAssignment.h"
#include "bscript/compiler/ast/ElementIndexes.h"
#include "bscript/compiler/ast/ElvisOperator.h"
#include "bscript/compiler/ast/EmptyStatement.h"
#include "bscript/compiler/ast/EnumDeclaration.h"
#include "bscript/compiler/ast/ErrorInitializer.h"
#include "bscript/compiler/ast/ExitStatement.h"
#include "bscript/compiler/ast/FloatValue.h"
#include "bscript/compiler/ast/ForeachLoop.h"
Add support for interpolated strings ("interstrings") (#369) * Update grammar to support interpolated strings * Struct initializers, case switchs are regular strings * Initial stub for interpolated string in AST This splits the previous STRING_LITERAL terminal into a production, stringLiteral, to handle REGULAR_STRING terminal and interpolatedString production. * Change interpolated strings to expressions * Fix mode handling in grammar * Grammar fixes - Fix mode handling in lexer - Interpolated strings only have one expression * Can successfully parse, stub compile interstrings $"He {there + print("hello")}"; * Add formatting string to grammar * Fix tests, and add test stubs * Rename curleys to brace; better AST generation * initial work interstring instruction * Initial work on formatted string instruction: emit * Formatted string instruction: execution * Add simple formatted string test * Add formatted string test src * Review changes #1 - Rename `FormattedString` to `FormatExpression` - Rename overloaded `try_to_format()` to `get_formatted()` * Review changes part 2 - Rename `InterpolatedString` ast node to `InterpolateString` * Review changes part 3 - Modify `ins_interpolate_string` * Review changes part 4 - Remove unused formal parameter * Add negative tests * Spruce up positive tests * Fix braces, escape chars inside interstring * Fix, add tests for escaped char and double brace * Address differences for new lines in tests * Update Escript version * Make empty expression an error * Add documentation for interpolated strings * Quick touchup on docs
2021-03-11 22:48:16 +01:00
#include "bscript/compiler/ast/FormatExpression.h"
#include "bscript/compiler/ast/FunctionBody.h"
#include "bscript/compiler/ast/FunctionCall.h"
Add compiler support for function expressions (#671) * initial poc of function expressions - update grammar - mock AST builder to return BBoolean(true) for a func expr - update prettifier for skeleton implementation * more skeleton work create AST class FunctionExpression mimicking boolean value * create ast UserFunction, add to workspace from functexpr * can generate instructions * reorg tests; add test for instructions * can track FunctionDepth in Variable * can get captures for funcexprs inside funcs.. tbd if this way of nesting works * Implement create-functor instruction - Move function depth from Variable to Variables - Introduce stacking of `Variables` for function expresions via `FunctionVariableScope` - Add `TOK_FUNCTOR` instruction for 'create-functor' - Handle emitting a `FunctionExpression` AST node - Update `emit.declare_variable` and `emit.access_variable` to account for function captures - Remove `in_function` from instruction generator, as it is tracked via `UserFunction` stack - Update tests * Address Discord comments - Swap pop param order * Bubble up captured variables through nested functions * Improve testing infrastructure; add some test cases * Fix compilation error * prepend captures to function parameters in funcref mth_call * update tests * move from function{} to @{} * implementation fix * some more tests * update docs * Some cleanup * fix CI annotation warning - 'argument': conversion from 'size_t' to 'VariableIndex', possible loss of data * Address review comments - Add `passert_always` - Use better example in docs * Some cleanup - Remove unused functions
2024-07-29 22:30:52 +02:00
#include "bscript/compiler/ast/FunctionExpression.h"
#include "bscript/compiler/ast/FunctionParameterDeclaration.h"
#include "bscript/compiler/ast/FunctionParameterList.h"
#include "bscript/compiler/ast/GeneratedFunction.h"
#include "bscript/compiler/ast/IfThenElseStatement.h"
#include "bscript/compiler/ast/IndexBinding.h"
Add support for interpolated strings ("interstrings") (#369) * Update grammar to support interpolated strings * Struct initializers, case switchs are regular strings * Initial stub for interpolated string in AST This splits the previous STRING_LITERAL terminal into a production, stringLiteral, to handle REGULAR_STRING terminal and interpolatedString production. * Change interpolated strings to expressions * Fix mode handling in grammar * Grammar fixes - Fix mode handling in lexer - Interpolated strings only have one expression * Can successfully parse, stub compile interstrings $"He {there + print("hello")}"; * Add formatting string to grammar * Fix tests, and add test stubs * Rename curleys to brace; better AST generation * initial work interstring instruction * Initial work on formatted string instruction: emit * Formatted string instruction: execution * Add simple formatted string test * Add formatted string test src * Review changes #1 - Rename `FormattedString` to `FormatExpression` - Rename overloaded `try_to_format()` to `get_formatted()` * Review changes part 2 - Rename `InterpolatedString` ast node to `InterpolateString` * Review changes part 3 - Modify `ins_interpolate_string` * Review changes part 4 - Remove unused formal parameter * Add negative tests * Spruce up positive tests * Fix braces, escape chars inside interstring * Fix, add tests for escaped char and double brace * Address differences for new lines in tests * Update Escript version * Make empty expression an error * Add documentation for interpolated strings * Quick touchup on docs
2021-03-11 22:48:16 +01:00
#include "bscript/compiler/ast/InterpolateString.h"
#include "bscript/compiler/ast/JumpStatement.h"
#include "bscript/compiler/ast/MemberAccess.h"
#include "bscript/compiler/ast/MemberAssignment.h"
#include "bscript/compiler/ast/MemberAssignmentByOperator.h"
#include "bscript/compiler/ast/MethodCall.h"
#include "bscript/compiler/ast/MethodCallArgumentList.h"
#include "bscript/compiler/ast/ModuleFunctionDeclaration.h"
#include "bscript/compiler/ast/Node.h"
#include "bscript/compiler/ast/Program.h"
#include "bscript/compiler/ast/ProgramParameterDeclaration.h"
#include "bscript/compiler/ast/ProgramParameterList.h"
Add support for regular expressions (#818) * implementation * tests * maybe fix windows compilation? * undo change of match_flag_type * switch to boost regex * move flags to BRegExp object * fix tests due to cmake 4 update * update grammar * update prettifier * move flag parsing to BRegExp ctor * add AST nodes, update instr generation and execution * copy tests but use regular expression literals * fix grammar for handling division correctly * modify return values a bit, update tests - string.match: make groups hold structs of matched, offset - string.match: add offset - string.replace: use a groups array like string.match vs individual arguments * bundle of changes - standardize error messages - add more tests for coverage - move BRegExp creation to static method: previous implementation had ctor throwing, which was no good inside executor since it didn't have a try/catch * more tests; add OT_REGEXP to basic.em * move string regex stuff to bregexp and support regex/wregex via std::visit * maybe fix compilation errors? * add unicode escape sequence handling * really maybe fix compilation error? * fix compiler warnings * refactor a bit to remove duplicate code * address discord comments - just return wstring, no need for vector<wchar_t> method * Add string.split by string and regexp * Use Max_Split instead of Limit This makes it match basic::SplitWords * add support for empty string delim in SplitWords * remove duplicate code across string.split and mf_SplitWords * Squashed commit of the following: commit b3148e069f4c4bad041b36144d312f375c028a6c Author: turleypol <turley@polserver.com> Date: Sat Sep 27 17:02:22 2025 +0200 memorylocation of input string is not allowed to changed, switched back to uniqueptr commit 5a4d1c8fcb2d8239ba0b693644dd20d9a5a167bb Author: turleypol <turley@polserver.com> Date: Sat Sep 27 10:18:01 2025 +0200 no need to use ptr for input commit 90fa0edc66034db77a43222612b4981bf09081a5 Author: turleypol <turley@polserver.com> Date: Sat Sep 27 09:56:29 2025 +0200 fixed typo commit bed4ff3ac9de88fb9e91350125339c8ab04c2da3 Author: turleypol <turley@polserver.com> Date: Sat Sep 27 09:47:36 2025 +0200 make Callback for BContinuation move only * use uninit when group isn't matched * fix multiline flag handling; add test * add docs and doc example tests * some cleanup * address review comments - allow move assignment * add core-changes
2025-11-17 18:02:45 +01:00
#include "bscript/compiler/ast/RegularExpressionValue.h"
#include "bscript/compiler/ast/RepeatUntilLoop.h"
#include "bscript/compiler/ast/ReturnStatement.h"
#include "bscript/compiler/ast/SequenceBinding.h"
#include "bscript/compiler/ast/SpreadElement.h"
#include "bscript/compiler/ast/StringValue.h"
#include "bscript/compiler/ast/StructInitializer.h"
#include "bscript/compiler/ast/StructMemberInitializer.h"
#include "bscript/compiler/ast/TopLevelStatements.h"
#include "bscript/compiler/ast/UnaryOperator.h"
#include "bscript/compiler/ast/UninitializedFunctionDeclaration.h"
#include "bscript/compiler/ast/UserFunction.h"
#include "bscript/compiler/ast/ValueConsumer.h"
#include "bscript/compiler/ast/VarStatement.h"
#include "bscript/compiler/ast/VariableAssignmentStatement.h"
#include "bscript/compiler/ast/VariableBinding.h"
#include "bscript/compiler/ast/WhileLoop.h"
2020-08-16 23:03:33 -07:00
namespace Pol::Bscript::Compiler
{
void NodeVisitor::visit_argument( Argument& node )
{
visit_children( node );
}
void NodeVisitor::visit_array_initializer( ArrayInitializer& node )
{
visit_children( node );
}
void NodeVisitor::visit_basic_for_loop( BasicForLoop& node )
{
visit_children( node );
}
2020-09-01 22:49:48 -07:00
void NodeVisitor::visit_binary_operator( BinaryOperator& node )
{
visit_children( node );
}
void NodeVisitor::visit_binding_statement( BindingStatement& node )
{
visit_children( node );
}
void NodeVisitor::visit_block( Block& node )
{
visit_children( node );
}
void NodeVisitor::visit_boolean_value( BooleanValue& node )
{
visit_children( node );
}
void NodeVisitor::visit_branch_selector( BranchSelector& node )
{
visit_children( node );
}
void NodeVisitor::visit_case_statement( CaseStatement& node )
{
visit_children( node );
}
void NodeVisitor::visit_case_dispatch_default_selector( CaseDispatchDefaultSelector& node )
{
visit_children( node );
}
void NodeVisitor::visit_case_dispatch_group( CaseDispatchGroup& node )
{
visit_children( node );
}
void NodeVisitor::visit_case_dispatch_groups( CaseDispatchGroups& node )
{
visit_children( node );
}
void NodeVisitor::visit_case_dispatch_selectors( CaseDispatchSelectors& node )
{
visit_children( node );
}
void NodeVisitor::visit_const_declaration( ConstDeclaration& node )
{
visit_children( node );
}
void NodeVisitor::visit_cstyle_for_loop( CstyleForLoop& node )
{
visit_children( node );
}
void NodeVisitor::visit_class_body( ClassBody& node )
{
visit_children( node );
}
void NodeVisitor::visit_class_declaration( ClassDeclaration& node )
{
visit_children( node );
}
void NodeVisitor::visit_class_instance( ClassInstance& node )
{
visit_children( node );
}
void NodeVisitor::visit_class_parameter_declaration( ClassParameterDeclaration& node )
{
visit_children( node );
}
void NodeVisitor::visit_class_parameter_list( ClassParameterList& node )
{
visit_children( node );
}
void NodeVisitor::visit_debug_statement_marker( DebugStatementMarker& node )
{
visit_children( node );
}
void NodeVisitor::visit_dictionary_entry( DictionaryEntry& node )
{
visit_children( node );
}
void NodeVisitor::visit_dictionary_initializer( DictionaryInitializer& node )
{
visit_children( node );
}
void NodeVisitor::visit_do_while_loop( DoWhileLoop& node )
{
visit_children( node );
}
void NodeVisitor::visit_element_access( ElementAccess& node )
{
visit_children( node );
}
void NodeVisitor::visit_element_assignment( ElementAssignment& node )
{
visit_children( node );
}
void NodeVisitor::visit_element_indexes( ElementIndexes& node )
{
visit_children( node );
}
void NodeVisitor::visit_elvis_operator( ElvisOperator& node )
{
visit_children( node );
}
void NodeVisitor::visit_empty_statement( EmptyStatement& node )
{
visit_children( node );
}
void NodeVisitor::visit_enum_declaration( EnumDeclaration& node )
{
visit_children( node );
}
void NodeVisitor::visit_error_initializer( ErrorInitializer& node )
{
visit_children( node );
}
void NodeVisitor::visit_exit_statement( ExitStatement& ) {}
void NodeVisitor::visit_float_value( FloatValue& ) {}
void NodeVisitor::visit_foreach_loop( ForeachLoop& node )
2020-08-19 10:43:08 -07:00
{
visit_children( node );
}
void NodeVisitor::visit_function_body( FunctionBody& node )
{
visit_children( node );
}
void NodeVisitor::visit_function_call( FunctionCall& node )
{
visit_children( node );
}
void NodeVisitor::visit_function_parameter_declaration( FunctionParameterDeclaration& node )
{
visit_children( node );
}
void NodeVisitor::visit_function_parameter_list( FunctionParameterList& node )
{
visit_children( node );
}
Add compiler support for function expressions (#671) * initial poc of function expressions - update grammar - mock AST builder to return BBoolean(true) for a func expr - update prettifier for skeleton implementation * more skeleton work create AST class FunctionExpression mimicking boolean value * create ast UserFunction, add to workspace from functexpr * can generate instructions * reorg tests; add test for instructions * can track FunctionDepth in Variable * can get captures for funcexprs inside funcs.. tbd if this way of nesting works * Implement create-functor instruction - Move function depth from Variable to Variables - Introduce stacking of `Variables` for function expresions via `FunctionVariableScope` - Add `TOK_FUNCTOR` instruction for 'create-functor' - Handle emitting a `FunctionExpression` AST node - Update `emit.declare_variable` and `emit.access_variable` to account for function captures - Remove `in_function` from instruction generator, as it is tracked via `UserFunction` stack - Update tests * Address Discord comments - Swap pop param order * Bubble up captured variables through nested functions * Improve testing infrastructure; add some test cases * Fix compilation error * prepend captures to function parameters in funcref mth_call * update tests * move from function{} to @{} * implementation fix * some more tests * update docs * Some cleanup * fix CI annotation warning - 'argument': conversion from 'size_t' to 'VariableIndex', possible loss of data * Address review comments - Add `passert_always` - Use better example in docs * Some cleanup - Remove unused functions
2024-07-29 22:30:52 +02:00
void NodeVisitor::visit_function_expression( FunctionExpression& node )
{
visit_children( node );
}
void NodeVisitor::visit_function_reference( FunctionReference& ) {}
void NodeVisitor::visit_identifier( Identifier& ) {}
2020-08-25 23:27:24 -07:00
void NodeVisitor::visit_if_then_else_statement( IfThenElseStatement& node )
{
visit_children( node );
}
void NodeVisitor::visit_index_binding( IndexBinding& node )
{
visit_children( node );
}
void NodeVisitor::visit_integer_value( IntegerValue& ) {}
void NodeVisitor::visit_jump_statement( JumpStatement& node )
{
visit_children( node );
}
void NodeVisitor::visit_member_access( MemberAccess& node )
{
visit_children( node );
}
void NodeVisitor::visit_sequence_binding( SequenceBinding& node )
{
visit_children( node );
}
void NodeVisitor::visit_member_assignment( MemberAssignment& node )
{
visit_children( node );
}
void NodeVisitor::visit_member_assignment_by_operator( MemberAssignmentByOperator& node )
{
visit_children( node );
}
void NodeVisitor::visit_method_call( MethodCall& node )
{
visit_children( node );
}
void NodeVisitor::visit_method_call_argument_list( MethodCallArgumentList& node )
{
visit_children( node );
}
void NodeVisitor::visit_module_function_declaration( ModuleFunctionDeclaration& node )
{
visit_children( node );
}
void NodeVisitor::visit_program( Program& node )
{
visit_children( node );
}
void NodeVisitor::visit_program_parameter_declaration( ProgramParameterDeclaration& node )
{
visit_children( node );
}
void NodeVisitor::visit_program_parameter_list( ProgramParameterList& node )
{
visit_children( node );
}
Add support for regular expressions (#818) * implementation * tests * maybe fix windows compilation? * undo change of match_flag_type * switch to boost regex * move flags to BRegExp object * fix tests due to cmake 4 update * update grammar * update prettifier * move flag parsing to BRegExp ctor * add AST nodes, update instr generation and execution * copy tests but use regular expression literals * fix grammar for handling division correctly * modify return values a bit, update tests - string.match: make groups hold structs of matched, offset - string.match: add offset - string.replace: use a groups array like string.match vs individual arguments * bundle of changes - standardize error messages - add more tests for coverage - move BRegExp creation to static method: previous implementation had ctor throwing, which was no good inside executor since it didn't have a try/catch * more tests; add OT_REGEXP to basic.em * move string regex stuff to bregexp and support regex/wregex via std::visit * maybe fix compilation errors? * add unicode escape sequence handling * really maybe fix compilation error? * fix compiler warnings * refactor a bit to remove duplicate code * address discord comments - just return wstring, no need for vector<wchar_t> method * Add string.split by string and regexp * Use Max_Split instead of Limit This makes it match basic::SplitWords * add support for empty string delim in SplitWords * remove duplicate code across string.split and mf_SplitWords * Squashed commit of the following: commit b3148e069f4c4bad041b36144d312f375c028a6c Author: turleypol <turley@polserver.com> Date: Sat Sep 27 17:02:22 2025 +0200 memorylocation of input string is not allowed to changed, switched back to uniqueptr commit 5a4d1c8fcb2d8239ba0b693644dd20d9a5a167bb Author: turleypol <turley@polserver.com> Date: Sat Sep 27 10:18:01 2025 +0200 no need to use ptr for input commit 90fa0edc66034db77a43222612b4981bf09081a5 Author: turleypol <turley@polserver.com> Date: Sat Sep 27 09:56:29 2025 +0200 fixed typo commit bed4ff3ac9de88fb9e91350125339c8ab04c2da3 Author: turleypol <turley@polserver.com> Date: Sat Sep 27 09:47:36 2025 +0200 make Callback for BContinuation move only * use uninit when group isn't matched * fix multiline flag handling; add test * add docs and doc example tests * some cleanup * address review comments - allow move assignment * add core-changes
2025-11-17 18:02:45 +01:00
void NodeVisitor::visit_regular_expression_value( RegularExpressionValue& node )
{
visit_children( node );
}
void NodeVisitor::visit_repeat_until_loop( RepeatUntilLoop& node )
{
visit_children( node );
}
void NodeVisitor::visit_return_statement( ReturnStatement& node )
{
visit_children( node );
}
void NodeVisitor::visit_spread_element( SpreadElement& node )
{
visit_children( node );
}
void NodeVisitor::visit_string_value( StringValue& ) {}
Add support for interpolated strings ("interstrings") (#369) * Update grammar to support interpolated strings * Struct initializers, case switchs are regular strings * Initial stub for interpolated string in AST This splits the previous STRING_LITERAL terminal into a production, stringLiteral, to handle REGULAR_STRING terminal and interpolatedString production. * Change interpolated strings to expressions * Fix mode handling in grammar * Grammar fixes - Fix mode handling in lexer - Interpolated strings only have one expression * Can successfully parse, stub compile interstrings $"He {there + print("hello")}"; * Add formatting string to grammar * Fix tests, and add test stubs * Rename curleys to brace; better AST generation * initial work interstring instruction * Initial work on formatted string instruction: emit * Formatted string instruction: execution * Add simple formatted string test * Add formatted string test src * Review changes #1 - Rename `FormattedString` to `FormatExpression` - Rename overloaded `try_to_format()` to `get_formatted()` * Review changes part 2 - Rename `InterpolatedString` ast node to `InterpolateString` * Review changes part 3 - Modify `ins_interpolate_string` * Review changes part 4 - Remove unused formal parameter * Add negative tests * Spruce up positive tests * Fix braces, escape chars inside interstring * Fix, add tests for escaped char and double brace * Address differences for new lines in tests * Update Escript version * Make empty expression an error * Add documentation for interpolated strings * Quick touchup on docs
2021-03-11 22:48:16 +01:00
void NodeVisitor::visit_interpolate_string( InterpolateString& node )
{
visit_children( node );
}
void NodeVisitor::visit_format_expression( FormatExpression& node )
{
visit_children( node );
}
void NodeVisitor::visit_struct_initializer( StructInitializer& node )
{
visit_children( node );
}
void NodeVisitor::visit_struct_member_initializer( StructMemberInitializer& node )
{
visit_children( node );
}
void NodeVisitor::visit_generated_function( GeneratedFunction& node )
{
visit_children( node );
}
void NodeVisitor::visit_top_level_statements( TopLevelStatements& node )
{
visit_children( node );
}
void NodeVisitor::visit_unary_operator( UnaryOperator& node )
{
visit_children( node );
}
void NodeVisitor::visit_uninitialized_function_declaration( UninitializedFunctionDeclaration& node )
{
visit_children( node );
}
void NodeVisitor::visit_uninitialized_value( UninitializedValue& ) {}
void NodeVisitor::visit_user_function( UserFunction& node )
{
visit_children( node );
}
void NodeVisitor::visit_value_consumer( ValueConsumer& node )
{
visit_children( node );
}
void NodeVisitor::visit_var_statement( VarStatement& node )
{
visit_children( node );
}
void NodeVisitor::visit_variable_assignment_statement( VariableAssignmentStatement& node )
{
visit_children( node );
}
void NodeVisitor::visit_variable_binding( VariableBinding& node )
{
visit_children( node );
}
void NodeVisitor::visit_while_loop( WhileLoop& node )
{
visit_children( node );
}
void NodeVisitor::visit_conditional_operator( ConditionalOperator& node )
{
visit_children( node );
}
void NodeVisitor::visit_constant_loop( ConstantPredicateLoop& node )
{
visit_children( node );
}
void NodeVisitor::visit_binary_operator_short_circuit( BinaryOperatorShortCircuit& node )
{
visit_children( node );
}
2020-08-16 23:03:33 -07:00
void NodeVisitor::visit_children( Node& parent )
{
for ( const auto& child : parent.children )
{
child->accept( *this );
}
}
} // namespace Pol::Bscript::Compiler