13 Commits

Author SHA1 Message Date
Ed_
c7647ab00f Updated docs 2023-08-08 11:56:42 -04:00
Ed_
d2fc1d0a56 Converted log_failure and fatal to macros (fixes GEN_PANIC not determining correct line or file) 2023-08-08 09:48:50 -04:00
Ed_
ed3246c6b0 Fixes for typedef serialization of functions..
Also fix for HashTable<>::rehash_fast not having finished implemenation...

The typedef fix is a sort of hack (like how parsing the rest of the language feels like tbh...).
I might make a def_typedef_fn to make it clearer how to define function typedefs using the upfront interface.
2023-08-07 20:16:04 -04:00
Ed_
c4d5637a64 Fixes to single header generation (bad parsing adt/csv injection in wrong place) 2023-08-07 14:52:26 -04:00
Ed_
c2f8c8aeb1 Added constructor and destructor supported (UNTESTED)
Just compiles and generates...

Also fixed a bug where parsing didn't have a token for virtual specifiers...
2023-08-07 03:10:45 -04:00
Ed_
c2319b9651 Fixes for test.singleheader_ast.cpp, also added a bench for it.
On a Ryzen R9 5950 it takes 11 ms to generate AST and 21 ms to serialize to file.
2023-08-06 17:46:17 -04:00
Ed_
a4f9596d3b Fixes to serialization, reduced Define_CodeType macro
Now the execution code is generated in bootstrap/singleheader gen.
2023-08-06 17:19:57 -04:00
Ed_
97750388ad No longer using components/temp/ast_inlines (switched to helper function to avoid macro usage)
Increased the arg count support of num_args to 100.
2023-08-06 14:58:43 -04:00
Ed_
00f6c45f15 Fixes for serializations found with last commit's test.
Should be fine to move on to next major feature....
2023-08-06 13:28:19 -04:00
Ed_
34f286d218 Library can now construct into AST and serialization itself (singleheader).
Still need to validate if they match.
2023-08-04 16:12:13 -04:00
Ed_
d36c3fa847 Single header generates again, some more cleanup.
Looking into properly dealing with empty lines...

I want to preserve the text's empty lines in the AST for serialization purposes (perserve formatting for gapes between definitions).
Don't want to introduce the possibility of it breaking though, so will have to ignore empty_lines in a general way (if they are in a bad spot).
Attempted to cover that by having TokArray::current() auto-skip empty lines and eat as well if the type doesn't match.
2023-08-03 23:18:33 -04:00
Ed_
5d7dfaf666 Heavy refactor..
Isolating large macros to their own directory (components/temp).
- Plan is to remove them soon with proper generation.

Added additional component files, separating the old data_structures header for a set of ast headers.
Header_end also had its inlines extracted out.
Necessary to complete the macro isolation.

ZPL parser dependencies were removed from the core library along with builder, its now generated in bootstrap as pare of making a gen_builder set of files.

Singleheader will be changed in next few commits to reflect this as well (By making builder deps and components a conditional option).

Tests are most likely all broken for now.
2023-08-03 11:01:43 -04:00
Ed_
114f678f1b Merge pull request #9 from Ed94/Preprocessor_support
Preprocessor support
2023-08-02 16:04:57 -04:00
83 changed files with 3536 additions and 2022 deletions

8
.gitignore vendored
View File

@ -6,8 +6,12 @@ build/*
**/*.gen.* **/*.gen.*
**/gen/gen.hpp **/gen/gen.hpp
**/gen/gen.cpp **/gen/gen.cpp
**/gen/gen_dep.hpp **/gen/gen.dep.hpp
**/gen/gen_dep.cpp **/gen/gen.dep.cpp
**/gen/gen.builder.hpp
**/gen/gen.builder.cpp
**/gen/gen.scanner.hpp
**/gen/gen.scanner.cpp
gencpp.hpp gencpp.hpp
gencpp.cpp gencpp.cpp

View File

@ -14,6 +14,7 @@
"windowsSdkVersion": "10.0.19041.0", "windowsSdkVersion": "10.0.19041.0",
"compilerPath": "C:/Users/Ed/scoop/apps/llvm/current/bin/clang++.exe", "compilerPath": "C:/Users/Ed/scoop/apps/llvm/current/bin/clang++.exe",
"intelliSenseMode": "windows-clang-x64", "intelliSenseMode": "windows-clang-x64",
"compileCommands": "${workspaceFolder}/project/build/compile_commands.json"
} }
], ],
"version": 4 "version": 4

View File

