2023-04-03 00:55:28 -07:00
|
|
|
/*
|
|
|
|
gencpp: A simple staged metaprogramming library for C++.
|
2023-04-05 23:21:23 -07:00
|
|
|
|
|
|
|
The library is mostly a compositon of code element constructors.
|
|
|
|
These build up a code AST to then serialize with a file builder.
|
|
|
|
|
2023-04-03 23:04:19 -07:00
|
|
|
This library is intended for small-to midsize projects.
|
2023-04-03 00:55:28 -07:00
|
|
|
|
2023-04-05 23:21:23 -07:00
|
|
|
AST type checking supports only a small subset of c++.
|
2023-04-03 00:55:28 -07:00
|
|
|
See the 'ECode' namespace and 'gen API' region to see what is supported.
|
|
|
|
|
2023-04-04 13:18:30 -07:00
|
|
|
### *WHAT IS NOT PROVIDED*
|
2023-04-03 23:04:19 -07:00
|
|
|
|
2023-04-09 21:38:47 -07:00
|
|
|
This library aims to be used in a "orthodox" or minimal C++ workspace.
|
|
|
|
|
|
|
|
* Macro or template generation : This library is to avoid those, adding support for them adds unnecessary complexity.
|
|
|
|
* Vendor provided dynamic dispatch (virtuals) : Roll your own, this library might roll its own vtable/interface generation helpers in the future.
|
2023-04-18 19:47:59 -07:00
|
|
|
* RTTI
|
|
|
|
* Exceptions
|
2023-04-09 21:38:47 -07:00
|
|
|
* Execution statement validation : Execution expressions are defined using the untyped string API.
|
|
|
|
|
|
|
|
Keywords in from "Modern C++":
|
2023-04-07 09:31:50 -07:00
|
|
|
Specifiers:
|
2023-04-09 21:38:47 -07:00
|
|
|
* constexpr : Great to store compile-time constants, (easier to garanteed when emitted from gentime)
|
|
|
|
* consteval : Technically fine so long as templates are not used. Need to make sure to execute in moderation.
|
|
|
|
* constinit : Better than constexpr at doing its job, however, its only c++ 20.
|
|
|
|
* export : Useful if c++ modules ever come around to actually being usable.
|
|
|
|
* import : ^^
|
|
|
|
* module : ^^
|
|
|
|
|
|
|
|
These features are in as they either are not horrible when used conservatively or are a performance benefit (modules).
|
|
|
|
|
2023-04-10 18:33:06 -07:00
|
|
|
#### When it comes to expressions:
|
2023-04-09 21:38:47 -07:00
|
|
|
|
2023-04-10 18:33:06 -07:00
|
|
|
There is no support for validating expressions.
|
|
|
|
The reason: thats where the can of worms open for parsing validation. This library would most likey more than double in size with that addition alone.
|
|
|
|
For most metaprogramming (espcially for C/C++), expression validation is not necessary for metaprogramming, it can be done by the compiler for the runtime program.
|
|
|
|
Most of the time, the critical complex metaprogramming conundrums are actaully producing the frame of abstractions around the expressions.
|
|
|
|
Thus its not very much a priority to add such a level of complexity to the library when there isn't a high reward or need for it.
|
2023-04-09 21:38:47 -07:00
|
|
|
|
2023-04-10 18:33:06 -07:00
|
|
|
To further this point, lets say you do have an error with an expressions composition.
|
|
|
|
It will either be caught by the c++ compiler when compiling the target program, or at runtime for the program.
|
2023-04-09 21:38:47 -07:00
|
|
|
|
2023-04-10 18:33:06 -07:00
|
|
|
* If its not caught by the compiler, the only downside is the error appers on the generated function.
|
|
|
|
Those with knowledge of how that definition was generated know where to find the code that inlined that expression in that file for that definition.
|
|
|
|
* If its caught at runtime. The expression will be shown in a stack trace if debug symbols are enabled in the generated function body.
|
|
|
|
Yet again those with knowledge of how that definition was generated know where to find the code that inlined that expression.
|
|
|
|
|
|
|
|
In both these cases the user will get objectively better debug information than you would normally get on most c++ compilers/editors using complex macros or templates.
|
2023-04-09 21:38:47 -07:00
|
|
|
|
|
|
|
### The Data & Interface:
|
2023-04-07 09:31:50 -07:00
|
|
|
|
2023-04-04 13:18:30 -07:00
|
|
|
The AST is managed by the library and provided the user via its interface prodedures.
|
|
|
|
|
|
|
|
Notes:
|
|
|
|
|
2023-04-05 00:03:56 -07:00
|
|
|
* The allocator definitions used are exposed to the user incase they want to dictate memory usage
|
2023-04-09 21:38:47 -07:00
|
|
|
* You'll find the memory handling in `init`, `gen_string_allocator`, `get_cached_string`, `make_code`, and `make_code_entries`.
|
2023-04-05 23:21:23 -07:00
|
|
|
* ASTs are wrapped for the user in a Code struct which essentially a warpper for a AST* type.
|
2023-04-04 13:18:30 -07:00
|
|
|
* Both AST and Code have member symbols but their data layout is enforced to be POD types.
|
2023-04-09 21:38:47 -07:00
|
|
|
* This library treats memory failures as fatal.
|
2023-04-10 18:33:06 -07:00
|
|
|
* The AST's data union is can either hold a static array of AST*'s, a dynmaic array if the the static capacity is not enough, or a cached string content.
|
|
|
|
* The dynamic array is allocated to arenas currently and are pretty wasteful if they go over their reserve size (its never recycled).
|
2023-04-04 13:18:30 -07:00
|
|
|
|
|
|
|
Data layout of AST struct:
|
|
|
|
|
2023-04-10 18:33:06 -07:00
|
|
|
union {
|
|
|
|
AST* ArrStatic[ArrS_Cap];
|
|
|
|
Array(AST*) Entries;
|
|
|
|
StringCached Content;
|
|
|
|
|
|
|
|
};
|
|
|
|
AST* Parent;
|
|
|
|
StringCached Name;
|
|
|
|
StringCached Comment;
|
|
|
|
CodeT Type;
|
|
|
|
OperatorT Op;
|
|
|
|
bool Readonly;
|
|
|
|
bool DynamicEntries;
|
|
|
|
u8 StaticIndex;
|
|
|
|
u8 _Align_Pad[6];
|
2023-04-04 13:18:30 -07:00
|
|
|
|
|
|
|
*`CodeT` is a typedef for `ECode::Type` which is the type of the enum.*
|
|
|
|
|
2023-04-10 18:33:06 -07:00
|
|
|
AST widths are setup to be AST_POD_Size.
|
|
|
|
The width dictates how much the static array can hold before it must give way to using an allocated array:
|
|
|
|
constexpr static
|
|
|
|
u32 ArrS_Cap =
|
|
|
|
( AST_POD_Size
|
|
|
|
- sizeof(AST*)
|
|
|
|
- sizeof(StringCached) * 2
|
|
|
|
- sizeof(CodeT)
|
|
|
|
- sizeof(OperatorT)
|
|
|
|
- sizeof(bool) * 2
|
|
|
|
- sizeof(u8) * 7 )
|
|
|
|
/ sizeof(AST*);
|
|
|
|
|
|
|
|
Ex: If the AST_POD_Size is 256 the capacity of the static array is 26.
|
|
|
|
|
2023-04-04 13:18:30 -07:00
|
|
|
ASTs can be set to readonly by calling Code's lock() member function.
|
2023-04-05 23:21:23 -07:00
|
|
|
Adding comments is always available even if the AST is set to readonly.
|
2023-04-04 13:18:30 -07:00
|
|
|
|
2023-04-10 18:33:06 -07:00
|
|
|
#### Misc
|
2023-04-09 21:38:47 -07:00
|
|
|
|
|
|
|
* The allocator definitions used are exposed to the user incase they want to dictate memory usage
|
|
|
|
* You'll find the memory handling in `init`, `gen_string_allocator`, `get_cached_string`, `make_code`, and `make_code_entries`.
|
|
|
|
* ASTs are wrapped for the user in a Code struct which essentially a warpper for a AST* type.
|
|
|
|
* Both AST and Code have member symbols but their data layout is enforced to be POD types.
|
|
|
|
* This library treats memory failures as fatal.
|
|
|
|
* The default setup assumes large definition sets may be provided to bodies so AST::Entires are dynamic arrays.
|
|
|
|
* They're allocated to arenas currently and are pretty wasteful if they go over their reserve size (its never recycled).
|
|
|
|
* Most likely will need to implement a dynamic-sized bucket allocation strategy for the entry arrays if memory is getting stressed.
|
|
|
|
* Otherwise if you are using fixed size entries and your definitions are under 128~512 entries for the body, you may be better of with a fixed-sized array.
|
|
|
|
* Strings are stored in their own set of arenas. AST constructors use cached strings for names, and content.
|
|
|
|
|
2023-04-04 13:18:30 -07:00
|
|
|
### There are four sets of interfaces for Code AST generation the library provides
|
|
|
|
|
|
|
|
* Upfront
|
|
|
|
* Incremental
|
|
|
|
* Parsing
|
2023-04-03 23:04:19 -07:00
|
|
|
* Untyped
|
|
|
|
|
2023-04-04 13:18:30 -07:00
|
|
|
### Upfront Construction
|
|
|
|
|
2023-04-03 23:04:19 -07:00
|
|
|
All component ASTs must be previously constructed, and provided on creation of the code AST.
|
|
|
|
The construction will fail and return InvalidCode otherwise.
|
|
|
|
|
2023-04-04 13:18:30 -07:00
|
|
|
Interface :
|
|
|
|
|
|
|
|
* def_class
|
2023-04-05 00:03:56 -07:00
|
|
|
* def_enum
|
2023-04-09 21:38:47 -07:00
|
|
|
* def_execution NOTE: This is equivalent to untyped_str, except that its intended for use only in execution scopes.
|
2023-04-07 09:31:50 -07:00
|
|
|
* def_friend
|
2023-04-05 23:21:23 -07:00
|
|
|
* def_function
|
2023-04-03 23:04:19 -07:00
|
|
|
* def_namespace
|
2023-04-05 00:03:56 -07:00
|
|
|
* def_operator
|
2023-04-03 23:04:19 -07:00
|
|
|
* def_param
|
|
|
|
* def_params
|
|
|
|
* def_specifier
|
|
|
|
* def_specifiers
|
|
|
|
* def_struct
|
|
|
|
* def_variable
|
|
|
|
* def_type
|
2023-04-05 00:03:56 -07:00
|
|
|
* def_typedef
|
2023-04-03 23:04:19 -07:00
|
|
|
* def_using
|
|
|
|
|
2023-04-06 16:19:11 -07:00
|
|
|
* def_class_body
|
|
|
|
* def_enum_body
|
2023-04-07 21:29:09 -07:00
|
|
|
* def_function_body NOTE: Use this for operator bodies as well.
|
2023-04-06 16:19:11 -07:00
|
|
|
* def_global_body
|
|
|
|
* def_namespace_body
|
|
|
|
* def_struct_body
|
|
|
|
|
2023-04-07 21:56:59 -07:00
|
|
|
Usage:
|
2023-04-07 21:29:09 -07:00
|
|
|
Code <name> = def_<function type>( ... );
|
|
|
|
|
|
|
|
Code <name>
|
|
|
|
{
|
|
|
|
...
|
|
|
|
<name> = def_<function name>( ... );
|
|
|
|
}
|
|
|
|
|
2023-04-04 13:18:30 -07:00
|
|
|
### Incremental construction
|
|
|
|
|
2023-04-03 23:04:19 -07:00
|
|
|
A Code ast is provided but only completed upfront if all components are provided.
|
2023-04-04 13:18:30 -07:00
|
|
|
Components are then added using the AST API for adding ASTs:
|
|
|
|
|
2023-04-05 00:03:56 -07:00
|
|
|
* code.add( AST* ) // Adds AST with validation.
|
|
|
|
* code.add_entry( AST* ) // Adds AST entry without validation.
|
2023-04-04 13:18:30 -07:00
|
|
|
|
|
|
|
Code ASTs may be explictly validated at anytime using Code's check() member function.
|
|
|
|
|
|
|
|
Interface :
|
2023-04-03 23:04:19 -07:00
|
|
|
|
|
|
|
* make_class
|
2023-04-05 00:03:56 -07:00
|
|
|
* make_enum
|
|
|
|
* make_enum_class
|
2023-04-05 23:21:23 -07:00
|
|
|
* make_function
|
2023-04-03 23:04:19 -07:00
|
|
|
* make_global_body
|
|
|
|
* make_namespace
|
|
|
|
* make_operator
|
2023-04-05 00:03:56 -07:00
|
|
|
* make_params
|
2023-04-03 23:04:19 -07:00
|
|
|
* make_specifiers
|
|
|
|
* make_struct
|
|
|
|
|
2023-04-07 21:56:59 -07:00
|
|
|
Usage:
|
2023-04-07 21:29:09 -07:00
|
|
|
Code <name> = make_<function name>( ... )
|
|
|
|
{
|
|
|
|
<name>->add( ... );
|
|
|
|
...
|
|
|
|
}
|
|
|
|
|
2023-04-04 13:18:30 -07:00
|
|
|
### Parse construction
|
|
|
|
|
2023-04-03 23:04:19 -07:00
|
|
|
A string provided to the API is parsed for the intended language construct.
|
|
|
|
|
2023-04-04 13:18:30 -07:00
|
|
|
Interface :
|
|
|
|
|
2023-04-03 23:04:19 -07:00
|
|
|
* parse_class
|
2023-04-05 00:03:56 -07:00
|
|
|
* parse_enum
|
2023-04-07 09:31:50 -07:00
|
|
|
* parse_friend
|
2023-04-05 23:21:23 -07:00
|
|
|
* parse_function
|
2023-04-05 00:03:56 -07:00
|
|
|
* parse_global_body
|
2023-04-03 23:04:19 -07:00
|
|
|
* parse_namespace
|
|
|
|
* parse_operator
|
|
|
|
* parse_struct
|
2023-04-05 00:03:56 -07:00
|
|
|
* parse_strucs
|
2023-04-03 23:04:19 -07:00
|
|
|
* parse_variable
|
|
|
|
* parse_type
|
2023-04-05 00:03:56 -07:00
|
|
|
* parse_typedef
|
2023-04-03 23:04:19 -07:00
|
|
|
* parse_using
|
2023-04-06 16:19:11 -07:00
|
|
|
|
|
|
|
* parse_classes
|
|
|
|
* parse_enums
|
|
|
|
* parse_functions
|
|
|
|
* parse_namespaces
|
|
|
|
* parse_operators
|
|
|
|
* parse_variables
|
|
|
|
* parse_typedefs
|
2023-04-05 00:03:56 -07:00
|
|
|
* parse_usings
|
2023-04-03 23:04:19 -07:00
|
|
|
|
|
|
|
The parse API treats any execution scope definitions with no validation and are turned into untyped Code ASTs.
|
|
|
|
This includes the assignmetn of variables; due to the library not yet supporting c/c++ expression parsing.
|
|
|
|
|
2023-04-07 21:29:09 -07:00
|
|
|
The pluralvariants provide an array of codes, its up to the user to add them to a body AST
|
2023-04-05 00:03:56 -07:00
|
|
|
(they are not auto-added to a body)
|
|
|
|
|
2023-04-07 21:56:59 -07:00
|
|
|
Usage:
|
2023-04-07 21:29:09 -07:00
|
|
|
Code <name> = parse_<function name>( string with code );
|
|
|
|
|
|
|
|
Code <name> = def_<function name>( ..., parse_<function name>(
|
|
|
|
<string with code>
|
|
|
|
));
|
|
|
|
|
|
|
|
Code <name> = make_<function name>( ... )
|
|
|
|
{
|
|
|
|
<name>->add( parse_<function name>(
|
|
|
|
<string with code>
|
|
|
|
));
|
|
|
|
}
|
|
|
|
|
2023-04-04 13:18:30 -07:00
|
|
|
### Untyped constructions
|
|
|
|
|
2023-04-03 23:04:19 -07:00
|
|
|
Code ASTs are constructed using unvalidated strings.
|
2023-04-04 13:18:30 -07:00
|
|
|
|
|
|
|
Interface :
|
|
|
|
|
2023-04-07 21:29:09 -07:00
|
|
|
* token_fmt
|
2023-04-03 23:04:19 -07:00
|
|
|
* untyped_str
|
|
|
|
* untyped_fmt
|
|
|
|
* untyped_token_fmt
|
|
|
|
|
2023-04-05 23:21:23 -07:00
|
|
|
During serialization any untyped Code AST is has its string value directly injected inline of
|
2023-04-03 23:04:19 -07:00
|
|
|
whatever context the content existed as an entry within.
|
2023-04-06 16:19:11 -07:00
|
|
|
Even though thesee are not validated from somewhat correct c/c++ syntax or components, it doesn't mean that
|
2023-04-03 23:04:19 -07:00
|
|
|
Untyped code can be added as any component of a Code AST:
|
2023-04-04 13:18:30 -07:00
|
|
|
|
|
|
|
* Untyped code cannot have children, thus there cannot be recursive injection this way.
|
2023-04-07 21:29:09 -07:00
|
|
|
* Untyped code can only be a child of a parent of body AST, or for values of an assignment (ex: variable assignment).
|
2023-04-04 13:18:30 -07:00
|
|
|
|
2023-04-03 23:04:19 -07:00
|
|
|
These restrictions help prevent abuse of untyped code to some extent.
|
2023-04-07 21:29:09 -07:00
|
|
|
|
|
|
|
Usage Conventions:
|
|
|
|
```
|
|
|
|
Code <name> = def_varaible( <type>, <name>, untyped_<function name>(
|
|
|
|
<string with code>
|
|
|
|
));
|
|
|
|
```
|
|
|
|
|
|
|
|
Template metaprogramming in the traditional sense becomes possible with the use of `token_fmt` and parse constructors:
|
|
|
|
|
|
|
|
```
|
|
|
|
char const* token_key, token_value;
|
|
|
|
char const* template = txt(
|
|
|
|
Code with {key value} to replace with token_values
|
|
|
|
...
|
|
|
|
);
|
|
|
|
char const* gen_code_str = token_fmt( template, num_tokens, token, ... );
|
|
|
|
Code <name> = parse_<function name>( gen_code_str );
|
|
|
|
```
|
|
|
|
|
2023-04-10 18:33:06 -07:00
|
|
|
## Extent of operator overload validation:
|
|
|
|
|
|
|
|
The AST and constructors will be able to validate that the arguments provided for the operator type match the expected form:
|
|
|
|
* If return type must match a parameter
|
|
|
|
* If number of parameters is correct
|
|
|
|
* If added as a member symbol to a class or struct, that operator matches the requirements for the class (types match up)
|
|
|
|
|
|
|
|
The user is responsible for making sure the code types provided are correct
|
|
|
|
and have the desired specifiers assigned to them beforehand.
|
|
|
|
|
2023-04-07 21:29:09 -07:00
|
|
|
## Code generation and modification
|
|
|
|
|
|
|
|
There are three provided interfaces:
|
|
|
|
* Builder
|
|
|
|
* Editor
|
|
|
|
* Scanner
|
|
|
|
|
|
|
|
Editor and Scanner are disabled by default, use GEN_FEATURE_EDITOR and GEN_FEATURE_SCANNER to enable them.
|
|
|
|
|
|
|
|
### Builder is a similar object to the jai language's string_builder.
|
|
|
|
* The purpose of it is to generate a file.
|
|
|
|
* A file is specified and opened for writting using the open( file_path) ) fucntion.
|
|
|
|
* The code is provided via print( code ) function will be seralized to its buffer.
|
|
|
|
* When all seralization is finished, use the write() comamnd to write the buffer to the file.
|
|
|
|
|
|
|
|
### Editor is for editing a series of files based on a set of requests provided to it.
|
|
|
|
* The purpose is to overrite a specific file, it places its contents in a buffer to scan.
|
|
|
|
* Requests are populated using the following interface:
|
|
|
|
* add : Add code.
|
|
|
|
* remove : Remove code.
|
|
|
|
* replace: Replace code.
|
|
|
|
|
|
|
|
All three have the same parameters with exception to remove which only has SymbolInfo and Policy:
|
|
|
|
* SymbolInfo:
|
|
|
|
* File : The file the symbol resides in.
|
|
|
|
Leave null to indicate to search all files.
|
|
|
|
* Marker : #define symbol that indicates a location or following signature is valid to manipulate.
|
|
|
|
Leave null to indicate that the signature should only be used.
|
|
|
|
* Signature : Use a Code symbol to find a valid location to manipulate, can be further filtered with the marker.
|
|
|
|
Leave null to indicate that the marker should only be used.
|
|
|
|
|
|
|
|
* Policy : Additional policy info for completing the request (empty for now)
|
|
|
|
* Code : Code to inject if adding, or replace existing code with.
|
|
|
|
|
|
|
|
Additionally if GEN_FEATURE_EDITOR_REFACTOR is defined, refactor( file_path, specification_path ) wil be made available.
|
|
|
|
Refactor is based of the refactor library and uses its interface.
|
|
|
|
It will on call add a request to the queue to run the refactor script on the file.
|
|
|
|
|
|
|
|
### Scanner allows the user to generate Code ASTs by reading files.
|
|
|
|
* The purpose is to grab definitions to generate metadata or generate new code from these definitions.
|
|
|
|
* Requests are populated using the add( SymbolInfo, Policy ) function.
|
|
|
|
The symbol info is the same as the one used for the editor. So is the case with Policy.
|
|
|
|
|
|
|
|
The file will only be read from, no writting supported.
|
|
|
|
|
|
|
|
One great use case is for example: generating the single-header library for gencpp!
|
|
|
|
|
|
|
|
### Additional Info (Editor and Scanner)
|
|
|
|
|
|
|
|
When all requests have been populated, call process_requests().
|
|
|
|
It will provide an output of receipt data of the results when it completes.
|
|
|
|
|
|
|
|
Files may be added to the Editor and Scanner additionally with add_files( num, files ).
|
|
|
|
This is intended for when you have requests that are for multiple files.
|
|
|
|
|
|
|
|
Request queue in both Editor and Scanner are cleared once process_requests completes.
|
|
|
|
|
2023-04-10 18:33:06 -07:00
|
|
|
### On multi-threading:
|
2023-04-07 21:29:09 -07:00
|
|
|
|
2023-04-10 18:33:06 -07:00
|
|
|
Currently supported but want to. The following changes would have to be made:
|
|
|
|
* Setup static data accesss with fences if more than one thread will generate ASTs ( or keep a different set for each thread)
|
2023-04-07 21:29:09 -07:00
|
|
|
* Make sure local peristent data of functions are also thread local.
|
|
|
|
* The builder should be done on a per-thread basis.
|
|
|
|
* Due to the design of the editor and scanner, it will most likely
|
|
|
|
be best to make each file a job to process request entries on.
|
|
|
|
Receipts should have an an array to store per thread.
|
|
|
|
They can be combined to the final reciepts array when all files have been processed.
|
|
|
|
|
|
|
|
For now single-threaded has a bunch of optimization that most likely have done to it and will be more than capable
|
|
|
|
for the majority of projects this thing is intended for. (IF you use this on Unreal... well your asking for it...)
|
|
|
|
|
|
|
|
### Notes on extending with whatever features you want
|
|
|
|
|
|
|
|
This library is very small, and you can easily extend it.
|
|
|
|
|
|
|
|
The untyped codes and builder/editor/scanner can be technically be used to circumvent
|
|
|
|
any sort of constrictions the library has with: modern c++, templates, macros, etc.
|
|
|
|
|
|
|
|
Typical use case is for getting define constants an old C/C++ library with the scanner:
|
|
|
|
Code parse_defines() can emit a custom code AST with Macro_Constant type.
|
|
|
|
|
|
|
|
Another would be getting preprocessor or template metaprogramming Codes from Unreal Engine definitions.
|
2023-04-03 00:55:28 -07:00
|
|
|
*/
|
|
|
|
|
2023-04-01 19:21:46 -07:00
|
|
|
#pragma once
|
|
|
|
|
|
|
|
#include "Bloat.hpp"
|
|
|
|
|
2023-04-03 23:04:19 -07:00
|
|
|
// Temporarily here for debugging purposes.
|
2023-04-05 23:21:23 -07:00
|
|
|
#define GEN_BAN_CPP_TEMPLATES
|
|
|
|
#define GEN_DEFINE_DSL
|
2023-04-03 23:04:19 -07:00
|
|
|
#define GEN_DEFINE_LIBRARY_CODE_CONSTANTS
|
2023-04-09 21:38:47 -07:00
|
|
|
// #define GEN_DONT_USE_FATAL
|
|
|
|
#define GEN_ENFORCE_READONLY_AST
|
2023-04-18 19:47:59 -07:00
|
|
|
|
|
|
|
#define GEN_FEATURE_INCREMENTAL
|
|
|
|
#define GEN_FEATURE_PARSING
|
2023-04-07 09:31:50 -07:00
|
|
|
#define GEN_FEATURE_EDITOR
|
|
|
|
#define GEN_FEATURE_SCANNER
|
2023-04-03 00:55:28 -07:00
|
|
|
|
2023-04-05 23:21:23 -07:00
|
|
|
|
2023-04-10 18:33:06 -07:00
|
|
|
#ifdef gen_time
|
2023-04-01 19:21:46 -07:00
|
|
|
namespace gen
|
|
|
|
{
|
2023-04-03 23:04:19 -07:00
|
|
|
using LogFailType = sw(*)(char const*, ...);
|
2023-04-01 19:21:46 -07:00
|
|
|
|
2023-04-05 23:21:23 -07:00
|
|
|
# ifdef GEN_BAN_CPP_TEMPLATES
|
2023-04-06 16:19:11 -07:00
|
|
|
# define template static_assert("Templates are banned within gen_time scope blocks")
|
2023-04-05 23:21:23 -07:00
|
|
|
# endif
|
2023-04-01 19:21:46 -07:00
|
|
|
|
2023-04-09 21:38:47 -07:00
|
|
|
// By default this library will either crash or exit if an error is detected while generating codes.
|
|
|
|
// Even if set to not use fatal, fatal will still be used for memory failures as the library is unusable when they occur.
|
|
|
|
# ifdef GEN_DONT_USE_FATAL
|
|
|
|
ct LogFailType log_failure = log_fmt;
|
2023-04-05 23:21:23 -07:00
|
|
|
# else
|
2023-04-09 21:38:47 -07:00
|
|
|
ct LogFailType log_failure = fatal;
|
2023-04-05 23:21:23 -07:00
|
|
|
# endif
|
2023-04-01 19:21:46 -07:00
|
|
|
|
2023-04-03 00:55:28 -07:00
|
|
|
namespace ECode
|
2023-04-01 19:21:46 -07:00
|
|
|
{
|
2023-04-10 18:33:06 -07:00
|
|
|
# define Define_Types \
|
|
|
|
Entry( Untyped ) \
|
|
|
|
Entry( Access_Public ) \
|
|
|
|
Entry( Access_Protected ) \
|
|
|
|
Entry( Access_Private ) \
|
|
|
|
Entry( Class ) \
|
|
|
|
Entry( Class_Fwd ) \
|
|
|
|
Entry( Class_Body ) \
|
|
|
|
Entry( Enum ) \
|
|
|
|
Entry( Enum_Fwd ) \
|
|
|
|
Entry( Enum_Body ) \
|
|
|
|
Entry( Enum_Class ) \
|
|
|
|
Entry( Enum_Class_Fwd ) \
|
|
|
|
Entry( Execution ) \
|
|
|
|
Entry( Friend ) \
|
|
|
|
Entry( Function ) \
|
|
|
|
Entry( Function_Fwd ) \
|
|
|
|
Entry( Function_Body ) \
|
|
|
|
Entry( Global_Body ) \
|
|
|
|
Entry( Namespace ) \
|
|
|
|
Entry( Namespace_Body ) \
|
|
|
|
Entry( Operator ) \
|
|
|
|
Entry( Operator_Fwd ) \
|
|
|
|
Entry( Operator_Member ) \
|
|
|
|
Entry( Operator_Member_Fwd ) \
|
|
|
|
Entry( Parameters ) \
|
|
|
|
Entry( Specifiers ) \
|
|
|
|
Entry( Struct ) \
|
|
|
|
Entry( Struct_Fwd ) \
|
|
|
|
Entry( Struct_Body ) \
|
|
|
|
Entry( Variable ) \
|
|
|
|
Entry( Typedef ) \
|
|
|
|
Entry( Typename ) \
|
2023-04-18 19:47:59 -07:00
|
|
|
Entry( Union ) \
|
2023-04-10 18:33:06 -07:00
|
|
|
Entry( Using ) \
|
2023-04-09 21:38:47 -07:00
|
|
|
Entry( Using_Namespace )
|
2023-04-07 09:31:50 -07:00
|
|
|
|
|
|
|
enum Type : u32
|
2023-04-01 19:21:46 -07:00
|
|
|
{
|
2023-04-07 09:31:50 -07:00
|
|
|
# define Entry( Type ) Type,
|
|
|
|
Define_Types
|
|
|
|
# undef Entry
|
|
|
|
|
|
|
|
Num_Types,
|
|
|
|
Invalid
|
2023-04-01 19:21:46 -07:00
|
|
|
};
|
|
|
|
|
2023-04-06 16:19:11 -07:00
|
|
|
inline
|
2023-04-10 18:33:06 -07:00
|
|
|
char const* to_str( Type type )
|
2023-04-01 19:21:46 -07:00
|
|
|
{
|
2023-04-05 23:21:23 -07:00
|
|
|
static
|
2023-04-03 00:55:28 -07:00
|
|
|
char const* lookup[Num_Types] = {
|
2023-04-07 09:31:50 -07:00
|
|
|
# define Entry( Type ) txt( Type ),
|
|
|
|
Define_Types
|
|
|
|
# undef Entry
|
2023-04-03 00:55:28 -07:00
|
|
|
};
|
2023-04-01 19:21:46 -07:00
|
|
|
|
2023-04-03 00:55:28 -07:00
|
|
|
return lookup[ type ];
|
2023-04-01 19:21:46 -07:00
|
|
|
}
|
2023-04-07 09:31:50 -07:00
|
|
|
|
|
|
|
# undef Define_Types
|
2023-04-03 00:55:28 -07:00
|
|
|
}
|
|
|
|
using CodeT = ECode::Type;
|
2023-04-01 19:21:46 -07:00
|
|
|
|
2023-04-07 21:29:09 -07:00
|
|
|
// Used to indicate if enum definitoin is an enum class or regular enum.
|
|
|
|
enum class EnumT : u8
|
|
|
|
{
|
|
|
|
Regular,
|
|
|
|
Class
|
|
|
|
};
|
|
|
|
|
|
|
|
ct EnumT EnumClass = EnumT::Class;
|
|
|
|
ct EnumT EnumRegular = EnumT::Regular;
|
|
|
|
|
|
|
|
enum class UsingT : u8
|
|
|
|
{
|
|
|
|
Regular,
|
|
|
|
Namespace
|
|
|
|
};
|
|
|
|
|
|
|
|
ct UsingT UsingRegular = UsingT::Regular;
|
|
|
|
ct UsingT UsingNamespace = UsingT::Namespace;
|
|
|
|
|
2023-04-03 23:04:19 -07:00
|
|
|
namespace EOperator
|
|
|
|
{
|
2023-04-07 21:29:09 -07:00
|
|
|
# define Define_Operators \
|
|
|
|
Entry( Assign, = ) \
|
|
|
|
Entry( Assign_Add, += ) \
|
|
|
|
Entry( Assign_Subtract, -= ) \
|
|
|
|
Entry( Assgin_Multiply, *= ) \
|
|
|
|
Entry( Assgin_Divide, /= ) \
|
|
|
|
Entry( Assgin_Modulo, %= ) \
|
|
|
|
Entry( Assgin_BAnd, &= ) \
|
2023-04-18 19:47:59 -07:00
|
|
|
Entry( Assgin_BOr, |= ) \
|
2023-04-07 21:29:09 -07:00
|
|
|
Entry( Assign_BXOr, ^= ) \
|
|
|
|
Entry( Assign_LShift, <<= ) \
|
|
|
|
Entry( Assign_RShift, >>= ) \
|
|
|
|
Entry( Increment, ++ ) \
|
|
|
|
Entry( Decrement, -- ) \
|
|
|
|
Entry( Unary_Plus, + ) \
|
|
|
|
Entry( Unary_Minus, - ) \
|
|
|
|
Entry( Add, + ) \
|
|
|
|
Entry( Subtract, - ) \
|
|
|
|
Entry( Multiply, * ) \
|
|
|
|
Entry( Divide, / ) \
|
|
|
|
Entry( Modulo, % ) \
|
|
|
|
Entry( BNot, ~ ) \
|
|
|
|
Entry( BAnd, & ) \
|
|
|
|
Entry( BOr, | ) \
|
|
|
|
Entry( BXOr, ^ ) \
|
|
|
|
Entry( LShift, << ) \
|
|
|
|
Entry( RShift, >> ) \
|
|
|
|
Entry( LNot, ! ) \
|
|
|
|
Entry( LAnd, && ) \
|
|
|
|
Entry( LOr, || ) \
|
|
|
|
Entry( Equals, == ) \
|
|
|
|
Entry( NotEquals, != ) \
|
|
|
|
Entry( Lesser, < ) \
|
|
|
|
Entry( Greater, > ) \
|
|
|
|
Entry( LesserEqual, <= ) \
|
|
|
|
Entry( GreaterEqual, >= ) \
|
|
|
|
Entry( Subscript, [] ) \
|
|
|
|
Entry( Indirection, * ) \
|
|
|
|
Entry( AddressOf, & ) \
|
|
|
|
Entry( MemberOfPointer, -> ) \
|
|
|
|
Entry( PtrToMemOfPtr, ->* ) \
|
|
|
|
Entry( FunctionCall, () )
|
2023-04-07 09:31:50 -07:00
|
|
|
|
|
|
|
enum Type : u32
|
2023-04-03 23:04:19 -07:00
|
|
|
{
|
2023-04-18 19:47:59 -07:00
|
|
|
# define Entry( Type_, Token_ ) Type_,
|
2023-04-07 09:31:50 -07:00
|
|
|
Define_Operators
|
|
|
|
# undef Entry
|
|
|
|
Comma,
|
2023-04-03 23:04:19 -07:00
|
|
|
|
2023-04-07 09:31:50 -07:00
|
|
|
Num_Ops,
|
|
|
|
Invalid
|
2023-04-03 23:04:19 -07:00
|
|
|
};
|
|
|
|
|
2023-04-05 23:21:23 -07:00
|
|
|
inline
|
2023-04-10 18:33:06 -07:00
|
|
|
char const* to_str( Type op )
|
2023-04-03 23:04:19 -07:00
|
|
|
{
|
2023-04-09 21:38:47 -07:00
|
|
|
using something = u8;
|
|
|
|
typedef u8 another;
|
|
|
|
|
2023-04-05 00:03:56 -07:00
|
|
|
local_persist
|
2023-04-03 23:04:19 -07:00
|
|
|
char const* lookup[ Num_Ops ] = {
|
2023-04-18 19:47:59 -07:00
|
|
|
# define Entry( Type_, Token_ ) txt(Token_),
|
2023-04-07 09:31:50 -07:00
|
|
|
Define_Operators
|
|
|
|
# undef Entry
|
|
|
|
","
|
2023-04-03 23:04:19 -07:00
|
|
|
};
|
|
|
|
|
|
|
|
return lookup[ op ];
|
|
|
|
}
|
2023-04-07 21:29:09 -07:00
|
|
|
|
2023-04-07 09:31:50 -07:00
|
|
|
# undef Define_Operators
|
2023-04-03 23:04:19 -07:00
|
|
|
}
|
|
|
|
using OperatorT = EOperator::Type;
|
|
|
|
|
|
|
|
namespace ESpecifier
|
|
|
|
{
|
2023-04-07 09:31:50 -07:00
|
|
|
# if defined(ZPL_SYSTEM_WINDOWS)
|
|
|
|
# define API_Export_Code __declspec(dllexport)
|
|
|
|
# define API_Import_Code __declspec(dllimport)
|
2023-04-18 19:47:59 -07:00
|
|
|
# define API_Keyword __declspec
|
2023-04-07 09:31:50 -07:00
|
|
|
# elif defined(ZPL_SYSTEM_MACOS)
|
|
|
|
# define API_Export_Code __attribute__ ((visibility ("default")))
|
|
|
|
# define API_Import_Code __attribute__ ((visibility ("default")))
|
2023-04-18 19:47:59 -07:00
|
|
|
# define API_Keyword __attribute__
|
2023-04-07 09:31:50 -07:00
|
|
|
# endif
|
|
|
|
|
2023-04-11 19:18:02 -07:00
|
|
|
# define Define_Specifiers \
|
2023-04-07 21:29:09 -07:00
|
|
|
Entry( API_Import, API_Export_Code ) \
|
|
|
|
Entry( API_Export, API_Import_Code ) \
|
2023-04-11 22:24:43 -07:00
|
|
|
Entry( Attribute, "You cannot stringize an attribute this way" ) \
|
|
|
|
Entry( Alignas, alignas ) \
|
|
|
|
Entry( Array_Decl, "You cannot stringize an array declare this way" ) \
|
2023-04-07 21:29:09 -07:00
|
|
|
Entry( C_Linkage, extern "C" ) \
|
2023-04-18 19:47:59 -07:00
|
|
|
Entry( Const, const ) \
|
2023-04-07 21:29:09 -07:00
|
|
|
Entry( Consteval, consteval ) \
|
|
|
|
Entry( Constexpr, constexpr ) \
|
|
|
|
Entry( Constinit, constinit ) \
|
|
|
|
Entry( Export, export ) \
|
|
|
|
Entry( External_Linkage, extern ) \
|
|
|
|
Entry( Import, import ) \
|
|
|
|
Entry( Inline, inline ) \
|
|
|
|
Entry( Internal_Linkage, static ) \
|
|
|
|
Entry( Local_Persist, static ) \
|
|
|
|
Entry( Module, module ) \
|
|
|
|
Entry( Mutable, mutable ) \
|
2023-04-10 18:33:06 -07:00
|
|
|
Entry( Ptr, * ) \
|
|
|
|
Entry( Ref, & ) \
|
2023-04-07 21:29:09 -07:00
|
|
|
Entry( Register, register ) \
|
|
|
|
Entry( RValue, && ) \
|
|
|
|
Entry( Static_Member, static ) \
|
2023-04-18 19:47:59 -07:00
|
|
|
Entry( Thread_Local, thread_local ) \
|
2023-04-18 21:56:43 -07:00
|
|
|
Entry( Volatile, volatile ) \
|
|
|
|
Entry( Type_Signed, signed ) \
|
|
|
|
Entry( Type_Unsigned, unsigned ) \
|
|
|
|
Entry( Type_Short, short ) \
|
|
|
|
Entry( Type_Long, long )
|
2023-04-07 09:31:50 -07:00
|
|
|
|
|
|
|
enum Type : u32
|
2023-04-03 23:04:19 -07:00
|
|
|
{
|
2023-04-07 09:31:50 -07:00
|
|
|
# define Entry( Specifier, Code ) Specifier,
|
|
|
|
Define_Specifiers
|
|
|
|
# undef Entry
|
2023-04-05 00:03:56 -07:00
|
|
|
|
2023-04-03 23:04:19 -07:00
|
|
|
Num_Specifiers,
|
2023-04-07 09:31:50 -07:00
|
|
|
Invalid,
|
2023-04-03 23:04:19 -07:00
|
|
|
};
|
|
|
|
|
|
|
|
// Specifier to string
|
|
|
|
inline
|
|
|
|
char const* to_str( Type specifier )
|
|
|
|
{
|
2023-04-05 00:03:56 -07:00
|
|
|
local_persist
|
2023-04-03 23:04:19 -07:00
|
|
|
char const* lookup[ Num_Specifiers ] = {
|
2023-04-07 09:31:50 -07:00
|
|
|
# define Entry( Spec_, Code_ ) txt(Code_),
|
|
|
|
Define_Specifiers
|
|
|
|
# undef Entry
|
2023-04-03 23:04:19 -07:00
|
|
|
};
|
|
|
|
|
|
|
|
return lookup[ specifier ];
|
|
|
|
}
|
2023-04-05 23:21:23 -07:00
|
|
|
|
2023-04-11 19:18:02 -07:00
|
|
|
inline
|
2023-04-03 23:04:19 -07:00
|
|
|
Type to_type( char const* str, s32 length )
|
|
|
|
{
|
2023-04-05 00:03:56 -07:00
|
|
|
local_persist
|
2023-04-03 23:04:19 -07:00
|
|
|
u32 keymap[ Num_Specifiers ];
|
|
|
|
do_once_start
|
|
|
|
for ( u32 index = 0; index < Num_Specifiers; index++ )
|
|
|
|
{
|
|
|
|
char const* enum_str = to_str( (Type)index );
|
|
|
|
|
2023-04-09 10:59:39 -07:00
|
|
|
keymap[index] = crc32( enum_str, strnlen(enum_str, 42) );
|
2023-04-03 23:04:19 -07:00
|
|
|
}
|
|
|
|
do_once_end
|
|
|
|
|
|
|
|
u32 hash = crc32(str, length );
|
|
|
|
|
|
|
|
for ( u32 index = 0; index < Num_Specifiers; index++ )
|
|
|
|
{
|
|
|
|
if ( keymap[index] == hash )
|
|
|
|
return (Type)index;
|
|
|
|
}
|
|
|
|
|
|
|
|
return Invalid;
|
|
|
|
}
|
2023-04-07 21:29:09 -07:00
|
|
|
|
|
|
|
# undef Define_Specifiers
|
2023-04-03 23:04:19 -07:00
|
|
|
}
|
|
|
|
using SpecifierT = ESpecifier::Type;
|
|
|
|
|
2023-04-05 00:03:56 -07:00
|
|
|
#pragma region Data Structures
|
2023-04-10 18:33:06 -07:00
|
|
|
// Implements basic string interning. Data structure is based off the ZPL Hashtable.
|
|
|
|
ZPL_TABLE_DECLARE( ZPL_EXTERN, StringTable, str_tbl_, String );
|
|
|
|
|
|
|
|
// Represents strings cached with the string table.
|
|
|
|
// Should never be modified, if changed string is desired, cache_string( str ) another.
|
|
|
|
using StringCached = char const*;
|
|
|
|
|
|
|
|
// Desired width of the AST data structure.
|
|
|
|
ct u32 AST_POD_Size = 256;
|
|
|
|
|
2023-04-03 00:55:28 -07:00
|
|
|
// TODO: If perf needs it, convert layout an SOA format.
|
2023-04-05 23:21:23 -07:00
|
|
|
/*
|
2023-04-03 00:55:28 -07:00
|
|
|
Simple AST POD with functionality to seralize into C++ syntax.
|
2023-04-01 19:21:46 -07:00
|
|
|
|
2023-04-03 00:55:28 -07:00
|
|
|
ASTs are currently stored as an AOS. They are always reconstructed on demand.
|
|
|
|
Thus redundant AST can easily occur.
|
|
|
|
Not sure if its better to store them in a hashmap.
|
2023-04-10 18:33:06 -07:00
|
|
|
|
|
|
|
Any type specific functions assume the construction of the AST was done correctly.
|
2023-04-03 00:55:28 -07:00
|
|
|
*/
|
|
|
|
struct AST
|
|
|
|
{
|
2023-04-07 21:29:09 -07:00
|
|
|
# pragma region Member Functions
|
2023-04-10 18:33:06 -07:00
|
|
|
|
|
|
|
// Used with incremental constructors
|
|
|
|
// Adds and checks entries to see if they are valid additions the type of ast.
|
2023-04-05 00:03:56 -07:00
|
|
|
bool add( AST* other );
|
|
|
|
|
2023-04-10 18:33:06 -07:00
|
|
|
void add_entry( AST* other );
|
|
|
|
|
2023-04-01 19:21:46 -07:00
|
|
|
forceinline
|
2023-04-10 18:33:06 -07:00
|
|
|
AST* body()
|
2023-04-01 19:21:46 -07:00
|
|
|
{
|
2023-04-10 18:33:06 -07:00
|
|
|
return Entries[0];
|
|
|
|
}
|
2023-04-03 00:55:28 -07:00
|
|
|
|
2023-04-10 18:33:06 -07:00
|
|
|
forceinline
|
|
|
|
bool check();
|
|
|
|
|
|
|
|
AST* duplicate();
|
2023-04-01 19:21:46 -07:00
|
|
|
|
2023-04-10 18:33:06 -07:00
|
|
|
forceinline
|
|
|
|
bool has_entries()
|
|
|
|
{
|
|
|
|
return Entries[0];
|
2023-04-07 09:31:50 -07:00
|
|
|
}
|
2023-04-07 21:29:09 -07:00
|
|
|
|
2023-04-05 00:03:56 -07:00
|
|
|
forceinline
|
2023-04-10 18:33:06 -07:00
|
|
|
bool is_invalid()
|
2023-04-05 00:03:56 -07:00
|
|
|
{
|
2023-04-10 18:33:06 -07:00
|
|
|
return Type != ECode::Invalid;
|
2023-04-05 00:03:56 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
forceinline
|
2023-04-10 18:33:06 -07:00
|
|
|
s32 num_entries()
|
|
|
|
{
|
|
|
|
return DynamicEntries ? array_count(Entries) : StaticIndex;
|
|
|
|
}
|
2023-04-05 00:03:56 -07:00
|
|
|
|
2023-04-10 18:33:06 -07:00
|
|
|
// Parameter
|
2023-04-07 09:31:50 -07:00
|
|
|
|
2023-04-11 19:18:02 -07:00
|
|
|
bool add_param( AST* type, s32 length, char const* name );
|
|
|
|
|
2023-04-01 19:21:46 -07:00
|
|
|
forceinline
|
2023-04-10 18:33:06 -07:00
|
|
|
AST* get_param( s32 index )
|
2023-04-01 19:21:46 -07:00
|
|
|
{
|
2023-04-10 18:33:06 -07:00
|
|
|
if ( index <= 0 )
|
|
|
|
return this;
|
2023-04-01 19:21:46 -07:00
|
|
|
|
2023-04-10 18:33:06 -07:00
|
|
|
return Entries[ index + 1 ];
|
2023-04-01 19:21:46 -07:00
|
|
|
}
|
|
|
|
|
2023-04-03 00:55:28 -07:00
|
|
|
forceinline
|
2023-04-10 18:33:06 -07:00
|
|
|
s32 param_count()
|
2023-04-03 00:55:28 -07:00
|
|
|
{
|
2023-04-10 18:33:06 -07:00
|
|
|
// The first entry (which holds the type) represents the first parameter.
|
|
|
|
return num_entries();
|
2023-04-03 00:55:28 -07:00
|
|
|
}
|
|
|
|
|
2023-04-07 09:31:50 -07:00
|
|
|
forceinline
|
2023-04-10 18:33:06 -07:00
|
|
|
AST* param_type()
|
|
|
|
{
|
|
|
|
return Entries[0];
|
|
|
|
}
|
|
|
|
|
2023-04-11 19:18:02 -07:00
|
|
|
// Specifiers
|
|
|
|
|
|
|
|
inline
|
|
|
|
bool add_specifier( SpecifierT spec )
|
|
|
|
{
|
|
|
|
if ( StaticIndex == AST::ArrSpecs_Cap )
|
|
|
|
{
|
|
|
|
log_failure("AST::add_specifier: Attempted to add over %d specifiers to a specifiers AST!", AST::ArrSpecs_Cap );
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
ArrSpecs[ StaticIndex ] = spec;
|
|
|
|
StaticIndex++;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
inline
|
|
|
|
bool has_specifier( SpecifierT spec )
|
|
|
|
{
|
|
|
|
for ( s32 Index = 0; Index < StaticIndex; Index++ )
|
|
|
|
{
|
|
|
|
if ( ArrSpecs[StaticIndex] == spec )
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2023-04-10 18:33:06 -07:00
|
|
|
// Typename
|
|
|
|
|
|
|
|
forceinline
|
|
|
|
bool typename_is_ptr()
|
|
|
|
{
|
|
|
|
zpl::assert_crash("not implemented");
|
|
|
|
}
|
|
|
|
|
|
|
|
forceinline
|
|
|
|
bool typename_is_ref()
|
|
|
|
{
|
|
|
|
zpl::assert_crash("not implemented");
|
|
|
|
}
|
|
|
|
|
|
|
|
forceinline
|
|
|
|
AST* typename_specifiers()
|
|
|
|
{
|
|
|
|
return Entries[0];
|
|
|
|
}
|
|
|
|
|
|
|
|
// Serialization
|
|
|
|
|
|
|
|
forceinline
|
|
|
|
char const* debug_str()
|
2023-04-07 09:31:50 -07:00
|
|
|
{
|
|
|
|
char const* fmt = txt(
|
|
|
|
\nCode Debug:
|
|
|
|
\nType : %s
|
|
|
|
\nReadonly: %s
|
|
|
|
\nParent : %s
|
|
|
|
\nName : %s
|
|
|
|
\nComment : %s
|
|
|
|
);
|
|
|
|
|
2023-04-07 21:29:09 -07:00
|
|
|
// These should be used immediately in a log.
|
|
|
|
// Thus if its desired to keep the debug str
|
|
|
|
// for multiple calls to bprintf,
|
|
|
|
// allocate this to proper string.
|
2023-04-10 18:33:06 -07:00
|
|
|
return bprintf( fmt
|
2023-04-07 09:31:50 -07:00
|
|
|
, type_str()
|
|
|
|
, Readonly ? "true" : "false"
|
|
|
|
, Parent ? Parent->Name : ""
|
|
|
|
, Name ? Name : ""
|
|
|
|
, Comment ? Comment : ""
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2023-04-03 00:55:28 -07:00
|
|
|
forceinline
|
2023-04-10 18:33:06 -07:00
|
|
|
char const* type_str()
|
2023-04-03 00:55:28 -07:00
|
|
|
{
|
2023-04-10 18:33:06 -07:00
|
|
|
return ECode::to_str( Type );
|
2023-04-03 00:55:28 -07:00
|
|
|
}
|
|
|
|
|
2023-04-09 10:59:39 -07:00
|
|
|
String to_string() const;
|
2023-04-07 21:29:09 -07:00
|
|
|
# pragma endregion Member Functions
|
2023-04-11 19:18:02 -07:00
|
|
|
constexpr static
|
|
|
|
uw ArrS_Cap =
|
2023-04-10 18:33:06 -07:00
|
|
|
( AST_POD_Size
|
|
|
|
- sizeof(AST*)
|
|
|
|
- sizeof(StringCached) * 2
|
|
|
|
- sizeof(CodeT)
|
|
|
|
- sizeof(OperatorT)
|
|
|
|
- sizeof(bool) * 2
|
|
|
|
- sizeof(u8) * 7 )
|
|
|
|
/ sizeof(AST*);
|
|
|
|
|
2023-04-11 19:18:02 -07:00
|
|
|
constexpr static
|
|
|
|
uw ArrSpecs_Cap = ArrS_Cap * (sizeof(AST*) / sizeof(SpecifierT));
|
|
|
|
|
|
|
|
# define Using_Code_POD \
|
|
|
|
union { \
|
|
|
|
AST* ArrStatic[AST::ArrS_Cap]; \
|
|
|
|
Array(AST*) Entries; \
|
|
|
|
StringCached Content; \
|
|
|
|
SpecifierT ArrSpecs[AST::ArrSpecs_Cap]; \
|
|
|
|
}; \
|
|
|
|
AST* Parent; \
|
|
|
|
StringCached Name; \
|
|
|
|
StringCached Comment; \
|
|
|
|
CodeT Type; \
|
|
|
|
OperatorT Op; \
|
|
|
|
bool Readonly; \
|
|
|
|
bool DynamicEntries; \
|
|
|
|
u8 StaticIndex; \
|
2023-04-10 18:33:06 -07:00
|
|
|
u8 _Align_Pad[6];
|
|
|
|
|
|
|
|
Using_Code_POD
|
2023-04-03 00:55:28 -07:00
|
|
|
};
|
|
|
|
|
|
|
|
struct CodePOD
|
|
|
|
{
|
2023-04-10 18:33:06 -07:00
|
|
|
Using_Code_POD
|
2023-04-07 21:29:09 -07:00
|
|
|
# undef Using_CodePOD;
|
2023-04-03 00:55:28 -07:00
|
|
|
};
|
|
|
|
|
2023-04-10 18:33:06 -07:00
|
|
|
ct sw size_AST = sizeof(AST);
|
|
|
|
ct sw size_POD = sizeof(CodePOD);
|
2023-04-07 09:31:50 -07:00
|
|
|
|
2023-04-03 00:55:28 -07:00
|
|
|
// Its intended for the AST to have equivalent size to its POD.
|
|
|
|
// All extra functionality within the AST namespace should just be syntatic sugar.
|
2023-04-10 18:33:06 -07:00
|
|
|
static_assert( sizeof(AST) == sizeof(CodePOD), "ERROR: AST IS NOT POD" );
|
|
|
|
static_assert( sizeof(CodePOD) == AST_POD_Size, "ERROR: AST POD is not size of AST_POD_Size" );
|
2023-04-03 00:55:28 -07:00
|
|
|
|
|
|
|
/*
|
|
|
|
AST* typedef as to not constantly have to add the '*' as this is written often..
|
|
|
|
|
2023-04-05 23:21:23 -07:00
|
|
|
If GEN_ENFORCE_READONLY_AST is defined, readonly assertions will be done on any member dreference,
|
2023-04-03 00:55:28 -07:00
|
|
|
and the 'gen API' related functions. will set their created ASTs to readonly before returning.
|
|
|
|
|
|
|
|
Casting to AST* will bypass.
|
|
|
|
*/
|
|
|
|
struct Code
|
|
|
|
{
|
2023-04-07 21:29:09 -07:00
|
|
|
# pragma region Statics
|
|
|
|
// Used internally for the most part to identify invaidly generated code.
|
2023-04-10 18:33:06 -07:00
|
|
|
static Code Invalid;
|
2023-04-07 21:29:09 -07:00
|
|
|
# pragma endregion Statics
|
|
|
|
|
|
|
|
# pragma region Member Functions
|
2023-04-03 23:04:19 -07:00
|
|
|
Code body()
|
|
|
|
{
|
2023-04-06 16:19:11 -07:00
|
|
|
if ( ast == nullptr )
|
|
|
|
{
|
|
|
|
log_failure("Code::body: AST is null!");
|
|
|
|
return Invalid;
|
|
|
|
}
|
|
|
|
|
2023-04-03 23:04:19 -07:00
|
|
|
if ( ast->Type == ECode::Invalid )
|
2023-04-05 00:03:56 -07:00
|
|
|
{
|
|
|
|
log_failure("Code::body: Type is invalid, cannot get");
|
2023-04-05 23:21:23 -07:00
|
|
|
return Invalid;
|
2023-04-05 00:03:56 -07:00
|
|
|
}
|
2023-04-03 23:04:19 -07:00
|
|
|
|
|
|
|
if ( ast->Entries == nullptr || array_count(ast->Entries) == 0 )
|
2023-04-05 00:03:56 -07:00
|
|
|
{
|
|
|
|
log_failure("Code::body: Entries of ast not properly setup.");
|
2023-04-05 23:21:23 -07:00
|
|
|
return Invalid;
|
2023-04-05 00:03:56 -07:00
|
|
|
}
|
2023-04-03 23:04:19 -07:00
|
|
|
|
2023-04-05 23:21:23 -07:00
|
|
|
# ifdef GEN_ENFORCE_READONLY_AST
|
2023-04-05 00:03:56 -07:00
|
|
|
if ( ast->Readonly )
|
|
|
|
{
|
2023-04-05 23:21:23 -07:00
|
|
|
log_failure("Attempted to a body AST from a readonly AST!");
|
|
|
|
return Invalid;
|
2023-04-05 00:03:56 -07:00
|
|
|
}
|
2023-04-05 23:21:23 -07:00
|
|
|
# endif
|
2023-04-05 00:03:56 -07:00
|
|
|
|
|
|
|
return * (Code*)( ast->body() );
|
2023-04-03 23:04:19 -07:00
|
|
|
}
|
|
|
|
|
2023-04-01 19:21:46 -07:00
|
|
|
forceinline
|
2023-04-03 23:04:19 -07:00
|
|
|
void lock()
|
2023-04-01 19:21:46 -07:00
|
|
|
{
|
2023-04-03 23:04:19 -07:00
|
|
|
ast->Readonly = true;
|
2023-04-01 19:21:46 -07:00
|
|
|
}
|
|
|
|
|
2023-04-03 23:04:19 -07:00
|
|
|
forceinline
|
2023-04-10 18:33:06 -07:00
|
|
|
char const* to_string()
|
|
|
|
{
|
|
|
|
return ast->to_string();
|
|
|
|
}
|
|
|
|
|
|
|
|
forceinline
|
|
|
|
operator bool()
|
2023-04-03 23:04:19 -07:00
|
|
|
{
|
2023-04-09 21:38:47 -07:00
|
|
|
return ast;
|
2023-04-03 23:04:19 -07:00
|
|
|
}
|
|
|
|
|
2023-04-10 18:33:06 -07:00
|
|
|
forceinline
|
|
|
|
bool operator ==( Code other )
|
2023-04-01 19:21:46 -07:00
|
|
|
{
|
2023-04-03 00:55:28 -07:00
|
|
|
return ast == other.ast;
|
2023-04-01 19:21:46 -07:00
|
|
|
}
|
|
|
|
|
2023-04-10 18:33:06 -07:00
|
|
|
forceinline
|
|
|
|
bool operator !=( Code other )
|
|
|
|
{
|
|
|
|
return ast != other.ast;
|
|
|
|
}
|
|
|
|
|
|
|
|
forceinline
|
2023-04-03 00:55:28 -07:00
|
|
|
operator AST*()
|
|
|
|
{
|
|
|
|
return ast;
|
|
|
|
}
|
2023-04-05 23:21:23 -07:00
|
|
|
|
2023-04-10 18:33:06 -07:00
|
|
|
forceinline
|
|
|
|
Code& operator=( Code other )
|
2023-04-01 19:21:46 -07:00
|
|
|
{
|
2023-04-05 00:03:56 -07:00
|
|
|
if ( ast == nullptr )
|
|
|
|
{
|
|
|
|
log_failure("Attempt to set with a null AST!");
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
2023-04-05 23:21:23 -07:00
|
|
|
#ifdef GEN_ENFORCE_READONLY_AST
|
2023-04-05 00:03:56 -07:00
|
|
|
if ( ast->Readonly )
|
|
|
|
{
|
2023-04-05 23:21:23 -07:00
|
|
|
log_failure("Attempted to set a readonly AST!");
|
2023-04-05 00:03:56 -07:00
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2023-04-03 00:55:28 -07:00
|
|
|
ast = other.ast;
|
|
|
|
|
|
|
|
return *this;
|
|
|
|
}
|
2023-04-01 19:21:46 -07:00
|
|
|
|
2023-04-03 00:55:28 -07:00
|
|
|
forceinline
|
2023-04-10 18:33:06 -07:00
|
|
|
AST* operator->()
|
2023-04-03 00:55:28 -07:00
|
|
|
{
|
|
|
|
if ( ast == nullptr )
|
2023-04-05 00:03:56 -07:00
|
|
|
{
|
|
|
|
log_failure("Attempt to dereference a nullptr!");
|
|
|
|
return nullptr;
|
|
|
|
}
|
2023-04-01 19:21:46 -07:00
|
|
|
|
2023-04-05 23:21:23 -07:00
|
|
|
# ifdef GEN_ENFORCE_READONLY_AST
|
2023-04-03 00:55:28 -07:00
|
|
|
if ( ast->Readonly )
|
2023-04-05 00:03:56 -07:00
|
|
|
{
|
2023-04-05 23:21:23 -07:00
|
|
|
log_failure("Attempted to access a member from a readonly AST!");
|
2023-04-05 00:03:56 -07:00
|
|
|
return nullptr;
|
|
|
|
}
|
2023-04-05 23:21:23 -07:00
|
|
|
# endif
|
2023-04-01 19:21:46 -07:00
|
|
|
|
2023-04-03 00:55:28 -07:00
|
|
|
return ast;
|
2023-04-01 19:21:46 -07:00
|
|
|
}
|
2023-04-07 21:29:09 -07:00
|
|
|
# pragma endregion Member Functions
|
2023-04-05 23:21:23 -07:00
|
|
|
|
2023-04-06 16:19:11 -07:00
|
|
|
AST* ast;
|
2023-04-01 19:21:46 -07:00
|
|
|
};
|
2023-04-03 23:04:19 -07:00
|
|
|
static_assert( sizeof(Code) == sizeof(AST*), "ERROR: Code is not POD" );
|
2023-04-01 19:21:46 -07:00
|
|
|
|
2023-04-03 00:55:28 -07:00
|
|
|
// Used when the its desired when omission is allowed in a definition.
|
2023-04-05 23:21:23 -07:00
|
|
|
ct Code NoCode = { nullptr };
|
|
|
|
// extern const Code InvalidCode;
|
2023-04-03 00:55:28 -07:00
|
|
|
|
|
|
|
/*
|
2023-04-07 21:29:09 -07:00
|
|
|
Type Table: Used to store Typename ASTs. Types are registered by their string literal value.
|
2023-04-01 19:21:46 -07:00
|
|
|
|
2023-04-07 21:29:09 -07:00
|
|
|
Provides interning specific to Typename ASTs.
|
|
|
|
Interning for other types should be possible (specifiers) with this, so long as they
|
|
|
|
don't have an set of child AST entries (Use the content field).
|
2023-04-03 00:55:28 -07:00
|
|
|
*/
|
2023-04-05 00:03:56 -07:00
|
|
|
ZPL_TABLE_DECLARE( ZPL_EXTERN, TypeTable, type_tbl_, Code );
|
|
|
|
#pragma endregion Data Structures
|
2023-04-02 09:35:14 -07:00
|
|
|
|
2023-04-05 00:03:56 -07:00
|
|
|
#pragma region Gen Interface
|
2023-04-09 21:38:47 -07:00
|
|
|
// Initialize the library.
|
|
|
|
// This currently just initializes the CodePool.
|
2023-04-02 09:35:14 -07:00
|
|
|
void init();
|
|
|
|
|
2023-04-10 18:33:06 -07:00
|
|
|
/*
|
|
|
|
Use this only if you know you generated the code you needed to a file.
|
|
|
|
And rather get rid of current code asts instead of growing the pool memory.
|
|
|
|
This generally can be done everytime a file is generated
|
|
|
|
TODO: In order for this to work, the type map needs its own arenas so do specifiers.
|
|
|
|
*/
|
|
|
|
void clear_code_memory();
|
2023-04-01 19:21:46 -07:00
|
|
|
|
2023-04-09 21:38:47 -07:00
|
|
|
// Used internally to retrive or make string allocations.
|
|
|
|
// Strings are stored in a series of string arenas of fixed size (SizePer_StringArena)
|
|
|
|
StringCached get_cached_string( char const* cstr, s32 length );
|
2023-04-07 09:31:50 -07:00
|
|
|
|
|
|
|
/*
|
2023-04-07 21:29:09 -07:00
|
|
|
This provides a fresh Code AST.
|
2023-04-07 09:31:50 -07:00
|
|
|
The gen interface use this as their method from getting a new AST object from the CodePool.
|
|
|
|
Use this if you want to make your own API for formatting the supported Code Types.
|
|
|
|
*/
|
|
|
|
Code make_code();
|
|
|
|
|
2023-04-09 21:38:47 -07:00
|
|
|
// This provides a fresh Code AST array for the entries field of the AST.
|
|
|
|
// This is done separately from the regular CodePool allocator.
|
2023-04-09 10:59:39 -07:00
|
|
|
Array(AST*) make_code_entries();
|
2023-04-07 21:29:09 -07:00
|
|
|
|
2023-04-05 00:03:56 -07:00
|
|
|
// Set these before calling gen's init() procedure.
|
2023-04-09 21:38:47 -07:00
|
|
|
// Data
|
2023-04-05 00:03:56 -07:00
|
|
|
|
2023-04-09 21:38:47 -07:00
|
|
|
void set_allocator_data_arrays ( AllocatorInfo data_array_allocator );
|
|
|
|
void set_allocator_code_pool ( AllocatorInfo pool_allocator );
|
|
|
|
void set_allocator_code_enries_arena( AllocatorInfo pool_allocator );
|
|
|
|
void set_allocator_string_arena ( AllocatorInfo string_allocator );
|
|
|
|
void set_allocator_string_table ( AllocatorInfo string_allocator );
|
|
|
|
void set_allocator_type_table ( AllocatorInfo type_reg_allocator );
|
2023-04-05 00:03:56 -07:00
|
|
|
|
|
|
|
# pragma region Upfront
|
2023-04-05 23:21:23 -07:00
|
|
|
Code def_class ( s32 length, char const* name, Code parent = NoCode, Code specifiers = NoCode, Code body = NoCode );
|
2023-04-07 21:29:09 -07:00
|
|
|
Code def_enum ( s32 length, char const* name, Code type = NoCode, EnumT specifier = EnumRegular, Code body = NoCode );
|
2023-04-09 21:38:47 -07:00
|
|
|
|
|
|
|
Code def_execution ( Code untyped_code );
|
|
|
|
|
2023-04-07 09:31:50 -07:00
|
|
|
Code def_friend ( Code symbol );
|
2023-04-06 16:19:11 -07:00
|
|
|
Code def_function ( s32 length, char const* name, Code params = NoCode, Code ret_type = NoCode, Code specifiers = NoCode, Code body = NoCode );
|
2023-04-05 23:21:23 -07:00
|
|
|
Code def_namespace ( s32 length, char const* name, Code body );
|
2023-04-06 16:19:11 -07:00
|
|
|
Code def_operator ( OperatorT op, Code params = NoCode, Code ret_type = NoCode, Code specifiers = NoCode, Code body = NoCode );
|
2023-04-05 23:21:23 -07:00
|
|
|
|
|
|
|
Code def_param ( Code type, s32 length, char const* name );
|
|
|
|
|
2023-04-06 16:19:11 -07:00
|
|
|
Code def_specifier ( SpecifierT specifier );
|
2023-04-05 23:21:23 -07:00
|
|
|
|
|
|
|
Code def_struct ( s32 length, char const* name, Code parent = NoCode, Code specifiers = NoCode, Code body = NoCode );
|
2023-04-11 19:18:02 -07:00
|
|
|
Code def_typedef ( s32 length, char const* name, Code type );
|
2023-04-05 23:21:23 -07:00
|
|
|
Code def_type ( s32 length, char const* name, Code specifiers = NoCode );
|
2023-04-07 21:29:09 -07:00
|
|
|
Code def_using ( s32 length, char const* name, Code type = NoCode, UsingT specifier = UsingRegular );
|
2023-04-05 23:21:23 -07:00
|
|
|
|
|
|
|
Code def_variable ( Code type, s32 length, char const* name, Code value = NoCode, Code specifiers = NoCode );
|
|
|
|
|
|
|
|
Code def_class_body ( s32 num, ... );
|
|
|
|
Code def_enum_body ( u32 num, ... );
|
2023-04-10 18:33:06 -07:00
|
|
|
Code def_enum_body ( u32 num, Code* codes );
|
2023-04-05 23:21:23 -07:00
|
|
|
Code def_global_body ( s32 num, ... );
|
2023-04-10 18:33:06 -07:00
|
|
|
Code def_global_body ( s32 num, Code* codes );
|
2023-04-05 23:21:23 -07:00
|
|
|
Code def_function_body ( s32 num, ... );
|
|
|
|
Code def_function_body ( s32 num, Code* codes );
|
|
|
|
Code def_namespace_body ( s32 num, ... );
|
2023-04-10 18:33:06 -07:00
|
|
|
Code def_namespace_body ( s32 num, Code* codes );
|
2023-04-05 23:21:23 -07:00
|
|
|
Code def_params ( s32 num, ... );
|
|
|
|
Code def_params ( s32 num, Code* params );
|
|
|
|
Code def_specifiers ( s32 num , ... );
|
|
|
|
Code def_specifiers ( s32 num, SpecifierT* specs );
|
|
|
|
Code def_struct_body ( s32 num, ... );
|
|
|
|
Code def_struct_body ( s32 num, Code* codes );
|
2023-04-05 00:03:56 -07:00
|
|
|
# pragma endregion Upfront
|
2023-04-03 23:04:19 -07:00
|
|
|
|
2023-04-05 00:03:56 -07:00
|
|
|
# pragma region Incremental
|
2023-04-18 19:47:59 -07:00
|
|
|
# ifdef GEN_FEATURE_INCREMENTAL
|
2023-04-09 10:59:39 -07:00
|
|
|
Code make_class ( s32 length, char const* name, Code parent = NoCode, Code specifiers = NoCode );
|
|
|
|
Code make_enum ( s32 length, char const* name, Code type = NoCode, EnumT specifier = EnumRegular );
|
2023-04-09 21:38:47 -07:00
|
|
|
Code make_function ( s32 length, char const* name, Code params = NoCode, Code ret_type = NoCode, Code specifiers = NoCode );
|
2023-04-09 10:59:39 -07:00
|
|
|
Code make_global_body ( s32 length = 1, char const* name = "", s32 num = 0, ... );
|
|
|
|
Code make_namespace ( s32 length, char const* name );
|
2023-04-09 21:38:47 -07:00
|
|
|
Code make_operator ( OperatorT op, Code params = NoCode, Code ret_type = NoCode, Code specifiers = NoCode );
|
2023-04-06 16:19:11 -07:00
|
|
|
Code make_params ();
|
|
|
|
Code make_specifiers ();
|
2023-04-09 21:38:47 -07:00
|
|
|
Code make_struct ( s32 length, char const* name, Code parent = NoCode, Code specifiers = NoCode );
|
2023-04-18 19:47:59 -07:00
|
|
|
# endif
|
2023-04-05 00:03:56 -07:00
|
|
|
# pragma endregion Incremental
|
2023-04-03 23:04:19 -07:00
|
|
|
|
2023-04-05 00:03:56 -07:00
|
|
|
# pragma region Parsing
|
2023-04-18 19:47:59 -07:00
|
|
|
# ifdef GEN_FEATURE_PARSING
|
2023-04-06 16:19:11 -07:00
|
|
|
Code parse_class ( s32 length, char const* class_def );
|
|
|
|
Code parse_enum ( s32 length, char const* enum_def );
|
2023-04-09 21:38:47 -07:00
|
|
|
Code parse_execution ( s32 length, char const* exec_def );
|
2023-04-07 09:31:50 -07:00
|
|
|
Code parse_friend ( s32 length, char const* friend_def );
|
2023-04-06 16:19:11 -07:00
|
|
|
Code parse_function ( s32 length, char const* fn_def );
|
|
|
|
Code parse_global_body( s32 length, char const* body_def );
|
|
|
|
Code parse_namespace ( s32 length, char const* namespace_def );
|
|
|
|
Code parse_operator ( s32 length, char const* operator_def );
|
|
|
|
Code parse_struct ( s32 length, char const* struct_def );
|
|
|
|
Code parse_variable ( s32 length, char const* var_def );
|
|
|
|
Code parse_type ( s32 length, char const* type_def );
|
|
|
|
Code parse_typedef ( s32 length, char const* typedef_def );
|
|
|
|
Code parse_using ( s32 length, char const* using_def );
|
|
|
|
|
2023-04-07 09:31:50 -07:00
|
|
|
s32 parse_classes ( s32 length, char const* class_defs, Code* out_class_codes );
|
|
|
|
s32 parse_enums ( s32 length, char const* enum_defs, Code* out_enum_codes );
|
|
|
|
s32 parse_friends ( s32 length, char const* friend_defs, Code* out_friend_codes );
|
2023-04-06 16:19:11 -07:00
|
|
|
s32 parse_functions ( s32 length, char const* fn_defs, Code* out_fn_codes );
|
|
|
|
s32 parse_namespaces( s32 length, char const* namespace_defs, Code* out_namespaces_codes );
|
|
|
|
s32 parse_operators ( s32 length, char const* operator_defs, Code* out_operator_codes );
|
|
|
|
s32 parse_structs ( s32 length, char const* struct_defs, Code* out_struct_codes );
|
|
|
|
s32 parse_variables ( s32 length, char const* vars_def, Code* out_var_codes );
|
|
|
|
s32 parse_typedefs ( s32 length, char const* typedef_def, Code* out_typedef_codes );
|
|
|
|
s32 parse_usings ( s32 length, char const* usings_def, Code* out_using_codes );
|
2023-04-18 19:47:59 -07:00
|
|
|
# endif
|
2023-04-05 00:03:56 -07:00
|
|
|
# pragma endregion Parsing
|
|
|
|
|
|
|
|
# pragma region Untyped text
|
2023-04-05 23:21:23 -07:00
|
|
|
Code untyped_str ( s32 length, char const* str);
|
|
|
|
Code untyped_fmt ( char const* fmt, ... );
|
|
|
|
Code untyped_token_fmt( char const* fmt, s32 num_tokens, ... );
|
2023-04-05 00:03:56 -07:00
|
|
|
# pragma endregion Untyped text
|
2023-04-03 00:55:28 -07:00
|
|
|
|
2023-04-01 22:07:44 -07:00
|
|
|
struct Builder
|
2023-04-01 19:21:46 -07:00
|
|
|
{
|
2023-04-09 10:59:39 -07:00
|
|
|
FileInfo File;
|
|
|
|
String Buffer;
|
2023-04-01 19:21:46 -07:00
|
|
|
|
2023-04-01 22:07:44 -07:00
|
|
|
void print( Code );
|
2023-04-01 19:21:46 -07:00
|
|
|
|
2023-04-01 22:07:44 -07:00
|
|
|
bool open( char const* path );
|
2023-04-01 19:21:46 -07:00
|
|
|
void write();
|
|
|
|
};
|
2023-04-06 16:19:11 -07:00
|
|
|
|
|
|
|
#ifdef GEN_FEATURE_EDITOR
|
|
|
|
struct Policy
|
|
|
|
{
|
2023-04-07 21:29:09 -07:00
|
|
|
// Nothing for now.
|
|
|
|
};
|
2023-04-06 16:19:11 -07:00
|
|
|
|
2023-04-07 21:29:09 -07:00
|
|
|
enum class SymbolType : u32
|
|
|
|
{
|
|
|
|
Code,
|
|
|
|
Line,
|
|
|
|
Marker
|
2023-04-06 16:19:11 -07:00
|
|
|
};
|
|
|
|
|
|
|
|
struct SymbolInfo
|
|
|
|
{
|
2023-04-09 21:38:47 -07:00
|
|
|
StringCached File;
|
2023-04-07 21:29:09 -07:00
|
|
|
char const* Marker;
|
|
|
|
Code Signature;
|
2023-04-06 16:19:11 -07:00
|
|
|
};
|
|
|
|
|
|
|
|
struct Editor
|
|
|
|
{
|
|
|
|
enum RequestType : u32
|
|
|
|
{
|
|
|
|
Add,
|
|
|
|
Replace,
|
|
|
|
Remove
|
|
|
|
};
|
|
|
|
|
2023-04-07 09:31:50 -07:00
|
|
|
struct SymbolData
|
2023-04-06 16:19:11 -07:00
|
|
|
{
|
|
|
|
Policy Policy;
|
2023-04-07 09:31:50 -07:00
|
|
|
SymbolInfo Info;
|
2023-04-06 16:19:11 -07:00
|
|
|
};
|
|
|
|
|
2023-04-07 09:31:50 -07:00
|
|
|
struct RequestEntry
|
|
|
|
{
|
|
|
|
union {
|
|
|
|
SymbolData Symbol;
|
2023-04-09 10:59:39 -07:00
|
|
|
String Specification;
|
2023-04-07 09:31:50 -07:00
|
|
|
};
|
|
|
|
RequestType Type;
|
|
|
|
};
|
2023-04-06 16:19:11 -07:00
|
|
|
|
2023-04-07 09:31:50 -07:00
|
|
|
struct Receipt
|
|
|
|
{
|
2023-04-09 21:38:47 -07:00
|
|
|
StringCached File;
|
2023-04-07 21:29:09 -07:00
|
|
|
Code Found;
|
|
|
|
Code Written;
|
|
|
|
bool Result;
|
2023-04-07 09:31:50 -07:00
|
|
|
};
|
|
|
|
|
2023-04-09 10:59:39 -07:00
|
|
|
static AllocatorInfo Allocator;
|
2023-04-06 16:19:11 -07:00
|
|
|
|
2023-04-09 10:59:39 -07:00
|
|
|
static void set_allocator( AllocatorInfo allocator );
|
2023-04-06 16:19:11 -07:00
|
|
|
|
2023-04-09 10:59:39 -07:00
|
|
|
Array(FileInfo) Files;
|
|
|
|
String Buffer;
|
|
|
|
Array(RequestEntry) Requests;
|
2023-04-06 16:19:11 -07:00
|
|
|
|
2023-04-07 21:29:09 -07:00
|
|
|
void add_files( s32 num, char const** files );
|
|
|
|
|
2023-04-07 09:31:50 -07:00
|
|
|
void add ( SymbolInfo definition, Policy policy, Code to_inject );
|
2023-04-07 21:29:09 -07:00
|
|
|
void remove ( SymbolInfo definition, Policy policy );
|
2023-04-07 09:31:50 -07:00
|
|
|
void replace( SymbolInfo definition, Policy policy, Code to_replace);
|
2023-04-06 16:19:11 -07:00
|
|
|
|
2023-04-07 21:29:09 -07:00
|
|
|
# ifdef GEN_FEATURE_EDITOR_REFACTOR
|
|
|
|
void refactor( char const* file_path, char const* specification_path );
|
2023-04-06 16:19:11 -07:00
|
|
|
# endif
|
2023-04-07 09:31:50 -07:00
|
|
|
|
2023-04-09 10:59:39 -07:00
|
|
|
bool process_requests( Array(Receipt) out_receipts );
|
2023-04-07 09:31:50 -07:00
|
|
|
};
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifdef GEN_FEATURE_SCANNER
|
|
|
|
struct Scanner
|
|
|
|
{
|
|
|
|
struct RequestEntry
|
|
|
|
{
|
|
|
|
SymbolInfo Info;
|
|
|
|
};
|
|
|
|
|
|
|
|
struct Receipt
|
|
|
|
{
|
2023-04-09 21:38:47 -07:00
|
|
|
StringCached File;
|
2023-04-07 21:29:09 -07:00
|
|
|
Code Defintion;
|
|
|
|
bool Result;
|
2023-04-07 09:31:50 -07:00
|
|
|
};
|
|
|
|
|
2023-04-09 10:59:39 -07:00
|
|
|
AllocatorInfo MemAlloc;
|
2023-04-07 09:31:50 -07:00
|
|
|
|
2023-04-09 10:59:39 -07:00
|
|
|
static void set_allocator( AllocatorInfo allocator );
|
2023-04-07 09:31:50 -07:00
|
|
|
|
2023-04-09 10:59:39 -07:00
|
|
|
Array(FileInfo) Files;
|
|
|
|
String Buffer;
|
|
|
|
Array(RequestEntry) Requests;
|
2023-04-07 09:31:50 -07:00
|
|
|
|
2023-04-07 21:29:09 -07:00
|
|
|
void add_files( s32 num, char const** files );
|
|
|
|
|
2023-04-07 09:31:50 -07:00
|
|
|
void add( SymbolInfo signature, Policy policy );
|
|
|
|
|
2023-04-09 10:59:39 -07:00
|
|
|
bool process_requests( Array(Receipt) out_receipts );
|
2023-04-06 16:19:11 -07:00
|
|
|
};
|
|
|
|
#endif
|
2023-04-05 00:03:56 -07:00
|
|
|
#pragma endregion Gen Interface
|
2023-04-01 19:21:46 -07:00
|
|
|
}
|
2023-04-01 22:07:44 -07:00
|
|
|
|
2023-04-06 16:19:11 -07:00
|
|
|
#pragma region Macros
|
2023-04-03 00:55:28 -07:00
|
|
|
# define gen_main main
|
2023-04-05 23:21:23 -07:00
|
|
|
|
|
|
|
# define __ NoCode
|
|
|
|
# define spec_alignas( Value_ ) ESpecifier::Alignas, Value
|
2023-04-03 00:55:28 -07:00
|
|
|
|
2023-04-06 16:19:11 -07:00
|
|
|
// This represents the naming convention for all typename Codes generated.
|
2023-04-07 21:29:09 -07:00
|
|
|
// Used by the DSL but can also be used without it.
|
|
|
|
# define type_ns( Name_ ) t_##Name_
|
|
|
|
|
2023-04-09 21:38:47 -07:00
|
|
|
// Convienence for defining any name used with the gen interface.
|
2023-04-07 21:29:09 -07:00
|
|
|
// Lets you provide the length and string literal to the functions without the need for the DSL.
|
|
|
|
# define name( Id_ ) txt_n_len( Id_ )
|
|
|
|
|
|
|
|
// Same as name just used to indicate intention of literal for code instead of names.
|
|
|
|
# define code( Code_ ) txt_n_len( Code_ )
|
2023-04-06 16:19:11 -07:00
|
|
|
|
2023-04-03 00:55:28 -07:00
|
|
|
/*
|
|
|
|
gen's Domain Specific Langauge.
|
|
|
|
|
2023-04-03 23:04:19 -07:00
|
|
|
Completely optional, makes the code gen syntax less verbose and cumbersome...
|
|
|
|
Since its C macros ends up looking like a lisp dialect...
|
2023-04-03 00:55:28 -07:00
|
|
|
|
2023-04-03 23:04:19 -07:00
|
|
|
Longforms auto-define the variable.
|
|
|
|
Shorthands are just the function call.
|
2023-04-03 00:55:28 -07:00
|
|
|
|
2023-04-06 16:19:11 -07:00
|
|
|
Anything below the make() macro is intended to be syntactically used in the follwing format:
|
2023-04-03 23:04:19 -07:00
|
|
|
make( <type>, <name> )
|
|
|
|
{
|
|
|
|
...
|
|
|
|
}
|
2023-04-03 00:55:28 -07:00
|
|
|
|
2023-04-03 23:04:19 -07:00
|
|
|
Where ... are whatever is deemed necessary to produce the definition for the def( <name> ).
|
2023-04-03 00:55:28 -07:00
|
|
|
|
2023-04-03 23:04:19 -07:00
|
|
|
The code macros are used to embed c/c++ to insert into the desired lcoation.
|
2023-04-03 00:55:28 -07:00
|
|
|
*/
|
2023-04-03 23:04:19 -07:00
|
|
|
#ifdef GEN_DEFINE_DSL
|
2023-04-05 23:21:23 -07:00
|
|
|
|
|
|
|
// Boilerplate
|
|
|
|
|
|
|
|
/*
|
|
|
|
In order to provide some convient syntax sugar this polymoprhic macro boilerplate is needed for:
|
|
|
|
* function( ... )
|
|
|
|
* operator( ... )
|
|
|
|
* params( ... )
|
|
|
|
|
|
|
|
macrofn based off of: https://stackoverflow.com/questions/3046889/optional-parameters-with-c-macros
|
|
|
|
specifically: https://stackoverflow.com/a/56038661
|
|
|
|
*/
|
|
|
|
|
2023-04-06 16:19:11 -07:00
|
|
|
# define macrofn_chooser(_f0, _f1, _f2, _f3, _f4, _f5, _f6, _f7, _f8, _f9, _f10, _f11, _f12, _f13, _f14, _f15, _f16, ...) _f16
|
|
|
|
# define macrofn_recomposer(ArgsWithParentheses_) macrofn_chooser ArgsWithParentheses_
|
|
|
|
# define macrofn_chose_from_arg_num(F, ...) macrofn_recomposer((__VA_ARGS__, F##_16, F##_15, F##_14, F##_13, F##_12, F##_11, F##_10, F##_9, F##_8, F##_7, F##_6, F##_5, F##_4, F##_3, F##_2, F##_1, ))
|
2023-04-09 10:59:39 -07:00
|
|
|
# define marcofn_no_arg_expander(Func_) ,,,,,,,,,,,,,,,,Func_ ## _0
|
|
|
|
# define macrofn_finder(Func_, ...) macrofn_chose_from_arg_num(Func_, marcofn_no_arg_expander __VA_ARGS__ (Func_))
|
2023-04-06 16:19:11 -07:00
|
|
|
# define macrofn_polymorphic(Func_, ...) macrofn_finder(Func_, __VA_ARGS__)(__VA_ARGS__)
|
|
|
|
|
|
|
|
# define function_5( Name_, Params_, RetType_, Specifiers_, Body_ ) gen::def_function( txt_n_len( Name_ ), macro_expand( Params_ ), type_ns(RetType_), Specifiers_, Body_ )
|
|
|
|
# define function_4( Name_, Params_, RetType_, Specifiers_ ) gen::def_function( txt_n_len( Name_ ), macro_expand( Params_ ), type_ns(RetType_), Specifiers_ )
|
|
|
|
# define function_3( Name_, Params_, RetType_ ) gen::def_function( txt_n_len( Name_ ), macro_expand( Params_ ), type_ns(RetType_) )
|
|
|
|
# define function_2( Name_, Params_ ) gen::def_function( txt_n_len( Name_ ), macro_expand( Params_ ) )
|
|
|
|
# define function_1( Name_ ) gen::def_function( txt_n_len( Name_ ) )
|
|
|
|
|
|
|
|
# define params_12( T_1, V_1, T_2, V_2, T_3, V_3, T_4, V_4, T_5, V_5, T_6, V_6 ) gen::def_params( 6, type_ns(T_1), txt_n_len( V_1), type_ns(T_2), txt_n_len( V_2), type_ns(T_3), txt_n_len( V_3), type_ns(T_4), txt_n_len( V_4), type_ns(T_5), txt_n_len( V_5), type_ns(T_6), txt_n_len(V_6))
|
|
|
|
# define params_10( T_1, V_1, T_2, V_2, T_3, V_3, T_4, V_4, T_5, V_5 ) gen::def_params( 5, type_ns(T_1), txt_n_len( V_1), type_ns(T_2), txt_n_len( V_2), type_ns(T_3), txt_n_len( V_3), type_ns(T_4), txt_n_len( V_4), type_ns(T_5), txt_n_len( V_5))
|
|
|
|
# define params_8( T_1, V_1, T_2, V_2, T_3, V_3, T_4, V_4 ) gen::def_params( 4, type_ns(T_1), txt_n_len( V_1), type_ns(T_2), txt_n_len( V_2), type_ns(T_3), txt_n_len( V_3), type_ns(T_4), txt_n_len( V_4) )
|
|
|
|
# define params_6( T_1, V_1, T_2, V_2, T_3, V_3 ) gen::def_params( 3, type_ns(T_1), txt_n_len( V_1), type_ns(T_2), txt_n_len( V_2), type_ns(T_3), txt_n_len( V_3))
|
|
|
|
# define params_4( T_1, V_1, T_2, V_2 ) gen::def_params( 2, type_ns(T_1), txt_n_len( V_1), type_ns(T_2), txt_n_len( V_2))
|
|
|
|
# define params_2( T_1, V_1 ) gen::def_param ( type_ns(T_1), txt_n_len( V_1))
|
|
|
|
# define params_bad static_assert("params(...): Invalid number of parameters provided.")
|
|
|
|
# define params_11 params_bad
|
|
|
|
# define params_9 params_bad
|
|
|
|
# define params_7 params_bad
|
|
|
|
# define params_5 params_bad
|
|
|
|
# define params_3 params_bad
|
|
|
|
# define params_1 params_bad
|
2023-04-05 23:21:23 -07:00
|
|
|
|
|
|
|
// Upfront
|
|
|
|
|
2023-04-06 16:19:11 -07:00
|
|
|
# define class( Name_, ... ) gen::def_class( txt_n_len(Name_), __VA_ARGS__ )
|
|
|
|
# define enum( Name_, Type_, Body_ ) gen::def_enum ( txt_n_len(Name_), type_ns(Type_), Body_ )
|
|
|
|
|
|
|
|
# define function( ... ) macrofn_polymorphic( function, __VA_ARGS__ )
|
|
|
|
# define namespace( Name_, Body_ ) gen::def_namespace ( txt_n_len(Name_), Body_ )
|
|
|
|
# define operator( Op_, ... ) macrofn_polymorphic( operator, __VA_ARGS__ )
|
|
|
|
# define params( ... ) macrofn_polymorphic( params, __VA_ARGS__ )
|
|
|
|
# define specifiers( ... ) gen::def_specifiers ( macro_num_args( __VA_ARGS__ ), __VA_ARGS__ )
|
|
|
|
# define struct( Name_, ... ) gen::def_struct ( txt_n_len(Name_), __VA_ARGS__ )
|
|
|
|
# define variable( Type_, Name_, ... ) gen::def_variable ( type_ns(Type_), txt_n_len(Name_), __VA_ARGS__ )
|
|
|
|
# define type( Value_, ... ) gen::def_type ( txt_n_len(Value_), __VA_ARGS__ )
|
|
|
|
# define type_fmt( Fmt_, ... ) gen::def_type ( bprintf( Fmt_, __VA_ARGS__ ) )
|
|
|
|
# define using( Name_, Type_ ) gen::def_using ( txt_n_len(Name_), type_ns(Type_) )
|
|
|
|
# define using_namespace( Name_ ) gen::def_using_namespace( txt_n_len(Name_) )
|
|
|
|
|
|
|
|
# define class_body( ... ) gen::def_class_body ( macro_num_args( __VA_ARGS__ ), __VA_ARGS__ )
|
|
|
|
# define enum_body( ... ) gen::def_enum_body ( macro_num_args( __VA_ARGS__ ), __VA_ARGS__ )
|
|
|
|
# define global_body( ... ) gen::def_global_body ( macro_num_args( __VA_ARGS__ ), __VA_ARGS__ )
|
|
|
|
# define function_body( ... ) gen::def_function_body ( macro_num_args( __VA_ARGS__ ), __VA_ARGS__ )
|
|
|
|
# define namespace_body( ... ) gen::def_namespace_body( macro_num_args( __VA_ARGS__ ), __VA_ARGS__ )
|
|
|
|
# define operator_body( ... ) gen::def_operator_body ( macro_num_args( __VA_ARGS__ ), __VA_ARGS__ )
|
|
|
|
# define struct_body( ... ) gen::def_struct_body ( macro_num_args( __VA_ARGS__ ), __VA_ARGS__ )
|
2023-04-05 23:21:23 -07:00
|
|
|
|
2023-04-18 19:47:59 -07:00
|
|
|
# ifdef GEN_FEATURE_INCREMENTAL
|
2023-04-05 23:21:23 -07:00
|
|
|
// Incremental
|
|
|
|
|
2023-04-06 16:19:11 -07:00
|
|
|
# define make( ConstructType_, Name_, ... ) Code Name_ = make_##ConstructType_( txt_n_len(Name_), __VA_ARGS__ );
|
2023-04-18 19:47:59 -07:00
|
|
|
# endif
|
2023-04-05 23:21:23 -07:00
|
|
|
|
2023-04-18 19:47:59 -07:00
|
|
|
# ifdef GEN_FEATURE_PARSING
|
2023-04-05 23:21:23 -07:00
|
|
|
// Parsing
|
|
|
|
|
|
|
|
# define class_code( ... ) gen::parse_class ( txt_n_len( __VA_ARGS__ ))
|
|
|
|
# define enum_code( ... ) gen::parse_enum ( txt_n_len( __VA_ARGS__ ))
|
|
|
|
# define function_code( ... ) gen::parse_function ( txt_n_len( __VA_ARGS__ ))
|
|
|
|
# define global_body_code( ... ) gen::parse_global_body( txt_n_len( __VA_ARGS__ ))
|
|
|
|
# define operator_code( ... ) gen::parse_operator ( txt_n_len( __VA_ARGS__ ))
|
|
|
|
# define namespace_code( ... ) gen::parse_namespace ( txt_n_len( __VA_ARGS__ ))
|
|
|
|
# define struct_code( ... ) gen::parse_struct ( txt_n_len( __VA_ARGS__ ))
|
|
|
|
# define variable_code( ... ) gen::parse_variable ( txt_n_len( __VA_ARGS__ ))
|
|
|
|
# define type_code( ... ) gen::parse_type ( txt_n_len( __VA_ARGS__ ))
|
|
|
|
# define typedef_code( ... ) gen::parse_typedef ( txt_n_len( __VA_ARGS__ ))
|
|
|
|
# define using_code( ... ) gen::parse_code ( txt_n_len( __VA_ARGS__ ))
|
2023-04-18 19:47:59 -07:00
|
|
|
# endif
|
2023-04-05 23:21:23 -07:00
|
|
|
|
|
|
|
// Untyped
|
|
|
|
|
|
|
|
# define code_str( ... ) gen::untyped_str ( txt_n_len(__VA_ARGS__) )
|
|
|
|
# define code_fmt( Fmt_, ... ) gen::untyped_fmt ( Fmt_, __VA_ARGS__ )
|
2023-04-06 16:19:11 -07:00
|
|
|
# define code_token( Fmt_, ... ) gen::untyped_token_fmt( Fmt_, macro_num_args( __VA_ARGS__) / 2, __VA_ARGS__ )
|
2023-04-03 00:55:28 -07:00
|
|
|
#endif
|
2023-04-06 16:19:11 -07:00
|
|
|
#pragma endregion Macros
|
2023-04-03 00:55:28 -07:00
|
|
|
|
2023-04-06 16:19:11 -07:00
|
|
|
#pragma region Constants
|
2023-04-05 23:21:23 -07:00
|
|
|
#ifdef GEN_DEFINE_LIBRARY_CODE_CONSTANTS
|
2023-04-03 00:55:28 -07:00
|
|
|
namespace gen
|
|
|
|
{
|
2023-04-09 21:38:47 -07:00
|
|
|
// Predefined typename codes. Are set to readonly and are setup during gen::init()
|
2023-04-03 23:04:19 -07:00
|
|
|
|
2023-04-06 16:19:11 -07:00
|
|
|
extern Code type_ns( s8 );
|
|
|
|
extern Code type_ns( s16 );
|
|
|
|
extern Code type_ns( s32 );
|
|
|
|
extern Code type_ns( s64 );
|
2023-04-03 23:04:19 -07:00
|
|
|
|
2023-04-06 16:19:11 -07:00
|
|
|
extern Code type_ns( u8 );
|
|
|
|
extern Code type_ns( u16 );
|
|
|
|
extern Code type_ns( u32 );
|
|
|
|
extern Code type_ns( u64 );
|
2023-04-03 23:04:19 -07:00
|
|
|
|
2023-04-06 16:19:11 -07:00
|
|
|
extern Code type_ns( sw );
|
|
|
|
extern Code type_ns( uw );
|
2023-04-03 00:55:28 -07:00
|
|
|
|
2023-04-06 16:19:11 -07:00
|
|
|
extern Code type_ns( f32 );
|
|
|
|
extern Code type_ns( f64 );
|
2023-04-03 00:55:28 -07:00
|
|
|
}
|
2023-04-03 23:04:19 -07:00
|
|
|
#endif
|
2023-04-05 00:03:56 -07:00
|
|
|
|
|
|
|
namespace gen
|
|
|
|
{
|
2023-04-09 21:38:47 -07:00
|
|
|
// These constexprs are used for allocation heavior of data structurs
|
|
|
|
// or string handling while constructing or serializing.
|
|
|
|
// Change them to suit your needs.
|
|
|
|
|
|
|
|
ct s32 InitSize_DataArrays = 16;
|
|
|
|
ct s32 InitSize_StringTable = megabytes(4);
|
|
|
|
ct s32 InitSize_TypeTable = megabytes(4);
|
|
|
|
|
|
|
|
ct s32 CodePool_NumBlocks = 4096;
|
2023-04-10 18:33:06 -07:00
|
|
|
ct s32 InitSize_CodeEntiresArray = 512;
|
2023-04-09 21:38:47 -07:00
|
|
|
ct s32 SizePer_CodeEntriresArena = megabytes(16);
|
|
|
|
ct s32 SizePer_StringArena = megabytes(32);
|
|
|
|
|
2023-04-05 23:21:23 -07:00
|
|
|
ct s32 MaxNameLength = 128;
|
2023-04-06 16:19:11 -07:00
|
|
|
ct s32 MaxUntypedStrLength = kilobytes(640);
|
2023-04-05 23:21:23 -07:00
|
|
|
ct s32 StringTable_MaxHashLength = kilobytes(1);
|
|
|
|
|
2023-04-09 21:38:47 -07:00
|
|
|
// Predefined Codes. Are set to readonly and are setup during gen::init()
|
|
|
|
|
2023-04-10 18:33:06 -07:00
|
|
|
extern Code type_ns( void );
|
|
|
|
extern Code type_ns( int );
|
|
|
|
extern Code type_ns( bool );
|
|
|
|
extern Code type_ns( char );
|
|
|
|
extern Code type_ns( wchar_t );
|
|
|
|
|
2023-04-05 00:03:56 -07:00
|
|
|
extern Code access_public;
|
|
|
|
extern Code access_protected;
|
|
|
|
extern Code access_private;
|
2023-04-06 16:19:11 -07:00
|
|
|
|
|
|
|
extern Code spec_constexpr;
|
|
|
|
extern Code spec_const;
|
|
|
|
extern Code spec_inline;
|
|
|
|
extern Code spec_ptr;
|
|
|
|
extern Code spec_ref;
|
2023-04-05 00:03:56 -07:00
|
|
|
}
|
2023-04-06 16:19:11 -07:00
|
|
|
#pragma endregion Constants
|
|
|
|
|
2023-04-10 18:33:06 -07:00
|
|
|
#pragma region Inlines
|
|
|
|
namespace gen
|
|
|
|
{
|
2023-04-11 19:18:02 -07:00
|
|
|
inline
|
|
|
|
void AST::add_entry( AST* other )
|
2023-04-10 18:33:06 -07:00
|
|
|
{
|
|
|
|
AST* to_add = other->Parent ?
|
|
|
|
other->duplicate() : other;
|
|
|
|
|
|
|
|
if (DynamicEntries)
|
|
|
|
array_append( Entries, to_add );
|
|
|
|
|
|
|
|
else
|
|
|
|
{
|
|
|
|
if ( StaticIndex < ArrS_Cap )
|
|
|
|
{
|
|
|
|
ArrStatic[StaticIndex] = to_add;
|
|
|
|
StaticIndex++;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
Entries = make_code_entries();
|
|
|
|
|
|
|
|
s32 index = 0;
|
|
|
|
do
|
|
|
|
{
|
|
|
|
array_append( Entries, ArrStatic[index] );
|
|
|
|
}
|
|
|
|
while ( StaticIndex--, StaticIndex );
|
|
|
|
|
|
|
|
array_append( Entries, to_add );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
to_add->Parent = this;
|
|
|
|
}
|
2023-04-11 19:18:02 -07:00
|
|
|
|
|
|
|
inline
|
|
|
|
bool AST::add_param( AST* type, s32 length, char const* name )
|
|
|
|
{
|
|
|
|
if ( length <= 0 )
|
|
|
|
{
|
|
|
|
log_failure( "gen::AST::add_param: Invalid name length provided - %d", length );
|
|
|
|
return Code::Invalid;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ( name == nullptr )
|
|
|
|
{
|
|
|
|
log_failure( "gen::AST::add_param: name is null");
|
|
|
|
return Code::Invalid;
|
|
|
|
}
|
|
|
|
|
|
|
|
s32
|
|
|
|
score = 0;
|
|
|
|
score += Name == nullptr;
|
|
|
|
score += Entries[0] == nullptr;
|
|
|
|
|
|
|
|
if ( score == 1 )
|
|
|
|
{
|
|
|
|
log_failure("gen::AST::add_param: this AST has bad data - %s", debug_str() );
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
else if ( score == 2)
|
|
|
|
{
|
|
|
|
Name = name;
|
|
|
|
Entries[0] = type;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
Code
|
|
|
|
result = make_code();
|
|
|
|
result->Type = ECode::Parameters;
|
|
|
|
|
|
|
|
result->add_entry( result );
|
|
|
|
return true;
|
|
|
|
}
|
2023-04-10 18:33:06 -07:00
|
|
|
}
|
|
|
|
#pragma endregion Inlines
|
|
|
|
|
2023-04-11 19:18:02 -07:00
|
|
|
// end: gen_time
|
|
|
|
#endif
|