polserver/pol-core/bscript/compiler/codegen/InstructionGenerator.h
Kevin Eady 5cf6949ea7
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 b3148e069f
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 5a4d1c8fcb
Author: turleypol <turley@polserver.com>
Date:   Sat Sep 27 10:18:01 2025 +0200

    no need to use ptr for input

commit 90fa0edc66
Author: turleypol <turley@polserver.com>
Date:   Sat Sep 27 09:56:29 2025 +0200

    fixed typo

commit bed4ff3ac9
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

105 lines
4.8 KiB
C++

#ifndef POLSERVER_INSTRUCTIONGENERATOR_H
#define POLSERVER_INSTRUCTIONGENERATOR_H
#include "bscript/compiler/ast/NodeVisitor.h"
#include <map>
#include <stack>
#include <string>
namespace Pol::Bscript::Compiler
{
class FlowControlLabel;
class InstructionEmitter;
class SourceLocation;
class Variable;
class InstructionGenerator : public NodeVisitor
{
public:
InstructionGenerator( InstructionEmitter&,
std::map<std::string, FlowControlLabel>& user_function_labels,
const std::map<std::string, size_t>& class_declaration_indexes );
void generate( Node& );
void generate_default_parameters( const UserFunction& );
void update_debug_location( const Node& );
void update_debug_location( const SourceLocation& );
void visit_array_initializer( ArrayInitializer& ) override;
void visit_basic_for_loop( BasicForLoop& loop ) override;
void visit_case_statement( CaseStatement& ) override;
void visit_cstyle_for_loop( CstyleForLoop& loop ) override;
void visit_binary_operator( BinaryOperator& ) override;
void visit_block( Block& ) override;
void visit_boolean_value( BooleanValue& ) override;
void visit_branch_selector( BranchSelector& ) override;
void visit_class_instance( ClassInstance& ) override;
void visit_debug_statement_marker( DebugStatementMarker& ) override;
void visit_dictionary_entry( DictionaryEntry& ) override;
void visit_dictionary_initializer( DictionaryInitializer& ) override;
void visit_do_while_loop( DoWhileLoop& ) override;
void visit_element_access( ElementAccess& ) override;
void visit_element_assignment( ElementAssignment& ) override;
void visit_elvis_operator( ElvisOperator& elvis ) override;
void visit_error_initializer( ErrorInitializer& ) override;
void visit_exit_statement( ExitStatement& ) override;
void visit_float_value( FloatValue& ) override;
void visit_foreach_loop( ForeachLoop& ) override;
void visit_function_call( FunctionCall& ) override;
void visit_function_parameter_list( FunctionParameterList& ) override;
void visit_function_parameter_declaration( FunctionParameterDeclaration& ) override;
void visit_function_expression( FunctionExpression& ) override;
void visit_function_reference( FunctionReference& ) override;
void visit_identifier( Identifier& ) override;
void visit_if_then_else_statement( IfThenElseStatement& ) override;
void visit_index_binding( IndexBinding& ) override;
void visit_integer_value( IntegerValue& ) override;
void visit_jump_statement( JumpStatement& ) override;
void visit_member_access( MemberAccess& member_access ) override;
void visit_member_assignment( MemberAssignment& ) override;
void visit_member_assignment_by_operator( MemberAssignmentByOperator& ) override;
void visit_method_call( MethodCall& member_call ) override;
void visit_program( Program& ) override;
void visit_program_parameter_declaration( ProgramParameterDeclaration& ) override;
void visit_regular_expression_value( RegularExpressionValue& ) override;
void visit_repeat_until_loop( RepeatUntilLoop& repeat_until ) override;
void visit_return_statement( ReturnStatement& ) override;
void visit_sequence_binding( SequenceBinding& ) override;
void visit_spread_element( SpreadElement& ) override;
void visit_string_value( StringValue& ) override;
void visit_struct_initializer( StructInitializer& ) override;
void visit_struct_member_initializer( StructMemberInitializer& ) override;
void visit_unary_operator( UnaryOperator& ) override;
void visit_uninitialized_value( UninitializedValue& ) override;
void visit_user_function( UserFunction& ) override;
void visit_value_consumer( ValueConsumer& ) override;
void visit_var_statement( VarStatement& ) override;
void visit_variable_assignment_statement( VariableAssignmentStatement& ) override;
void visit_while_loop( WhileLoop& ) override;
void visit_variable_binding( VariableBinding& ) override;
void visit_interpolate_string( InterpolateString& ) override;
void visit_format_expression( FormatExpression& ) override;
void visit_conditional_operator( ConditionalOperator& ) override;
void visit_constant_loop( ConstantPredicateLoop& ) override;
void visit_binary_operator_short_circuit( BinaryOperatorShortCircuit& ) override;
private:
void emit_access_variable( Variable& );
// There are two of these because sometimes when calling a method
// on InstructionEmitter, the variable name reads better as a noun,
// and sometimes it reads better as a verb.
InstructionEmitter& emitter;
InstructionEmitter& emit;
std::map<std::string, FlowControlLabel>& user_function_labels;
const std::map<std::string, size_t>& class_declaration_indexes;
std::stack<UserFunction*> user_functions;
};
} // namespace Pol::Bscript::Compiler
#endif // POLSERVER_INSTRUCTIONGENERATOR_H