@ -6,12 +6,11 @@ The library API is a composition of code element constructors.
These build up a code AST to then serialize with a file builder. These build up a code AST to then serialize with a file builder.
This code base attempts follow the [handmade philosophy](https://handmade.network/manifesto), This code base attempts follow the [handmade philosophy](https://handmade.network/manifesto),
its not meant to be a black box metaprogramming utility, its meant for the user to extend for their project domain. its not meant to be a black box metaprogramming utility, it should be easy to intergrate into a user's their project domain.
## Notes ## Notes
The project has reached an *alpha* state, all the current functionality works for the test cases but it will most likely break in many other cases. The project has reached an *alpha* state, all the current functionality works for the test cases but it will most likely break in many other cases.
The [issues](https://github.com/Ed94/gencpp/issues) marked with v1.0 Feature indicate whats left before the library is considered feature complete.
A `natvis` and `natstepfilter` are provided in the scripts directory. A `natvis` and `natstepfilter` are provided in the scripts directory.
@ -25,7 +24,7 @@ A metaprogram is built to generate files before the main program is built. We'll
`gen.cpp` \`s `main()` is defined as `gen_main()` which the user will have to define once for their program. There they will dictate everything that should be generated. `gen.cpp` \`s `main()` is defined as `gen_main()` which the user will have to define once for their program. There they will dictate everything that should be generated.
In order to keep the locality of this code within the same files the following pattern may be used: In order to keep the locality of this code within the same files the following pattern may be used (although this pattern isn't required at all):
Within `program.cpp` : Within `program.cpp` :
@ -41,6 +40,8 @@ u32 gen_main()
} }
#endif #endif
// "Stage" agnostic code.
#ifndef GEN_TIME #ifndef GEN_TIME
#include "program.gen.cpp" #include "program.gen.cpp"
@ -56,6 +57,7 @@ Example using each construction interface:
### Upfront ### Upfront
Validation and construction through a functional interface.
```cpp ```cpp
Code t_uw = def_type( name(uw) ); Code t_uw = def_type( name(uw) );
@ -75,6 +77,8 @@ Code header;
### Parse ### Parse
Validation through ast construction.
```cpp ```cpp
Code header = parse_struct( code( Code header = parse_struct( code(
struct ArrayHeader struct ArrayHeader
@ -89,6 +93,8 @@ Code header = parse_struct( code(
### Untyped ### Untyped
No validation, just glorified text injection.
```cpp ```cpp
Code header = code_str( Code header = code_str(
struct ArrayHeader struct ArrayHeader

View File

@ -1,34 +1,36 @@
# Parsing # Parsing
The library features a naive parser tailored for only what the library needs to construct the supported syntax of C++ into its AST. The library features a naive parser tailored for only what the library needs to construct the supported syntax of C++ into its AST.
This parser does not, and should not do the compiler's job. By only supporting this minimal set of features, the parser is kept under 5000 loc. This parser does not, and should not do the compiler's job. By only supporting this minimal set of features, the parser is kept (so far) under 5000 loc.
The parsing implementation supports the following for the user: The parsing implementation supports the following for the user:
```cpp ```cpp
CodeClass parse_class ( StrC class_def ); CodeClass parse_class ( StrC class_def );
CodeEnum parse_enum ( StrC enum_def ); CodeConstructor parse_constructor ( StrC constructor_def );
CodeBody parse_export_body ( StrC export_def ); CodeDestructor parse_destructor ( StrC destructor_def );
CodeExtern parse_extern_link ( StrC exten_link_def); CodeEnum parse_enum ( StrC enum_def );
CodeFriend parse_friend ( StrC friend_def ); CodeBody parse_export_body ( StrC export_def );
CodeFn parse_function ( StrC fn_def ); CodeExtern parse_extern_link ( StrC exten_link_def );
CodeBody parse_global_body ( StrC body_def ); CodeFriend parse_friend ( StrC friend_def );
CodeNamespace parse_namespace ( StrC namespace_def ); CodeFn parse_function ( StrC fn_def );
CodeOperator parse_operator ( StrC operator_def ); CodeBody parse_global_body ( StrC body_def );
CodeOpCast parse_operator_cast( StrC operator_def ); CodeNS parse_namespace ( StrC namespace_def );
CodeStruct parse_struct ( StrC struct_def ); CodeOperator parse_operator ( StrC operator_def );
CodeTemplate parse_template ( StrC template_def ); CodeOpCast parse_operator_cast( StrC operator_def );
CodeType parse_type ( StrC type_def ); CodeStruct parse_struct ( StrC struct_def );
CodeTypedef parse_typedef ( StrC typedef_def ); CodeTemplate parse_template ( StrC template_def );
CodeUnion parse_union ( StrC union_def ); CodeType parse_type ( StrC type_def );
CodeUsing parse_using ( StrC using_def ); CodeTypedef parse_typedef ( StrC typedef_def );
CodeVar parse_variable ( StrC var_def ); CodeUnion parse_union ( StrC union_def );
CodeUsing parse_using ( StrC using_def );
CodeVar parse_variable ( StrC var_def );
``` ```
***Parsing will aggregate any tokens within a function body or expression statement to an untyped Code AST.*** ***Parsing will aggregate any tokens within a function body or expression statement to an untyped Code AST.***
Everything is done in one pass for both the preprocessor directives and the rest of the language. Everything is done in one pass for both the preprocessor directives and the rest of the language.
The parser performs no macro expansion as the scope of gencpp feature-set is to only support the preprocessor for the goal of having rudimentary awareness of preprocessor ***conditionals***, ***defines***, and ***includes***, and ***`pragmas`**. The parser performs no macro expansion as the scope of gencpp feature-set is to only support the preprocessor for the goal of having rudimentary awareness of preprocessor ***conditionals***, ***defines***, and ***includes***, and ***pragmas**.
The keywords supported for the preprocessor are: The keywords supported for the preprocessor are:
@ -41,12 +43,30 @@ The keywords supported for the preprocessor are:
* undef * undef
* pragma * pragma
Each directive `#` line is considered one preproecessor unit, and will be treated as one Preprocessor AST. *These ASTs will be considered members or entries of braced scope they reside within*. Each directive `#` line is considered one preproecessor unit, and will be treated as one Preprocessor AST. *These ASTs will be considered members or entries of braced scope they reside within*.
All keywords except *include* are suppported as members of a scope for a class/struct, global, or namespace body. If a directive is used with an unsupported keyword its will be processed as an untyped AST.
Any preprocessor definition abuse that changes the syntax of the core language is unsupported and will fail to parse if not kept within an execution scope (function body, or expression assignment). The preprocessor lines are stored as members of their associated scope they are parsed within. ( Global, Namespace, Class/Struct )
Exceptions to the above rule (If its too hard to keep track of just follow the above notion): Any preprocessor definition abuse that changes the syntax of the core language is unsupported and will fail to parse if not kept within an execution scope (function body, or expression assignment).
Exceptions:
* Typedefs allow of a macro exansion to be defined after the keyword; Ex: `typedef GEN_FILE_OPEN_PROC( file_open_proc );` * function signatures are allowed for a preprocessed macro: `neverinline MACRO() { ... }`
* typedefs allow for a preprocessed macro: `typedef MACRO();`
*(See functions `parse_operator_function_or_variable` and `parse_typedef` )*
The lexing and parsing takes shortcuts from whats expected in the standard.
* Numeric literals are not checked for validity.
* The parse API treats any execution scope definitions with no validation and are turned into untyped Code ASTs.
* *This includes the assignment of variables.*
* Attributes ( `[[]]` (standard), `__declspec` (Microsoft), or `__attribute__` (GNU) )
* Assumed to *come before specifiers* (`const`, `constexpr`, `extern`, `static`, etc) for a function
* Or in the usual spot for class, structs, (*right after the declaration keyword*)
* typedefs have attributes with the type (`parse_type`)
* As a general rule; if its not available from the upfront constructors, its not available in the parsing constructors.
* *Upfront constructors are not necessarily used in the parsing constructors, this is just a good metric to know what can be parsed.*
* Parsing attributes can be extended to support user defined macros by defining `GEN_DEFINE_ATTRIBUTE_TOKENS` (see `gen.hpp` for the formatting)
Empty lines used throughout the file are preserved for formatting purposes for ast serialization.

View File

@ -40,12 +40,6 @@ Otherwise the library is free of any templates.
### *WHAT IS NOT PROVIDED* ### *WHAT IS NOT PROVIDED*
* Execution statement validation : Execution expressions are defined using the untyped AST.
* Lambdas (This naturally means its unsupported)
* Non-trivial template validation support.
* RAII : This needs support for constructors/destructor parsing
* Haven't gotten around to yet (its in the github issues)
Keywords kept from "Modern C++": Keywords kept from "Modern C++":
* constexpr : Great to store compile-time constants. * constexpr : Great to store compile-time constants.
@ -55,13 +49,9 @@ Keywords kept from "Modern C++":
* import : ^^ * import : ^^
* module : ^^ * module : ^^
When it comes to expressions:
**There is no support for validating expressions.** **There is no support for validating expressions.**
Its difficult to parse without enough benefits (At the metaprogramming level). Its difficult to parse without enough benefits (At the metaprogramming level).
When it comes to templates:
**Only trivial template support is provided.** **Only trivial template support is provided.**
The intention is for only simple, non-recursive substitution. The intention is for only simple, non-recursive substitution.
The parameters of the template are treated like regular parameter AST entries. The parameters of the template are treated like regular parameter AST entries.
@ -78,7 +68,7 @@ Use at your own mental peril.
### The Data & Interface ### The Data & Interface
As mentioned in [Usage](#usage), the user is provided Code objects by calling the constructor's functions to generate them or find existing matches. As mentioned in root readme, the user is provided Code objects by calling the constructor's functions to generate them or find existing matches.
The AST is managed by the library and provided the user via its interface. The AST is managed by the library and provided the user via its interface.
However, the user may specifiy memory configuration. However, the user may specifiy memory configuration.
@ -89,39 +79,44 @@ Data layout of AST struct:
union { union {
struct struct
{ {
AST* Attributes; // Class, Enum, Function, Struct, Typedef, Union, Using, Variable AST* Attributes; // Class, Enum, Function, Struct, Typedef, Union, Using, Variable
AST* Specs; // Function, Operator, Type symbol, Variable AST* Specs; // Function, Operator, Type symbol, Variable
union { union {
AST* ParentType; // Class, Struct AST* InitializerList; // Constructor, Destructor
AST* ReturnType; // Function, Operator AST* ParentType; // Class, Struct
AST* UnderlyingType; // Enum, Typedef AST* ReturnType; // Function, Operator
AST* ValueType; // Parameter, Variable AST* UnderlyingType; // Enum, Typedef
AST* ValueType; // Parameter, Variable
}; };
AST* Params; // Function, Operator, Template
union { union {
AST* ArrExpr; // Type Symbol AST* BitfieldSize; // Varaiable (Class/Struct Data Member)
AST* Body; // Class, Enum, Function, Namespace, Struct, Union AST* Params; // Function, Operator, Template
AST* Declaration; // Friend, Template };
AST* Value; // Parameter, Variable union {
AST* ArrExpr; // Type Symbol
AST* Body; // Class, Constructr, Destructor, Enum, Function, Namespace, Struct, Union
AST* Declaration; // Friend, Template
AST* Value; // Parameter, Variable
}; };
}; };
StringCached Content; // Attributes, Comment, Execution, Include StringCached Content; // Attributes, Comment, Execution, Include
SpecifierT ArrSpecs[AST::ArrSpecs_Cap]; // Specifiers SpecifierT ArrSpecs[AST::ArrSpecs_Cap]; // Specifiers
}; };
union { union {
AST* Prev; AST* Prev;
AST* Front; // Used by CodeBody AST* Front;
AST* Last; // Used by CodeParam AST* Last;
}; };
union { union {
AST* Next; AST* Next;
AST* Back; // Used by CodeBody AST* Back;
}; };
AST* Parent; AST* Parent;
StringCached Name; StringCached Name;
CodeT Type; CodeT Type;
ModuleFlag ModuleFlags; ModuleFlag ModuleFlags;
union { union {
b32 IsFunction; // Used by typedef to not serialize the name field.
OperatorT Op; OperatorT Op;
AccessSpec ParentAccess; AccessSpec ParentAccess;
s32 NumEntries; s32 NumEntries;
@ -145,7 +140,7 @@ uw ArrSpecs_Cap =
- sizeof(StringCached) - sizeof(StringCached)
- sizeof(CodeT) - sizeof(CodeT)
- sizeof(ModuleFlag) - sizeof(ModuleFlag)
- sizeof(s32) - sizeof(u32)
) )
/ sizeof(SpecifierT) -1; // -1 for 4 extra bytes (Odd num of AST*) / sizeof(SpecifierT) -1; // -1 for 4 extra bytes (Odd num of AST*)
``` ```
@ -155,7 +150,7 @@ uw ArrSpecs_Cap =
Data Notes: Data Notes:
* The allocator definitions used are exposed to the user incase they want to dictate memory usage * 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`. * You'll find the memory handling in `init`, `deinit`, `reset`, `gen_string_allocator`, `get_cached_string`, `make_code`.
* ASTs are wrapped for the user in a Code struct which is a wrapper for a AST* type. * ASTs are wrapped for the user in a Code struct which is a wrapper for a AST* type.
* Both AST and Code have member symbols but their data layout is enforced to be POD types. * Both AST and Code have member symbols but their data layout is enforced to be POD types.
* This library treats memory failures as fatal. * This library treats memory failures as fatal.
@ -168,15 +163,15 @@ Data Notes:
* Linked lists used children nodes on bodies, and parameters. * Linked lists used children nodes on bodies, and parameters.
* Its intended to generate the AST in one go and serialize after. The constructors and serializer are designed to be a "one pass, front to back" setup. * Its intended to generate the AST in one go and serialize after. The constructors and serializer are designed to be a "one pass, front to back" setup.
* Allocations can be tuned by defining the folloiwng macros: * Allocations can be tuned by defining the folloiwng macros:
* `GEN_BUILDER_STR_BUFFER_RESERVE`
* `GEN_CODEPOOL_NUM_BLOCKS` : Number of blocks per code pool in the code allocator
* `GEN_GLOBAL_BUCKET_SIZE` : Size of each bucket area for the global allocator * `GEN_GLOBAL_BUCKET_SIZE` : Size of each bucket area for the global allocator
* `GEN_LEX_ALLOCATOR_SIZE` * `GEN_CODEPOOL_NUM_BLOCKS` : Number of blocks per code pool in the code allocator
* `GEN_SIZE_PER_STRING_ARENA` : Size per arena used with string caching.
* `GEN_MAX_COMMENT_LINE_LENGTH` : Longest length a comment can have per line. * `GEN_MAX_COMMENT_LINE_LENGTH` : Longest length a comment can have per line.
* `GEN_MAX_NAME_LENGTH` : Max length of any identifier. * `GEN_MAX_NAME_LENGTH` : Max length of any identifier.
* `GEN_MAX_UNTYPED_STR_LENGTH` : Max content length for any untyped code. * `GEN_MAX_UNTYPED_STR_LENGTH` : Max content length for any untyped code.
* `GEN_SIZE_PER_STRING_ARENA` : Size per arena used with string caching.
* `GEN_TOKEN_FMT_TOKEN_MAP_MEM_SIZE` : token_fmt_va uses local_persit memory of this size for the hashtable. * `GEN_TOKEN_FMT_TOKEN_MAP_MEM_SIZE` : token_fmt_va uses local_persit memory of this size for the hashtable.
* `GEN_LEX_ALLOCATOR_SIZE`
* `GEN_BUILDER_STR_BUFFER_RESERVE`
The following CodeTypes are used which the user may optionally use strong typing with if they enable: `GEN_ENFORCE_STRONG_CODE_TYPES` The following CodeTypes are used which the user may optionally use strong typing with if they enable: `GEN_ENFORCE_STRONG_CODE_TYPES`
@ -187,7 +182,6 @@ The following CodeTypes are used which the user may optionally use strong typing
* CodeConstructor * CodeConstructor
* CodeDefine * CodeDefine
* CodeDestructor * CodeDestructor
* CodePreprocessCond
* CodeEnum * CodeEnum
* CodeExec * CodeExec
* CodeExtern * CodeExtern
@ -195,10 +189,12 @@ The following CodeTypes are used which the user may optionally use strong typing
* CodeFriend * CodeFriend
* CodeFn * CodeFn
* CodeModule * CodeModule
* CodeNamespace * CodeNS
* CodeOperator * CodeOperator
* CodeOpCast * CodeOpCast
* CodeParam : Has support for `for-range` iterating across parameters. * CodeParam : Has support for `for-range` iterating across parameters.
* CodePreprocessCond
* CodePragma
* CodeSpecifiers : Has support for `for-range` iterating across specifiers. * CodeSpecifiers : Has support for `for-range` iterating across specifiers.
* CodeStruct * CodeStruct
* CodeTemplate * CodeTemplate
@ -221,7 +217,7 @@ Retrieving a raw version of the ast can be done using the `raw()` function defin
### Upfront Construction ### Upfront Construction
All component ASTs must be previously constructed, and provided on creation of the code AST. All component ASTs must be previously constructed, and provided on creation of the code AST.
The construction will fail and return Code::Invalid otherwise. The construction will fail and return CodeInvalid otherwise.
Interface :`` Interface :``
@ -231,6 +227,7 @@ Interface :``
* def_comment * def_comment
* def_class * def_class
* def_constructor * def_constructor
* def_define
* def_destructor * def_destructor
* def_enum * def_enum
* def_execution * def_execution
@ -245,6 +242,7 @@ Interface :``
* def_operator_cast * def_operator_cast
* def_param * def_param
* def_params * def_params
* def_preprocess_cond
* def_specifier * def_specifier
* def_specifiers * def_specifiers
* def_struct * def_struct
@ -321,19 +319,6 @@ Interface :
* parse_using * parse_using
* parse_variable * parse_variable
The lexing and parsing takes shortcuts from whats expected in the standard.
* Numeric literals are not check for validity.
* The parse API treats any execution scope definitions with no validation and are turned into untyped Code ASTs.
* *This includes the assignment of variables.*
* Attributes ( `[[]]` (standard), `__declspec` (Microsoft), or `__attribute__` (GNU) )
* Assumed to *come before specifiers* (`const`, `constexpr`, `extern`, `static`, etc) for a function
* Or in the usual spot for class, structs, (*right after the declaration keyword*)
* typedefs have attributes with the type (`parse_type`)
* As a general rule; if its not available from the upfront constructors, its not available in the parsing constructors.
* *Upfront constructors are not necessarily used in the parsing constructors, this is just a good metric to know what can be parsed.*
* Parsing attributes can be extended to support user defined macros by defining `GEN_DEFINE_ATTRIBUTE_TOKENS` (see `gen.hpp` for the formatting)
Usage: Usage:
```cpp ```cpp
@ -342,13 +327,6 @@ Code <name> = parse_<function name>( string with code );
Code <name> = def_<function name>( ..., parse_<function name>( Code <name> = def_<function name>( ..., parse_<function name>(
<string with code> <string with code>
)); ));
Code <name> = make_<function name>( ... )
{
<name>->add( parse_<function name>(
<string with code>
));
}
``` ```
### Untyped constructions ### Untyped constructions
@ -408,12 +386,15 @@ The following are provided predefined by the library as they are commonly used:
* `access_public` * `access_public`
* `access_protected` * `access_protected`
* `access_private` * `access_private`
* `attrib_api_export`
* `attrib_api_import`
* `module_global_fragment` * `module_global_fragment`
* `module_private_fragment` * `module_private_fragment`
* `fmt_newline`
* `pragma_once`
* `param_varaidc` (Used for varadic definitions) * `param_varaidc` (Used for varadic definitions)
* `preprocess_else` * `preprocess_else`
* `preprocess_endif` * `preprocess_endif`
* `pragma_once`
* `spec_const` * `spec_const`
* `spec_consteval` * `spec_consteval`
* `spec_constexpr` * `spec_constexpr`
@ -425,8 +406,10 @@ The following are provided predefined by the library as they are commonly used:
* `spec_internal_linkage` (internal macro) * `spec_internal_linkage` (internal macro)
* `spec_local_persist` (local_persist macro) * `spec_local_persist` (local_persist macro)
* `spec_mutable` * `spec_mutable`
* `spec_neverinline`
* `spec_override` * `spec_override`
* `spec_ptr` * `spec_ptr`
* `spec_pure`
* `spec_ref` * `spec_ref`
* `spec_register` * `spec_register`
* `spec_rvalue` * `spec_rvalue`
@ -434,10 +417,6 @@ The following are provided predefined by the library as they are commonly used:
* `spec_thread_local` * `spec_thread_local`
* `spec_virtual` * `spec_virtual`
* `spec_volatile` * `spec_volatile`
* `spec_type_signed`
* `spec_type_unsigned`
* `spec_type_short`
* `spec_type_long`
* `t_empty` (Used for varaidc macros) * `t_empty` (Used for varaidc macros)
* `t_auto` * `t_auto`
* `t_void` * `t_void`

View File

@ -1,7 +1,6 @@
# Documentation # Documentation
The library is fragmented into a series of headers and sources files meant to be scanned in and then generated to a tailored format for the target The library is fragmented into a series of headers and source files meant to be scanned in and then generated to a tailored format for the target `gen` files.
`gen` files.
The principal (user) files are `gen.hpp` and `gen.cpp`. The principal (user) files are `gen.hpp` and `gen.cpp`.
They contain includes for its various components: `components/<component_name>.<hpp/cpp>` They contain includes for its various components: `components/<component_name>.<hpp/cpp>`
@ -14,7 +13,6 @@ They directly include `depedencies/file_handling.<hpp/cpp>` as the core library
**TODO : Right now the library is not finished, as such the first self-hosting iteration is still WIP** **TODO : Right now the library is not finished, as such the first self-hosting iteration is still WIP**
Both libraries use *pre-generated* (self-hosting I guess) version of the library to then generate the latest version of itself. Both libraries use *pre-generated* (self-hosting I guess) version of the library to then generate the latest version of itself.
(sort of a verification that the generated version is equivalent).
The default `gen.bootstrap.cpp` located in the project folder is meant to be produce a standard segmented library, where the components of the library The default `gen.bootstrap.cpp` located in the project folder is meant to be produce a standard segmented library, where the components of the library
have relatively dedicated header and source files. Dependencies included at the top of the file and each header starting with a pragma once. have relatively dedicated header and source files. Dependencies included at the top of the file and each header starting with a pragma once.
@ -28,7 +26,6 @@ Feature Macros:
* This is auto-generated if using the bootstrap or single-header generation * This is auto-generated if using the bootstrap or single-header generation
* *Note: The user will use the `AttributeTokens.csv` when the library is fully self-hosting.* * *Note: The user will use the `AttributeTokens.csv` when the library is fully self-hosting.*
* `GEN_DEFINE_LIBRARY_CORE_CONSTANTS` : Optional typename codes as they are non-standard to C/C++ and not necessary to library usage * `GEN_DEFINE_LIBRARY_CORE_CONSTANTS` : Optional typename codes as they are non-standard to C/C++ and not necessary to library usage
* `GEN_DONT_USE_NAMESPACE` : By default, the library is wrapped in a `gen` namespace, this will disable that expose it to the global scope.
* `GEN_DONT_ENFORCE_GEN_TIME_GUARD` : By default, the library ( gen.hpp/ gen.cpp ) expects the macro `GEN_TIME` to be defined, this disables that. * `GEN_DONT_ENFORCE_GEN_TIME_GUARD` : By default, the library ( gen.hpp/ gen.cpp ) expects the macro `GEN_TIME` to be defined, this disables that.
* `GEN_ENFORCE_STRONG_CODE_TYPES` : Enforces casts to filtered code types. * `GEN_ENFORCE_STRONG_CODE_TYPES` : Enforces casts to filtered code types.
* `GEN_EXPOSE_BACKEND` : Will expose symbols meant for internal use only. * `GEN_EXPOSE_BACKEND` : Will expose symbols meant for internal use only.
@ -36,13 +33,13 @@ Feature Macros:
## On multi-threading ## On multi-threading
Currently unsupported. The following changes would have to be made: Currently unsupported.
## Extending the library ## Extending the library
This library is relatively very small, and can be extended without much hassle. This library is relatively very small, and can be extended without much hassle.
The convention you'll see used throughout the API of the library is as follows: The convention you'll see used throughout the interface of the library is as follows:
1. Check name or parameters to make sure they are valid for the construction requested 1. Check name or parameters to make sure they are valid for the construction requested
2. Create a code object using `make_code`. 2. Create a code object using `make_code`.
@ -55,3 +52,4 @@ Names or Content fields are interned strings and thus showed be cached using `ge
The library has its code segmented into component files, use it to help create a derived version without needing to have to rewrite a generated file directly or build on top of the header via composition or inheritance. The library has its code segmented into component files, use it to help create a derived version without needing to have to rewrite a generated file directly or build on top of the header via composition or inheritance.
When the scanner is implemented, this will be even easier to customize. When the scanner is implemented, this will be even easier to customize.

View File

@ -1,5 +1,3 @@
#pragma region AST
Code Code::Global; Code Code::Global;
Code Code::Invalid; Code Code::Invalid;
@ -31,6 +29,10 @@ String AST::to_string()
log_failure("Attempted to serialize invalid code! - %s", Parent ? Parent->debug_str() : Name ); log_failure("Attempted to serialize invalid code! - %s", Parent ? Parent->debug_str() : Name );
break; break;
case NewLine:
result.append("\n");
break;
case Untyped: case Untyped:
case Execution: case Execution:
result.append( Content ); result.append( Content );
@ -143,6 +145,70 @@ String AST::to_string()
} }
break; break;
case Constructor:
{
result.append( Parent->Name );
if ( Params )
result.append_fmt( "( %s )", Params->to_string() );
else
result.append( "(void)" );
if ( InitializerList )
result.append_fmt( " : %s", InitializerList->to_string() );
result.append_fmt( "\n{\n%s\n}", Body->to_string() );
}
break;
case Constructor_Fwd:
{
result.append( Parent->Name );
if ( Params )
result.append_fmt( "( %s )", Params->to_string() );
else
result.append( "(void);" );
}
break;
case Destructor:
{
if ( Specs )
{
CodeSpecifiers specs = Specs->cast<CodeSpecifiers>();
if ( specs.has( ESpecifier::Virtual ) )
result.append_fmt( "virtual ~%s()", Parent->Name );
else
result.append_fmt( "~%s()", Parent->Name );
}
else
result.append_fmt( "~%s()", Parent->Name );
result.append_fmt( "\n{\n%s\n}", Body->to_string() );
}
break;
case Destructor_Fwd:
{
if ( Specs )
{
CodeSpecifiers specs = Specs->cast<CodeSpecifiers>();
if ( specs.has( ESpecifier::Virtual ) )
result.append_fmt( "virtual ~%s();", Parent->Name );
else
result.append_fmt( "~%s()", Parent->Name );
if ( specs.has( ESpecifier::Pure ) )
result.append( " = 0;" );
}
else
result.append_fmt( "~%s();", Parent->Name );
}
break;
case Enum: case Enum:
{ {
if ( bitfield_is_equal( u32, ModuleFlags, ModuleFlag::Export )) if ( bitfield_is_equal( u32, ModuleFlags, ModuleFlag::Export ))
@ -259,7 +325,7 @@ String AST::to_string()
s32 left = NumEntries; s32 left = NumEntries;
while ( left-- ) while ( left-- )
{ {
result.append_fmt( "%s\n", curr.to_string() ); result.append_fmt( "%s", curr.to_string() );
++curr; ++curr;
} }
@ -306,9 +372,7 @@ String AST::to_string()
if ( Specs ) if ( Specs )
{ {
CodeSpecifiers specs = cast<CodeSpecifiers>(); for ( SpecifierT spec : Specs->cast<CodeSpecifiers>() )
for ( SpecifierT spec : specs )
{ {
if ( ESpecifier::is_trailing( spec ) ) if ( ESpecifier::is_trailing( spec ) )
result.append_fmt( " %s", (char const*)ESpecifier::to_str( spec ) ); result.append_fmt( " %s", (char const*)ESpecifier::to_str( spec ) );
@ -346,9 +410,7 @@ String AST::to_string()
if ( Specs ) if ( Specs )
{ {
CodeSpecifiers specs = cast<CodeSpecifiers>(); for ( SpecifierT spec : Specs->cast<CodeSpecifiers>() )
for ( SpecifierT spec : specs )
{ {
if ( ESpecifier::is_trailing( spec ) ) if ( ESpecifier::is_trailing( spec ) )
result.append_fmt( " %s", (char const*)ESpecifier::to_str( spec ) ); result.append_fmt( " %s", (char const*)ESpecifier::to_str( spec ) );
@ -373,7 +435,7 @@ String AST::to_string()
if ( bitfield_is_equal( u32, ModuleFlags, ModuleFlag::Export )) if ( bitfield_is_equal( u32, ModuleFlags, ModuleFlag::Export ))
result.append( "export " ); result.append( "export " );
result.append_fmt( "namespace %s\n{\n%s}" result.append_fmt( "namespace %s\n{\n%s\n}"
, Name , Name
, Body->to_string() , Body->to_string()
); );
@ -402,9 +464,7 @@ String AST::to_string()
if ( Specs ) if ( Specs )
{ {
CodeSpecifiers specs = cast<CodeSpecifiers>(); for ( SpecifierT spec : Specs->cast<CodeSpecifiers>() )
for ( SpecifierT spec : specs )
{ {
if ( ESpecifier::is_trailing( spec ) ) if ( ESpecifier::is_trailing( spec ) )
result.append_fmt( " %s", (char const*)ESpecifier::to_str( spec ) ); result.append_fmt( " %s", (char const*)ESpecifier::to_str( spec ) );
@ -439,9 +499,7 @@ String AST::to_string()
if ( Specs ) if ( Specs )
{ {
CodeSpecifiers specs = cast<CodeSpecifiers>(); for ( SpecifierT spec : Specs->cast<CodeSpecifiers>() )
for ( SpecifierT spec : specs )
{ {
if ( ESpecifier::is_trailing( spec ) ) if ( ESpecifier::is_trailing( spec ) )
result.append_fmt( " %s", (char const*)ESpecifier::to_str( spec ) ); result.append_fmt( " %s", (char const*)ESpecifier::to_str( spec ) );
@ -457,13 +515,11 @@ String AST::to_string()
if ( Specs ) if ( Specs )
{ {
if ( Name && Name.length() ) if ( Name && Name.length() )
result.append_fmt( "%.*soperator %s()", Name.length(), Name, EOperator::to_str( Op )); result.append_fmt( "%.*soperator %s()", Name.length(), Name, ValueType->to_string() );
else else
result.append_fmt( "operator %s()", EOperator::to_str( Op ) ); result.append_fmt( "operator %s()", ValueType->to_string() );
CodeSpecifiers specs = cast<CodeSpecifiers>(); for ( SpecifierT spec : Specs->cast<CodeSpecifiers>() )
for ( SpecifierT spec : specs )
{ {
if ( ESpecifier::is_trailing( spec ) ) if ( ESpecifier::is_trailing( spec ) )
result.append_fmt( " %s", (char const*)ESpecifier::to_str( spec ) ); result.append_fmt( " %s", (char const*)ESpecifier::to_str( spec ) );
@ -483,11 +539,9 @@ String AST::to_string()
case Operator_Cast_Fwd: case Operator_Cast_Fwd:
if ( Specs ) if ( Specs )
{ {
result.append_fmt( "operator %s()", ValueType->to_string() ); result.append_fmt( "operator %s()", ValueType->to_string() );
CodeSpecifiers specs = cast<CodeSpecifiers>(); for ( SpecifierT spec : Specs->cast<CodeSpecifiers>() )
for ( SpecifierT spec : specs )
{ {
if ( ESpecifier::is_trailing( spec ) ) if ( ESpecifier::is_trailing( spec ) )
result.append_fmt( " %s", (char const*)ESpecifier::to_str( spec ) ); result.append_fmt( " %s", (char const*)ESpecifier::to_str( spec ) );
@ -526,9 +580,8 @@ String AST::to_string()
} }
} }
break; break;
case Preprocess_Define: case Preprocess_Define:
result.append_fmt( "#define %s%s", Name, Content ); result.append_fmt( "#define %s %s", Name, Content );
break; break;
case Preprocess_If: case Preprocess_If:
@ -544,7 +597,7 @@ String AST::to_string()
break; break;
case Preprocess_Include: case Preprocess_Include:
result.append_fmt( "#include \"%s\"", Content ); result.append_fmt( "#include \"%s\"\n", Content );
break; break;
case Preprocess_ElIf: case Preprocess_ElIf:
@ -552,11 +605,11 @@ String AST::to_string()
break; break;
case Preprocess_Else: case Preprocess_Else:
result.append_fmt( "#else" ); result.append_fmt( "\n#else" );
break; break;
case Preprocess_EndIf: case Preprocess_EndIf:
result.append_fmt( "#endif\n" ); result.append_fmt( "#endif" );
break; break;
case Preprocess_Pragma: case Preprocess_Pragma:
@ -569,7 +622,7 @@ String AST::to_string()
s32 left = NumEntries; s32 left = NumEntries;
while ( left-- ) while ( left-- )
{ {
if ( ESpecifier::is_trailing( ArrSpecs[idx]) ) if ( ESpecifier::is_trailing( ArrSpecs[idx]) && ArrSpecs[idx] != ESpecifier::Const )
{ {
idx++; idx++;
continue; continue;
@ -588,7 +641,7 @@ String AST::to_string()
if ( Name == nullptr) if ( Name == nullptr)
{ {
result.append( "struct\n{\n%s\n};", Body->to_string() ); result.append_fmt( "struct\n{\n%s\n};", Body->to_string() );
break; break;
} }
@ -669,7 +722,10 @@ String AST::to_string()
result.append( "typedef "); result.append( "typedef ");
result.append_fmt( "%s %s", UnderlyingType->to_string(), Name ); if ( IsFunction )
result.append( UnderlyingType->to_string() );
else
result.append_fmt( "%s %s", UnderlyingType->to_string(), Name );
if ( UnderlyingType->Type == Typename && UnderlyingType->ArrExpr ) if ( UnderlyingType->Type == Typename && UnderlyingType->ArrExpr )
{ {
@ -798,8 +854,21 @@ String AST::to_string()
} }
break; break;
case Class_Body: #if 0
{
Code curr = Front->cast<Code>();
s32 left = NumEntries;
while ( left -- )
{
result.append_fmt( "%s", curr.to_string() );
++curr;
}
}
break;
#endif
case Enum_Body: case Enum_Body:
case Class_Body:
case Extern_Linkage_Body: case Extern_Linkage_Body:
case Function_Body: case Function_Body:
case Global_Body: case Global_Body:
@ -811,7 +880,11 @@ String AST::to_string()
s32 left = NumEntries; s32 left = NumEntries;
while ( left -- ) while ( left -- )
{ {
result.append_fmt( "%s\n", curr.to_string() ); result.append_fmt( "%s", curr.to_string() );
if ( curr->Type != ECode::NewLine )
result.append( "\n" );
++curr; ++curr;
} }
} }
@ -888,7 +961,31 @@ bool AST::validate_body()
CheckEntries( GEN_AST_BODY_FUNCTION_UNALLOWED_TYPES ); CheckEntries( GEN_AST_BODY_FUNCTION_UNALLOWED_TYPES );
break; break;
case Global_Body: case Global_Body:
CheckEntries( GEN_AST_BODY_GLOBAL_UNALLOWED_TYPES ); for (Code entry : cast<CodeBody>())
{
switch (entry->Type)
{
case Access_Public:
case Access_Protected:
case Access_Private:
case PlatformAttributes:
case Class_Body:
case Enum_Body:
case Execution:
case Friend:
case Function_Body:
case Global_Body:
case Namespace_Body:
case Operator_Member:
case Operator_Member_Fwd:
case Parameters:
case Specifiers:
case Struct_Body:
case Typename:
log_failure("AST::validate_body: Invalid entry in body %s", entry.debug_str());
return false;
}
}
break; break;
case Namespace_Body: case Namespace_Body:
CheckEntries( GEN_AST_BODY_NAMESPACE_UNALLOWED_TYPES ); CheckEntries( GEN_AST_BODY_NAMESPACE_UNALLOWED_TYPES );
@ -916,5 +1013,3 @@ bool AST::validate_body()
#undef CheckEntries #undef CheckEntries
} }
#pragma endregion AST

577
project/components/ast.hpp Normal file
View File

@ -0,0 +1,577 @@
struct AST;
struct AST_Body;
struct AST_Attributes;
struct AST_Comment;
struct AST_Constructor;
struct AST_Class;
struct AST_Define;
struct AST_Destructor;
struct AST_Enum;
struct AST_Exec;
struct AST_Extern;
struct AST_Include;
struct AST_Friend;
struct AST_Fn;
struct AST_Module;
struct AST_NS;
struct AST_Operator;
struct AST_OpCast;
struct AST_Param;
struct AST_Pragma;
struct AST_PreprocessCond;
struct AST_Specifiers;
struct AST_Struct;
struct AST_Template;
struct AST_Type;
struct AST_Typedef;
struct AST_Union;
struct AST_Using;
struct AST_Var;
struct Code;
struct CodeBody;
// These are to offer ease of use and optionally strong type safety for the AST.
struct CodeAttributes;
struct CodeComment;
struct CodeConstructor;
struct CodeDestructor;
struct CodeClass;
struct CodeDefine;
struct CodeEnum;
struct CodeExec;
struct CodeExtern;
struct CodeInclude;
struct CodeFriend;
struct CodeFn;
struct CodeModule;
struct CodeNS;
struct CodeOperator;
struct CodeOpCast;
struct CodeParam;
struct CodePreprocessCond;
struct CodePragma;
struct CodeSpecifiers;
struct CodeStruct;
struct CodeTemplate;
struct CodeType;
struct CodeTypedef;
struct CodeUnion;
struct CodeUsing;
struct CodeVar;
/*
AST* wrapper
- Not constantly have to append the '*' as this is written often..
- Allows for implicit conversion to any of the ASTs (raw or filtered).
*/
struct Code
{
# pragma region Statics
// Used to identify ASTs that should always be duplicated. (Global constant ASTs)
static Code Global;
// Used to identify invalid generated code.
static Code Invalid;
# pragma endregion Statics
#define Using_Code( Typename ) \
char const* debug_str(); \
Code duplicate(); \
bool is_equal( Code other ); \
bool is_valid(); \
void set_global(); \
String to_string(); \
Typename& operator = ( AST* other ); \
Typename& operator = ( Code other ); \
bool operator ==( Code other ); \
bool operator !=( Code other ); \
operator bool();
Using_Code( Code );
template< class Type >
Type cast()
{
return * rcast( Type*, this );
}
AST* operator ->()
{
return ast;
}
Code& operator ++();
Code& operator*()
{
return *this;
}
AST* ast;
#ifdef GEN_ENFORCE_STRONG_CODE_TYPES
# define operator explicit operator
#endif
operator CodeAttributes() const;
operator CodeComment() const;
operator CodeConstructor() const;
operator CodeDestructor() const;
operator CodeClass() const;
operator CodeDefine() const;
operator CodeExec() const;
operator CodeEnum() const;
operator CodeExtern() const;
operator CodeInclude() const;
operator CodeFriend() const;
operator CodeFn() const;
operator CodeModule() const;
operator CodeNS() const;
operator CodeOperator() const;
operator CodeOpCast() const;
operator CodeParam() const;
operator CodePragma() const;
operator CodePreprocessCond() const;
operator CodeSpecifiers() const;
operator CodeStruct() const;
operator CodeTemplate() const;
operator CodeType() const;
operator CodeTypedef() const;
operator CodeUnion() const;
operator CodeUsing() const;
operator CodeVar() const;
operator CodeBody() const;
#undef operator
};
struct Code_POD
{
AST* ast;
};
static_assert( sizeof(Code) == sizeof(Code_POD), "ERROR: Code is not POD" );
// Desired width of the AST data structure.
constexpr u32 AST_POD_Size = 128;
/*
Simple AST POD with functionality to seralize into C++ syntax.
*/
struct AST
{
# pragma region Member Functions
void append ( AST* other );
char const* debug_str ();
AST* duplicate ();
Code& entry ( u32 idx );
bool has_entries();
bool is_equal ( AST* other );
String to_string ();
char const* type_str();
bool validate_body();
template< class Type >
Type cast()
{
return * this;
}
operator Code();
operator CodeBody();
operator CodeAttributes();
operator CodeComment();
operator CodeConstructor();
operator CodeDestructor();
operator CodeClass();
operator CodeDefine();
operator CodeEnum();
operator CodeExec();
operator CodeExtern();
operator CodeInclude();
operator CodeFriend();
operator CodeFn();
operator CodeModule();
operator CodeNS();
operator CodeOperator();
operator CodeOpCast();
operator CodeParam();
operator CodePragma();
operator CodePreprocessCond();
operator CodeSpecifiers();
operator CodeStruct();
operator CodeTemplate();
operator CodeType();
operator CodeTypedef();
operator CodeUnion();
operator CodeUsing();
operator CodeVar();
# pragma endregion Member Functions
constexpr static
uw ArrSpecs_Cap =
(
AST_POD_Size
- sizeof(AST*) * 3
- sizeof(StringCached)
- sizeof(CodeT)
- sizeof(ModuleFlag)
- sizeof(u32)
)
/ sizeof(SpecifierT) -1; // -1 for 4 extra bytes
union {
struct
{
AST* Attributes; // Class, Enum, Function, Struct, Typedef, Union, Using, Variable
AST* Specs; // Function, Operator, Type symbol, Variable
union {
AST* InitializerList; // Constructor, Destructor
AST* ParentType; // Class, Struct
AST* ReturnType; // Function, Operator
AST* UnderlyingType; // Enum, Typedef
AST* ValueType; // Parameter, Variable
};
union {
AST* BitfieldSize; // Varaiable (Class/Struct Data Member)
AST* Params; // Function, Operator, Template
};
union {
AST* ArrExpr; // Type Symbol
AST* Body; // Class, Constructr, Destructor, Enum, Function, Namespace, Struct, Union
AST* Declaration; // Friend, Template
AST* Value; // Parameter, Variable
};
};
StringCached Content; // Attributes, Comment, Execution, Include
SpecifierT ArrSpecs[AST::ArrSpecs_Cap]; // Specifiers
};
union {
AST* Prev;
AST* Front;
AST* Last;
};
union {
AST* Next;
AST* Back;
};
AST* Parent;
StringCached Name;
CodeT Type;
ModuleFlag ModuleFlags;
union {
b32 IsFunction; // Used by typedef to not serialize the name field.
OperatorT Op;
AccessSpec ParentAccess;
s32 NumEntries;
};
};
struct AST_POD
{
union {
struct
{
AST* Attributes; // Class, Enum, Function, Struct, Typename, Union, Using, Variable
AST* Specs; // Function, Operator, Type symbol, Variable
union {
AST* InitializerList; // Constructor, Destructor
AST* ParentType; // Class, Struct
AST* ReturnType; // Function, Operator
AST* UnderlyingType; // Enum, Typedef
AST* ValueType; // Parameter, Variable
};
union {
AST* BitfieldSize; // Varaiable (Class/Struct Data Member)
AST* Params; // Function, Operator, Template
};
union {
AST* ArrExpr; // Type Symbol
AST* Body; // Class, Constructr, Destructor, Enum, Function, Namespace, Struct, Union
AST* Declaration; // Friend, Template
AST* Value; // Parameter, Variable
};
};
StringCached Content; // Attributes, Comment, Execution, Include
SpecifierT ArrSpecs[AST::ArrSpecs_Cap]; // Specifiers
};
union {
AST* Prev;
AST* Front;
AST* Last;
};
union {
AST* Next;
AST* Back;
};
AST* Parent;
StringCached Name;
CodeT Type;
ModuleFlag ModuleFlags;
union {
b32 IsFunction; // Used by typedef to not serialize the name field.
OperatorT Op;
AccessSpec ParentAccess;
s32 NumEntries;
};
};
// Its intended for the AST to have equivalent size to its POD.
// All extra functionality within the AST namespace should just be syntatic sugar.
static_assert( sizeof(AST) == sizeof(AST_POD), "ERROR: AST IS NOT POD" );
static_assert( sizeof(AST_POD) == AST_POD_Size, "ERROR: AST POD is not size of AST_POD_Size" );
// Used when the its desired when omission is allowed in a definition.
#define NoCode { nullptr }
#define CodeInvalid (* Code::Invalid.ast) // Uses an implicitly overloaded cast from the AST to the desired code type.
#pragma region Code Types
struct CodeBody
{
Using_Code( CodeBody );
void append( Code other )
{
raw()->append( other.ast );
}
void append( CodeBody body )
{
for ( Code entry : body )
{
append( entry );
}
}
bool has_entries()
{
return rcast( AST*, ast )->has_entries();
}
AST* raw()
{
return rcast( AST*, ast );
}
AST_Body* operator->()
{
return ast;
}
operator Code()
{
return * rcast( Code*, this );
}
#pragma region Iterator
Code begin()
{
if ( ast )
return { rcast( AST*, ast)->Front };
return { nullptr };
}
Code end()
{
return { rcast(AST*, ast)->Back->Next };
}
#pragma endregion Iterator
AST_Body* ast;
};
struct CodeClass
{
Using_Code( CodeClass );
void add_interface( CodeType interface );
AST* raw()
{
return rcast( AST*, ast );
}
operator Code()
{
return * rcast( Code*, this );
}
AST_Class* operator->()
{
if ( ast == nullptr )
{
log_failure("Attempt to dereference a nullptr");
return nullptr;
}
return ast;
}
AST_Class* ast;
};
struct CodeParam
{
Using_Code( CodeParam );
void append( CodeParam other );
CodeParam get( s32 idx );
bool has_entries();
AST* raw()
{
return rcast( AST*, ast );
}
AST_Param* operator->()
{
if ( ast == nullptr )
{
log_failure("Attempt to dereference a nullptr!");
return nullptr;
}
return ast;
}
operator Code()
{
return { (AST*)ast };
}
#pragma region Iterator
CodeParam begin()
{
if ( ast )
return { ast };
return { nullptr };
}
CodeParam end()
{
return { (AST_Param*) rcast( AST*, ast)->Last };
}
CodeParam& operator++();
CodeParam operator*()
{
return * this;
}
#pragma endregion Iterator
AST_Param* ast;
};
struct CodeSpecifiers
{
Using_Code( CodeSpecifiers );
bool append( SpecifierT spec )
{
if ( ast == nullptr )
{
log_failure("CodeSpecifiers: Attempted to append to a null specifiers AST!");
return false;
}
if ( raw()->NumEntries == AST::ArrSpecs_Cap )
{
log_failure("CodeSpecifiers: Attempted to append over %d specifiers to a specifiers AST!", AST::ArrSpecs_Cap );
return false;
}
raw()->ArrSpecs[ raw()->NumEntries ] = spec;
raw()->NumEntries++;
return true;
}
s32 has( SpecifierT spec )
{
for ( s32 idx = 0; idx < raw()->NumEntries; idx++ )
{
if ( raw()->ArrSpecs[ raw()->NumEntries ] == spec )
return idx;
}
return -1;
}
AST* raw()
{
return rcast( AST*, ast );
}
AST_Specifiers* operator->()
{
if ( ast == nullptr )
{
log_failure("Attempt to dereference a nullptr!");
return nullptr;
}
return ast;
}
operator Code()
{
return { (AST*) ast };
}
#pragma region Iterator
SpecifierT* begin()
{
if ( ast )
return & raw()->ArrSpecs[0];
return nullptr;
}
SpecifierT* end()
{
return raw()->ArrSpecs + raw()->NumEntries;
}
#pragma endregion Iterator
AST_Specifiers* ast;
};
struct CodeStruct
{
Using_Code( CodeStruct );
void add_interface( CodeType interface );
AST* raw()
{
return rcast( AST*, ast );
}
operator Code()
{
return * rcast( Code*, this );
}
AST_Struct* operator->()
{
if ( ast == nullptr )
{
log_failure("Attempt to dereference a nullptr");
return nullptr;
}
return ast;
}
AST_Struct* ast;
};
#define Define_CodeType( Typename ) \
struct Code##Typename \
{ \
Using_Code( Code ## Typename ); \
AST* raw(); \
operator Code(); \
AST_##Typename* operator->(); \
AST_##Typename* ast; \
}
Define_CodeType( Attributes );
Define_CodeType( Comment );
Define_CodeType( Constructor );
Define_CodeType( Define );
Define_CodeType( Destructor );
Define_CodeType( Enum );
Define_CodeType( Exec );
Define_CodeType( Extern );
Define_CodeType( Include );
Define_CodeType( Friend );
Define_CodeType( Fn );
Define_CodeType( Module );
Define_CodeType( NS );
Define_CodeType( Operator );
Define_CodeType( OpCast );
Define_CodeType( Pragma );
Define_CodeType( PreprocessCond );
Define_CodeType( Template );
Define_CodeType( Type );
Define_CodeType( Typedef );
Define_CodeType( Union );
Define_CodeType( Using );
Define_CodeType( Var );
#undef Define_CodeType
#undef Using_Code
#pragma endregion Code Types

View File

@ -49,7 +49,6 @@
case Execution: \ case Execution: \
case Friend: \ case Friend: \
case Function_Body: \ case Function_Body: \
case Global_Body: \
case Namespace_Body: \ case Namespace_Body: \
case Operator_Member: \ case Operator_Member: \
case Operator_Member_Fwd: \ case Operator_Member_Fwd: \
@ -77,3 +76,4 @@
case Specifiers: \ case Specifiers: \
case Struct_Body: \ case Struct_Body: \
case Typename: case Typename:

View File

@ -1,591 +1,4 @@
#pragma region Data Structures #pragma region AST Types
// Implements basic string interning. Data structure is based off the ZPL Hashtable.
using StringTable = HashTable<String const>;
// Represents strings cached with the string table.
// Should never be modified, if changed string is desired, cache_string( str ) another.
using StringCached = String const;
struct AST;
struct AST_Body;
struct AST_Attributes;
struct AST_Comment;
struct AST_Class;
struct AST_Define;
struct AST_Enum;
struct AST_Exec;
struct AST_Extern;
struct AST_Include;
struct AST_Friend;
struct AST_Fn;
struct AST_Module;
struct AST_Namespace;
struct AST_Operator;
struct AST_OpCast;
struct AST_Param;
struct AST_Pragma;
struct AST_PreprocessCond;
struct AST_Specifiers;
struct AST_Struct;
struct AST_Template;
struct AST_Type;
struct AST_Typedef;
struct AST_Union;
struct AST_Using;
struct AST_Var;
struct Code;
struct CodeBody;
// These are to offer ease of use and optionally strong type safety for the AST.
struct CodeAttributes;
struct CodeComment;
struct CodeClass;
struct CodeDefine;
struct CodeEnum;
struct CodeExec;
struct CodeExtern;
struct CodeInclude;
struct CodeFriend;
struct CodeFn;
struct CodeModule;
struct CodeNamespace;
struct CodeOperator;
struct CodeOpCast;
struct CodeParam;
struct CodePreprocessCond;
struct CodePragma;
struct CodeSpecifiers;
struct CodeStruct;
struct CodeTemplate;
struct CodeType;
struct CodeTypedef;
struct CodeUnion;
struct CodeUsing;
struct CodeVar;
/*
AST* wrapper
- Not constantly have to append the '*' as this is written often..
- Allows for implicit conversion to any of the ASTs (raw or filtered).
*/
struct Code
{
# pragma region Statics
// Used to identify ASTs that should always be duplicated. (Global constant ASTs)
static Code Global;
// Used to identify invalid generated code.
static Code Invalid;
# pragma endregion Statics
#define Using_Code( Typename ) \
char const* debug_str(); \
Code duplicate(); \
bool is_equal( Code other ); \
bool is_valid(); \
void set_global(); \
String to_string(); \
Typename& operator = ( AST* other ); \
Typename& operator = ( Code other ); \
bool operator ==( Code other ); \
bool operator !=( Code other ); \
operator bool() \
{ \
return ast != nullptr; \
}
template< class Type >
Type cast()
{
return * rcast( Type*, this );
}
AST* operator ->()
{
return ast;
}
Code& operator ++();
Code& operator*()
{
return *this;
}
Using_Code( Code );
AST* ast;
#ifdef GEN_ENFORCE_STRONG_CODE_TYPES
# define operator explicit operator
#endif
operator CodeAttributes() const;
operator CodeComment() const;
operator CodeClass() const;
operator CodeDefine() const;
operator CodeExec() const;
operator CodeEnum() const;
operator CodeExtern() const;
operator CodeInclude() const;
operator CodeFriend() const;
operator CodeFn() const;
operator CodeModule() const;
operator CodeNamespace() const;
operator CodeOperator() const;
operator CodeOpCast() const;
operator CodeParam() const;
operator CodePragma() const;
operator CodePreprocessCond() const;
operator CodeSpecifiers() const;
operator CodeStruct() const;
operator CodeTemplate() const;
operator CodeType() const;
operator CodeTypedef() const;
operator CodeUnion() const;
operator CodeUsing() const;
operator CodeVar() const;
operator CodeBody() const;
#undef operator
};
struct Code_POD
{
AST* ast;
};
static_assert( sizeof(Code) == sizeof(Code_POD), "ERROR: Code is not POD" );
// Desired width of the AST data structure.
constexpr u32 AST_POD_Size = 128;
/*
Simple AST POD with functionality to seralize into C++ syntax.
*/
struct AST
{
# pragma region Member Functions
void append ( AST* other );
char const* debug_str ();
AST* duplicate ();
Code& entry ( u32 idx );
bool has_entries();
bool is_equal ( AST* other );
String to_string ();
char const* type_str();
bool validate_body();
template< class Type >
Type cast()
{
return * this;
}
operator Code();
operator CodeBody();
operator CodeAttributes();
operator CodeComment();
operator CodeClass();
operator CodeDefine();
operator CodeEnum();
operator CodeExec();
operator CodeExtern();
operator CodeInclude();
operator CodeFriend();
operator CodeFn();
operator CodeModule();
operator CodeNamespace();
operator CodeOperator();
operator CodeOpCast();
operator CodeParam();
operator CodePragma();
operator CodePreprocessCond();
operator CodeSpecifiers();
operator CodeStruct();
operator CodeTemplate();
operator CodeType();
operator CodeTypedef();
operator CodeUnion();
operator CodeUsing();
operator CodeVar();
# pragma endregion Member Functions
constexpr static
uw ArrSpecs_Cap =
(
AST_POD_Size
- sizeof(AST*) * 3
- sizeof(StringCached)
- sizeof(CodeT)
- sizeof(ModuleFlag)
- sizeof(s32)
)
/ sizeof(SpecifierT) -1; // -1 for 4 extra bytes
union {
struct
{
AST* Attributes; // Class, Enum, Function, Struct, Typedef, Union, Using, Variable
AST* Specs; // Function, Operator, Type symbol, Variable
union {
AST* ParentType; // Class, Struct
AST* ReturnType; // Function, Operator
AST* UnderlyingType; // Enum, Typedef
AST* ValueType; // Parameter, Variable
};
union {
AST* Params; // Function, Operator, Template
AST* BitfieldSize; // Varaiable (Class/Struct Data Member)
};
union {
AST* ArrExpr; // Type Symbol
AST* Body; // Class, Enum, Function, Namespace, Struct, Union
AST* Declaration; // Friend, Template
AST* Value; // Parameter, Variable
};
};
StringCached Content; // Attributes, Comment, Execution, Include
SpecifierT ArrSpecs[AST::ArrSpecs_Cap]; // Specifiers
};
union {
AST* Prev;
AST* Front;
AST* Last;
};
union {
AST* Next;
AST* Back;
};
AST* Parent;
StringCached Name;
CodeT Type;
ModuleFlag ModuleFlags;
union {
OperatorT Op;
AccessSpec ParentAccess;
s32 NumEntries;
};
};
struct AST_POD
{
union {
struct
{
AST* Attributes; // Class, Enum, Function, Struct, Typename, Union, Using, Variable
AST* Specs; // Function, Operator, Type symbol, Variable
union {
AST* ParentType; // Class, Struct
AST* ReturnType; // Function, Operator
AST* UnderlyingType; // Enum, Typedef
AST* ValueType; // Parameter, Variable
};
union {
AST* Params; // Function, Operator, Template
AST* BitfieldSize; // Varaiable (Class/Struct Data Member)
};
union {
AST* ArrExpr; // Type Symbol
AST* Body; // Class, Enum, Function, Namespace, Struct, Union
AST* Declaration; // Friend, Template
AST* Value; // Parameter, Variable
};
};
StringCached Content; // Attributes, Comment, Execution, Include
SpecifierT ArrSpecs[AST::ArrSpecs_Cap]; // Specifiers
};
union {
AST* Prev;
AST* Front;
AST* Last;
};
union {
AST* Next;
AST* Back;
};
AST* Parent;
StringCached Name;
CodeT Type;
ModuleFlag ModuleFlags;
union {
OperatorT Op;
AccessSpec ParentAccess;
s32 NumEntries;
};
};
// Its intended for the AST to have equivalent size to its POD.
// All extra functionality within the AST namespace should just be syntatic sugar.
static_assert( sizeof(AST) == sizeof(AST_POD), "ERROR: AST IS NOT POD" );
static_assert( sizeof(AST_POD) == AST_POD_Size, "ERROR: AST POD is not size of AST_POD_Size" );
// Used when the its desired when omission is allowed in a definition.
#define NoCode { nullptr }
#define CodeInvalid (* Code::Invalid.ast) // Uses an implicitly overloaded cast from the AST to the desired code type.
#pragma region Code Types
#define Define_CodeType( Typename ) \
struct Code##Typename \
{ \
Using_Code( Code##Typename ); \
AST* raw() \
{ \
return rcast( AST*, ast ); \
} \
operator Code() \
{ \
return * rcast( Code*, this ); \
} \
AST_##Typename* operator->() \
{ \
if ( ast == nullptr ) \
{ \
log_failure("Attempt to dereference a nullptr!"); \
return nullptr; \
} \
return ast; \
} \
AST_##Typename* ast; \
}
struct CodeBody
{
Using_Code( CodeBody );
void append( Code other )
{
raw()->append( other.ast );
}
void append( CodeBody body )
{
for ( Code entry : body )
{
append( entry );
}
}
bool has_entries()
{
return rcast( AST*, ast )->has_entries();
}
AST* raw()
{
return rcast( AST*, ast );
}
AST_Body* operator->()
{
return ast;
}
operator Code()
{
return * rcast( Code*, this );
}
#pragma region Iterator
Code begin()
{
if ( ast )
return { rcast( AST*, ast)->Front };
return { nullptr };
}
Code end()
{
return { rcast(AST*, ast)->Back->Next };
}
#pragma endregion Iterator
AST_Body* ast;
};
Define_CodeType( Attributes );
Define_CodeType( Comment );
Define_CodeType( Define );
Define_CodeType( Enum );
Define_CodeType( Exec );
Define_CodeType( Extern );
Define_CodeType( Include );
Define_CodeType( Friend );
Define_CodeType( Fn );
Define_CodeType( Module );
Define_CodeType( Namespace );
Define_CodeType( Operator );
Define_CodeType( OpCast );
Define_CodeType( Pragma );
Define_CodeType( PreprocessCond );
Define_CodeType( Template );
Define_CodeType( Type );
Define_CodeType( Typedef );
Define_CodeType( Union );
Define_CodeType( Using );
Define_CodeType( Var );
struct CodeClass
{
Using_Code( CodeClass );
void add_interface( CodeType interface );
AST* raw()
{
return rcast( AST*, ast );
}
operator Code()
{
return * rcast( Code*, this );
}
AST_Class* operator->()
{
if ( ast == nullptr )
{
log_failure("Attempt to dereference a nullptr");
return nullptr;
}
return ast;
}
AST_Class* ast;
};
struct CodeParam
{
Using_Code( CodeParam );
void append( CodeParam other );
CodeParam get( s32 idx );
bool has_entries();
AST* raw()
{
return rcast( AST*, ast );
}
AST_Param* operator->()
{
if ( ast == nullptr )
{
log_failure("Attempt to dereference a nullptr!");
return nullptr;
}
return ast;
}
operator Code()
{
return { (AST*)ast };
}
#pragma region Iterator
CodeParam begin()
{
if ( ast )
return { ast };
return { nullptr };
}
CodeParam end()
{
return { (AST_Param*) rcast( AST*, ast)->Last };
}
CodeParam& operator++();
CodeParam operator*()
{
return * this;
}
#pragma endregion Iterator
AST_Param* ast;
};
struct CodeSpecifiers
{
Using_Code( CodeSpecifiers );
bool append( SpecifierT spec )
{
if ( ast == nullptr )
{
log_failure("CodeSpecifiers: Attempted to append to a null specifiers AST!");
return false;
}
if ( raw()->NumEntries == AST::ArrSpecs_Cap )
{
log_failure("CodeSpecifiers: Attempted to append over %d specifiers to a specifiers AST!", AST::ArrSpecs_Cap );
return false;
}
raw()->ArrSpecs[ raw()->NumEntries ] = spec;
raw()->NumEntries++;
return true;
}
s32 has( SpecifierT spec )
{
for ( s32 idx = 0; idx < raw()->NumEntries; idx++ )
{
if ( raw()->ArrSpecs[ raw()->NumEntries ] == spec )
return idx;
}
return -1;
}
AST* raw()
{
return rcast( AST*, ast );
}
AST_Specifiers* operator->()
{
if ( ast == nullptr )
{
log_failure("Attempt to dereference a nullptr!");
return nullptr;
}
return ast;
}
operator Code()
{
return { (AST*) ast };
}
#pragma region Iterator
SpecifierT* begin()
{
if ( ast )
return & raw()->ArrSpecs[0];
return nullptr;
}
SpecifierT* end()
{
return raw()->ArrSpecs + raw()->NumEntries;
}
#pragma endregion Iterator
AST_Specifiers* ast;
};
struct CodeStruct
{
Using_Code( CodeStruct );
void add_interface( CodeType interface );
AST* raw()
{
return rcast( AST*, ast );
}
operator Code()
{
return * rcast( Code*, this );
}
AST_Struct* operator->()
{
if ( ast == nullptr )
{
log_failure("Attempt to dereference a nullptr");
return nullptr;
}
return ast;
}
AST_Struct* ast;
};
#undef Define_CodeType
#undef Using_Code
#pragma endregion Code Types
#pragma region Filtered ASTs
/* /*
Show only relevant members of the AST for its type. Show only relevant members of the AST for its type.
AST* fields are replaced with Code types. AST* fields are replaced with Code types.
@ -658,6 +71,27 @@ struct AST_Class
}; };
static_assert( sizeof(AST_Class) == sizeof(AST), "ERROR: AST_Class is not the same size as AST"); static_assert( sizeof(AST_Class) == sizeof(AST), "ERROR: AST_Class is not the same size as AST");
struct AST_Constructor
{
union {
char _PAD_[ sizeof(SpecifierT) * AST::ArrSpecs_Cap ];
struct
{
char _PAD_PROPERTIES_ [ sizeof(AST*) * 3 ];
Code InitializerList;
CodeParam Params;
Code Body;
};
};
Code Prev;
Code Next;
Code Parent;
StringCached Name;
CodeT Type;
char _PAD_UNUSED_[ sizeof(ModuleFlag) + sizeof(u32) ];
};
static_assert( sizeof(AST_Constructor) == sizeof(AST), "ERROR: AST_Constructor is not the same size as AST");
struct AST_Define struct AST_Define
{ {
union { union {
@ -673,6 +107,27 @@ struct AST_Define
}; };
static_assert( sizeof(AST_Define) == sizeof(AST), "ERROR: AST_Define is not the same size as AST"); static_assert( sizeof(AST_Define) == sizeof(AST), "ERROR: AST_Define is not the same size as AST");
struct AST_Destructor
{
union {
char _PAD_[ sizeof(SpecifierT) * AST::ArrSpecs_Cap ];
struct
{
char _PAD_PROPERTIES_ [ sizeof(AST*) * 1 ];
CodeSpecifiers Specs;
char _PAD_PROPERTIES_2_ [ sizeof(AST*) * 2 ];
Code Body;
};
};
Code Prev;
Code Next;
Code Parent;
StringCached Name;
CodeT Type;
char _PAD_UNUSED_[ sizeof(ModuleFlag) + sizeof(u32) ];
};
static_assert( sizeof(AST_Destructor) == sizeof(AST), "ERROR: AST_Destructor is not the same size as AST");
struct AST_Enum struct AST_Enum
{ {
union { union {
@ -803,7 +258,7 @@ struct AST_Module
}; };
static_assert( sizeof(AST_Module) == sizeof(AST), "ERROR: AST_Module is not the same size as AST"); static_assert( sizeof(AST_Module) == sizeof(AST), "ERROR: AST_Module is not the same size as AST");
struct AST_Namespace struct AST_NS
{ {
union { union {
char _PAD_[ sizeof(SpecifierT) * AST::ArrSpecs_Cap ]; char _PAD_[ sizeof(SpecifierT) * AST::ArrSpecs_Cap ];
@ -820,7 +275,7 @@ struct AST_Namespace
ModuleFlag ModuleFlags; ModuleFlag ModuleFlags;
char _PAD_UNUSED_[ sizeof(u32) ]; char _PAD_UNUSED_[ sizeof(u32) ];
}; };
static_assert( sizeof(AST_Namespace) == sizeof(AST), "ERROR: AST_Namespace is not the same size as AST"); static_assert( sizeof(AST_NS) == sizeof(AST), "ERROR: AST_NS is not the same size as AST");
struct AST_Operator struct AST_Operator
{ {
@ -1014,7 +469,7 @@ struct AST_Typedef
StringCached Name; StringCached Name;
CodeT Type; CodeT Type;
ModuleFlag ModuleFlags; ModuleFlag ModuleFlags;
char _PAD_UNUSED_[ sizeof(u32) ]; b32 IsFunction;
}; };
static_assert( sizeof(AST_Typedef) == sizeof(AST), "ERROR: AST_Typedef is not the same size as AST"); static_assert( sizeof(AST_Typedef) == sizeof(AST), "ERROR: AST_Typedef is not the same size as AST");
@ -1083,6 +538,5 @@ struct AST_Var
char _PAD_UNUSED_[ sizeof(u32) ]; char _PAD_UNUSED_[ sizeof(u32) ];
}; };
static_assert( sizeof(AST_Var) == sizeof(AST), "ERROR: AST_Var is not the same size as AST"); static_assert( sizeof(AST_Var) == sizeof(AST), "ERROR: AST_Var is not the same size as AST");
#pragma endregion Filtered ASTs #pragma endregion AST Types
#pragma endregion Data Structures

View File

@ -1,164 +0,0 @@
namespace Parser
{
/*
This is a simple lexer that focuses on tokenizing only tokens relevant to the library.
It will not be capable of lexing C++ code with unsupported features.
For the sake of scanning files, it can scan preprocessor directives
__Attributes_Start is only used to indicate the start of the user_defined attribute list.
*/
#ifndef GEN_DEFINE_ATTRIBUTE_TOKENS
# define GEN_DEFINE_ATTRIBUTE_TOKENS \
Entry( API_Export, "GEN_API_Export_Code" ) \
Entry( API_Import, "GEN_API_Import_Code" )
#endif
# define Define_TokType \
Entry( Invalid, "INVALID" ) \
Entry( Access_Private, "private" ) \
Entry( Access_Protected, "protected" ) \
Entry( Access_Public, "public" ) \
Entry( Access_MemberSymbol, "." ) \
Entry( Access_StaticSymbol, "::") \
Entry( Ampersand, "&" ) \
Entry( Ampersand_DBL, "&&" ) \
Entry( Assign_Classifer, ":" ) \
Entry( Attribute_Open, "[[" ) \
Entry( Attribute_Close, "]]" ) \
Entry( BraceCurly_Open, "{" ) \
Entry( BraceCurly_Close, "}" ) \
Entry( BraceSquare_Open, "[" ) \
Entry( BraceSquare_Close, "]" ) \
Entry( Capture_Start, "(" ) \
Entry( Capture_End, ")" ) \
Entry( Comment, "__comment__" ) \
Entry( Char, "__character__" ) \
Entry( Comma, "," ) \
Entry( Decl_Class, "class" ) \
Entry( Decl_GNU_Attribute, "__attribute__" ) \
Entry( Decl_MSVC_Attribute, "__declspec" ) \
Entry( Decl_Enum, "enum" ) \
Entry( Decl_Extern_Linkage, "extern" ) \
Entry( Decl_Friend, "friend" ) \
Entry( Decl_Module, "module" ) \
Entry( Decl_Namespace, "namespace" ) \
Entry( Decl_Operator, "operator" ) \
Entry( Decl_Struct, "struct" ) \
Entry( Decl_Template, "template" ) \
Entry( Decl_Typedef, "typedef" ) \
Entry( Decl_Using, "using" ) \
Entry( Decl_Union, "union" ) \
Entry( Identifier, "__identifier__" ) \
Entry( Module_Import, "import" ) \
Entry( Module_Export, "export" ) \
Entry( Number, "__number__" ) \
Entry( Operator, "__operator__" ) \
Entry( Preprocess_Define, "define") \
Entry( Preprocess_If, "if") \
Entry( Preprocess_IfDef, "ifdef") \
Entry( Preprocess_IfNotDef, "ifndef") \
Entry( Preprocess_ElIf, "elif") \
Entry( Preprocess_Else, "else") \
Entry( Preprocess_EndIf, "endif") \
Entry( Preprocess_Include, "include" ) \
Entry( Preprocess_Pragma, "pragma") \
Entry( Preprocess_Content, "__macro_content__") \
Entry( Preprocess_Macro, "__macro__") \
Entry( Preprocess_Unsupported, "__unsupported__" ) \
Entry( Spec_Alignas, "alignas" ) \
Entry( Spec_Const, "const" ) \
Entry( Spec_Consteval, "consteval" ) \
Entry( Spec_Constexpr, "constexpr" ) \
Entry( Spec_Constinit, "constinit" ) \
Entry( Spec_Explicit, "explicit" ) \
Entry( Spec_Extern, "extern" ) \
Entry( Spec_Final, "final" ) \
Entry( Spec_Global, "global" ) \
Entry( Spec_Inline, "inline" ) \
Entry( Spec_Internal_Linkage, "internal" ) \
Entry( Spec_LocalPersist, "local_persist" ) \
Entry( Spec_Mutable, "mutable" ) \
Entry( Spec_NeverInline, "neverinline" ) \
Entry( Spec_Override, "override" ) \
Entry( Spec_Static, "static" ) \
Entry( Spec_ThreadLocal, "thread_local" ) \
Entry( Spec_Volatile, "volatile") \
Entry( Star, "*" ) \
Entry( Statement_End, ";" ) \
Entry( StaticAssert, "static_assert" ) \
Entry( String, "__string__" ) \
Entry( Type_Unsigned, "unsigned" ) \
Entry( Type_Signed, "signed" ) \
Entry( Type_Short, "short" ) \
Entry( Type_Long, "long" ) \
Entry( Type_char, "char" ) \
Entry( Type_int, "int" ) \
Entry( Type_double, "double" ) \
Entry( Type_MS_int8, "__int8" ) \
Entry( Type_MS_int16, "__int16" ) \
Entry( Type_MS_int32, "__int32" ) \
Entry( Type_MS_int64, "__int64" ) \
Entry( Type_MS_W64, "_W64" ) \
Entry( Varadic_Argument, "..." ) \
Entry( __Attributes_Start, "__attrib_start__" )
namespace ETokType
{
enum Type : u32
{
# define Entry( Name_, Str_ ) Name_,
Define_TokType
GEN_DEFINE_ATTRIBUTE_TOKENS
# undef Entry
NumTokens,
};
internal inline
Type to_type( StrC str_tok )
{
local_persist
StrC lookup[(u32)NumTokens] =
{
# define Entry( Name_, Str_ ) { sizeof(Str_), Str_ },
Define_TokType
GEN_DEFINE_ATTRIBUTE_TOKENS
# undef Entry
};
for ( u32 index = 0; index < (u32)NumTokens; index++ )
{
s32 lookup_len = lookup[index].Len - 1;
char const* lookup_str = lookup[index].Ptr;
if ( lookup_len != str_tok.Len )
continue;
if ( str_compare( str_tok.Ptr, lookup_str, lookup_len ) == 0 )
return scast(Type, index);
}
return Invalid;
}
internal inline
char const* to_str( Type type )
{
local_persist
char const* lookup[(u32)NumTokens] =
{
# define Entry( Name_, Str_ ) Str_,
Define_TokType
GEN_DEFINE_ATTRIBUTE_TOKENS
# undef Entry
};
return lookup[(u32)type];
}
# undef Define_TokType
};
using TokType = ETokType::Type;
} // Parser

View File

@ -1,396 +1,3 @@
#pragma region Inlines
void AST::append( AST* other )
{
if ( other->Parent )
other = other->duplicate();
other->Parent = this;
if ( Front == nullptr )
{
Front = other;
Back = other;
NumEntries++;
return;
}
AST*
Current = Back;
Current->Next = other;
other->Prev = Current;
Back = other;
NumEntries++;
}
char const* AST::debug_str()
{
if ( Parent )
{
char const* fmt = stringize(
\nType : %s
\nParent : %s %s
\nName : %s
);
// 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.
return str_fmt_buf( fmt
, type_str()
, Parent->Name
, Parent->type_str()
, Name ? Name : ""
);
}
char const* fmt = stringize(
\nType : %s
\nName : %s
);
// 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.
return str_fmt_buf( fmt
, type_str()
, Name ? Name : ""
);
}
Code& AST::entry( u32 idx )
{
AST** current = & Front;
while ( idx >= 0 && current != nullptr )
{
if ( idx == 0 )
return * rcast( Code*, current);
current = & ( * current )->Next;
idx--;
}
return * rcast( Code*, current);
}
bool AST::has_entries()
{
return NumEntries;
}
char const* AST::type_str()
{
return ECode::to_str( Type );
}
AST::operator Code()
{
return { this };
}
Code& Code::operator ++()
{
if ( ast )
ast = ast->Next;
return *this;
}
#pragma region AST & Code Gen Common
#define Define_CodeImpl( Typename ) \
char const* Typename::debug_str() \
{ \
if ( ast == nullptr ) \
return "Code::debug_str: AST is null!"; \
\
return rcast(AST*, ast)->debug_str(); \
} \
Code Typename::duplicate() \
{ \
if ( ast == nullptr ) \
{ \
log_failure("Code::duplicate: Cannot duplicate code, AST is null!"); \
return Code::Invalid; \
} \
\
return { rcast(AST*, ast)->duplicate() }; \
} \
bool Typename::is_equal( Code other ) \
{ \
if ( ast == nullptr || other.ast == nullptr ) \
{ \
log_failure("Code::is_equal: Cannot compare code, AST is null!"); \
return false; \
} \
\
return rcast(AST*, ast)->is_equal( other.ast ); \
} \
bool Typename::is_valid() \
{ \
return (AST*) ast != nullptr && rcast( AST*, ast)->Type != CodeT::Invalid; \
} \
void Typename::set_global() \
{ \
if ( ast == nullptr ) \
{ \
log_failure("Code::set_global: Cannot set code as global, AST is null!"); \
return; \
} \
\
rcast(AST*, ast)->Parent = Code::Global.ast; \
} \
String Typename::to_string() \
{ \
if ( ast == nullptr ) \
{ \
log_failure("Code::to_string: Cannot convert code to string, AST is null!"); \
return { nullptr }; \
} \
\
return rcast(AST*, ast)->to_string(); \
} \
Typename& Typename::operator =( Code other ) \
{ \
if ( other.ast && other->Parent ) \
{ \
ast = rcast( decltype(ast), other.ast->duplicate() ); \
rcast( AST*, ast)->Parent = nullptr; \
} \
\
ast = rcast( decltype(ast), other.ast ); \
return *this; \
} \
bool Typename::operator ==( Code other ) \
{ \
return (AST*) ast == other.ast; \
} \
bool Typename::operator !=( Code other ) \
{ \
return (AST*) ast != other.ast; \
}
Define_CodeImpl( Code );
Define_CodeImpl( CodeBody );
Define_CodeImpl( CodeAttributes );
Define_CodeImpl( CodeComment );
Define_CodeImpl( CodeClass );
Define_CodeImpl( CodeDefine );
Define_CodeImpl( CodeEnum );
Define_CodeImpl( CodeExec );
Define_CodeImpl( CodeExtern );
Define_CodeImpl( CodeInclude );
Define_CodeImpl( CodeFriend );
Define_CodeImpl( CodeFn );
Define_CodeImpl( CodeModule );
Define_CodeImpl( CodeNamespace );
Define_CodeImpl( CodeOperator );
Define_CodeImpl( CodeOpCast );
Define_CodeImpl( CodeParam );
Define_CodeImpl( CodePragma );
Define_CodeImpl( CodePreprocessCond );
Define_CodeImpl( CodeSpecifiers );
Define_CodeImpl( CodeStruct );
Define_CodeImpl( CodeTemplate );
Define_CodeImpl( CodeType );
Define_CodeImpl( CodeTypedef );
Define_CodeImpl( CodeUnion );
Define_CodeImpl( CodeUsing );
Define_CodeImpl( CodeVar );
#undef Define_CodeImpl
#define Define_AST_Cast( typename ) \
AST::operator Code ## typename() \
{ \
return { rcast( AST_ ## typename*, this ) }; \
}
Define_AST_Cast( Body );
Define_AST_Cast( Attributes );
Define_AST_Cast( Comment );
Define_AST_Cast( Class );
Define_AST_Cast( Define );
Define_AST_Cast( Enum );
Define_AST_Cast( Exec );
Define_AST_Cast( Extern );
Define_AST_Cast( Include );
Define_AST_Cast( Friend );
Define_AST_Cast( Fn );
Define_AST_Cast( Module );
Define_AST_Cast( Namespace );
Define_AST_Cast( Operator );
Define_AST_Cast( OpCast );
Define_AST_Cast( Param );
Define_AST_Cast( Pragma );
Define_AST_Cast( PreprocessCond );
Define_AST_Cast( Struct );
Define_AST_Cast( Specifiers );
Define_AST_Cast( Template );
Define_AST_Cast( Type );
Define_AST_Cast( Typedef );
Define_AST_Cast( Union );
Define_AST_Cast( Using );
Define_AST_Cast( Var );
#undef Define_AST_Cast
#define Define_CodeCast( type ) \
Code::operator Code ## type() const \
{ \
return { (AST_ ## type*) ast }; \
}
Define_CodeCast( Attributes );
Define_CodeCast( Comment );
Define_CodeCast( Class );
Define_CodeCast( Define );
Define_CodeCast( Exec );
Define_CodeCast( Enum );
Define_CodeCast( Extern );
Define_CodeCast( Include );
Define_CodeCast( Friend );
Define_CodeCast( Fn );
Define_CodeCast( Module );
Define_CodeCast( Namespace );
Define_CodeCast( Operator );
Define_CodeCast( OpCast );
Define_CodeCast( Param );
Define_CodeCast( Pragma );
Define_CodeCast( PreprocessCond );
Define_CodeCast( Specifiers );
Define_CodeCast( Struct );
Define_CodeCast( Template );
Define_CodeCast( Type );
Define_CodeCast( Typedef );
Define_CodeCast( Union );
Define_CodeCast( Using );
Define_CodeCast( Var );
Define_CodeCast( Body);
#undef Define_CodeCast
#pragma endregion AST & Code Gen Common
void CodeClass::add_interface( CodeType type )
{
if ( ! ast->Next )
{
ast->Next = type;
ast->Last = ast->Next;
return;
}
ast->Next->Next = type;
ast->Last = ast->Next->Next;
}
void CodeParam::append( CodeParam other )
{
AST* self = (AST*) ast;
AST* entry = (AST*) other.ast;
if ( entry->Parent )
entry = entry->duplicate();
entry->Parent = self;
if ( self->Last == nullptr )
{
self->Last = entry;
self->Next = entry;
self->NumEntries++;
return;
}
self->Last->Next = entry;
self->Last = entry;
self->NumEntries++;
}
CodeParam CodeParam::get( s32 idx )
{
CodeParam param = *this;
do
{
if ( ! ++ param )
return { nullptr };
return { (AST_Param*) param.raw()->Next };
}
while ( --idx );
return { nullptr };
}
bool CodeParam::has_entries()
{
return ast->NumEntries > 0;
}
CodeParam& CodeParam::operator ++()
{
ast = ast->Next.ast;
return * this;
}
void CodeStruct::add_interface( CodeType type )
{
if ( ! ast->Next )
{
ast->Next = type;
ast->Last = ast->Next;
}
ast->Next->Next = type;
ast->Last = ast->Next->Next;
}
CodeBody def_body( CodeT type )
{
switch ( type )
{
using namespace ECode;
case Class_Body:
case Enum_Body:
case Export_Body:
case Extern_Linkage:
case Function_Body:
case Global_Body:
case Namespace_Body:
case Struct_Body:
case Union_Body:
break;
default:
log_failure( "def_body: Invalid type %s", (char const*)ECode::to_str(type) );
return (CodeBody)Code::Invalid;
}
Code
result = make_code();
result->Type = type;
return (CodeBody)result;
}
//! Do not use directly. Use the token_fmt macro instead.
// Takes a format string (char const*) and a list of tokens (StrC) and returns a StrC of the formatted string.
StrC token_fmt_impl( sw num, ... )
{
local_persist thread_local
char buf[GEN_PRINTF_MAXLEN] = { 0 };
mem_set( buf, 0, GEN_PRINTF_MAXLEN );
va_list va;
va_start(va, num );
sw result = token_fmt_va(buf, GEN_PRINTF_MAXLEN, num, va);
va_end(va);
return { result, buf };
}
#pragma endregion Inlines
#pragma region Constants #pragma region Constants
#ifndef GEN_GLOBAL_BUCKET_SIZE #ifndef GEN_GLOBAL_BUCKET_SIZE
@ -450,6 +57,9 @@ extern CodeAttributes attrib_api_import;
extern Code module_global_fragment; extern Code module_global_fragment;
extern Code module_private_fragment; extern Code module_private_fragment;
// Exposed, but this is really used for parsing.
extern Code fmt_newline;
extern CodePragma pragma_once; extern CodePragma pragma_once;
extern CodeParam param_varadic; extern CodeParam param_varadic;
@ -471,6 +81,7 @@ extern CodeSpecifiers spec_mutable;
extern CodeSpecifiers spec_neverinline; extern CodeSpecifiers spec_neverinline;
extern CodeSpecifiers spec_override; extern CodeSpecifiers spec_override;
extern CodeSpecifiers spec_ptr; extern CodeSpecifiers spec_ptr;
extern CodeSpecifiers spec_pure;
extern CodeSpecifiers spec_ref; extern CodeSpecifiers spec_ref;
extern CodeSpecifiers spec_register; extern CodeSpecifiers spec_register;
extern CodeSpecifiers spec_rvalue; extern CodeSpecifiers spec_rvalue;
@ -541,6 +152,7 @@ extern CodeType t_typename;
// Global allocator used for data with process lifetime. // Global allocator used for data with process lifetime.
extern AllocatorInfo GlobalAllocator; extern AllocatorInfo GlobalAllocator;
extern Array< Arena > Global_AllocatorBuckets; extern Array< Arena > Global_AllocatorBuckets;
extern Array< Pool > CodePools; extern Array< Pool > CodePools;
extern Array< Arena > StringArenas; extern Array< Arena > StringArenas;
@ -556,3 +168,4 @@ extern CodeType t_typename;
extern AllocatorInfo Allocator_TypeTable; extern AllocatorInfo Allocator_TypeTable;
#endif #endif

View File

@ -15,3 +15,12 @@
#ifndef GEN_ROLL_OWN_DEPENDENCIES #ifndef GEN_ROLL_OWN_DEPENDENCIES
# include "gen.dep.hpp" # include "gen.dep.hpp"
#endif #endif
#ifdef GEN_DONT_USE_NAMESPACE
# define GEN_NS_BEGIN
# define GEN_NS_END
#else
# define GEN_NS_BEGIN namespace gen {
# define GEN_NS_END }
#endif

View File

@ -0,0 +1,214 @@
void AST::append( AST* other )
{
if ( other->Parent )
other = other->duplicate();
other->Parent = this;
if ( Front == nullptr )
{
Front = other;
Back = other;
NumEntries++;
return;
}
AST*
Current = Back;
Current->Next = other;
other->Prev = Current;
Back = other;
NumEntries++;
}
char const* AST::debug_str()
{
if ( Parent )
{
char const* fmt = stringize(
\nType : %s
\nParent : %s %s
\nName : %s
);
// 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.
return str_fmt_buf( fmt
, type_str()
, Parent->Name
, Parent->type_str()
, Name ? Name : ""
);
}
char const* fmt = stringize(
\nType : %s
\nName : %s
);
// 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.
return str_fmt_buf( fmt
, type_str()
, Name ? Name : ""
);
}
Code& AST::entry( u32 idx )
{
AST** current = & Front;
while ( idx >= 0 && current != nullptr )
{
if ( idx == 0 )
return * rcast( Code*, current);
current = & ( * current )->Next;
idx--;
}
return * rcast( Code*, current);
}
bool AST::has_entries()
{
return NumEntries;
}
char const* AST::type_str()
{
return ECode::to_str( Type );
}
AST::operator Code()
{
return { this };
}
Code& Code::operator ++()
{
if ( ast )
ast = ast->Next;
return *this;
}
void CodeClass::add_interface( CodeType type )
{
if ( ! ast->Next )
{
ast->Next = type;
ast->Last = ast->Next;
return;
}
ast->Next->Next = type;
ast->Last = ast->Next->Next;
}
void CodeParam::append( CodeParam other )
{
AST* self = (AST*) ast;
AST* entry = (AST*) other.ast;
if ( entry->Parent )
entry = entry->duplicate();
entry->Parent = self;
if ( self->Last == nullptr )
{
self->Last = entry;
self->Next = entry;
self->NumEntries++;
return;
}
self->Last->Next = entry;
self->Last = entry;
self->NumEntries++;
}
CodeParam CodeParam::get( s32 idx )
{
CodeParam param = *this;
do
{
if ( ! ++ param )
return { nullptr };
return { (AST_Param*) param.raw()->Next };
}
while ( --idx );
return { nullptr };
}
bool CodeParam::has_entries()
{
return ast->NumEntries > 0;
}
CodeParam& CodeParam::operator ++()
{
ast = ast->Next.ast;
return * this;
}
void CodeStruct::add_interface( CodeType type )
{
if ( ! ast->Next )
{
ast->Next = type;
ast->Last = ast->Next;
}
ast->Next->Next = type;
ast->Last = ast->Next->Next;
}
CodeBody def_body( CodeT type )
{
switch ( type )
{
using namespace ECode;
case Class_Body:
case Enum_Body:
case Export_Body:
case Extern_Linkage:
case Function_Body:
case Global_Body:
case Namespace_Body:
case Struct_Body:
case Union_Body:
break;
default:
log_failure( "def_body: Invalid type %s", (char const*)ECode::to_str(type) );
return (CodeBody)Code::Invalid;
}
Code
result = make_code();
result->Type = type;
return (CodeBody)result;
}
StrC token_fmt_impl( sw num, ... )
{
local_persist thread_local
char buf[GEN_PRINTF_MAXLEN] = { 0 };
mem_set( buf, 0, GEN_PRINTF_MAXLEN );
va_list va;
va_start(va, num );
sw result = token_fmt_va(buf, GEN_PRINTF_MAXLEN, num, va);
va_end(va);
return { result, buf };
}

View File

@ -112,6 +112,10 @@ void define_constants()
module_private_fragment->Content = module_private_fragment->Name; module_private_fragment->Content = module_private_fragment->Name;
module_private_fragment.set_global(); module_private_fragment.set_global();
fmt_newline = make_code();
fmt_newline->Type = ECode::NewLine;
fmt_newline.set_global();
pragma_once = (CodePragma) make_code(); pragma_once = (CodePragma) make_code();
pragma_once->Type = ECode::Untyped; pragma_once->Type = ECode::Untyped;
pragma_once->Name = get_cached_string( txt_StrC("once") ); pragma_once->Name = get_cached_string( txt_StrC("once") );
@ -193,6 +197,7 @@ void define_constants()
def_constant_spec( neverinline, ESpecifier::NeverInline ); def_constant_spec( neverinline, ESpecifier::NeverInline );
def_constant_spec( override, ESpecifier::Override ); def_constant_spec( override, ESpecifier::Override );
def_constant_spec( ptr, ESpecifier::Ptr ); def_constant_spec( ptr, ESpecifier::Ptr );
def_constant_spec( pure, ESpecifier::Pure )
def_constant_spec( ref, ESpecifier::Ref ); def_constant_spec( ref, ESpecifier::Ref );
def_constant_spec( register, ESpecifier::Register ); def_constant_spec( register, ESpecifier::Register );
def_constant_spec( rvalue, ESpecifier::RValue ); def_constant_spec( rvalue, ESpecifier::RValue );
@ -383,9 +388,7 @@ StringCached get_cached_string( StrC str )
return result; return result;
} }
/* // Used internally to retireve a Code object form the CodePool.
Used internally to retireve a Code object form the CodePool.
*/
Code make_code() Code make_code()
{ {
Pool* allocator = & CodePools.back(); Pool* allocator = & CodePools.back();
@ -440,3 +443,4 @@ void set_allocator_string_table( AllocatorInfo allocator )
{ {
Allocator_StringArena = allocator; Allocator_StringArena = allocator;
} }

View File

@ -45,8 +45,12 @@ CodeClass def_class( StrC name
, ModuleFlag mflags = ModuleFlag::None , ModuleFlag mflags = ModuleFlag::None
, CodeType* interfaces = nullptr, s32 num_interfaces = 0 ); , CodeType* interfaces = nullptr, s32 num_interfaces = 0 );
CodeConstructor def_constructor( CodeParam params = NoCode, Code initializer_list = NoCode, Code body = NoCode );
CodeDefine def_define( StrC name, StrC content ); CodeDefine def_define( StrC name, StrC content );
CodeDestructor def_destructor( Code body = NoCode, CodeSpecifiers specifiers = NoCode );
CodeEnum def_enum( StrC name CodeEnum def_enum( StrC name
, Code body = NoCode, CodeType type = NoCode , Code body = NoCode, CodeType type = NoCode
, EnumT specifier = EnumRegular, CodeAttributes attributes = NoCode , EnumT specifier = EnumRegular, CodeAttributes attributes = NoCode
@ -63,7 +67,7 @@ CodeFn def_function( StrC name
CodeInclude def_include ( StrC content ); CodeInclude def_include ( StrC content );
CodeModule def_module ( StrC name, ModuleFlag mflags = ModuleFlag::None ); CodeModule def_module ( StrC name, ModuleFlag mflags = ModuleFlag::None );
CodeNamespace def_namespace( StrC name, Code body, ModuleFlag mflags = ModuleFlag::None ); CodeNS def_namespace( StrC name, Code body, ModuleFlag mflags = ModuleFlag::None );
CodeOperator def_operator( OperatorT op, StrC nspace CodeOperator def_operator( OperatorT op, StrC nspace
, CodeParam params = NoCode, CodeType ret_type = NoCode, Code body = NoCode , CodeParam params = NoCode, CodeType ret_type = NoCode, Code body = NoCode
@ -136,29 +140,32 @@ CodeBody def_union_body ( s32 num, Code* codes );
#pragma region Parsing #pragma region Parsing
CodeClass parse_class ( StrC class_def ); CodeClass parse_class ( StrC class_def );
CodeEnum parse_enum ( StrC enum_def ); CodeConstructor parse_constructor ( StrC constructor_def );
CodeBody parse_export_body ( StrC export_def ); CodeDestructor parse_destructor ( StrC destructor_def );
CodeExtern parse_extern_link ( StrC exten_link_def); CodeEnum parse_enum ( StrC enum_def );
CodeFriend parse_friend ( StrC friend_def ); CodeBody parse_export_body ( StrC export_def );
CodeFn parse_function ( StrC fn_def ); CodeExtern parse_extern_link ( StrC exten_link_def );
CodeBody parse_global_body ( StrC body_def ); CodeFriend parse_friend ( StrC friend_def );
CodeNamespace parse_namespace ( StrC namespace_def ); CodeFn parse_function ( StrC fn_def );
CodeOperator parse_operator ( StrC operator_def ); CodeBody parse_global_body ( StrC body_def );
CodeOpCast parse_operator_cast( StrC operator_def ); CodeNS parse_namespace ( StrC namespace_def );
CodeStruct parse_struct ( StrC struct_def ); CodeOperator parse_operator ( StrC operator_def );
CodeTemplate parse_template ( StrC template_def ); CodeOpCast parse_operator_cast( StrC operator_def );
CodeType parse_type ( StrC type_def ); CodeStruct parse_struct ( StrC struct_def );
CodeTypedef parse_typedef ( StrC typedef_def ); CodeTemplate parse_template ( StrC template_def );
CodeUnion parse_union ( StrC union_def ); CodeType parse_type ( StrC type_def );
CodeUsing parse_using ( StrC using_def ); CodeTypedef parse_typedef ( StrC typedef_def );
CodeVar parse_variable ( StrC var_def ); CodeUnion parse_union ( StrC union_def );
CodeUsing parse_using ( StrC using_def );
CodeVar parse_variable ( StrC var_def );
#pragma endregion Parsing #pragma endregion Parsing
#pragma region Untyped text #pragma region Untyped text
sw token_fmt_va( char* buf, uw buf_size, s32 num_tokens, va_list va ); sw token_fmt_va( char* buf, uw buf_size, s32 num_tokens, va_list va );
//! Do not use directly. Use the token_fmt macro instead.
StrC token_fmt_impl( sw, ... ); StrC token_fmt_impl( sw, ... );
Code untyped_str ( StrC content); Code untyped_str ( StrC content);
@ -168,3 +175,4 @@ Code untyped_token_fmt( char const* fmt, s32 num_tokens, ... );
#pragma endregion Untyped text #pragma endregion Untyped text
#pragma endregion Gen Interface #pragma endregion Gen Interface

File diff suppressed because it is too large Load Diff

View File

@ -1,3 +1,5 @@
#pragma region Upfront
enum class OpValidateResult : u32 enum class OpValidateResult : u32
{ {
Fail, Fail,
@ -375,7 +377,7 @@ OpValidateResult operator__validate( OperatorT op, CodeParam params_code, CodeTy
/* /*
The implementaiton of the upfront constructors involves bascially doing three things: The implementaiton of the upfront constructors involves doing three things:
* Validate the arguments given to construct the intended type of AST is valid. * Validate the arguments given to construct the intended type of AST is valid.
* Construct said AST type. * Construct said AST type.
* Lock the AST (set to readonly) and return the valid object. * Lock the AST (set to readonly) and return the valid object.
@ -420,6 +422,53 @@ CodeComment def_comment( StrC content )
return (CodeComment) result; return (CodeComment) result;
} }
CodeConstructor def_constructor( CodeParam params, Code initializer_list, Code body )
{
using namespace ECode;
if ( params && params->Type != Parameters )
{
log_failure("gen::def_constructor: params must be of Parameters type - %s", params.debug_str());
return CodeInvalid;
}
CodeConstructor
result = (CodeConstructor) make_code();
if ( params )
{
result->Params = params;
}
if ( initializer_list )
{
result->InitializerList = initializer_list;
}
if ( body )
{
switch ( body->Type )
{
case Function_Body:
case Untyped:
break;
default:
log_failure("gen::def_constructor: body must be either of Function_Body or Untyped type - %s", body.debug_str());
return CodeInvalid;
}
result->Type = Constructor;
result->Body = body;
}
else
{
result->Type = Constructor_Fwd;
}
return result;
}
CodeClass def_class( StrC name CodeClass def_class( StrC name
, Code body , Code body
, CodeType parent, AccessSpec parent_access , CodeType parent, AccessSpec parent_access
@ -503,12 +552,52 @@ CodeDefine def_define( StrC name, StrC content )
CodeDefine CodeDefine
result = (CodeDefine) make_code(); result = (CodeDefine) make_code();
result->Type = Preprocess_Define;
result->Name = get_cached_string( name ); result->Name = get_cached_string( name );
result->Content = get_cached_string( content ); result->Content = get_cached_string( content );
return result; return result;
} }
CodeDestructor def_destructor( Code body, CodeSpecifiers specifiers )
{
using namespace ECode;
if ( specifiers && specifiers->Type != Specifiers )
{
log_failure( "gen::def_destructor: specifiers was not a 'Specifiers' type: %s", specifiers.debug_str() );
return CodeInvalid;
}
CodeDestructor result = (CodeDestructor) make_code();
if ( specifiers )
result->Specs = specifiers;
if ( body )
{
switch ( body->Type )
{
case Function_Body:
case Untyped:
break;
default:
log_failure("gen::def_destructor: body must be either of Function_Body or Untyped type - %s", body.debug_str());
return CodeInvalid;
}
result->Type = Destructor;
result->Body = body;
}
else
{
result->Type = Destructor_Fwd;
}
return result;
}
CodeEnum def_enum( StrC name CodeEnum def_enum( StrC name
, Code body, CodeType type , Code body, CodeType type
, EnumT specifier, CodeAttributes attributes , EnumT specifier, CodeAttributes attributes
@ -760,7 +849,7 @@ CodeModule def_module( StrC name, ModuleFlag mflags )
return (CodeModule) result; return (CodeModule) result;
} }
CodeNamespace def_namespace( StrC name, Code body, ModuleFlag mflags ) CodeNS def_namespace( StrC name, Code body, ModuleFlag mflags )
{ {
using namespace ECode; using namespace ECode;
@ -773,8 +862,8 @@ CodeNamespace def_namespace( StrC name, Code body, ModuleFlag mflags )
return CodeInvalid; return CodeInvalid;
} }
CodeNamespace CodeNS
result = (CodeNamespace) make_code(); result = (CodeNS) make_code();
result->Type = Namespace; result->Type = Namespace;
result->Name = get_cached_string( name ); result->Name = get_cached_string( name );
result->ModuleFlags = mflags; result->ModuleFlags = mflags;
@ -809,8 +898,13 @@ CodeOperator def_operator( OperatorT op, StrC nspace
return CodeInvalid; return CodeInvalid;
} }
char const* name = str_fmt_buf( "%.*soperator %s", nspace.Len, nspace.Ptr, to_str(op) ); char const* name = nullptr;
StrC op_str = to_str( op );
if ( nspace.Len > 0 )
name = str_fmt_buf( "%.*soperator %.*s", nspace.Len, nspace.Ptr, op_str.Len, op_str.Ptr );
else
name = str_fmt_buf( "operator %.*s", op_str.Len, op_str.Ptr );
CodeOperator CodeOperator
result = (CodeOperator) make_code(); result = (CodeOperator) make_code();
result->Name = get_cached_string( { str_len(name), name } ); result->Name = get_cached_string( { str_len(name), name } );
@ -1127,7 +1221,6 @@ CodeTypedef def_typedef( StrC name, Code type, CodeAttributes attributes, Module
{ {
using namespace ECode; using namespace ECode;
name_check( def_typedef, name );
null_check( def_typedef, type ); null_check( def_typedef, type );
switch ( type->Type ) switch ( type->Type )
@ -1166,12 +1259,28 @@ CodeTypedef def_typedef( StrC name, Code type, CodeAttributes attributes, Module
CodeTypedef CodeTypedef
result = (CodeTypedef) make_code(); result = (CodeTypedef) make_code();
result->Name = get_cached_string( name );
result->Type = ECode::Typedef; result->Type = ECode::Typedef;
result->ModuleFlags = mflags; result->ModuleFlags = mflags;
result->UnderlyingType = type; result->UnderlyingType = type;
if ( name.Len <= 0 )
{
if (type->Type != Untyped)
{
log_failure( "gen::def_typedef: name was empty and type was not untyped (indicating its a function typedef) - %s", type.debug_str() );
return CodeInvalid;
}
result->Name = get_cached_string( type->Name );
result->IsFunction = true;
}
else
{
result->Name = get_cached_string( name );
result->IsFunction = false;
}
return result; return result;
} }
@ -1306,27 +1415,6 @@ CodeVar def_variable( CodeType type, StrC name, Code value
return result; return result;
} }
/*
Body related functions typically follow the same implementation pattern.
Opted to use inline helper macros to get the implementaiton done.
The implementation pattern is as follows:
* Validate a valid parameter num was provided, or code array
def_body_start or def_body_code_array_start( <name of function >)
* Begin the code entry do-while loop, make sure each entry is valid processing its type in the switc
def_body_code_validation_start( <name of function> )
* Define the switch case statements between the macros.
* Add the code entry, finish the closing implemenation for the do-while loop.
def_body_code_validation_end( <name of function> )
* Lock the body AST and return it.
If a function's implementation deviates from the macros then its just writen it out.
*/
#pragma region Helper Macros for def_**_body functions #pragma region Helper Macros for def_**_body functions
#define def_body_start( Name_ ) \ #define def_body_start( Name_ ) \
using namespace ECode; \ using namespace ECode; \
@ -1352,61 +1440,43 @@ if ( codes == nullptr ) \
return CodeInvalid; \ return CodeInvalid; \
} }
#define def_body_code_validation_start( Name_ ) \
do \
{ \
Code_POD pod = va_arg(va, Code_POD); \
Code entry = pcast(Code, pod); \
\
if ( ! entry ) \
{ \
log_failure("gen::" stringize(Name_) ": Provided an null entry"); \
return CodeInvalid; \
} \
\
switch ( entry->Type ) \
{
#define def_body_code_array_validation_start( Name_ ) \
do \
{ \
Code entry = *codes; codes++; \
\
if ( ! entry ) \
{ \
log_failure("gen::" stringize(Name_) ": Provided an null entry"); \
return CodeInvalid; \
} \
\
switch ( entry->Type ) \
{
#define def_body_code_validation_end( Name_ ) \
log_failure("gen::" stringize(Name_) ": Entry type is not allowed: %s", entry.debug_str() ); \
return CodeInvalid; \
\
default: \
break; \
} \
\
result.append( entry ); \
} \
while ( num--, num > 0 )
#pragma endregion Helper Macros for def_**_body functions #pragma endregion Helper Macros for def_**_body functions
CodeBody def_class_body( s32 num, ... ) CodeBody def_class_body( s32 num, ... )
{ {
def_body_start( def_class_body ); def_body_start( def_class_body );
CodeBody CodeBody result = ( CodeBody )make_code();
result = (CodeBody) make_code(); result->Type = Class_Body;
result->Type = Class_Body;
va_list va; va_list va;
va_start(va, num); va_start( va, num );
def_body_code_validation_start( def_class_body ); do
GEN_AST_BODY_CLASS_UNALLOWED_TYPES {
def_body_code_validation_end( def_class_body ); Code_POD pod = va_arg(va, Code_POD);
Code entry = pcast(Code, pod);
if (!entry)
{
log_failure("gen::"
"def_class_body"
": Provided an null entry");
return CodeInvalid;
}
switch (entry->Type)
{
GEN_AST_BODY_CLASS_UNALLOWED_TYPES
log_failure("gen::" "def_class_body" ": Entry type is not allowed: %s", entry.debug_str());
return CodeInvalid;
default:
break;
}
result.append(entry);
}
while (num--, num > 0);
va_end(va); va_end(va);
return result; return result;
@ -1420,9 +1490,30 @@ CodeBody def_class_body( s32 num, Code* codes )
result = (CodeBody) make_code(); result = (CodeBody) make_code();
result->Type = Function_Body; result->Type = Function_Body;
def_body_code_array_validation_start( def_class_body ); do
GEN_AST_BODY_CLASS_UNALLOWED_TYPES {
def_body_code_validation_end( def_class_body ); Code entry = *codes;
codes++;
if (!entry)
{
log_failure("gen::" "def_class_body" ": Provided an null entry");
return CodeInvalid;
}
switch (entry->Type)
{
GEN_AST_BODY_CLASS_UNALLOWED_TYPES
log_failure("gen::" "def_class_body" ": Entry type is not allowed: %s", entry.debug_str());
return CodeInvalid;
default:
break;
}
result.append(entry);
}
while (num--, num > 0);
return result; return result;
} }
@ -1450,7 +1541,7 @@ CodeBody def_enum_body( s32 num, ... )
if ( entry->Type != Untyped && entry->Type != Comment ) if ( entry->Type != Untyped && entry->Type != Comment )
{ {
log_failure("gen::def_enum_body: Entry type is not allowed - %s. Must be of untyped or comment type.", entry.debug_str() ); \ log_failure("gen::def_enum_body: Entry type is not allowed - %s. Must be of untyped or comment type.", entry.debug_str() );
return CodeInvalid; return CodeInvalid;
} }
@ -1482,7 +1573,7 @@ CodeBody def_enum_body( s32 num, Code* codes )
if ( entry->Type != Untyped && entry->Type != Comment ) if ( entry->Type != Untyped && entry->Type != Comment )
{ {
log_failure("gen::def_enum_body: Entry type is not allowed: %s", entry.debug_str() ); \ log_failure("gen::def_enum_body: Entry type is not allowed: %s", entry.debug_str() );
return CodeInvalid; return CodeInvalid;
} }
@ -1503,9 +1594,30 @@ CodeBody def_export_body( s32 num, ... )
va_list va; va_list va;
va_start(va, num); va_start(va, num);
def_body_code_validation_start( def_export_body ); do
GEN_AST_BODY_EXPORT_UNALLOWED_TYPES {
def_body_code_validation_end( def_export_body ); Code_POD pod = va_arg(va, Code_POD);
Code entry = pcast(Code, pod);
if (!entry)
{
log_failure("gen::" "def_export_body" ": Provided an null entry");
return CodeInvalid;
}
switch (entry->Type)
{
GEN_AST_BODY_EXPORT_UNALLOWED_TYPES
log_failure("gen::" "def_export_body" ": Entry type is not allowed: %s", entry.debug_str());
return CodeInvalid;
default:
break;
}
result.append(entry);
}
while (num--, num > 0);
va_end(va); va_end(va);
return result; return result;
@ -1519,9 +1631,30 @@ CodeBody def_export_body( s32 num, Code* codes )
result = (CodeBody) make_code(); result = (CodeBody) make_code();
result->Type = Export_Body; result->Type = Export_Body;
def_body_code_array_validation_start( def_export_body ); do
GEN_AST_BODY_EXPORT_UNALLOWED_TYPES {
def_body_code_validation_end( def_export_body ); Code entry = *codes;
codes++;
if (!entry)
{
log_failure("gen::" "def_export_body" ": Provided an null entry");
return CodeInvalid;
}
switch (entry->Type)
{
GEN_AST_BODY_EXPORT_UNALLOWED_TYPES
log_failure("gen::" "def_export_body" ": Entry type is not allowed: %s", entry.debug_str());
return CodeInvalid;
default:
break;
}
result.append(entry);
}
while (num--, num > 0);
return result; return result;
} }
@ -1536,9 +1669,30 @@ CodeBody def_extern_link_body( s32 num, ... )
va_list va; va_list va;
va_start(va, num); va_start(va, num);
def_body_code_validation_start( def_extern_linkage_body ); do
GEN_AST_BODY_EXTERN_LINKAGE_UNALLOWED_TYPES {
def_body_code_validation_end( def_extern_linkage_body ); Code_POD pod = va_arg(va, Code_POD);
Code entry = pcast(Code, pod);
if (!entry)
{
log_failure("gen::" "def_extern_linkage_body" ": Provided an null entry");
return CodeInvalid;
}
switch (entry->Type)
{
GEN_AST_BODY_EXTERN_LINKAGE_UNALLOWED_TYPES
log_failure("gen::" "def_extern_linkage_body" ": Entry type is not allowed: %s", entry.debug_str());
return CodeInvalid;
default:
break;
}
result.append(entry);
}
while (num--, num > 0);
va_end(va); va_end(va);
return result; return result;
@ -1552,9 +1706,31 @@ CodeBody def_extern_link_body( s32 num, Code* codes )
result = (CodeBody) make_code(); result = (CodeBody) make_code();
result->Type = Extern_Linkage_Body; result->Type = Extern_Linkage_Body;
def_body_code_array_validation_start( def_extern_linkage_body ); do
GEN_AST_BODY_EXTERN_LINKAGE_UNALLOWED_TYPES {
def_body_code_validation_end( def_extern_linkage_body ); Code entry = *codes;
codes++;
if (!entry)
{
log_failure("gen::" "def_extern_linkage_body" ": Provided an null entry");
return CodeInvalid;
}
switch (entry->Type)
{
GEN_AST_BODY_EXTERN_LINKAGE_UNALLOWED_TYPES
log_failure("gen::" "def_extern_linkage_body" ": Entry type is not allowed: %s", entry.debug_str());
return CodeInvalid;
default:
break;
}
result.append(entry);
}
while (num--, num > 0);
return result; return result;
} }
@ -1569,9 +1745,31 @@ CodeBody def_function_body( s32 num, ... )
va_list va; va_list va;
va_start(va, num); va_start(va, num);
def_body_code_validation_start( def_function_body ); do
GEN_AST_BODY_FUNCTION_UNALLOWED_TYPES {
def_body_code_validation_end( def_function_body ); Code_POD pod = va_arg(va, Code_POD);
Code entry = pcast(Code, pod);
if (!entry)
{
log_failure("gen::" stringize(def_function_body) ": Provided an null entry");
return CodeInvalid;
}
switch (entry->Type)
{
GEN_AST_BODY_FUNCTION_UNALLOWED_TYPES
log_failure("gen::" stringize(def_function_body) ": Entry type is not allowed: %s", entry.debug_str());
return CodeInvalid;
default:
break;
}
result.append(entry);
}
while (num--, num > 0);
va_end(va); va_end(va);
return result; return result;
@ -1585,9 +1783,29 @@ CodeBody def_function_body( s32 num, Code* codes )
result = (CodeBody) make_code(); result = (CodeBody) make_code();
result->Type = Function_Body; result->Type = Function_Body;
def_body_code_array_validation_start( def_function_body ); do
GEN_AST_BODY_FUNCTION_UNALLOWED_TYPES {
def_body_code_validation_end( def_function_body ); Code entry = *codes;
codes++;
if (!entry)
{
log_failure("gen::" "def_function_body" ": Provided an null entry");
return CodeInvalid;
}
switch (entry->Type)
{
GEN_AST_BODY_FUNCTION_UNALLOWED_TYPES
log_failure("gen::" "def_function_body" ": Entry type is not allowed: %s", entry.debug_str());
return CodeInvalid;
default:
break;
}
result.append(entry);
}
while (num--, num > 0);
return result; return result;
} }
@ -1602,9 +1820,34 @@ CodeBody def_global_body( s32 num, ... )
va_list va; va_list va;
va_start(va, num); va_start(va, num);
def_body_code_validation_start( def_global_body ); do
GEN_AST_BODY_GLOBAL_UNALLOWED_TYPES {
def_body_code_validation_end( def_global_body ); Code_POD pod = va_arg(va, Code_POD);
Code entry = pcast(Code, pod);
if (!entry)
{
log_failure("gen::" "def_global_body" ": Provided an null entry");
return CodeInvalid;
}
switch (entry->Type)
{
case Global_Body:
result.append( entry.cast<CodeBody>() ) ;
continue;
GEN_AST_BODY_GLOBAL_UNALLOWED_TYPES
log_failure("gen::" "def_global_body" ": Entry type is not allowed: %s", entry.debug_str());
return (*Code::Invalid.ast);
default:
break;
}
result.append(entry);
}
while (num--, num > 0);
va_end(va); va_end(va);
return result; return result;
@ -1618,9 +1861,34 @@ CodeBody def_global_body( s32 num, Code* codes )
result = (CodeBody) make_code(); result = (CodeBody) make_code();
result->Type = Global_Body; result->Type = Global_Body;
def_body_code_array_validation_start( def_global_body ); do
GEN_AST_BODY_GLOBAL_UNALLOWED_TYPES {
def_body_code_validation_end( def_global_body ); Code entry = *codes;
codes++;
if (!entry)
{
log_failure("gen::" "def_global_body" ": Provided an null entry");
return CodeInvalid;
}
switch (entry->Type)
{
case Global_Body:
result.append( entry.cast<CodeBody>() ) ;
continue;
GEN_AST_BODY_GLOBAL_UNALLOWED_TYPES
log_failure("gen::" "def_global_body" ": Entry type is not allowed: %s", entry.debug_str());
return CodeInvalid;
default:
break;
}
result.append(entry);
}
while (num--, num > 0);
return result; return result;
} }
@ -1635,9 +1903,30 @@ CodeBody def_namespace_body( s32 num, ... )
va_list va; va_list va;
va_start(va, num); va_start(va, num);
def_body_code_validation_start( def_namespace_body ); do
GEN_AST_BODY_NAMESPACE_UNALLOWED_TYPES {
def_body_code_validation_end( def_namespace_body ); Code_POD pod = va_arg(va, Code_POD);
Code entry = pcast(Code, pod);
if (!entry)
{
log_failure("gen::" "def_namespace_body" ": Provided an null entry");
return CodeInvalid;
}
switch (entry->Type)
{
GEN_AST_BODY_NAMESPACE_UNALLOWED_TYPES
log_failure("gen::" "def_namespace_body" ": Entry type is not allowed: %s", entry.debug_str());
return CodeInvalid;
default:
break;
}
result.append(entry);
}
while (num--, num > 0);
va_end(va); va_end(va);
return result; return result;
@ -1651,9 +1940,29 @@ CodeBody def_namespace_body( s32 num, Code* codes )
result = (CodeBody) make_code(); result = (CodeBody) make_code();
result->Type = Global_Body; result->Type = Global_Body;
def_body_code_array_validation_start( def_namespace_body ); do
GEN_AST_BODY_NAMESPACE_UNALLOWED_TYPES {
def_body_code_validation_end( def_namespace_body ); Code entry = *codes;
codes++;
if (!entry)
{
log_failure("gen::" "def_namespace_body" ": Provided an null entry");
return CodeInvalid;
}
switch (entry->Type)
{
GEN_AST_BODY_NAMESPACE_UNALLOWED_TYPES
log_failure("gen::" "def_namespace_body" ": Entry type is not allowed: %s", entry.debug_str() );
return CodeInvalid;
default: break;
}
result.append(entry);
}
while (num--, num > 0);
return result; return result;
} }
@ -1803,9 +2112,30 @@ CodeBody def_struct_body( s32 num, ... )
va_list va; va_list va;
va_start(va, num); va_start(va, num);
def_body_code_validation_start( def_struct_body ); do
GEN_AST_BODY_STRUCT_UNALLOWED_TYPES {
def_body_code_validation_end( def_struct_body ); Code_POD pod = va_arg(va, Code_POD);
Code entry = pcast(Code, pod);
if (!entry)
{
log_failure("gen::" "def_struct_body" ": Provided an null entry");
return CodeInvalid;
}
switch (entry->Type)
{
GEN_AST_BODY_STRUCT_UNALLOWED_TYPES
log_failure("gen::" "def_struct_body" ": Entry type is not allowed: %s", entry.debug_str());
return CodeInvalid;
default:
break;
}
result.append(entry);
}
while (num--, num > 0);
va_end(va); va_end(va);
return result; return result;
@ -1819,9 +2149,30 @@ CodeBody def_struct_body( s32 num, Code* codes )
result = (CodeBody) make_code(); result = (CodeBody) make_code();
result->Type = Struct_Body; result->Type = Struct_Body;
def_body_code_array_validation_start( def_struct_body ); do
GEN_AST_BODY_STRUCT_UNALLOWED_TYPES {
def_body_code_validation_end( def_struct_body ); Code entry = *codes;
codes++;
if (!entry)
{
log_failure("gen::" "def_struct_body" ": Provided an null entry");
return CodeInvalid;
}
switch (entry->Type)
{
GEN_AST_BODY_STRUCT_UNALLOWED_TYPES
log_failure("gen::" "def_struct_body" ": Entry type is not allowed: %s", entry.debug_str() );
return CodeInvalid;
default:
break;
}
result.append(entry);
}
while (num--, num > 0);
return result; return result;
} }
@ -1849,7 +2200,7 @@ CodeBody def_union_body( s32 num, ... )
if ( entry->Type != Untyped && entry->Type != Comment ) if ( entry->Type != Untyped && entry->Type != Comment )
{ {
log_failure("gen::def_union_body: Entry type is not allowed - %s. Must be of untyped or comment type.", entry.debug_str() ); \ log_failure("gen::def_union_body: Entry type is not allowed - %s. Must be of untyped or comment type.", entry.debug_str() );
return CodeInvalid; return CodeInvalid;
} }
@ -1895,3 +2246,9 @@ CodeBody def_union_body( s32 num, CodeUnion* codes )
# undef name_check # undef name_check
# undef null_check # undef null_check
# undef null_or_invalid_check # undef null_or_invalid_check
# undef def_body_start
# undef def_body_code_array_start
#pragma endregion Upfront

View File

@ -2,8 +2,11 @@
# error Gen.hpp : GEN_TIME not defined # error Gen.hpp : GEN_TIME not defined
#endif #endif
#include "gen.hpp"
//! If its desired to roll your own dependencies, define GEN_ROLL_OWN_DEPENDENCIES before including this file. //! If its desired to roll your own dependencies, define GEN_ROLL_OWN_DEPENDENCIES before including this file.
//! Dependencies are derived from the c-zpl library: https://github.com/zpl-c/zpl //! Dependencies are derived from the c-zpl library: https://github.com/zpl-c/zpl
#ifndef GEN_ROLL_OWN_DEPENDENCIES #ifndef GEN_ROLL_OWN_DEPENDENCIES
# include "gen.dep.cpp" # include "gen.dep.cpp"
#endif #endif

View File

@ -32,6 +32,8 @@ global CodeAttributes attrib_api_import;
global Code module_global_fragment; global Code module_global_fragment;
global Code module_private_fragment; global Code module_private_fragment;
global Code fmt_newline;
global CodeParam param_varadic; global CodeParam param_varadic;
global CodePragma pragma_once; global CodePragma pragma_once;
@ -53,6 +55,7 @@ global CodeSpecifiers spec_mutable;
global CodeSpecifiers spec_neverinline; global CodeSpecifiers spec_neverinline;
global CodeSpecifiers spec_override; global CodeSpecifiers spec_override;
global CodeSpecifiers spec_ptr; global CodeSpecifiers spec_ptr;
global CodeSpecifiers spec_pure;
global CodeSpecifiers spec_ref; global CodeSpecifiers spec_ref;
global CodeSpecifiers spec_register; global CodeSpecifiers spec_register;
global CodeSpecifiers spec_rvalue; global CodeSpecifiers spec_rvalue;
@ -92,3 +95,4 @@ global CodeType t_f64;
#endif #endif
#pragma endregion Constants #pragma endregion Constants

View File

@ -0,0 +1,8 @@
# Temporary Code
These are heavy macro code used throughout the library thats intended to be replaced with codegen done with the library itself.
The reason for this is to minimize macro generation to only trivial cases.
This makes the library more verbose but makes it easier to debug which is of higher priority.
Any sort of verbosity cost will be mitigated with better docs and heavy usage of pragma regions.

View File

@ -0,0 +1,230 @@
// This is the non-bootstraped version of the Common AST Implementation. This will be obsolete once bootstrap is stress tested.
#pragma region AST Common
#define Define_CodeImpl( Typename ) \
char const* Typename::debug_str() \
{ \
if ( ast == nullptr ) \
return "Code::debug_str: AST is null!"; \
\
return rcast(AST*, ast)->debug_str(); \
} \
Code Typename::duplicate() \
{ \
if ( ast == nullptr ) \
{ \
log_failure("Code::duplicate: Cannot duplicate code, AST is null!"); \
return Code::Invalid; \
} \
\
return { rcast(AST*, ast)->duplicate() }; \
} \
bool Typename::is_equal( Code other ) \
{ \
if ( ast == nullptr || other.ast == nullptr ) \
{ \
log_failure("Code::is_equal: Cannot compare code, AST is null!"); \
return false; \
} \
\
return rcast(AST*, ast)->is_equal( other.ast ); \
} \
bool Typename::is_valid() \
{ \
return (AST*) ast != nullptr && rcast( AST*, ast)->Type != CodeT::Invalid; \
} \
void Typename::set_global() \
{ \
if ( ast == nullptr ) \
{ \
log_failure("Code::set_global: Cannot set code as global, AST is null!"); \
return; \
} \
\
rcast(AST*, ast)->Parent = Code::Global.ast; \
} \
String Typename::to_string() \
{ \
if ( ast == nullptr ) \
{ \
log_failure("Code::to_string: Cannot convert code to string, AST is null!"); \
return { nullptr }; \
} \
\
return rcast(AST*, ast)->to_string(); \
} \
Typename& Typename::operator =( Code other ) \
{ \
if ( other.ast && other->Parent ) \
{ \
ast = rcast( decltype(ast), other.ast->duplicate() ); \
rcast( AST*, ast)->Parent = nullptr; \
} \
\
ast = rcast( decltype(ast), other.ast ); \
return *this; \
} \
bool Typename::operator ==( Code other ) \
{ \
return (AST*) ast == other.ast; \
} \
bool Typename::operator !=( Code other ) \
{ \
return (AST*) ast != other.ast; \
} \
Typename::operator bool() \
{ \
return ast != nullptr; \
}
#define Define_CodeType_Impl( Typename ) \
AST* Code##Typename::raw() \
{ \
return rcast( AST*, ast ); \
} \
Code##Typename::operator Code() \
{ \
return *rcast( Code*, this ); \
} \
AST_##Typename* Code##Typename::operator->() \
{ \
if ( ast == nullptr ) \
{ \
log_failure( "Attempt to dereference a nullptr!" ); \
return nullptr; \
} \
return ast; \
} \
Define_CodeImpl( Code );
Define_CodeImpl( CodeBody );
Define_CodeImpl( CodeAttributes );
Define_CodeImpl( CodeComment );
Define_CodeImpl( CodeClass );
Define_CodeImpl( CodeConstructor );
Define_CodeImpl( CodeDefine );
Define_CodeImpl( CodeDestructor );
Define_CodeImpl( CodeEnum );
Define_CodeImpl( CodeExec );
Define_CodeImpl( CodeExtern );
Define_CodeImpl( CodeInclude );
Define_CodeImpl( CodeFriend );
Define_CodeImpl( CodeFn );
Define_CodeImpl( CodeModule );
Define_CodeImpl( CodeNS );
Define_CodeImpl( CodeOperator );
Define_CodeImpl( CodeOpCast );
Define_CodeImpl( CodeParam );
Define_CodeImpl( CodePragma );
Define_CodeImpl( CodePreprocessCond );
Define_CodeImpl( CodeSpecifiers );
Define_CodeImpl( CodeStruct );
Define_CodeImpl( CodeTemplate );
Define_CodeImpl( CodeType );
Define_CodeImpl( CodeTypedef );
Define_CodeImpl( CodeUnion );
Define_CodeImpl( CodeUsing );
Define_CodeImpl( CodeVar );
Define_CodeType_Impl( Attributes );
Define_CodeType_Impl( Comment );
Define_CodeType_Impl( Constructor );
Define_CodeType_Impl( Define );
Define_CodeType_Impl( Destructor );
Define_CodeType_Impl( Enum );
Define_CodeType_Impl( Exec );
Define_CodeType_Impl( Extern );
Define_CodeType_Impl( Include );
Define_CodeType_Impl( Friend );
Define_CodeType_Impl( Fn );
Define_CodeType_Impl( Module );
Define_CodeType_Impl( NS );
Define_CodeType_Impl( Operator );
Define_CodeType_Impl( OpCast );
Define_CodeType_Impl( Pragma );
Define_CodeType_Impl( PreprocessCond );
Define_CodeType_Impl( Template );
Define_CodeType_Impl( Type );
Define_CodeType_Impl( Typedef );
Define_CodeType_Impl( Union );
Define_CodeType_Impl( Using );
Define_CodeType_Impl( Var );
#undef Define_CodeImpl
#undef Define_CodeType_Impl
#define Define_AST_Cast( typename ) \
AST::operator Code ## typename() \
{ \
return { rcast( AST_ ## typename*, this ) }; \
}
Define_AST_Cast( Body );
Define_AST_Cast( Attributes );
Define_AST_Cast( Comment );
Define_AST_Cast( Constructor );
Define_AST_Cast( Class );
Define_AST_Cast( Define );
Define_AST_Cast( Destructor );
Define_AST_Cast( Enum );
Define_AST_Cast( Exec );
Define_AST_Cast( Extern );
Define_AST_Cast( Include );
Define_AST_Cast( Friend );
Define_AST_Cast( Fn );
Define_AST_Cast( Module );
Define_AST_Cast( NS );
Define_AST_Cast( Operator );
Define_AST_Cast( OpCast );
Define_AST_Cast( Param );
Define_AST_Cast( Pragma );
Define_AST_Cast( PreprocessCond );
Define_AST_Cast( Struct );
Define_AST_Cast( Specifiers );
Define_AST_Cast( Template );
Define_AST_Cast( Type );
Define_AST_Cast( Typedef );
Define_AST_Cast( Union );
Define_AST_Cast( Using );
Define_AST_Cast( Var );
#undef Define_AST_Cast
#define Define_CodeCast( type ) \
Code::operator Code ## type() const \
{ \
return { (AST_ ## type*) ast }; \
}
Define_CodeCast( Attributes );
Define_CodeCast( Comment );
Define_CodeCast( Constructor );
Define_CodeCast( Class );
Define_CodeCast( Define );
Define_CodeCast( Destructor );
Define_CodeCast( Exec );
Define_CodeCast( Enum );
Define_CodeCast( Extern );
Define_CodeCast( Include );
Define_CodeCast( Friend );
Define_CodeCast( Fn );
Define_CodeCast( Module );
Define_CodeCast( NS );
Define_CodeCast( Operator );
Define_CodeCast( OpCast );
Define_CodeCast( Param );
Define_CodeCast( Pragma );
Define_CodeCast( PreprocessCond );
Define_CodeCast( Specifiers );
Define_CodeCast( Struct );
Define_CodeCast( Template );
Define_CodeCast( Type );
Define_CodeCast( Typedef );
Define_CodeCast( Union );
Define_CodeCast( Using );
Define_CodeCast( Var );
Define_CodeCast( Body);
#undef Define_CodeCast
#pragma endregion AST Common

View File

@ -5,6 +5,7 @@ namespace ECode
# define Define_Types \ # define Define_Types \
Entry( Invalid ) \ Entry( Invalid ) \
Entry( Untyped ) \ Entry( Untyped ) \
Entry( NewLine ) \
Entry( Comment ) \ Entry( Comment ) \
Entry( Access_Private ) \ Entry( Access_Private ) \
Entry( Access_Protected ) \ Entry( Access_Protected ) \
@ -13,6 +14,10 @@ namespace ECode
Entry( Class ) \ Entry( Class ) \
Entry( Class_Fwd ) \ Entry( Class_Fwd ) \
Entry( Class_Body ) \ Entry( Class_Body ) \
Entry( Constructor ) \
Entry( Constructor_Fwd ) \
Entry( Destructor ) \
Entry( Destructor_Fwd ) \
Entry( Enum ) \ Entry( Enum ) \
Entry( Enum_Fwd ) \ Entry( Enum_Fwd ) \
Entry( Enum_Body ) \ Entry( Enum_Body ) \
@ -53,8 +58,8 @@ namespace ECode
Entry( Template ) \ Entry( Template ) \
Entry( Typedef ) \ Entry( Typedef ) \
Entry( Typename ) \ Entry( Typename ) \
Entry( Union ) \ Entry( Union ) \
Entry( Union_Body) \ Entry( Union_Body) \
Entry( Using ) \ Entry( Using ) \
Entry( Using_Namespace ) \ Entry( Using_Namespace ) \
Entry( Variable ) Entry( Variable )
@ -84,3 +89,4 @@ namespace ECode
# undef Define_Types # undef Define_Types
} }
using CodeT = ECode::Type; using CodeT = ECode::Type;

View File

@ -57,14 +57,14 @@ namespace EOperator
}; };
inline inline
char const* to_str( Type op ) StrC to_str( Type op )
{ {
local_persist local_persist
char const* lookup[ Num_Ops ] = { StrC lookup[ Num_Ops ] = {
# define Entry( Type_, Token_ ) stringize(Token_), # define Entry( Type_, Token_ ) { sizeof(stringize(Token_)), stringize(Token_) },
Define_Operators Define_Operators
# undef Entry # undef Entry
"," txt_StrC(",")
}; };
return lookup[ op ]; return lookup[ op ];
@ -73,3 +73,4 @@ namespace EOperator
# undef Define_Operators # undef Define_Operators
} }
using OperatorT = EOperator::Type; using OperatorT = EOperator::Type;

View File

@ -31,7 +31,8 @@ namespace ESpecifier
Entry( Virtual, virtual ) \ Entry( Virtual, virtual ) \
Entry( Const, const ) \ Entry( Const, const ) \
Entry( Final, final ) \ Entry( Final, final ) \
Entry( Override, override ) Entry( Override, override ) \
Entry( Pure, = 0 )
enum Type : u32 enum Type : u32
{ {
@ -106,3 +107,4 @@ namespace ESpecifier
# undef Define_Specifiers # undef Define_Specifiers
} }
using SpecifierT = ESpecifier::Type; using SpecifierT = ESpecifier::Type;

View File

@ -0,0 +1,170 @@
namespace Parser
{
/*
This is a simple lexer that focuses on tokenizing only tokens relevant to the library.
It will not be capable of lexing C++ code with unsupported features.
For the sake of scanning files, it can scan preprocessor directives
__Attributes_Start is only used to indicate the start of the user_defined attribute list.
*/
#ifndef GEN_DEFINE_ATTRIBUTE_TOKENS
# define GEN_DEFINE_ATTRIBUTE_TOKENS \
Entry( API_Export, "GEN_API_Export_Code" ) \
Entry( API_Import, "GEN_API_Import_Code" )
#endif
# define Define_TokType \
Entry( Invalid, "INVALID" ) \
Entry( Access_Private, "private" ) \
Entry( Access_Protected, "protected" ) \
Entry( Access_Public, "public" ) \
Entry( Access_MemberSymbol, "." ) \
Entry( Access_StaticSymbol, "::") \
Entry( Ampersand, "&" ) \
Entry( Ampersand_DBL, "&&" ) \
Entry( Assign_Classifer, ":" ) \
Entry( Attribute_Open, "[[" ) \
Entry( Attribute_Close, "]]" ) \
Entry( BraceCurly_Open, "{" ) \
Entry( BraceCurly_Close, "}" ) \
Entry( BraceSquare_Open, "[" ) \
Entry( BraceSquare_Close, "]" ) \
Entry( Capture_Start, "(" ) \
Entry( Capture_End, ")" ) \
Entry( Comment, "__comment__" ) \
Entry( Comment_End, "__comment_end__" ) \
Entry( Comment_Start, "__comment start__" ) \
Entry( Char, "__character__" ) \
Entry( Comma, "," ) \
Entry( Decl_Class, "class" ) \
Entry( Decl_GNU_Attribute, "__attribute__" ) \
Entry( Decl_MSVC_Attribute, "__declspec" ) \
Entry( Decl_Enum, "enum" ) \
Entry( Decl_Extern_Linkage, "extern" ) \
Entry( Decl_Friend, "friend" ) \
Entry( Decl_Module, "module" ) \
Entry( Decl_Namespace, "namespace" ) \
Entry( Decl_Operator, "operator" ) \
Entry( Decl_Struct, "struct" ) \
Entry( Decl_Template, "template" ) \
Entry( Decl_Typedef, "typedef" ) \
Entry( Decl_Using, "using" ) \
Entry( Decl_Union, "union" ) \
Entry( Identifier, "__identifier__" ) \
Entry( Module_Import, "import" ) \
Entry( Module_Export, "export" ) \
Entry( NewLine, "__NewLine__" ) \
Entry( Number, "__number__" ) \
Entry( Operator, "__operator__" ) \
Entry( Preprocess_Hash, "#" ) \
Entry( Preprocess_Define, "define") \
Entry( Preprocess_If, "if") \
Entry( Preprocess_IfDef, "ifdef") \
Entry( Preprocess_IfNotDef, "ifndef") \
Entry( Preprocess_ElIf, "elif") \
Entry( Preprocess_Else, "else") \
Entry( Preprocess_EndIf, "endif") \
Entry( Preprocess_Include, "include" ) \
Entry( Preprocess_Pragma, "pragma") \
Entry( Preprocess_Content, "__macro_content__") \
Entry( Preprocess_Macro, "__macro__") \
Entry( Preprocess_Unsupported, "__unsupported__" ) \
Entry( Spec_Alignas, "alignas" ) \
Entry( Spec_Const, "const" ) \
Entry( Spec_Consteval, "consteval" ) \
Entry( Spec_Constexpr, "constexpr" ) \
Entry( Spec_Constinit, "constinit" ) \
Entry( Spec_Explicit, "explicit" ) \
Entry( Spec_Extern, "extern" ) \
Entry( Spec_Final, "final" ) \
Entry( Spec_Global, "global" ) \
Entry( Spec_Inline, "inline" ) \
Entry( Spec_Internal_Linkage, "internal" ) \
Entry( Spec_LocalPersist, "local_persist" ) \
Entry( Spec_Mutable, "mutable" ) \
Entry( Spec_NeverInline, "neverinline" ) \
Entry( Spec_Override, "override" ) \
Entry( Spec_Static, "static" ) \
Entry( Spec_ThreadLocal, "thread_local" ) \
Entry( Spec_Virtual, "virtual" ) \
Entry( Spec_Volatile, "volatile") \
Entry( Star, "*" ) \
Entry( Statement_End, ";" ) \
Entry( StaticAssert, "static_assert" ) \
Entry( String, "__string__" ) \
Entry( Type_Unsigned, "unsigned" ) \
Entry( Type_Signed, "signed" ) \
Entry( Type_Short, "short" ) \
Entry( Type_Long, "long" ) \
Entry( Type_char, "char" ) \
Entry( Type_int, "int" ) \
Entry( Type_double, "double" ) \
Entry( Type_MS_int8, "__int8" ) \
Entry( Type_MS_int16, "__int16" ) \
Entry( Type_MS_int32, "__int32" ) \
Entry( Type_MS_int64, "__int64" ) \
Entry( Type_MS_W64, "_W64" ) \
Entry( Varadic_Argument, "..." ) \
Entry( __Attributes_Start, "__attrib_start__" )
namespace ETokType
{
enum Type : u32
{
# define Entry( Name_, Str_ ) Name_,
Define_TokType
GEN_DEFINE_ATTRIBUTE_TOKENS
# undef Entry
NumTokens,
};
internal inline
Type to_type( StrC str_tok )
{
local_persist
StrC lookup[(u32)NumTokens] =
{
# define Entry( Name_, Str_ ) { sizeof(Str_), Str_ },
Define_TokType
GEN_DEFINE_ATTRIBUTE_TOKENS
# undef Entry
};
for ( u32 index = 0; index < (u32)NumTokens; index++ )
{
s32 lookup_len = lookup[index].Len - 1;
char const* lookup_str = lookup[index].Ptr;
if ( lookup_len != str_tok.Len )
continue;
if ( str_compare( str_tok.Ptr, lookup_str, lookup_len ) == 0 )
return scast(Type, index);
}
return Invalid;
}
internal inline
StrC to_str( Type type )
{
local_persist
StrC lookup[(u32)NumTokens] =
{
# define Entry( Name_, Str_ ) { sizeof(Str_), Str_ },
Define_TokType
GEN_DEFINE_ATTRIBUTE_TOKENS
# undef Entry
};
return lookup[(u32)type];
}
# undef Define_TokType
};
using TokType = ETokType::Type;
} // Parser

View File

@ -3,9 +3,9 @@ using LogFailType = sw(*)(char const*, ...);
// By default this library will either crash or exit if an error is detected while generating codes. // 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. // 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 #ifdef GEN_DONT_USE_FATAL
constexpr LogFailType log_failure = log_fmt; #define log_failure log_fmt
#else #else
constexpr LogFailType log_failure = fatal; #define log_failure fatal
#endif #endif
enum class AccessSpec : u32 enum class AccessSpec : u32
@ -113,4 +113,13 @@ constexpr char const* Attribute_Keyword = stringize( GEN_Attribute_Keyword );
#endif #endif
constexpr char const* Attribute_Keyword = ""; constexpr char const* Attribute_Keyword = "";
#endif #endif
// Implements basic string interning. Data structure is based off the ZPL Hashtable.
using StringTable = HashTable<String const>;
// Represents strings cached with the string table.
// Should never be modified, if changed string is desired, cache_string( str ) another.
using StringCached = String const;

View File

@ -181,3 +181,4 @@ Code untyped_token_fmt( s32 num_tokens, ... )
return result; return result;
} }

View File

@ -117,4 +117,5 @@ typedef s8 b8;
typedef s16 b16; typedef s16 b16;
typedef s32 b32; typedef s32 b32;
#pragma region Basic Types #pragma endregion Basic Types

View File

@ -424,9 +424,16 @@ struct HashTable
for ( idx = 0; idx < Entries.num(); idx++ ) for ( idx = 0; idx < Entries.num(); idx++ )
{ {
Entry* entry; Entry* entry;
FindResult find_result; FindResult find_result;
entry = & Entries[ idx ];
find_result = find( entry->Key );
if ( find_result.PrevIndex < 0 )
Hashes[ find_result.HashIndex ] = idx;
else
Entries[ find_result.PrevIndex ].Next = idx;
} }
} }
@ -533,3 +540,4 @@ protected:
}; };
#pragma endregion Containers #pragma endregion Containers

View File

@ -1,4 +1,4 @@
#pragma endregion Debug #pragma region Debug
void assert_handler( char const* condition, char const* file, s32 line, char const* msg, ... ) void assert_handler( char const* condition, char const* file, s32 line, char const* msg, ... )
{ {
@ -39,3 +39,4 @@ s32 assert_crash( char const* condition )
#endif #endif
#pragma endregion Debug #pragma endregion Debug

View File

@ -1,4 +1,4 @@
#pragma endregion Debug #pragma region Debug
#if defined( _MSC_VER ) #if defined( _MSC_VER )
# if _MSC_VER < 1300 # if _MSC_VER < 1300
@ -33,4 +33,27 @@ void assert_handler( char const* condition, char const* file, s32 line, char con
s32 assert_crash( char const* condition ); s32 assert_crash( char const* condition );
void process_exit( u32 code ); void process_exit( u32 code );
#if Build_Debug
#define fatal( fmt, ... ) \
do \
{ \
local_persist thread_local \
char buf[GEN_PRINTF_MAXLEN] = { 0 }; \
\
str_fmt(buf, GEN_PRINTF_MAXLEN, fmt, __VA_ARGS__); \
GEN_PANIC(buf); \
} \
while (0)
#else
# define fatal( fmt, ... ) \
do \
{ \
str_fmt_out_err_va( fmt, __VA_ARGS__ ); \
process_exit(1); \
} \
while (0)
#endif
#pragma endregion Debug #pragma endregion Debug

View File

@ -634,3 +634,4 @@ internal GEN_FILE_CLOSE_PROC( _memory_file_close )
FileOperations const memory_file_operations = { _memory_file_read, _memory_file_write, _memory_file_seek, _memory_file_close }; FileOperations const memory_file_operations = { _memory_file_read, _memory_file_write, _memory_file_seek, _memory_file_close };
#pragma endregion File Handling #pragma endregion File Handling

View File

@ -370,3 +370,4 @@ u8* file_stream_buf( FileInfo* file, sw* size );
extern FileOperations const memory_file_operations; extern FileOperations const memory_file_operations;
#pragma endregion File Handling #pragma endregion File Handling

View File

@ -82,4 +82,5 @@ u64 crc64( void const* data, sw len )
return result; return result;
} }
#pragma region Hashing #pragma endregion Hashing

View File

@ -4,3 +4,4 @@ u32 crc32( void const* data, sw len );
u64 crc64( void const* data, sw len ); u64 crc64( void const* data, sw len );
#pragma endregion Hashing #pragma endregion Hashing

View File

@ -122,10 +122,21 @@
#pragma endregion Platform Detection #pragma endregion Platform Detection
#pragma region Mandatory Includes #pragma region Mandatory Includes
# include <stdarg.h> # include <stdarg.h>
# include <stddef.h> # include <stddef.h>
# if defined( GEN_SYSTEM_WINDOWS ) # if defined( GEN_SYSTEM_WINDOWS )
# include <intrin.h> # include <intrin.h>
# endif # endif
#pragma endregion Mandatory Includes #pragma endregion Mandatory Includes
#ifdef GEN_DONT_USE_NAMESPACE
# define GEN_NS_BEGIN
# define GEN_NS_END
#else
# define GEN_NS_BEGIN namespace gen {
# define GEN_NS_END }
#endif

View File

@ -14,6 +14,7 @@
#define bitfield_is_equal( Type, Field, Mask ) ( (Type(Mask) & Type(Field)) == Type(Mask) ) #define bitfield_is_equal( Type, Field, Mask ) ( (Type(Mask) & Type(Field)) == Type(Mask) )
// Casting // Casting
#define ccast( Type, Value ) ( * const_cast< Type* >( & (Value) ) ) #define ccast( Type, Value ) ( * const_cast< Type* >( & (Value) ) )
#define pcast( Type, Value ) ( * reinterpret_cast< Type* >( & ( Value ) ) ) #define pcast( Type, Value ) ( * reinterpret_cast< Type* >( & ( Value ) ) )
#define rcast( Type, Value ) reinterpret_cast< Type >( Value ) #define rcast( Type, Value ) reinterpret_cast< Type >( Value )
@ -21,38 +22,66 @@
// Num Arguments (Varadics) // Num Arguments (Varadics)
#if defined(__GNUC__) || defined(__clang__) #if defined(__GNUC__) || defined(__clang__)
// Supports 0-10 arguments // Supports 0-50 arguments
#define num_args_impl( _0, \ #define num_args_impl( _0, \
_1, _2, _3, _4, _5, _6, _7, _8, _9, _10, \ _1, _2, _3, _4, _5, _6, _7, _8, _9, _10, \
_11, _12, _13, _14, _15, _16, _17, _18, _19, _20, \ _11, _12, _13, _14, _15, _16, _17, _18, _19, _20, \
N, ... \ _21, _22, _23, _24, _25, _26, _27, _28, _29, _30, \
_31, _32, _33, _34, _35, _36, _37, _38, _39, _40, \
_41, _42, _43, _44, _45, _46, _47, _48, _49, _50, \
_51, _52, _53, _54, _55, _56, _57, _58, _59, _60, \
_61, _62, _63, _64, _65, _66, _67, _68, _69, _70, \
_71, _72, _73, _74, _75, _76, _77, _78, _79, _80, \
_81, _82, _83, _84, _85, _86, _87, _88, _89, _90, \
_91, _92, _93, _94, _95, _96, _97, _98, _99, _100, \
N, ... \
) N ) N
// _21, _22, _23, _24, _25, _26, _27, _28, _29, _30, \
// _31, _32, _33, _34, _35, _36, _37, _38, _39, _40, \
// _41, _42, _43, _44, _45, _46, _47, _48, _49, _50,
// ## deletes preceding comma if _VA_ARGS__ is empty (GCC, Clang) // ## deletes preceding comma if _VA_ARGS__ is empty (GCC, Clang)
#define num_args(...) \ #define num_args(...) \
num_args_impl(_, ## __VA_ARGS__, \ num_args_impl(_, ## __VA_ARGS__, \
20, 19, 18, 17, 16, 15, 14, 13, 12, 11, \ 100, 99, 98, 97, 96, 95, 94, 93, 92, 91, \
10, 9, 8, 7, 6, 5, 4, 3, 2, 1, \ 90, 89, 88, 87, 86, 85, 84, 83, 82, 81, \
0 \ 80, 79, 78, 77, 76, 75, 74, 73, 72, 71, \
70, 69, 68, 67, 66, 65, 64, 63, 62, 61, \
60, 59, 58, 57, 56, 55, 54, 53, 52, 51, \
50, 49, 48, 47, 46, 45, 44, 43, 42, 41, \
40, 39, 38, 37, 36, 35, 34, 33, 32, 31, \
30, 29, 28, 27, 26, 25, 24, 23, 22, 21, \
20, 19, 18, 17, 16, 15, 14, 13, 12, 11, \
10, 9, 8, 7, 6, 5, 4, 3, 2, 1, \
0 \
) )
// 50, 49, 48, 47, 46, 45, 44, 43, 42, 41, \
// 40, 39, 38, 37, 36, 35, 34, 33, 32, 31, \
// 30, 29, 28, 27, 26, 25, 24, 23, 22, 21,
#else #else
// Supports 1-10 arguments // Supports 1-50 arguments
#define num_args_impl( \ #define num_args_impl( \
_1, _2, _3, _4, _5, _6, _7, _8, _9, _10, \ _1, _2, _3, _4, _5, _6, _7, _8, _9, _10, \
_11, _12, _13, _14, _15, _16, _17, _18, _19, _20, \ _11, _12, _13, _14, _15, _16, _17, _18, _19, _20, \
N, ... \ _21, _22, _23, _24, _25, _26, _27, _28, _29, _30, \
_31, _32, _33, _34, _35, _36, _37, _38, _39, _40, \
_41, _42, _43, _44, _45, _46, _47, _48, _49, _50, \
_51, _52, _53, _54, _55, _56, _57, _58, _59, _60, \
_61, _62, _63, _64, _65, _66, _67, _68, _69, _70, \
_71, _72, _73, _74, _75, _76, _77, _78, _79, _80, \
_81, _82, _83, _84, _85, _86, _87, _88, _89, _90, \
_91, _92, _93, _94, _95, _96, _97, _98, _99, _100, \
N, ... \
) N ) N
#define num_args(...) \ #define num_args(...) \
num_args_impl( __VA_ARGS__, \ num_args_impl( __VA_ARGS__, \
20, 19, 18, 17, 16, 15, 14, 13, 12, 11, \ 100, 99, 98, 97, 96, 95, 94, 93, 92, 91, \
10, 9, 8, 7, 6, 5, 4, 3, 2, 1 \ 90, 89, 88, 87, 86, 85, 84, 83, 82, 81, \
80, 79, 78, 77, 76, 75, 74, 73, 72, 71, \
70, 69, 68, 67, 66, 65, 64, 63, 62, 61, \
60, 59, 58, 57, 56, 55, 54, 53, 52, 51, \
50, 49, 48, 47, 46, 45, 44, 43, 42, 41, \
50, 49, 48, 47, 46, 45, 44, 43, 42, 41, \
40, 39, 38, 37, 36, 35, 34, 33, 32, 31, \
30, 29, 28, 27, 26, 25, 24, 23, 22, 21, \
20, 19, 18, 17, 16, 15, 14, 13, 12, 11, \
10, 9, 8, 7, 6, 5, 4, 3, 2, 1 \
) )
#endif #endif
@ -105,3 +134,4 @@ void swap( Type& a, Type& b )
} }
#pragma endregion Macros #pragma endregion Macros

View File

@ -387,3 +387,4 @@ void Pool::clear()
} }
#pragma endregion Memory #pragma endregion Memory

View File

@ -484,3 +484,4 @@ struct Pool
}; };
#pragma endregion Memory #pragma endregion Memory

View File

@ -799,7 +799,6 @@ ADT_Error adt_str_to_number_strict( ADT_Node* node )
# define GEN_CSV_ASSERT( msg ) # define GEN_CSV_ASSERT( msg )
#endif #endif
u8 csv_parse_delimiter( CSV_Object* root, char* text, AllocatorInfo allocator, b32 has_header, char delim ) u8 csv_parse_delimiter( CSV_Object* root, char* text, AllocatorInfo allocator, b32 has_header, char delim )
{ {
CSV_Error error = ECSV_Error__NONE; CSV_Error error = ECSV_Error__NONE;
@ -1104,3 +1103,4 @@ String csv_write_string_delimiter( AllocatorInfo a, CSV_Object* obj, char delimi
} }
#pragma endregion CSV #pragma endregion CSV

View File

@ -423,3 +423,4 @@ GEN_IMPL_INLINE String csv_write_string( AllocatorInfo a, CSV_Object* obj )
} }
#pragma endregion CSV #pragma endregion CSV

View File

@ -541,6 +541,16 @@ sw str_fmt_file( struct FileInfo* f, char const* fmt, ... )
return res; return res;
} }
sw str_fmt( char* str, sw n, char const* fmt, ... )
{
sw res;
va_list va;
va_start( va, fmt );
res = str_fmt_va( str, n, fmt, va );
va_end( va );
return res;
}
sw str_fmt_out_va( char const* fmt, va_list va ) sw str_fmt_out_va( char const* fmt, va_list va )
{ {
return str_fmt_file_va( file_get_standard( EFileStandard_OUTPUT ), fmt, va ); return str_fmt_file_va( file_get_standard( EFileStandard_OUTPUT ), fmt, va );
@ -562,3 +572,4 @@ sw str_fmt_out_err( char const* fmt, ... )
} }
#pragma endregion Printing #pragma endregion Printing

View File

@ -9,13 +9,11 @@ struct FileInfo;
// NOTE: A locally persisting buffer is used internally // NOTE: A locally persisting buffer is used internally
char* str_fmt_buf ( char const* fmt, ... ); char* str_fmt_buf ( char const* fmt, ... );
char* str_fmt_buf_va ( char const* fmt, va_list va ); char* str_fmt_buf_va ( char const* fmt, va_list va );
sw str_fmt ( char* str, sw n, char const* fmt, ... );
sw str_fmt_va ( char* str, sw n, char const* fmt, va_list va ); sw str_fmt_va ( char* str, sw n, char const* fmt, va_list va );
sw str_fmt_out_va ( char const* fmt, va_list va ); sw str_fmt_out_va ( char const* fmt, va_list va );
sw str_fmt_out_err ( char const* fmt, ... ); sw str_fmt_out_err ( char const* fmt, ... );
sw str_fmt_out_err_va( char const* fmt, va_list va ); sw str_fmt_out_err_va( char const* fmt, va_list va );
// TODO : Move these to file handling.
sw str_fmt_file ( FileInfo* f, char const* fmt, ... ); sw str_fmt_file ( FileInfo* f, char const* fmt, ... );
sw str_fmt_file_va ( FileInfo* f, char const* fmt, va_list va ); sw str_fmt_file_va ( FileInfo* f, char const* fmt, va_list va );
@ -35,29 +33,5 @@ sw log_fmt(char const* fmt, ...)
return res; return res;
} }
inline
sw fatal(char const* fmt, ...)
{
local_persist thread_local
char buf[GEN_PRINTF_MAXLEN] = { 0 };
va_list va;
#if Build_Debug
va_start(va, fmt);
str_fmt_va(buf, GEN_PRINTF_MAXLEN, fmt, va);
va_end(va);
assert_crash(buf);
return -1;
#else
va_start(va, fmt);
str_fmt_out_err_va( fmt, va);
va_end(va);
exit(1);
return -1;
#endif
}
#pragma endregion Printing #pragma endregion Printing

View File

@ -78,3 +78,4 @@
#endif #endif
#pragma endregion Macros and Includes #pragma endregion Macros and Includes

View File

@ -37,3 +37,4 @@ bool String::append_fmt( char const* fmt, ... )
} }
#pragma endregion String #pragma endregion String

