polserver/pol-core/bscript/compiler/codegen/InstructionEmitter.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

170 lines
6 KiB
C++

#ifndef POLSERVER_INSTRUCTIONEMITTER_H
#define POLSERVER_INSTRUCTIONEMITTER_H
#include <map>
#include <memory>
#include <string>
#include <vector>
#ifndef OBJMEMBERS_H
#include "objmembers.h"
#endif
#ifndef OBJMETHODS_H
#include "objmethods.h"
#endif
#ifndef __TOKENS_H
#include "tokens.h"
#endif
#include "bscript/compiler/codegen/CodeEmitter.h"
#include "bscript/compiler/codegen/DataEmitter.h"
#include "bscript/compiler/model/SimpleTypes.h"
#include "bscript/compiler/representation/CompiledScript.h"
#include "bscript/compiler/representation/DebugStore.h"
namespace Pol::Bscript
{
class StoredToken;
}
namespace Pol::Bscript::Compiler
{
class CaseJumpDataBlock;
class CompiledScript;
class FlowControlLabel;
class LocalVariableScopeInfo;
class ModuleDeclarationRegistrar;
class ClassDeclaration;
class ClassDeclarationRegistrar;
class ModuleFunctionDeclaration;
class FunctionReferenceRegistrar;
class UserFunction;
class Node;
class Variable;
class SourceLocation;
class Report;
class InstructionEmitter
{
public:
InstructionEmitter( CodeSection& code, DataSection& data, DebugStore& debug,
ExportedFunctions& exported_functions, ModuleDeclarationRegistrar&,
FunctionReferenceRegistrar&, ClassDeclarationRegistrar&, Report& );
void initialize_data();
void register_exported_function( FlowControlLabel& label, const std::string& name,
unsigned arguments );
void register_class_declaration( const ClassDeclaration&,
std::map<std::string, FlowControlLabel>& );
unsigned enter_debug_block( const LocalVariableScopeInfo& );
void set_debug_block( unsigned );
void access_variable( const Variable&, VariableIndex function_params_count,
VariableIndex function_capture_count );
void array_append();
void array_create();
void array_declare();
void assign();
void assign_subscript_consume();
void assign_subscript();
void assign_multisubscript( unsigned indexes );
void assign_variable( const Variable&, VariableIndex function_params_count,
VariableIndex function_capture_count );
void basic_for_init( FlowControlLabel& );
void basic_for_next( FlowControlLabel& );
void binary_operator( BTokenId token_id );
void check_mro( unsigned offset );
void call_method( const std::string& name, unsigned argument_count );
void call_method_id( MethodID method_id, unsigned argument_count );
void call_modulefunc( const ModuleFunctionDeclaration& );
void call_userfunc( FlowControlLabel& );
void classinst_create( unsigned index );
unsigned casejmp();
unsigned case_dispatch_table( const CaseJumpDataBlock& );
void consume();
void ctrl_statementbegin( unsigned file_index, unsigned file_offset,
const std::string& source_text );
// If `take`, the variable's value will be taken/moved from the top of ValueStack
void declare_variable( const Variable&, VariableIndex function_capture_count, bool take );
void dictionary_create();
void dictionary_add_member();
void error_create();
void exit();
void foreach_init( FlowControlLabel& );
void foreach_step( FlowControlLabel& );
void function_reference( const UserFunction&, FlowControlLabel& );
void functor_create( const UserFunction&, FlowControlLabel& );
void get_arg( const std::string& name );
void get_member( const std::string& name );
void get_member_id( MemberID );
void jmp_always( FlowControlLabel& );
void jmp_if_false( FlowControlLabel& );
void jmp_if_true( FlowControlLabel& );
void label( FlowControlLabel& );
void leaveblock( unsigned local_vars_to_remove );
void makelocal();
void pop_param( const std::string& name );
void pop_param_byref( const std::string& name );
void progend();
void return_from_user_function();
// Emit the 'this' variable, always stored at local `this_offset`.
void return_from_constructor_function( unsigned int this_offset );
unsigned skip_if_true_else_consume();
void set_member( const std::string& name );
void set_member_id( MemberID member_id );
void set_member_id_consume( MemberID member_id );
void set_member_consume( const std::string& name );
void set_member_by_operator( BTokenId, MemberID );
void spread( bool spread_into );
void struct_create();
void struct_add_uninit_member( const std::string& name );
void struct_add_member( const std::string& name );
void subscript_single();
void subscript_multiple( unsigned indexes );
void unary_operator( BTokenId );
void uninit();
void unpack_sequence( unsigned count, unsigned rest_at );
void unpack_indices( unsigned count, unsigned rest_at );
void value( double );
void value( int );
void value( bool );
void value( const std::string& );
void regular_expression_value( const std::string& pattern, const std::string& flags );
void interpolate_string( unsigned count );
void format_expression();
void debug_file_line( unsigned file_index, unsigned line_number );
void debug_statementbegin();
unsigned next_instruction_address();
void debug_user_function( const std::string& name, unsigned first_address,
unsigned last_address );
void patch_offset( unsigned index, unsigned offset );
bool has_function_reference( const UserFunction& );
void logical_jmp( FlowControlLabel& label, bool if_true );
void logical_convert();
private:
unsigned emit_data( const std::string& );
unsigned emit_token( BTokenId id, BTokenType type, unsigned offset = 0 );
unsigned append_token( StoredToken& );
void register_with_label( FlowControlLabel&, unsigned offset );
CodeEmitter code_emitter;
DataEmitter data_emitter;
DebugStore& debug;
ExportedFunctions& exported_functions;
ModuleDeclarationRegistrar& module_declaration_registrar;
FunctionReferenceRegistrar& function_reference_registrar;
ClassDeclarationRegistrar& class_declaration_registrar;
DebugStore::InstructionInfo debug_instruction_info{};
Report& report;
};
} // namespace Pol::Bscript::Compiler
#endif // POLSERVER_INSTRUCTIONEMITTER_H