View File

@ -373,3 +373,4 @@ struct String_POD
static_assert( sizeof( String_POD ) == sizeof( String ), "String is not a POD" ); static_assert( sizeof( String_POD ) == sizeof( String ), "String is not a POD" );
#pragma endregion String #pragma endregion String

View File

@ -207,3 +207,4 @@ f64 str_to_f64( const char* str, char** end_ptr )
} }
#pragma endregion String Ops #pragma endregion String Ops

View File

@ -260,3 +260,4 @@ GEN_IMPL_INLINE void str_to_upper( char* str )
} }
#pragma endregion String Ops #pragma endregion String Ops

View File

@ -160,3 +160,4 @@
#endif #endif
#pragma endregion Timing #pragma endregion Timing

View File

@ -12,3 +12,4 @@ u64 time_rel_ms( void );
#endif #endif
#pragma endregion Timing #pragma endregion Timing

View File

@ -1,5 +1,6 @@
Invalid Invalid
Untyped Untyped
NewLine
Comment Comment
Access_Private Access_Private
Access_Protected Access_Protected
@ -8,6 +9,10 @@ PlatformAttributes
Class Class
Class_Fwd Class_Fwd
Class_Body Class_Body
Constructor
Constructor_Fwd
Destructor
Destructor_Fwd
Enum Enum
Enum_Fwd Enum_Fwd
Enum_Body Enum_Body

1 Invalid
2 Untyped
3 NewLine
4 Comment
5 Access_Private
6 Access_Protected
9 Class
10 Class_Fwd
11 Class_Body
12 Constructor
13 Constructor_Fwd
14 Destructor
15 Destructor_Fwd
16 Enum
17 Enum_Fwd
18 Enum_Body

View File

@ -21,3 +21,4 @@ Virtual, virtual
Const, const Const, const
Final, final Final, final
Override, override Override, override
Pure, = 0

1 Invalid INVALID
21 Const const
22 Final final
23 Override override
24 Pure = 0

View File

@ -16,6 +16,8 @@ BraceSquare_Close, "]"
Capture_Start, "(" Capture_Start, "("
Capture_End, ")" Capture_End, ")"
Comment, "__comemnt__" Comment, "__comemnt__"
Comment_End, "__comment_end__"
Comment_Start, "__comment_start__"
Char, "__character__" Char, "__character__"
Comma, "," Comma, ","
Decl_Class, "class" Decl_Class, "class"
@ -26,7 +28,7 @@ Decl_Extern_Linkage, "extern"
Decl_Friend, "friend" Decl_Friend, "friend"
Decl_Module, "module" Decl_Module, "module"
Decl_Namespace, "namespace" Decl_Namespace, "namespace"
Decl_Operator, "__operator__" Decl_Operator, "operator"
Decl_Struct, "struct" Decl_Struct, "struct"
Decl_Template, "template" Decl_Template, "template"
Decl_Typedef, "typedef" Decl_Typedef, "typedef"
@ -35,17 +37,20 @@ Decl_Union, "union"
Identifier, "__identifier__" Identifier, "__identifier__"
Module_Import, "import" Module_Import, "import"
Module_Export, "export" Module_Export, "export"
NewLine, "__new_line__"
Number, "__number__" Number, "__number__"
Operator, "__operator__" Operator, "__operator__"
Preprocess_Hash, "#"
Preprocess_Define, "define" Preprocess_Define, "define"
Preprocess_If, "if" Preprocess_If, "if"
Preprocess_IfDef, "ifdef" Preprocess_IfDef, "ifdef"
Preprocess_IfNotDef, "ifndef" Preprocess_IfNotDef, "ifndef"
Preprocess_ElIf, "elif" Preprocess_ElIf, "elif"
Preprocess_Else, "else" Preprocess_Else, "else"
Preprocess_EndIf, "endif" Preprocess_EndIf, "endif"
Preprocess_Include, "include" Preprocess_Include, "include"
Preprocess_Pragma, "pragma" Preprocess_Pragma, "pragma"
Preprocess_Content, "__macro_content__"
Preprocess_Macro, "__macro__" Preprocess_Macro, "__macro__"
Preprocess_Unsupported, "__unsupported__" Preprocess_Unsupported, "__unsupported__"
Spec_Alignas, "alignas" Spec_Alignas, "alignas"
@ -66,6 +71,7 @@ Spec_Override, "override"
Spec_Static, "static" Spec_Static, "static"
Spec_ThreadLocal, "thread_local" Spec_ThreadLocal, "thread_local"
Spec_Volatile, "volatile" Spec_Volatile, "volatile"
Spec_Virtual, "virtual"
Star, "*" Star, "*"
Statement_End, ";" Statement_End, ";"
StaticAssert, "static_assert" StaticAssert, "static_assert"
@ -77,10 +83,10 @@ Type_Long, "long"
Type_char, "char" Type_char, "char"
Type_int, "int" Type_int, "int"
Type_double, "double" Type_double, "double"
Type_MS_int8, "__int8" Type_MS_int8, "__int8"
Type_MS_int16, "__int16" Type_MS_int16, "__int16"
Type_MS_int32, "__int32" Type_MS_int32, "__int32"
Type_MS_int64, "__int64" Type_MS_int64, "__int64"
Type_MS_W64, "_W64" Type_MS_W64, "_W64"
Varadic_Argument, "..." Varadic_Argument, "..."
__Attributes_Start, "__attrib_start__" __Attributes_Start, "__attrib_start__"

1 Invalid __invalid__
16 Capture_Start (
17 Capture_End )
18 Comment __comemnt__
19 Comment_End __comment_end__
20 Comment_Start __comment_start__
21 Char __character__
22 Comma ,
23 Decl_Class class
28 Decl_Friend friend
29 Decl_Module module
30 Decl_Namespace namespace
31 Decl_Operator __operator__ operator
32 Decl_Struct struct
33 Decl_Template template
34 Decl_Typedef typedef
37 Identifier __identifier__
38 Module_Import import
39 Module_Export export
40 NewLine __new_line__
41 Number __number__
42 Operator __operator__
43 Preprocess_Hash #
44 Preprocess_Define define
45 Preprocess_If if
46 Preprocess_IfDef ifdef
47 Preprocess_IfNotDef ifndef
48 Preprocess_ElIf elif
49 Preprocess_Else else
50 Preprocess_EndIf endif
51 Preprocess_Include include
52 Preprocess_Pragma pragma
53 Preprocess_Content __macro_content__
54 Preprocess_Macro __macro__
55 Preprocess_Unsupported __unsupported__
56 Spec_Alignas alignas
71 Spec_Static static
72 Spec_ThreadLocal thread_local
73 Spec_Volatile volatile
74 Spec_Virtual virtual
75 Star *
76 Statement_End ;
77 StaticAssert static_assert
83 Type_char char
84 Type_int int
85 Type_double double
86 Type_MS_int8 __int8
87 Type_MS_int16 __int16
88 Type_MS_int32 __int32
89 Type_MS_int64 __int64
90 Type_MS_W64 _W64
91 Varadic_Argument ...
92 __Attributes_Start __attrib_start__

View File

@ -1,3 +1,25 @@
Builder Builder::open( char const* path )
{
Builder result;
FileError error = file_open_mode( & result.File, EFileMode_WRITE, path );
if ( error != EFileError_NONE )
{
log_failure( "gen::File::open - Could not open file: %s", path);
return result;
}
result.Buffer = String::make_reserve( GlobalAllocator, Builder_StrBufferReserve );
return result;
}
void Builder::pad_lines( s32 num )
{
Buffer.append( "\n" );
}
void Builder::print( Code code ) void Builder::print( Code code )
{ {
Buffer.append( code->to_string() ); Buffer.append( code->to_string() );
@ -16,21 +38,6 @@ void Builder::print_fmt( char const* fmt, ... )
Buffer.append( buf, res ); Buffer.append( buf, res );
} }
bool Builder::open( char const* path )
{
FileError error = file_open_mode( & File, EFileMode_WRITE, path );
if ( error != EFileError_NONE )
{
log_failure( "gen::File::open - Could not open file: %s", path);
return false;
}
Buffer = String::make_reserve( GlobalAllocator, Builder_StrBufferReserve );
return true;
}
void Builder::write() void Builder::write()
{ {
bool result = file_write( & File, Buffer, Buffer.length() ); bool result = file_write( & File, Buffer, Buffer.length() );

View File

@ -3,9 +3,13 @@ struct Builder
FileInfo File; FileInfo File;
String Buffer; String Buffer;
static Builder open( char const* path );
void pad_lines( s32 num );
void print( Code ); void print( Code );
void print_fmt( char const* fmt, ... ); void print_fmt( char const* fmt, ... );
bool open( char const* path );
void write(); void write();
}; };

View File

@ -1,8 +1,3 @@
#pragma once
#include "gen.hpp"
GEN_NS_BEGIN
Code scan_file( char const* path ) Code scan_file( char const* path )
{ {
FileInfo file; FileInfo file;
@ -28,6 +23,7 @@ Code scan_file( char const* path )
return untyped_str( str ); return untyped_str( str );
} }
#if 0
struct Policy struct Policy
{ {
// Nothing for now. // Nothing for now.
@ -69,5 +65,5 @@ struct Scanner
bool process_requests( Array<Receipt> out_receipts ); bool process_requests( Array<Receipt> out_receipts );
}; };
#endif
GEN_NS_END

View File

@ -2,32 +2,22 @@
#define GEN_ENFORCE_STRONG_CODE_TYPES #define GEN_ENFORCE_STRONG_CODE_TYPES
#define GEN_EXPOSE_BACKEND #define GEN_EXPOSE_BACKEND
#include "gen.cpp" #include "gen.cpp"
#include "file_processors/scanner.hpp"
#include "helpers/helper.hpp" #include "helpers/helper.hpp"
GEN_NS_BEGIN
#include "dependencies/parsing.cpp"
GEN_NS_END
#include "file_processors/builder.hpp"
#include "file_processors/builder.cpp"
#include "file_processors/scanner.hpp"
using namespace gen; using namespace gen;
bool namespace_by_default = true; constexpr char const* generation_notice =
"// This file was generated automatially by gen.bootstrap.cpp "
constexpr StrC nspace_default = txt_StrC(R"( "(See: https://github.com/Ed94/gencpp)\n\n";
#if defined(GEN_DONT_USE_NAMESPACE) && ! defined(GEN_NS_BEGIN)
# define GEN_NS_BEGIN
# define GEN_NS_END
#elif ! defined(GEN_NS_BEGIN)
# define GEN_NS_BEGIN namespace gen {
# define GEN_NS_END }
#endif
)");
constexpr StrC nspace_non_default = txt_StrC(R"(
#if ! defined(GEN_USE_NAMESPACE) && ! defined(GEN_NS_BEGIN)
# define GEN_NS_BEGIN
# define GEN_NS_END
#elif ! defined(GEN_NS_BEGIN)
# define GEN_NS_BEGIN namespace gen {
# define GEN_NS_END }
#endif
)");
int gen_main() int gen_main()
{ {
@ -39,7 +29,6 @@ int gen_main()
// gen_dep.hpp // gen_dep.hpp
{ {
Code header_start = scan_file( "dependencies/header_start.hpp" ); Code header_start = scan_file( "dependencies/header_start.hpp" );
Code nspace_macro = untyped_str( namespace_by_default ? nspace_default : nspace_non_default );
Code macros = scan_file( "dependencies/macros.hpp" ); Code macros = scan_file( "dependencies/macros.hpp" );
Code basic_types = scan_file( "dependencies/basic_types.hpp" ); Code basic_types = scan_file( "dependencies/basic_types.hpp" );
Code debug = scan_file( "dependencies/debug.hpp" ); Code debug = scan_file( "dependencies/debug.hpp" );
@ -49,120 +38,116 @@ int gen_main()
Code containers = scan_file( "dependencies/containers.hpp" ); Code containers = scan_file( "dependencies/containers.hpp" );
Code hashing = scan_file( "dependencies/hashing.hpp" ); Code hashing = scan_file( "dependencies/hashing.hpp" );
Code string = scan_file( "dependencies/string.hpp" ); Code string = scan_file( "dependencies/string.hpp" );
Code parsing = scan_file( "dependencies/parsing.hpp" ); Code file_handling = scan_file( "dependencies/file_handling.hpp" );
Code timing = scan_file( "dependencies/timing.hpp" ); Code timing = scan_file( "dependencies/timing.hpp" );
// TOOD : Make this optional
Code file_handling = scan_file( "dependencies/file_handling.hpp" );
Builder Builder
deps_header; deps_header = Builder::open("gen/gen.dep.hpp");
deps_header.open("gen/gen_dep.hpp"); deps_header.print_fmt( generation_notice );
deps_header.print_fmt("// This file is intended to be included within gen.hpp (There is no pragma diagnostic ignores)\n\n"); deps_header.print_fmt( "// This file is intended to be included within gen.hpp (There is no pragma diagnostic ignores)\n\n" );
deps_header.print_fmt("#pragma once\n\n"); deps_header.print( header_start );
deps_header.print( header_start ); deps_header.print_fmt( "GEN_NS_BEGIN\n\n" );
deps_header.print( nspace_macro );
deps_header.print_fmt( "GEN_NS_BEGIN\n\n");
deps_header.print( macros ); deps_header.print( macros );
deps_header.print( basic_types ); deps_header.print( basic_types );
deps_header.print( debug ); deps_header.print( debug );
deps_header.print( memory ); deps_header.print( memory );
deps_header.print( string_ops ); deps_header.print( string_ops );
deps_header.print( printing ); deps_header.print( printing );
deps_header.print( containers ); deps_header.print( containers );
deps_header.print( hashing ); deps_header.print( hashing );
deps_header.print( string ); deps_header.print( string );
deps_header.print( file_handling ); deps_header.print( file_handling );
deps_header.print( parsing ); deps_header.print( timing );
deps_header.print( timing );
deps_header.print_fmt( "GEN_NS_END\n\n"); deps_header.print_fmt( "GEN_NS_END\n\n" );
deps_header.write(); deps_header.write();
} }
// gen_dep.cpp // gen_dep.cpp
{ {
CodeInclude header = def_include( txt_StrC("gen_dep.hpp") ); Code src_start = scan_file( "dependencies/src_start.cpp" );
Code impl_start = scan_file( "dependencies/impl_start.cpp" ); Code debug = scan_file( "dependencies/debug.cpp" );
Code debug = scan_file( "dependencies/debug.cpp" ); Code string_ops = scan_file( "dependencies/string_ops.cpp" );
Code string_ops = scan_file( "dependencies/string_ops.cpp" ); Code printing = scan_file( "dependencies/printing.cpp" );
Code printing = scan_file( "dependencies/printing.cpp" ); Code memory = scan_file( "dependencies/memory.cpp" );
Code memory = scan_file( "dependencies/memory.cpp" ); Code hashing = scan_file( "dependencies/hashing.cpp" );
Code parsing = scan_file( "dependencies/parsing.cpp" ); Code string = scan_file( "dependencies/string.cpp" );
Code hashing = scan_file( "dependencies/hashing.cpp" ); Code file_handling = scan_file( "dependencies/file_handling.cpp" );
Code string = scan_file( "dependencies/string.cpp" ); Code timing = scan_file( "dependencies/timing.cpp" );
Code timing = scan_file( "dependencies/timing.cpp" );
Builder Builder
deps_impl; deps_impl = Builder::open( "gen/gen.dep.cpp" );
deps_impl.open("gen/gen_dep.cpp"); deps_impl.print_fmt( generation_notice );
deps_impl.print_fmt("// This file is intended to be included within gen.cpp (There is no pragma diagnostic ignores)\n\n"); deps_impl.print_fmt( "// This file is intended to be included within gen.cpp (There is no pragma diagnostic ignores)\n\n" );
deps_impl.print( impl_start ); deps_impl.print( src_start );
deps_impl.print( header ); deps_impl.print_fmt( "GEN_NS_BEGIN\n\n" );
deps_impl.print_fmt( "\nGEN_NS_BEGIN\n");
deps_impl.print( debug ); deps_impl.print( debug );
deps_impl.print( string_ops ); deps_impl.print( string_ops );
deps_impl.print( printing ); deps_impl.print( printing );
deps_impl.print( hashing ); deps_impl.print( hashing );
deps_impl.print( memory ); deps_impl.print( memory );
deps_impl.print( parsing ); deps_impl.print( string );
deps_impl.print( string ); deps_impl.print( file_handling );
deps_impl.print( timing ); deps_impl.print( timing );
deps_impl.print_fmt( "GEN_NS_END\n\n"); deps_impl.print_fmt( "GEN_NS_END\n\n" );
deps_impl.write(); deps_impl.write();
} }
// gen.hpp // gen.hpp
{ {
Code header_start = scan_file( "components/header_start.hpp" ); Code header_start = scan_file( "components/header_start.hpp" );
Code nspace_macro = untyped_str( namespace_by_default ? nspace_default : nspace_non_default );
Code types = scan_file( "components/types.hpp" ); Code types = scan_file( "components/types.hpp" );
Code data_structs = scan_file( "components/data_structures.hpp" ); Code ast = scan_file( "components/ast.hpp" );
Code ast_types = scan_file( "components/ast_types.hpp" );
Code interface = scan_file( "components/interface.hpp" ); Code interface = scan_file( "components/interface.hpp" );
Code inlines = scan_file( "components/inlines.hpp" );
Code header_end = scan_file( "components/header_end.hpp" ); Code header_end = scan_file( "components/header_end.hpp" );
CodeBody ecode = gen_ecode( "enums/ECode.csv" ); CodeBody ecode = gen_ecode ( "enums/ECode.csv" );
CodeBody eoperator = gen_eoperator( "enums/EOperator.csv" ); CodeBody eoperator = gen_eoperator ( "enums/EOperator.csv" );
CodeBody especifier = gen_especifier( "enums/ESpecifier.csv" ); CodeBody especifier = gen_especifier( "enums/ESpecifier.csv" );
CodeBody ast_inlines = gen_ast_inlines();
// TODO : Make this optional to include
Code builder = scan_file( "file_processors/builder.hpp" );
Builder Builder
header; header = Builder::open( "gen/gen.hpp" );
header.open( "gen/gen.hpp" ); header.print_fmt( generation_notice );
header.print_fmt("#pragma once\n\n"); header.print_fmt( "#pragma once\n\n" );
header.print( push_ignores ); header.print( push_ignores );
header.print( header_start ); header.print( header_start );
header.print( nspace_macro ); header.print_fmt( "GEN_NS_BEGIN\n\n" );
header.print_fmt( "GEN_NS_BEGIN\n\n");
header.print_fmt("#pragma region Types\n\n"); header.print_fmt( "#pragma region Types\n\n" );
header.print( types ); header.print( types );
header.print( ecode ); header.print( ecode );
header.print( eoperator ); header.print( eoperator );
header.print( especifier ); header.print( especifier );
header.print_fmt("#pragma endregion Types\n\n"); header.print_fmt( "#pragma endregion Types\n\n" );
header.print( data_structs ); header.print_fmt( "#pragma region AST\n\n" );
header.print( interface ); header.print( ast );
header.print( header_end ); header.print( ast_types );
header.print_fmt( "#pragma endregion AST\n\n" );
header.print( builder ); header.print( interface );
header.print_fmt( "GEN_NS_END\n\n"); header.print_fmt( "#pragma region Inlines\n\n" );
header.print( pop_ignores ); header.print( inlines );
header.print( ast_inlines );
header.print_fmt( "#pragma endregion Inlines\n\n" );
header.print( header_end );
header.print_fmt( "GEN_NS_END\n\n" );
header.print( pop_ignores );
header.write(); header.write();
} }
// gen.cpp // gen.cpp
{ {
Code impl_start = scan_file( "components/impl_start.cpp" ); Code src_start = scan_file( "components/src_start.cpp" );
CodeInclude header = def_include( txt_StrC("gen.hpp") ); Code static_data = scan_file( "components/static_data.cpp" );
Code data = scan_file( "components/static_data.cpp" );
Code ast_case_macros = scan_file( "components/ast_case_macros.cpp" ); Code ast_case_macros = scan_file( "components/ast_case_macros.cpp" );
Code ast = scan_file( "components/ast.cpp" ); Code ast = scan_file( "components/ast.cpp" );
Code interface = scan_file( "components/interface.cpp" ); Code interface = scan_file( "components/interface.cpp" );
@ -170,33 +155,98 @@ int gen_main()
Code parsing = scan_file( "components/interface.parsing.cpp" ); Code parsing = scan_file( "components/interface.parsing.cpp" );
Code untyped = scan_file( "components/untyped.cpp" ); Code untyped = scan_file( "components/untyped.cpp" );
CodeBody etoktype = gen_etoktype( "enums/ETokType.csv", "enums/AttributeTokens.csv" ); CodeBody etoktype = gen_etoktype( "enums/ETokType.csv", "enums/AttributeTokens.csv" );
CodeNamespace parser_nspace = def_namespace( name(Parser), def_namespace_body( args(etoktype)) ); CodeNS parser_nspace = def_namespace( name(Parser), def_namespace_body( args(etoktype)) );
// TODO : Make this optional to include Builder
src = Builder::open( "gen/gen.cpp" );
src.print_fmt( generation_notice );
src.print( push_ignores );
src.print( src_start );
src.print_fmt( "\nGEN_NS_BEGIN\n\n");
src.print( static_data );
src.print_fmt( "#pragma region AST\n\n" );
src.print( ast_case_macros );
src.print( ast );
src.print_fmt( "#pragma endregion AST\n\n" );
src.print_fmt( "#pragma region Interface\n\n" );
src.print( interface );
src.print( upfront );
src.print_fmt( "#pragma region Parsing\n\n" );
src.print( parser_nspace );
src.print( parsing );
src.print( untyped );
src.print_fmt( "#pragma endregion Parsing\n\n" );
src.print_fmt( "#pragma endregion Interface\n\n" );
src.print_fmt( "GEN_NS_END\n\n");
src.print( pop_ignores );
src.write();
}
// gen_builder.hpp
{
Code builder = scan_file( "file_processors/builder.hpp" );
Builder
header = Builder::open( "gen/gen.builder.hpp" );
header.print_fmt( generation_notice );
header.print_fmt( "#pragma once\n\n" );
header.print( def_include( txt_StrC("gen.hpp") ));
header.print_fmt( "\nGEN_NS_BEGIN\n\n" );
header.print( builder );
header.print_fmt( "\nGEN_NS_END\n\n" );
header.write();
}
// gen_builder.cpp
{
Code builder = scan_file( "file_processors/builder.cpp" ); Code builder = scan_file( "file_processors/builder.cpp" );
Builder Builder
impl; src = Builder::open( "gen/gen.builder.cpp" );
impl.open( "gen/gen.cpp" ); src.print_fmt( generation_notice );
impl.print( push_ignores ); src.print( def_include( txt_StrC("gen.builder.hpp") ) );
impl.print( impl_start ); src.print_fmt( "\nGEN_NS_BEGIN\n\n" );
impl.print( header ); src.print( builder );
impl.print_fmt( "\nGEN_NS_BEGIN\n\n"); src.print_fmt( "\nGEN_NS_END\n\n" );
src.write();
}
impl.print( data ); // gen_scanner.hpp
impl.print( ast_case_macros ); {
impl.print( ast ); Code parsing = scan_file( "dependencies/parsing.hpp" );
impl.print( interface ); Code scanner = scan_file( "file_processors/scanner.hpp" );
impl.print( upfront );
impl.print( parser_nspace );
impl.print( parsing );
impl.print( untyped );
impl.print( builder ); Builder
impl.print_fmt( "GEN_NS_END\n\n"); header = Builder::open( "gen/gen.scanner.hpp" );
impl.print( pop_ignores ); header.print_fmt( generation_notice );
impl.write(); header.print_fmt( "#pragma once\n\n" );
header.print( def_include( txt_StrC("gen.hpp") ) );
header.print_fmt( "\nGEN_NS_BEGIN\n\n" );
header.print( parsing );
header.print( scanner );
header.print_fmt( "\nGEN_NS_END\n\n" );
header.write();
}
// gen_scanner.cpp
{
Code parsing = scan_file( "dependencies/parsing.cpp" );
// Code scanner = scan_file( "file_processors/scanner.cpp" );
Builder
src = Builder::open( "gen/gen.scanner.cpp" );
src.print_fmt( generation_notice );
src.print( def_include( txt_StrC("gen.scanner.hpp") ) );
src.print_fmt( "\nGEN_NS_BEGIN\n\n" );
src.print( parsing );
// src.print( scanner );
src.print_fmt( "\nGEN_NS_END\n\n" );
src.write();
} }
gen::deinit(); gen::deinit();

View File

@ -6,14 +6,14 @@
# error Gen.hpp : GEN_TIME not defined # error Gen.hpp : GEN_TIME not defined
#endif #endif
#include "gen.hpp"
//! If its desired to roll your own dependencies, define GEN_ROLL_OWN_DEPENDENCIES before including this file. //! If its desired to roll your own dependencies, define GEN_ROLL_OWN_DEPENDENCIES before including this file.
//! Dependencies are derived from the c-zpl library: https://github.com/zpl-c/zpl //! Dependencies are derived from the c-zpl library: https://github.com/zpl-c/zpl
#ifndef GEN_ROLL_OWN_DEPENDENCIES #ifndef GEN_ROLL_OWN_DEPENDENCIES
# include "gen.dep.cpp" # include "gen.dep.cpp"
#endif #endif
#include "gen.hpp"
GEN_NS_BEGIN GEN_NS_BEGIN
#include "components/static_data.cpp" #include "components/static_data.cpp"
@ -23,12 +23,10 @@ GEN_NS_BEGIN
#include "components/interface.cpp" #include "components/interface.cpp"
#include "components/interface.upfront.cpp" #include "components/interface.upfront.cpp"
#include "components/etoktype.cpp" #include "components/temp/etoktype.cpp"
#include "components/interface.parsing.cpp" #include "components/interface.parsing.cpp"
#include "components/untyped.cpp" #include "components/untyped.cpp"
#include "file_processors/builder.cpp"
GEN_NS_END GEN_NS_END
#include "helpers/pop_ignores.inline.hpp" #include "helpers/pop_ignores.inline.hpp"

View File

@ -1,17 +1,19 @@
// This file is intended to be included within gen.cpp (There is no pragma diagnostic ignores) // This file is intended to be included within gen.cpp (There is no pragma diagnostic ignores)
#include "gen.dep.hpp" #include "gen.dep.hpp"
#include "dependencies/impl_start.cpp" #include "dependencies/src_start.cpp"
GEN_NS_BEGIN GEN_NS_BEGIN
#include "dependencies/debug.cpp" #include "dependencies/debug.cpp"
#include "dependencies/string_ops.cpp" #include "dependencies/string_ops.cpp"
#include "dependencies/printing.cpp" #include "dependencies/printing.cpp"
#include "dependencies/memory.cpp" #include "dependencies/memory.cpp"
#include "dependencies/parsing.cpp"
#include "dependencies/hashing.cpp" #include "dependencies/hashing.cpp"
#include "dependencies/string.cpp" #include "dependencies/string.cpp"
#include "dependencies/timing.cpp" #include "dependencies/timing.cpp"
#include "dependencies/file_handling.cpp" #include "dependencies/file_handling.cpp"

View File

@ -3,26 +3,20 @@
#include "dependencies/header_start.hpp" #include "dependencies/header_start.hpp"
#ifdef GEN_DONT_USE_NAMESPACE
# define GEN_NS_BEGIN
# define GEN_NS_END
#else
# define GEN_NS_BEGIN namespace gen {
# define GEN_NS_END }
#endif
GEN_NS_BEGIN GEN_NS_BEGIN
#include "dependencies/macros.hpp" #include "dependencies/macros.hpp"
#include "dependencies/basic_types.hpp" #include "dependencies/basic_types.hpp"
#include "dependencies/debug.hpp" #include "dependencies/debug.hpp"
#include "dependencies/memory.hpp" #include "dependencies/memory.hpp"
#include "dependencies/string_ops.hpp" #include "dependencies/string_ops.hpp"
#include "dependencies/printing.hpp" #include "dependencies/printing.hpp"
#include "dependencies/containers.hpp" #include "dependencies/containers.hpp"
#include "dependencies/hashing.hpp" #include "dependencies/hashing.hpp"
#include "dependencies/string.hpp" #include "dependencies/string.hpp"
#include "dependencies/parsing.hpp"
#include "dependencies/timing.hpp" #include "dependencies/timing.hpp"
#include "dependencies/file_handling.hpp" #include "dependencies/file_handling.hpp"

View File

@ -11,25 +11,21 @@
#include "helpers/push_ignores.inline.hpp" #include "helpers/push_ignores.inline.hpp"
#include "components/header_start.hpp" #include "components/header_start.hpp"
#ifdef GEN_DONT_USE_NAMESPACE
# define GEN_NS_BEGIN
# define GEN_NS_END
#else
# define GEN_NS_BEGIN namespace gen {
# define GEN_NS_END }
#endif
GEN_NS_BEGIN GEN_NS_BEGIN
#include "components/types.hpp" #include "components/types.hpp"
#include "components/ecode.hpp" #include "components/temp/ecode.hpp"
#include "components/eoperator.hpp" #include "components/temp/eoperator.hpp"
#include "components/especifier.hpp" #include "components/temp/especifier.hpp"
#include "components/data_structures.hpp"
#include "components/interface.hpp"
#include "components/header_end.hpp"
#include "file_processors/builder.hpp" #include "components/ast.hpp"
#include "components/ast_types.hpp"
#include "components/interface.hpp"
#include "components/inlines.hpp"
#include "components/temp/ast_inlines.hpp"
#include "components/header_end.hpp"
GEN_NS_END GEN_NS_END

View File

@ -2,6 +2,10 @@
#include "gen.hpp" #include "gen.hpp"
GEN_NS_BEGIN
#include "dependencies/parsing.hpp"
GEN_NS_END
using namespace gen; using namespace gen;
CodeBody gen_ecode( char const* path ) CodeBody gen_ecode( char const* path )
@ -49,7 +53,7 @@ CodeBody gen_ecode( char const* path )
))); )));
#pragma pop_macro( "local_persist" ) #pragma pop_macro( "local_persist" )
CodeNamespace nspace = def_namespace( name(ECode), def_namespace_body( args( enum_code, to_str ) ) ); CodeNS nspace = def_namespace( name(ECode), def_namespace_body( args( enum_code, to_str ) ) );
CodeUsing code_t = def_using( name(CodeT), def_type( name(ECode::Type) ) ); CodeUsing code_t = def_using( name(CodeT), def_type( name(ECode::Type) ) );
return def_global_body( args( nspace, code_t ) ); return def_global_body( args( nspace, code_t ) );
@ -103,7 +107,7 @@ CodeBody gen_eoperator( char const* path )
))); )));
#pragma pop_macro( "local_persist" ) #pragma pop_macro( "local_persist" )
CodeNamespace nspace = def_namespace( name(EOperator), def_namespace_body( args( enum_code, to_str ) ) ); CodeNS nspace = def_namespace( name(EOperator), def_namespace_body( args( enum_code, to_str ) ) );
CodeUsing operator_t = def_using( name(OperatorT), def_type( name(EOperator::Type) ) ); CodeUsing operator_t = def_using( name(OperatorT), def_type( name(EOperator::Type) ) );
@ -199,7 +203,7 @@ CodeBody gen_especifier( char const* path )
#pragma pop_macro( "do_once_start" ) #pragma pop_macro( "do_once_start" )
#pragma pop_macro( "do_once_end" ) #pragma pop_macro( "do_once_end" )
CodeNamespace nspace = def_namespace( name(ESpecifier), def_namespace_body( args( enum_code, is_trailing, to_str, to_type ) ) ); CodeNS nspace = def_namespace( name(ESpecifier), def_namespace_body( args( enum_code, is_trailing, to_str, to_type ) ) );
CodeUsing specifier_t = def_using( name(SpecifierT), def_type( name(ESpecifier::Type) ) ); CodeUsing specifier_t = def_using( name(SpecifierT), def_type( name(ESpecifier::Type) ) );
@ -208,7 +212,7 @@ CodeBody gen_especifier( char const* path )
CodeBody gen_etoktype( char const* etok_path, char const* attr_path ) CodeBody gen_etoktype( char const* etok_path, char const* attr_path )
{ {
char scratch_mem[kilobytes(64)]; char scratch_mem[kilobytes(16)];
Arena scratch = Arena::init_from_memory( scratch_mem, sizeof(scratch_mem) ); Arena scratch = Arena::init_from_memory( scratch_mem, sizeof(scratch_mem) );
FileContents enum_content = file_read_contents( scratch, zero_terminate, etok_path ); FileContents enum_content = file_read_contents( scratch, zero_terminate, etok_path );
@ -226,10 +230,11 @@ CodeBody gen_etoktype( char const* etok_path, char const* attr_path )
Array<ADT_Node> attribute_strs = csv_attr_nodes.nodes[0].nodes; Array<ADT_Node> attribute_strs = csv_attr_nodes.nodes[0].nodes;
Array<ADT_Node> attribute_str_strs = csv_attr_nodes.nodes[1].nodes; Array<ADT_Node> attribute_str_strs = csv_attr_nodes.nodes[1].nodes;
String enum_entries = String::make_reserve( GlobalAllocator, kilobytes(2) ); String enum_entries = String::make_reserve( GlobalAllocator, kilobytes(2) );
String to_str_entries = String::make_reserve( GlobalAllocator, kilobytes(4) ); String to_str_entries = String::make_reserve( GlobalAllocator, kilobytes(4) );
String attribute_entries = String::make_reserve( GlobalAllocator, kilobytes(2) ); String attribute_entries = String::make_reserve( GlobalAllocator, kilobytes(2) );
String to_str_attributes = String::make_reserve( GlobalAllocator, kilobytes(4) ); String to_str_attributes = String::make_reserve( GlobalAllocator, kilobytes(4) );
String attribute_define_entries = String::make_reserve( GlobalAllocator, kilobytes(4) );
for (uw idx = 0; idx < enum_strs.num(); idx++) for (uw idx = 0; idx < enum_strs.num(); idx++)
{ {
@ -247,8 +252,19 @@ CodeBody gen_etoktype( char const* etok_path, char const* attr_path )
attribute_entries.append_fmt( "%s,\n", attribute_str ); attribute_entries.append_fmt( "%s,\n", attribute_str );
to_str_attributes.append_fmt( "{ sizeof(\"%s\"), \"%s\" },\n", entry_to_str, entry_to_str); to_str_attributes.append_fmt( "{ sizeof(\"%s\"), \"%s\" },\n", entry_to_str, entry_to_str);
attribute_define_entries.append_fmt( "Entry( %s, %s )", attribute_str, entry_to_str );
if ( idx < attribute_strs.num() - 1 )
attribute_define_entries.append( " \\\n");
else
attribute_define_entries.append( "\n");
} }
#pragma push_macro( "GEN_DEFINE_ATTRIBUTE_TOKENS" )
#undef GEN_DEFINE_ATTRIBUTE_TOKENS
CodeDefine attribute_entires_def = def_define( name(GEN_DEFINE_ATTRIBUTE_TOKENS), attribute_define_entries );
#pragma pop_macro( "GEN_DEFINE_ATTRIBUTE_TOKENS" )
CodeEnum enum_code = parse_enum(token_fmt("entries", (StrC)enum_entries, "attribute_toks", (StrC)attribute_entries, stringize( CodeEnum enum_code = parse_enum(token_fmt("entries", (StrC)enum_entries, "attribute_toks", (StrC)attribute_entries, stringize(
enum Type : u32 enum Type : u32
{ {
@ -308,8 +324,280 @@ CodeBody gen_etoktype( char const* etok_path, char const* attr_path )
#pragma pop_macro( "do_once_start" ) #pragma pop_macro( "do_once_start" )
#pragma pop_macro( "do_once_end" ) #pragma pop_macro( "do_once_end" )
CodeNamespace nspace = def_namespace( name(ETokType), def_namespace_body( args( enum_code, to_str, to_type ) ) ); CodeNS nspace = def_namespace( name(ETokType), def_namespace_body( args( attribute_entires_def, enum_code, to_str, to_type ) ) );
CodeUsing td_toktype = def_using( name(TokType), def_type( name(ETokType::Type) ) ); CodeUsing td_toktype = def_using( name(TokType), def_type( name(ETokType::Type) ) );
return def_global_body( args( nspace, td_toktype ) ); return def_global_body( args( nspace, td_toktype ) );
} }
CodeBody gen_ast_inlines()
{
#pragma push_macro("rcast")
#undef rcast
char const* code_impl_tmpl = stringize(
\n
char const* <typename>::debug_str()
{
if ( ast == nullptr )
return "Code::debug_str: AST is null!";
return rcast(AST*, ast)->debug_str();
}
Code <typename>::duplicate()
{
if ( ast == nullptr )
{
log_failure("Code::duplicate: Cannot duplicate code, AST is null!");
return Code::Invalid;
}
return { rcast(AST*, ast)->duplicate() };
}
bool <typename>::is_equal( Code other )
{
if ( ast == nullptr || other.ast == nullptr )
{
log_failure("Code::is_equal: Cannot compare code, AST is null!");
return false;
}
return rcast(AST*, ast)->is_equal( other.ast );
}
bool <typename>::is_valid()
{
return (AST*) ast != nullptr && rcast( AST*, ast)->Type != CodeT::Invalid;
}
void <typename>::set_global()
{
if ( ast == nullptr )
{
log_failure("Code::set_global: Cannot set code as global, AST is null!");
return;
}
rcast(AST*, ast)->Parent = Code::Global.ast;
}
String <typename>::to_string()
{
if ( ast == nullptr )
{
log_failure("Code::to_string: Cannot convert code to string, AST is null!");
return { nullptr };
}
return rcast(AST*, ast)->to_string();
}
<typename>& <typename>::operator =( Code other )
{
if ( other.ast && other->Parent )
{
ast = rcast( decltype(ast), other.ast->duplicate() );
rcast( AST*, ast)->Parent = nullptr;
}
ast = rcast( decltype(ast), other.ast );
return *this;
}
bool <typename>::operator ==( Code other )
{
return (AST*) ast == other.ast;
}
bool <typename>::operator !=( Code other )
{
return (AST*) ast != other.ast;
}
<typename>::operator bool()
{
return ast != nullptr;
}
);
char const* codetype_impl_tmpl = stringize(
AST* Code<typename>::raw()
{
return rcast( AST*, ast );
}
Code<typename>::operator Code()
{
return *rcast( Code*, this );
}
AST_<typename>* Code<typename>::operator->()
{
if ( ast == nullptr )
{
log_failure( "Attempt to dereference a nullptr!" );
return nullptr;
}
return ast;
}
\n
);
CodeBody impl_code = parse_global_body( token_fmt( "typename", StrC name(Code), code_impl_tmpl ));
CodeBody impl_code_body = parse_global_body( token_fmt( "typename", StrC name(CodeBody), code_impl_tmpl ));
CodeBody impl_code_attr = parse_global_body( token_fmt( "typename", StrC name(CodeAttributes), code_impl_tmpl ));
CodeBody impl_code_cmt = parse_global_body( token_fmt( "typename", StrC name(CodeComment), code_impl_tmpl ));
CodeBody impl_code_constr = parse_global_body( token_fmt( "typename", StrC name(CodeConstructor), code_impl_tmpl ));
CodeBody impl_code_class = parse_global_body( token_fmt( "typename", StrC name(CodeClass), code_impl_tmpl ));
CodeBody impl_code_define = parse_global_body( token_fmt( "typename", StrC name(CodeDefine), code_impl_tmpl ));
CodeBody impl_code_destruct = parse_global_body( token_fmt( "typename", StrC name(CodeDestructor), code_impl_tmpl ));
CodeBody impl_code_enum = parse_global_body( token_fmt( "typename", StrC name(CodeEnum), code_impl_tmpl ));
CodeBody impl_code_exec = parse_global_body( token_fmt( "typename", StrC name(CodeExec), code_impl_tmpl ));
CodeBody impl_code_extern = parse_global_body( token_fmt( "typename", StrC name(CodeExtern), code_impl_tmpl ));
CodeBody impl_code_include = parse_global_body( token_fmt( "typename", StrC name(CodeInclude), code_impl_tmpl ));
CodeBody impl_code_friend = parse_global_body( token_fmt( "typename", StrC name(CodeFriend), code_impl_tmpl ));
CodeBody impl_code_fn = parse_global_body( token_fmt( "typename", StrC name(CodeFn), code_impl_tmpl ));
CodeBody impl_code_module = parse_global_body( token_fmt( "typename", StrC name(CodeModule), code_impl_tmpl ));
CodeBody impl_code_ns = parse_global_body( token_fmt( "typename", StrC name(CodeNS), code_impl_tmpl ));
CodeBody impl_code_op = parse_global_body( token_fmt( "typename", StrC name(CodeOperator), code_impl_tmpl ));
CodeBody impl_code_opcast = parse_global_body( token_fmt( "typename", StrC name(CodeOpCast), code_impl_tmpl ));
CodeBody impl_code_param = parse_global_body( token_fmt( "typename", StrC name(CodeParam), code_impl_tmpl ));
CodeBody impl_code_pragma = parse_global_body( token_fmt( "typename", StrC name(CodePragma), code_impl_tmpl ));
CodeBody impl_code_precond = parse_global_body( token_fmt( "typename", StrC name(CodePreprocessCond), code_impl_tmpl ));
CodeBody impl_code_specs = parse_global_body( token_fmt( "typename", StrC name(CodeSpecifiers), code_impl_tmpl ));
CodeBody impl_code_struct = parse_global_body( token_fmt( "typename", StrC name(CodeStruct), code_impl_tmpl ));
CodeBody impl_code_tmpl = parse_global_body( token_fmt( "typename", StrC name(CodeTemplate), code_impl_tmpl ));
CodeBody impl_code_type = parse_global_body( token_fmt( "typename", StrC name(CodeType), code_impl_tmpl ));
CodeBody impl_code_typedef = parse_global_body( token_fmt( "typename", StrC name(CodeTypedef), code_impl_tmpl ));
CodeBody impl_code_union = parse_global_body( token_fmt( "typename", StrC name(CodeUnion), code_impl_tmpl ));
CodeBody impl_code_using = parse_global_body( token_fmt( "typename", StrC name(CodeUsing), code_impl_tmpl ));
CodeBody impl_code_var = parse_global_body( token_fmt( "typename", StrC name(CodeVar), code_impl_tmpl ));
impl_code_attr. append( parse_global_body( token_fmt( "typename", StrC name(Attributes), codetype_impl_tmpl )));
impl_code_cmt. append( parse_global_body( token_fmt( "typename", StrC name(Comment), codetype_impl_tmpl )));
impl_code_constr. append( parse_global_body( token_fmt( "typename", StrC name(Constructor), codetype_impl_tmpl )));
impl_code_define. append( parse_global_body( token_fmt( "typename", StrC name(Define), codetype_impl_tmpl )));
impl_code_destruct.append( parse_global_body( token_fmt( "typename", StrC name(Destructor), codetype_impl_tmpl )));
impl_code_enum. append( parse_global_body( token_fmt( "typename", StrC name(Enum), codetype_impl_tmpl )));
impl_code_exec. append( parse_global_body( token_fmt( "typename", StrC name(Exec), codetype_impl_tmpl )));
impl_code_extern. append( parse_global_body( token_fmt( "typename", StrC name(Extern), codetype_impl_tmpl )));
impl_code_include. append( parse_global_body( token_fmt( "typename", StrC name(Include), codetype_impl_tmpl )));
impl_code_friend. append( parse_global_body( token_fmt( "typename", StrC name(Friend), codetype_impl_tmpl )));
impl_code_fn. append( parse_global_body( token_fmt( "typename", StrC name(Fn), codetype_impl_tmpl )));
impl_code_module. append( parse_global_body( token_fmt( "typename", StrC name(Module), codetype_impl_tmpl )));
impl_code_ns. append( parse_global_body( token_fmt( "typename", StrC name(NS), codetype_impl_tmpl )));
impl_code_op. append( parse_global_body( token_fmt( "typename", StrC name(Operator), codetype_impl_tmpl )));
impl_code_opcast. append( parse_global_body( token_fmt( "typename", StrC name(OpCast), codetype_impl_tmpl )));
impl_code_pragma . append( parse_global_body( token_fmt( "typename", StrC name(Pragma), codetype_impl_tmpl )));
impl_code_precond. append( parse_global_body( token_fmt( "typename", StrC name(PreprocessCond), codetype_impl_tmpl )));
impl_code_tmpl. append( parse_global_body( token_fmt( "typename", StrC name(Template), codetype_impl_tmpl )));
impl_code_type. append( parse_global_body( token_fmt( "typename", StrC name(Type), codetype_impl_tmpl )));
impl_code_typedef. append( parse_global_body( token_fmt( "typename", StrC name(Typedef), codetype_impl_tmpl )));
impl_code_union. append( parse_global_body( token_fmt( "typename", StrC name(Union), codetype_impl_tmpl )));
impl_code_using. append( parse_global_body( token_fmt( "typename", StrC name(Using), codetype_impl_tmpl )));
impl_code_var. append( parse_global_body( token_fmt( "typename", StrC name(Var), codetype_impl_tmpl )));
char const* cast_tmpl = stringize(
AST::operator Code<typename>()
{
return { rcast( AST_<typename>*, this ) };
}
Code::operator Code<typename>() const
{
return { (AST_<typename>*) ast };
}
);
CodeBody impl_cast_body = parse_global_body( token_fmt( "typename", StrC name(Body), cast_tmpl ));
CodeBody impl_cast_attribute = parse_global_body( token_fmt( "typename", StrC name(Attributes), cast_tmpl ));
CodeBody impl_cast_cmt = parse_global_body( token_fmt( "typename", StrC name(Comment), cast_tmpl ));
CodeBody impl_cast_constr = parse_global_body( token_fmt( "typename", StrC name(Constructor), cast_tmpl ));
CodeBody impl_cast_class = parse_global_body( token_fmt( "typename", StrC name(Class), cast_tmpl ));
CodeBody impl_cast_define = parse_global_body( token_fmt( "typename", StrC name(Define), cast_tmpl ));
CodeBody impl_cast_destruct = parse_global_body( token_fmt( "typename", StrC name(Destructor), cast_tmpl ));
CodeBody impl_cast_enum = parse_global_body( token_fmt( "typename", StrC name(Enum), cast_tmpl ));
CodeBody impl_cast_exec = parse_global_body( token_fmt( "typename", StrC name(Exec), cast_tmpl ));
CodeBody impl_cast_extern = parse_global_body( token_fmt( "typename", StrC name(Extern), cast_tmpl ));
CodeBody impl_cast_friend = parse_global_body( token_fmt( "typename", StrC name(Friend), cast_tmpl ));
CodeBody impl_cast_fn = parse_global_body( token_fmt( "typename", StrC name(Fn), cast_tmpl ));
CodeBody impl_cast_include = parse_global_body( token_fmt( "typename", StrC name(Include), cast_tmpl ));
CodeBody impl_cast_module = parse_global_body( token_fmt( "typename", StrC name(Module), cast_tmpl ));
CodeBody impl_cast_ns = parse_global_body( token_fmt( "typename", StrC name(NS), cast_tmpl ));
CodeBody impl_cast_op = parse_global_body( token_fmt( "typename", StrC name(Operator), cast_tmpl ));
CodeBody impl_cast_opcast = parse_global_body( token_fmt( "typename", StrC name(OpCast), cast_tmpl ));
CodeBody impl_cast_param = parse_global_body( token_fmt( "typename", StrC name(Param), cast_tmpl ));
CodeBody impl_cast_pragma = parse_global_body( token_fmt( "typename", StrC name(Pragma), cast_tmpl ));
CodeBody impl_cast_precond = parse_global_body( token_fmt( "typename", StrC name(PreprocessCond), cast_tmpl ));
CodeBody impl_cast_specs = parse_global_body( token_fmt( "typename", StrC name(Specifiers), cast_tmpl ));
CodeBody impl_cast_struct = parse_global_body( token_fmt( "typename", StrC name(Struct), cast_tmpl ));
CodeBody impl_cast_tmpl = parse_global_body( token_fmt( "typename", StrC name(Template), cast_tmpl ));
CodeBody impl_cast_type = parse_global_body( token_fmt( "typename", StrC name(Type), cast_tmpl ));
CodeBody impl_cast_typedef = parse_global_body( token_fmt( "typename", StrC name(Typedef), cast_tmpl ));
CodeBody impl_cast_union = parse_global_body( token_fmt( "typename", StrC name(Union), cast_tmpl ));
CodeBody impl_cast_using = parse_global_body( token_fmt( "typename", StrC name(Using), cast_tmpl ));
CodeBody impl_cast_var = parse_global_body( token_fmt( "typename", StrC name(Var), cast_tmpl ));
CodeBody result = def_global_body( args(
def_pragma( txt_StrC("region generated code inline implementation")),
fmt_newline,
impl_code,
impl_code_body,
impl_code_attr,
impl_code_cmt,
impl_code_constr,
impl_code_class,
impl_code_define,
impl_code_destruct,
impl_code_enum,
impl_code_exec,
impl_code_extern,
impl_code_friend,
impl_code_fn,
impl_code_include,
impl_code_module,
impl_code_ns,
impl_code_op,
impl_code_opcast,
impl_code_param,
impl_code_pragma,
impl_code_precond,
impl_code_specs,
impl_code_struct,
impl_code_tmpl,
impl_code_type,
impl_code_typedef,
impl_code_union,
impl_code_using,
impl_code_var,
fmt_newline,
def_pragma( txt_StrC("endregion generated code inline implementation")),
fmt_newline,
def_pragma( txt_StrC("region generated AST/Code cast implementation")),
fmt_newline,
impl_cast_body,
impl_cast_attribute,
impl_cast_cmt,
impl_cast_constr,
impl_cast_class,
impl_cast_define,
impl_cast_destruct,
impl_cast_enum,
impl_cast_exec,
impl_cast_extern,
impl_cast_friend,
impl_cast_fn,
impl_cast_include,
impl_cast_module,
impl_cast_ns,
impl_cast_op,
impl_cast_opcast,
impl_cast_param,
impl_cast_pragma,
impl_cast_precond,
impl_cast_specs,
impl_cast_struct,
impl_cast_tmpl,
impl_cast_type,
impl_cast_typedef,
impl_cast_union,
impl_cast_using,
impl_cast_var,
fmt_newline,
def_pragma( txt_StrC("endregion generated AST/Code cast implementation")),
fmt_newline
));
return result;
#pragma pop_macro("rcast")
}

View File

@ -6,16 +6,22 @@ AlignAfterOpenBracket: BlockIndent
AlignArrayOfStructures: Left AlignArrayOfStructures: Left
AlignConsecutiveAssignments: AlignConsecutiveAssignments:
Enabled: true Enabled: true
AcrossEmptyLines: false AcrossEmptyLines: true
AcrossComments: true AcrossComments: false
AlignCompound: true AlignCompound: true
PadOperators: true PadOperators: true
AlignConsecutiveBitFields: AcrossComments AlignConsecutiveBitFields:
AlignConsecutiveDeclarations: AcrossComments Enabled: true
AcrossEmptyLines: true
AcrossComments: false
AlignConsecutiveDeclarations:
Enabled: true
AcrossEmptyLines: false
AcrossComments: false
AlignConsecutiveMacros: AlignConsecutiveMacros:
Enabled: true Enabled: true
AcrossEmptyLines: true AcrossEmptyLines: true
AcrossComments: true AcrossComments: false
AlignEscapedNewlines: Left AlignEscapedNewlines: Left
AlignOperands: DontAlign AlignOperands: DontAlign
@ -62,7 +68,7 @@ BraceWrapping:
BeforeWhile: false BeforeWhile: false
BreakAfterAttributes: Always BreakAfterAttributes: Always
# BreakArrays: false BreakArrays: true
# BreakBeforeInlineASMColon: OnlyMultiline # BreakBeforeInlineASMColon: OnlyMultiline
BreakBeforeBinaryOperators: NonAssignment BreakBeforeBinaryOperators: NonAssignment
BreakBeforeBraces: Allman BreakBeforeBraces: Allman
@ -73,7 +79,7 @@ BreakBeforeTernaryOperators: true
BreakConstructorInitializers: BeforeComma BreakConstructorInitializers: BeforeComma
BreakStringLiterals: true BreakStringLiterals: true
ColumnLimit: 120 ColumnLimit: 160
CompactNamespaces: true CompactNamespaces: true
@ -92,12 +98,11 @@ FixNamespaceComments: true
IncludeBlocks: Preserve IncludeBlocks: Preserve
IndentCaseBlocks: false IndentCaseBlocks: false
IndentCaseLabels: true IndentCaseLabels: true
IndentExternBlock: AfterExternBlock IndentExternBlock: AfterExternBlock
IndentGotoLabels: true IndentGotoLabels: true
IndentPPDirectives: AfterHash IndentPPDirectives: None
IndentRequires: true IndentRequires: true
IndentWidth: 4 IndentWidth: 4
IndentWrappedFunctionNames: true IndentWrappedFunctionNames: true
@ -128,7 +133,7 @@ SeparateDefinitionBlocks: Always
ShortNamespaceLines: 40 ShortNamespaceLines: 40
SortIncludes: false SortIncludes: false
SortUsingDeclarations: true SortUsingDeclarations: false
SpaceAfterCStyleCast: false SpaceAfterCStyleCast: false
SpaceAfterLogicalNot: true SpaceAfterLogicalNot: true

View File

@ -22,7 +22,7 @@ write-host "`n`nBuilding gencpp bootstrap`n"
if ( -not( Test-Path $path_project_build) ) if ( -not( Test-Path $path_project_build) )
{ {
# Generate build files for meta-program # Generate build files for bootstrap
Push-Location $path_project Push-Location $path_project
$args_meson = @() $args_meson = @()
$args_meson += "setup" $args_meson += "setup"
@ -32,7 +32,7 @@ Push-Location $path_project
Pop-Location Pop-Location
} }
# Compile meta-program # Compile bootstrap
Push-Location $path_root Push-Location $path_root
$args_ninja = @() $args_ninja = @()
$args_ninja += "-C" $args_ninja += "-C"
@ -42,29 +42,37 @@ Push-Location $path_root
Pop-Location Pop-Location
Push-location $path_project Push-location $path_project
if ( -not(Test-Path($path_project_gen) )) { if ( -not(Test-Path($path_project_gen) )) {
New-Item -ItemType Directory -Path $path_project_gen New-Item -ItemType Directory -Path $path_project_gen
} }
# Run meta-program # Run bootstrap
$gencpp_bootstrap = Join-Path $path_project_build gencpp_bootstrap.exe $gencpp_bootstrap = Join-Path $path_project_build gencpp_bootstrap.exe
Write-Host `nRunning gencpp bootstrap... Write-Host `nRunning gencpp bootstrap...
& $gencpp_bootstrap & $gencpp_bootstrap
# Format generated files # Format generated gencpp
Write-Host `nBeginning format... Write-Host `nBeginning format...
$formatParams = @( $formatParams = @(
'-i' # In-place '-i' # In-place
'-style=file:../scripts/.clang-format' '-style=file:../scripts/.clang-format'
'-verbose' '-verbose'
) )
$include = @('gen.hpp', 'gen.cpp', 'gen_dep.hpp', 'gen_dep.cpp') $include = @(
$exclude = $null 'gen.hpp', 'gen.cpp',
'gen.dep.hpp', 'gen.dep.cpp',
'gen.builder.hpp', 'gen.builder.cpp'
'gen.scanner.hpp', 'gen.scanner.cpp'
)
$exclude = $null
$targetFiles = @(Get-ChildItem -Recurse -Path $path_project_gen -Include $include -Exclude $exclude | Select-Object -ExpandProperty FullName) $targetFiles = @(Get-ChildItem -Recurse -Path $path_project_gen -Include $include -Exclude $exclude | Select-Object -ExpandProperty FullName)
clang-format $formatParams $targetFiles clang-format $formatParams $targetFiles
Write-Host "`nFormatting complete" Write-Host "`nFormatting complete"
Pop-Location Pop-Location
# Build and run validation

View File

@ -213,7 +213,7 @@
</Expand> </Expand>
</Type> </Type>
<Type Name="gen::AST_Namespace"> <Type Name="gen::AST_NS">
<DisplayString>{Name} Type: {Type}</DisplayString> <DisplayString>{Name} Type: {Type}</DisplayString>
<Expand> <Expand>
<Item Name="ModuleFlags">ModuleFlags</Item> <Item Name="ModuleFlags">ModuleFlags</Item>
@ -498,7 +498,7 @@
</Expand> </Expand>
</Type> </Type>
<Type Name="gen::CodeNamespace"> <Type Name="gen::CodeNS">
<DisplayString Condition="ast == nullptr">Null</DisplayString> <DisplayString Condition="ast == nullptr">Null</DisplayString>
<DisplayString Condition="ast != nullptr">{ast->Name} {ast->Type}</DisplayString> <DisplayString Condition="ast != nullptr">{ast->Name} {ast->Type}</DisplayString>
<Expand> <Expand>

View File

@ -42,29 +42,32 @@ Push-Location $path_root
Pop-Location Pop-Location
Push-location $path_singleheader Push-location $path_singleheader
if ( -not(Test-Path($path_singleheader_gen) )) { if ( -not(Test-Path($path_singleheader_gen) )) {
New-Item -ItemType Directory -Path $path_singleheader_gen New-Item -ItemType Directory -Path $path_singleheader_gen
} }
# Run meta-program # Run meta-program
$gencpp_singleheader = Join-Path $path_singleheader_build gencpp_singleheader.exe $gencpp_singleheader = Join-Path $path_singleheader_build gencpp_singleheader.exe
Write-Host `nRunning gencpp singleheader... Write-Host `nRunning gencpp singleheader...
& $gencpp_singleheader & $gencpp_singleheader
# Format generated files # Format generated files
Write-Host `nBeginning format... Write-Host `nBeginning format...
$formatParams = @( $formatParams = @(
'-i' # In-place '-i' # In-place
'-style=file:../scripts/.clang-format' '-style=file:../scripts/.clang-format'
'-verbose' '-verbose'
) )
$include = @('gen.hpp') $include = @('gen.hpp')
$exclude = $null $exclude = $null
$targetFiles = @(Get-ChildItem -Recurse -Path $path_project -Include $include -Exclude $exclude | Select-Object -ExpandProperty FullName) $targetFiles = @(Get-ChildItem -Recurse -Path $path_project -Include $include -Exclude $exclude | Select-Object -ExpandProperty FullName)
clang-format $formatParams $targetFiles clang-format $formatParams $targetFiles
Write-Host "`nFormatting complete" Write-Host "`nFormatting complete"
Pop-Location Pop-Location
# Build and run validation

View File

@ -1,3 +1,5 @@
cls
[string] $type = $null [string] $type = $null
[string] $test = $false [string] $test = $false

View File

@ -11,4 +11,13 @@
*/ */
#if ! defined(GEN_DONT_ENFORCE_GEN_TIME_GUARD) && ! defined(GEN_TIME) #if ! defined(GEN_DONT_ENFORCE_GEN_TIME_GUARD) && ! defined(GEN_TIME)
# error Gen.hpp : GEN_TIME not defined # error Gen.hpp : GEN_TIME not defined
#endif #endif
#ifdef GEN_DONT_USE_NAMESPACE
# define GEN_NS_BEGIN
# define GEN_NS_END
#else
# define GEN_NS_BEGIN namespace gen {
# define GEN_NS_END }
#endif

View File

@ -2,32 +2,22 @@
#define GEN_ENFORCE_STRONG_CODE_TYPES #define GEN_ENFORCE_STRONG_CODE_TYPES
#define GEN_EXPOSE_BACKEND #define GEN_EXPOSE_BACKEND
#include "gen.cpp" #include "gen.cpp"
#include "file_processors/scanner.hpp"
#include "helpers/helper.hpp" #include "helpers/helper.hpp"
GEN_NS_BEGIN
#include "dependencies/parsing.cpp"
GEN_NS_END
#include "file_processors/builder.hpp"
#include "file_processors/builder.cpp"
#include "file_processors/scanner.hpp"
using namespace gen; using namespace gen;
bool namespace_by_default = true; constexpr char const* generation_notice =
"// This file was generated automatially by gen.bootstrap.cpp "
constexpr StrC nspace_default = txt_StrC(R"( "(See: https://github.com/Ed94/gencpp)\n\n";
#if defined(GEN_DONT_USE_NAMESPACE) && ! defined(GEN_NS_BEGIN)
# define GEN_NS_BEGIN
# define GEN_NS_END
#elif ! defined(GEN_NS_BEGIN)
# define GEN_NS_BEGIN namespace gen {
# define GEN_NS_END }
#endif
)");
constexpr StrC nspace_non_default = txt_StrC(R"(
#if ! defined(GEN_USE_NAMESPACE) && ! defined(GEN_NS_BEGIN)
# define GEN_NS_BEGIN
# define GEN_NS_END
#elif ! defined(GEN_NS_BEGIN)
# define GEN_NS_BEGIN namespace gen {
# define GEN_NS_END }
#endif
)");
constexpr StrC implementation_guard_start = txt_StrC(R"( constexpr StrC implementation_guard_start = txt_StrC(R"(
#pragma region GENCPP IMPLEMENTATION GUARD #pragma region GENCPP IMPLEMENTATION GUARD
@ -50,36 +40,34 @@ constexpr StrC roll_own_dependencies_guard_start = txt_StrC(R"(
constexpr StrC roll_own_dependencies_guard_end = txt_StrC(R"( constexpr StrC roll_own_dependencies_guard_end = txt_StrC(R"(
// GEN_ROLL_OWN_DEPENDENCIES // GEN_ROLL_OWN_DEPENDENCIES
#endif #endif
)"); )");
global bool generate_gen_dep = true;
global bool generate_builder = true;
global bool generate_editor = true;
global bool generate_scanner = true;
int gen_main() int gen_main()
{ {
#define project_dir "../project/"
gen::init(); gen::init();
#define project_dir "../project/" Code push_ignores = scan_file( project_dir "helpers/push_ignores.inline.hpp" );
Code pop_ignores = scan_file( project_dir "helpers/pop_ignores.inline.hpp" );
Code push_ignores = scan_file( project_dir "helpers/push_ignores.inline.hpp" ); Code single_header_start = scan_file( "components/header_start.hpp" );
Code pop_ignores = scan_file( project_dir "helpers/pop_ignores.inline.hpp" );
Code header_start = scan_file( "components/header_start.hpp" );
Code nspace_macro = untyped_str( namespace_by_default ? nspace_default : nspace_non_default );
Builder Builder
header; header = Builder::open( "gen/gen.hpp" );
header.open( "gen/gen.hpp" ); header.print_fmt( generation_notice );
header.print( push_ignores );
header.print_fmt("#pragma once\n\n"); header.print_fmt("#pragma once\n\n");
header.print( push_ignores );
// Headers // Headers
{ {
header.print( header_start ); header.print( single_header_start );
header.print( nspace_macro );
if ( generate_gen_dep )
{ {
header.print_fmt( roll_own_dependencies_guard_start );
Code header_start = scan_file( project_dir "dependencies/header_start.hpp" ); Code header_start = scan_file( project_dir "dependencies/header_start.hpp" );
Code macros = scan_file( project_dir "dependencies/macros.hpp" ); Code macros = scan_file( project_dir "dependencies/macros.hpp" );
Code basic_types = scan_file( project_dir "dependencies/basic_types.hpp" ); Code basic_types = scan_file( project_dir "dependencies/basic_types.hpp" );
@ -91,11 +79,12 @@ int gen_main()
Code hashing = scan_file( project_dir "dependencies/hashing.hpp" ); Code hashing = scan_file( project_dir "dependencies/hashing.hpp" );
Code string = scan_file( project_dir "dependencies/string.hpp" ); Code string = scan_file( project_dir "dependencies/string.hpp" );
Code file_handling = scan_file( project_dir "dependencies/file_handling.hpp" ); Code file_handling = scan_file( project_dir "dependencies/file_handling.hpp" );
Code parsing = scan_file( project_dir "dependencies/parsing.hpp" );
Code timing = scan_file( project_dir "dependencies/timing.hpp" ); Code timing = scan_file( project_dir "dependencies/timing.hpp" );
header.print_fmt( roll_own_dependencies_guard_start );
header.print( header_start ); header.print( header_start );
header.print_fmt( "GEN_NS_BEGIN\n\n" ); header.print_fmt( "GEN_NS_BEGIN\n\n" );
header.print( macros ); header.print( macros );
header.print( basic_types ); header.print( basic_types );
header.print( debug ); header.print( debug );
@ -106,23 +95,30 @@ int gen_main()
header.print( hashing ); header.print( hashing );
header.print( string ); header.print( string );
header.print( file_handling ); header.print( file_handling );
header.print( parsing );
header.print( timing ); header.print( timing );
header.print_fmt( "GEN_NS_END\n" );
if ( generate_scanner )
{
header.print_fmt( "#pragma region Parsing\n\n" );
header.print( scan_file( project_dir "dependencies/parsing.hpp" ) );
header.print_fmt( "#pragma endregion Parsing\n\n" );
}
header.print_fmt( "GEN_NS_END\n" );
header.print_fmt( roll_own_dependencies_guard_end ); header.print_fmt( roll_own_dependencies_guard_end );
} }
Code types = scan_file( project_dir "components/types.hpp" ); Code types = scan_file( project_dir "components/types.hpp" );
Code data_structs = scan_file( project_dir "components/data_structures.hpp" ); Code ast = scan_file( project_dir "components/ast.hpp" );
Code interface = scan_file( project_dir "components/interface.hpp" ); Code ast_types = scan_file( project_dir "components/ast_types.hpp" );
Code header_end = scan_file( project_dir "components/header_end.hpp" ); Code interface = scan_file( project_dir "components/interface.hpp" );
Code inlines = scan_file( project_dir "components/inlines.hpp" );
Code header_end = scan_file( project_dir "components/header_end.hpp" );
CodeBody ecode = gen_ecode( project_dir "enums/ECode.csv" ); CodeBody ecode = gen_ecode ( project_dir "enums/ECode.csv" );
CodeBody eoperator = gen_eoperator( project_dir "enums/EOperator.csv" ); CodeBody eoperator = gen_eoperator ( project_dir "enums/EOperator.csv" );
CodeBody especifier = gen_especifier( project_dir "enums/ESpecifier.csv" ); CodeBody especifier = gen_especifier( project_dir "enums/ESpecifier.csv" );
CodeBody ast_inlines = gen_ast_inlines();
Code builder = scan_file( project_dir "file_processors/builder.hpp" );
header.print_fmt( "GEN_NS_BEGIN\n\n" ); header.print_fmt( "GEN_NS_BEGIN\n\n" );
@ -133,50 +129,78 @@ int gen_main()
header.print( especifier ); header.print( especifier );
header.print_fmt("#pragma endregion Types\n\n"); header.print_fmt("#pragma endregion Types\n\n");
header.print( data_structs ); header.print_fmt("#pragma region AST\n\n");
header.print( ast );
header.print( ast_types );
header.print_fmt("#pragma endregion AST\n\n");
header.print( interface ); header.print( interface );
header.print_fmt( "#pragma region Inlines\n\n" );
header.print( inlines );
header.print( ast_inlines );
header.print_fmt( "#pragma endregion Inlines\n\n" );
header.print( header_end ); header.print( header_end );
header.print( builder );
if ( generate_builder )
{
header.print_fmt( "#pragma region Builder\n\n" );
header.print( scan_file( project_dir "file_processors/builder.hpp" ) );
header.print_fmt( "#pragma endregion Builder\n\n" );
}
if ( generate_scanner )
{
header.print_fmt( "#pragma region Scanner\n\n" );
header.print( scan_file( project_dir "file_processors/scanner.hpp" ) );
header.print_fmt( "#pragma endregion Scanner\n\n" );
}
header.print_fmt( "GEN_NS_END\n" ); header.print_fmt( "GEN_NS_END\n" );
} }
// Implementation // Implementation
{ {
header.print_fmt( "%s\n", (char const*) implementation_guard_start ); header.print_fmt( "%s\n", (char const*) implementation_guard_start );
if ( generate_gen_dep )
{ {
header.print_fmt( roll_own_dependencies_guard_start ); Code impl_start = scan_file( project_dir "dependencies/src_start.cpp" );
Code debug = scan_file( project_dir "dependencies/debug.cpp" );
Code impl_start = scan_file( project_dir "dependencies/impl_start.cpp" ); Code string_ops = scan_file( project_dir "dependencies/string_ops.cpp" );
Code debug = scan_file( project_dir "dependencies/debug.cpp" ); Code printing = scan_file( project_dir "dependencies/printing.cpp" );
Code string_ops = scan_file( project_dir "dependencies/string_ops.cpp" ); Code memory = scan_file( project_dir "dependencies/memory.cpp" );
Code printing = scan_file( project_dir "dependencies/printing.cpp" ); Code hashing = scan_file( project_dir "dependencies/hashing.cpp" );
Code memory = scan_file( project_dir "dependencies/memory.cpp" ); Code string = scan_file( project_dir "dependencies/string.cpp" );
Code parsing = scan_file( project_dir "dependencies/parsing.cpp" );
Code hashing = scan_file( project_dir "dependencies/hashing.cpp" );
Code string = scan_file( project_dir "dependencies/string.cpp" );
Code timing = scan_file( project_dir "dependencies/timing.cpp" );
Code file_handling = scan_file( project_dir "dependencies/file_handling.cpp" ); Code file_handling = scan_file( project_dir "dependencies/file_handling.cpp" );
Code timing = scan_file( project_dir "dependencies/timing.cpp" );
header.print_fmt( roll_own_dependencies_guard_start );
header.print_fmt( "GEN_NS_BEGIN\n\n"); header.print_fmt( "GEN_NS_BEGIN\n\n");
header.print( impl_start ); header.print( impl_start );
header.print( debug ); header.print( debug );
header.print( string_ops ); header.print( string_ops );
header.print( printing ); header.print( printing );
header.print( memory ); header.print( memory );
header.print( parsing );
header.print( hashing ); header.print( hashing );
header.print( string ); header.print( string );
header.print( file_handling );
header.print( timing ); header.print( timing );
header.print( file_handling ); if ( generate_scanner )
{
header.print_fmt( "#pragma region Parsing\n\n" );
header.print( scan_file( project_dir "dependencies/parsing.cpp" ) );
header.print_fmt( "#pragma endregion Parsing\n\n" );
}
header.print_fmt( "GEN_NS_END\n"); header.print_fmt( "GEN_NS_END\n");
header.print_fmt( roll_own_dependencies_guard_end ); header.print_fmt( roll_own_dependencies_guard_end );
} }
Code data = scan_file( project_dir "components/static_data.cpp" ); Code static_data = scan_file( project_dir "components/static_data.cpp" );
Code ast_case_macros = scan_file( project_dir "components/ast_case_macros.cpp" ); Code ast_case_macros = scan_file( project_dir "components/ast_case_macros.cpp" );
Code ast = scan_file( project_dir "components/ast.cpp" ); Code ast = scan_file( project_dir "components/ast.cpp" );
Code interface = scan_file( project_dir "components/interface.cpp" ); Code interface = scan_file( project_dir "components/interface.cpp" );
@ -184,21 +208,43 @@ int gen_main()
Code parsing = scan_file( project_dir "components/interface.parsing.cpp" ); Code parsing = scan_file( project_dir "components/interface.parsing.cpp" );
Code untyped = scan_file( project_dir "components/untyped.cpp" ); Code untyped = scan_file( project_dir "components/untyped.cpp" );
CodeBody etoktype = gen_etoktype( project_dir "enums/ETokType.csv", project_dir "enums/AttributeTokens.csv" ); CodeBody etoktype = gen_etoktype( project_dir "enums/ETokType.csv", project_dir "enums/AttributeTokens.csv" );
CodeNamespace parser_nspace = def_namespace( name(Parser), def_namespace_body( args(etoktype)) ); CodeNS parser_nspace = def_namespace( name(Parser), def_namespace_body( args(etoktype)) );
Code builder = scan_file( project_dir "file_processors/builder.cpp" );
header.print_fmt( "GEN_NS_BEGIN\n\n"); header.print_fmt( "GEN_NS_BEGIN\n\n");
header.print( data ); header.print( static_data );
header.print_fmt( "#pragma region AST\n\n" );
header.print( ast_case_macros ); header.print( ast_case_macros );
header.print( ast ); header.print( ast );
header.print_fmt( "#pragma endregion AST\n\n" );
header.print_fmt( "#pragma region Interface\n\n" );
header.print( interface ); header.print( interface );
header.print( upfront ); header.print( upfront );
header.print_fmt( "#pragma region Parsing\n\n" );
header.print( parser_nspace ); header.print( parser_nspace );
header.print( parsing ); header.print( parsing );
header.print_fmt( "#pragma endregion Parsing\n\n" );
header.print( untyped ); header.print( untyped );
header.print( builder ); header.print_fmt( "#pragma endregion Interface\n\n");
if ( generate_builder )
{
header.print_fmt( "#pragma region Builder\n\n" );
header.print( scan_file( project_dir "file_processors/builder.cpp" ) );
header.print_fmt( "#pragma endregion Builder\n\n" );
}
#if 0
if ( generate_scanner )
{
header.print_fmt( "#pragma region Scanner\n\n" );
header.print( scan_file( project_dir "file_processors/scanner.cpp" ) );
header.print_fmt( "#pragma endregion Scanner\n\n" );
}
#endif
header.print_fmt( "GEN_NS_END\n"); header.print_fmt( "GEN_NS_END\n");
header.print_fmt( "%s\n", (char const*) implementation_guard_end ); header.print_fmt( "%s\n", (char const*) implementation_guard_end );
@ -209,4 +255,5 @@ int gen_main()
gen::deinit(); gen::deinit();
return 0; return 0;
#undef project_dir
} }

View File

@ -1,11 +1,6 @@
# Test # Test
The following tests focus on attempting to generate some math, containers, and the memory module of zpl. The implementaiton here is not well organized and needs a rewrite..
Not all the files are written how I would practically use the library, the containers for example would I only do basic sanity and parsing tests for the most part.
be better on in c++ as templates, since the templates they generate are trivial symbols to inspect or debug. The library is getting practical usage tests in [genc](https://github.com/Ed94/genc) and other projects.
An example of a non-trivial generation is a container for elements with SOA or AOS policy for layout.
(If a unified element syntax is desired)
The test is divided between two major sets of tests: Parsed and Upfront.

View File

@ -1,9 +1,9 @@
#if GEN_TIME
#define GEN_DEFINE_LIBRARY_CODE_CONSTANTS #define GEN_DEFINE_LIBRARY_CODE_CONSTANTS
#define GEN_ENFORCE_STRONG_CODE_TYPES #define GEN_ENFORCE_STRONG_CODE_TYPES
#define GEN_EXPOSE_BACKEND #define GEN_EXPOSE_BACKEND
#define GEN_BENCHMARK #define GEN_BENCHMARK
#include "gen.hpp" #include "gen/gen.hpp"
#include "gen/gen.builder.hpp"
using namespace gen; using namespace gen;
Code gen_SOA( CodeStruct struct_def, s32 num_entries = 0 ) Code gen_SOA( CodeStruct struct_def, s32 num_entries = 0 )
@ -118,7 +118,7 @@ void check_SOA()
{ {
log_fmt("\ncheck_SOA:"); log_fmt("\ncheck_SOA:");
gen::init(); gen::init();
Builder soa_test; soa_test.open( "SOA.gen.hpp" ); Builder soa_test = Builder::open( "SOA.gen.hpp" );
soa_test.print( parse_using( code( soa_test.print( parse_using( code(
using u16 = unsigned short; using u16 = unsigned short;
@ -142,4 +142,3 @@ void check_SOA()
gen::deinit(); gen::deinit();
log_fmt(" passed!\n"); log_fmt(" passed!\n");
} }
#endif

View File

@ -132,7 +132,7 @@ u32 gen_sanity()
// Namespace // Namespace
{ {
CodeNamespace def = parse_namespace( code( CodeNS def = parse_namespace( code(
namespace TestNamespace namespace TestNamespace
{ {
} }
@ -283,7 +283,7 @@ u32 gen_sanity()
using TestUsing = u8; using TestUsing = u8;
)); ));
CodeNamespace nspace = parse_namespace( code( CodeNS nspace = parse_namespace( code(
namespace TestNamespace namespace TestNamespace
{ {
}; };

View File

@ -4,11 +4,11 @@
#define GEN_ENFORCE_STRONG_CODE_TYPES #define GEN_ENFORCE_STRONG_CODE_TYPES
#define GEN_EXPOSE_BACKEND #define GEN_EXPOSE_BACKEND
#define GEN_BENCHMARK #define GEN_BENCHMARK
#include "Parsed\Array.Parsed.hpp" #include "Array.Parsed.hpp"
#include "Parsed\Buffer.Parsed.hpp" #include "Buffer.Parsed.hpp"
#include "Parsed\HashTable.Parsed.hpp" #include "HashTable.Parsed.hpp"
#include "Parsed\Ring.Parsed.hpp" #include "Ring.Parsed.hpp"
#include "Parsed\Sanity.Parsed.hpp" #include "Sanity.Parsed.hpp"
#include "SOA.cpp" #include "SOA.cpp"
#include "gen.cpp" #include "gen.cpp"

View File

@ -1,11 +1,11 @@
// Testing to make sure backend of library is operating properly. // Testing to make sure backend of library is operating properly.
#ifdef GEN_TIME
#define GEN_DEFINE_LIBRARY_CODE_CONSTANTS #define GEN_DEFINE_LIBRARY_CODE_CONSTANTS
#define GEN_ENFORCE_STRONG_CODE_TYPES #define GEN_ENFORCE_STRONG_CODE_TYPES
#define GEN_EXPOSE_BACKEND #define GEN_EXPOSE_BACKEND
#define GEN_BENCHMARK #define GEN_BENCHMARK
#include "gen.hpp" #include "gen/gen.hpp"
#include "gen/gen.builder.hpp"
void check_sanity() void check_sanity()
{ {
@ -89,4 +89,3 @@ void check_sanity()
gen::deinit(); gen::deinit();
log_fmt("\nSanity passed!\n"); log_fmt("\nSanity passed!\n");
} }
#endif

View File

@ -2,7 +2,8 @@
#define GEN_ENFORCE_STRONG_CODE_TYPES #define GEN_ENFORCE_STRONG_CODE_TYPES
#define GEN_EXPOSE_BACKEND #define GEN_EXPOSE_BACKEND
#define GEN_BENCHMARK #define GEN_BENCHMARK
#include "gen.cpp" #include "gen/gen.cpp"
#include "gen/gen.builder.cpp"
#include "sanity.cpp" #include "sanity.cpp"
#include "SOA.cpp" #include "SOA.cpp"
#include "test.singleheader_ast.cpp" #include "test.singleheader_ast.cpp"

View File

@ -1,11 +1,10 @@
#pragma once
#define GEN_DEFINE_LIBRARY_CODE_CONSTANTS #define GEN_DEFINE_LIBRARY_CODE_CONSTANTS
#define GEN_ENFORCE_STRONG_CODE_TYPES #define GEN_ENFORCE_STRONG_CODE_TYPES
#define GEN_EXPOSE_BACKEND #define GEN_EXPOSE_BACKEND
#define GEN_BENCHMARK #define GEN_BENCHMARK
#include "gen.hpp" #include "gen/gen.hpp"
#include "file_processors/scanner.hpp" #include "gen/gen.builder.hpp"
#include "gen/gen.scanner.hpp"
using namespace gen; using namespace gen;
void check_singleheader_ast() void check_singleheader_ast()
@ -14,29 +13,21 @@ void check_singleheader_ast()
gen::init(); gen::init();
log_fmt("\ncheck_singleheader_ast:\n"); log_fmt("\ncheck_singleheader_ast:\n");
FileContents file = file_read_contents( GlobalAllocator, true, project_dir "singleheader/gen/gen.hpp" ); FileContents file = file_read_contents( GlobalAllocator, true, project_dir "singleheader/gen/gen.hpp" );
u64 time_start = time_rel_ms();
CodeBody ast = parse_global_body( { file.size, (char const*)file.data } );
CodeBody ast = parse_global_body( { file.size, (char const*)file.data } ); log_fmt("\nAst generated. Time taken: %llu ms\n", time_rel_ms() - time_start);
log_fmt("generated AST!!!\n"); log_fmt("\nSerializng ast:\n");
time_start = time_rel_ms();
s32 idx = 0; Builder
for ( Code entry : ast ) builder = Builder::open( "singleheader_copy.gen.hpp" );
{
if (idx == 900)
{
log_fmt("break here\n");
}
log_fmt("Entry %d: %s\n", idx, entry.to_string() );
idx++;
}
Builder builder;
builder.open( "singleheader_copy.gen.hpp" );
log_fmt("\n\nserializng ast\n");
builder.print( ast ); builder.print( ast );
builder.write(); builder.write();
log_fmt("passed!!\n"); log_fmt("passed!! Time taken: %llu ms\n", time_rel_ms() - time_start);
gen::deinit(); gen::deinit();
} }

View File

@ -139,7 +139,7 @@ u32 gen_sanity_upfront()
// Namespace // Namespace
{ {
CodeNamespace namespace_def; CodeNS namespace_def;
{ {
CodeBody body = def_namespace_body( 1 CodeBody body = def_namespace_body( 1
, def_comment( txt_StrC("Empty namespace body") ) , def_comment( txt_StrC("Empty namespace body") )

View File

@ -5,12 +5,11 @@
#define GEN_EXPOSE_BACKEND #define GEN_EXPOSE_BACKEND
#define GEN_BENCHMARK #define GEN_BENCHMARK
#include "gen.cpp" #include "gen.cpp"
#include "Upfront\Array.Upfront.hpp" #include "Array.Upfront.hpp"
#include "Upfront\Buffer.Upfront.hpp" #include "Buffer.Upfront.hpp"
#include "Upfront\HashTable.Upfront.hpp" #include "HashTable.Upfront.hpp"
#include "Upfront\Ring.Upfront.hpp" #include "Ring.Upfront.hpp"
#include "Upfront\Sanity.Upfront.hpp" #include "Sanity.Upfront.hpp"
using namespace gen; using namespace gen;

View File

@ -0,0 +1,3 @@
// Constructs an AST from the bootstrap generated gen files, then serializes it to a set of files.
// Using the new set of serialized files, reconstructs the AST and then serializes it again.
// The two sets of serialized files should be identical. (Verified by comparing the file hashes)

View File

@ -0,0 +1,3 @@
// Constructs an AST from the singlheader generated gen files, then serializes it to a set of files.
// Using the new set of serialized files, reconstructs the AST and then serializes it again (to different set of files).
// The two sets of serialized files should be identical. (Verified by comparing the file hashes)