191 Commits

Author SHA1 Message Date
Ed_
8f47f3b30f Comment, Constructor, Destructor, Define, Enum, Exec, Extern, Include, Friend, Fn codes member proc usage reductions 2024-12-02 16:59:13 -05:00
Ed_
0bad61fda6 remove raw member def from code types, reduction on CodeAttributes 2024-12-02 11:20:31 -05:00
Ed_
ea18792373 Progress on member proc usage reduction (CodeParam, CodeSpecifiers) 2024-12-02 10:58:24 -05:00
Ed_
16b8a3a164 began to remove usage of code specific types member procs 2024-12-02 04:12:09 -05:00
Ed_
5b0079fb0c ast interface uage reductions 2024-12-02 03:18:52 -05:00
Ed_
9321a04ebc reduction of Code struct member function usage in base lib 2024-12-02 02:38:55 -05:00
Ed_
9b68791e38 fixes for array when not using member features. 2024-12-02 02:11:49 -05:00
Ed_
2dcc968c39 Preparing for reductions on code_types.hpp 2024-12-02 01:56:49 -05:00
Ed_
c38b077c37 Code::set_global reduction 2024-12-02 00:43:57 -05:00
Ed_
f9b5029e64 Code::is_valid rection 2024-12-02 00:41:41 -05:00
Ed_
2b24511f7d Code::is_equal reduction 2024-12-02 00:34:40 -05:00
Ed_
5cd69e1742 Code::is_body reduction 2024-12-02 00:18:54 -05:00
Ed_
007bfa0cb0 Code::duplicate reduction 2024-12-02 00:16:11 -05:00
Ed_
37c33ffb3e reduction on debug_str 2024-12-02 00:10:24 -05:00
Ed_
937235b776 progress (Code) 2024-12-02 00:03:38 -05:00
Ed_
f9c21ebc04 progress 2024-12-01 23:35:58 -05:00
Ed_
fec709cc76 Progresss 2024-12-01 21:59:43 -05:00
Ed_
80cb3f4eca Significant progress reducing c++ feature usage in the library. 2024-12-01 18:50:37 -05:00
Ed_
9e88cb8724 String::is_equal added (bad last commit) 2024-12-01 13:29:33 -05:00
Ed_
f61c1c560d String::is_equal added 2024-12-01 13:29:16 -05:00
Ed_
8ef982003a Added is_body to AST and Code types 2024-12-01 12:48:58 -05:00
Ed_
31691b1466 Fixed issue with HashTable region detection 2024-12-01 05:37:03 -05:00
Ed_
ed0c0422ad Looking into what the library's convention for enums will be.
Most likely will just reduce them to C-enums with underlying type.
Otherwise there has to be a mechanism to drop the defs down to them anyways, and eliminate the namespace wraps.
2024-12-01 05:30:37 -05:00
Ed_
e5acac1d18 String member definitions not longer used in the base project 2024-12-01 03:06:30 -05:00
Ed_
c7b072266f progress on c_library.cpp 2024-12-01 01:40:31 -05:00
Ed_
a96d03eaed brought over the generators of array and hashtable for c-lib gen
From the old genc repo. Still need to fully check that its code is up to date
2024-12-01 01:40:14 -05:00
Ed_
0b4ccac8f9 Removed usage of hashtable member procs 2024-12-01 01:39:21 -05:00
Ed_
31a3609b28 some fixes to c's fixed_arena gen 2024-11-30 23:48:14 -05:00
Ed_
fbdb870986 Finished first pass reviewing memory.hpp for C lib generation 2024-11-30 23:38:27 -05:00
Ed_
6d04165b96 Reduce cpp freatures usage of Array container.
Almost ready to be inter-operable with C
2024-11-30 18:54:19 -05:00
Ed_
cc245cc263 new files 2024-11-30 17:22:06 -05:00
Ed_
06deb1e836 memory.hpp no longer uses memory mappings by default 2024-11-30 17:18:49 -05:00
Ed_
5527a27f7b prepare c_library meta-program a bit 2024-11-30 16:54:03 -05:00
Ed_
a67fdef20a dir restructuring
just making it more organized (gen_ prefix for library generation meta-programs)
2024-11-30 16:50:53 -05:00
Ed_
056a5863b8 for the future... 2024-11-30 14:34:28 -05:00
Ed_
79eb5f1f76 strings done 2024-11-30 14:13:30 -05:00
Ed_
c6cb583518 Hashtable done 2024-11-30 13:31:59 -05:00
Ed_
34eec66f35 Array done 2024-11-30 13:14:47 -05:00
Ed_
4137ebfbd8 pool done (see previous commits for context) 2024-11-30 12:27:54 -05:00
Ed_
5958dd2055 Did arena and fixedarena changes (for reducing usage of member procs) 2024-11-30 12:16:01 -05:00
Ed_
163ad0a511 looking into removing "oop" features from base library
I want to make member functions an optional addition the user can generate a derivative library with.
The purpose is to simplify the implementation as to make generating a C-variant simpiler.

I also want to use it as a study to see how much simpiler it makes the library without having it.
2024-11-29 15:18:06 -05:00
Ed_
e3c2a577ba addded String::contains defs 2024-11-29 14:50:54 -05:00
Ed_
81a0376c99 Need cstdlib for systems calls in generator files. update to package_release.ps1 2024-10-27 21:38:02 -04:00
Ed_
1417a68757 updates to test validations
They don't really work great right now...
2024-10-27 21:19:25 -04:00
Ed_
1e4d5ce630 touchup to singleheader.cpp 2024-10-27 21:18:58 -04:00
Ed_
0f2763a115 fixes for unreal use 2024-10-27 21:18:41 -04:00
Ed_
420f452d35 More fixes found by self-parsing 2024-10-27 20:22:36 -04:00
Ed_
908c385de5 Fix exp asts having bad union definitions.
They're not used yet but was causing self-parsing issues.
2024-10-27 20:22:24 -04:00
Ed_
c1878265c8 Minimizing reformatting of generated library files 2024-10-27 20:01:54 -04:00
Ed_
23742868c4 Delete auxillary/vis_ast
When I get to making this it will be with SectrPrototype
2024-10-27 18:59:17 -04:00
Ed_
2e5e31ed3b gencpp : General refactors to dependencies
Mostly just cleanup and renaming of certain stuff (mostly in dependencies).

* Changed uw and sw to usize and ssize.
* Removed zpl_cast usage throughout dependencies
* No longer using GEN_DEF_INLINE & GEN_IMPL_INLINE
* header_start.hpp renamed to platform.hpp for depdendencies header.
2024-10-27 18:58:37 -04:00
Ed_
00df336610 fix type on parser namespace in singleheader.cpp 2024-10-26 18:42:23 -04:00
Ed_
d89c9a6072 de-hardcode target_arch.psm1 import 2024-10-25 13:11:21 -04:00
Ed_
6aa99ac1d5 change how path_root is resolved so it works when cloned into another repository 2024-10-25 13:07:39 -04:00
Ed_
3989f5fa83 formatting and removing unused code 2024-10-25 12:54:55 -04:00
Ed_
f90c0a59b6 inital implemention of UE library variant generator completed 2024-10-25 05:01:37 -04:00
Ed_
33f992ef56 Updated generated ast_inlines.hpp so that operator defs have the inline explicit
* Added support for parsing/serializing specifiers for OpCast roughtly.. Doesn't have constraints on what specifiers beyond whats expected in global nspace scope..
* Minor adjustments to hashtable to avoid UE compile errors
* Make sure scanner.cpp is being made by bootstrap
2024-10-25 04:08:20 -04:00
Ed_
0542204b35 progress on unreal variant generator 2024-10-25 03:00:07 -04:00
Ed_
e5616c5879 generated code update + reverting some fixes for now... 2024-10-25 02:59:56 -04:00
Ed_
40a256f6c3 initial setup for generating a library compatible for usage as an Unreal thirdyparty module. 2024-10-25 01:04:48 -04:00
Ed_
b8e1aa6eb7 WIP : Fixes and other changes
* Number literals weren't getting properly lexed
* Fixes for compiler errors with Unreal Engine configuration.
* Support for "post-name" macros in parameters
* Support for variables initializing directly using constructor syntax.
* Explicitly added inline keyword to header inlines for compiling compile library in multiple translation units.
2024-10-25 01:04:17 -04:00
Ed_
e1592ba410 Bug fixes and updates to package_release.ps1
- Incrased size of the defines_map_arena to 256KB
- Various fixes for the parser
- Various fixes for code serialization
- Fix for is_equal member func in Code types
- Fixes for hasthable container
- Added are_equal static func to String type for use against StrC
- Added starts_with functions to String type
- package_release.ps1 now packages all docs (forgot to update it with last release)
2024-05-05 21:53:22 -04:00
Ed_
4a2a93d41b Updated docs 2024-04-17 18:29:30 -04:00
Ed_
36260f6edb Updated gencpp with latest fixes used in other projects.
This variant can support parsing some Unreal Engine files!!
2024-04-17 17:40:32 -04:00
Ed_
83d691c65c Pushing latest changes for gencpp 2024-04-17 16:55:22 -04:00
Ed_
626ab703a7 Fixed some compilation errors 2023-11-22 15:41:41 -05:00
Ed_
6b10cd2b3e Updated AST_Types.md doc 2023-11-22 15:03:24 -05:00
Ed_
91a3250d4c Finished current iteration of parser_algo docs and parser.cpp inline comment docs
Added some todos and prep for upcoming changes
2023-11-22 14:23:21 -05:00
Ed_
a667eb4afe Progress on parser documentation 2023-11-21 23:36:56 -05:00
Ed_
f67f9547df WIP: Improvements to parser, updated docs
Trying to get support for typename keyword soon
2023-11-21 21:27:33 -05:00
Ed_
772db608be Added support for predefining preprocessor defines before parsing strings of code.
This prevents issues for preprocessor defines not getting treated properly for specific circumstances (such as macro wrappers for specifiers).
2023-11-21 20:09:14 -05:00
Ed_
be023325a9 Added AST & Code definitions segemnted code defs to separate file
For upcoming statements & expression types, just beginning to lay things out for the future.
2023-11-21 20:07:49 -05:00
Ed_
a0ee683f82 Added new csvs for ECode & ETokType to keep track of upcoming additions 2023-11-21 20:06:05 -05:00
Ed_
6ad0ae97bc Added bool type keyword to ETokType, move volatile pos in ESpecifier
volatile can be applied to a function so I moved it there.
2023-11-21 20:05:38 -05:00
Ed_
3319bfcaa9 Updates to test and workspace
Added a more granular test using the original library files. Already helped me identify a bug.
2023-11-21 20:03:51 -05:00
Ed_
9d27c7d37e Lexer improvement prep, segmentation of lexer and parser. 2023-11-20 21:24:27 -05:00
Ed_
5c73fbee83 WIP : AST serialization improvements
Code untestd its an initial draft
2023-11-19 20:35:21 -05:00
Ed_
36ebbfe29b Got refactored raylib working in the vis_ast binary
Now to make that debugger...
2023-11-19 00:01:35 -05:00
Ed_
053daaf877 Merge branch 'main' into dev 2023-11-17 19:26:21 -05:00
Ed_
040ec00606 Raylib c refactor scripts complete 2023-11-17 19:25:54 -05:00
Ed_
919166efa1 fix for build script 2023-10-25 23:55:29 -04:00
Ed_
9bd6dac783 Merge pull request #51 from Ed94/dev 2023-10-25 23:47:29 -04:00
Ed_
212d907d73 Fixed bug with inline comments for variables not parsing correctly. 2023-10-25 23:44:25 -04:00
Ed_
c1ab233686 Fix for bug with comma separated declaration list vars
Known Bug: Inline comments are not printing now
2023-10-25 21:43:22 -04:00
Ed_
b42b224c0d Forgot to push some parser fixes 2023-10-25 03:33:35 -04:00
Ed_
041671762b Got through parsing raylib.h, started to do some major refactors. 2023-10-25 03:26:25 -04:00
Ed_
d0f3b6187e Added initial support for comma-separated variable declaration lists. 2023-10-25 00:25:35 -04:00
Ed_
b22f589203 Progress on preparing raylib for usage 2023-10-24 12:46:59 -04:00
Ed_
4af85f8612 update gitignore for exes 2023-10-24 06:02:07 -04:00
Ed_
e9c151f85d Deleting a sneaked in binary 2023-10-24 06:01:47 -04:00
Ed_
a900e86b65 Got raylib bootstrapped to compile
Still need to setup a refactor script among other things before using.
2023-10-24 06:00:28 -04:00
Ed_
d5a4b77033 Some initial boostrapping of new visual ast aux project 2023-10-24 03:14:20 -04:00
Ed_
eaba60f80b Updated build scripts to latest setup used with HandmadeHero repo 2023-10-24 01:40:21 -04:00
Ed_
912cc6b538 Bugfixes during hiatus
Found while doing some metaprogramming for day 42 of handmade hero.
2023-10-22 21:41:36 -04:00
Ed_
3dd5482a46 set ContinuationIndentWidth to 4 for clang format. 2023-10-01 20:19:16 -04:00
Ed_
632fa10027 Updated readme with partial hiatus notice. 2023-10-01 12:37:29 -04:00
Ed_
6498b13658 Fix for non-foreign include content not getting parsed correctly. 2023-10-01 12:16:15 -04:00
Ed_
7f562cd77f Merge branch 'dev' 2023-09-28 20:36:12 -04:00
Ed_
3a0e971ebf Bugfix for parse_variable, const check for prefix specifiers causing infinite loop. 2023-09-28 20:35:20 -04:00
Ed_
4997cb5878 Merge pull request #45 from Ed94/dev
Dev
2023-09-25 17:50:16 -04:00
Ed_
729c891cbd Last fixes before handmade hero hiatus 2023-09-25 17:48:16 -04:00
Ed_
754bcfb31e Fixes to get it back to where I was last at with the const specifier issue 2023-09-25 16:42:29 -04:00
Ed_
a8708abf8b WIP : Various changes to project before small hiatus. (Broken)
I need to manually review these as the changes have various errors that are difficult to diagnose why.

I took a break to do handmade hero and now a bit rusty.
2023-09-25 12:12:11 -04:00
Ed_
4b48b96a79 WIP : better AST::debug_str()
Now its more contexually rich to the ast type, however I need to hookup tokens from parsing to the AST. There needs to be a way for the debug string to lookup the token and provide the contexual line.
Can either pass it ( TokArray* toks ) from the parser on failure (or `CodeFile`)..
Technically there is more than enough room for another Token* ptr. I could add another and specifiers would still have at minimum 14 slots before needing to extended to next specs.
**************... yeah
2023-09-12 02:32:44 -04:00
Ed_
9495fc2985 Updates to documentation 2023-09-11 18:34:37 -04:00
Ed_
378de73a7d Merge branch 'main' of https://github.com/Ed94/gencpp 2023-09-08 15:15:13 -04:00
Ed_
35ac0c1048 Fix compile-error for singleheader found with scanner's scan_file. 2023-09-08 15:14:43 -04:00
Ed_
2c893d5e35 Merge pull request #34 from Ed94/dev
Dev
2023-09-07 23:06:02 -04:00
Ed_
6ea40094ee More fixes from clang warnings. Parser::lex prep for changes
parse_static_assert now properly adds new-line to end of statement.

I'm going to end up making a static_assert ast... that or when the statement ast is made it will handle adding that newline.
2023-09-07 22:52:51 -04:00
Ed_
0a8d3bfc6a Added fixed arenas (just ergonomics) 2023-09-07 22:29:04 -04:00
Ed_
212b4e6d16 Fixes for compiling with clang
Clang just had better errors and caught stuff msvc did not.
2023-09-07 21:11:57 -04:00
Ed_
d606c790ca Added a new AST member NextVar to be used for comma separated variable support.
Interface adding has been adjusted to use ParentType->Next.

Using the AST_Class->Next was bad since that breaks the linked list a body AST would have used for traversal.
2023-09-06 03:06:30 -04:00
Ed_
2bfbef1d0c strip_formatting : Remove code thats no longer needed.
Its everyting related to stripping a function body.
2023-09-06 02:09:26 -04:00
Ed_
f1fb75cc1c Progress on strip_formatting function, support for multi-dimentional array variables and typenames.
strip_formatting suffers from some edge failure with what looks to be escaped character literals (not entirely sure).

I've decided to not remove formatting from unvalidated function bodies since I plan to support parsing its content properly.
However expression values for a statement will fail to have their formatting removed with this.

Since I don't plan to parse those anytime soon, I'll have to fix any edge cases for those at least..
2023-09-06 02:07:09 -04:00
Ed_
2200bcde9a Improved singleheader test
Need to make the debug_str provided by the AST type aware to provide as much contextual information as possible (finally got to this point with validation).

Singleheader test now directly calls clang-format to cleanup the reconstructed copy of the singleheader. Its needed to remove any sort of formatting discrepancies found by the parser since its sensistive to that for new-lines, etc.
2023-09-05 13:36:59 -04:00
Ed_
3e249d9bc5 Reorganization of parser, refactor of parse_type( bool* ) and progression of parser docs
Wanted to make parser implementation easier to sift through, so I emphasized alphabetical order more.

Since I couldn't just strip whitespace from typenames I decided to make the parse_type more aware of the typename's components if it was a function signature.
This ofc lead to the dark & damp hell that is parsing typenames.

Also made initial implementation to support parsing decltype within a typename signature..

The test failure for the singleheader is still a thing, these changes have not addressed that.
2023-09-05 01:48:11 -04:00
Ed_
3868e1e811 Added cursed typedef 2023-09-04 12:32:31 -04:00
Ed_
543427dfe5 Fixes to parsing for validation
Removed whitespace stripping from parse_type, prepped for doing some major changes for function signature typenames

Moved template argument parsing to its own helper function since its used in more the one spot.

Latest failure is due to stack overflow when validating parameters. (Shouldn't be hard to debug)
2023-09-03 23:36:51 -04:00
Ed_
f2d4ec96f0 Dumb fix for name check on parameters
Its now spitting out actual validation failures.
2023-09-03 20:34:27 -04:00
Ed_
1076818250 Got whitepace stripping properly working (AFAICT) for parse_define()
Made debug for viewing whitespace in AST::is_equal with String::visualize_whitespace()

Format stripping code is currently confined within parse_define()

I plan to move it to its own function soon, I just want to make sure its finalized first.

Other unvalidated content will need to have an extra check for preprocessed lines.
Example: Function bodies can have a #define <identifier> <definition>. I cannot strip the last <new line> as it will break the semantic importance to distinguish that line.
So it needs to be:
<content before> <new line>
<preprocessed line> <new line>
<content after>

In the content string that is minimally preserved
2023-09-03 20:29:49 -04:00
Ed_
c4c308c8ba Fixes for auxilary code + typo fix in ast.cpp
Needed the intellisense directive ifdef wrap.
2023-08-29 00:03:08 -04:00
Ed_
a4d9a63d71 Updated single-header based on last cs and also docs 2023-08-28 23:52:44 -04:00
Ed_
0197afd543 Changed how editor intellisense directives are handled for compoenents and dependencies
Didn't like the way I was processing them in scan_file.
2023-08-28 23:46:50 -04:00
Ed_
7249a7317d Added space stripping during for content of various ASTs
* Typedef/Typename
* Function Names
* Pragmas
* Attributes
2023-08-26 11:55:05 -04:00
Ed_
abf51e4aa9 Adjustment to AST::is_equal based on issues found with last CS.
Defined check_member_content, will spit out the strings if they aren't equivalent so the user can verify for themselves if its correct.
2023-08-25 18:57:53 -04:00
Ed_
9b6dc3cbd8 Work on AST::is_equal.
The NumEntries checks need to be deferred until the end as a final unresolved check on valdiation. As if there really is a discrepancy of entires it should be revealed by the specific entry failing.

Right now the latest failure with the single header check involves a define directive specifically the define does omit whitespace properly and so the check interprets the different cached content to be non-equivalent.

This will happen with all unvalidated aspects of the AST ( expressions, function bodies, etc )

There are two ways to resolve, either make an AST that can tokenize all items (not realistic), or I need to strip non-syntax important whitespace and then cache the string. This would mean removing everything but a single whitespace for all content within a content string. Otherwise, I would have to somehow make sure the content of the string has the exact formatting between both files for the definitions that matter.

AST types with this issue:
* Define Directive
* Pragma Directive
* Comment
* Execution
* Platform Attributes
* Untyped

Comments can technically be left unverified as they do not matter semantically.
When the serialization is first emitted, the content these strings should for the most part be equivalent. However I do see some possible failures for that if a different style of bracket placment is used (between the serialization).

At that point what I could do is just leave those unverified and just emit the content to the user as warning that the ast and the other compared could not be verified.

Those technically can be handled on a per-eye basis, and worst case the tests with the compiler will in the determine if any critical defintions are missing for the user.
2023-08-25 18:40:13 -04:00
Ed_
9edcbad907 Fixes to parsing marco content, progress on validation test (single-header) 2023-08-23 21:19:31 -04:00
Ed_
a6766cf0b1 Singleheader validation test got through ast reconstruction, failed to validate the reconstructed AST. 2023-08-23 18:16:45 -04:00
Ed_
8635b0fd1b doc update for parampack typename 2023-08-23 13:18:32 -04:00
Ed_
30eec99628 Changes to include usage, starting to attempt singleheader automated verification 2023-08-23 13:17:22 -04:00
Ed_
f9117a2353 Added zpl's ivrtual memory to dependencies (unused for now) 2023-08-23 11:07:43 -04:00
Ed_
c81f4b34ee Cleanup and doc updates 2023-08-23 02:17:47 -04:00
Ed_
c97762ac16 Added support for inline comments
Also now doing comment serialization on def_comment directly as parse_comment doesn't need it.

Essentially comment ast types serialize the same way s untyped and execution ASTs
2023-08-23 00:25:14 -04:00
Ed_
5e79e8ba65 Started to setup for codebase validation tests.
Fleshed out initial version of AST::is_equal( AST* )

Setup the test directory with initial files for each major validation test.
2023-08-22 16:01:50 -04:00
Ed_
49a2cd7b2c Uncomment formatting comments in build script (forgot to after completing fixes) 2023-08-22 02:17:28 -04:00
Ed_
a6c6574390 More formatting fixes 2023-08-22 02:09:20 -04:00
Ed_
c4846dad26 Formatting fixes 2023-08-22 01:51:59 -04:00
Ed_
a61a28b778 Fixx to bitfield variables
Related to https://github.com/Ed94/gencpp/issues/25
2023-08-22 00:40:38 -04:00
Ed_
d0c995893d Lower inital memory allocation amounts. (Lower latency iteration for running generator)
I need to change it so that they all use one big arena allocation for the initial. This can be done when the global allocator is changed to a growable arena.
2023-08-21 23:49:23 -04:00
Ed_
a42e241afb Got rid of the temp compoonent files, they are now generated via bootstrapping.
This isn't the last step though everything in the main project directory that isn't md files needs to be generated only.
Can't do that till testing is robust enough...
2023-08-21 23:28:39 -04:00
Ed_
6d85dd8fe8 Update vcproj 2023-08-21 23:09:40 -04:00
Ed_
db6e8b33eb got intellisense working for the most part...
VScode works withs some issues.
VS2022 fails.
10xEditor works fine.
JetBrains Rider fails due to it not supporting <push/pop>_macro pragmas
2023-08-21 23:07:03 -04:00
Ed_
7be3617083 Runtime fixed 2023-08-21 22:48:05 -04:00
Ed_
5c47777972 compiles agian... 2023-08-21 21:40:23 -04:00
Ed_
4a2ed6de4e another vs code automated edit... 2023-08-21 21:40:05 -04:00
Ed_
9f64760b7a Commenting out includes to diagnose... 2023-08-21 21:20:29 -04:00
Ed_
68f34e6fab gitignore update for vc140.pdb
will remove after I figure out how its going rogue.
2023-08-21 21:12:52 -04:00
Ed_
1f9bbddbb7 vs setting update 2023-08-21 21:12:25 -04:00
Ed_
050b00f28a WIP - Broken Compile : Added pragma once and includes to all files + Parser fixes, and String improvements
Adding the pragma once and includes the files broke compilation, still diagnosing why.

- Some string functions were moved to the cpp, still need to do some more evaluation of it and the containers...
- Added support for forceinline and neverinline to parsing (untested)
- Added support for specifiers in operator cast such as explicit, inline/forceinline/neverinline, etc.
    - Before it only support const.
    - Still need to support volatile.
- Forceinline was not supported at all for tokenization, fixed that.
2023-08-21 20:30:13 -04:00
Ed_
1241f44fd4 scan_file function can auto skips pragma once and includes
Needed so that includes could be added to components so that intellisense would not fail since the parser (for most editors) doesn't properly parse the enviornment and cheats on a per-file basis.
2023-08-21 20:27:00 -04:00
Ed_
7d1c499e7b Build script improvements
Made sure devshell only loads up the msvc env once.
2023-08-20 22:39:46 -04:00
Ed_
b3f0c2734f Merge remote-tracking branch 'origin/main' into dev 2023-08-20 18:04:51 -04:00
Ed_
eaeb8acee4 Remove Some vcproj changes, removal of the rogue vc140.pdb 2023-08-20 18:04:35 -04:00
Ed_
12bf878cb3 Merge pull request #23 from Ed94/dev
dev pull - Build script updates and misc fixes
2023-08-20 17:55:35 -04:00
Ed_
52ae431ad6 Made clean script remove the gen dir now from test dir. 2023-08-20 15:51:51 -04:00
Ed_
05fa62eced Test building & generation fixed with altest scripts 2023-08-20 15:45:06 -04:00
Ed_
2f7836b191 Fixes to buildscript 2023-08-20 13:18:09 -04:00
Ed_
f574a9ba9a Further improvements to build script
test is failing to complete properly, need to debug...
2023-08-20 13:02:50 -04:00
Ed_
a6bf60a51e Simpilication of build script, added initial support for tests 2023-08-20 12:31:28 -04:00
Ed_
37d9782cf2 Singleheader now compiles with new build script on both clang & msvc 2023-08-20 10:17:55 -04:00
Ed_
11679ba8b4 New build script works for clang and msvc!
Need to update singleheader and test to use it.
2023-08-19 21:33:01 -04:00
Ed_
8985f0a4d9 MSVC in latest script works
Clang is having issues.
2023-08-19 17:08:13 -04:00
Ed_
32a910515e More refactoring, getting rid of meson in favor of just powershell scripts 2023-08-19 12:18:48 -04:00
Ed_
aa928ff446 Scripting updates, some refactors..
Made a package release script.

Did refactors based on some design considerations
Still need to make some major decisions...
2023-08-09 18:47:59 -04:00
Ed_
5aff89262b Fixes (Doc typos, pragma once worng type, non-debug fatal compile fail) 2023-08-09 09:50:12 -04:00
Ed_
b5fa864318 Fixes and improvements to serialization.
There were multiple issues with comment and newline lexing.

Extended printing functions to support Strings with %S flag (captial 'S').
Allows for length detection. Also made it so that precision for strings is the string length.
2023-08-08 22:14:58 -04:00
Ed_
bb35444be9 Fix for log_failure macro expansion in helper.hpp: CodeBody gen_ast_inlines() 2023-08-08 15:53:10 -04:00
Ed_
67d02c1f62 Fix for wrong tokens for GNU/MSVC attribute captures (parse_attributes)
Also a fix for a typo in the readme...
2023-08-08 15:35:06 -04:00
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
Ed_
a8a9b681f0 test completes singleheader ast construction and serailizes with corruption 2023-08-02 14:01:56 -04:00
Ed_
b96b0821c1 Fixes towards parsing (getting to line 12575 now of the singleheader. 2023-08-02 12:39:35 -04:00
Ed_
4c8a0f0005 Iterations on serialization improvements. 2023-08-01 20:56:00 -04:00
Ed_
684569750d first serialization of singlehearder without asserts. (Still failing after around 4k lines. 2023-08-01 16:07:47 -04:00
Ed_
0f16d1131e Got past parsing, fixing serialization 2023-08-01 14:02:54 -04:00
Ed_
528ef72a51 More progress on parsing
Made it to line 2597 of self parsing its singleheader

Complex global or member defintions are now supported.
2023-08-01 05:17:24 -04:00
Ed_
21a8f3bb39 WIP: It can parse to around ~2k lines. Need to improve its ability to detect when a forward declare of a class/enum/struct/union..
This language truly is a mess.
2023-08-01 00:42:08 -04:00
Ed_
2b63fc27cd Progress toward preprocessor parsing, lexing works, parsing does not. 2023-07-30 18:55:57 -04:00
Ed_
bfbfae466f Naive preprocessor support initial implementation (compiles and runs, not heavily tested) 2023-07-30 01:21:04 -04:00
Ed_
3d7cb85e71 Merge pull request #8 from Ed94/parser_context_stack
Parser context stack
2023-07-29 17:16:04 -04:00
178 changed files with 25883 additions and 9771 deletions

41
.gitignore vendored
View File

@ -1,29 +1,34 @@
.idea .idea
build/* **/build/*
.vs .vs
**/*.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
# Build results **/*.lib
[Dd]ebug/ **/*.pdb
[Dd]ebugPublic/ **/*.exe
[Rr]elease/ **/*.dll
[Rr]eleases/
x64/ release/**
x86/
[Ww][Ii][Nn]32/ # Unreal
[Aa][Rr][Mm]/ **/Unreal/*.h
[Aa][Rr][Mm]64/ **/Unreal/*.cpp
bld/ ! **/Unreal/validate.unreal.cpp
[Bb]in/ project/auxillary/vis_ast/dependencies/temp
[Oo]bj/ test/gen/original
[Ll]og/ singleheader/gen/scratch.hpp
[Ll]ogs/ test/gen/scratch.cpp
gen_c_library/gen

14
.vscode/bookmarks.json vendored Normal file
View File

@ -0,0 +1,14 @@
{
"files": [
{
"path": "project/auxillary/vis_ast/dependencies/temp/raylib-master/src/rcamera.h",
"bookmarks": [
{
"line": 140,
"column": 14,
"label": ""
}
]
}
]
}

View File

@ -1,7 +1,7 @@
{ {
"configurations": [ "configurations": [
{ {
"name": "Win32", "name": "Win32 msvc",
"includePath": [ "includePath": [
"${workspaceFolder}/**" "${workspaceFolder}/**"
], ],
@ -9,11 +9,36 @@
"_DEBUG", "_DEBUG",
"UNICODE", "UNICODE",
"_UNICODE", "_UNICODE",
"GEN_TIME" "GEN_TIME",
"GEN_IMPLEMENTATION",
// "GEN_DONT_USE_NAMESPACE"
"GEN_INTELLISENSE_DIRECTIVES",
"INTELLISENSE_DIRECTIVES"
],
"windowsSdkVersion": "10.0.19041.0",
"compilerPath": "C:/Program Files/Microsoft Visual Studio/2022/Professional/VC/Tools/MSVC/14.29.30133/bin/HostX64/x64/cl.exe",
"intelliSenseMode": "msvc-x64",
"compileCommands": "${workspaceFolder}/project/build/compile_commands.json"
},
{
"name": "Win32 clang",
"includePath": [
"${workspaceFolder}/**"
],
"defines": [
"_DEBUG",
"UNICODE",
"_UNICODE",
"GEN_TIME",
"GEN_IMPLEMENTATION",
// "GEN_DONT_USE_NAMESPACE"
"GEN_INTELLISENSE_DIRECTIVES",
"INTELLISENSE_DIRECTIVES"
], ],
"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

37
.vscode/launch.json vendored
View File

@ -8,9 +8,9 @@
"type": "lldb", "type": "lldb",
"request": "launch", "request": "launch",
"name": "Debug gentime lldb", "name": "Debug gentime lldb",
"program": "${workspaceFolder}/test/gen/build/gencpp.exe", "program": "${workspaceFolder}/test/test.exe",
"args": [], "args": [],
"cwd": "${workspaceFolder}/test/gen/", "cwd": "${workspaceFolder}/test/",
"postRunCommands": [ "postRunCommands": [
] ]
}, },
@ -18,16 +18,16 @@
"type": "cppvsdbg", "type": "cppvsdbg",
"request": "launch", "request": "launch",
"name": "Debug gentime vsdbg", "name": "Debug gentime vsdbg",
"program": "${workspaceFolder}/test/gen/build/gencpp.exe", "program": "${workspaceFolder}/test/build/test.exe",
"args": [], "args": [],
"cwd": "${workspaceFolder}/test/gen/", "cwd": "${workspaceFolder}/test/",
"visualizerFile": "${workspaceFolder}/scripts/gencpp.natvis" "visualizerFile": "${workspaceFolder}/scripts/gencpp.natvis"
}, },
{ {
"type": "cppvsdbg", "type": "cppvsdbg",
"request": "launch", "request": "launch",
"name": "Debug bootstrap vsdbg", "name": "Debug bootstrap vsdbg",
"program": "${workspaceFolder}/project/build/gencpp_bootstrap.exe", "program": "${workspaceFolder}/project/build/bootstrap.exe",
"args": [], "args": [],
"cwd": "${workspaceFolder}/project/", "cwd": "${workspaceFolder}/project/",
"visualizerFile": "${workspaceFolder}/scripts/gencpp.natvis" "visualizerFile": "${workspaceFolder}/scripts/gencpp.natvis"
@ -40,6 +40,33 @@
"args": [], "args": [],
"cwd": "${workspaceFolder}/singleheader/", "cwd": "${workspaceFolder}/singleheader/",
"visualizerFile": "${workspaceFolder}/scripts/gencpp.natvis" "visualizerFile": "${workspaceFolder}/scripts/gencpp.natvis"
},
{
"type": "cppvsdbg",
"request": "launch",
"name": "Debug unreal vsdbg",
"program": "${workspaceFolder}/unreal_engine/build/unreal.exe",
"args": [],
"cwd": "${workspaceFolder}/unreal_engine/",
"visualizerFile": "${workspaceFolder}/scripts/gencpp.natvis"
},
{
"type": "cppvsdbg",
"request": "launch",
"name": "Debug raylib refactor vsdbg",
"program": "${workspaceFolder}/project/auxillary/vis_ast/dependencies/raylib/build/raylib_refactor.exe",
"args": [],
"cwd": "${workspaceFolder}/project/auxillary/vis_ast/dependencies/temp/raylib-master/src/",
"visualizerFile": "${workspaceFolder}/scripts/gencpp.natvis"
},
{
"type": "cppvsdbg",
"request": "launch",
"name": "Debug VIS AST",
"program": "${workspaceFolder}/project/auxillary/vis_ast/binaries/vis_ast.exe",
"args": [],
"cwd": "${workspaceFolder}/project/auxillary/vis_ast/binaries/",
"visualizerFile": "${workspaceFolder}/scripts/gencpp.natvis"
} }
] ]
} }

28
.vscode/settings.json vendored
View File

@ -18,7 +18,29 @@
"algorithm": "cpp", "algorithm": "cpp",
"limits": "cpp", "limits": "cpp",
"concepts": "cpp", "concepts": "cpp",
"*.rh": "cpp" "*.rh": "cpp",
"chrono": "cpp",
"string": "cpp",
"filesystem": "cpp",
"format": "cpp",
"ratio": "cpp",
"xstring": "cpp",
"functional": "cpp",
"vector": "cpp",
"list": "cpp",
"xhash": "cpp",
"glfw3.h": "c",
"stdbool.h": "c",
"objbase.h": "c",
"mmreg.h": "c",
"mmsystem.h": "c",
"propidl.h": "c",
"android_native_app_glue.h": "c",
"raylib.h": "c",
"*.m": "cpp",
"atomic": "cpp",
"gen.h": "c",
"string_ops.hpp": "c"
}, },
"C_Cpp.intelliSenseEngineFallback": "disabled", "C_Cpp.intelliSenseEngineFallback": "disabled",
"mesonbuild.configureOnOpen": true, "mesonbuild.configureOnOpen": true,
@ -30,5 +52,7 @@
"**/.vscode": true, "**/.vscode": true,
"**/.vs": true, "**/.vs": true,
"**/sanity.gen.hpp": true "**/sanity.gen.hpp": true
} },
"autoHide.autoHidePanel": false,
"autoHide.autoHideSideBar": false
} }

View File

@ -5,25 +5,25 @@ An attempt at simple staged metaprogramming for c/c++.
The library API is a composition of code element constructors. 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 project domain.
### TOC
* [Notes](#notes)
* [Usage](#usage)
* [Building](#building)
## 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. **On Partial Hiatus: Life has got me tackling other issues..**
The [issues](https://github.com/Ed94/gencpp/issues) marked with v1.0 Feature indicate whats left before the library is considered feature complete. I will be passively updating the library with bug fixes and minor improvements as I use it for my personal projects.
There won't be any major reworks or features to this thing for a while.
A `natvis` and `natstepfilter` are provided in the scripts directory. This project is still in development (very much an alpha state), so expect bugs and missing features.
See [issues](https://github.com/Ed94/gencpp/issues) for a list of known bugs or todos.
The library can already be used to generate code just fine, but the parser is where the most work is needed. If your C++ isn't "down to earth" expect issues.
A `natvis` and `natstepfilter` are provided in the scripts directory (its outdated, I'll update this readme when its not).
***The editor and scanner have not been implemented yet. The scanner will come first, then the editor.*** ***The editor and scanner have not been implemented yet. The scanner will come first, then the editor.***
A C variant is hosted [here](https://github.com/Ed94/genc); I haven't gotten headwind on it, should be easier to make than this... A C variant is hosted [here](https://github.com/Ed94/genc); I will complete it when this library is feature complete, it should be easier to make than this...
## Usage ## Usage
@ -31,7 +31,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` :
@ -47,6 +47,8 @@ u32 gen_main()
} }
#endif #endif
// "Stage" agnostic code.
#ifndef GEN_TIME #ifndef GEN_TIME
#include "program.gen.cpp" #include "program.gen.cpp"
@ -62,9 +64,10 @@ 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(usize) );
Code t_allocator = def_type( name(allocator) ); Code t_allocator = def_type( name(allocator) );
Code t_string_const = def_type( name(char), def_specifiers( args( ESpecifier::Const, ESpecifier::Ptr ) )); Code t_string_const = def_type( name(char), def_specifiers( args( ESpecifier::Const, ESpecifier::Ptr ) ));
@ -81,12 +84,14 @@ Code header;
### Parse ### Parse
Validation through ast construction.
```cpp ```cpp
Code header = parse_struct( code( Code header = parse_struct( code(
struct ArrayHeader struct ArrayHeader
{ {
uw Num; usize Num;
uw Capacity; usize Capacity;
allocator Allocator; allocator Allocator;
}; };
)); ));
@ -95,12 +100,14 @@ 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
{ {
uw Num; usize Num;
uw Capacity; usize Capacity;
allocator Allocator; allocator Allocator;
}; };
); );
@ -116,14 +123,14 @@ All three constrcuton interfaces will generate the following C code:
```cpp ```cpp
struct ArrayHeader struct ArrayHeader
{ {
uw Num; usize Num;
uw Capacity; usize Capacity;
allocator Allocator; allocator Allocator;
}; };
``` ```
**Note: The formatting shown here is not how it will look. For your desired formatting its recommended to run a pass through the files with an auto-formatter.** **Note: The formatting shown here is not how it will look. For your desired formatting its recommended to run a pass through the files with an auto-formatter.**
*(The library currently uses clang-format for formatting, beaware its pretty slow...)* *(The library currently uses clang-format for formatting, beware its pretty slow...)*
## Building ## Building

44
docs/AST_Design.md Normal file
View File

@ -0,0 +1,44 @@
## Current Design
`AST` is the actual managed node object for the library.
Its raw and really not meant to be used directly.
All user interaction must be with its pointer so the type they deal with is `AST*`.
For user-facing code, they should never be giveen a nullptr. Instead, they should be given a designated `Invalid` AST node.
In order to abstract away constant use of `AST*`, I wanted to provide a wrapper for it.
The simpliest being just a type alias.
```cpp
using Code = AST*;
```
This is what the genc library would have to use due to its constraints of a langauge.
The actual content per type of AST is covered within [AST_Types.md](AST_Types.md).
These are pure PODS that just have the lay members relevant to the type of AST node they represent.
Each of them has a Code type alias specific to it.
Again, the simpliest case for these would be a type alias.
```cpp
using struct AST_Typedef CodeTypedef;
```
As of November 21st, 2023, the AST has had a strict layout for how its content is laid out.
This will be abandoned during its redesign that will occur starting with support for statments & expressions for either execution and type declarations.
Having a strict layout is too resctrictive vs allowing each AST type to have maximum control over the layout.
The redesign will occur after the following todos are addressed:
* [Improvements Lexer & Token struct#27](https://github.com/Ed94/gencpp/issues/27)
* [Generalize AST Flags to a single 4-byte flag#42](https://github.com/Ed94/gencpp/issues/42)
* [AST-Code Object Redesign.#38](https://github.com/Ed94/gencpp/issues/38)
* [Code-AST Documentation#40](https://github.com/Ed94/gencpp/issues/40)
* [AST::debug_str() improvements#33](https://github.com/Ed94/gencpp/issues/33)
* [AST::is_equal implemented and works with singleheader-test#31](https://github.com/Ed94/gencpp/issues/31)
* [Parser : Add ability to have a parse failure and continue with errors recorded.#35](https://github.com/Ed94/gencpp/issues/35)
* [Scanner : Add CodeFile#29](https://github.com/Ed94/gencpp/issues/29)
* [Auxiliary : AST visual debugger#36](https://github.com/Ed94/gencpp/issues/36)

737
docs/AST_Types.md Normal file
View File

@ -0,0 +1,737 @@
# AST Types Documentation
While the Readme for docs covers the data layout per AST, this will focus on the AST types avaialble, and their nuances.
## Body
These are containers representing a scope body of a definition that can be of the following `ECode` type:
* Class_Body
* Enum_Body
* Export_Body
* Extern_Linkage_Body
* Function_Body
* Global_Body
* Namespace_Body
* Struct_Body
* Union_Body
Fields:
```cpp
Code Front;
Code Back;
parser::Token* Tok;
Code Parent;
StringCached Name;
CodeT Type;
s32 NumEntries;
```
The `Front` member represents the start of the link list and `Back` the end.
NumEntries is the number of entries in the body.
Parent should have a compatible ECode type for the type of defintion used.
Serialization:
Will output only the entries, the braces are handled by the parent.
```cpp
<Front>...
<Back>
```
## Attributes
Represent standard or vendor specific C/C++ attributes.
Fields:
```cpp
StringCached Content;
Code Prev;
Code Next;
parser::Token* Tok;
Code Parent;
StringCached Name;
CodeT Type;
```
Serialization:
```cpp
<Content>
```
While the parser supports the `__declspec` and `__attribute__` syntax, the upfront constructor ( def_attributes ) must have the user specify the entire attribute, including the `[[]]`, `__declspec` or `__attribute__` parts.
## Comment
Stores a comment.
Fields:
```cpp
StringCached Content;
Code Prev;
Code Next;
parser::Token* Tok;
Code Parent;
StringCached Name;
CodeT Type;
```
Serialization:
```cpp
<Content>
```
The parser will perserve comments found if residing with a body or in accepted inline-to-definition locations.
Otherwise they will be skipped by the TokArray::__eat and TokArray::current( skip foramtting enabled ) functions.
The upfront constructor: `def_comment` expects to recieve a comment without the `//` or `/* */` parts. It will add them during construction.
## Class & Struct
Fields:
```cpp
CodeComment InlineCmt; // Only supported by forward declarations
CodeAttributes Attributes;
CodeType ParentType;
CodeBody Body;
CodeType Prev; // Used to store references to interfaces
CodeType Next; // Used to store references to interfaces
parser::Token* Tok;
Code Parent;
StringCached Name;
CodeT Type;
ModuleFlag ModuleFlags;
AccessSpec ParentAccess;
```
Serialization:
```cpp
// Class_Fwd
<ModuleFlags> <class/struct> <Name>; <InlineCmt>
// Class
<ModuleFlags> <class/struct> <Attributes> <Name> : <ParentAccess> <ParentType>, public <ParentType->Next>, ... <InlineCmt>
{
<Body>
};
```
You'll notice that only one parent type is supported only with parent access. This library only supports single inheritance, the rest must be done through interfaces.
## Constructor
Fields:
```cpp
CodeComment InlineCmt; // Only supported by forward declarations
Code InitializerList;
CodeParam Params;
Code Body;
Code Prev;
Code Next;
parser::Token* Tok;
Code Parent;
StringCached Name;
CodeT Type;
```
Serialization:
```cpp
// Constructor_Fwd
<Specs> <Parent->Name>( <Params> ); <InlineCmt>
// Constructor
<Specs> <Parent->Name>( <Params> ) <InlineCmt>
: <InitializerList>
{
<Body>
}
// Constructor Source Implementation
<Specs> <Parent>::~<Parent->Name>( <Params> ) <Specs>
{
<Body>
}
```
## Define
Represents a preprocessor define
Fields:
```cpp
StringCached Content;
Code Prev;
Code Next;
parser::Token* Tok;
Code Parent;
StringCached Name;
CodeT Type;
```
Serialization:
```cpp
#define <Name> <Content>
```
## Destructor
Fields:
```cpp
CodeComment InlineCmt;
CodeSpecifiers Specs;
Code Body;
Code Prev;
Code Next;
parser::Token* Tok;
Code Parent;
StringCached Name;
CodeT Type;
```
Serialization:
```cpp
// Destructor_Fwd
<Specs> ~<Parent->Name>( <Params> ) <Specs>; <InlineCmt>
// Destructor
<Specs> ~<Parent->Name>( <Params> ) <Specs>
{
<Body>
}
// Destructor Source Implementation
<Specs> <Parent>::~<Parent->Name>( <Params> ) <Specs>
{
<Body>
}
```
## Enum
Fields:
```cpp
CodeComment InlineCmt;
CodeAttributes Attributes;
CodeType UnderlyingType;
CodeBody Body;
Code Prev;
Code Next;
parser::Token* Tok;
Code Parent;
StringCached Name;
CodeT Type;
ModuleFlag ModuleFlags;
```
Serialization:
```cpp
// Enum_Fwd
<ModuleFlags> enum class <Name> : <UnderlyingType>; <InlineCmt>
// Enum
<ModuleFlags> <enum or enum class> <Name> : <UnderlyingType>
{
<Body>
};
```
## Execution
Just represents an execution body. Equivalent to an untyped body.
Will be obsolute when function body parsing is implemented.
Fields:
```cpp
StringCached Content;
Code Prev;
Code Next;
parser::Token* Tok;
Code Parent;
StringCached Name;
CodeT Type;
```
Serialization:
```cpp
<Content>
```
## External Linkage
Fields:
```cpp
CodeBody Body;
Code Prev;
Code Next;
parser::Token* Tok;
Code Parent;
StringCached Name;
CodeT Type;
```
Serialization:
```cpp
extern "<Name>"
{
<Body>
}
```
## Include
Fields:
```cpp
StringCached Content;
Code Prev;
Code Next;
Code Parent;
parser::Token* Tok;
StringCached Name;
CodeT Type;
```
Serialization:
```cpp
#include <Content>
```
## Friend
This library (until its necessary become some third-party library to do otherwise) does not support friend declarations with in-statment function definitions.
Fields:
```cpp
CodeComment InlineCmt;
Code Declaration;
Code Prev;
Code Next;
parser::Token* Tok;
Code Parent;
StringCached Name;
CodeT Type;
```
Serialization:
```cpp
friend <Declaration>; <InlineCmt>
```
## Function
Fields:
```cpp
CodeComment InlineCmt;
CodeAttributes Attributes;
CodeSpecifiers Specs;
CodeType ReturnType;
CodeParam Params;
CodeBody Body;
Code Prev;
Code Next;
parser::Token* Tok;
Code Parent;
StringCached Name;
CodeT Type;
ModuleFlag ModuleFlags;
```
Serialization:
```cpp
// Function_Fwd
<ModuleFlags> <Attributes> <Specs> <ReturnType> <Name>( <Params> ) <Specs>; <InlineCmt>
// Function
<ModuleFlags> <Attributes> <Specs> <ReturnType> <Name>( <Params> ) <Specs>
{
<Body>
}
```
## Module
Fields:
```cpp
Code Prev;
Code Next;
parser::Token* Tok;
Code Parent;
StringCached Name;
CodeT Type;
ModuleFlag ModuleFlags;
```
Serialization:
```cpp
<ModuleFlags> module <Name>;
```
## Namespace
Fields:
```cpp
CodeBody Body;
Code Prev;
Code Next;
parser::Token* Tok;
Code Parent;
StringCached Name;
CodeT Type;
ModuleFlag ModuleFlags;
```
Serialization:
```cpp
<ModuleFlags> namespace <Name>
{
<Body>
}
```
## Operator Overload
Fields:
```cpp
CodeComment InlineCmt;
CodeAttributes Attributes;
CodeSpecifiers Specs;
CodeType ReturnType;
CodeParam Params;
CodeBody Body;
Code Prev;
Code Next;
parser::Token* Tok;
Code Parent;
StringCached Name;
CodeT Type;
ModuleFlag ModuleFlags;
OperatorT Op;
```
Serialization:
```cpp
// Operator_Fwd
<ModuleFlags> <Attributes> <Specs> <ReturnType> operator <Op>( <Params> ) <Specs>; <InlineCmt>
// Operator
<ModuleFlags> <Attributes> <Specs> <ReturnType> <Name>operator <Op>( <Params> ) <Specs>
{
<Body>
}
```
## Operator Cast Overload ( User-Defined Type Conversion )
Fields:
```cpp
CodeComment InlineCmt;
CodeSpecifiers Specs;
CodeType ValueType;
CodeBody Body;
Code Prev;
Code Next;
parser::Token* Tok;
Code Parent;
StringCached Name;
CodeT Type;
```
Serialization:
```cpp
// Operator_Cast_Fwd
<Specs> operator <ValueType>() <Specs>; <InlineCmt>
// Operator_Cast
<Specs> <Name>operator <ValueType>() <Specs>
{
<Body>
}
```
## Parameters (AST_Param)
Fields:
```cpp
CodeType ValueType;
Code Macro;
Code Value;
CodeParam Last;
CodeParam Next;
parser::Token* Tok;
Code Parent;
StringCached Name;
CodeT Type;
s32 NumEntries;
```
Serialization:
```cpp
<Macro>, <Next> ... <Last>
<Macro> <ValueType> <Name>, <Next>... <Last>
```
## Pragma
Fields:
```cpp
StringCached Content;
Code Prev;
Code Next;
parser::Token* Tok;
Code Parent;
StringCached Name;
CodeT Type;
```
Serialization:
```cpp
#pragma <Content>
```
## Preprocessor Conditional
Fields:
```cpp
StringCached Content;
Code Prev;
Code Next;
paser::Token* Tok;
Code Parent;
StringCached Name;
CodeT Type;
```
Serialization:
```cpp
#<based off Type> <Content>
```
## Specifiers
Fields:
```cpp
SpecifierT ArrSpecs[ AST_ArrSpecs_Cap ];
CodeSpecifiers NextSpecs;
Code Prev;
Code Next;
parser::Token* Tok;
Code Parent;
StringCached Name;
CodeT Type;
s32 NumEntries;
```
Serialization:
```cpp
<Spec>, ...
```
## Template
Fields:
```cpp
CodeParam Params;
Code Declaration;
Code Prev;
Code Next;
parser::Token* Tok;
Code Parent;
StringCached Name;
CodeT Type;
ModuleFlag ModuleFlags;
```
Serialization:
```cpp
<ModuleFlags>
template< <Params> >
<Declaration>
```
## Typename
Typenames represent the type "symbol".
Fields:
```cpp
CodeAttributes Attributes;
CodeSpecifiers Specs;
CodeReturnType ReturnType;
CodeParam Params;
Code ArrExpr;
Code Prev;
Code Next;
parser::Token* Tok;
Code Parent;
StringCached Name;
CodeT Type;
b32 IsParamPack;
```
Serialization:
```cpp
<Attributes> <Name> <Specs> <IsParamPack ?: ...>
```
## Typedef
Behave as usual except function or macro typedefs.
Those (macros) don't use the underlying type field as everything was serialized under the Name field.
Fields:
```cpp
CodeComment InlineCmt;
Code UnderlyingType;
Code Prev;
Code Next;
parse::Token* Tok
Code Parent;
StringCached Name;
CodeT Type;
ModuleFlag ModuleFlags;
b32 IsFunction;
```
Serialization:
```cpp
// Regular
<ModuleFlags> typedef <UnderlyingType> <Name>; <InlineCmt>
// Functions
<ModuleFlags> typedef <ReturnType> <Name>( <Parameters> ); <InlineCmt>
<ModuleFlags> typedef <ReturnType> ( <Expression that yeilds an Identifier signature> )( <Parameters> ); <InlineCmt>
```
## Union
Fields:
```cpp
CodeAttributes Attributes;
CodeBody Body;
Code Prev;
Code Next;
parser::Token* Tok;
Code Parent;
StringCached Name;
CodeT Type;
ModuleFlag ModuleFlags;
```
Serialization:
```cpp
<ModuleFlags> union <Attributes> <Name>
{
<Body>
}
```
## Using
Fields:
```cpp
CodeComment InlineCmt;
CodeAttributes Attributes;
CodeType UnderlyingType;
Code Prev;
Code Next;
parser::Token* Tok;
Code Parent;
StringCached Name;
CodeT Type;
ModuleFlag ModuleFlags;
```
Serialization:
```cpp
// Regular
<ModuleFlags> using <Attributes> <Name> = <UnderlyingType>; <InlineCmt>
// Namespace
<ModuleFlags> using namespace <Name>; <InlineCmt>
```
## Variable
[Algo](./Parser_Algo.md:)
Fields:
```cpp
CodeComment InlineCmt;
CodeAttributes Attributes;
CodeSpecifiers Specs;
CodeType ValueType;
Code BitfieldSize;
Code Value;
CodeVar NextVar;
Code Prev;
Code Next;
parser::Token* Tok;
Code Parent;
StringCached Name;
CodeT Type;
ModuleFlag ModuleFlags;
```
Serialization:
```cpp
// Regular
<ModuleFlags> <Attributes> <Specs> <ValueType> <Name> = <Value>, NextVar ...; <InlineCmt>
// Bitfield
<ModuleFlags> <Attributes> <Specs> <ValueType> <Name> : <BitfieldSize> = <Value>, NextVar ...; <InlineCmt>
```

702
docs/Parser_Algo.md Normal file
View File

@ -0,0 +1,702 @@
# Parser's Algorithim
gencpp uses a hand-written recursive descent parser. Both the lexer and parser currently handle a full C/C++ file in a single pass.
## Notable implementation background
### Lexer
The lex procedure does the lexical pass of content provided as a `StrC` type.
The tokens are stored (for now) in `gen::parser::Tokens`.
Fields:
```cpp
Array<Token> Arr;
s32 Idx;
```
What token types are supported can be found in [ETokType.csv](../project/enums/ETokType.csv) you can also find the token types in [ETokType.h](../project/components/gen/etoktype.cpp) , which is the generated enum from the csv file.
Tokens are defined with the struct `gen::parser::Token`:
Fields:
```cpp
char const* Text;
sptr Length;
TokType Type;
s32 Line;
s32 Column;
u32 Flags;
```
Flags is a bitfield made up of TokFlags (Token Flags):
* `TF_Operator` : Any operator token used in expressions
* `TF_Assign`
* Using statment assignment
* Parameter argument default value assignment
* Variable declaration initialization assignment
* `TF_Preprocess` : Related to a preprocessing directive
* `TF_Preprocess_Cond` : A preprocess conditional
* `TF_Attribute` : An attribute token
* `TF_AccessSpecifier` : An accesor operation token
* `TF_Specifier` : One of the specifier tokens
* `TF_EndDefinition` : Can be interpreted as an end definition for a scope.
* `TF_Formatting` : Considered a part of the formatting
* `TF_Literal` : Anything considered a literal by C++.
I plan to replace IsAssign with a general flags field and properly keep track of all operator types instead of abstracting it away to `ETokType::Operator`.
Traversing the tokens is done with the following interface macros:
| Macro | Description |
| --- | --- |
| `currtok_noskip` | Get the current token without skipping whitespace |
| `currtok` | Get the current token, skip any whitespace tokens |
| `prevtok` | Get the previous token (does not skip whitespace) |
| `nexttok` | Get the next token (does not skip whitespace) |
| `eat( Token Type )` | Check to see if the current token is of the given type, if so, advance Token's index to the next token |
| `left` | Get the number of tokens left in the token array |
| `check_noskip` | Check to see if the current token is of the given type, without skipping whitespace |
| `check` | Check to see if the current token is of the given type, skip any whitespace tokens |
### Parser
The parser has a limited user interface, only specific types of definitions or statements are expected to be provided by the user directly when using to construct an AST dynamically (See SOA for example). It however does attempt to provide capability to parse a full C/C++ from production codebases.
Each public user interface procedure has the following format:
```cpp
<code type> parse_<definition type>( StrC def )
{
check_parse_args( def );
using namespace Parser;
TokArray toks = lex( def );
if ( toks.Arr == nullptr )
return CodeInvalid;
// Parse the tokens and return a constructed AST using internal procedures
...
}
```
The most top-level parsing procedure used for C/C++ file parsing is `parse_global_body`:
It uses a helper procedure called `parse_global_nspace`.
Each internal procedure will have the following format:
```cpp
internal
<code type> parse_<definition_type>( <empty or contextual params> )
{
push_scope();
...
<code type> result = (<code type>) make_code();
...
Context.pop();
return result;
}
```
Below is an outline of the general alogirithim used for these internal procedures. The intention is to provide a basic briefing to aid the user in traversing the actual code definitions. These appear in the same order as they are in the `parser.cpp` file
***NOTE: This is still heavily in an alpha state. A large swaph of this can change, make sure these docs are up to date before considering them 1:1 with the repo commit your considering.***
## `parse_array_decl`
1. Check if its an array declaration with no expression.
1. Consume and return empty array declaration
2. Opening square bracket
3. Consume expression
4. Closing square bracket
5. If adjacent opening bracket
1. Repeat array declaration parse until no brackets remain
## `parse_assignment_expression`
1. Eat the assignment operator
2. Make sure there is content or at least an end statement after.
3. Flatten the assignment expression to an untyped Code string.
## `parse_attributes`
1. Check for standard attribute
2. Check for GNU attribute
3. Check for MSVC attribute
4. Check for a token registered as an attribute
a. Check and grab the arguments of a token registered of an attribute if it has any.
5. Repeat for chained attributes. Flatten them to a single attribute AST node.
## `parse_class_struct`
1. Check for export module specifier
2. class or struct keyword
3. `parse_attributes`
4. If identifier : `parse_identifier`
5. Parse inherited parent or interfaces
6. If opening curly brace : `parse_class_struct_body`
7. If not an inplace definition
1. End statement
2. Check for inline comment
## `parse_class_struct_body`
1. Opening curly brace
2. Parse the body (Possible options):
1. Ignore dangling end statements
2. Newline : ast constant
3. Comment : `parse_comment`
4. Access_Public : ast constant
5. Access_Protected : ast constant
6. Access_Private : ast constant
7. Decl_Class : `parse_complicated_definition`
8. Decl_Enum : `parse_complicated_definition`
9. Decl_Friend : `parse_friend`
10. Decl_Operator : `parse_operator_cast`
11. Decl_Struct : `parse_complicated_definition`
12. Decl_Template : `parse_template`
13. Decl_Typedef : `parse_typedef`
14. Decl_Union : `parse_complicated_definition`
15. Decl_Using : `parse_using`
16. Operator == '~'
1. `parse_destructor`
17. Preprocess_Define : `parse_define`
18. Preprocess_Include : `parse_include`
19. Preprocess_Conditional (if, ifdef, ifndef, elif, else, endif) : `parse_preprocess_cond` or else/endif ast constant
20. Preprocess_Macro : `parse_simple_preprocess`
21. Preprocess_Pragma : `parse_pragma`
22. Preprocess_Unsupported : `parse_simple_preprocess`
23. StaticAssert : `parse_static_assert`
24. The following compound into a resolved definition or declaration:
1. Attributes (Standard, GNU, MSVC) : `parse_attributes`
2. Specifiers (consteval, constexpr, constinit, explicit, forceinline, inline, mutable, neverinline, static, volatile, virtual)
3. Possible Destructor : `parse_destructor`
4. Possible User defined operator cast : `parse_operator_cast`
5. Possible Constructor : `parse_constructor`
6. Something that has the following: (identifier, const, unsigned, signed, short, long, bool, char, int, double)
1. Possible Constructor `parse_constructor`
2. Possible Operator, Function, or varaible : `parse_operator_function_or_variable`
25. Something completely unknown (will just make untyped...) : `parse_untyped`
## `parse_comment`
1. Just wrap the token into a cached string ( the lexer did the processing )
## `parse_compilcated_definition`
This is a helper function used by the following functions to help resolve a declaration or definition:
* `parse_class_struct_body`
* `parse_global_nspace`
* `parse_union`
A portion of the code in `parse_typedef` is very similar to this as both have to resolve a similar issue.
1. Look ahead to the termination token (End statement)
2. Check to see if it fits the pattern for a forward declare
3. If the previous token was an identifier ( `token[-1]` ):
1. Look back one more token : `[-2]`
2. If the token has a closing brace its an inplace definition
3. If the `token[-2]` is an identifier & `token[-3]` is the declaration type, its a variable using a namespaced type.
4. If the `token[-2]` is an indirection, then its a variable using a namespaced/forwarded type.
5. If the `token[-2]` is an assign classifier, and the starting tokens were the which type with possible `class` token after, its an enum forward declaration.
6. If any of the above is the case, `parse_operator_function_or_variable`
4. If the `token[2]` is a vendor fundamental type (builtin) then it is an enum forward declaration.
5. If the previous token was a closing curly brace, its a definition : `parse_forward_or_definition`
6. If the previous token was a closing square brace, its an array definition : `parse_operator_function_or_variable`
## `parse_define`
1. Define directive
2. Get identifier
3. Get Content (Optional)
## `parse_forward_or_definition`
* Parse any of the following for either a forward declaration or definition:
1. Decl_Class : `parse_class`
2. Decl_Enum : `parse_enum`
3. Decl_Struct : `parse_struct`
4. Decl_Union : `parse_union`
## `parse_function_after_name`
This is needed as a function defintion is not easily resolvable early on, as such this function handles resolving a function
after its been made ceratin that the type of declaration or definition is indeed for a function signature.
By the point this function is called the following are known : export module flag, attributes, specifiers, return type, & name
1. `parse_parameters`
2. parse postfix specifiers (we do not check if the specifier here is correct or not to be here... yet)
3. If there is a body : `parse_body`
4. Otherwise :
1. Statment end
2. Check for inline comment
## `parse_function_body`
Currently there is no actual parsing of the function body. Any content with the braces is shoved into an execution AST node.
In the future statements and expressions will be parsed.
1. Open curly brace
2. Grab all tokens between the brace and the closing brace, shove them in a execution AST node.
3. Closing curly brace
## `parse_global_nspace`
1. Make sure this is being called for a valid type (namespace, global body, export body, linkage body)
2. If its not a global body, consume the opening curly brace
3. Parse the body (Possible options):
1. Ignore dangling end statements
2. NewLine : ast constant
3. Comment : `parse_comment`
4. Decl_Cass : `parse_complicated_definition`
5. Decl_Enum : `parse_complicated_definition`
6. Decl_Extern_Linkage : `parse_extern_link`
7. Decl_Namespace : `parse_namespace`
8. Decl_Struct : `parse_complicated_definition`
9. Decl_Template : `parse_template`
10. Decl_Typedef : `parse_typedef`
11. Decl_Union : `parse_complicated_definition`
12. Decl_Using : `parse_using`
13. Preprocess_Define : `parse_define`
14. Preprocess_Include : `parse_include`
15. Preprocess_If, IfDef, IfNotDef, Elif : `parse_preprocess_cond`
16. Preprocess_Else : ast constant
17. Preprocess_Endif : ast constant
18. Preprocess_Macro : `parse_simple_preprocess`
19. Preprocess_Pragma : `parse_pragma`
20. Preprocess_Unsupported : `parse_simple_preprocess`
21. StaticAssert : `parse_static_assert`
22. Module_Export : `parse_export_body`
23. Module_Import : NOT_IMPLEMENTED
24. The following compound into a resolved definition or declaration:
1. Attributes ( Standard, GNU, MSVC, Macro ) : `parse_attributes`
2. Specifiers ( consteval, constexpr, constinit, extern, forceinline, global, inline, internal_linkage, neverinline, static )
3. Is either ( identifier, const specifier, long, short, signed, unsigned, bool, char, double, int)
1. Attempt to parse as constrcutor or destructor : `parse_global_nspace_constructor_destructor`
2. If its an operator cast (definition outside class) : `parse_operator_cast`
3. Its an operator, function, or varaible : `parse_operator_function_or_varaible`
4. If its not a global body, consume the closing curly brace
## `parse_global_nspace_constructor_destructor`
1. Look ahead for the start of the arguments for a possible constructor/destructor
2. Go back past the identifier
3. Check to see if its a destructor by checking for the `~`
4. Continue the next token should be a `::`
5. Determine if the next valid identifier (ignoring possible template parameters) is the same as the first identifier of the function.
6. If it is we have either a constructor or destructor so parse using their respective functions (`parse_constructor`, `parse_destructor`).
## `parse_identifier`
This is going to get heavily changed down the line to have a more broken down "identifier expression" so that the qualifier, template args, etc, can be distinguished between the targeted identifier.
The function can parse all of them, however the AST node compresses them all into a string.
1. Consume first identifier
2. `parse_template_args`
3. While there is a static symbol accessor ( `::` )
1. Consume `::`
2. Consume member identifier
3. `parse_template args` (for member identifier)
4. If a `~` is encounted and the scope is for a destructor's identifier, do not consume it and return with what parsed.
## `parse_include`
1. Consume include directive
2. Consume the path
## `parse_operator_after_ret_type`
This is needed as a operator defintion is not easily resolvable early on, as such this function handles resolving a operator after its been made ceratin that the type of declaration or definition is indeed for a operator signature.
By the point this function is called the following are known : export module flag, attributes, specifiers, return type
1. If there is any qualifiers for the operator, parse them
2. Consume operator keyword
3. Determine the operator type (This will be offloaded to the lexer moreso than how it is now) & consume
4. `parse_params`
5. If there is no parameters this is operator is a member of pointer if its symbols is a *.
6. Parse postfix specifiers
7. If there is a opening curly brace, `parse function_body`
8. Otherwise: consume end statement, check for inline comment.
## `parse_operator_function_or_variable`
When this function is called, attribute and specifiers may have been resolved, however what comes next can still be either an operator, function, or varaible.
1. Check for preprocessor macro, if there is one : `parse_simple_preprocess`
2. `parse_type` (Does the bulk of the work)
3. Begin lookahead to see if we get qualifiers or we eventually find the operator declaration
4. If we find an operator keyword : `parse_operator_after_ret_type`
5. otherwise :
1. `parse_identifier`
2. If we se a opening parenthesis (capture start), its a function : `parse_function_after_name`
3. Its a variable : `parse_variable_after_name`
## `parse_pragma`
1. Consume pragma directive
2. Process the token content into cached string
## `parse_params`
1. Consume either a `(` or `<` based on `use_template_capture` arg
2. If the we immdiately find a closing token, consume it and finish.
3. If we encounter a varadic argument, consume it and return a `param_varadic` ast constant
4. `parse_type`
5. If we have a macro, parse it (Unreal has macros as tags to parameters and or as entire arguments).
6. So long as next token isn't a comma
a. If we have an identifier
1. Consume it
2. Check for assignment:
a. Consume assign operator
b. Parse the expression
7. While we continue to encounter commas
a. Consume them
b. Repeat steps 3 to 6.2.b
8. Consume the closing token
## `parse_preprocess_cond`
1. Parse conditional directive
2. Process directive's content expression
## `parse_simple_preprocess`
There is still decent room for improvement in this setup. Right now the entire macro's relevant tokens are shoved into an untyped AST. It would be better to store it instead in an `AST_Macro` node instead down the line.
1. Consume the macro token
2. Check for an opening curly brace
1. Consume opening curly brace
2. Until the closing curly is encountered consume all tokens.
3. If the parent context is a typedef
1. Check for end stement
1. Consume it
2. Consume potential inline comment
3. Otherwise do steps 3 to 3.1.2
4. Shove it all in an untyped string
## `parse_static_assert`
1. Consume static assert and opening curly brace
2. Consume all tokens until the the closing brace is reached.
3. Consume curly brace and end statement
4. Place all tokens within braces into a content for the assert.
## `parse_template_args`
This will get changed heavily once we have better support for typename expressions
1. Consume opening angle bracket
2. Consume all tokens until closing angle bracket
3. Consme closing angle bracket
4. Return the currtok with the ammended length.
## `parse_variable_after_name`
This is needed as a variable defintion is not easily resolvable early on, it takes a long evaluation period before its known that the declaration or definition is a variable. As such this function handles resolving a variable.
By the point this function is called the following are known : export module flag, attributes, specifiers, value type, name
1. If its an assignment, parse the assignment expression (currently to an untyped string)
2. If its an opening curly brace, parse the expression within (currnelty to an untyped stirng).
1. Consume the closing curly brace
3. If its a `:`, we're dealing with bitfield definition:
1. Consume the assign classifier
2. Consume the expression (currently to an untyped string)
4. If a comma is encountered : `parse_variable declaration_list`
5. Consume statement end
6. Check for inline comment
## `parse_variable_declaration_list`
1. Consume the comma
2. Parse specifiers
3. `parse_variable_after_name`
## `parse_class`
1. `parse_class_struct`
## `parse_constructor`
This currently doesn't support postfix specifiers (planning to in the future)
1. `parse_identifier`
2. `parse_parameters`
3. If currtok is a `:`
1. Consume `:`
2. Parse the initializer list
3. `parse_function_body`
4. If currtok is an opening curly brace
1. `parse_function_body`
5. Otherwise:
1. Consume statement end
2. Check for inline comment
## `parse_destructor`
1. Check for and consume virtual specifier
2. Check for the `~` operator
3. `parse_identifier`
4. Consume opening and closing parenthesis
5. Check for assignment operator:
1. Consume assignment op
2. Consume pure specifier `0`
6. If not pure virtual & currtok is opening curly brace:
1. `parse_function_body`
7. Otherwise:
1. Consume end statement
2. If currtok is comment : `parse_comment`
## `parse_enum`
1. Consume enum token
2. Check for and consume class token
3. `parse_attributes`
4. If there is an identifier consume it
5. Check for a `:`
1. Consume `:`
2. `parse_type`
6. If there is a body parse it (Consume `{`):
1. Newline : ast constant
2. Comment : `parse_comment`
3. Preprocess_Define : `parse_define`
4. Preprocess_Conditional (if, ifdef, ifndef, elif ) : `parse_preprocess_cond`
5. Preprocess_Else : ast constant
6. Preprocess_Endif : ast constant
7. Preprocess_Macro : `parse_simple_preprocess`
8. Preprocess_Pragma : `parse_pragma`
9. Preprocess_Unsupported : `parse_smple_preprocess`
10. An actual enum entry
1. Consume identifier
2. If there is an assignment operator:
1. Consume operator
2. Consume the expression (assigned to untyped string for now)
3. If a macro is encountered consume it (Unreal UMETA macro support)
3. If there is a comma, consume it
## `parse_export_body`
1. `parse_global_nspace`
## `parse_extern_link_body`
1. `parse_global_nspace`
## `parse_extern_link`
1. Consume Decl_Extern_Linkage
2. Consume the linkage identifier
3. `parse_extern_link_body`
## `parse_friend`
1. Consume `friend`
2. `parse_type`
3. If the currok is an identifier its a function declaration or definition
1. `parse_function_after_name`
4. Consume end statement so long as its not a function definion
5. Check for inline comment, `parse_comment` if exists
## `parse_function`
1. Check and parse for `export`
2. `parse_attributes`
3. Parse specifiers
4. `parse_type`
5. `parse_identifier`
6. `parse_function_after_name`
## `parse_namespace`
1. Consume namespace declaration
2. Parse identifier
3. `parse_global_namespace`
## `parse_operator`
1. Check for and parse export declaration
2. `parse_attributes`
3. Parse specifiers
4. `parse_type`
5. `parse_operator_after_ret_type`
## `parse_operator_cast`
1. Look for and parse a qualifier namespace for the cast (in-case this is defined outside the class's scope)
2. Consume operator declaration
3. `parse_type`
4. Consume opening and closing parethesis
5. Check for a const qualifiying specifier
6. Check to see if this is a definition (`{`)
1. Consume `{`
2. Parse body to untyped string (parsing statement and expressions not supported yet)
3. Consume `}`
7. Otherwise:
1. Consume end statement
2. Check for and consume comment : `parse_comment`
## `parse_struct`
1. `parse_class_struct`
## `parse_template`
Note: This currently doesn't support templated operator casts (going to need to add support for it)
1. Check for and parse export declaration
2. Consume template declaration
3. `parse_params`
4. Parse for any of the following:
1. Decl_Class : `parse_class`
2. Decl_Struct : `parse_struct`
3. Decl_Union : `parse_union`
4. Decl_Using : `parse_using`
5. The following compound into a resolved definition or declaration:
1. `parse_attributes`
2. Parse specifiers
3. Attempt to parse as constructor or destructor: `parse_global_nspace_constructor_destructor`
4. Otherwise: `parse_operator_function_or_variable`
## `parse_type`
This function's implementation is awful and not done correctly. It will most likely be overhauled in the future as I plan to segement the AST_Type into several AST varaints along with sub-types to help produce robust type expressions.
Hopefully I won't need to make authentic type expressions as I was hopeing to avoid that...
### Current Algorithim
Anything that is in the qualifier capture of the function typename is treated as an expression abstracted as an untyped string
1. `parse_attributes`
2. Parse specifiers
3. If the `parse_type` was called from a template parse, check to see if class was used instead of typname and consume as name.
4. This is where things get ugly for each of these depend on what the next token is.
1. If its an in-place definition of a class, enum, struct, or union:
2. If its a decltype (Not supported yet but draft impl there)
3. If its a compound native type expression (unsigned, char, short, long, int, float, dobule, etc )
4. Ends up being a regular type alias of an identifier
5. Parse specifiers (postfix)
6. We need to now look ahead to see If we're dealing with a function typename
7. If wer're dealing with a function typename:
1. Shove the specifiers, and identifier code we have so far into a return type typename's Name (untyped string)
1. Reset the specifiers code for the top-level typeanme
2. Check to see if the next token is an identifier:
1. `parse_identifier`
3. Check to see if the next token is capture start and is not the last capture ("qualifier capture"):
1. Consume `(`
2. Consume expresssion between capture
3. Consume `)`
4. `parse_params`
5. Parse postfix specifiers
8. Check for varaidic argument (param pack) token:
1. Consume varadic argument token
### WIP - Alternative Algorithim
Currently wrapped up via macro: `GEN_USE_NEW_TYPENAME_PARSING`
Anything that is in the qualifier capture of the function typename is treated as an expression abstracted as an untyped string
1. `parse_attributes`
2. Parse specifiers (prefix)
3. This is where things get ugly for each of these depend on what the next token is.
1. If its an in-place definition of a class, enum, struct, or union:
2. If its a decltype (Not supported yet but draft impl there)
3. If its a compound native type expression (unsigned, char, short, long, int, float, dobule, etc )
4. Ends up being a regular type alias of an identifier
4. Parse specifiers (postfix)
1. If any specifiers are found populate specifiers code with them.
5. We need to now look ahead to see If we're dealing with a function typename
6. If wer're dealing with a function typename:
1. Shove the specifiers, and identifier code we have so far into a return type typename's Name (untyped string)
1. Reset the specifiers code for the top-level typename
2. Check to see if the next token is an identifier:
1. `parse_identifier`
3. Check to see if the next token is capture start and is not the last capture ("qualifier capture"):
1. Consume `(`
2. Parse binding specifiers
3. `parse_identifier`
4. `parse_parameters` -> params_nested
5. Consume `)`
6. Construct a nested function typename definition for the qualifier `Name`
4. `parse_params` - > params
5. Parse postfix specifiers
7. Check for varaidic argument (param pack) token:
1. Consume varadic argument token
### **Later: Algorithim based on typename expressions**
## `parse_typedef`
1. Check for export module specifier
2. typedef keyword
3. If its a preprocess macro: Get the macro name
4. Else:
1. Check to see if its a complicated definition (in-place enum, class, struct, union)
2. If its a complicated definition:
1. Perform the look ahead similar to `parse_complicated_definition`'s implementation
2. Check to see if its a forward declaration : `parse_forward_declaration`
3. If end[-1] is an identifier:
1. Its either an in-place, varaible type qualified identifier, or indirection type:
1. `parse_foward_or_definition`
4. else if end[-1] is a closing curly brace
1. Its a definition: `parse_forward_or_definition`
5. else if end[-1] is a closing square brace
2. Its an array definition: `parse_type`
3. Else : `parse-type`
4. Check for identifier : Consume the token
5. `parse_array_decl`
5. Consume end statement
6. Check for inline comment : `parse_comment`
## `parse_union`
1. Check for export module specifier
2. union keyword
3. `parse_attributes`
4. Check for identifier
5. Parse the body (Possible options):
1. Newline
2. Comment
3. Decl_Class
4. Decl_Enum
5. Decl_Struct
6. Decl_Union
7. Preprocess_Define
8. Preprocess_Conditional (if, ifdef, ifndef, elif, else, endif)
9. Preprocess_Macro
10. Preprocess_Pragma
11. Unsupported preprocess directive
12. Variable
6. If its not an inplace definiton: End Statement
## `parse_using`
1. Check for export module specifier
2. using keyword
3. Check to see if its a using namespace
4. Get the identifier
5. If its a regular using declaration:
1. `parse_attributes`
2. `parse_type`
3. `parse_array_decl`
6. End statement
7. Check for inline comment
## `parse_variable`
1. Check for export module specifier
2. `parse_attributes`
3. `parse specifiers`
4. `parse_type`
5. `parse_identifier`
6. `parse_variable_after_name`

View File

@ -1,12 +1,41 @@
# 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) around ~5600 loc. I hope to keep it under 10k loc worst case.
You can think of this parser of a frontend parser vs a semantic parser. Its intuitively similar to WYSIWYG. What you precerive as the syntax from the user-side before the compiler gets a hold of it, is what you get.
User exposed interface:
```cpp
CodeClass parse_class ( StrC class_def );
CodeConstructor parse_constructor ( StrC constructor_def );
CodeDestructor parse_destructor ( StrC destructor_def );
CodeEnum parse_enum ( StrC enum_def );
CodeBody parse_export_body ( StrC export_def );
CodeExtern parse_extern_link ( StrC exten_link_def );
CodeFriend parse_friend ( StrC friend_def );
CodeFn parse_function ( StrC fn_def );
CodeBody parse_global_body ( StrC body_def );
CodeNS parse_namespace ( StrC namespace_def );
CodeOperator parse_operator ( StrC operator_def );
CodeOpCast parse_operator_cast( StrC operator_def );
CodeStruct parse_struct ( StrC struct_def );
CodeTemplate parse_template ( StrC template_def );
CodeType parse_type ( StrC type_def );
CodeTypedef parse_typedef ( StrC typedef_def );
CodeUnion parse_union ( StrC union_def );
CodeUsing parse_using ( StrC using_def );
CodeVar parse_variable ( StrC var_def );
```
To parse file buffers, use the `parse_global_body` function.
***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***. 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***, ***includes***, and ***pragmas***.
*(Conditionals and defines are a TODO)*
The keywords supported for the preprocessor are: The keywords supported for the preprocessor are:
@ -16,34 +45,37 @@ The keywords supported for the preprocessor are:
* ifdef * ifdef
* elif * elif
* endif * endif
* undef * pragma
Just like with actual preprocessor, 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.
The preprocessor lines are stored as members of their associated scope they are parsed within. ( Global, Namespace, Class/Struct )
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). 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:
The parsing implementation supports the following for the user: * function signatures are allowed for a preprocessed macro: `neverinline MACRO() { ... }`
* Disable with: `#define GEN_PARSER_DISABLE_MACRO_FUNCTION_SIGNATURES`
* typedefs allow for a preprocessed macro: `typedef MACRO();`
* Disable with: `#define GEN_PARSER_DISABLE_MACRO_TYPEDEF`
```cpp *(Exceptions are added on an on-demand basis)*
CodeClass parse_class ( StrC class_def ); *(See functions `parse_operator_function_or_variable` and `parse_typedef` )*
CodeEnum parse_enum ( StrC enum_def );
CodeBody parse_export_body ( StrC export_def );
CodeExtern parse_extern_link ( StrC exten_link_def);
CodeFriend parse_friend ( StrC friend_def );
CodeFn parse_function ( StrC fn_def );
CodeBody parse_global_body ( StrC body_def );
CodeNamespace parse_namespace ( StrC namespace_def );
CodeOperator parse_operator ( StrC operator_def );
CodeOpCast parse_operator_cast( StrC operator_def );
CodeStruct parse_struct ( StrC struct_def );
CodeTemplate parse_template ( StrC template_def );
CodeType parse_type ( StrC type_def );
CodeTypedef parse_typedef ( StrC typedef_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.*** Adding your own exceptions is possible by simply modifying the parser to allow for the syntax you need.
*Note: You could interpret this strictness as a feature. This would allow the user to see if their codebase or a third-party's codebase some some egregious preprocessor abuse.*
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 right afterthe return type.
* Or in the usual spot for class, structs, (*right after the declaration keyword*)
* typedefs have attributes with the type (`parse_type`)
* 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 during ast serialization.

View File

@ -25,6 +25,8 @@ This library was written in a subset of C++ where the following are not used at
* Exceptions * Exceptions
Polymorphic & Member-functions are used as an ergonomic choice, along with a conserative use of operator overloads. Polymorphic & Member-functions are used as an ergonomic choice, along with a conserative use of operator overloads.
The base library itself does not use anything but C-like features to allow for generating a derviative compatiable with C (WIP).
There are only 4 template definitions in the entire library. (`Array<Type>`, `Hashtable<Type>`, `swap<Type>`, and `AST/Code::cast<Type>`) There are only 4 template definitions in the entire library. (`Array<Type>`, `Hashtable<Type>`, `swap<Type>`, and `AST/Code::cast<Type>`)
Two generic templated containers are used throughout the library: Two generic templated containers are used throughout the library:
@ -32,7 +34,7 @@ Two generic templated containers are used throughout the library:
* `template< class Type> struct Array` * `template< class Type> struct Array`
* `template< class Type> struct HashTable` * `template< class Type> struct HashTable`
Both Code and AST definitions have a `template< class Type> Code/AST cast()`. Its just an alternative way to explicitly cast to each other. Both Code and AST definitions have a `template< class Type> Code/AST :: cast()`. Its just an alternative way to explicitly cast to each other.
`template< class Type> swap( Type& a, Type& b)` is used over a macro. `template< class Type> swap( Type& a, Type& b)` is used over a macro.
@ -40,26 +42,9 @@ 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)
* RAII : This needs support for constructors/destructor parsing
* Haven't gotten around to yet (its in the github issues)
Keywords kept from "Modern C++":
* constexpr : Great to store compile-time constants.
* consteval : Technically fine, need to make sure to execute in moderation.
* constinit : Better than constexpr at doing its job, however, its only c++ 20.
* export : Useful if c++ modules ever come around to actually being usable.
* import : ^^
* module : ^^
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).
I plan to add this only at the tail of the project parsing milestone.
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.
@ -77,54 +62,70 @@ 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 to the user via its interface.
However, the user may specifiy memory configuration. However, the user may specifiy memory configuration.
Data layout of AST struct: Data layout of AST struct (Subject to heavily change with upcoming redesign):
```cpp ```cpp
union { union {
struct struct
{ {
AST* Attributes; // Class, Enum, Function, Struct, Typedef, Union, Using, Variable AST* InlineCmt; // Class, Constructor, Destructor, Enum, Friend, Functon, Operator, OpCast, Struct, Typedef, Using, Variable
AST* Specs; // Function, Operator, Type symbol, Variable AST* Attributes; // Class, Enum, Function, Struct, Typedef, Union, Using, Variable
AST* Specs; // Destructor, Function, Operator, Typename, Variable
union { union {
AST* ParentType; // Class, Struct AST* InitializerList; // Constructor
AST* ReturnType; // Function, Operator AST* ParentType; // Class, Struct, ParentType->Next has a possible list of interfaces.
AST* UnderlyingType; // Enum, Typedef AST* ReturnType; // Function, Operator, Typename
AST* ValueType; // Parameter, Variable AST* UnderlyingType; // Enum, Typedef
AST* ValueType; // Parameter, Variable
}; };
AST* Params; // Function, Operator, Template
union { union {
AST* ArrExpr; // Type Symbol AST* Macro; // Parameters
AST* Body; // Class, Enum, Function, Namespace, Struct, Union AST* BitfieldSize; // Variable (Class/Struct Data Member)
AST* Declaration; // Friend, Template AST* Params; // Constructor, Function, Operator, Template, Typename
AST* Value; // Parameter, Variable };
union {
AST* ArrExpr; // Typename
AST* Body; // Class, Constructr, Destructor, Enum, Function, Namespace, Struct, Union
AST* Declaration; // Friend, Template
AST* Value; // Parameter, Variable
};
union {
AST* NextVar; // Variable; Possible way to handle comma separated variables declarations. ( , NextVar->Specs NextVar->Name NextVar->ArrExpr = NextVar->Value )
AST* SpecsFuncSuffix; // Only used with typenames, to store the function suffix if typename is function signature.
}; };
}; };
StringCached Content; // Attributes, Comment, Execution, Include StringCached Content; // Attributes, Comment, Execution, Include
SpecifierT ArrSpecs[AST::ArrSpecs_Cap]; // Specifiers struct {
SpecifierT ArrSpecs[AST_ArrSpecs_Cap]; // Specifiers
AST* NextSpecs; // 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.
b32 IsParamPack; // Used by typename to know if type should be considered a parameter pack.
OperatorT Op; OperatorT Op;
AccessSpec ParentAccess; AccessSpec ParentAccess;
s32 NumEntries; s32 NumEntries;
}; };
s32 Token; // Handle to the token, stored in the CodeFile (Otherwise unretrivable)
``` ```
*`CodeT` is a typedef for `ECode::Type` which has an underlying type of `u32`* *`CodeT` is a typedef for `ECode::Type` which has an underlying type of `u32`*
@ -137,14 +138,14 @@ The width dictates how much the static array can hold before it must give way to
```cpp ```cpp
constexpr static constexpr static
uw ArrSpecs_Cap = usize ArrSpecs_Cap =
( (
AST_POD_Size AST_POD_Size
- sizeof(AST*) * 3 - sizeof(AST*) * 3
- 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*)
``` ```
@ -154,7 +155,14 @@ 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`.
* Allocators are defined with the `AllocatorInfo` structure found in `dependencies\memory.hpp`
* Most of the work is just defining the allocation procedure:
```cpp
void* ( void* allocator_data, AllocType type, ssize size, ssize alignment, void* old_memory, ssize old_size, u64 flags );
```
* 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.
@ -167,15 +175,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`
@ -186,7 +194,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
@ -194,10 +201,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
@ -220,7 +229,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 :``
@ -230,6 +239,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
@ -244,6 +254,8 @@ Interface :``
* def_operator_cast * def_operator_cast
* def_param * def_param
* def_params * def_params
* def_pragma
* def_preprocess_cond
* def_specifier * def_specifier
* def_specifiers * def_specifiers
* def_struct * def_struct
@ -320,19 +332,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
@ -341,13 +340,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
@ -390,7 +382,7 @@ Code <name> = code_str( <some code without "" quotes > )
Template metaprogramming in the traditional sense becomes possible with the use of `token_fmt` and parse constructors: Template metaprogramming in the traditional sense becomes possible with the use of `token_fmt` and parse constructors:
```cpp ```cpp
StrC value = txt_StrC("Something"); StrC value = txt("Something");
char const* template_str = txt( char const* template_str = txt(
Code with <key> to replace with token_values Code with <key> to replace with token_values
@ -407,25 +399,31 @@ 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`
* `param_varaidc` (Used for varadic definitions) * `param_varaidc` (Used for varadic definitions)
* `pragma_once`
* `preprocess_else` * `preprocess_else`
* `preprocess_endif` * `preprocess_endif`
* `pragma_once`
* `spec_const` * `spec_const`
* `spec_consteval` * `spec_consteval`
* `spec_constexpr` * `spec_constexpr`
* `spec_constinit` * `spec_constinit`
* `spec_extern_linkage` (extern) * `spec_extern_linkage` (extern)
* `spec_final` * `spec_final`
* `spec_forceinline`
* `spec_global` (global macro) * `spec_global` (global macro)
* `spec_inline` * `spec_inline`
* `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`
@ -433,10 +431,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`
@ -458,8 +452,8 @@ Optionally the following may be defined if `GEN_DEFINE_LIBRARY_CODE_CONSTANTS` i
* `t_u16` * `t_u16`
* `t_u32` * `t_u32`
* `t_u64` * `t_u64`
* `t_sw` * `t_sw` (ssize_t)
* `t_uw` * `t_uw` (size_t)
* `t_f32` * `t_f32`
* `t_f64` * `t_f64`
@ -470,13 +464,14 @@ The AST and constructors will be able to validate that the arguments provided fo
* If return type must match a parameter * If return type must match a parameter
* If number of parameters is correct * If number of parameters is correct
* If added as a member symbol to a class or struct, that operator matches the requirements for the class (types match up) * If added as a member symbol to a class or struct, that operator matches the requirements for the class (types match up)
* There is no support for validating new & delete operations (yet)
The user is responsible for making sure the code types provided are correct The user is responsible for making sure the code types provided are correct
and have the desired specifiers assigned to them beforehand. and have the desired specifiers assigned to them beforehand.
## Code generation and modification ## Code generation and modification
There are three provided file interfaces: There are three provided auxillary interfaces:
* Builder * Builder
* Editor * Editor
@ -487,51 +482,10 @@ Editor and Scanner are disabled by default, use `GEN_FEATURE_EDITOR` and `GEN_FE
### Builder is a similar object to the jai language's string_builder ### Builder is a similar object to the jai language's string_builder
* The purpose of it is to generate a file. * The purpose of it is to generate a file.
* A file is specified and opened for writing using the open( file_path) ) function. * A file is specified and opened for writing using the open( file_path) function.
* The code is provided via print( code ) function will be serialized to its buffer. * The code is provided via print( code ) function will be serialized to its buffer.
* When all serialization is finished, use the write() command to write the buffer to the file. * When all serialization is finished, use the write() command to write the buffer to the file.
### Editor is for editing a series of files based on a set of requests provided to it ### Scanner Auxillary Interface
**Note: Not implemented yet** Provides *(eventually)* `scan_file` to automatically populate a CodeFile which contains a parsed AST (`Code`) of the file, with any contextual failures that are reported from the parser.
* The purpose is to overrite a specific file, it places its contents in a buffer to scan.
* Requests are populated using the following interface:
* add : Add code.
* remove : Remove code.
* replace: Replace code.
All three have the same parameters with exception to remove which only has SymbolInfo and Policy:
* SymbolInfo:
* File : The file the symbol resides in. Leave null to indicate to search all files. Leave null to indicated all-file search.
* Marker : #define symbol that indicates a location or following signature is valid to manipulate. Leave null to indicate the signature should only be used.
* Signature : Use a Code symbol to find a valid location to manipulate, can be further filtered with the marker. Leave null to indicate the marker should only be used.
* Policy : Additional policy info for completing the request (empty for now)
* Code : Code to inject if adding, or replace existing code with.
Additionally if `GEN_FEATURE_EDITOR_REFACTOR` is defined, refactor( file_path, specification_path ) wil be made available.
Refactor is based of the refactor library and uses its interface.
It will on call add a request to the queue to run the refactor script on the file.
### Scanner allows the user to generate Code ASTs by reading files
**Note: Not implemented yet**
* The purpose is to grab definitions to generate metadata or generate new code from these definitions.
* Requests are populated using the add( SymbolInfo, Policy ) function. The symbol info is the same as the one used for the editor. So is the case with Policy.
The file will only be read from, no writing supported.
One great use case is for example: generating the single-header library for gencpp!
### Additional Info (Editor and Scanner)
When all requests have been populated, call process_requests().
It will provide an output of receipt data of the results when it completes.
Files may be added to the Editor and Scanner additionally with add_files( num, files ).
This is intended for when you have requests that are for multiple files.
Request queue in both Editor and Scanner are cleared once process_requests completes.

398
gen_c_library/c_library.cpp Normal file
View File

@ -0,0 +1,398 @@
#define GEN_DEFINE_LIBRARY_CODE_CONSTANTS
#define GEN_ENFORCE_STRONG_CODE_TYPES
#define GEN_EXPOSE_BACKEND
#define GEN_SUPPORT_CPP_MEMBER_FEATURES 1
#define GEN_SUPPORT_CPP_REFERENCES 1
#include "../project/gen.cpp"
#include "helpers/push_ignores.inline.hpp"
#include "helpers/helper.hpp"
GEN_NS_BEGIN
#include "dependencies/parsing.cpp"
GEN_NS_END
#include "auxillary/builder.hpp"
#include "auxillary/builder.cpp"
#include "auxillary/scanner.hpp"
#include <cstdlib> // for system()
#include "components/memory.fixed_arena.hpp"
#include "components/misc.hpp"
#include "components/containers.array.hpp"
#include "components/containers.hashtable.hpp"
using namespace gen;
constexpr char const* generation_notice =
"// This file was generated automatially by gencpp's c_library.cpp"
"(See: https://github.com/Ed94/gencpp)\n\n";
constexpr StrC roll_own_dependencies_guard_start = txt(R"(
//! 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
#ifndef GEN_ROLL_OWN_DEPENDENCIES
)");
constexpr StrC roll_own_dependencies_guard_end = txt(R"(
// GEN_ROLL_OWN_DEPENDENCIES
#endif
)");
constexpr StrC implementation_guard_start = txt(R"(
#pragma region GENCPP IMPLEMENTATION GUARD
#if defined(GEN_IMPLEMENTATION) && ! defined(GEN_IMPLEMENTED)
# define GEN_IMPLEMENTED
)");
constexpr StrC implementation_guard_end = txt(R"(
#endif
#pragma endregion GENCPP IMPLEMENTATION GUARD
)");
void format_file( char const* path )
{
String resolved_path = String::make(GlobalAllocator, to_str(path));
String style_arg = String::make(GlobalAllocator, txt("-style=file:"));
style_arg.append("../scripts/.clang-format ");
// Need to execute clang format on the generated file to get it to match the original.
#define clang_format "clang-format "
#define cf_format_inplace "-i "
#define cf_verbose "-verbose "
String command = String::make( GlobalAllocator, clang_format );
command.append( cf_format_inplace );
command.append( cf_verbose );
command.append( style_arg );
command.append( resolved_path );
log_fmt("\tRunning clang-format on file:\n");
system( command );
log_fmt("\tclang-format finished reformatting.\n");
#undef cf_cmd
#undef cf_format_inplace
#undef cf_style
#undef cf_verbse
}
Code dump_to_scratch_and_retireve( Code code )
{
Builder ecode_file_temp = Builder::open("gen/scratch.hpp");
ecode_file_temp.print(code);
ecode_file_temp.write();
format_file("gen/scratch.hpp");
Code result = scan_file( "gen/scratch.hpp" );
remove("gen/scratch.hpp");
return result;
}
CodeBody parse_file( const char* path )
{
FileContents file = file_read_contents( GlobalAllocator, true, path );
CodeBody code = parse_global_body( { file.size, (char const*)file.data } );
log_fmt("\nParsed: %s\n", path);
return code;
}
int gen_main()
{
#define project_dir "../project/"
gen::init();
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 c_library_header_start = scan_file( "components/header_start.hpp" );
Builder
header = Builder::open( "gen/gen.h" );
header.print_fmt( generation_notice );
header.print_fmt("#pragma once\n\n");
header.print( push_ignores );
// Headers
{
header.print( c_library_header_start );
Code platform = scan_file( project_dir "dependencies/platform.hpp" );
Code macros = scan_file( project_dir "dependencies/macros.hpp" );
Code basic_types = scan_file( project_dir "dependencies/basic_types.hpp" );
Code debug = scan_file( project_dir "dependencies/debug.hpp" );
header.print_fmt( roll_own_dependencies_guard_start );
header.print( platform );
header.print_fmt( "\nGEN_NS_BEGIN\n" );
header.print( macros );
header.print( basic_types );
header.print( debug );
CodeBody parsed_memory = parse_file( project_dir "dependencies/memory.hpp" );
CodeBody memory = def_body(ECode::Global_Body);
for ( Code entry = parsed_memory.begin(); entry != parsed_memory.end(); ++ entry )
{
switch (entry->Type)
{
case ECode::Using:
{
log_fmt("REPLACE THIS MANUALLY: %S\n", entry->Name);
CodeUsing using_ver = cast(CodeUsing, entry);
CodeTypedef typedef_ver = def_typedef(using_ver->Name, using_ver->UnderlyingType);
memory.append(typedef_ver);
}
break;
case ECode::Function_Fwd:
{
CodeFn fn = cast(CodeFn, entry);
if ( fn->Name.is_equal(txt("free")) )
{
fn->Name = get_cached_string(txt("gen_free_ptr"));
}
memory.append(entry);
}
break;
case ECode::Function:
{
CodeFn fn = cast(CodeFn, entry);
s32 constexpr_found = fn->Specs.remove( ESpecifier::Constexpr );
if (constexpr_found > -1) {
log_fmt("Found constexpr: %S\n", entry->to_string());
fn->Specs.append(ESpecifier::Inline);
}
if ( fn->Name.is_equal(txt("free")) )
{
fn->Name = get_cached_string(txt("gen_free_ptr"));
}
memory.append(entry);
}
break;
case ECode::Template:
{
CodeTemplate tmpl = cast(CodeTemplate, entry);
if ( tmpl->Declaration->Name.contains(txt("swap")))
{
CodeBody macro_swap = parse_global_body( txt(R"(
#define swap( a, b ) \
do \
{ \
typeof( a ) temp = ( a ); \
( a ) = ( b ); \
( b ) = temp; \
} while ( 0 )
)"
));
memory.append(macro_swap);
}
}
break;
case ECode::Class:
case ECode::Struct:
{
CodeBody body = entry->Body->operator CodeBody();
CodeBody new_body = def_body( entry->Body->Type );
for ( Code body_entry = body.begin(); body_entry != body.end(); ++ body_entry ) switch
(body_entry->Type) {
case ECode::Preprocess_If:
{
ignore_preprocess_cond_block(txt("GEN_SUPPORT_CPP_MEMBER_FEATURES"), body_entry, body );
}
break;
default:
new_body.append(body_entry);
break;
}
entry->Body = rcast(AST*, new_body.ast);
memory.append(entry);
}
break;
case ECode::Preprocess_If:
{
b32 found = ignore_preprocess_cond_block(txt("GEN_SUPPORT_CPP_MEMBER_FEATURES"), entry, parsed_memory );
if (found) break;
memory.append(entry);
}
break;
case ECode::Preprocess_IfDef:
{
b32 found = ignore_preprocess_cond_block(txt("GEN_INTELLISENSE_DIRECTIVES"), entry, parsed_memory );
if (found) break;
memory.append(entry);
}
break;
case ECode::Preprocess_Pragma:
{
b32 found = swap_pragma_region_implementation( txt("FixedArena"), gen_fixed_arenas, entry, memory);
if (found) break;
memory.append(entry);
}
break;
default: {
memory.append(entry);
}
break;
}
}
header.print( dump_to_scratch_and_retireve(memory) );
Code string_ops = scan_file( project_dir "dependencies/string_ops.hpp" );
header.print( string_ops );
CodeBody printing_parsed = parse_file( project_dir "dependencies/printing.hpp" );
CodeBody printing = def_body(ECode::Global_Body);
for ( Code entry = printing_parsed.begin(); entry != printing_parsed.end(); ++ entry )
{
switch (entry->Type)
{
case ECode::Preprocess_IfDef:
{
b32 found = ignore_preprocess_cond_block(txt("GEN_INTELLISENSE_DIRECTIVES"), entry, printing_parsed );
if (found) break;
printing.append(entry);
}
break;
case ECode::Variable:
{
if (contains(entry->Name, txt("Msg_Invalid_Value")))
{
CodeDefine define = def_define(entry->Name, entry->Value->Content);
printing.append(define);
continue;
}
printing.append(entry);
}
break;
default:
printing.append(entry);
break;
}
}
header.print(dump_to_scratch_and_retireve(printing));
CodeBody containers = def_body(ECode::Global_Body);
{
containers.append( def_pragma(code(region Containers)));
containers.append( gen_array_base() );
containers.append( gen_hashtable_base() );
containers.append( def_pragma(code(endregion Containers)));
}
header.print(fmt_newline);
header.print(dump_to_scratch_and_retireve(containers));
Code hashing = scan_file( project_dir "dependencies/hashing.hpp" );
header.print( hashing );
CodeBody parsed_strings = parse_file( project_dir "dependencies/strings.hpp" );
CodeBody strings = def_body(ECode::Global_Body);
for ( Code entry = parsed_strings.begin(); entry != parsed_strings.end(); ++ entry )
{
switch (entry->Type)
{
case ECode::Preprocess_If:
{
ignore_preprocess_cond_block(txt("! GEN_COMPILER_C"), entry, parsed_strings);
}
break;
case ECode::Preprocess_IfDef:
{
ignore_preprocess_cond_block(txt("GEN_INTELLISENSE_DIRECTIVES"), entry, parsed_strings );
}
break;
case ECode::Struct_Fwd:
{
if ( entry->Name.is_equal(txt("String")) )
{
CodeTypedef c_def = parse_typedef(code( typedef char* String; ));
strings.append(c_def);
strings.append(fmt_newline);
++ entry;
continue;
}
strings.append(entry);
}
break;
case ECode::Struct:
{
CodeBody body = entry->Body->operator CodeBody();
CodeBody new_body = def_body( entry->Body->Type );
for ( Code body_entry = body.begin(); body_entry != body.end(); ++ body_entry ) switch
(body_entry->Type) {
case ECode::Preprocess_If:
{
b32 found = ignore_preprocess_cond_block(txt("! GEN_COMPILER_C"), body_entry, body );
if (found) break;
new_body.append(body_entry);
}
break;
default:
new_body.append(body_entry);
break;
}
entry->Body = rcast(AST*, new_body.ast);
strings.append(entry);
}
break;
default:
strings.append(entry);
break;
}
}
header.print(dump_to_scratch_and_retireve(strings));
Code filesystem = scan_file( project_dir "dependencies/filesystem.hpp" );
Code timing = scan_file( project_dir "dependencies/timing.hpp" );
// header.print( filesystem );
// header.print( timing );
header.print_fmt( "\nGEN_NS_END\n" );
header.print_fmt( roll_own_dependencies_guard_end );
Code types = scan_file( project_dir "components/types.hpp" );
Code ast = scan_file( project_dir "components/ast.hpp" );
Code ast_types = scan_file( project_dir "components/ast_types.hpp" );
Code code_types = scan_file( project_dir "components/code_types.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 eoperator = gen_eoperator ( project_dir "enums/EOperator.csv" );
CodeBody especifier = gen_especifier( project_dir "enums/ESpecifier.csv" );
CodeBody ast_inlines = gen_ast_inlines();
#if 0
header.print_fmt("#pragma region Types\n");
header.print( types );
header.print( fmt_newline );
header.print( dump_to_scratch_and_retireve( ecode ));
header.print( fmt_newline );
header.print( dump_to_scratch_and_retireve( eoperator ));
header.print( fmt_newline );
header.print( dump_to_scratch_and_retireve( especifier ));
header.print( fmt_newline );
header.print_fmt("#pragma endregion Types\n\n");
#endif
}
header.print( pop_ignores );
header.write();
// format_file( "gen/gen.h" );
gen::deinit();
return 0;
#undef project_dir
}

View File

@ -0,0 +1,295 @@
#pragma once
#include "../project/gen.hpp"
using namespace gen;
CodeBody gen_array_base()
{
CodeTypedef td_header = parse_typedef( code( typedef struct ArrayHeader ArrayHeader; ));
CodeStruct header = parse_struct( code(
struct ArrayHeader
{
AllocatorInfo Allocator;
usize Capacity;
usize Num;
};
));
// Code grow_formula = untyped_str( txt( "#define gen_array_grow_formula( value ) ( 2 * value + 8 )\n" ));
Code get_header = untyped_str( txt( "#define array_get_header( Type, self ) ( (ArrayHeader*)( self ) - 1)\n" ));
return def_global_body( args( fmt_newline, td_header, header, get_header, fmt_newline ) );
};
CodeBody gen_array( StrC type, StrC array_name )
{
String array_type = String::fmt_buf( GlobalAllocator, "%.*s", array_name.Len, array_name.Ptr );
String fn = String::fmt_buf( GlobalAllocator, "%.*s", array_name.Len, array_name.Ptr );
str_to_lower(fn.Data);
#pragma push_macro( "GEN_ASSERT" )
#undef GEN_ASSERT
CodeBody result = parse_global_body( token_fmt( "array_type", (StrC)array_type, "fn", (StrC)fn, "type", (StrC)type
, stringize(
typedef <type>* <array_type>;
<array_type> <fn>_init ( AllocatorInfo allocator );
<array_type> <fn>_init_reserve ( AllocatorInfo allocator, usize capacity );
bool <fn>_append ( <array_type>* self, <type> value );
bool <fn>_append_items ( <array_type>* self, <type>* items, usize item_num );
bool <fn>_append_at ( <array_type>* self, <type> item, usize idx );
bool <fn>_append_items_at( <array_type>* self, <type>* items, usize item_num, usize idx );
<type>* <fn>_back ( <array_type> self );
void <fn>_clear ( <array_type> self );
bool <fn>_fill ( <array_type> self, usize begin, usize end, <type> value );
void <fn>_free ( <array_type> self );
bool <fn>_grow ( <array_type>* self, usize min_capacity );
usize <fn>_num ( <array_type> self );
<type> <fn>_pop ( <array_type> self );
bool <fn>_reserve ( <array_type>* self, usize new_capacity );
bool <fn>_resize ( <array_type>* self, usize num );
bool <fn>_set_capacity ( <array_type>* self, usize new_capacity );
<array_type> <fn>_init( AllocatorInfo allocator )
{
return <fn>_init_reserve( allocator, array_grow_formula( 0 ) );
}
<array_type> <fn>_init_reserve( AllocatorInfo allocator, usize capacity )
{
ArrayHeader* header = cast(ArrayHeader*, alloc( allocator, sizeof(ArrayHeader) + sizeof(<type>) * capacity ) );
if ( header == NULL )
return NULL;
header->Allocator = allocator;
header->Capacity = capacity;
header->Num = 0;
return cast( <type>*, header + 1 );
}
bool <fn>_append( <array_type>* self, <type> value )
{
ArrayHeader* header = get_header( * self );
if ( header->Num == header->Capacity )
{
if ( ! <fn>_grow( self, header->Capacity))
return false;
header = get_header( * self );
}
(* self)[ header->Num ] = value;
header->Num++;
return true;
}
bool <fn>_append_items( <array_type>* self, <type>* items, usize item_num )
{
ArrayHeader* header = get_header( * self );
if ( header->Num + item_num > header->Capacity )
{
if ( ! <fn>_grow( self, header->Capacity + item_num ))
return false;
header = get_header( * self );
}
mem_copy( (* self) + header->Num, items, sizeof(<type>) * item_num );
header->Num += item_num;
return true;
}
bool <fn>_append_at( <array_type>* self, <type> item, usize idx )
{
ArrayHeader* header = get_header( * self );
if ( idx >= header->Num )
idx = header->Num - 1;
if ( idx < 0 )
idx = 0;
if ( header->Capacity < header->Num + 1 )
{
if ( ! <fn>_grow( self, header->Capacity + 1 ) )
return false;
header = get_header( * self );
}
<array_type> target = (* self) + idx;
mem_move( target + 1, target, (header->Num - idx) * sizeof(<type>) );
header->Num++;
return true;
}
bool <fn>_append_items_at( <array_type>* self, <type>* items, usize item_num, usize idx )
{
ArrayHeader* header = get_header( * self );
if ( idx >= header->Num )
{
return <fn>_append_items( self, items, item_num );
}
if ( item_num > header->Capacity )
{
if ( ! <fn>_grow( self, item_num + header->Capacity ) )
return false;
header = get_header( * self );
}
<type>* target = (* self) + idx + item_num;
<type>* src = (* self) + idx;
mem_move( target, src, (header->Num - idx) * sizeof(<type>) );
mem_copy( src, items, item_num * sizeof(<type>) );
header->Num += item_num;
return true;
}
<type>* <fn>_back( <array_type> self )
{
ArrayHeader* header = get_header( self );
if ( header->Num == 0 )
return NULL;
return self + header->Num - 1;
}
void <fn>_clear( <array_type> self )
{
ArrayHeader* header = get_header( self );
header->Num = 0;
}
bool <fn>_fill( <array_type> self, usize begin, usize end, <type> value )
{
ArrayHeader* header = get_header( self );
if ( begin < 0 || end >= header->Num )
return false;
for ( ssize idx = begin; idx < end; idx ++ )
self[ idx ] = value;
return true;
}
void <fn>_free( <array_type> self )
{
ArrayHeader* header = get_header( self );
free( header->Allocator, header );
self = NULL;
}
bool <fn>_grow( <array_type>* self, usize min_capacity )
{
ArrayHeader* header = get_header( *self );
usize new_capacity = array_grow_formula( header->Capacity );
if ( new_capacity < min_capacity )
new_capacity = min_capacity;
return <fn>_set_capacity( self, new_capacity );
}
usize <fn>_num( <array_type> self )
{
return get_header(self)->Num;
}
<type> <fn>_pop( <array_type> self )
{
ArrayHeader* header = get_header( self );
GEN_ASSERT( header->Num > 0 );
<type> result = self[ header->Num - 1 ];
header->Num--;
return result;
}
void <fn>_remove_at( <array_type> self, usize idx )
{
ArrayHeader* header = get_header( self );
GEN_ASSERT( idx < header->Num );
mem_move( self + idx, self + idx + 1, sizeof( <type> ) * ( header->Num - idx - 1 ) );
header->Num--;
}
bool <fn>_reserve( <array_type>* self, usize new_capacity )
{
ArrayHeader* header = get_header( * self );
if ( header->Capacity < new_capacity )
return <fn>_set_capacity( self, new_capacity );
return true;
}
bool <fn>_resize( <array_type>* self, usize num )
{
ArrayHeader* header = get_header( * self );
if ( header->Capacity < num )
{
if ( ! <fn>_grow( self, num ) )
return false;
header = get_header( * self );
}
header->Num = num;
return true;
}
bool <fn>_set_capacity( <array_type>* self, usize new_capacity )
{
ArrayHeader* header = get_header( * self );
if ( new_capacity == header->Capacity )
return true;
if ( new_capacity < header->Num )
header->Num = new_capacity;
usize size = sizeof( ArrayHeader ) + sizeof( <type> ) * new_capacity;
ArrayHeader* new_header = cast( ArrayHeader*, alloc( header->Allocator, size ));
if ( new_header == NULL )
return false;
mem_move( new_header, header, sizeof( ArrayHeader ) + sizeof( <type> ) * header->Num );
free( header->Allocator, & header );
new_header->Capacity = new_capacity;
* self = cast( <type>*, new_header + 1 );
return true;
}
)));
#pragma pop_macro( "GEN_ASSERT" )
return def_global_body( args(
def_pragma( to_str( str_fmt_buf( "region %S", array_type ))),
fmt_newline,
result,
fmt_newline,
def_pragma( to_str( str_fmt_buf( "endregion %S", array_type ))),
fmt_newline
));
};
// CodeBody gen_

View File

@ -0,0 +1,354 @@
#pragma once
#include "../project/gen.hpp"
#include "containers.array.hpp"
using namespace gen;
CodeBody gen_hashtable_base()
{
return parse_global_body( code(
typedef struct HT_FindResult HT_FindResult;
struct HT_FindResult
{
ssize HashIndex;
ssize PrevIndex;
ssize EntryIndex;
};
));
}
CodeBody gen_hashtable( StrC type, StrC hashtable_name )
{
String
fn = String::make_reserve( GlobalAllocator, hashtable_name.Len + sizeof("gen") );
fn.append_fmt( "%.*s", hashtable_name.Len, hashtable_name.Ptr );
str_to_lower(fn.Data);
String
tbl_type = String::make_reserve( GlobalAllocator, hashtable_name.Len + sizeof("gen") );
tbl_type.append_fmt( "%.*s", hashtable_name.Len, hashtable_name.Ptr );
String name_lower = String::make( GlobalAllocator, hashtable_name );
str_to_lower( name_lower.Data );
String hashtable_entry = String::fmt_buf( GlobalAllocator, "HTE_%.*s", hashtable_name.Len, hashtable_name.Ptr );
String entry_array_name = String::fmt_buf( GlobalAllocator, "Arr_HTE_%.*s", hashtable_name.Len, hashtable_name.Ptr );
String entry_array_fn_ns = String::fmt_buf( GlobalAllocator, "arr_hte_%.*s", name_lower.length(), name_lower.Data );
CodeBody hashtable_types = parse_global_body( token_fmt(
"type", (StrC) type,
"tbl_name", (StrC) hashtable_name,
"tbl_type", (StrC) tbl_type,
stringize(
typedef struct HTE_<tbl_name> HTE_<tbl_name>;
struct HTE_<tbl_name>
{
u64 Key;
ssize Next;
<type> Value;
};
typedef void (* <tbl_type>_MapProc) ( <tbl_type> self, u64 key, <type> value );
typedef void (* <tbl_type>_MapMutProc) ( <tbl_type> self, u64 key, <type>* value );
)));
CodeBody entry_array = gen_array( hashtable_entry, entry_array_name );
#pragma push_macro( "GEN_ASSERT" )
#pragma push_macro( "GEN_ASSERT_NOT_NULL" )
#undef GEN_ASSERT
#undef GEN_ASSERT_NOT_NULL
CodeBody hashtable_def = parse_global_body( token_fmt(
"type", (StrC) type,
"tbl_name", (StrC) hashtable_name,
"tbl_type", (StrC) tbl_type,
"fn", (StrC) fn,
"entry_type", (StrC) hashtable_entry,
"array_entry", (StrC) entry_array_name,
"fn_array", (StrC) entry_array_fn_ns,
stringize(
typedef struct <tbl_type> <tbl_type>;
struct <tbl_type>
{
Array_ssize Hashes;
<array_entry> Entries;
};
<tbl_type> <fn>_make ( AllocatorInfo allocator );
<tbl_type> <fn>_make_reserve( AllocatorInfo allocator, ssize num );
void <fn>_clear ( <tbl_type> self );
void <fn>_destroy ( <tbl_type> self );
<type>* <fn>_get ( <tbl_type> self, u64 key );
void <fn>_map ( <tbl_type> self, <tbl_type>_MapProc map_proc );
void <fn>_map_mut ( <tbl_type> self, <tbl_type>_MapMutProc map_proc );
void <fn>_grow ( <tbl_type>* self );
void <fn>_rehash ( <tbl_type>* self, ssize new_num );
void <fn>_rehash_fast ( <tbl_type> self );
void <fn>_remove ( <tbl_type> self, u64 key );
void <fn>_remove_entry( <tbl_type> self, ssize idx );
void <fn>_set ( <tbl_type>* self, u64 key, <type> value );
ssize <fn>_slot ( <tbl_type> self, u64 key );
ssize <fn>__add_entry( <tbl_type> self, u64 key );
HT_FindResult <fn>__find ( <tbl_type> self, u64 key );
b32 <fn>__full ( <tbl_type> self );
<tbl_type> <fn>_make( AllocatorInfo allocator )
{
<tbl_type>
result = { NULL, NULL };
result.Hashes = array_ssize_make( allocator );
result.Entries = <fn_array>_make( allocator );
return result;
}
<tbl_type> <fn>_make_reserve( AllocatorInfo allocator, ssize num )
{
<tbl_type>
result = { NULL, NULL };
result.Hashes = array_ssize_make_reserve( allocator, num );
result.Entries = <fn_array>_make_reserve( allocator, num );
return result;
}
void <fn>_clear( <tbl_type> self )
{
for ( ssize idx = 0; idx < array_header( self.Hashes )->Num; idx++ )
self.Hashes[idx] = -1;
array_ssize_clear( self.Hashes );
<fn_array>_clear( self.Entries );
}
void <fn>_destroy( <tbl_type> self )
{
if ( self.Hashes && self.Entries )
{
array_ssize_free( self.Hashes );
<fn_array>_free( self.Entries );
}
}
<type>* <fn>_get( <tbl_type> self, u64 key )
{
ssize idx = <fn>__find( self, key ).EntryIndex;
if ( idx > 0 )
return & self.Entries[idx].Value;
return NULL;
}
void <fn>_map( <tbl_type> self, <tbl_type>_MapProc map_proc )
{
GEN_ASSERT_NOT_NULL( map_proc );
for ( ssize idx = 0; idx < array_header( self.Entries )->Num; idx++ )
{
map_proc( self, self.Entries[idx].Key, self.Entries[idx].Value );
}
}
void <fn>_map_mut( <tbl_type> self, <tbl_type>_MapMutProc map_proc )
{
GEN_ASSERT_NOT_NULL( map_proc );
for ( ssize idx = 0; idx < array_header( self.Entries )->Num; idx++ )
{
map_proc( self, self.Entries[idx].Key, & self.Entries[idx].Value );
}
}
void <fn>_grow( <tbl_type>* self )
{
ssize new_num = array_grow_formula( array_header( self->Entries )->Num );
<fn>_rehash( self, new_num );
}
void <fn>_rehash( <tbl_type>* self, ssize new_num )
{
ssize idx;
ssize last_added_index;
ArrayHeader* old_hash_header = array_header( self->Hashes );
ArrayHeader* old_entries_header = array_header( self->Entries );
<tbl_type> new_tbl = <fn>_make_reserve( old_hash_header->Allocator, old_hash_header->Num );
ArrayHeader* new_hash_header = array_header( new_tbl.Hashes );
for ( idx = 0; idx < new_hash_header->Num; idx++ )
new_tbl.Hashes[idx] = -1;
for ( idx = 0; idx < old_entries_header->Num; idx++ )
{
<entry_type>* entry;
HT_FindResult find_result;
if ( new_hash_header->Num == 0 )
<fn>_grow( & new_tbl );
entry = & self->Entries[ idx ];
find_result = <fn>__find( new_tbl, entry->Key );
last_added_index = <fn>__add_entry( new_tbl, entry->Key );
if ( find_result.PrevIndex < 0 )
new_tbl.Hashes[ find_result.HashIndex ] = last_added_index;
else
new_tbl.Entries[ find_result.PrevIndex ].Next = last_added_index;
new_tbl.Entries[ last_added_index ].Next = find_result.EntryIndex;
new_tbl.Entries[ last_added_index ].Value = entry->Value;
}
<fn>_destroy( *self );
* self = new_tbl;
}
void <fn>_rehash_fast( <tbl_type> self )
{
ssize idx;
for ( idx = 0; idx < array_header( self.Entries )->Num; idx++ )
self.Entries[ idx ].Next = -1;
for ( idx = 0; idx < array_header( self.Hashes )->Num; idx++ )
self.Hashes[ idx ] = -1;
for ( idx = 0; idx < array_header( self.Entries )->Num; idx++ )
{
<entry_type>* entry;
HT_FindResult find_result;
entry = & self.Entries[ idx ];
find_result = <fn>__find( self, entry->Key );
if ( find_result.PrevIndex < 0 )
self.Hashes[ find_result.HashIndex ] = idx;
else
self.Entries[ find_result.PrevIndex ].Next = idx;
}
}
void <fn>_remove( <tbl_type> self, u64 key )
{
HT_FindResult find_result = <fn>__find( self, key );
if ( find_result.EntryIndex >= 0 )
{
<fn_array>_remove_at( self.Entries, find_result.EntryIndex );
<fn>_rehash_fast( self );
}
}
void <fn>_remove_entry( <tbl_type> self, ssize idx )
{
<fn_array>_remove_at( self.Entries, idx );
}
void <fn>_set( <tbl_type>* self, u64 key, <type> value )
{
ssize idx;
HT_FindResult find_result;
if ( array_header( self->Hashes )->Num == 0 )
<fn>_grow( self );
find_result = <fn>__find( * self, key );
if ( find_result.EntryIndex >= 0 )
{
idx = find_result.EntryIndex;
}
else
{
idx = <fn>__add_entry( * self, key );
if ( find_result.PrevIndex >= 0 )
{
self->Entries[ find_result.PrevIndex ].Next = idx;
}
else
{
self->Hashes[ find_result.HashIndex ] = idx;
}
}
self->Entries[ idx ].Value = value;
if ( <fn>__full( * self ) )
<fn>_grow( self );
}
ssize <fn>_slot( <tbl_type> self, u64 key )
{
for ( ssize idx = 0; idx < array_header( self.Hashes )->Num; ++idx )
if ( self.Hashes[ idx ] == key )
return idx;
return -1;
}
ssize <fn>__add_entry( <tbl_type> self, u64 key )
{
ssize idx;
<entry_type> entry = { key, -1 };
idx = array_header( self.Entries )->Num;
<fn_array>_append( & self.Entries, entry );
return idx;
}
HT_FindResult <fn>__find( <tbl_type> self, u64 key )
{
HT_FindResult result = { -1, -1, -1 };
ArrayHeader* hash_header = array_header( self.Hashes );
if ( hash_header->Num > 0 )
{
result.HashIndex = key % hash_header->Num;
result.EntryIndex = self.Hashes[ result.HashIndex ];
while ( result.EntryIndex >= 0 )
{
if ( self.Entries[ result.EntryIndex ].Key == key )
break;
result.PrevIndex = result.EntryIndex;
result.EntryIndex = self.Entries[ result.EntryIndex ].Next;
}
}
return result;
}
b32 <fn>__full( <tbl_type> self )
{
ArrayHeader* hash_header = array_header( self.Hashes );
ArrayHeader* entries_header = array_header( self.Entries );
return 0.75f * hash_header->Num < entries_header->Num;
}
)));
#pragma pop_macro( "GEN_ASSERT" )
#pragma pop_macro( "GEN_ASSERT_NOT_NULL" )
char const* cmt_str = str_fmt_buf( "Name: %.*s Type: %.*s"
, tbl_type.length(), tbl_type.Data
, type.Len, type.Ptr );
return def_global_body(args(
def_pragma( to_str( str_fmt_buf( "region %S", tbl_type ))),
fmt_newline,
hashtable_types,
fmt_newline,
entry_array,
hashtable_def,
fmt_newline,
def_pragma( to_str( str_fmt_buf( "endregion %S", tbl_type ))),
fmt_newline
));
}

View File

@ -0,0 +1,14 @@
/*
gencpp: An attempt at "simple" staged metaprogramming for c/c++.
See Readme.md for more information from the project repository.
Public Address:
https://github.com/Ed94/gencpp
This is a single header C-Library variant.
Define GEN_IMPLEMENTATION before including this file in a single compilation unit.
*/
#if ! defined(GEN_DONT_ENFORCE_GEN_TIME_GUARD) && ! defined(GEN_TIME)
# error Gen.hpp : GEN_TIME not defined
#endif

View File

@ -0,0 +1,122 @@
#pragma once
#include "../project/gen.hpp"
using namespace gen;
CodeBody gen_fixed_arenas()
{
CodeBody result = def_body(ECode::Global_Body);
result.append(def_pragma(txt("region FixedArena")));
char const* template_struct = stringize(
struct FixedArena_<Name>
{
char memory[<Size>];
Arena arena;
};
);
char const* template_interface = stringize(
inline
void fixed_arena_init_<Name>(FixedArena_<Name>* result) {
zero_size(& result->memory[0], <Size>);
result->arena = arena_init_from_memory(& result->memory[0], <Size>);
}
inline
ssize fixed_arena_size_remaining_<Name>(FixedArena_<Name>* fixed_arena, ssize alignment) {
return size_remaining(fixed_arena->arena, alignment);
}
);
CodeStruct arena_struct_1kb = parse_struct( token_fmt_impl( 3, "Name", txt("1KB"), "Size", txt("kilobytes(1)"), template_struct ));
CodeStruct arena_struct_4kb = parse_struct( token_fmt_impl( 3, "Name", txt("4KB"), "Size", txt("kilobytes(4)"), template_struct ));
CodeStruct arena_struct_8kb = parse_struct( token_fmt_impl( 3, "Name", txt("8KB"), "Size", txt("kilobytes(8)"), template_struct ));
CodeStruct arena_struct_16kb = parse_struct( token_fmt_impl( 3, "Name", txt("16KB"), "Size", txt("kilobytes(16)"), template_struct ));
CodeStruct arena_struct_32kb = parse_struct( token_fmt_impl( 3, "Name", txt("32KB"), "Size", txt("kilobytes(32)"), template_struct ));
CodeStruct arena_struct_64kb = parse_struct( token_fmt_impl( 3, "Name", txt("64KB"), "Size", txt("kilobytes(64)"), template_struct ));
CodeStruct arena_struct_128kb = parse_struct( token_fmt_impl( 3, "Name", txt("128KB"), "Size", txt("kilobytes(128)"), template_struct ));
CodeStruct arena_struct_256kb = parse_struct( token_fmt_impl( 3, "Name", txt("256KB"), "Size", txt("kilobytes(256)"), template_struct ));
CodeStruct arena_struct_512kb = parse_struct( token_fmt_impl( 3, "Name", txt("512KB"), "Size", txt("kilobytes(512)"), template_struct ));
CodeStruct arena_struct_1mb = parse_struct( token_fmt_impl( 3, "Name", txt("1MB"), "Size", txt("megabytes(1)"), template_struct ));
CodeStruct arena_struct_2mb = parse_struct( token_fmt_impl( 3, "Name", txt("2MB"), "Size", txt("megabytes(2)"), template_struct ));
CodeStruct arena_struct_4mb = parse_struct( token_fmt_impl( 3, "Name", txt("4MB"), "Size", txt("megabytes(4)"), template_struct ));
CodeBody arena_interface_1kb = parse_global_body( token_fmt_impl( 3, "Name", txt("1KB"), "Size", txt("kilobytes(1)"), template_interface ));
CodeBody arena_interface_4kb = parse_global_body( token_fmt_impl( 3, "Name", txt("4KB"), "Size", txt("kilobytes(4)"), template_interface ));
CodeBody arena_interface_8kb = parse_global_body( token_fmt_impl( 3, "Name", txt("8KB"), "Size", txt("kilobytes(8)"), template_interface ));
CodeBody arena_interface_16kb = parse_global_body( token_fmt_impl( 3, "Name", txt("16KB"), "Size", txt("kilobytes(16)"), template_interface ));
CodeBody arena_interface_32kb = parse_global_body( token_fmt_impl( 3, "Name", txt("32KB"), "Size", txt("kilobytes(32)"), template_interface ));
CodeBody arena_interface_64kb = parse_global_body( token_fmt_impl( 3, "Name", txt("64KB"), "Size", txt("kilobytes(64)"), template_interface ));
CodeBody arena_interface_128kb = parse_global_body( token_fmt_impl( 3, "Name", txt("128KB"), "Size", txt("kilobytes(128)"), template_interface ));
CodeBody arena_interface_256kb = parse_global_body( token_fmt_impl( 3, "Name", txt("256KB"), "Size", txt("kilobytes(256)"), template_interface ));
CodeBody arena_interface_512kb = parse_global_body( token_fmt_impl( 3, "Name", txt("512KB"), "Size", txt("kilobytes(512)"), template_interface ));
CodeBody arena_interface_1mb = parse_global_body( token_fmt_impl( 3, "Name", txt("1MB"), "Size", txt("megabytes(1)"), template_interface ));
CodeBody arena_interface_2mb = parse_global_body( token_fmt_impl( 3, "Name", txt("2MB"), "Size", txt("megabytes(2)"), template_interface ));
CodeBody arena_interface_4mb = parse_global_body( token_fmt_impl( 3, "Name", txt("4MB"), "Size", txt("megabytes(4)"), template_interface ));
result.append(arena_struct_1kb);
result.append(arena_struct_4kb);
result.append(arena_struct_8kb);
result.append(arena_struct_16kb);
result.append(arena_struct_32kb);
result.append(arena_struct_128kb);
result.append(arena_struct_256kb);
result.append(arena_struct_512kb);
result.append(arena_struct_1mb);
result.append(arena_struct_2mb);
result.append(arena_struct_4mb);
result.append(arena_interface_1kb);
result.append(arena_interface_4kb);
result.append(arena_interface_8kb);
result.append(arena_interface_16kb);
result.append(arena_interface_32kb);
result.append(arena_interface_128kb);
result.append(arena_interface_256kb);
result.append(arena_interface_512kb);
result.append(arena_interface_1mb);
result.append(arena_interface_2mb);
result.append(arena_interface_4mb);
CodeDefine def = def_define(txt("fixed_arena_allocator_info(fixed_arena)"), code({ arena_allocator_proc, & fixed_arena.arena }) );
result.append(def);
result.append(parse_global_body(txt(R"(
#define fixed_arena_init(expr) _Generic((expr), \
FixedArena_1KB* : fixed_arena_init_1KB, \
FixedArena_4KB* : fixed_arena_init_4KB, \
FixedArena_8KB* : fixed_arena_init_8KB, \
FixedArena_16KB* : fixed_arena_init_16KB, \
FixedArena_32KB* : fixed_arena_init_32KB, \
FixedArena_64KB* : fixed_arena_init_64KB, \
FixedArena_128KB* : fixed_arena_init_128KB, \
FixedArena_256KB* : fixed_arena_init_256KB, \
FixedArena_512KB* : fixed_arena_init_512KB, \
FixedArena_1MB* : fixed_arena_init_1MB, \
FixedArena_2MB* : fixed_arena_init_2MB, \
FixedArena_4MB* : fixed_arena_init_4MB \
)(expr)
#define fixed_arena_size_remaining(expr, alignment) _Generic((expr), \
FixedArena_1KB* : fixed_arena_size_remaining_1KB, \
FixedArena_4KB* : fixed_arena_size_remaining_4KB, \
FixedArena_8KB* : fixed_arena_size_remaining_8KB, \
FixedArena_16KB* : fixed_arena_size_remaining_16KB, \
FixedArena_32KB* : fixed_arena_size_remaining_32KB, \
FixedArena_64KB* : fixed_arena_size_remaining_64KB, \
FixedArena_128KB* : fixed_arena_size_remaining_128KB, \
FixedArena_256KB* : fixed_arena_size_remaining_256KB, \
FixedArena_512KB* : fixed_arena_size_remaining_512KB, \
FixedArena_1MB* : fixed_arena_size_remaining_1MB, \
FixedArena_2MB* : fixed_arena_size_remaining_2MB, \
FixedArena_4MB* : fixed_arena_size_remaining_4MB \
)(expr, alignment)
)"
)));
result.append(def_pragma(txt("endregion FixedArena")));
return result;
}

View File

@ -0,0 +1,72 @@
// #pragma once
// #include "../project/gen.hpp"
// using namespace gen;
using SwapContentProc = CodeBody(void);
b32 ignore_preprocess_cond_block( StrC cond_sig, Code& entry_iter, CodeBody& body )
{
b32 found = false;
CodePreprocessCond cond = cast(CodePreprocessCond, entry_iter);
if ( cond->Content.contains(cond_sig) )
{
log_fmt("Preprocess cond found: %S\n", cond->Content);
found = true;
s32 depth = 1;
++ entry_iter; for(b32 continue_for = true; continue_for && entry_iter != body.end(); ) switch
(entry_iter->Type) {
case ECode::Preprocess_If:
case ECode::Preprocess_IfDef:
case ECode::Preprocess_IfNotDef:
depth ++;
break;
case ECode::Preprocess_EndIf:
{
depth --;
if (depth == 0) {
continue_for = false;
break;
}
}
break;
default:
++ entry_iter;
break;
}
}
return found;
}
bool swap_pragma_region_implementation( StrC region_name, SwapContentProc* swap_content, Code& entry_iter, CodeBody& body )
{
bool found = false;
CodePragma possible_region = cast(CodePragma, entry_iter);
String region_sig = string_fmt_buf(GlobalAllocator, "region %s", region_name.Ptr);
String endregion_sig = string_fmt_buf(GlobalAllocator, "endregion %s", region_name.Ptr);
if ( possible_region->Content.contains(region_sig))
{
found = true;
// body.append(possible_region);
body.append(swap_content());
++ entry_iter; for(b32 continue_for = true; continue_for; ++entry_iter) switch
(entry_iter->Type) {
case ECode::Preprocess_Pragma:
{
CodePragma possible_end_region = cast(CodePragma, entry_iter);
if ( possible_end_region->Content.contains(endregion_sig) ) {
// body.append(possible_end_region);
continue_for = false;
}
}
break;
}
body.append(entry_iter);
}
return found;
}

7
gen_c_library/gen.c Normal file
View File

@ -0,0 +1,7 @@
#define GEN_IMPLEMENTATION
#include "gen/gen.h"
int main()
{
// init();
}

View File

@ -0,0 +1,4 @@
# Singleheader
Creates a single header file version of the library using `singleheader.cpp`.
Follows the same convention seen in the gb, stb, and zpl libraries.

View File

@ -0,0 +1,314 @@
#define GEN_DEFINE_LIBRARY_CODE_CONSTANTS
#define GEN_ENFORCE_STRONG_CODE_TYPES
#define GEN_EXPOSE_BACKEND
#define GEN_SUPPORT_CPP_MEMBER_FEATURES 1
#define GEN_SUPPORT_CPP_REFERENCES 0
#include "gen.cpp"
#include "helpers/push_ignores.inline.hpp"
#include "helpers/helper.hpp"
GEN_NS_BEGIN
#include "dependencies/parsing.cpp"
GEN_NS_END
#include "auxillary/builder.hpp"
#include "auxillary/builder.cpp"
#include "auxillary/scanner.hpp"
#include <cstdlib> // for system()
using namespace gen;
constexpr char const* generation_notice =
"// This file was generated automatially by gencpp's singleheader.cpp"
"(See: https://github.com/Ed94/gencpp)\n\n";
constexpr StrC implementation_guard_start = txt(R"(
#pragma region GENCPP IMPLEMENTATION GUARD
#if defined(GEN_IMPLEMENTATION) && ! defined(GEN_IMPLEMENTED)
# define GEN_IMPLEMENTED
)");
constexpr StrC implementation_guard_end = txt(R"(
#endif
#pragma endregion GENCPP IMPLEMENTATION GUARD
)");
constexpr StrC roll_own_dependencies_guard_start = txt(R"(
//! 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
#ifndef GEN_ROLL_OWN_DEPENDENCIES
)");
constexpr StrC roll_own_dependencies_guard_end = txt(R"(
// GEN_ROLL_OWN_DEPENDENCIES
#endif
)");
global bool generate_gen_dep = true;
global bool generate_builder = true;
global bool generate_editor = true;
global bool generate_scanner = true;
void format_file( char const* path )
{
String resolved_path = String::make(GlobalAllocator, to_str(path));
String style_arg = String::make(GlobalAllocator, txt("-style=file:"));
style_arg.append("../scripts/.clang-format ");
// Need to execute clang format on the generated file to get it to match the original.
#define clang_format "clang-format "
#define cf_format_inplace "-i "
#define cf_verbose "-verbose "
String command = String::make( GlobalAllocator, clang_format );
command.append( cf_format_inplace );
command.append( cf_verbose );
command.append( style_arg );
command.append( resolved_path );
log_fmt("\tRunning clang-format on file:\n");
system( command );
log_fmt("\tclang-format finished reformatting.\n");
#undef cf_cmd
#undef cf_format_inplace
#undef cf_style
#undef cf_verbse
}
Code dump_to_scratch_and_retireve( Code code )
{
Builder ecode_file_temp = Builder::open("gen/scratch.hpp");
ecode_file_temp.print(code);
ecode_file_temp.write();
format_file("gen/scratch.hpp");
Code result = scan_file( "gen/scratch.hpp" );
remove("gen/scratch.hpp");
return result;
}
int gen_main()
{
#define project_dir "../project/"
gen::init();
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 single_header_start = scan_file( "components/header_start.hpp" );
Builder
header = Builder::open( "gen/gen.hpp" );
header.print_fmt( generation_notice );
header.print_fmt("#pragma once\n\n");
header.print( push_ignores );
// Headers
{
header.print( single_header_start );
if ( generate_gen_dep )
{
Code platform = scan_file( project_dir "dependencies/platform.hpp" );
Code macros = scan_file( project_dir "dependencies/macros.hpp" );
Code basic_types = scan_file( project_dir "dependencies/basic_types.hpp" );
Code debug = scan_file( project_dir "dependencies/debug.hpp" );
Code memory = scan_file( project_dir "dependencies/memory.hpp" );
Code string_ops = scan_file( project_dir "dependencies/string_ops.hpp" );
Code printing = scan_file( project_dir "dependencies/printing.hpp" );
Code containers = scan_file( project_dir "dependencies/containers.hpp" );
Code hashing = scan_file( project_dir "dependencies/hashing.hpp" );
Code strings = scan_file( project_dir "dependencies/strings.hpp" );
Code filesystem = scan_file( project_dir "dependencies/filesystem.hpp" );
Code timing = scan_file( project_dir "dependencies/timing.hpp" );
header.print_fmt( roll_own_dependencies_guard_start );
header.print( platform );
header.print_fmt( "\nGEN_NS_BEGIN\n" );
header.print( macros );
header.print( basic_types );
header.print( debug );
header.print( memory );
header.print( string_ops );
header.print( printing );
header.print( containers );
header.print( hashing );
header.print( strings );
header.print( filesystem );
header.print( timing );
if ( generate_scanner )
{
header.print_fmt( "\n#pragma region Parsing\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_newline );
}
Code types = scan_file( project_dir "components/types.hpp" );
Code ast = scan_file( project_dir "components/ast.hpp" );
Code ast_types = scan_file( project_dir "components/ast_types.hpp" );
Code code_types = scan_file( project_dir "components/code_types.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 eoperator = gen_eoperator ( project_dir "enums/EOperator.csv" );
CodeBody especifier = gen_especifier( project_dir "enums/ESpecifier.csv" );
CodeBody ast_inlines = gen_ast_inlines();
header.print_fmt( "GEN_NS_BEGIN\n\n" );
header.print_fmt("#pragma region Types\n");
header.print( types );
header.print( fmt_newline );
header.print( dump_to_scratch_and_retireve( ecode ));
header.print( fmt_newline );
header.print( dump_to_scratch_and_retireve( eoperator ));
header.print( fmt_newline );
header.print( dump_to_scratch_and_retireve( especifier ));
header.print( fmt_newline );
header.print_fmt("#pragma endregion Types\n\n");
header.print_fmt("#pragma region AST\n");
header.print( ast );
header.print( code_types );
header.print( ast_types );
header.print_fmt("\n#pragma endregion AST\n");
header.print( interface );
header.print_fmt( "\n#pragma region Inlines\n" );
header.print( inlines );
header.print( dump_to_scratch_and_retireve( ast_inlines ));
header.print( fmt_newline );
header.print_fmt( "#pragma endregion Inlines\n" );
header.print( header_end );
if ( generate_builder )
{
header.print_fmt( "\n#pragma region Builder\n" );
header.print( scan_file( project_dir "auxillary/builder.hpp" ) );
header.print_fmt( "#pragma endregion Builder\n" );
}
header.print_fmt( "GEN_NS_END\n" );
}
// Implementation
{
header.print_fmt( "%s\n", (char const*) implementation_guard_start );
if ( generate_gen_dep )
{
Code impl_start = scan_file( project_dir "dependencies/src_start.cpp" );
Code debug = scan_file( project_dir "dependencies/debug.cpp" );
Code string_ops = scan_file( project_dir "dependencies/string_ops.cpp" );
Code printing = scan_file( project_dir "dependencies/printing.cpp" );
Code memory = scan_file( project_dir "dependencies/memory.cpp" );
Code hashing = scan_file( project_dir "dependencies/hashing.cpp" );
Code strings = scan_file( project_dir "dependencies/strings.cpp" );
Code filesystem = scan_file( project_dir "dependencies/filesystem.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( impl_start );
header.print( debug );
header.print( string_ops );
header.print( printing );
header.print( memory );
header.print( hashing );
header.print( strings );
header.print( filesystem );
header.print( timing );
if ( generate_scanner )
{
header.print_fmt( "\n#pragma region Parsing\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( roll_own_dependencies_guard_end );
}
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 = scan_file( project_dir "components/ast.cpp" );
Code code = scan_file( project_dir "components/code_serialization.cpp" );
Code interface = scan_file( project_dir "components/interface.cpp" );
Code upfront = scan_file( project_dir "components/interface.upfront.cpp" );
Code lexer = scan_file( project_dir "components/lexer.cpp" );
Code parser = scan_file( project_dir "components/parser.cpp" );
Code parsing_interface = scan_file( project_dir "components/interface.parsing.cpp" );
Code untyped = scan_file( project_dir "components/interface.untyped.cpp" );
CodeBody etoktype = gen_etoktype( project_dir "enums/ETokType.csv", project_dir "enums/AttributeTokens.csv" );
CodeNS parser_nspace = def_namespace( name(parser), def_namespace_body( args(etoktype)) );
header.print_fmt( "\nGEN_NS_BEGIN\n");
header.print( static_data );
header.print_fmt( "#pragma region AST\n\n" );
header.print( ast_case_macros );
header.print( ast );
header.print( code );
header.print_fmt( "#pragma endregion AST\n\n" );
header.print_fmt( "#pragma region Interface\n" );
header.print( interface );
header.print( upfront );
header.print_fmt( "\n#pragma region Parsing\n\n" );
header.print( dump_to_scratch_and_retireve(parser_nspace) );
header.print( lexer );
header.print( parser );
header.print( parsing_interface );
header.print_fmt( "\n#pragma endregion Parsing\n" );
header.print( untyped );
header.print_fmt( "\n#pragma endregion Interface\n\n");
if ( generate_builder )
{
header.print_fmt( "#pragma region Builder\n" );
header.print( scan_file( project_dir "auxillary/builder.cpp" ) );
header.print_fmt( "\n#pragma endregion Builder\n\n" );
}
// Scanner header depends on implementation
if ( generate_scanner )
{
header.print_fmt( "\n#pragma region Scanner\n" );
header.print( scan_file( project_dir "auxillary/scanner.hpp" ) );
header.print_fmt( "#pragma endregion Scanner\n\n" );
}
#if 0
if ( generate_scanner )
{
header.print_fmt( "#pragma region Scanner\n\n" );
header.print( scan_file( project_dir "auxillary/scanner.cpp" ) );
header.print_fmt( "#pragma endregion Scanner\n\n" );
}
#endif
header.print_fmt( "GEN_NS_END\n");
header.print_fmt( "%s\n", (char const*) implementation_guard_end );
}
header.print( pop_ignores );
header.write();
gen::deinit();
return 0;
#undef project_dir
}

View File

@ -0,0 +1,3 @@
# Unreal Engine Version Generator
This generates a variant of gencpp thats compatiable with use as a thirdparty module within a plugin or module of an Unreal Project or the Engine itself.

View File

@ -0,0 +1,31 @@
/*
gencpp: An attempt at "simple" staged metaprogramming for c/c++.
See Readme.md for more information from the project repository.
Public Address:
https://github.com/Ed94/gencpp
This is a variant intended for use with Unreal Engine 5
*/
#if ! defined(GEN_DONT_ENFORCE_GEN_TIME_GUARD) && ! defined(GEN_TIME)
# error Gen.hpp : GEN_TIME not defined
#endif
//! 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
#ifndef GEN_ROLL_OWN_DEPENDENCIES
# include "gen.dep.hpp"
#endif
#ifndef GEN_NS_BEGIN
# ifdef GEN_DONT_USE_NAMESPACE
# define GEN_NS
# define GEN_NS_BEGIN
# define GEN_NS_END
# else
# define GEN_NS gen::
# define GEN_NS_BEGIN namespace gen {
# define GEN_NS_END }
# endif
#endif

View File

@ -0,0 +1,5 @@
#if ! defined(GEN_DONT_ENFORCE_GEN_TIME_GUARD) && ! defined(GEN_TIME)
# error Gen.hpp : GEN_TIME not defined
#endif
#include "gen.hpp"

View File

@ -0,0 +1,7 @@
API_Export, GEN_API_Export_Code
API_Import, GEN_API_Import_Code
COREUOBJECT_API, COREUOBJECT_API
ENGINE_API, ENGINE_API
GAMEPLAYABILITIES_API, GAMEPLAYABILITIES_API
UMG_API, UMG_API
UE_DEPRECATED, UE_DEPRECATED
1 API_Export GEN_API_Export_Code
2 API_Import GEN_API_Import_Code
3 COREUOBJECT_API COREUOBJECT_API
4 ENGINE_API ENGINE_API
5 GAMEPLAYABILITIES_API GAMEPLAYABILITIES_API
6 UMG_API UMG_API
7 UE_DEPRECATED UE_DEPRECATED

View File

@ -0,0 +1,397 @@
#define GEN_DEFINE_LIBRARY_CODE_CONSTANTS
#define GEN_ENFORCE_STRONG_CODE_TYPES
#define GEN_EXPOSE_BACKEND
#include "gen.cpp"
#include "helpers/push_ignores.inline.hpp"
#include "helpers/helper.hpp"
GEN_NS_BEGIN
#include "dependencies/parsing.cpp"
GEN_NS_END
#include "auxillary/builder.hpp"
#include "auxillary/builder.cpp"
#include "auxillary/scanner.hpp"
#include <cstdlib> // for system()
using namespace gen;
constexpr char const* generation_notice =
"// This file was generated automatially by gencpp's unreal.cpp "
"(See: https://github.com/Ed94/gencpp)\n\n";
constexpr StrC implementation_guard_start = txt(R"(
#pragma region GENCPP IMPLEMENTATION GUARD
#if defined(GEN_IMPLEMENTATION) && ! defined(GEN_IMPLEMENTED)
# define GEN_IMPLEMENTED
)");
constexpr StrC implementation_guard_end = txt(R"(
#endif
#pragma endregion GENCPP IMPLEMENTATION GUARD
)");
constexpr StrC roll_own_dependencies_guard_start = txt(R"(
//! 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
#ifndef GEN_ROLL_OWN_DEPENDENCIES
)");
constexpr StrC roll_own_dependencies_guard_end = txt(R"(
// GEN_ROLL_OWN_DEPENDENCIES
#endif
)");
global bool generate_gen_dep = true;
global bool generate_builder = true;
global bool generate_editor = true;
global bool generate_scanner = true;
void format_file( char const* path )
{
String resolved_path = String::make(GlobalAllocator, to_str(path));
String style_arg = String::make(GlobalAllocator, txt("-style=file:"));
style_arg.append("../scripts/.clang-format ");
// Need to execute clang format on the generated file to get it to match the original.
#define clang_format "clang-format "
#define cf_format_inplace "-i "
#define cf_verbose "-verbose "
String command = String::make( GlobalAllocator, clang_format );
command.append( cf_format_inplace );
command.append( cf_verbose );
command.append( style_arg );
command.append( resolved_path );
log_fmt("\tRunning clang-format on file:\n");
system( command );
log_fmt("\tclang-format finished reformatting.\n");
#undef cf_cmd
#undef cf_format_inplace
#undef cf_style
#undef cf_verbse
}
Code dump_to_scratch_and_retireve( Code code )
{
Builder ecode_file_temp = Builder::open("gen/scratch.hpp");
ecode_file_temp.print(code);
ecode_file_temp.write();
format_file("gen/scratch.hpp");
Code result = scan_file( "gen/scratch.hpp" );
remove("gen/scratch.hpp");
return result;
}
int gen_main()
{
#define project_dir "../project/"
gen::init();
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 ue_forceinline = code_str(FORCEINLINE);
// Code
// gen_dep.hpp
{
CodeBody macros = def_body( CodeT::Global_Body );
{
FileContents content = file_read_contents( GlobalAllocator, true, project_dir "dependencies/macros.hpp" );
CodeBody ori_macros = parse_global_body( StrC { content.size, (char const*)content.data });
for (Code code = ori_macros.begin();
code != ori_macros.end();
++ code )
{
switch (code->Type)
{
using namespace ECode;
case Preprocess_Define:
{
CodeDefine define = code.cast<CodeDefine>();
if ( define->Name.starts_with(txt("global")) )
{
macros.append(parse_global_body(txt("#define global // Global variables")));
continue;
}
macros.append(define);
}
break;
default:
macros.append(code);
break;
}
}
}
Code platform = scan_file( project_dir "dependencies/platform.hpp" );
Code basic_types = scan_file( project_dir "dependencies/basic_types.hpp" );
Code debug = scan_file( project_dir "dependencies/debug.hpp" );
Code memory = scan_file( project_dir "dependencies/memory.hpp" );
Code string_ops = scan_file( project_dir "dependencies/string_ops.hpp" );
Code printing = scan_file( project_dir "dependencies/printing.hpp" );
Code containers = scan_file( project_dir "dependencies/containers.hpp" );
Code hashing = scan_file( project_dir "dependencies/hashing.hpp" );
Code strings = scan_file( project_dir "dependencies/strings.hpp" );
Code filesystem = scan_file( project_dir "dependencies/filesystem.hpp" );
Code timing = scan_file( project_dir "dependencies/timing.hpp" );
Builder
header = Builder::open("gen/gen.dep.hpp");
header.print_fmt( generation_notice );
header.print( pragma_once );
header.print( push_ignores );
header.print( platform );
header.print_fmt( "\nGEN_NS_BEGIN\n" );
header.print( fmt_newline);
header.print( dump_to_scratch_and_retireve(macros) );
header.print( basic_types );
header.print( debug );
header.print( memory );
header.print( string_ops );
header.print( printing );
header.print( containers );
header.print( hashing );
header.print( strings );
header.print( filesystem );
header.print( timing );
header.print_fmt( "\nGEN_NS_END\n" );
header.print( fmt_newline );
header.print( pop_ignores );
header.write();
}
// gen_dep.cpp
{
Code src_start = scan_file( project_dir "dependencies/src_start.cpp" );
Code debug = scan_file( project_dir "dependencies/debug.cpp" );
Code string_ops = scan_file( project_dir "dependencies/string_ops.cpp" );
Code printing = scan_file( project_dir "dependencies/printing.cpp" );
Code memory = scan_file( project_dir "dependencies/memory.cpp" );
Code hashing = scan_file( project_dir "dependencies/hashing.cpp" );
Code strings = scan_file( project_dir "dependencies/strings.cpp" );
Code filesystem = scan_file( project_dir "dependencies/filesystem.cpp" );
Code timing = scan_file( project_dir "dependencies/timing.cpp" );
Builder
src = Builder::open( "gen/gen.dep.cpp" );
src.print_fmt( generation_notice );
src.print( def_include(txt("gen.dep.hpp")));
src.print( fmt_newline );
src.print( push_ignores );
src.print( src_start );
src.print_fmt( "\nGEN_NS_BEGIN\n" );
src.print( debug );
src.print( string_ops );
src.print( printing );
src.print( hashing );
src.print( memory );
src.print( strings );
src.print( filesystem );
src.print( timing );
src.print_fmt( "\nGEN_NS_END\n" );
src.print( fmt_newline );
src.print( pop_ignores );
src.write();
}
// gen.hpp
{
Code header_start = scan_file( "components/header_start.hpp" );
Code types = scan_file( project_dir "components/types.hpp" );
Code ast = scan_file( project_dir "components/ast.hpp" );
Code ast_types = scan_file( project_dir "components/ast_types.hpp" );
Code code_types = scan_file( project_dir "components/code_types.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 eoperator = gen_eoperator ( project_dir "enums/EOperator.csv" );
CodeBody especifier = gen_especifier( project_dir "enums/ESpecifier.csv" );
CodeBody ast_inlines = gen_ast_inlines();
Builder
header = Builder::open( "gen/gen.hpp" );
header.print_fmt( generation_notice );
header.print_fmt( "#pragma once\n\n" );
header.print( push_ignores );
header.print( fmt_newline );
header.print( header_start );
header.print_fmt( "\nGEN_NS_BEGIN\n\n" );
header.print_fmt( "#pragma region Types\n" );
header.print( types );
header.print( fmt_newline );
header.print( dump_to_scratch_and_retireve(ecode) );
header.print( fmt_newline );
header.print( dump_to_scratch_and_retireve(eoperator) );
header.print( fmt_newline );
header.print( dump_to_scratch_and_retireve(especifier) );
header.print( fmt_newline );
header.print_fmt( "#pragma endregion Types\n\n" );
header.print_fmt( "#pragma region AST\n" );
header.print( ast );
header.print( code_types );
header.print( ast_types );
header.print_fmt( "\n#pragma endregion AST\n" );
header.print( interface );
header.print_fmt( "\n#pragma region Inlines\n" );
header.print( inlines );
header.print( fmt_newline );
header.print( dump_to_scratch_and_retireve(ast_inlines) );
header.print( fmt_newline );
header.print_fmt( "#pragma endregion Inlines\n" );
header.print( header_end );
header.print_fmt( "GEN_NS_END\n\n" );
header.print( pop_ignores );
header.write();
}
// gen.cpp
{
Code src_start = scan_file( "components/src_start.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 = scan_file( project_dir "components/ast.cpp" );
Code code_serialization = scan_file( project_dir "components/code_serialization.cpp" );
Code interface = scan_file( project_dir "components/interface.cpp" );
Code upfront = scan_file( project_dir "components/interface.upfront.cpp" );
Code lexer = scan_file( project_dir "components/lexer.cpp" );
Code parser = scan_file( project_dir "components/parser.cpp" );
Code parsing_interface = scan_file( project_dir "components/interface.parsing.cpp" );
Code untyped = scan_file( project_dir "components/interface.untyped.cpp" );
// Note(Ed): The Attribute tokens need to be expanded and regenerated on a per-project/installation of this library for a specific codebase of Unreal.
// We can support an arbitrary set of modules or plugin apis for parsing
// but its up to the user to define them all (This will just provide whats I've used up till now).
CodeBody etoktype = gen_etoktype( project_dir "enums/ETokType.csv", "enums/AttributeTokens.csv" );
CodeNS nspaced_etoktype = def_namespace( name(parser), def_namespace_body( args(etoktype)) );
Builder
src = Builder::open( "gen/gen.cpp" );
src.print_fmt( generation_notice );
src.print( push_ignores );
src.print( fmt_newline );
src.print( src_start );
src.print( fmt_newline );
src.print_fmt( "GEN_NS_BEGIN\n");
src.print( static_data );
src.print_fmt( "\n#pragma region AST\n\n" );
src.print( ast_case_macros );
src.print( ast );
src.print( code_serialization );
src.print_fmt( "\n#pragma endregion AST\n" );
src.print_fmt( "\n#pragma region Interface\n" );
src.print( interface );
src.print( upfront );
src.print_fmt( "\n#pragma region Parsing\n\n" );
src.print( dump_to_scratch_and_retireve(nspaced_etoktype) );
src.print( lexer );
src.print( parser );
src.print( parsing_interface );
src.print( untyped );
src.print_fmt( "\n#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( project_dir "auxillary/builder.hpp" );
Builder
header = Builder::open( "gen/gen.builder.hpp" );
header.print_fmt( generation_notice );
header.print( push_ignores );
header.print( fmt_newline );
header.print_fmt( "#pragma once\n\n" );
header.print( def_include( txt("gen.hpp") ));
header.print_fmt( "\nGEN_NS_BEGIN\n" );
header.print( builder );
header.print_fmt( "GEN_NS_END\n" );
header.print( fmt_newline );
header.print( pop_ignores );
header.write();
}
// gen_builder.cpp
{
Code builder = scan_file( project_dir "auxillary/builder.cpp" );
Builder
src = Builder::open( "gen/gen.builder.cpp" );
src.print_fmt( generation_notice );
src.print( push_ignores );
src.print( fmt_newline );
src.print( def_include( txt("gen.builder.hpp") ) );
src.print_fmt( "\nGEN_NS_BEGIN\n" );
src.print( builder );
src.print_fmt( "\nGEN_NS_END\n" );
src.print( fmt_newline );
src.print( pop_ignores );
src.write();
}
// gen_scanner.hpp
{
Code parsing = scan_file( project_dir "dependencies/parsing.hpp" );
Code scanner = scan_file( project_dir "auxillary/scanner.hpp" );
Builder
header = Builder::open( "gen/gen.scanner.hpp" );
header.print_fmt( generation_notice );
header.print_fmt( "#pragma once\n\n" );
header.print( push_ignores );
header.print( fmt_newline );
header.print( def_include( txt("gen.hpp") ) );
header.print_fmt( "\nGEN_NS_BEGIN\n" );
header.print( parsing );
header.print( scanner );
header.print_fmt( "GEN_NS_END\n" );
header.print( fmt_newline );
header.print( pop_ignores );
header.write();
}
// gen.scanner.cpp
{
Code parsing = scan_file( project_dir "dependencies/parsing.cpp" );
Code scanner = scan_file( project_dir "auxillary/scanner.cpp" );
Builder
src = Builder::open( "gen/gen.scanner.cpp" );
src.print_fmt( generation_notice );
src.print( push_ignores );
src.print( fmt_newline );
src.print( def_include( txt("gen.scanner.hpp") ) );
src.print_fmt( "\nGEN_NS_BEGIN\n" );
src.print( parsing );
// src.print( scanner );
src.print_fmt( "GEN_NS_END\n" );
src.print( fmt_newline );
src.print( pop_ignores );
src.write();
}
}

View File

@ -6,23 +6,26 @@
<SyncFiles>true</SyncFiles> <SyncFiles>true</SyncFiles>
<Recursive>true</Recursive> <Recursive>true</Recursive>
<ShowEmptyFolders>true</ShowEmptyFolders> <ShowEmptyFolders>true</ShowEmptyFolders>
<IncludeFilesWithoutExt>false</IncludeFilesWithoutExt>
<IsVirtual>false</IsVirtual> <IsVirtual>false</IsVirtual>
<IsFolder>false</IsFolder> <IsFolder>false</IsFolder>
<BuildCommand>powershell ./scripts/build.ps1</BuildCommand> <BuildCommand>pwsh ./scripts/build.ps1 msvc debug bootstrap</BuildCommand>
<RebuildCommand>powershell ./scripts/rebuild.ps1</RebuildCommand> <RebuildCommand></RebuildCommand>
<BuildFileCommand></BuildFileCommand> <BuildFileCommand></BuildFileCommand>
<CleanCommand>powershell ./scripts/clean.ps1</CleanCommand> <CleanCommand>pwsh ./scripts/clean.ps1</CleanCommand>
<BuildWorkingDirectory></BuildWorkingDirectory> <BuildWorkingDirectory></BuildWorkingDirectory>
<CancelBuild></CancelBuild> <CancelBuild></CancelBuild>
<RunCommand>./test/gen/build/gencpp.exe</RunCommand> <Exe>./test/gen/build/gencpp.exe</Exe>
<RunCommandWorkingDirectory></RunCommandWorkingDirectory> <Args></Args>
<DebugCommand>powershell ./scripts/build.ps1</DebugCommand> <WorkingDirectory></WorkingDirectory>
<ExePathCommand>./test/gen/build/gencpp.exe</ExePathCommand> <DebugCommand>pwsh ./scripts/build.ps1</DebugCommand>
<DebugSln>gencpp.sln</DebugSln> <DebugSln></DebugSln>
<UseVisualStudioEnvBat>false</UseVisualStudioEnvBat> <UseVisualStudioEnvBat>true</UseVisualStudioEnvBat>
<CaptureExeOutput>false</CaptureExeOutput>
<Configurations> <Configurations>
<Configuration>Debug</Configuration> <Configuration>Debug</Configuration>
<Configuration>Release</Configuration> <Configuration>Release</Configuration>
<Configuration>bootstrap debug</Configuration>
</Configurations> </Configurations>
<Platforms> <Platforms>
<Platform>x64</Platform> <Platform>x64</Platform>
@ -31,24 +34,40 @@
<AdditionalIncludePath>C:\Program Files\Microsoft Visual Studio\2022\Professional\VC\Tools\MSVC\14.36.32532\include</AdditionalIncludePath> <AdditionalIncludePath>C:\Program Files\Microsoft Visual Studio\2022\Professional\VC\Tools\MSVC\14.36.32532\include</AdditionalIncludePath>
<AdditionalIncludePath>C:\Program Files\Microsoft Visual Studio\2022\Professional\VC\Tools\MSVC\14.36.32532\ATLMFC\include</AdditionalIncludePath> <AdditionalIncludePath>C:\Program Files\Microsoft Visual Studio\2022\Professional\VC\Tools\MSVC\14.36.32532\ATLMFC\include</AdditionalIncludePath>
<AdditionalIncludePath>C:\Program Files\Microsoft Visual Studio\2022\Professional\VC\Auxiliary\VS\include</AdditionalIncludePath> <AdditionalIncludePath>C:\Program Files\Microsoft Visual Studio\2022\Professional\VC\Auxiliary\VS\include</AdditionalIncludePath>
<AdditionalIncludePath>C:\Program Files (x86)\Windows Kits\10\include\10.0.19041.0\ucrt</AdditionalIncludePath> <AdditionalIncludePath>C:\Program Files (x86)\Windows Kits\10\include\10.0.22621.0\ucrt</AdditionalIncludePath>
<AdditionalIncludePath>C:\Program Files (x86)\Windows Kits\10\\include\10.0.19041.0\\um</AdditionalIncludePath> <AdditionalIncludePath>C:\Program Files (x86)\Windows Kits\10\\include\10.0.22621.0\\um</AdditionalIncludePath>
<AdditionalIncludePath>C:\Program Files (x86)\Windows Kits\10\\include\10.0.19041.0\\shared</AdditionalIncludePath> <AdditionalIncludePath>C:\Program Files (x86)\Windows Kits\10\\include\10.0.22621.0\\shared</AdditionalIncludePath>
<AdditionalIncludePath>C:\Program Files (x86)\Windows Kits\10\\include\10.0.19041.0\\winrt</AdditionalIncludePath> <AdditionalIncludePath>C:\Program Files (x86)\Windows Kits\10\\include\10.0.22621.0\\winrt</AdditionalIncludePath>
<AdditionalIncludePath>C:\Program Files (x86)\Windows Kits\10\\include\10.0.19041.0\\cppwinrt</AdditionalIncludePath> <AdditionalIncludePath>C:\Program Files (x86)\Windows Kits\10\\include\10.0.22621.0\\cppwinrt</AdditionalIncludePath>
<AdditionalIncludePath>C:\Program Files (x86)\Windows Kits\NETFXSDK\4.8\include\um</AdditionalIncludePath> <AdditionalIncludePath>C:\Program Files (x86)\Windows Kits\NETFXSDK\4.8\include\um</AdditionalIncludePath>
</AdditionalIncludePaths> </AdditionalIncludePaths>
<Defines></Defines> <Defines>
<Define>GEN_TIME</Define>
<Define>GEN_SYSTEM_WINDOWS</Define>
<Define>GEN_INTELLISENSE_DIRECTIVES</Define>
<Define>GEN_EXECUTION_EXPRESSION_SUPPORT</Define>
<Define>GEN_BENCHMARK</Define>
<Define>GEN_COMPILER_MSVC</Define>
</Defines>
<ConfigProperties> <ConfigProperties>
<ConfigAndPlatform> <ConfigAndPlatform>
<Name>Debug:x64</Name> <Name>Debug:x64</Name>
<Defines></Defines> <Defines></Defines>
<ForceIncludes></ForceIncludes> <ForceIncludes></ForceIncludes>
</ConfigAndPlatform> </ConfigAndPlatform>
<ConfigAndPlatform>
<Name>bootstrap debug:x64</Name>
<Defines></Defines>
<ForceIncludes></ForceIncludes>
</ConfigAndPlatform>
<Config> <Config>
<Name>Debug</Name> <Name>Debug</Name>
<Defines></Defines> <Defines></Defines>
</Config> </Config>
<Config>
<Name>bootstrap debug</Name>
<Defines></Defines>
</Config>
<Platform> <Platform>
<Name>x64</Name> <Name>x64</Name>
<Defines></Defines> <Defines></Defines>

View File

@ -7,20 +7,37 @@ Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "gencpp", "gencpp.vcxproj",
EndProject EndProject
Global Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|x64 = Debug|x64 bootstrap debug|x64 = bootstrap debug|x64
Debug|x86 = Debug|x86 bootstrap debug|x86 = bootstrap debug|x86
Release|x64 = Release|x64 bootstrap release|x64 = bootstrap release|x64
Release|x86 = Release|x86 bootstrap release|x86 = bootstrap release|x86
singleheader debug|x64 = singleheader debug|x64
singleheader debug|x86 = singleheader debug|x86
singleheader release|x64 = singleheader release|x64
singleheader release|x86 = singleheader release|x86
test debug|x64 = test debug|x64
test debug|x86 = test debug|x86
EndGlobalSection EndGlobalSection
GlobalSection(ProjectConfigurationPlatforms) = postSolution GlobalSection(ProjectConfigurationPlatforms) = postSolution
{53AF600D-C09C-4F39-83E0-E022AA9479F2}.Debug|x64.ActiveCfg = Debug|x64 {53AF600D-C09C-4F39-83E0-E022AA9479F2}.bootstrap debug|x64.ActiveCfg = bootstrap release|x64
{53AF600D-C09C-4F39-83E0-E022AA9479F2}.Debug|x64.Build.0 = Debug|x64 {53AF600D-C09C-4F39-83E0-E022AA9479F2}.bootstrap debug|x64.Build.0 = bootstrap release|x64
{53AF600D-C09C-4F39-83E0-E022AA9479F2}.Debug|x86.ActiveCfg = Debug|Win32 {53AF600D-C09C-4F39-83E0-E022AA9479F2}.bootstrap debug|x86.ActiveCfg = bootstrap debug|Win32
{53AF600D-C09C-4F39-83E0-E022AA9479F2}.Debug|x86.Build.0 = Debug|Win32 {53AF600D-C09C-4F39-83E0-E022AA9479F2}.bootstrap debug|x86.Build.0 = bootstrap debug|Win32
{53AF600D-C09C-4F39-83E0-E022AA9479F2}.Release|x64.ActiveCfg = Release|x64 {53AF600D-C09C-4F39-83E0-E022AA9479F2}.bootstrap release|x64.ActiveCfg = bootstrap release|x64
{53AF600D-C09C-4F39-83E0-E022AA9479F2}.Release|x64.Build.0 = Release|x64 {53AF600D-C09C-4F39-83E0-E022AA9479F2}.bootstrap release|x86.ActiveCfg = bootstrap release|Win32
{53AF600D-C09C-4F39-83E0-E022AA9479F2}.Release|x86.ActiveCfg = Release|Win32 {53AF600D-C09C-4F39-83E0-E022AA9479F2}.bootstrap release|x86.Build.0 = bootstrap release|Win32
{53AF600D-C09C-4F39-83E0-E022AA9479F2}.Release|x86.Build.0 = Release|Win32 {53AF600D-C09C-4F39-83E0-E022AA9479F2}.singleheader debug|x64.ActiveCfg = singleheader debug|x64
{53AF600D-C09C-4F39-83E0-E022AA9479F2}.singleheader debug|x64.Build.0 = singleheader debug|x64
{53AF600D-C09C-4F39-83E0-E022AA9479F2}.singleheader debug|x86.ActiveCfg = singleheader debug|Win32
{53AF600D-C09C-4F39-83E0-E022AA9479F2}.singleheader debug|x86.Build.0 = singleheader debug|Win32
{53AF600D-C09C-4F39-83E0-E022AA9479F2}.singleheader release|x64.ActiveCfg = bootstrap debug|x64
{53AF600D-C09C-4F39-83E0-E022AA9479F2}.singleheader release|x64.Build.0 = bootstrap debug|x64
{53AF600D-C09C-4F39-83E0-E022AA9479F2}.singleheader release|x86.ActiveCfg = singleheader release|Win32
{53AF600D-C09C-4F39-83E0-E022AA9479F2}.singleheader release|x86.Build.0 = singleheader release|Win32
{53AF600D-C09C-4F39-83E0-E022AA9479F2}.test debug|x64.ActiveCfg = test debug|x64
{53AF600D-C09C-4F39-83E0-E022AA9479F2}.test debug|x64.Build.0 = test debug|x64
{53AF600D-C09C-4F39-83E0-E022AA9479F2}.test debug|x86.ActiveCfg = test debug|Win32
{53AF600D-C09C-4F39-83E0-E022AA9479F2}.test debug|x86.Build.0 = test debug|Win32
EndGlobalSection EndGlobalSection
GlobalSection(SolutionProperties) = preSolution GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE HideSolutionNode = FALSE

View File

@ -1,20 +1,44 @@
<?xml version="1.0" encoding="utf-8"?> <?xml version="1.0" encoding="utf-8"?>
<Project DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003"> <Project DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<ItemGroup Label="ProjectConfigurations"> <ItemGroup Label="ProjectConfigurations">
<ProjectConfiguration Include="Debug|Win32"> <ProjectConfiguration Include="bootstrap debug|Win32">
<Configuration>Debug</Configuration> <Configuration>bootstrap debug</Configuration>
<Platform>Win32</Platform> <Platform>Win32</Platform>
</ProjectConfiguration> </ProjectConfiguration>
<ProjectConfiguration Include="Release|Win32"> <ProjectConfiguration Include="bootstrap debug|x64">
<Configuration>Release</Configuration> <Configuration>bootstrap debug</Configuration>
<Platform>Win32</Platform>
</ProjectConfiguration>
<ProjectConfiguration Include="Debug|x64">
<Configuration>Debug</Configuration>
<Platform>x64</Platform> <Platform>x64</Platform>
</ProjectConfiguration> </ProjectConfiguration>
<ProjectConfiguration Include="Release|x64"> <ProjectConfiguration Include="bootstrap release|Win32">
<Configuration>Release</Configuration> <Configuration>bootstrap release</Configuration>
<Platform>Win32</Platform>
</ProjectConfiguration>
<ProjectConfiguration Include="bootstrap release|x64">
<Configuration>bootstrap release</Configuration>
<Platform>x64</Platform>
</ProjectConfiguration>
<ProjectConfiguration Include="singleheader debug|Win32">
<Configuration>singleheader debug</Configuration>
<Platform>Win32</Platform>
</ProjectConfiguration>
<ProjectConfiguration Include="singleheader debug|x64">
<Configuration>singleheader debug</Configuration>
<Platform>x64</Platform>
</ProjectConfiguration>
<ProjectConfiguration Include="singleheader release|Win32">
<Configuration>singleheader release</Configuration>
<Platform>Win32</Platform>
</ProjectConfiguration>
<ProjectConfiguration Include="singleheader release|x64">
<Configuration>singleheader release</Configuration>
<Platform>x64</Platform>
</ProjectConfiguration>
<ProjectConfiguration Include="test debug|Win32">
<Configuration>test debug</Configuration>
<Platform>Win32</Platform>
</ProjectConfiguration>
<ProjectConfiguration Include="test debug|x64">
<Configuration>test debug</Configuration>
<Platform>x64</Platform> <Platform>x64</Platform>
</ProjectConfiguration> </ProjectConfiguration>
</ItemGroup> </ItemGroup>
@ -25,24 +49,54 @@
<WindowsTargetPlatformVersion>10.0</WindowsTargetPlatformVersion> <WindowsTargetPlatformVersion>10.0</WindowsTargetPlatformVersion>
</PropertyGroup> </PropertyGroup>
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.Default.props" /> <Import Project="$(VCTargetsPath)\Microsoft.Cpp.Default.props" />
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'" Label="Configuration"> <PropertyGroup Condition="'$(Configuration)|$(Platform)'=='bootstrap debug|Win32'" Label="Configuration">
<ConfigurationType>Makefile</ConfigurationType> <ConfigurationType>Makefile</ConfigurationType>
<UseDebugLibraries>true</UseDebugLibraries> <UseDebugLibraries>true</UseDebugLibraries>
<PlatformToolset>v143</PlatformToolset> <PlatformToolset>v143</PlatformToolset>
</PropertyGroup> </PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|Win32'" Label="Configuration"> <PropertyGroup Condition="'$(Configuration)|$(Platform)'=='test debug|Win32'" Label="Configuration">
<ConfigurationType>Makefile</ConfigurationType>
<UseDebugLibraries>false</UseDebugLibraries>
<PlatformToolset>v143</PlatformToolset>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|x64'" Label="Configuration">
<ConfigurationType>Makefile</ConfigurationType> <ConfigurationType>Makefile</ConfigurationType>
<UseDebugLibraries>true</UseDebugLibraries> <UseDebugLibraries>true</UseDebugLibraries>
<PlatformToolset>v143</PlatformToolset> <PlatformToolset>v143</PlatformToolset>
</PropertyGroup> </PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|x64'" Label="Configuration"> <PropertyGroup Condition="'$(Configuration)|$(Platform)'=='singleheader debug|Win32'" Label="Configuration">
<ConfigurationType>Makefile</ConfigurationType> <ConfigurationType>Makefile</ConfigurationType>
<UseDebugLibraries>false</UseDebugLibraries> <UseDebugLibraries>true</UseDebugLibraries>
<PlatformToolset>v143</PlatformToolset>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='singleheader release|Win32'" Label="Configuration">
<ConfigurationType>Makefile</ConfigurationType>
<UseDebugLibraries>true</UseDebugLibraries>
<PlatformToolset>v143</PlatformToolset>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='bootstrap release|Win32'" Label="Configuration">
<ConfigurationType>Makefile</ConfigurationType>
<UseDebugLibraries>true</UseDebugLibraries>
<PlatformToolset>v143</PlatformToolset>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='bootstrap debug|x64'" Label="Configuration">
<ConfigurationType>Makefile</ConfigurationType>
<UseDebugLibraries>true</UseDebugLibraries>
<PlatformToolset>v143</PlatformToolset>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='test debug|x64'" Label="Configuration">
<ConfigurationType>Makefile</ConfigurationType>
<UseDebugLibraries>true</UseDebugLibraries>
<PlatformToolset>v143</PlatformToolset>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='singleheader debug|x64'" Label="Configuration">
<ConfigurationType>Makefile</ConfigurationType>
<UseDebugLibraries>true</UseDebugLibraries>
<PlatformToolset>v143</PlatformToolset>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='singleheader release|x64'" Label="Configuration">
<ConfigurationType>Makefile</ConfigurationType>
<UseDebugLibraries>true</UseDebugLibraries>
<PlatformToolset>v143</PlatformToolset>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='bootstrap release|x64'" Label="Configuration">
<ConfigurationType>Makefile</ConfigurationType>
<UseDebugLibraries>true</UseDebugLibraries>
<PlatformToolset>v143</PlatformToolset> <PlatformToolset>v143</PlatformToolset>
</PropertyGroup> </PropertyGroup>
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.props" /> <Import Project="$(VCTargetsPath)\Microsoft.Cpp.props" />
@ -50,97 +104,195 @@
</ImportGroup> </ImportGroup>
<ImportGroup Label="Shared"> <ImportGroup Label="Shared">
</ImportGroup> </ImportGroup>
<ImportGroup Label="PropertySheets" Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'"> <ImportGroup Condition="'$(Configuration)|$(Platform)'=='bootstrap debug|Win32'" Label="PropertySheets">
<Import Project="$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props" Condition="exists('$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props')" Label="LocalAppDataPlatform" /> <Import Project="$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props" Condition="exists('$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props')" Label="LocalAppDataPlatform" />
</ImportGroup> </ImportGroup>
<ImportGroup Label="PropertySheets" Condition="'$(Configuration)|$(Platform)'=='Release|Win32'"> <ImportGroup Condition="'$(Configuration)|$(Platform)'=='test debug|Win32'" Label="PropertySheets">
<Import Project="$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props" Condition="exists('$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props')" Label="LocalAppDataPlatform" /> <Import Project="$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props" Condition="exists('$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props')" Label="LocalAppDataPlatform" />
</ImportGroup> </ImportGroup>
<ImportGroup Label="PropertySheets" Condition="'$(Configuration)|$(Platform)'=='Debug|x64'"> <ImportGroup Condition="'$(Configuration)|$(Platform)'=='singleheader debug|Win32'" Label="PropertySheets">
<Import Project="$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props" Condition="exists('$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props')" Label="LocalAppDataPlatform" /> <Import Project="$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props" Condition="exists('$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props')" Label="LocalAppDataPlatform" />
</ImportGroup> </ImportGroup>
<ImportGroup Label="PropertySheets" Condition="'$(Configuration)|$(Platform)'=='Release|x64'"> <ImportGroup Condition="'$(Configuration)|$(Platform)'=='singleheader release|Win32'" Label="PropertySheets">
<Import Project="$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props" Condition="exists('$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props')" Label="LocalAppDataPlatform" />
</ImportGroup>
<ImportGroup Condition="'$(Configuration)|$(Platform)'=='bootstrap release|Win32'" Label="PropertySheets">
<Import Project="$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props" Condition="exists('$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props')" Label="LocalAppDataPlatform" />
</ImportGroup>
<ImportGroup Condition="'$(Configuration)|$(Platform)'=='bootstrap debug|x64'" Label="PropertySheets">
<Import Project="$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props" Condition="exists('$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props')" Label="LocalAppDataPlatform" />
</ImportGroup>
<ImportGroup Condition="'$(Configuration)|$(Platform)'=='test debug|x64'" Label="PropertySheets">
<Import Project="$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props" Condition="exists('$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props')" Label="LocalAppDataPlatform" />
</ImportGroup>
<ImportGroup Condition="'$(Configuration)|$(Platform)'=='singleheader debug|x64'" Label="PropertySheets">
<Import Project="$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props" Condition="exists('$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props')" Label="LocalAppDataPlatform" />
</ImportGroup>
<ImportGroup Condition="'$(Configuration)|$(Platform)'=='singleheader release|x64'" Label="PropertySheets">
<Import Project="$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props" Condition="exists('$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props')" Label="LocalAppDataPlatform" />
</ImportGroup>
<ImportGroup Condition="'$(Configuration)|$(Platform)'=='bootstrap release|x64'" Label="PropertySheets">
<Import Project="$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props" Condition="exists('$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props')" Label="LocalAppDataPlatform" /> <Import Project="$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props" Condition="exists('$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props')" Label="LocalAppDataPlatform" />
</ImportGroup> </ImportGroup>
<PropertyGroup Label="UserMacros" /> <PropertyGroup Label="UserMacros" />
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'"> <PropertyGroup Condition="'$(Configuration)|$(Platform)'=='bootstrap debug|Win32'">
<NMakeBuildCommandLine>./scripts/build.ps1</NMakeBuildCommandLine> <NMakeBuildCommandLine>./scripts/build.ps1</NMakeBuildCommandLine>
<NMakeCleanCommandLine>./scripts/clean.ps1</NMakeCleanCommandLine> <NMakeCleanCommandLine>./scripts/clean.ps1</NMakeCleanCommandLine>
<NMakeReBuildCommandLine>./scripts/build.ps1</NMakeReBuildCommandLine> <NMakeReBuildCommandLine>./scripts/build.ps1</NMakeReBuildCommandLine>
<NMakePreprocessorDefinitions>WIN32;_DEBUG;$(NMakePreprocessorDefinitions)</NMakePreprocessorDefinitions> <NMakePreprocessorDefinitions>WIN32;_DEBUG;$(NMakePreprocessorDefinitions)</NMakePreprocessorDefinitions>
</PropertyGroup> </PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|Win32'"> <PropertyGroup Condition="'$(Configuration)|$(Platform)'=='test debug|Win32'">
<NMakeBuildCommandLine>./scripts/build.ps1</NMakeBuildCommandLine> <NMakeBuildCommandLine>./scripts/build.ps1</NMakeBuildCommandLine>
<NMakeCleanCommandLine>./scripts/clean.ps1</NMakeCleanCommandLine> <NMakeCleanCommandLine>./scripts/clean.ps1</NMakeCleanCommandLine>
<NMakeReBuildCommandLine>./scripts/build.ps1</NMakeReBuildCommandLine> <NMakeReBuildCommandLine>./scripts/build.ps1</NMakeReBuildCommandLine>
<NMakePreprocessorDefinitions>WIN32;NDEBUG;$(NMakePreprocessorDefinitions)</NMakePreprocessorDefinitions> <NMakePreprocessorDefinitions>WIN32;_DEBUG;$(NMakePreprocessorDefinitions)</NMakePreprocessorDefinitions>
</PropertyGroup> </PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|x64'"> <PropertyGroup Condition="'$(Configuration)|$(Platform)'=='singleheader debug|Win32'">
<NMakeBuildCommandLine>powershell.exe -ExecutionPolicy Unrestricted -File "$(ProjectDir)scripts\build.ps1"</NMakeBuildCommandLine> <NMakeBuildCommandLine>./scripts/build.ps1</NMakeBuildCommandLine>
<NMakeReBuildCommandLine>powershell.exe -ExecutionPolicy Unrestricted -File "$(ProjectDir)scripts\build.ps1"</NMakeReBuildCommandLine> <NMakeCleanCommandLine>./scripts/clean.ps1</NMakeCleanCommandLine>
<NMakeCleanCommandLine>powershell.exe -ExecutionPolicy Unrestricted -File "$(ProjectDir)scripts\clean.ps1"</NMakeCleanCommandLine> <NMakeReBuildCommandLine>./scripts/build.ps1</NMakeReBuildCommandLine>
<NMakePreprocessorDefinitions>GEN_TIME;$(NMakePreprocessorDefinitions)</NMakePreprocessorDefinitions> <NMakePreprocessorDefinitions>WIN32;_DEBUG;$(NMakePreprocessorDefinitions)</NMakePreprocessorDefinitions>
<IncludePath>$(ProjectDir)thirdparty;$(ProjectDir)project;$(ProjectDir)test;$(IncludePath)</IncludePath>
<SourcePath>$(ProjectDir)project;$(ProjectDir)test;$(SourcePath)</SourcePath>
</PropertyGroup> </PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|x64'"> <PropertyGroup Condition="'$(Configuration)|$(Platform)'=='singleheader release|Win32'">
<NMakeBuildCommandLine>powershell.exe -ExecutionPolicy Unrestricted -File "$(ProjectDir)scripts\build.ps1"</NMakeBuildCommandLine> <NMakeBuildCommandLine>./scripts/build.ps1</NMakeBuildCommandLine>
<NMakeReBuildCommandLine>powershell.exe -ExecutionPolicy Unrestricted -File "$(ProjectDir)scripts\build.ps1"</NMakeReBuildCommandLine> <NMakeCleanCommandLine>./scripts/clean.ps1</NMakeCleanCommandLine>
<NMakeCleanCommandLine>powershell.exe -ExecutionPolicy Unrestricted -File "$(ProjectDir)scripts\clean.ps1"</NMakeCleanCommandLine> <NMakeReBuildCommandLine>./scripts/build.ps1</NMakeReBuildCommandLine>
<NMakePreprocessorDefinitions>GEN_TIME;$(NMakePreprocessorDefinitions)</NMakePreprocessorDefinitions> <NMakePreprocessorDefinitions>WIN32;_DEBUG;$(NMakePreprocessorDefinitions)</NMakePreprocessorDefinitions>
<IncludePath>$(ProjectDir)thirdparty;$(ProjectDir)project;$(ProjectDir)test;$(IncludePath)</IncludePath>
<SourcePath>$(ProjectDir)project;$(ProjectDir)test;$(SourcePath)</SourcePath>
</PropertyGroup> </PropertyGroup>
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Debug|x64'"> <PropertyGroup Condition="'$(Configuration)|$(Platform)'=='bootstrap release|Win32'">
<NMakeBuildCommandLine>./scripts/build.ps1</NMakeBuildCommandLine>
<NMakeCleanCommandLine>./scripts/clean.ps1</NMakeCleanCommandLine>
<NMakeReBuildCommandLine>./scripts/build.ps1</NMakeReBuildCommandLine>
<NMakePreprocessorDefinitions>WIN32;_DEBUG;$(NMakePreprocessorDefinitions)</NMakePreprocessorDefinitions>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='bootstrap debug|x64'">
<NMakeBuildCommandLine>pwsh.exe -ExecutionPolicy Unrestricted -File "$(ProjectDir)scripts\build.ps1" msvc debug bootstrap</NMakeBuildCommandLine>
<NMakeReBuildCommandLine>
</NMakeReBuildCommandLine>
<NMakeCleanCommandLine>pwsh.exe -ExecutionPolicy Unrestricted -File "$(ProjectDir)scripts\clean.ps1"</NMakeCleanCommandLine>
<NMakePreprocessorDefinitions>GEN_INTELLISENSE_DIRECTIVES;GEN_TIME;GEN_BENCHMARK;$(NMakePreprocessorDefinitions)</NMakePreprocessorDefinitions>
<IncludePath>$(ProjectDir)project;$(IncludePath)</IncludePath>
<SourcePath>$(ProjectDir)project;$(SourcePath)</SourcePath>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='test debug|x64'">
<NMakeBuildCommandLine>pwsh.exe -ExecutionPolicy Unrestricted -File "$(ProjectDir)scripts\build.ps1" msvc debug test</NMakeBuildCommandLine>
<NMakeReBuildCommandLine />
<NMakeCleanCommandLine>pwsh.exe -ExecutionPolicy Unrestricted -File "$(ProjectDir)scripts\clean.ps1"</NMakeCleanCommandLine>
<NMakePreprocessorDefinitions>GEN_INTELLISENSE_DIRECTIVES;GEN_TIME;GEN_BENCHMARK;$(NMakePreprocessorDefinitions)</NMakePreprocessorDefinitions>
<IncludePath>$(ProjectDir)project;$(ProjectDir)test;$(IncludePath)</IncludePath>
<SourcePath>$(ProjectDir)project;$(SourcePath)</SourcePath>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='singleheader debug|x64'">
<NMakeBuildCommandLine>pwsh.exe -ExecutionPolicy Unrestricted -File "$(ProjectDir)scripts\build.ps1" msvc debug singleheader</NMakeBuildCommandLine>
<NMakeReBuildCommandLine>
</NMakeReBuildCommandLine>
<NMakeCleanCommandLine>pwsh.exe -ExecutionPolicy Unrestricted -File "$(ProjectDir)scripts\clean.ps1"</NMakeCleanCommandLine>
<NMakePreprocessorDefinitions>GEN_INTELLISENSE_DIRECTIVES;GEN_TIME;GEN_BENCHMARK;$(NMakePreprocessorDefinitions)</NMakePreprocessorDefinitions>
<IncludePath>$(ProjectDir)project;$(ProjectDir)test;$(IncludePath)</IncludePath>
<SourcePath>$(ProjectDir)project;$(SourcePath)</SourcePath>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='singleheader release|x64'">
<NMakeBuildCommandLine>pwsh.exe -ExecutionPolicy Unrestricted -File "$(ProjectDir)scripts\build.ps1" msvc release singleheader</NMakeBuildCommandLine>
<NMakeReBuildCommandLine>
</NMakeReBuildCommandLine>
<NMakeCleanCommandLine>pwsh.exe -ExecutionPolicy Unrestricted -File "$(ProjectDir)scripts\clean.ps1"</NMakeCleanCommandLine>
<NMakePreprocessorDefinitions>GEN_INTELLISENSE_DIRECTIVES;GEN_TIME;GEN_BENCHMARK;$(NMakePreprocessorDefinitions)</NMakePreprocessorDefinitions>
<IncludePath>$(ProjectDir)project;$(ProjectDir)test;$(IncludePath)</IncludePath>
<SourcePath>$(ProjectDir)project;$(SourcePath)</SourcePath>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='bootstrap release|x64'">
<NMakeBuildCommandLine>pwsh.exe -ExecutionPolicy Unrestricted -File "$(ProjectDir)scripts\build.ps1" msvc release bootstrap</NMakeBuildCommandLine>
<NMakeReBuildCommandLine>
</NMakeReBuildCommandLine>
<NMakeCleanCommandLine>pwsh.exe -ExecutionPolicy Unrestricted -File "$(ProjectDir)scripts\clean.ps1"</NMakeCleanCommandLine>
<NMakePreprocessorDefinitions>GEN_INTELLISENSE_DIRECTIVES;GEN_TIME;GEN_BENCHMARK;$(NMakePreprocessorDefinitions)</NMakePreprocessorDefinitions>
<IncludePath>$(ProjectDir)project;$(ProjectDir)test;$(IncludePath)</IncludePath>
<SourcePath>$(ProjectDir)project;$(SourcePath)</SourcePath>
</PropertyGroup>
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='bootstrap debug|x64'">
<ClCompile>
<LanguageStandard_C>stdc11</LanguageStandard_C>
</ClCompile>
</ItemDefinitionGroup>
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='test debug|x64'">
<ClCompile>
<LanguageStandard_C>stdc11</LanguageStandard_C>
</ClCompile>
</ItemDefinitionGroup>
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='singleheader debug|x64'">
<ClCompile>
<LanguageStandard_C>stdc11</LanguageStandard_C>
</ClCompile>
</ItemDefinitionGroup>
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='singleheader release|x64'">
<ClCompile>
<LanguageStandard_C>stdc11</LanguageStandard_C>
</ClCompile>
</ItemDefinitionGroup>
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='bootstrap release|x64'">
<ClCompile> <ClCompile>
<LanguageStandard_C>stdc11</LanguageStandard_C> <LanguageStandard_C>stdc11</LanguageStandard_C>
</ClCompile> </ClCompile>
</ItemDefinitionGroup> </ItemDefinitionGroup>
<ItemGroup> <ItemGroup>
<None Include=".editorconfig" /> <None Include=".editorconfig" />
<None Include="project\enums\AttributeTokens.csv" />
<None Include="project\enums\ECode.csv" />
<None Include="project\enums\EOperator.csv" />
<None Include="project\enums\ESpecifier.csv" />
<None Include="project\enums\ETokType.csv" />
<None Include="Readme.md" /> <None Include="Readme.md" />
<None Include="scripts\.clang-format" />
<None Include="scripts\build.ci.ps1" /> <None Include="scripts\build.ci.ps1" />
<None Include="scripts\build.ps1" /> <None Include="scripts\build.ps1" />
<None Include="scripts\clean.ps1" /> <None Include="scripts\clean.ps1" />
<None Include="scripts\gen.ps1" />
<None Include="scripts\genccp.natstepfilter" /> <None Include="scripts\genccp.natstepfilter" />
<None Include="scripts\gencpp.refactor" /> <None Include="scripts\gencpp.refactor" />
<None Include="scripts\get_sources.ps1" /> <None Include="scripts\helpers\devshell.ps1" />
<None Include="scripts\helpers\target_arch.psm1" />
<None Include="scripts\package_release.ps1" />
<None Include="scripts\refactor.ps1" />
<None Include="test\gen\meson.build" /> <None Include="test\gen\meson.build" />
<None Include="test\meson.build" /> <None Include="test\meson.build" />
<None Include="test\Readme.md" /> <None Include="test\Readme.md" />
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>
<ClInclude Include="project\components\gen.data_structures.hpp" /> <ClInclude Include="project\auxillary\builder.hpp" />
<ClInclude Include="project\components\gen.ecode.hpp" /> <ClInclude Include="project\auxillary\editor.hpp" />
<ClInclude Include="project\components\gen.eoperator.hpp" /> <ClInclude Include="project\auxillary\scanner.hpp" />
<ClInclude Include="project\components\gen.especifier.hpp" /> <ClInclude Include="project\components\ast.hpp" />
<ClInclude Include="project\components\gen.header_end.hpp" /> <ClInclude Include="project\components\ast_types.hpp" />
<ClInclude Include="project\components\gen.header_start.hpp" /> <ClInclude Include="project\components\code_types.hpp" />
<ClInclude Include="project\components\gen.interface.hpp" /> <ClInclude Include="project\components\gen\ast_inlines.hpp" />
<ClInclude Include="project\components\gen.types.hpp" /> <ClInclude Include="project\components\gen\ecode.hpp" />
<ClInclude Include="project\dependencies\gen.basic_types.hpp" /> <ClInclude Include="project\components\gen\eoperator.hpp" />
<ClInclude Include="project\dependencies\gen.containers.hpp" /> <ClInclude Include="project\components\gen\especifier.hpp" />
<ClInclude Include="project\dependencies\gen.debug.hpp" /> <ClInclude Include="project\components\header_end.hpp" />
<ClInclude Include="project\dependencies\gen.file_handling.hpp" /> <ClInclude Include="project\components\header_start.hpp" />
<ClInclude Include="project\dependencies\gen.hashing.hpp" /> <ClInclude Include="project\components\inlines.hpp" />
<ClInclude Include="project\dependencies\gen.header_start.hpp" /> <ClInclude Include="project\components\interface.hpp" />
<ClInclude Include="project\dependencies\gen.macros.hpp" /> <ClInclude Include="project\components\types.hpp" />
<ClInclude Include="project\dependencies\gen.memory.hpp" /> <ClInclude Include="project\dependencies\basic_types.hpp" />
<ClInclude Include="project\dependencies\gen.parsing.hpp" /> <ClInclude Include="project\dependencies\containers.hpp" />
<ClInclude Include="project\dependencies\gen.printing.hpp" /> <ClInclude Include="project\dependencies\debug.hpp" />
<ClInclude Include="project\dependencies\gen.string.hpp" /> <ClInclude Include="project\dependencies\filesystem.hpp" />
<ClInclude Include="project\dependencies\gen.string_ops.hpp" /> <ClInclude Include="project\dependencies\hashing.hpp" />
<ClInclude Include="project\dependencies\gen.timing.hpp" /> <ClInclude Include="project\dependencies\header_start.hpp" />
<ClInclude Include="project\filesystem\gen.builder.hpp" /> <ClInclude Include="project\dependencies\macros.hpp" />
<ClInclude Include="project\filesystem\gen.editor.hpp" /> <ClInclude Include="project\dependencies\memory.hpp" />
<ClInclude Include="project\filesystem\gen.scanner.hpp" /> <ClInclude Include="project\dependencies\parsing.hpp" />
<ClInclude Include="project\dependencies\printing.hpp" />
<ClInclude Include="project\dependencies\strings.hpp" />
<ClInclude Include="project\dependencies\string_ops.hpp" />
<ClInclude Include="project\dependencies\timing.hpp" />
<ClInclude Include="project\gen.dep.hpp" /> <ClInclude Include="project\gen.dep.hpp" />
<ClInclude Include="project\gen.hpp" /> <ClInclude Include="project\gen.hpp" />
<ClInclude Include="project\helpers\gen.pop_ignores.inline.hpp" /> <ClInclude Include="project\helpers\helper.hpp" />
<ClInclude Include="project\helpers\gen.push_ignores.inline.hpp" /> <ClInclude Include="project\helpers\pop_ignores.inline.hpp" />
<ClInclude Include="project\helpers\gen.undef.macros.hpp" /> <ClInclude Include="project\helpers\push_ignores.inline.hpp" />
<ClInclude Include="singleheader\components\gen.header_start.hpp" /> <ClInclude Include="project\helpers\undef.macros.hpp" />
<ClInclude Include="singleheader\components\header_start.hpp" />
<ClInclude Include="test\CURSED_TYPEDEF.h" />
<ClInclude Include="test\DummyInclude.hpp" /> <ClInclude Include="test\DummyInclude.hpp" />
<ClInclude Include="test\Parsed\Buffer.Parsed.hpp" /> <ClInclude Include="test\Parsed\Buffer.Parsed.hpp" />
<ClInclude Include="test\Parsed\HashTable.Parsed.hpp" /> <ClInclude Include="test\Parsed\HashTable.Parsed.hpp" />
@ -157,57 +309,65 @@
<ClInclude Include="test\Parsed\Sanity.Parsed.hpp" /> <ClInclude Include="test\Parsed\Sanity.Parsed.hpp" />
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>
<ClCompile Include="project\components\gen.ast.cpp" /> <ClCompile Include="project\auxillary\builder.cpp" />
<ClCompile Include="project\components\gen.ast_case_macros.cpp" /> <ClCompile Include="project\auxillary\scanner.cpp" />
<ClCompile Include="project\components\gen.data.cpp" /> <ClCompile Include="project\bootstrap.cpp" />
<ClCompile Include="project\components\gen.etoktype.cpp" /> <ClCompile Include="project\components\ast.cpp" />
<ClCompile Include="project\components\gen.impl_start.cpp" /> <ClCompile Include="project\components\ast_case_macros.cpp" />
<ClCompile Include="project\components\gen.interface.cpp" /> <ClCompile Include="project\components\code_serialization.cpp" />
<ClCompile Include="project\components\gen.interface.parsing.cpp" /> <ClCompile Include="project\components\gen\etoktype.cpp" />
<ClCompile Include="project\components\gen.interface.upfront.cpp" /> <ClCompile Include="project\components\interface.cpp" />
<ClCompile Include="project\components\gen.untyped.cpp" /> <ClCompile Include="project\components\interface.parsing.cpp" />
<ClCompile Include="project\dependencies\gen.debug.cpp" /> <ClCompile Include="project\components\interface.untyped.cpp" />
<ClCompile Include="project\dependencies\gen.file_handling.cpp" /> <ClCompile Include="project\components\interface.upfront.cpp" />
<ClCompile Include="project\dependencies\gen.hashing.cpp" /> <ClCompile Include="project\components\lexer.cpp" />
<ClCompile Include="project\dependencies\gen.impl_start.cpp" /> <ClCompile Include="project\components\parser.cpp" />
<ClCompile Include="project\dependencies\gen.memory.cpp" /> <ClCompile Include="project\components\src_start.cpp" />
<ClCompile Include="project\dependencies\gen.parsing.cpp" /> <ClCompile Include="project\components\static_data.cpp" />
<ClCompile Include="project\dependencies\gen.printing.cpp" /> <ClCompile Include="project\dependencies\debug.cpp" />
<ClCompile Include="project\dependencies\gen.string.cpp" /> <ClCompile Include="project\dependencies\filesystem.cpp" />
<ClCompile Include="project\dependencies\gen.string_ops.cpp" /> <ClCompile Include="project\dependencies\hashing.cpp" />
<ClCompile Include="project\dependencies\gen.timing.cpp" /> <ClCompile Include="project\dependencies\memory.cpp" />
<ClCompile Include="project\gen.bootstrap.cpp" /> <ClCompile Include="project\dependencies\parsing.cpp" />
<ClCompile Include="project\dependencies\printing.cpp" />
<ClCompile Include="project\dependencies\src_start.cpp" />
<ClCompile Include="project\dependencies\strings.cpp" />
<ClCompile Include="project\dependencies\string_ops.cpp" />
<ClCompile Include="project\dependencies\timing.cpp" />
<ClCompile Include="project\Example.cpp" />
<ClCompile Include="project\gen.cpp" /> <ClCompile Include="project\gen.cpp" />
<ClCompile Include="project\gen.dep.cpp" /> <ClCompile Include="project\gen.dep.cpp" />
<ClCompile Include="singleheader\gen.singleheader.cpp" /> <ClCompile Include="singleheader\singleheader.cpp" />
<ClCompile Include="test\parsed\test.parsing.cpp" />
<ClCompile Include="test\parsing.cpp" /> <ClCompile Include="test\parsing.cpp" />
<ClCompile Include="test\sanity.cpp" /> <ClCompile Include="test\sanity.cpp" />
<ClCompile Include="test\SOA.cpp" /> <ClCompile Include="test\SOA.cpp" />
<ClCompile Include="test\test.cpp" /> <ClCompile Include="test\test.cpp" />
<ClCompile Include="test\test.parsing.cpp" /> <ClCompile Include="test\test.parsing.cpp" />
<ClCompile Include="test\test.singleheader_ast.cpp" />
<ClCompile Include="test\test.Upfront.cpp" /> <ClCompile Include="test\test.Upfront.cpp" />
<ClCompile Include="test\upfront.cpp" /> <ClCompile Include="test\upfront.cpp" />
<ClCompile Include="test\upfront\test.upfront.cpp" />
<ClCompile Include="test\validate_bootstrap.cpp" />
<ClCompile Include="test\validate_singleheader.cpp" />
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>
<Natvis Include=".vscode\gencpp.natvis" /> <Natvis Include=".vscode\gencpp.natvis" />
<Natvis Include="scripts\gencpp.natvis" /> <Natvis Include="scripts\gencpp.natvis" />
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>
<Content Include="project\components\AttributeTokens.csv" /> <Content Include="project\enums\AttributeTokens.csv" />
<Content Include="project\components\ECode.csv" /> <Content Include="project\enums\ECode.csv" />
<Content Include="project\components\EOperator.csv" /> <Content Include="project\enums\EOperator.csv" />
<Content Include="project\components\ESpecifier.csv" /> <Content Include="project\enums\ESpecifier.csv" />
<Content Include="project\components\ETokType.csv" /> <Content Include="project\enums\ETokType.csv" />
<Content Include="project\meson.build" />
<Content Include="scripts\.clang-format" /> <Content Include="scripts\.clang-format" />
<Content Include="scripts\bootstrap.ci.ps1" /> <Content Include="scripts\helpers\devshell.ps1" />
<Content Include="scripts\bootstrap.ps1" /> <Content Include="scripts\helpers\target_arch.psm1" />
<Content Include="scripts\msvc\build_msvc.ps1" />
<Content Include="scripts\msvc\devshell.ps1" />
<Content Include="scripts\refactor.ps1" /> <Content Include="scripts\refactor.ps1" />
<Content Include="scripts\singleheader.ci.ps1" /> </ItemGroup>
<Content Include="scripts\singleheader.ps1" /> <ItemGroup>
<Content Include="singleheader\meson.build" /> <Folder Include="singleheader\gen\" />
</ItemGroup> </ItemGroup>
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.targets" /> <Import Project="$(VCTargetsPath)\Microsoft.Cpp.targets" />
<ImportGroup Label="ExtensionTargets"> <ImportGroup Label="ExtensionTargets">

View File

@ -18,18 +18,9 @@
<ClCompile Include="project\gen.cpp"> <ClCompile Include="project\gen.cpp">
<Filter>Source Files</Filter> <Filter>Source Files</Filter>
</ClCompile> </ClCompile>
<ClCompile Include="test\gen\build\meson-private\sanitycheckc.c">
<Filter>Source Files</Filter>
</ClCompile>
<ClCompile Include="test\gen\build\meson-private\sanitycheckcpp.cc">
<Filter>Source Files</Filter>
</ClCompile>
<ClCompile Include="test\test.cpp"> <ClCompile Include="test\test.cpp">
<Filter>Source Files</Filter> <Filter>Source Files</Filter>
</ClCompile> </ClCompile>
<ClCompile Include="singleheader\gen\gen.singleheader.cpp">
<Filter>Source Files</Filter>
</ClCompile>
<ClCompile Include="test\test.Upfront.cpp"> <ClCompile Include="test\test.Upfront.cpp">
<Filter>Source Files</Filter> <Filter>Source Files</Filter>
</ClCompile> </ClCompile>
@ -51,28 +42,100 @@
<ClCompile Include="test\test.parsing.cpp"> <ClCompile Include="test\test.parsing.cpp">
<Filter>Source Files</Filter> <Filter>Source Files</Filter>
</ClCompile> </ClCompile>
<ClCompile Include="project\components\gen.ast.cpp"> <ClCompile Include="project\components\ast.cpp">
<Filter>Source Files</Filter> <Filter>Source Files</Filter>
</ClCompile> </ClCompile>
<ClCompile Include="project\components\gen.interface.parsing.cpp"> <ClCompile Include="project\components\ast_case_macros.cpp">
<Filter>Source Files</Filter> <Filter>Source Files</Filter>
</ClCompile> </ClCompile>
<ClCompile Include="project\components\gen.interface.upfront.cpp"> <ClCompile Include="project\components\interface.cpp">
<Filter>Source Files</Filter> <Filter>Source Files</Filter>
</ClCompile> </ClCompile>
<ClCompile Include="project\dependencies\gen.dep.cpp"> <ClCompile Include="project\components\interface.parsing.cpp">
<Filter>Source Files</Filter> <Filter>Source Files</Filter>
</ClCompile> </ClCompile>
<ClCompile Include="project\dependencies\gen.file_handling.cpp"> <ClCompile Include="project\components\interface.upfront.cpp">
<Filter>Source Files</Filter> <Filter>Source Files</Filter>
</ClCompile> </ClCompile>
<ClCompile Include="project\dependencies\gen.memory.cpp"> <ClCompile Include="project\components\src_start.cpp">
<Filter>Source Files</Filter> <Filter>Source Files</Filter>
</ClCompile> </ClCompile>
<ClCompile Include="project\dependencies\gen.parsing.cpp"> <ClCompile Include="project\components\static_data.cpp">
<Filter>Source Files</Filter> <Filter>Source Files</Filter>
</ClCompile> </ClCompile>
<ClCompile Include="project\dependencies\gen.printing.cpp"> <ClCompile Include="project\bootstrap.cpp">
<Filter>Source Files</Filter>
</ClCompile>
<ClCompile Include="project\dependencies\debug.cpp">
<Filter>Source Files</Filter>
</ClCompile>
<ClCompile Include="project\dependencies\filesystem.cpp">
<Filter>Source Files</Filter>
</ClCompile>
<ClCompile Include="project\dependencies\hashing.cpp">
<Filter>Source Files</Filter>
</ClCompile>
<ClCompile Include="project\dependencies\memory.cpp">
<Filter>Source Files</Filter>
</ClCompile>
<ClCompile Include="project\dependencies\parsing.cpp">
<Filter>Source Files</Filter>
</ClCompile>
<ClCompile Include="project\dependencies\printing.cpp">
<Filter>Source Files</Filter>
</ClCompile>
<ClCompile Include="project\dependencies\src_start.cpp">
<Filter>Source Files</Filter>
</ClCompile>
<ClCompile Include="project\dependencies\string_ops.cpp">
<Filter>Source Files</Filter>
</ClCompile>
<ClCompile Include="project\dependencies\strings.cpp">
<Filter>Source Files</Filter>
</ClCompile>
<ClCompile Include="project\dependencies\timing.cpp">
<Filter>Source Files</Filter>
</ClCompile>
<ClCompile Include="project\auxillary\builder.cpp">
<Filter>Source Files</Filter>
</ClCompile>
<ClCompile Include="project\auxillary\scanner.cpp">
<Filter>Source Files</Filter>
</ClCompile>
<ClCompile Include="singleheader\singleheader.cpp">
<Filter>Source Files</Filter>
</ClCompile>
<ClCompile Include="test\test.singleheader_ast.cpp">
<Filter>Source Files</Filter>
</ClCompile>
<ClCompile Include="test\parsed\test.parsing.cpp">
<Filter>Source Files</Filter>
</ClCompile>
<ClCompile Include="test\upfront\test.upfront.cpp">
<Filter>Source Files</Filter>
</ClCompile>
<ClCompile Include="test\validate_singleheader.cpp">
<Filter>Source Files</Filter>
</ClCompile>
<ClCompile Include="test\validate_bootstrap.cpp">
<Filter>Source Files</Filter>
</ClCompile>
<ClCompile Include="project\components\interface.untyped.cpp">
<Filter>Source Files</Filter>
</ClCompile>
<ClCompile Include="project\components\gen\etoktype.cpp">
<Filter>Source Files</Filter>
</ClCompile>
<ClCompile Include="project\Example.cpp">
<Filter>Source Files</Filter>
</ClCompile>
<ClCompile Include="project\components\parser.cpp">
<Filter>Source Files</Filter>
</ClCompile>
<ClCompile Include="project\components\lexer.cpp">
<Filter>Source Files</Filter>
</ClCompile>
<ClCompile Include="project\components\code_serialization.cpp">
<Filter>Source Files</Filter> <Filter>Source Files</Filter>
</ClCompile> </ClCompile>
</ItemGroup> </ItemGroup>
@ -80,27 +143,9 @@
<ClInclude Include="project\gen.hpp"> <ClInclude Include="project\gen.hpp">
<Filter>Header Files</Filter> <Filter>Header Files</Filter>
</ClInclude> </ClInclude>
<ClInclude Include="project\gen.undef.macros.hpp">
<Filter>Header Files</Filter>
</ClInclude>
<ClInclude Include="test\DummyInclude.hpp"> <ClInclude Include="test\DummyInclude.hpp">
<Filter>Header Files</Filter> <Filter>Header Files</Filter>
</ClInclude> </ClInclude>
<ClInclude Include="test\gen\array.Upfront.gen.hpp">
<Filter>Header Files</Filter>
</ClInclude>
<ClInclude Include="test\gen\buffer.Upfront.gen.hpp">
<Filter>Header Files</Filter>
</ClInclude>
<ClInclude Include="test\gen\hashtable.Upfront.gen.hpp">
<Filter>Header Files</Filter>
</ClInclude>
<ClInclude Include="test\gen\ring.Upfront.gen.hpp">
<Filter>Header Files</Filter>
</ClInclude>
<ClInclude Include="test\gen\sanity.Upfront.gen.hpp">
<Filter>Header Files</Filter>
</ClInclude>
<ClInclude Include="test\Parsed\Buffer.Parsed.hpp"> <ClInclude Include="test\Parsed\Buffer.Parsed.hpp">
<Filter>Header Files</Filter> <Filter>Header Files</Filter>
</ClInclude> </ClInclude>
@ -137,82 +182,112 @@
<ClInclude Include="project\gen.dep.hpp"> <ClInclude Include="project\gen.dep.hpp">
<Filter>Header Files</Filter> <Filter>Header Files</Filter>
</ClInclude> </ClInclude>
<ClInclude Include="project\gen.editor.hpp">
<Filter>Header Files</Filter>
</ClInclude>
<ClInclude Include="project\gen.pop_ignores.inline.hpp">
<Filter>Header Files</Filter>
</ClInclude>
<ClInclude Include="project\gen.push_ignores.inline.hpp">
<Filter>Header Files</Filter>
</ClInclude>
<ClInclude Include="project\gen.scanner.hpp">
<Filter>Header Files</Filter>
</ClInclude>
<ClInclude Include="test\parsing.hpp"> <ClInclude Include="test\parsing.hpp">
<Filter>Header Files</Filter> <Filter>Header Files</Filter>
</ClInclude> </ClInclude>
<ClInclude Include="test\upfront.hpp"> <ClInclude Include="test\upfront.hpp">
<Filter>Header Files</Filter> <Filter>Header Files</Filter>
</ClInclude> </ClInclude>
<ClInclude Include="project\components\gen.data_structures.hpp"> <ClInclude Include="project\components\ast.hpp">
<Filter>Header Files</Filter> <Filter>Header Files</Filter>
</ClInclude> </ClInclude>
<ClInclude Include="project\components\gen.interface.hpp"> <ClInclude Include="project\components\ast_types.hpp">
<Filter>Header Files</Filter> <Filter>Header Files</Filter>
</ClInclude> </ClInclude>
<ClInclude Include="project\components\gen.types.hpp"> <ClInclude Include="project\components\header_end.hpp">
<Filter>Header Files</Filter> <Filter>Header Files</Filter>
</ClInclude> </ClInclude>
<ClInclude Include="project\dependencies\gen.basic_types.hpp"> <ClInclude Include="project\components\header_start.hpp">
<Filter>Header Files</Filter> <Filter>Header Files</Filter>
</ClInclude> </ClInclude>
<ClInclude Include="project\dependencies\gen.containers.hpp"> <ClInclude Include="project\components\inlines.hpp">
<Filter>Header Files</Filter> <Filter>Header Files</Filter>
</ClInclude> </ClInclude>
<ClInclude Include="project\dependencies\gen.dep.hpp"> <ClInclude Include="project\components\interface.hpp">
<Filter>Header Files</Filter> <Filter>Header Files</Filter>
</ClInclude> </ClInclude>
<ClInclude Include="project\dependencies\gen.file_handling.hpp"> <ClInclude Include="project\components\types.hpp">
<Filter>Header Files</Filter> <Filter>Header Files</Filter>
</ClInclude> </ClInclude>
<ClInclude Include="project\dependencies\gen.header_start.hpp"> <ClInclude Include="project\helpers\helper.hpp">
<Filter>Header Files</Filter> <Filter>Header Files</Filter>
</ClInclude> </ClInclude>
<ClInclude Include="project\dependencies\gen.macros.hpp"> <ClInclude Include="project\helpers\pop_ignores.inline.hpp">
<Filter>Header Files</Filter> <Filter>Header Files</Filter>
</ClInclude> </ClInclude>
<ClInclude Include="project\dependencies\gen.memory.hpp"> <ClInclude Include="project\helpers\push_ignores.inline.hpp">
<Filter>Header Files</Filter> <Filter>Header Files</Filter>
</ClInclude> </ClInclude>
<ClInclude Include="project\dependencies\gen.parsing.hpp"> <ClInclude Include="project\helpers\undef.macros.hpp">
<Filter>Header Files</Filter> <Filter>Header Files</Filter>
</ClInclude> </ClInclude>
<ClInclude Include="project\dependencies\gen.printing.hpp"> <ClInclude Include="project\dependencies\basic_types.hpp">
<Filter>Header Files</Filter> <Filter>Header Files</Filter>
</ClInclude> </ClInclude>
<ClInclude Include="project\dependencies\gen.string.hpp"> <ClInclude Include="project\dependencies\containers.hpp">
<Filter>Header Files</Filter> <Filter>Header Files</Filter>
</ClInclude> </ClInclude>
<ClInclude Include="project\dependencies\gen.string_ops.hpp"> <ClInclude Include="project\dependencies\debug.hpp">
<Filter>Header Files</Filter> <Filter>Header Files</Filter>
</ClInclude> </ClInclude>
<ClInclude Include="project\filesystem\gen.builder.hpp"> <ClInclude Include="project\dependencies\filesystem.hpp">
<Filter>Header Files</Filter> <Filter>Header Files</Filter>
</ClInclude> </ClInclude>
<ClInclude Include="project\filesystem\gen.editor.hpp"> <ClInclude Include="project\dependencies\hashing.hpp">
<Filter>Header Files</Filter> <Filter>Header Files</Filter>
</ClInclude> </ClInclude>
<ClInclude Include="project\filesystem\gen.scanner.hpp"> <ClInclude Include="project\dependencies\header_start.hpp">
<Filter>Header Files</Filter> <Filter>Header Files</Filter>
</ClInclude> </ClInclude>
<ClInclude Include="project\helpers\gen.pop_ignores.inline.hpp"> <ClInclude Include="project\dependencies\macros.hpp">
<Filter>Header Files</Filter> <Filter>Header Files</Filter>
</ClInclude> </ClInclude>
<ClInclude Include="project\helpers\gen.push_ignores.inline.hpp"> <ClInclude Include="project\dependencies\memory.hpp">
<Filter>Header Files</Filter> <Filter>Header Files</Filter>
</ClInclude> </ClInclude>
<ClInclude Include="project\helpers\gen.undef.macros.hpp"> <ClInclude Include="project\dependencies\parsing.hpp">
<Filter>Header Files</Filter>
</ClInclude>
<ClInclude Include="project\dependencies\printing.hpp">
<Filter>Header Files</Filter>
</ClInclude>
<ClInclude Include="project\dependencies\string_ops.hpp">
<Filter>Header Files</Filter>
</ClInclude>
<ClInclude Include="project\dependencies\strings.hpp">
<Filter>Header Files</Filter>
</ClInclude>
<ClInclude Include="project\dependencies\timing.hpp">
<Filter>Header Files</Filter>
</ClInclude>
<ClInclude Include="project\auxillary\builder.hpp">
<Filter>Header Files</Filter>
</ClInclude>
<ClInclude Include="project\auxillary\editor.hpp">
<Filter>Header Files</Filter>
</ClInclude>
<ClInclude Include="project\auxillary\scanner.hpp">
<Filter>Header Files</Filter>
</ClInclude>
<ClInclude Include="singleheader\components\header_start.hpp">
<Filter>Header Files</Filter>
</ClInclude>
<ClInclude Include="project\components\gen\ast_inlines.hpp">
<Filter>Header Files</Filter>
</ClInclude>
<ClInclude Include="project\components\gen\ecode.hpp">
<Filter>Header Files</Filter>
</ClInclude>
<ClInclude Include="project\components\gen\eoperator.hpp">
<Filter>Header Files</Filter>
</ClInclude>
<ClInclude Include="project\components\gen\especifier.hpp">
<Filter>Header Files</Filter>
</ClInclude>
<ClInclude Include="test\CURSED_TYPEDEF.h">
<Filter>Header Files</Filter>
</ClInclude>
<ClInclude Include="project\components\code_types.hpp">
<Filter>Header Files</Filter> <Filter>Header Files</Filter>
</ClInclude> </ClInclude>
</ItemGroup> </ItemGroup>
@ -222,13 +297,21 @@
<None Include="scripts\build.ci.ps1" /> <None Include="scripts\build.ci.ps1" />
<None Include="scripts\build.ps1" /> <None Include="scripts\build.ps1" />
<None Include="scripts\clean.ps1" /> <None Include="scripts\clean.ps1" />
<None Include="scripts\get_sources.ps1" />
<None Include="test\gen\meson.build" /> <None Include="test\gen\meson.build" />
<None Include="test\meson.build" /> <None Include="test\meson.build" />
<None Include="test\Readme.md" /> <None Include="test\Readme.md" />
<None Include="scripts\gen.ps1" />
<None Include="scripts\genccp.natstepfilter" /> <None Include="scripts\genccp.natstepfilter" />
<None Include="scripts\gencpp.refactor" /> <None Include="scripts\gencpp.refactor" />
<None Include="project\enums\AttributeTokens.csv" />
<None Include="project\enums\ECode.csv" />
<None Include="project\enums\EOperator.csv" />
<None Include="project\enums\ESpecifier.csv" />
<None Include="project\enums\ETokType.csv" />
<None Include="scripts\helpers\devshell.ps1" />
<None Include="scripts\helpers\target_arch.psm1" />
<None Include="scripts\package_release.ps1" />
<None Include="scripts\refactor.ps1" />
<None Include="scripts\.clang-format" />
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>
<Natvis Include=".vscode\gencpp.natvis" /> <Natvis Include=".vscode\gencpp.natvis" />

View File

@ -3,14 +3,29 @@
<PropertyGroup> <PropertyGroup>
<ShowAllFiles>true</ShowAllFiles> <ShowAllFiles>true</ShowAllFiles>
</PropertyGroup> </PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|x64'"> <PropertyGroup Condition="'$(Configuration)|$(Platform)'=='bootstrap debug|x64'">
<LocalDebuggerAttach>false</LocalDebuggerAttach> <LocalDebuggerAttach>false</LocalDebuggerAttach>
<DebuggerFlavor>WindowsLocalDebugger</DebuggerFlavor> <DebuggerFlavor>WindowsLocalDebugger</DebuggerFlavor>
<LocalDebuggerCommand>C:\projects\gencpp\test\gen\build\gencpp.exe</LocalDebuggerCommand> <LocalDebuggerCommand>$(ProjectDir)project\build\bootstrap.exe</LocalDebuggerCommand>
</PropertyGroup> </PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|x64'"> <PropertyGroup Condition="'$(Configuration)|$(Platform)'=='test debug|x64'">
<LocalDebuggerAttach>false</LocalDebuggerAttach> <LocalDebuggerAttach>false</LocalDebuggerAttach>
<DebuggerFlavor>WindowsLocalDebugger</DebuggerFlavor> <DebuggerFlavor>WindowsLocalDebugger</DebuggerFlavor>
<LocalDebuggerCommand>C:\projects\gencpp\test\gen\build\gencpp.exe</LocalDebuggerCommand> <LocalDebuggerCommand>$(ProjectDir)project\build\bootstrap.exe</LocalDebuggerCommand>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='singleheader debug|x64'">
<LocalDebuggerAttach>false</LocalDebuggerAttach>
<DebuggerFlavor>WindowsLocalDebugger</DebuggerFlavor>
<LocalDebuggerCommand>$(ProjectDir)project\build\bootstrap.exe</LocalDebuggerCommand>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='singleheader release|x64'">
<LocalDebuggerAttach>false</LocalDebuggerAttach>
<DebuggerFlavor>WindowsLocalDebugger</DebuggerFlavor>
<LocalDebuggerCommand>$(ProjectDir)project\build\bootstrap.exe</LocalDebuggerCommand>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='bootstrap release|x64'">
<LocalDebuggerAttach>false</LocalDebuggerAttach>
<DebuggerFlavor>WindowsLocalDebugger</DebuggerFlavor>
<LocalDebuggerCommand>$(ProjectDir)project\build\bootstrap.exe</LocalDebuggerCommand>
</PropertyGroup> </PropertyGroup>
</Project> </Project>

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>`
@ -9,12 +8,9 @@ They contain includes for its various components: `components/<component_name>.<
Dependencies are bundled into `gen.dep.<hpp/cpp>`. Dependencies are bundled into `gen.dep.<hpp/cpp>`.
Just like the `gen.<hpp/cpp>` they include their components: `dependencies/<dependency_name>.<hpp/cpp>` Just like the `gen.<hpp/cpp>` they include their components: `dependencies/<dependency_name>.<hpp/cpp>`
The fle processors are in their own respective files. (Ex: `file_processors/<file_processor>.<hpp/cpp>` ) Code not making up the core library is located in `auxiliary/<auxiliary_name>.<hpp/cpp>`. These are optional extensions or tools for the library.
They directly include `depedencies/file_handling.<hpp/cpp>` as the core library does not include file processing by defualt.
**TODO : Right now the library is not finished structurally, 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,21 +24,28 @@ 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.
* `GEN_ROLL_OWN_DEPENDENCIES` : Optional override so that user may define the dependencies themselves. * `GEN_ROLL_OWN_DEPENDENCIES` : Optional override so that user may define the dependencies themselves.
* `GEN_DONT_ALLOW_INVALID_CODE` (Not implemented yet) : Will fail when an invalid code is constructed, parsed, or serialized.
By default the base library implementation strictly uses a C-like interface. This is to allow for the generation of a C-variant of the library using [gen_c_library](../gen_c_library/). However, the library was written in C++ and supports some of its features:
* `GEN_SUPPORT_CPP_REFERENCES` : Will enable support for reference interface on some definitions
* `GEN_SUPPORT_CPP_MEMBER_FEATURES` : Will enable support for definitions to have their interface as members.
*Note: A variant of the C++ library could be generated where those additonal support features are removed (see gen_c_library implementation for an idea of how)*
## On multi-threading ## On multi-threading
Currently unsupported. The following changes would have to be made: Currently unsupported. I want the library to be *stable* and *correct*, with the addition of exhausting all basic single-threaded optimizations before I consider multi-threading.
## 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`.
@ -54,4 +57,10 @@ Names or Content fields are interned strings and thus showed be cached using `ge
`def_operator` is the most sophisticated constructor as it has multiple permutations of definitions that could be created that are not trivial to determine if valid. `def_operator` is the most sophisticated constructor as it has multiple permutations of definitions that could be created that are not trivial to determine if valid.
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.
The parser is documented under `docs/Parsing.md` and `docs/Parser_Algo.md`.
## A note on compilation and runtime generation speed
The library is designed to be fast to compile and generate code at runtime as fast as resonable possible on a debug build.
Its recommended that your metaprogam be compiled using a single translation unit (unity build).

View File

@ -0,0 +1,59 @@
#ifdef GEN_INTELLISENSE_DIRECTIVES
# include "builder.hpp"
#endif
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 );
// log_fmt("$Builder - Opened file: %s\n", result.File.filename );
return result;
}
void Builder::pad_lines( s32 num )
{
append( & Buffer, "\n" );
}
void Builder::print( Code code )
{
String str = to_string(code);
// const ssize len = str.length();
// log_fmt( "%s - print: %.*s\n", File.filename, len > 80 ? 80 : len, str.Data );
append( & Buffer, str );
}
void Builder::print_fmt( char const* fmt, ... )
{
ssize res;
char buf[ GEN_PRINTF_MAXLEN ] = { 0 };
va_list va;
va_start( va, fmt );
res = str_fmt_va( buf, count_of( buf ) - 1, fmt, va ) - 1;
va_end( va );
// log_fmt( "$%s - print_fmt: %.*s\n", File.filename, res > 80 ? 80 : res, buf );
append( & Buffer, buf, res );
}
void Builder::write()
{
b32 result = file_write( & File, Buffer, length(Buffer) );
if ( result == false )
log_failure("gen::File::write - Failed to write to file: %s\n", file_name( & File ) );
log_fmt( "Generated: %s\n", File.filename );
file_close( & File );
free(& Buffer);
}

View File

@ -0,0 +1,20 @@
#ifdef GEN_INTELLISENSE_DIRECTIVES
# pragma once
# include "gen.hpp"
#endif
struct Builder
{
FileInfo File;
String Buffer;
static Builder open( char const* path );
void pad_lines( s32 num );
void print( Code );
void print_fmt( char const* fmt, ... );
void write();
};

View File

@ -0,0 +1,23 @@
#ifdef GEN_INTELLISENSE_DIRECTIVES
# pragma once
# include "../gen.hpp"
#endif
/*
Explicitly generates a resolved definition of a cpp template definition.
TODO(Ed): Needs implementing for the C-library variant.
TODO(Ed): We need a non <token> syntax subst implemtnation for Strings for this to work. It must subst keywords directly based on template parameter names.
This is only meant to be used on relatively trivial templates, where the type or numeric is mostly a 'duck' type.
It cannot parse complex template parameters.
The varadic args should correspond 1:1 with the type of objects the generator expects from the template's parameters.alignas.
*/
CodeOperator gen_operator_template( CodeTemplate template, ... );
CodeFn gen_func_template( CodeTemplate template, ... );
Code gen_class_struct_template( CodeTemplate template, ... );
Code gen_template( CodeTemplate template, ... );
Code gen_template( StrC template, StrC instantiation );

View File

@ -0,0 +1,5 @@
#ifdef GEN_INTELLISENSE_DIRECTIVES
# include "scanner.hpp"
#endif

View File

@ -0,0 +1,160 @@
#ifdef GEN_INTELLISENSE_DIRECTIVES
# pragma once
# include "gen.hpp"
#endif
// This is a simple file reader that reads the entire file into memory.
// It has an extra option to skip the first few lines for undesired includes.
// This is done so that includes can be kept in dependency and component files so that intellisense works.
inline
Code scan_file( char const* path )
{
FileInfo file;
FileError error = file_open_mode( & file, EFileMode_READ, path );
if ( error != EFileError_NONE )
{
GEN_FATAL( "scan_file: Could not open: %s", path );
}
ssize fsize = file_size( & file );
if ( fsize <= 0 )
{
GEN_FATAL("scan_file: %s is empty", path );
}
String str = string_make_reserve( GlobalAllocator, fsize );
file_read( & file, str, fsize );
get_header(str)->Length = fsize;
// Skip GEN_INTELLISENSE_DIRECTIVES preprocessor blocks
// Its designed so that the directive should be the first thing in the file.
// Anything that comes before it will also be omitted.
{
#define current (*scanner)
#define matched 0
#define move_fwd() do { ++ scanner; -- left; } while (0)
const StrC directive_start = txt( "ifdef" );
const StrC directive_end = txt( "endif" );
const StrC def_intellisense = txt("GEN_INTELLISENSE_DIRECTIVES" );
bool found_directive = false;
char const* scanner = str.Data;
s32 left = fsize;
while ( left )
{
// Processing directive.
if ( current == '#' )
{
move_fwd();
while ( left && char_is_space( current ) )
move_fwd();
if ( ! found_directive )
{
if ( left && str_compare( scanner, directive_start.Ptr, directive_start.Len ) == matched )
{
scanner += directive_start.Len;
left -= directive_start.Len;
while ( left && char_is_space( current ) )
move_fwd();
if ( left && str_compare( scanner, def_intellisense.Ptr, def_intellisense.Len ) == matched )
{
scanner += def_intellisense.Len;
left -= def_intellisense.Len;
found_directive = true;
}
}
// Skip to end of line
while ( left && current != '\r' && current != '\n' )
move_fwd();
move_fwd();
if ( left && current == '\n' )
move_fwd();
continue;
}
if ( left && str_compare( scanner, directive_end.Ptr, directive_end.Len ) == matched )
{
scanner += directive_end.Len;
left -= directive_end.Len;
// Skip to end of line
while ( left && current != '\r' && current != '\n' )
move_fwd();
move_fwd();
if ( left && current == '\n' )
move_fwd();
// sptr skip_size = fsize - left;
if ( (scanner + 2) >= ( str.Data + fsize ) )
{
mem_move( str, scanner, left );
get_header(str)->Length = left;
break;
}
mem_move( str, scanner, left );
get_header(str)->Length = left;
break;
}
}
move_fwd();
}
#undef move_fwd
#undef matched
#undef current
}
file_close( & file );
return untyped_str( str );
}
#if 0
struct CodeFile
{
using namespace Parser;
String FilePath;
TokArray Tokens;
Array<ParseFailure> ParseFailures;
Code CodeRoot;
};
namespace Parser
{
struct ParseFailure
{
String Reason;
Code Node;
};
}
CodeFile scan_file( char const* path )
{
using namespace Parser;
CodeFile
result = {};
result.FilePath = String::make( GlobalAllocator, path );
Code code = scan_file( path );
result.CodeRoot = code;
ParseContext context = parser_get_last_context();
result.Tokens = context.Tokens;
result.ParseFailures = context.Failures;
return result;
}
#endif

350
project/bootstrap.cpp Normal file
View File

@ -0,0 +1,350 @@
#define GEN_DEFINE_LIBRARY_CODE_CONSTANTS
#define GEN_ENFORCE_STRONG_CODE_TYPES
#define GEN_EXPOSE_BACKEND
#define GEN_SUPPORT_CPP_MEMBER_FEATURES 0
#define GEN_SUPPORT_CPP_REFERENCES 0
#include "gen.cpp"
#include "helpers/push_ignores.inline.hpp"
#include "helpers/helper.hpp"
GEN_NS_BEGIN
#include "dependencies/parsing.cpp"
GEN_NS_END
#include "auxillary/builder.hpp"
#include "auxillary/builder.cpp"
#include "auxillary/scanner.hpp"
using namespace gen;
constexpr char const* generation_notice =
"// This file was generated automatially by gencpp's bootstrap.cpp "
"(See: https://github.com/Ed94/gencpp)\n\n";
#include <cstdlib> // for system()
void format_file( char const* path )
{
String resolved_path = string_make(GlobalAllocator, to_str(path));
String style_arg = string_make(GlobalAllocator, txt("-style=file:"));
append( & style_arg, "../scripts/.clang-format ");
// Need to execute clang format on the generated file to get it to match the original.
#define clang_format "clang-format "
#define cf_format_inplace "-i "
#define cf_verbose "-verbose "
String command = string_make( GlobalAllocator, clang_format );
append( & command, cf_format_inplace );
append( & command, cf_verbose );
append( & command, style_arg );
append( & command, resolved_path );
log_fmt("\tRunning clang-format on file:\n");
system( command );
log_fmt("\tclang-format finished reformatting.\n");
#undef cf_cmd
#undef cf_format_inplace
#undef cf_style
#undef cf_verbse
}
Code dump_to_scratch_and_retireve( Code code )
{
Builder ecode_file_temp = Builder::open("gen/scratch.hpp");
ecode_file_temp.print(code);
ecode_file_temp.write();
format_file("gen/scratch.hpp");
Code result = scan_file( "gen/scratch.hpp" );
remove("gen/scratch.hpp");
return result;
}
int gen_main()
{
gen::init();
// PreprocessorDefines.append("GEN_NS");
Code push_ignores = scan_file( "helpers/push_ignores.inline.hpp" );
Code pop_ignores = scan_file( "helpers/pop_ignores.inline.hpp" );
// gen_dep.hpp
{
Code platform = scan_file( "dependencies/platform.hpp" );
Code macros = scan_file( "dependencies/macros.hpp" );
Code basic_types = scan_file( "dependencies/basic_types.hpp" );
Code debug = scan_file( "dependencies/debug.hpp" );
Code memory = scan_file( "dependencies/memory.hpp" );
Code string_ops = scan_file( "dependencies/string_ops.hpp" );
Code printing = scan_file( "dependencies/printing.hpp" );
Code containers = scan_file( "dependencies/containers.hpp" );
Code hashing = scan_file( "dependencies/hashing.hpp" );
Code strings = scan_file( "dependencies/strings.hpp" );
Code filesystem = scan_file( "dependencies/filesystem.hpp" );
Code timing = scan_file( "dependencies/timing.hpp" );
Builder
header = Builder::open("gen/gen.dep.hpp");
header.print_fmt( generation_notice );
header.print_fmt( "// This file is intended to be included within gen.hpp (There is no pragma diagnostic ignores)\n" );
header.print( platform );
header.print_fmt( "\nGEN_NS_BEGIN\n" );
header.print( macros );
header.print( basic_types );
header.print( debug );
header.print( memory );
header.print( string_ops );
header.print( printing );
header.print( containers );
header.print( hashing );
header.print( strings );
header.print( filesystem );
header.print( timing );
header.print_fmt( "\nGEN_NS_END\n" );
header.write();
}
// gen_dep.cpp
{
Code src_start = scan_file( "dependencies/src_start.cpp" );
Code debug = scan_file( "dependencies/debug.cpp" );
Code string_ops = scan_file( "dependencies/string_ops.cpp" );
Code printing = scan_file( "dependencies/printing.cpp" );
Code memory = scan_file( "dependencies/memory.cpp" );
Code hashing = scan_file( "dependencies/hashing.cpp" );
Code strings = scan_file( "dependencies/strings.cpp" );
Code filesystem = scan_file( "dependencies/filesystem.cpp" );
Code timing = scan_file( "dependencies/timing.cpp" );
Builder
src = Builder::open( "gen/gen.dep.cpp" );
src.print_fmt( generation_notice );
src.print_fmt( "// This file is intended to be included within gen.cpp (There is no pragma diagnostic ignores)\n" );
src.print( src_start );
src.print_fmt( "\nGEN_NS_BEGIN\n" );
src.print( debug );
src.print( string_ops );
src.print( printing );
src.print( hashing );
src.print( memory );
src.print( strings );
src.print( filesystem );
src.print( timing );
src.print_fmt( "\nGEN_NS_END\n" );
src.write();
}
CodeBody gen_component_header = def_global_body( args(
def_preprocess_cond( PreprocessCond_IfDef, txt("GEN_INTELLISENSE_DIRECTIVES") ),
pragma_once,
def_include(txt("components/types.hpp")),
preprocess_endif,
fmt_newline,
untyped_str( to_str(generation_notice) )
));
// gen.hpp
{
Code header_start = scan_file( "components/header_start.hpp" );
Code types = scan_file( "components/types.hpp" );
Code ast = scan_file( "components/ast.hpp" );
Code ast_types = scan_file( "components/ast_types.hpp" );
Code code_types = scan_file( "components/code_types.hpp" );
Code interface = scan_file( "components/interface.hpp" );
Code inlines = scan_file( "components/inlines.hpp" );
Code header_end = scan_file( "components/header_end.hpp" );
CodeBody ecode = gen_ecode ( "enums/ECode.csv" );
CodeBody eoperator = gen_eoperator ( "enums/EOperator.csv" );
CodeBody especifier = gen_especifier( "enums/ESpecifier.csv" );
CodeBody ast_inlines = gen_ast_inlines();
Builder
header = Builder::open( "gen/gen.hpp" );
header.print_fmt( generation_notice );
header.print_fmt( "#pragma once\n\n" );
header.print( push_ignores );
header.print( header_start );
header.print_fmt( "\nGEN_NS_BEGIN\n\n" );
header.print_fmt( "#pragma region Types\n" );
header.print( types );
header.print( fmt_newline);
header.print( dump_to_scratch_and_retireve(ecode) );
header.print( fmt_newline);
header.print( dump_to_scratch_and_retireve(eoperator) );
header.print( fmt_newline);
header.print( dump_to_scratch_and_retireve(especifier) );
header.print( fmt_newline);
header.print_fmt( "#pragma endregion Types\n\n" );
header.print_fmt( "#pragma region AST\n" );
header.print( ast );
header.print( code_types );
header.print( ast_types );
header.print_fmt( "\n#pragma endregion AST\n" );
header.print( interface );
header.print_fmt( "\n#pragma region Inlines\n" );
header.print( inlines );
header.print( fmt_newline );
header.print( dump_to_scratch_and_retireve(ast_inlines) );
header.print( fmt_newline );
header.print_fmt( "#pragma endregion Inlines\n" );
header.print( header_end );
header.print_fmt( "GEN_NS_END\n\n" );
header.print( pop_ignores );
header.write();
Builder
header_ecode = Builder::open( "components/gen/ecode.hpp" );
header_ecode.print( gen_component_header );
header_ecode.print( ecode );
header_ecode.write();
Builder
header_eoperator = Builder::open( "components/gen/eoperator.hpp" );
header_eoperator.print( gen_component_header );
header_eoperator.print( eoperator );
header_eoperator.write();
Builder
header_especifier = Builder::open( "components/gen/especifier.hpp" );
header_especifier.print( gen_component_header );
header_especifier.print( especifier );
header_especifier.write();
Builder
header_ast_inlines = Builder::open( "components/gen/ast_inlines.hpp" );
header_ast_inlines.print( gen_component_header );
header_ast_inlines.print( ast_inlines );
header_ast_inlines.write();
}
// gen.cpp
{
Code src_start = scan_file( "components/src_start.cpp" );
Code static_data = scan_file( "components/static_data.cpp" );
Code ast_case_macros = scan_file( "components/ast_case_macros.cpp" );
Code ast = scan_file( "components/ast.cpp" );
Code code_serialization = scan_file( "components/code_serialization.cpp" );
Code interface = scan_file( "components/interface.cpp" );
Code upfront = scan_file( "components/interface.upfront.cpp" );
Code lexer = scan_file( "components/lexer.cpp" );
Code parser = scan_file( "components/parser.cpp" );
Code parsing_interface = scan_file( "components/interface.parsing.cpp" );
Code untyped = scan_file( "components/interface.untyped.cpp" );
CodeBody etoktype = gen_etoktype( "enums/ETokType.csv", "enums/AttributeTokens.csv" );
CodeNS nspaced_etoktype = def_namespace( name(parser), def_namespace_body( args(etoktype)) );
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");
src.print( static_data );
src.print_fmt( "\n#pragma region AST\n\n" );
src.print( ast_case_macros );
src.print( ast );
src.print( code_serialization );
src.print_fmt( "\n#pragma endregion AST\n" );
src.print_fmt( "\n#pragma region Interface\n" );
src.print( interface );
src.print( upfront );
src.print_fmt( "\n#pragma region Parsing\n\n" );
src.print( dump_to_scratch_and_retireve(nspaced_etoktype) );
src.print( lexer );
src.print( parser );
src.print( parsing_interface );
src.print( untyped );
src.print_fmt( "\n#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();
Builder
src_etoktype = Builder::open( "components/gen/etoktype.cpp" );
src_etoktype.print( gen_component_header );
src_etoktype.print( nspaced_etoktype );
src_etoktype.write();
}
// gen_builder.hpp
{
Code builder = scan_file( "auxillary/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("gen.hpp") ));
header.print_fmt( "\nGEN_NS_BEGIN\n" );
header.print( builder );
header.print_fmt( "GEN_NS_END\n" );
header.write();
}
// gen_builder.cpp
{
Code builder = scan_file( "auxillary/builder.cpp" );
Builder
src = Builder::open( "gen/gen.builder.cpp" );
src.print_fmt( generation_notice );
src.print( def_include( txt("gen.builder.hpp") ) );
src.print_fmt( "\nGEN_NS_BEGIN\n" );
src.print( builder );
src.print_fmt( "\nGEN_NS_END\n" );
src.write();
}
// gen_scanner.hpp
{
Code parsing = scan_file( "dependencies/parsing.hpp" );
Code scanner = scan_file( "auxillary/scanner.hpp" );
Builder
header = Builder::open( "gen/gen.scanner.hpp" );
header.print_fmt( generation_notice );
header.print_fmt( "#pragma once\n\n" );
header.print( def_include( txt("gen.hpp") ) );
header.print_fmt( "\nGEN_NS_BEGIN\n" );
header.print( parsing );
header.print( scanner );
header.print_fmt( "\nGEN_NS_END\n" );
header.write();
}
// gen_scanner.cpp
{
Code parsing = scan_file( "dependencies/parsing.cpp" );
Code scanner = scan_file( "auxillary/scanner.cpp" );
Builder
src = Builder::open( "gen/gen.scanner.cpp" );
src.print_fmt( generation_notice );
src.print( def_include( txt("gen.scanner.hpp") ) );
src.print_fmt( "\nGEN_NS_BEGIN\n" );
src.print( parsing );
// src.print( scanner );
src.print_fmt( "GEN_NS_END\n" );
src.write();
}
gen::deinit();
return 0;
}

File diff suppressed because it is too large Load Diff

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

@ -0,0 +1,494 @@
#ifdef GEN_INTELLISENSE_DIRECTIVES
#pragma once
#include "types.hpp"
#include "gen/ecode.hpp"
#include "gen/eoperator.hpp"
#include "gen/especifier.hpp"
#endif
struct AST;
struct AST_Body;
struct AST_Attributes;
struct AST_Comment;
struct AST_Constructor;
// struct AST_BaseClass;
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;
#if GEN_EXECUTION_EXPRESSION_SUPPORT
struct AST_Expr;
struct AST_Expr_Assign;
struct AST_Expr_Alignof;
struct AST_Expr_Binary;
struct AST_Expr_CStyleCast;
struct AST_Expr_FunctionalCast;
struct AST_Expr_CppCast;
struct AST_Expr_ProcCall;
struct AST_Expr_Decltype;
struct AST_Expr_Comma; // TODO(Ed) : This is a binary op not sure if it needs its own AST...
struct AST_Expr_AMS; // Access Member Symbol
struct AST_Expr_Sizeof;
struct AST_Expr_Subscript;
struct AST_Expr_Ternary;
struct AST_Expr_UnaryPrefix;
struct AST_Expr_UnaryPostfix;
struct AST_Expr_Element;
struct AST_Stmt;
struct AST_Stmt_Break;
struct AST_Stmt_Case;
struct AST_Stmt_Continue;
struct AST_Stmt_Decl;
struct AST_Stmt_Do;
struct AST_Stmt_Expr; // TODO(Ed) : Is this distinction needed? (Should it be a flag instead?)
struct AST_Stmt_Else;
struct AST_Stmt_If;
struct AST_Stmt_For;
struct AST_Stmt_Goto;
struct AST_Stmt_Label;
struct AST_Stmt_Switch;
struct AST_Stmt_While;
#endif
struct AST_Struct;
struct AST_Template;
struct AST_Type;
struct AST_Typedef;
struct AST_Union;
struct AST_Using;
struct AST_Var;
#if GEN_COMPILER_C
#define Define_Code(Type) typedef AST_##Type* Code##Type
#else
#define Define_Code(Type) struct Code##Type
#endif
#if GEN_COMPILER_C
typedef AST* code;
#else
struct Code;
#endif
Define_Code(Body);
// These are to offer ease of use and optionally strong type safety for the AST.
Define_Code(Attributes);
// struct CodeBaseClass;
Define_Code(Comment);
Define_Code(Class);
Define_Code(Constructor);
Define_Code(Define);
Define_Code(Destructor);
Define_Code(Enum);
Define_Code(Exec);
Define_Code(Extern);
Define_Code(Include);
Define_Code(Friend);
Define_Code(Fn);
Define_Code(Module);
Define_Code(NS);
Define_Code(Operator);
Define_Code(OpCast);
Define_Code(Param);
Define_Code(PreprocessCond);
Define_Code(Pragma);
Define_Code(Specifiers);
#if GEN_EXECUTION_EXPRESSION_SUPPORT
Define_Code(Expr);
Define_Code(Expr_Assign);
Define_Code(Expr_Alignof);
Define_Code(Expr_Binary);
Define_Code(Expr_CStyleCast);
Define_Code(Expr_FunctionalCast);
Define_Code(Expr_CppCast);
Define_Code(Expr_Element);
Define_Code(Expr_ProcCall);
Define_Code(Expr_Decltype);
Define_Code(Expr_Comma);
Define_Code(Expr_AMS); // Access Member Symbol
Define_Code(Expr_Sizeof);
Define_Code(Expr_Subscript);
Define_Code(Expr_Ternary);
Define_Code(Expr_UnaryPrefix);
Define_Code(Expr_UnaryPostfix);
Define_Code(Stmt);
Define_Code(Stmt_Break);
Define_Code(Stmt_Case);
Define_Code(Stmt_Continue);
Define_Code(Stmt_Decl);
Define_Code(Stmt_Do);
Define_Code(Stmt_Expr);
Define_Code(Stmt_Else);
Define_Code(Stmt_If);
Define_Code(Stmt_For);
Define_Code(Stmt_Goto);
Define_Code(Stmt_Label);
Define_Code(Stmt_Switch);
Define_Code(Stmt_While);
#endif
Define_Code(Struct);
Define_Code(Template);
Define_Code(Type);
Define_Code(Typedef);
Define_Code(Union);
Define_Code(Using);
Define_Code(Var);
#undef Define_Code
namespace parser
{
struct Token;
}
#if ! GEN_COMPILER_C
template< class Type> forceinline Type tmpl_cast( Code self ) { return * rcast( Type*, & self ); }
#endif
char const* debug_str (Code code);
Code duplicate (Code code);
bool is_equal (Code code, Code other);
bool is_body (Code code);
bool is_valid (Code code);
void set_global(Code code);
String to_string (Code code);
#if ! GEN_COMPILER_C
/*
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
{
AST* ast;
# define Using_Code( Typename ) \
char const* debug_str() { return GEN_NS debug_str(* this); } \
Code duplicate() { return GEN_NS duplicate(* this); } \
bool is_equal( Code other ) { return GEN_NS is_equal(* this, other); } \
bool is_body() { return GEN_NS is_body(* this); } \
bool is_valid() { return GEN_NS is_valid(* this); } \
void set_global() { return GEN_NS set_global(* this); }
# define Using_CodeOps( Typename ) \
Typename& operator = ( AST* other ); \
Typename& operator = ( Code other ); \
bool operator ==( Code other ) { return (AST*)ast == other.ast; } \
bool operator !=( Code other ) { return (AST*)ast != other.ast; } \
operator bool();
#if GEN_SUPPORT_CPP_MEMBER_FEATURES
Using_Code( Code );
String to_string() { return GEN_NS to_string(* this); }
#endif
Using_CodeOps( Code );
AST* operator ->() { return ast; }
Code& operator ++();
// TODO(Ed) : Remove this overload.
auto& operator*()
{
local_persist thread_local
Code NullRef = { nullptr };
if ( ast == nullptr )
return NullRef;
return *this;
}
#ifdef GEN_ENFORCE_STRONG_CODE_TYPES
# define operator explicit operator
#endif
operator CodeBody() const;
operator CodeAttributes() const;
// operator CodeBaseClass() const;
operator CodeComment() const;
operator CodeClass() const;
operator CodeConstructor() const;
operator CodeDefine() const;
operator CodeDestructor() 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;
#undef operator
};
#endif
#pragma region Statics
// Used to identify ASTs that should always be duplicated. (Global constant ASTs)
extern Code Code_Global;
// Used to identify invalid generated code.
extern Code Code_Invalid;
#pragma endregion Statics
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 int const AST_POD_Size = 128;
void append ( AST* self, AST* other );
char const* debug_str ( AST* self );
AST* duplicate ( AST* self );
Code* entry ( AST* self, u32 idx );
bool has_entries ( AST* self );
bool is_body ( AST* self );
bool is_equal ( AST* self, AST* other );
String to_string ( AST* self );
void to_string ( AST* self, String* result );
char const* type_str ( AST* self );
bool validate_body( AST* self );
#if GEN_CPP_SUPPORT_REFERENCES
void append ( AST& self, AST& other ) { return append(& self, & other); }
bool is_body ( AST& self ) { return is_body(& self); }
bool is_equal ( AST& self, AST& other ) { return is_equal(& self, & other); }
char const* debug_str( AST& self ) { return debug_str( & self ); }
String to_string( AST& self ) { return to_string( & self ); }
char const* type_str ( AST& self ) { return type_str( & self ); }
#endif
constexpr static
int AST_ArrSpecs_Cap =
(
AST_POD_Size
- sizeof(AST*) * 3
- sizeof(parser::Token*)
- sizeof(AST*)
- sizeof(StringCached)
- sizeof(CodeT)
- sizeof(ModuleFlag)
- sizeof(int)
)
/ sizeof(int) - 1; // -1 for 4 extra bytes
/*
Simple AST POD with functionality to seralize into C++ syntax.
*/
struct AST
{
#if GEN_SUPPORT_CPP_MEMBER_FEATURES
# pragma region Member Functions
void append ( AST* other ) { GEN_NS append(this, other); }
char const* debug_str () { return GEN_NS debug_str(this); }
AST* duplicate () { return GEN_NS duplicate(this); }
Code* entry ( u32 idx ) { return GEN_NS entry(this, idx); }
bool has_entries() { return GEN_NS has_entries(this); }
bool is_equal ( AST* other ) { return GEN_NS is_equal(this, other); }
bool is_body() { return GEN_NS is_body(this); }
char const* type_str() { return GEN_NS type_str(this); }
bool validate_body() { return GEN_NS validate_body(this); }
String to_string() { return GEN_NS to_string(this); }
void to_string( String& result ) { return GEN_NS to_string(this, & result); }
# pragma endregion Member Functions
#endif
operator Code();
operator CodeBody();
operator CodeAttributes();
// operator CodeBaseClass();
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();
union {
struct
{
AST* InlineCmt; // Class, Constructor, Destructor, Enum, Friend, Functon, Operator, OpCast, Struct, Typedef, Using, Variable
AST* Attributes; // Class, Enum, Function, Struct, Typedef, Union, Using, Variable
AST* Specs; // Destructor, Function, Operator, Typename, Variable
union {
AST* InitializerList; // Constructor
AST* ParentType; // Class, Struct, ParentType->Next has a possible list of interfaces.
AST* ReturnType; // Function, Operator, Typename
AST* UnderlyingType; // Enum, Typedef
AST* ValueType; // Parameter, Variable
};
union {
AST* Macro; // Parameter
AST* BitfieldSize; // Variable (Class/Struct Data Member)
AST* Params; // Constructor, Function, Operator, Template, Typename
};
union {
AST* ArrExpr; // Typename
AST* Body; // Class, Constructor, Destructor, Enum, Friend, Function, Namespace, Struct, Union
AST* Declaration; // Friend, Template
AST* Value; // Parameter, Variable
};
union {
AST* NextVar; // Variable; Possible way to handle comma separated variables declarations. ( , NextVar->Specs NextVar->Name NextVar->ArrExpr = NextVar->Value )
AST* SuffixSpecs; // Only used with typenames, to store the function suffix if typename is function signature. ( May not be needed )
AST* PostNameMacro; // Only used with parameters for specifically UE_REQUIRES (Thanks Unreal)
};
};
StringCached Content; // Attributes, Comment, Execution, Include
struct {
SpecifierT ArrSpecs[AST_ArrSpecs_Cap]; // Specifiers
AST* NextSpecs; // Specifiers; If ArrSpecs is full, then NextSpecs is used.
};
};
union {
AST* Prev;
AST* Front;
AST* Last;
};
union {
AST* Next;
AST* Back;
};
parser::Token* Token; // Reference to starting token, only avaialble if it was derived from parsing.
AST* Parent;
StringCached Name;
CodeT Type;
// CodeFlag CodeFlags;
ModuleFlag ModuleFlags;
union {
b32 IsFunction; // Used by typedef to not serialize the name field.
b32 IsParamPack; // Used by typename to know if type should be considered a parameter pack.
OperatorT Op;
AccessSpec ParentAccess;
s32 NumEntries;
s32 VarConstructorInit; // Used by variables to know that initialization is using a constructor expression instead of an assignment expression.
b32 EnumUnderlyingMacro; // Used by enums incase the user wants to wrap underlying type specification in a macro
};
};
struct AST_POD
{
union {
struct
{
AST* InlineCmt; // Class, Constructor, Destructor, Enum, Friend, Functon, Operator, OpCast, Struct, Typedef, Using, Variable
AST* Attributes; // Class, Enum, Function, Struct, Typedef, Union, Using, Variable
AST* Specs; // Destructor, Function, Operator, Typename, Variable
union {
AST* InitializerList; // Constructor
AST* ParentType; // Class, Struct, ParentType->Next has a possible list of interfaces.
AST* ReturnType; // Function, Operator, Typename
AST* UnderlyingType; // Enum, Typedef
AST* ValueType; // Parameter, Variable
};
union {
AST* Macro; // Parameter
AST* BitfieldSize; // Variable (Class/Struct Data Member)
AST* Params; // Constructor, Function, Operator, Template, Typename
};
union {
AST* ArrExpr; // Typename
AST* Body; // Class, Constructr, Destructor, Enum, Friend, Function, Namespace, Struct, Union
AST* Declaration; // Friend, Template
AST* Value; // Parameter, Variable
};
union {
AST* NextVar; // Variable; Possible way to handle comma separated variables declarations. ( , NextVar->Specs NextVar->Name NextVar->ArrExpr = NextVar->Value )
AST* SuffixSpecs; // Only used with typenames, to store the function suffix if typename is function signature. ( May not be needed )
AST* PostNameMacro; // Only used with parameters for specifically UE_REQUIRES (Thanks Unreal)
};
};
StringCached Content; // Attributes, Comment, Execution, Include
struct {
SpecifierT ArrSpecs[AST_ArrSpecs_Cap]; // Specifiers
AST* NextSpecs; // Specifiers; If ArrSpecs is full, then NextSpecs is used.
};
};
union {
AST* Prev;
AST* Front;
AST* Last;
};
union {
AST* Next;
AST* Back;
};
parser::Token* Token; // Reference to starting token, only avaialble if it was derived from parsing.
AST* Parent;
StringCached Name;
CodeT Type;
CodeFlag CodeFlags;
ModuleFlag ModuleFlags;
union {
b32 IsFunction; // Used by typedef to not serialize the name field.
b32 IsParamPack; // Used by typename to know if type should be considered a parameter pack.
OperatorT Op;
AccessSpec ParentAccess;
s32 NumEntries;
s32 VarConstructorInit; // Used by variables to know that initialization is using a constructor expression instead of an assignment expression.
};
};
// 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 InvalidCode (* Code_Invalid.ast) // Uses an implicitly overloaded cast from the AST to the desired code type.

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: \

File diff suppressed because it is too large Load Diff

File diff suppressed because it is too large Load Diff

View File

@ -0,0 +1,968 @@
#ifdef GEN_INTELLISENSE_DIRECTIVES
#pragma once
#include "ast.hpp"
#endif
void append ( CodeBody body, Code other );
void append ( CodeBody body, CodeBody other );
String to_string ( CodeBody body );
void to_string ( CodeBody body, String* result );
void to_string_export ( CodeBody body, String* result );
Code begin( CodeBody body);
Code end ( CodeBody body );
void add_interface( CodeClass self, CodeType interface );
String to_string ( CodeClass self );
void to_string_def( CodeClass self, String* result );
void to_string_fwd( CodeClass self, String* result );
void append (CodeParam params, CodeParam param );
CodeParam get (CodeParam params, s32 idx);
bool has_entries(CodeParam params );
String to_string (CodeParam params );
void to_string (CodeParam params, String* result );
CodeParam begin(CodeParam params);
CodeParam end (CodeParam params);
bool append (CodeSpecifiers specifiers, SpecifierT spec);
s32 has (CodeSpecifiers specifiers, SpecifierT spec);
s32 remove (CodeSpecifiers specifiers, SpecifierT to_remove );
String to_string(CodeSpecifiers specifiers);
void to_string(CodeSpecifiers specifiers, String* result);
SpecifierT* begin(CodeSpecifiers specifiers );
SpecifierT* end (CodeSpecifiers specifiers);
void add_interface(CodeStruct self, CodeType interface);
String to_string (CodeStruct self);
void to_string_fwd(CodeStruct self, String* result);
void to_string_def(CodeStruct self, String* result);
String to_string(CodeAttributes attributes);
String to_string(CodeComment comment );
String to_string (CodeConstructor constructor);
void to_string_def(CodeConstructor constructor, String* result );
void to_string_fwd(CodeConstructor constructor, String* result );
String to_string(CodeDefine define);
void to_string(CodeDefine define, String* result);
String to_string (CodeDestructor destructor);
void to_string_def(CodeDestructor destructor, String* result );
void to_string_fwd(CodeDestructor destructor, String* result );
String to_string (CodeEnum self);
void to_string_def (CodeEnum self, String* result );
void to_string_fwd (CodeEnum self, String* result );
void to_string_class_def(CodeEnum self, String* result );
void to_string_class_fwd(CodeEnum self, String* result );
String to_string(CodeExec exec);
void to_string(CodeExtern self, String* result);
String to_string(CodeInclude include);
void to_string(CodeInclude include, String* result);
String to_string(CodeFriend self);
void to_string(CodeFriend self, String* result);
String to_string (CodeFn self);
void to_string_def(CodeFn self, String* result);
void to_string_fwd(CodeFn self, String* result);
#pragma region Code Types
// These structs are not used at all by the C vairant.
#if ! GEN_COMPILER_C
// stati_assert( GEN_COMPILER_C, "This should not be compiled with the C-library" );
struct CodeBody
{
#if GEN_SUPPORT_CPP_MEMBER_FEATURES
Using_Code( CodeBody );
void append( Code other ) { return GEN_NS append( *this, other ); }
void append( CodeBody body ) { return GEN_NS append(*this, body); }
bool has_entries() { return GEN_NS has_entries(rcast( AST*, ast )); }
String to_string() { return GEN_NS to_string(* this); }
void to_string( String& result ) { return GEN_NS to_string(* this, & result ); }
void to_string_export( String& result ) { return GEN_NS to_string_export(* this, & result); }
Code begin() { return GEN_NS begin(* this); }
Code end() { return GEN_NS end(* this); }
#endif
Using_CodeOps( CodeBody );
operator Code() { return * rcast( Code*, this ); }
AST_Body* operator->() { return ast; }
AST_Body* ast;
};
struct CodeClass
{
#if GEN_SUPPORT_CPP_MEMBER_FEATURES
Using_Code( CodeClass );
void add_interface( CodeType interface );
String to_string();
void to_string_def( String& result );
void to_string_fwd( String& result );
#endif
Using_CodeOps( CodeClass );
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
{
#if GEN_SUPPORT_CPP_MEMBER_FEATURES
Using_Code( CodeParam );
void append( CodeParam other );
CodeParam get( s32 idx );
bool has_entries();
String to_string();
void to_string( String& result );
#endif
Using_CodeOps( CodeParam );
AST_Param* operator->()
{
if ( ast == nullptr )
{
log_failure("Attempt to dereference a nullptr!");
return nullptr;
}
return ast;
}
operator Code() { return { (AST*)ast }; }
CodeParam operator*() { return * this; }
CodeParam& operator++();
AST_Param* ast;
};
struct CodeSpecifiers
{
#if GEN_SUPPORT_CPP_MEMBER_FEATURES
Using_Code( CodeSpecifiers );
bool append( SpecifierT spec ) { return GEN_NS append(* this, spec); }
s32 has( SpecifierT spec ) { return GEN_NS has(* this, spec); }
s32 remove( SpecifierT to_remove ) { return GEN_NS remove(* this, to_remove); }
String to_string() { return GEN_NS to_string(* this ); }
void to_string( String& result ) { return GEN_NS to_string(* this, & result); }
#endif
Using_CodeOps(CodeSpecifiers);
operator Code() { return { (AST*) ast }; }
AST_Specifiers* operator->()
{
if ( ast == nullptr )
{
log_failure("Attempt to dereference a nullptr!");
return nullptr;
}
return ast;
}
AST_Specifiers* ast;
};
struct CodeStruct
{
#if GEN_SUPPORT_CPP_MEMBER_FEATURES
Using_Code( CodeStruct );
void add_interface( CodeType interface );
String to_string();
void to_string_fwd( String& result );
void to_string_def( String& result );
#endif
Using_CodeOps( CodeStruct );
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;
};
struct CodeAttributes
{
#if GEN_SUPPORT_CPP_MEMBER_FEATURES
Using_Code(CodeAttributes);
String to_string();
#endif
Using_CodeOps(CodeAttributes);
operator Code();
AST_Attributes *operator->();
AST_Attributes *ast;
};
// Define_CodeType( BaseClass );
struct CodeComment
{
#if GEN_SUPPORT_CPP_MEMBER_FEATURES
Using_Code(CodeComment);
String to_string() { return GEN_NS to_string(* this); }
#endif
Using_CodeOps(CodeComment);
operator Code();
AST_Comment *operator->();
AST_Comment *ast;
};
struct CodeConstructor
{
#if GEN_SUPPORT_CPP_MEMBER_FEATURES
Using_Code( CodeConstructor );
String to_string() { return GEN_NS to_string(* this); }
void to_string_def( String& result ) { return GEN_NS to_string_def(* this, & result); }
void to_string_fwd( String& result ) { return GEN_NS to_string_fwd(* this, & result); }
#endif
Using_CodeOps(CodeConstructor);
operator Code();
AST_Constructor* operator->();
AST_Constructor* ast;
};
struct CodeDefine
{
#if GEN_SUPPORT_CPP_MEMBER_FEATURES
Using_Code( CodeDefine );
String to_string() { return GEN_NS to_string(* this); }
void to_string( String& result ) { return GEN_NS to_string(* this, & result); }
#endif
Using_CodeOps(CodeDefine);
operator Code();
AST_Define* operator->();
AST_Define* ast;
};
struct CodeDestructor
{
#if GEN_SUPPORT_CPP_MEMBER_FEATURES
Using_Code( CodeDestructor );
String to_string() { return GEN_NS to_string(* this); }
void to_string_def( String& result ) { return GEN_NS to_string_def(* this, & result); }
void to_string_fwd( String& result ) { return GEN_NS to_string_fwd(* this, & result); }
#endif
Using_CodeOps(CodeDestructor);
operator Code();
AST_Destructor* operator->();
AST_Destructor* ast;
};
struct CodeEnum
{
#if GEN_SUPPORT_CPP_MEMBER_FEATURES
Using_Code( CodeEnum );
String to_string() { return GEN_NS to_string(* this); }
void to_string_def( String& result ) { return GEN_NS to_string_def(* this); }
void to_string_fwd( String& result ) { return GEN_NS to_string_fwd(* this); }
void to_string_class_def( String& result ) { return GEN_NS to_string_class_def(* this); }
void to_string_class_fwd( String& result ) { return GEN_NS to_string_class_fwd(* this); }
#endif
Using_CodeOps(CodeEnum);
operator Code();
AST_Enum* operator->();
AST_Enum* ast;
};
struct CodeExec
{
#if GEN_SUPPORT_CPP_MEMBER_FEATURES
Using_Code(CodeExec);
String to_string() { return GEN_NS to_string(* this); }
#endif
Using_CodeOps(CodeExec);
operator Code();
AST_Exec *operator->();
AST_Exec *ast;
};
#if GEN_EXECUTION_EXPRESSION_SUPPORT
struct CodeExpr
{
Using_Code( CodeExpr );
void to_string( String& result );
AST* raw();
operator Code();
AST_Expr* operator->();
AST_Expr* ast;
};
struct CodeExpr_Assign
{
Using_Code( CodeExpr_Assign );
void to_string( String& result );
AST* raw();
operator Code();
AST_Expr_Assign* operator->();
AST_Expr_Assign* ast;
};
struct CodeExpr_Alignof
{
Using_Code( CodeExpr_Alignof );
void to_string( String& result );
AST* raw();
operator Code();
AST_Expr_Alignof* operator->();
AST_Expr_Alignof* ast;
};
struct CodeExpr_Binary
{
Using_Code( CodeExpr_Binary );
void to_string( String& result );
AST* raw();
operator Code();
AST_Expr_Binary* operator->();
AST_Expr_Binary* ast;
};
struct CodeExpr_CStyleCast
{
Using_Code( CodeExpr_CStyleCast );
void to_string( String& result );
AST* raw();
operator Code();
AST_Expr_CStyleCast* operator->();
AST_Expr_CStyleCast* ast;
};
struct CodeExpr_FunctionalCast
{
Using_Code( CodeExpr_FunctionalCast );
void to_string( String& result );
AST* raw();
operator Code();
AST_Expr_FunctionalCast* operator->();
AST_Expr_FunctionalCast* ast;
};
struct CodeExpr_CppCast
{
Using_Code( CodeExpr_CppCast );
void to_string( String& result );
AST* raw();
operator Code();
AST_Expr_CppCast* operator->();
AST_Expr_CppCast* ast;
};
struct CodeExpr_Element
{
Using_Code( CodeExpr_Element );
void to_string( String& result );
AST* raw();
operator Code();
AST_Expr_Element* operator->();
AST_Expr_Element* ast;
};
struct CodeExpr_ProcCall
{
Using_Code( CodeExpr_ProcCall );
void to_string( String& result );
AST* raw();
operator Code();
AST_Expr_ProcCall* operator->();
AST_Expr_ProcCall* ast;
};
struct CodeExpr_Decltype
{
Using_Code( CodeExpr_Decltype );
void to_string( String& result );
AST* raw();
operator Code();
AST_Expr_Decltype* operator->();
AST_Expr_Decltype* ast;
};
struct CodeExpr_Comma
{
Using_Code( CodeExpr_Comma );
void to_string( String& result );
AST* raw();
operator Code();
AST_Expr_Comma* operator->();
AST_Expr_Comma* ast;
};
struct CodeExpr_AMS
{
Using_Code( CodeExpr_AMS );
void to_string( String& result );
AST* raw();
operator Code();
AST_Expr_AMS* operator->();
AST_Expr_AMS* ast;
};
struct CodeExpr_Sizeof
{
Using_Code( CodeExpr_Sizeof );
void to_string( String& result );
AST* raw();
operator Code();
AST_Expr_Sizeof* operator->();
AST_Expr_Sizeof* ast;
};
struct CodeExpr_Subscript
{
Using_Code( CodeExpr_Subscript );
void to_string( String& result );
AST* raw();
operator Code();
AST_Expr_Subscript* operator->();
AST_Expr_Subscript* ast;
};
struct CodeExpr_Ternary
{
Using_Code( CodeExpr_Ternary );
void to_string( String& result );
AST* raw();
operator Code();
AST_Expr_Ternary* operator->();
AST_Expr_Ternary* ast;
};
struct CodeExpr_UnaryPrefix
{
Using_Code( CodeExpr_UnaryPrefix );
void to_string( String& result );
AST* raw();
operator Code();
AST_Expr_UnaryPrefix* operator->();
AST_Expr_UnaryPrefix* ast;
};
struct CodeExpr_UnaryPostfix
{
Using_Code( CodeExpr_UnaryPostfix );
void to_string( String& result );
AST* raw();
operator Code();
AST_Expr_UnaryPostfix* operator->();
AST_Expr_UnaryPostfix* ast;
};
#endif
struct CodeExtern
{
#if GEN_SUPPORT_CPP_MEMBER_FEATURES
Using_Code( CodeExtern );
void to_string( String& result ) { return GEN_NS to_string(* this, & result); }
#endif
Using_CodeOps(CodeExtern);
operator Code();
AST_Extern* operator->();
AST_Extern* ast;
};
struct CodeInclude
{
#if GEN_SUPPORT_CPP_MEMBER_FEATURES
Using_Code( CodeInclude );
String to_string() { return GEN_NS to_string(* this); }
void to_string( String& result ) { return GEN_NS to_string(* this, & result); }
#endif
Using_CodeOps(CodeInclude);
operator Code();
AST_Include* operator->();
AST_Include* ast;
};
struct CodeFriend
{
#if GEN_SUPPORT_CPP_MEMBER_FEATURES
Using_Code( CodeFriend );
String to_string() { return GEN_NS to_string(* this); }
void to_string( String& result ) { return GEN_NS to_string(* this, & result); }
#endif
Using_CodeOps(CodeFriend);
operator Code();
AST_Friend* operator->();
AST_Friend* ast;
};
struct CodeFn
{
#if GEN_SUPPORT_CPP_MEMBER_FEATURES
Using_Code( CodeFn );
String to_string() { return GEN_NS to_string(* this); }
void to_string_def( String& result ) { return GEN_NS to_string_def(* this); }
void to_string_fwd( String& result ) { return GEN_NS to_string_fwd(* this); }
#endif
Using_CodeOps(CodeFn);
operator Code();
AST_Fn* operator->();
AST_Fn* ast;
};
struct CodeModule
{
#if GEN_SUPPORT_CPP_MEMBER_FEATURES || 1
Using_Code( CodeModule );
String to_string();
void to_string( String& result );
#endif
Using_CodeOps(CodeModule);
operator Code();
AST_Module* operator->();
AST_Module* ast;
};
struct CodeNS
{
#if GEN_SUPPORT_CPP_MEMBER_FEATURES || 1
Using_Code( CodeNS );
String to_string();
void to_string( String& result );
#endif
Using_CodeOps(CodeNS);
operator Code();
AST_NS* operator->();
AST_NS* ast;
};
struct CodeOperator
{
#if GEN_SUPPORT_CPP_MEMBER_FEATURES || 1
Using_Code( CodeOperator );
String to_string();
void to_string_def( String& result );
void to_string_fwd( String& result );
#endif
Using_CodeOps(CodeOperator);
operator Code();
AST_Operator* operator->();
AST_Operator* ast;
};
struct CodeOpCast
{
#if GEN_SUPPORT_CPP_MEMBER_FEATURES || 1
Using_Code( CodeOpCast );
String to_string();
void to_string_def( String& result );
void to_string_fwd( String& result );
#endif
Using_CodeOps(CodeOpCast);
operator Code();
AST_OpCast* operator->();
AST_OpCast* ast;
};
struct CodePragma
{
#if GEN_SUPPORT_CPP_MEMBER_FEATURES || 1
Using_Code( CodePragma );
String to_string();
void to_string( String& result );
#endif
Using_CodeOps( CodePragma );
operator Code();
AST_Pragma* operator->();
AST_Pragma* ast;
};
struct CodePreprocessCond
{
#if GEN_SUPPORT_CPP_MEMBER_FEATURES || 1
Using_Code( CodePreprocessCond );
String to_string();
void to_string_if( String& result );
void to_string_ifdef( String& result );
void to_string_ifndef( String& result );
void to_string_elif( String& result );
void to_string_else( String& result );
void to_string_endif( String& result );
#endif
Using_CodeOps( CodePreprocessCond );
operator Code();
AST_PreprocessCond* operator->();
AST_PreprocessCond* ast;
};
#if GEN_EXECUTION_EXPRESSION_SUPPORT
struct CodeStmt
{
Using_Code( CodeStmt );
String to_string();
void to_string( String& result );
AST* raw();
operator Code();
AST_Stmt* operator->();
AST_Stmt* ast;
};
struct CodeStmt_Break
{
Using_Code( CodeStmt_Break );
String to_string();
void to_string( String& result );
AST* raw();
operator Code();
AST_Stmt_Break* operator->();
AST_Stmt_Break* ast;
};
struct CodeStmt_Case
{
Using_Code( CodeStmt_Case );
String to_string();
void to_string( String& result );
AST* raw();
operator Code();
AST_Stmt_Case* operator->();
AST_Stmt_Case* ast;
};
struct CodeStmt_Continue
{
Using_Code( CodeStmt_Continue );
String to_string();
void to_string( String& result );
AST* raw();
operator Code();
AST_Stmt_Continue* operator->();
AST_Stmt_Continue* ast;
};
struct CodeStmt_Decl
{
Using_Code( CodeStmt_Decl );
String to_string();
void to_string( String& result );
AST* raw();
operator Code();
AST_Stmt_Decl* operator->();
AST_Stmt_Decl* ast;
};
struct CodeStmt_Do
{
Using_Code( CodeStmt_Do );
String to_string();
void to_string( String& result );
AST* raw();
operator Code();
AST_Stmt_Do* operator->();
AST_Stmt_Do* ast;
};
struct CodeStmt_Expr
{
Using_Code( CodeStmt_Expr );
String to_string();
void to_string( String& result );
AST* raw();
operator Code();
AST_Stmt_Expr* operator->();
AST_Stmt_Expr* ast;
};
struct CodeStmt_Else
{
Using_Code( CodeStmt_Else );
String to_string();
void to_string( String& result );
AST* raw();
operator Code();
AST_Stmt_Else* operator->();
AST_Stmt_Else* ast;
};
struct CodeStmt_If
{
Using_Code( CodeStmt_If );
String to_string();
void to_string( String& result );
AST* raw();
operator Code();
AST_Stmt_If* operator->();
AST_Stmt_If* ast;
};
struct CodeStmt_For
{
Using_Code( CodeStmt_For );
String to_string();
void to_string( String& result );
AST* raw();
operator Code();
AST_Stmt_For* operator->();
AST_Stmt_For* ast;
};
struct CodeStmt_Goto
{
Using_Code( CodeStmt_Goto );
String to_string();
void to_string( String& result );
AST* raw();
operator Code();
AST_Stmt_Goto* operator->();
AST_Stmt_Goto* ast;
};
struct CodeStmt_Label
{
Using_Code( CodeStmt_Label );
String to_string();
void to_string( String& result );
AST* raw();
operator Code();
AST_Stmt_Label* operator->();
AST_Stmt_Label* ast;
};
struct CodeStmt_Switch
{
Using_Code( CodeStmt_Switch );
String to_string();
void to_string( String& result );
AST* raw();
operator Code();
AST_Stmt_Switch* operator->();
AST_Stmt_Switch* ast;
};
struct CodeStmt_While
{
Using_Code( CodeStmt_While );
String to_string();
void to_string( String& result );
AST* raw();
operator Code();
AST_Stmt_While* operator->();
AST_Stmt_While* ast;
};
#endif
struct CodeTemplate
{
#if GEN_SUPPORT_CPP_MEMBER_FEATURES || 1
Using_Code( CodeTemplate );
String to_string();
void to_string( String& result );
#endif
Using_CodeOps( CodeTemplate );
operator Code();
AST_Template* operator->();
AST_Template* ast;
};
struct CodeType
{
#if GEN_SUPPORT_CPP_MEMBER_FEATURES || 1
Using_Code( CodeType );
String to_string();
void to_string( String& result );
#endif
Using_CodeOps( CodeType );
operator Code();
AST_Type* operator->();
AST_Type* ast;
};
struct CodeTypedef
{
#if GEN_SUPPORT_CPP_MEMBER_FEATURES || 1
Using_Code( CodeTypedef );
String to_string();
void to_string( String& result );
#endif
Using_CodeOps( CodeTypedef );
operator Code();
AST_Typedef* operator->();
AST_Typedef* ast;
};
struct CodeUnion
{
#if GEN_SUPPORT_CPP_MEMBER_FEATURES || 1
Using_Code( CodeUnion );
String to_string();
void to_string( String& result );
#endif
Using_CodeOps(CodeUnion);
operator Code();
AST_Union* operator->();
AST_Union* ast;
};
struct CodeUsing
{
#if GEN_SUPPORT_CPP_MEMBER_FEATURES || 1
Using_Code( CodeUsing );
String to_string();
void to_string( String& result );
void to_string_ns( String& result );
#endif
Using_CodeOps(CodeUsing);
operator Code();
AST_Using* operator->();
AST_Using* ast;
};
struct CodeVar
{
#if GEN_SUPPORT_CPP_MEMBER_FEATURES || 1
Using_Code( CodeVar );
String to_string();
void to_string( String& result );
#endif
Using_CodeOps(CodeVar);
operator Code();
AST_Var* operator->();
AST_Var* ast;
};
#undef Define_CodeType
#undef Using_Code
#undef Using_CodeOps
#if GEN_SUPPORT_CPP_REFERENCES
void to_string_export( CodeBody body, String& result ) { return to_string_export(body, & result); };
#endif
#endif //if ! GEN_COMPILER_C
#pragma endregion Code Types

File diff suppressed because it is too large Load Diff

View File

@ -1,78 +0,0 @@
// This is the non-bootstraped version of the ECode. This will be obsolete once bootstrap is stress tested.
namespace ECode
{
# define Define_Types \
Entry( Invalid ) \
Entry( Untyped ) \
Entry( Comment ) \
Entry( Access_Private ) \
Entry( Access_Protected ) \
Entry( Access_Public ) \
Entry( PlatformAttributes ) \
Entry( Class ) \
Entry( Class_Fwd ) \
Entry( Class_Body ) \
Entry( Enum ) \
Entry( Enum_Fwd ) \
Entry( Enum_Body ) \
Entry( Enum_Class ) \
Entry( Enum_Class_Fwd ) \
Entry( Execution ) \
Entry( Export_Body ) \
Entry( Extern_Linkage ) \
Entry( Extern_Linkage_Body ) \
Entry( Friend ) \
Entry( Function ) \
Entry( Function_Fwd ) \
Entry( Function_Body ) \
Entry( Global_Body ) \
Entry( Module ) \
Entry( Namespace ) \
Entry( Namespace_Body ) \
Entry( Operator ) \
Entry( Operator_Fwd ) \
Entry( Operator_Member ) \
Entry( Operator_Member_Fwd ) \
Entry( Operator_Cast ) \
Entry( Operator_Cast_Fwd ) \
Entry( Parameters ) \
Entry( Preprocessor_Include ) \
Entry( Specifiers ) \
Entry( Struct ) \
Entry( Struct_Fwd ) \
Entry( Struct_Body ) \
Entry( Template ) \
Entry( Typedef ) \
Entry( Typename ) \
Entry( Union ) \
Entry( Union_Body) \
Entry( Using ) \
Entry( Using_Namespace ) \
Entry( Variable )
enum Type : u32
{
# define Entry( Type ) Type,
Define_Types
# undef Entry
Num_Types
};
inline
StrC to_str( Type type )
{
static
StrC lookup[Num_Types] = {
# define Entry( Type ) { sizeof(stringize(Type)), stringize(Type) },
Define_Types
# undef Entry
};
return lookup[ type ];
}
# undef Define_Types
}
using CodeT = ECode::Type;

View File

@ -1,75 +0,0 @@
// This is the non-bootstraped version of the EOperator. This will be obsolete once bootstrap is stress tested.
namespace EOperator
{
# define Define_Operators \
Entry( Invalid, INVALID ) \
Entry( Assign, = ) \
Entry( Assign_Add, += ) \
Entry( Assign_Subtract, -= ) \
Entry( Assign_Multiply, *= ) \
Entry( Assign_Divide, /= ) \
Entry( Assign_Modulo, %= ) \
Entry( Assign_BAnd, &= ) \
Entry( Assign_BOr, |= ) \
Entry( Assign_BXOr, ^= ) \
Entry( Assign_LShift, <<= ) \
Entry( Assign_RShift, >>= ) \
Entry( Increment, ++ ) \
Entry( Decrement, -- ) \
Entry( Unary_Plus, + ) \
Entry( Unary_Minus, - ) \
Entry( UnaryNot, ! ) \
Entry( Add, + ) \
Entry( Subtract, - ) \
Entry( Multiply, * ) \
Entry( Divide, / ) \
Entry( Modulo, % ) \
Entry( BNot, ~ ) \
Entry( BAnd, & ) \
Entry( BOr, | ) \
Entry( BXOr, ^ ) \
Entry( LShift, << ) \
Entry( RShift, >> ) \
Entry( LAnd, && ) \
Entry( LOr, || ) \
Entry( LEqual, == ) \
Entry( LNot, != ) \
Entry( Lesser, < ) \
Entry( Greater, > ) \
Entry( LesserEqual, <= ) \
Entry( GreaterEqual, >= ) \
Entry( Subscript, [] ) \
Entry( Indirection, * ) \
Entry( AddressOf, & ) \
Entry( MemberOfPointer, -> ) \
Entry( PtrToMemOfPtr, ->* ) \
Entry( FunctionCall, () )
enum Type : u32
{
# define Entry( Type_, Token_ ) Type_,
Define_Operators
# undef Entry
Comma,
Num_Ops,
};
inline
char const* to_str( Type op )
{
local_persist
char const* lookup[ Num_Ops ] = {
# define Entry( Type_, Token_ ) stringize(Token_),
Define_Operators
# undef Entry
","
};
return lookup[ op ];
}
# undef Define_Operators
}
using OperatorT = EOperator::Type;

View File

@ -1,104 +0,0 @@
// This is the non-bootstraped version of the ESpecifier. This will be obsolete once bootstrap is stress tested.
namespace ESpecifier
{
/*
Note: The following are handled separately:
attributes
alignas
*/
# define Define_Specifiers \
Entry( Invalid, INVALID ) \
Entry( Consteval, consteval ) \
Entry( Constexpr, constexpr ) \
Entry( Constinit, constinit ) \
Entry( Explicit, explicit ) \
Entry( External_Linkage, extern ) \
Entry( Global, global ) \
Entry( Inline, inline ) \
Entry( Internal_Linkage, internal ) \
Entry( Local_Persist, local_persist ) \
Entry( Mutable, mutable ) \
Entry( Ptr, * ) \
Entry( Ref, & ) \
Entry( Register, register ) \
Entry( RValue, && ) \
Entry( Static, static ) \
Entry( Thread_Local, thread_local ) \
Entry( Volatile, volatile ) \
Entry( Virtual, virtual ) \
Entry( Const, const ) \
Entry( Final, final ) \
Entry( Override, override )
enum Type : u32
{
# define Entry( Specifier, Code ) Specifier,
Define_Specifiers
# undef Entry
NumSpecifiers,
};
inline
bool is_trailing( Type specifier )
{
return specifier > Virtual;
}
// Specifier to string
inline
StrC to_str( Type specifier )
{
local_persist
StrC lookup[ NumSpecifiers ] = {
# pragma push_macro( "global" )
# pragma push_macro( "internal" )
# pragma push_macro( "local_persist" )
# undef global
# undef internal
# undef local_persist
# define Entry( Spec_, Code_ ) { sizeof(stringize(Code_)), stringize(Code_) },
Define_Specifiers
# undef Entry
# pragma pop_macro( "global" )
# pragma pop_macro( "internal" )
# pragma pop_macro( "local_persist" )
};
return lookup[ specifier ];
}
inline
Type to_type( StrC str )
{
local_persist
u32 keymap[ NumSpecifiers ];
do_once_start
for ( u32 index = 0; index < NumSpecifiers; index++ )
{
StrC enum_str = to_str( (Type)index );
// We subtract 1 to remove the null terminator
// This is because the tokens lexed are not null terminated.
keymap[index] = crc32( enum_str.Ptr, enum_str.Len - 1);
}
do_once_end
u32 hash = crc32( str.Ptr, str.Len );
for ( u32 index = 0; index < NumSpecifiers; index++ )
{
if ( keymap[index] == hash )
return (Type)index;
}
return Invalid;
}
# undef Define_Specifiers
}
using SpecifierT = ESpecifier::Type;

View File

@ -1,151 +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( Preproces_Include, "include" ) \
Entry( Preprocess_If, "#if") \
Entry( Preprocess_Elif, "#elif") \
Entry( Preprocess_Else, "#else") \
Entry( Preprocess_EndIf, "#endif") \
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_Override, "override" ) \
Entry( Spec_Static, "static" ) \
Entry( Spec_ThreadLocal, "thread_local" ) \
Entry( Spec_Volatile, "volatile") \
Entry( Star, "*" ) \
Entry( Statement_End, ";" ) \
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( 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

File diff suppressed because it is too large Load Diff

View File

@ -0,0 +1,144 @@
#ifdef GEN_INTELLISENSE_DIRECTIVES
#pragma once
#include "components/types.hpp"
#endif
// This file was generated automatially by gencpp's bootstrap.cpp (See: https://github.com/Ed94/gencpp)
namespace ECode
{
enum Type : u32
{
Invalid,
Untyped,
NewLine,
Comment,
Access_Private,
Access_Protected,
Access_Public,
PlatformAttributes,
Class,
Class_Fwd,
Class_Body,
Constructor,
Constructor_Fwd,
Destructor,
Destructor_Fwd,
Enum,
Enum_Fwd,
Enum_Body,
Enum_Class,
Enum_Class_Fwd,
Execution,
Export_Body,
Extern_Linkage,
Extern_Linkage_Body,
Friend,
Function,
Function_Fwd,
Function_Body,
Global_Body,
Module,
Namespace,
Namespace_Body,
Operator,
Operator_Fwd,
Operator_Member,
Operator_Member_Fwd,
Operator_Cast,
Operator_Cast_Fwd,
Parameters,
Preprocess_Define,
Preprocess_Include,
Preprocess_If,
Preprocess_IfDef,
Preprocess_IfNotDef,
Preprocess_ElIf,
Preprocess_Else,
Preprocess_EndIf,
Preprocess_Pragma,
Specifiers,
Struct,
Struct_Fwd,
Struct_Body,
Template,
Typedef,
Typename,
Union,
Union_Body,
Using,
Using_Namespace,
Variable,
NumTypes
};
inline StrC to_str( Type type )
{
local_persist StrC lookup[] {
{ sizeof( "Invalid" ), "Invalid" },
{ sizeof( "Untyped" ), "Untyped" },
{ sizeof( "NewLine" ), "NewLine" },
{ sizeof( "Comment" ), "Comment" },
{ sizeof( "Access_Private" ), "Access_Private" },
{ sizeof( "Access_Protected" ), "Access_Protected" },
{ sizeof( "Access_Public" ), "Access_Public" },
{ sizeof( "PlatformAttributes" ), "PlatformAttributes" },
{ sizeof( "Class" ), "Class" },
{ sizeof( "Class_Fwd" ), "Class_Fwd" },
{ sizeof( "Class_Body" ), "Class_Body" },
{ sizeof( "Constructor" ), "Constructor" },
{ sizeof( "Constructor_Fwd" ), "Constructor_Fwd" },
{ sizeof( "Destructor" ), "Destructor" },
{ sizeof( "Destructor_Fwd" ), "Destructor_Fwd" },
{ sizeof( "Enum" ), "Enum" },
{ sizeof( "Enum_Fwd" ), "Enum_Fwd" },
{ sizeof( "Enum_Body" ), "Enum_Body" },
{ sizeof( "Enum_Class" ), "Enum_Class" },
{ sizeof( "Enum_Class_Fwd" ), "Enum_Class_Fwd" },
{ sizeof( "Execution" ), "Execution" },
{ sizeof( "Export_Body" ), "Export_Body" },
{ sizeof( "Extern_Linkage" ), "Extern_Linkage" },
{ sizeof( "Extern_Linkage_Body" ), "Extern_Linkage_Body" },
{ sizeof( "Friend" ), "Friend" },
{ sizeof( "Function" ), "Function" },
{ sizeof( "Function_Fwd" ), "Function_Fwd" },
{ sizeof( "Function_Body" ), "Function_Body" },
{ sizeof( "Global_Body" ), "Global_Body" },
{ sizeof( "Module" ), "Module" },
{ sizeof( "Namespace" ), "Namespace" },
{ sizeof( "Namespace_Body" ), "Namespace_Body" },
{ sizeof( "Operator" ), "Operator" },
{ sizeof( "Operator_Fwd" ), "Operator_Fwd" },
{ sizeof( "Operator_Member" ), "Operator_Member" },
{ sizeof( "Operator_Member_Fwd" ), "Operator_Member_Fwd" },
{ sizeof( "Operator_Cast" ), "Operator_Cast" },
{ sizeof( "Operator_Cast_Fwd" ), "Operator_Cast_Fwd" },
{ sizeof( "Parameters" ), "Parameters" },
{ sizeof( "Preprocess_Define" ), "Preprocess_Define" },
{ sizeof( "Preprocess_Include" ), "Preprocess_Include" },
{ sizeof( "Preprocess_If" ), "Preprocess_If" },
{ sizeof( "Preprocess_IfDef" ), "Preprocess_IfDef" },
{ sizeof( "Preprocess_IfNotDef" ), "Preprocess_IfNotDef" },
{ sizeof( "Preprocess_ElIf" ), "Preprocess_ElIf" },
{ sizeof( "Preprocess_Else" ), "Preprocess_Else" },
{ sizeof( "Preprocess_EndIf" ), "Preprocess_EndIf" },
{ sizeof( "Preprocess_Pragma" ), "Preprocess_Pragma" },
{ sizeof( "Specifiers" ), "Specifiers" },
{ sizeof( "Struct" ), "Struct" },
{ sizeof( "Struct_Fwd" ), "Struct_Fwd" },
{ sizeof( "Struct_Body" ), "Struct_Body" },
{ sizeof( "Template" ), "Template" },
{ sizeof( "Typedef" ), "Typedef" },
{ sizeof( "Typename" ), "Typename" },
{ sizeof( "Union" ), "Union" },
{ sizeof( "Union_Body" ), "Union_Body" },
{ sizeof( "Using" ), "Using" },
{ sizeof( "Using_Namespace" ), "Using_Namespace" },
{ sizeof( "Variable" ), "Variable" },
};
return lookup[type];
}
} // namespace ECode
using CodeT = ECode::Type;

View File

@ -0,0 +1,118 @@
#ifdef GEN_INTELLISENSE_DIRECTIVES
#pragma once
#include "components/types.hpp"
#endif
// This file was generated automatially by gencpp's bootstrap.cpp (See: https://github.com/Ed94/gencpp)
namespace EOperator
{
enum Type : u32
{
Invalid,
Assign,
Assign_Add,
Assign_Subtract,
Assign_Multiply,
Assign_Divide,
Assign_Modulo,
Assign_BAnd,
Assign_BOr,
Assign_BXOr,
Assign_LShift,
Assign_RShift,
Increment,
Decrement,
Unary_Plus,
Unary_Minus,
UnaryNot,
Add,
Subtract,
Multiply,
Divide,
Modulo,
BNot,
BAnd,
BOr,
BXOr,
LShift,
RShift,
LAnd,
LOr,
LEqual,
LNot,
Lesser,
Greater,
LesserEqual,
GreaterEqual,
Subscript,
Indirection,
AddressOf,
MemberOfPointer,
PtrToMemOfPtr,
FunctionCall,
Comma,
New,
NewArray,
Delete,
DeleteArray,
NumOps
};
inline StrC to_str( Type op )
{
local_persist StrC lookup[] {
{ sizeof( "INVALID" ), "INVALID" },
{ sizeof( "=" ), "=" },
{ sizeof( "+=" ), "+=" },
{ sizeof( "-=" ), "-=" },
{ sizeof( "*=" ), "*=" },
{ sizeof( "/=" ), "/=" },
{ sizeof( "%=" ), "%=" },
{ sizeof( "&=" ), "&=" },
{ sizeof( "|=" ), "|=" },
{ sizeof( "^=" ), "^=" },
{ sizeof( "<<=" ), "<<=" },
{ sizeof( ">>=" ), ">>=" },
{ sizeof( "++" ), "++" },
{ sizeof( "--" ), "--" },
{ sizeof( "+" ), "+" },
{ sizeof( "-" ), "-" },
{ sizeof( "!" ), "!" },
{ sizeof( "+" ), "+" },
{ sizeof( "-" ), "-" },
{ sizeof( "*" ), "*" },
{ sizeof( "/" ), "/" },
{ sizeof( "%" ), "%" },
{ sizeof( "~" ), "~" },
{ sizeof( "&" ), "&" },
{ sizeof( "|" ), "|" },
{ sizeof( "^" ), "^" },
{ sizeof( "<<" ), "<<" },
{ sizeof( ">>" ), ">>" },
{ sizeof( "&&" ), "&&" },
{ sizeof( "||" ), "||" },
{ sizeof( "==" ), "==" },
{ sizeof( "!=" ), "!=" },
{ sizeof( "<" ), "<" },
{ sizeof( ">" ), ">" },
{ sizeof( "<=" ), "<=" },
{ sizeof( ">=" ), ">=" },
{ sizeof( "[]" ), "[]" },
{ sizeof( "*" ), "*" },
{ sizeof( "&" ), "&" },
{ sizeof( "->" ), "->" },
{ sizeof( "->*" ), "->*" },
{ sizeof( "()" ), "()" },
{ sizeof( "," ), "," },
{ sizeof( "new" ), "new" },
{ sizeof( "new[]" ), "new[]" },
{ sizeof( "delete" ), "delete" },
{ sizeof( "delete[]" ), "delete[]" },
};
return lookup[op];
}
} // namespace EOperator
using OperatorT = EOperator::Type;

View File

@ -0,0 +1,98 @@
#ifdef GEN_INTELLISENSE_DIRECTIVES
#pragma once
#include "components/types.hpp"
#endif
// This file was generated automatially by gencpp's bootstrap.cpp (See: https://github.com/Ed94/gencpp)
namespace ESpecifier
{
enum Type : u32
{
Invalid,
Consteval,
Constexpr,
Constinit,
Explicit,
External_Linkage,
ForceInline,
Global,
Inline,
Internal_Linkage,
Local_Persist,
Mutable,
NeverInline,
Ptr,
Ref,
Register,
RValue,
Static,
Thread_Local,
Virtual,
Const,
Final,
NoExceptions,
Override,
Pure,
Volatile,
NumSpecifiers
};
inline bool is_trailing( Type specifier )
{
return specifier > Virtual;
}
inline StrC to_str( Type type )
{
local_persist StrC lookup[] {
{ sizeof( "INVALID" ), "INVALID" },
{ sizeof( "consteval" ), "consteval" },
{ sizeof( "constexpr" ), "constexpr" },
{ sizeof( "constinit" ), "constinit" },
{ sizeof( "explicit" ), "explicit" },
{ sizeof( "extern" ), "extern" },
{ sizeof( "forceinline" ), "forceinline" },
{ sizeof( "global" ), "global" },
{ sizeof( "inline" ), "inline" },
{ sizeof( "internal" ), "internal" },
{ sizeof( "local_persist" ), "local_persist" },
{ sizeof( "mutable" ), "mutable" },
{ sizeof( "neverinline" ), "neverinline" },
{ sizeof( "*" ), "*" },
{ sizeof( "&" ), "&" },
{ sizeof( "register" ), "register" },
{ sizeof( "&&" ), "&&" },
{ sizeof( "static" ), "static" },
{ sizeof( "thread_local" ), "thread_local" },
{ sizeof( "virtual" ), "virtual" },
{ sizeof( "const" ), "const" },
{ sizeof( "final" ), "final" },
{ sizeof( "noexcept" ), "noexcept" },
{ sizeof( "override" ), "override" },
{ sizeof( "= 0" ), "= 0" },
{ sizeof( "volatile" ), "volatile" },
};
return lookup[type];
}
inline Type to_type( StrC str )
{
local_persist u32 keymap[NumSpecifiers];
do_once_start for ( u32 index = 0; index < NumSpecifiers; index++ )
{
StrC enum_str = to_str( (Type)index );
keymap[index] = crc32( enum_str.Ptr, enum_str.Len - 1 );
}
do_once_end u32 hash = crc32( str.Ptr, str.Len );
for ( u32 index = 0; index < NumSpecifiers; index++ )
{
if ( keymap[index] == hash )
return (Type)index;
}
return Invalid;
}
} // namespace ESpecifier
using SpecifierT = ESpecifier::Type;

View File

@ -0,0 +1,241 @@
#ifdef GEN_INTELLISENSE_DIRECTIVES
#pragma once
#include "components/types.hpp"
#endif
// This file was generated automatially by gencpp's bootstrap.cpp (See: https://github.com/Ed94/gencpp)
namespace parser
{
namespace ETokType
{
#define GEN_DEFINE_ATTRIBUTE_TOKENS Entry( Attribute_API_Export, "GEN_API_Export_Code" ) Entry( Attribute_API_Import, "GEN_API_Import_Code" )
enum Type : u32
{
Invalid,
Access_Private,
Access_Protected,
Access_Public,
Access_MemberSymbol,
Access_StaticSymbol,
Ampersand,
Ampersand_DBL,
Assign_Classifer,
Attribute_Open,
Attribute_Close,
BraceCurly_Open,
BraceCurly_Close,
BraceSquare_Open,
BraceSquare_Close,
Capture_Start,
Capture_End,
Comment,
Comment_End,
Comment_Start,
Char,
Comma,
Decl_Class,
Decl_GNU_Attribute,
Decl_MSVC_Attribute,
Decl_Enum,
Decl_Extern_Linkage,
Decl_Friend,
Decl_Module,
Decl_Namespace,
Decl_Operator,
Decl_Struct,
Decl_Template,
Decl_Typedef,
Decl_Using,
Decl_Union,
Identifier,
Module_Import,
Module_Export,
NewLine,
Number,
Operator,
Preprocess_Hash,
Preprocess_Define,
Preprocess_If,
Preprocess_IfDef,
Preprocess_IfNotDef,
Preprocess_ElIf,
Preprocess_Else,
Preprocess_EndIf,
Preprocess_Include,
Preprocess_Pragma,
Preprocess_Content,
Preprocess_Macro,
Preprocess_Unsupported,
Spec_Alignas,
Spec_Const,
Spec_Consteval,
Spec_Constexpr,
Spec_Constinit,
Spec_Explicit,
Spec_Extern,
Spec_Final,
Spec_ForceInline,
Spec_Global,
Spec_Inline,
Spec_Internal_Linkage,
Spec_LocalPersist,
Spec_Mutable,
Spec_NeverInline,
Spec_Override,
Spec_Static,
Spec_ThreadLocal,
Spec_Volatile,
Spec_Virtual,
Star,
Statement_End,
StaticAssert,
String,
Type_Typename,
Type_Unsigned,
Type_Signed,
Type_Short,
Type_Long,
Type_bool,
Type_char,
Type_int,
Type_double,
Type_MS_int8,
Type_MS_int16,
Type_MS_int32,
Type_MS_int64,
Type_MS_W64,
Varadic_Argument,
__Attributes_Start,
Attribute_API_Export,
Attribute_API_Import,
NumTokens
};
inline StrC to_str( Type type )
{
local_persist StrC lookup[] {
{ sizeof( "__invalid__" ), "__invalid__" },
{ sizeof( "private" ), "private" },
{ sizeof( "protected" ), "protected" },
{ sizeof( "public" ), "public" },
{ sizeof( "." ), "." },
{ sizeof( "::" ), "::" },
{ sizeof( "&" ), "&" },
{ sizeof( "&&" ), "&&" },
{ sizeof( ":" ), ":" },
{ sizeof( "[[" ), "[[" },
{ sizeof( "]]" ), "]]" },
{ sizeof( "{" ), "{" },
{ sizeof( "}" ), "}" },
{ sizeof( "[" ), "[" },
{ sizeof( "]" ), "]" },
{ sizeof( "(" ), "(" },
{ sizeof( ")" ), ")" },
{ sizeof( "__comment__" ), "__comment__" },
{ sizeof( "__comment_end__" ), "__comment_end__" },
{ sizeof( "__comment_start__" ), "__comment_start__" },
{ sizeof( "__character__" ), "__character__" },
{ sizeof( "," ), "," },
{ sizeof( "class" ), "class" },
{ sizeof( "__attribute__" ), "__attribute__" },
{ sizeof( "__declspec" ), "__declspec" },
{ sizeof( "enum" ), "enum" },
{ sizeof( "extern" ), "extern" },
{ sizeof( "friend" ), "friend" },
{ sizeof( "module" ), "module" },
{ sizeof( "namespace" ), "namespace" },
{ sizeof( "operator" ), "operator" },
{ sizeof( "struct" ), "struct" },
{ sizeof( "template" ), "template" },
{ sizeof( "typedef" ), "typedef" },
{ sizeof( "using" ), "using" },
{ sizeof( "union" ), "union" },
{ sizeof( "__identifier__" ), "__identifier__" },
{ sizeof( "import" ), "import" },
{ sizeof( "export" ), "export" },
{ sizeof( "__new_line__" ), "__new_line__" },
{ sizeof( "__number__" ), "__number__" },
{ sizeof( "__operator__" ), "__operator__" },
{ sizeof( "#" ), "#" },
{ sizeof( "define" ), "define" },
{ sizeof( "if" ), "if" },
{ sizeof( "ifdef" ), "ifdef" },
{ sizeof( "ifndef" ), "ifndef" },
{ sizeof( "elif" ), "elif" },
{ sizeof( "else" ), "else" },
{ sizeof( "endif" ), "endif" },
{ sizeof( "include" ), "include" },
{ sizeof( "pragma" ), "pragma" },
{ sizeof( "__macro_content__" ), "__macro_content__" },
{ sizeof( "__macro__" ), "__macro__" },
{ sizeof( "__unsupported__" ), "__unsupported__" },
{ sizeof( "alignas" ), "alignas" },
{ sizeof( "const" ), "const" },
{ sizeof( "consteval" ), "consteval" },
{ sizeof( "constexpr" ), "constexpr" },
{ sizeof( "constinit" ), "constinit" },
{ sizeof( "explicit" ), "explicit" },
{ sizeof( "extern" ), "extern" },
{ sizeof( "final" ), "final" },
{ sizeof( "forceinline" ), "forceinline" },
{ sizeof( "global" ), "global" },
{ sizeof( "inline" ), "inline" },
{ sizeof( "internal" ), "internal" },
{ sizeof( "local_persist" ), "local_persist" },
{ sizeof( "mutable" ), "mutable" },
{ sizeof( "neverinline" ), "neverinline" },
{ sizeof( "override" ), "override" },
{ sizeof( "static" ), "static" },
{ sizeof( "thread_local" ), "thread_local" },
{ sizeof( "volatile" ), "volatile" },
{ sizeof( "virtual" ), "virtual" },
{ sizeof( "*" ), "*" },
{ sizeof( ";" ), ";" },
{ sizeof( "static_assert" ), "static_assert" },
{ sizeof( "__string__" ), "__string__" },
{ sizeof( "typename" ), "typename" },
{ sizeof( "unsigned" ), "unsigned" },
{ sizeof( "signed" ), "signed" },
{ sizeof( "short" ), "short" },
{ sizeof( "long" ), "long" },
{ sizeof( "bool" ), "bool" },
{ sizeof( "char" ), "char" },
{ sizeof( "int" ), "int" },
{ sizeof( "double" ), "double" },
{ sizeof( "__int8" ), "__int8" },
{ sizeof( "__int16" ), "__int16" },
{ sizeof( "__int32" ), "__int32" },
{ sizeof( "__int64" ), "__int64" },
{ sizeof( "_W64" ), "_W64" },
{ sizeof( "..." ), "..." },
{ sizeof( "__attrib_start__" ), "__attrib_start__" },
{ sizeof( "GEN_API_Export_Code" ), "GEN_API_Export_Code" },
{ sizeof( "GEN_API_Import_Code" ), "GEN_API_Import_Code" },
};
return lookup[type];
}
inline Type to_type( StrC str )
{
local_persist u32 keymap[NumTokens];
do_once_start for ( u32 index = 0; index < NumTokens; index++ )
{
StrC enum_str = to_str( (Type)index );
keymap[index] = crc32( enum_str.Ptr, enum_str.Len - 1 );
}
do_once_end u32 hash = crc32( str.Ptr, str.Len );
for ( u32 index = 0; index < NumTokens; index++ )
{
if ( keymap[index] == hash )
return (Type)index;
}
return Invalid;
}
} // namespace ETokType
using TokType = ETokType::Type;
} // namespace parser

View File

@ -1,400 +1,16 @@
#pragma region Inlines #ifdef GEN_INTELLISENSE_DIRECTIVES
#pragma once
void AST::append( AST* other ) #include "inlines.hpp"
{ #include "gen/ast_inlines.hpp"
if ( other->Parent ) #endif
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()
{
char const* fmt = stringize(
\nCode Debug:
\nType : %s
\nParent : %s
\nName : %s
\nComment : %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 ? Parent->Name : ""
, 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( 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( 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( 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( 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( 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( 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
#ifdef GEN_DEFINE_LIBRARY_CODE_CONSTANTS
// Predefined typename codes. Are set to readonly and are setup during gen::init()
extern CodeType t_b32;
extern CodeType t_s8;
extern CodeType t_s16;
extern CodeType t_s32;
extern CodeType t_s64;
extern CodeType t_u8;
extern CodeType t_u16;
extern CodeType t_u32;
extern CodeType t_u64;
extern CodeType t_sw;
extern CodeType t_uw;
extern CodeType t_f32;
extern CodeType t_f64;
#endif
#ifndef GEN_GLOBAL_BUCKET_SIZE #ifndef GEN_GLOBAL_BUCKET_SIZE
# define GEN_GLOBAL_BUCKET_SIZE megabytes(10) # define GEN_GLOBAL_BUCKET_SIZE megabytes(8)
#endif #endif
#ifndef GEN_CODEPOOL_NUM_BLOCKS #ifndef GEN_CODEPOOL_NUM_BLOCKS
# define GEN_CODEPOOL_NUM_BLOCKS kilobytes(64) # define GEN_CODEPOOL_NUM_BLOCKS kilobytes(16)
#endif #endif
#ifndef GEN_SIZE_PER_STRING_ARENA #ifndef GEN_SIZE_PER_STRING_ARENA
# define GEN_SIZE_PER_STRING_ARENA megabytes(1) # define GEN_SIZE_PER_STRING_ARENA megabytes(1)
@ -412,10 +28,10 @@ StrC token_fmt_impl( sw num, ... )
# define GEN_TOKEN_FMT_TOKEN_MAP_MEM_SIZE kilobytes(4) # define GEN_TOKEN_FMT_TOKEN_MAP_MEM_SIZE kilobytes(4)
#endif #endif
#ifndef GEN_LEX_ALLOCATOR_SIZE #ifndef GEN_LEX_ALLOCATOR_SIZE
# define GEN_LEX_ALLOCATOR_SIZE megabytes(10) # define GEN_LEX_ALLOCATOR_SIZE megabytes(4)
#endif #endif
#ifndef GEN_BUILDER_STR_BUFFER_RESERVE #ifndef GEN_BUILDER_STR_BUFFER_RESERVE
# define GEN_BUILDER_STR_BUFFER_RESERVE megabytes(1) # define GEN_BUILDER_STR_BUFFER_RESERVE megabytes(2)
#endif #endif
// These constexprs are used for allocation behavior of data structures // These constexprs are used for allocation behavior of data structures
@ -426,7 +42,7 @@ constexpr s32 InitSize_DataArrays = 16;
// NOTE: This limits the maximum size of an allocation // NOTE: This limits the maximum size of an allocation
// If you are generating a string larger than this, increase the size of the bucket here. // If you are generating a string larger than this, increase the size of the bucket here.
constexpr uw Global_BucketSize = GEN_GLOBAL_BUCKET_SIZE; constexpr usize Global_BucketSize = GEN_GLOBAL_BUCKET_SIZE;
constexpr s32 CodePool_NumBlocks = GEN_CODEPOOL_NUM_BLOCKS; constexpr s32 CodePool_NumBlocks = GEN_CODEPOOL_NUM_BLOCKS;
constexpr s32 SizePer_StringArena = GEN_SIZE_PER_STRING_ARENA; constexpr s32 SizePer_StringArena = GEN_SIZE_PER_STRING_ARENA;
@ -437,6 +53,50 @@ constexpr s32 TokenFmt_TokenMap_MemSize = GEN_TOKEN_FMT_TOKEN_MAP_MEM_SIZE;
constexpr s32 LexAllocator_Size = GEN_LEX_ALLOCATOR_SIZE; constexpr s32 LexAllocator_Size = GEN_LEX_ALLOCATOR_SIZE;
constexpr s32 Builder_StrBufferReserve = GEN_BUILDER_STR_BUFFER_RESERVE; constexpr s32 Builder_StrBufferReserve = GEN_BUILDER_STR_BUFFER_RESERVE;
extern Code access_public;
extern Code access_protected;
extern Code access_private;
extern CodeAttributes attrib_api_export;
extern CodeAttributes attrib_api_import;
extern Code module_global_fragment;
extern Code module_private_fragment;
extern Code fmt_newline;
extern CodePragma pragma_once;
extern CodeParam param_varadic;
extern CodePreprocessCond preprocess_else;
extern CodePreprocessCond preprocess_endif;
extern CodeSpecifiers spec_const;
extern CodeSpecifiers spec_consteval;
extern CodeSpecifiers spec_constexpr;
extern CodeSpecifiers spec_constinit;
extern CodeSpecifiers spec_extern_linkage;
extern CodeSpecifiers spec_final;
extern CodeSpecifiers spec_forceinline;
extern CodeSpecifiers spec_global;
extern CodeSpecifiers spec_inline;
extern CodeSpecifiers spec_internal_linkage;
extern CodeSpecifiers spec_local_persist;
extern CodeSpecifiers spec_mutable;
extern CodeSpecifiers spec_neverinline;
extern CodeSpecifiers spec_noexcept;
extern CodeSpecifiers spec_override;
extern CodeSpecifiers spec_ptr;
extern CodeSpecifiers spec_pure;
extern CodeSpecifiers spec_ref;
extern CodeSpecifiers spec_register;
extern CodeSpecifiers spec_rvalue;
extern CodeSpecifiers spec_static_member;
extern CodeSpecifiers spec_thread_local;
extern CodeSpecifiers spec_virtual;
extern CodeSpecifiers spec_volatile;
extern CodeType t_empty; // Used with varaidc parameters. (Exposing just in case its useful for another circumstance) extern CodeType t_empty; // Used with varaidc parameters. (Exposing just in case its useful for another circumstance)
extern CodeType t_auto; extern CodeType t_auto;
extern CodeType t_void; extern CodeType t_void;
@ -447,45 +107,33 @@ extern CodeType t_wchar_t;
extern CodeType t_class; extern CodeType t_class;
extern CodeType t_typename; extern CodeType t_typename;
extern CodeParam param_varadic; #ifdef GEN_DEFINE_LIBRARY_CODE_CONSTANTS
// Predefined typename codes. Are set to readonly and are setup during gen::init()
extern CodeAttributes attrib_api_export; extern CodeType t_b32;
extern CodeAttributes attrib_api_import;
extern Code access_public; extern CodeType t_s8;
extern Code access_protected; extern CodeType t_s16;
extern Code access_private; extern CodeType t_s32;
extern CodeType t_s64;
extern Code module_global_fragment; extern CodeType t_u8;
extern Code module_private_fragment; extern CodeType t_u16;
extern CodeType t_u32;
extern CodeType t_u64;
extern Code pragma_once; extern CodeType t_ssize;
extern CodeType t_usize;
extern CodeSpecifiers spec_const; extern CodeType t_f32;
extern CodeSpecifiers spec_consteval; extern CodeType t_f64;
extern CodeSpecifiers spec_constexpr; #endif
extern CodeSpecifiers spec_constinit;
extern CodeSpecifiers spec_extern_linkage;
extern CodeSpecifiers spec_final;
extern CodeSpecifiers spec_global;
extern CodeSpecifiers spec_inline;
extern CodeSpecifiers spec_internal_linkage;
extern CodeSpecifiers spec_local_persist;
extern CodeSpecifiers spec_mutable;
extern CodeSpecifiers spec_override;
extern CodeSpecifiers spec_ptr;
extern CodeSpecifiers spec_ref;
extern CodeSpecifiers spec_register;
extern CodeSpecifiers spec_rvalue;
extern CodeSpecifiers spec_static_member;
extern CodeSpecifiers spec_thread_local;
extern CodeSpecifiers spec_virtual;
extern CodeSpecifiers spec_volatile;
#pragma endregion Constants #pragma endregion Constants
#pragma region Macros #pragma region Macros
#ifndef token_fmt
# define gen_main main # define gen_main main
# define __ NoCode # define __ NoCode
@ -499,19 +147,26 @@ extern CodeSpecifiers spec_volatile;
# define args( ... ) num_args( __VA_ARGS__ ), __VA_ARGS__ # define args( ... ) num_args( __VA_ARGS__ ), __VA_ARGS__
# define code_str( ... ) gen::untyped_str( code( __VA_ARGS__ ) ) # define code_str( ... ) GEN_NS untyped_str( code( __VA_ARGS__ ) )
# define code_fmt( ... ) gen::untyped_str( token_fmt( __VA_ARGS__ ) ) # define code_fmt( ... ) GEN_NS untyped_str( token_fmt( __VA_ARGS__ ) )
// Takes a format string (char const*) and a list of tokens (StrC) and returns a StrC of the formatted string. // Takes a format string (char const*) and a list of tokens (StrC) and returns a StrC of the formatted string.
# define token_fmt( ... ) gen::token_fmt_impl( (num_args( __VA_ARGS__ ) + 1) / 2, __VA_ARGS__ ) # define token_fmt( ... ) GEN_NS token_fmt_impl( (num_args( __VA_ARGS__ ) + 1) / 2, __VA_ARGS__ )
#endif
#pragma endregion Macros #pragma endregion Macros
// Used by the lexer to persistently treat all these identifiers as preprocessor defines.
// Populate with strings via gen::get_cached_string.
// Functional defines must have format: id( ;at minimum to indicate that the define is only valid with arguments.
extern Array< StringCached > PreprocessorDefines;
#ifdef GEN_EXPOSE_BACKEND #ifdef GEN_EXPOSE_BACKEND
// 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;

View File

@ -1,3 +1,5 @@
#pragma once
/* /*
gencpp: An attempt at "simple" staged metaprogramming for c/c++. gencpp: An attempt at "simple" staged metaprogramming for c/c++.
@ -15,3 +17,15 @@
#ifndef GEN_ROLL_OWN_DEPENDENCIES #ifndef GEN_ROLL_OWN_DEPENDENCIES
# include "gen.dep.hpp" # include "gen.dep.hpp"
#endif #endif
#ifndef GEN_NS_BEGIN
# ifdef GEN_DONT_USE_NAMESPACE
# define GEN_NS
# define GEN_NS_BEGIN
# define GEN_NS_END
# else
# define GEN_NS gen::
# define GEN_NS_BEGIN namespace gen {
# define GEN_NS_END }
# endif
#endif

View File

@ -0,0 +1,433 @@
#ifdef GEN_INTELLISENSE_DIRECTIVES
#pragma once
#include "interface.hpp"
#endif
inline
void append( AST* self, AST* other )
{
GEN_ASSERT(self != nullptr);
GEN_ASSERT(other != nullptr);
if ( other->Parent )
other = duplicate(other);
other->Parent = self;
if ( self->Front == nullptr )
{
self->Front = other;
self->Back = other;
self->NumEntries++;
return;
}
AST*
Current = self->Back;
Current->Next = other;
other->Prev = Current;
self->Back = other;
self->NumEntries++;
}
inline
Code* entry( AST* self, u32 idx )
{
GEN_ASSERT(self != nullptr);
AST** current = & self->Front;
while ( idx >= 0 && current != nullptr )
{
if ( idx == 0 )
return rcast( Code*, current);
current = & ( * current )->Next;
idx--;
}
return rcast( Code*, current);
}
inline
bool has_entries(AST* self)
{
GEN_ASSERT(self != nullptr);
return self->NumEntries > 0;
}
inline
bool is_body(AST* self)
{
GEN_ASSERT(self != nullptr);
switch (self->Type)
{
case ECode::Enum_Body:
case ECode::Class_Body:
case ECode::Union_Body:
case ECode::Export_Body:
case ECode::Global_Body:
case ECode::Struct_Body:
case ECode::Function_Body:
case ECode::Namespace_Body:
case ECode::Extern_Linkage_Body:
return true;
}
return false;
}
inline
char const* type_str(AST* self)
{
GEN_ASSERT(self != nullptr);
return ECode::to_str( self->Type );
}
inline
AST::operator Code()
{
return { this };
}
#pragma region Code
inline
char const* debug_str( Code code )
{
if ( code.ast == nullptr )
return "Code::debug_str: AST is null!";
return debug_str( code.ast );
}
inline
Code duplicate( Code code )
{
if ( code.ast == nullptr )
{
log_failure("Code::duplicate: Cannot duplicate code, AST is null!");
return Code_Invalid;
}
return { duplicate(code.ast) };
}
inline
bool is_body(Code code)
{
if ( code.ast == nullptr )
{
return is_body(code.ast);
}
return false;
}
inline
bool is_equal( Code self, Code other )
{
if ( self.ast == nullptr || other.ast == nullptr )
{
// Just check if they're both null.
// log_failure( "Code::is_equal: Cannot compare code, AST is null!" );
return self.ast == nullptr && other.ast == nullptr;
}
return is_equal( self.ast, other.ast );
}
inline
bool is_valid(Code self)
{
return self.ast != nullptr && self.ast->Type != CodeT::Invalid;
}
inline
void set_global(Code self)
{
if ( self.ast == nullptr )
{
log_failure("Code::set_global: Cannot set code as global, AST is null!");
return;
}
self->Parent = Code_Global.ast;
}
inline
Code& Code::operator ++()
{
if ( ast )
ast = ast->Next;
return *this;
}
#pragma endregion Code
#pragma region CodeBody
inline
void append( CodeBody self, Code other )
{
GEN_ASSERT(other.ast != nullptr);
if (is_body(other)) {
append( self, cast(CodeBody, other) );
return;
}
append( rcast(AST*, self.ast), other.ast );
}
inline
void append( CodeBody self, CodeBody body )
{
GEN_ASSERT(self.ast != nullptr);
for ( Code entry : body ) {
append( self, entry );
}
}
inline
Code begin( CodeBody body) {
if ( body.ast )
return { rcast( AST*, body.ast)->Front };
return { nullptr };
}
inline
Code end(CodeBody body ){
return { rcast(AST*, body.ast)->Back->Next };
}
#pragma endregion CodeBody
#pragma region CodeClass
inline
void add_interface( CodeClass self, CodeType type )
{
GEN_ASSERT(self.ast !=nullptr);
CodeType possible_slot = self->ParentType;
if ( possible_slot.ast )
{
// Were adding an interface to parent type, so we need to make sure the parent type is public.
self->ParentAccess = AccessSpec_Public;
// If your planning on adding a proper parent,
// then you'll need to move this over to ParentType->next and update ParentAccess accordingly.
}
while ( possible_slot.ast != nullptr )
{
possible_slot.ast = (AST_Type*) possible_slot->Next.ast;
}
possible_slot.ast = type.ast;
}
#pragma endregion CodeClass
#pragma region CodeParam
inline
void append( CodeParam appendee, CodeParam other )
{
GEN_ASSERT(appendee.ast != nullptr);
AST* self = cast(Code, appendee).ast;
AST* entry = (AST*) other.ast;
if ( entry->Parent )
entry = GEN_NS duplicate( entry );
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++;
}
inline
CodeParam get(CodeParam self, s32 idx )
{
GEN_ASSERT(self.ast != nullptr);
CodeParam param = * self;
do
{
if ( ! ++ param )
return { nullptr };
param = { (AST_Param*) cast(Code, param)->Next };
}
while ( --idx );
return param;
}
inline
bool has_entries(CodeParam self)
{
GEN_ASSERT(self.ast != nullptr);
return self->NumEntries > 0;
}
inline
CodeParam& CodeParam::operator ++()
{
ast = ast->Next.ast;
return * this;
}
inline
CodeParam begin(CodeParam params)
{
if ( params.ast )
return { params.ast };
return { nullptr };
}
inline
CodeParam end(CodeParam params)
{
// return { (AST_Param*) rcast( AST*, ast)->Last };
return { nullptr };
}
#pragma endregion CodeParam
#pragma region CodeSpecifiers
inline
bool append(CodeSpecifiers self, SpecifierT spec )
{
if ( self.ast == nullptr )
{
log_failure("CodeSpecifiers: Attempted to append to a null specifiers AST!");
return false;
}
if ( self->NumEntries == AST_ArrSpecs_Cap )
{
log_failure("CodeSpecifiers: Attempted to append over %d specifiers to a specifiers AST!", AST_ArrSpecs_Cap );
return false;
}
self->ArrSpecs[ self->NumEntries ] = spec;
self->NumEntries++;
return true;
}
inline
s32 has(CodeSpecifiers self, SpecifierT spec)
{
GEN_ASSERT(self.ast != nullptr);
for ( s32 idx = 0; idx < self->NumEntries; idx++ ) {
if ( self->ArrSpecs[ idx ] == spec )
return idx;
}
return -1;
}
inline
s32 remove( CodeSpecifiers self, SpecifierT to_remove )
{
AST_Specifiers* ast = self.ast;
if ( ast == nullptr )
{
log_failure("CodeSpecifiers: Attempted to append to a null specifiers AST!");
return -1;
}
if ( self->NumEntries == AST_ArrSpecs_Cap )
{
log_failure("CodeSpecifiers: Attempted to append over %d specifiers to a specifiers AST!", AST_ArrSpecs_Cap );
return -1;
}
s32 result = -1;
s32 curr = 0;
s32 next = 0;
for(; next < self->NumEntries; ++ curr, ++ next)
{
SpecifierT spec = self->ArrSpecs[next];
if (spec == to_remove)
{
result = next;
next ++;
if (next >= self->NumEntries)
break;
spec = self->ArrSpecs[next];
}
self->ArrSpecs[ curr ] = spec;
}
if (result > -1) {
self->NumEntries --;
}
return result;
}
inline
SpecifierT* begin(CodeSpecifiers self)
{
if ( self.ast )
return & self->ArrSpecs[0];
return nullptr;
}
inline
SpecifierT* end(CodeSpecifiers self)
{
return self->ArrSpecs + self->NumEntries;
}
#pragma endregion CodeSpecifiers
#pragma region CodeStruct
inline
void add_interface(CodeStruct self, CodeType type )
{
CodeType possible_slot = self->ParentType;
if ( possible_slot.ast )
{
// Were adding an interface to parent type, so we need to make sure the parent type is public.
self->ParentAccess = AccessSpec_Public;
// If your planning on adding a proper parent,
// then you'll need to move this over to ParentType->next and update ParentAccess accordingly.
}
while ( possible_slot.ast != nullptr )
{
possible_slot.ast = (AST_Type*) possible_slot->Next.ast;
}
possible_slot.ast = type.ast;
}
#pragma endregion Code
#pragma region Interface
inline
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;
}
inline
StrC token_fmt_impl( ssize 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 );
ssize result = token_fmt_va(buf, GEN_PRINTF_MAXLEN, num, va);
va_end(va);
return { result, buf };
}
#pragma endregion Interface

View File

@ -1,7 +1,17 @@
#ifdef GEN_INTELLISENSE_DIRECTIVES
#pragma once
#include "code_serialization.cpp"
#endif
namespace parser {
internal void init();
internal void deinit();
}
internal internal
void* Global_Allocator_Proc( void* allocator_data, AllocType type, sw size, sw alignment, void* old_memory, sw old_size, u64 flags ) void* Global_Allocator_Proc( void* allocator_data, AllocType type, ssize size, ssize alignment, void* old_memory, ssize old_size, u64 flags )
{ {
Arena* last = & Global_AllocatorBuckets.back(); Arena* last = back(& Global_AllocatorBuckets);
switch ( type ) switch ( type )
{ {
@ -9,18 +19,18 @@ void* Global_Allocator_Proc( void* allocator_data, AllocType type, sw size, sw a
{ {
if ( ( last->TotalUsed + size ) > last->TotalSize ) if ( ( last->TotalUsed + size ) > last->TotalSize )
{ {
Arena bucket = Arena::init_from_allocator( heap(), Global_BucketSize ); Arena bucket = arena_init_from_allocator( heap(), Global_BucketSize );
if ( bucket.PhysicalStart == nullptr ) if ( bucket.PhysicalStart == nullptr )
fatal( "Failed to create bucket for Global_AllocatorBuckets"); GEN_FATAL( "Failed to create bucket for Global_AllocatorBuckets");
if ( ! Global_AllocatorBuckets.append( bucket ) ) if ( ! append( & Global_AllocatorBuckets, bucket ) )
fatal( "Failed to append bucket to Global_AllocatorBuckets"); GEN_FATAL( "Failed to append bucket to Global_AllocatorBuckets");
last = & Global_AllocatorBuckets.back(); last = back(& Global_AllocatorBuckets);
} }
return alloc_align( * last, size, alignment ); return alloc_align( allocator_info(last), size, alignment );
} }
case EAllocation_FREE: case EAllocation_FREE:
{ {
@ -36,15 +46,15 @@ void* Global_Allocator_Proc( void* allocator_data, AllocType type, sw size, sw a
{ {
if ( last->TotalUsed + size > last->TotalSize ) if ( last->TotalUsed + size > last->TotalSize )
{ {
Arena bucket = Arena::init_from_allocator( heap(), Global_BucketSize ); Arena bucket = arena_init_from_allocator( heap(), Global_BucketSize );
if ( bucket.PhysicalStart == nullptr ) if ( bucket.PhysicalStart == nullptr )
fatal( "Failed to create bucket for Global_AllocatorBuckets"); GEN_FATAL( "Failed to create bucket for Global_AllocatorBuckets");
if ( ! Global_AllocatorBuckets.append( bucket ) ) if ( ! append( & Global_AllocatorBuckets, bucket ) )
fatal( "Failed to append bucket to Global_AllocatorBuckets"); GEN_FATAL( "Failed to append bucket to Global_AllocatorBuckets");
last = & Global_AllocatorBuckets.back(); last = back(& Global_AllocatorBuckets);
} }
void* result = alloc_align( last->Backing, size, alignment ); void* result = alloc_align( last->Backing, size, alignment );
@ -64,12 +74,74 @@ void* Global_Allocator_Proc( void* allocator_data, AllocType type, sw size, sw a
internal internal
void define_constants() void define_constants()
{ {
Code::Global = make_code(); Code_Global = make_code();
Code::Global->Name = get_cached_string( txt_StrC("Global Code") ); scast(String, Code_Global->Name) = get_cached_string( txt("Global Code") );
Code::Global->Content = Code::Global->Name; scast(String, Code_Global->Content) = Code_Global->Name;
Code::Invalid = make_code(); Code_Invalid = make_code();
Code::Invalid.set_global(); set_global(Code_Invalid);
t_empty = (CodeType) make_code();
t_empty->Type = ECode::Typename;
t_empty->Name = get_cached_string( txt("") );
set_global(t_empty);
access_private = make_code();
access_private->Type = ECode::Access_Private;
access_private->Name = get_cached_string( txt("private:\n") );
set_global(access_private);
access_protected = make_code();
access_protected->Type = ECode::Access_Protected;
access_protected->Name = get_cached_string( txt("protected:\n") );
set_global(access_protected);
access_public = make_code();
access_public->Type = ECode::Access_Public;
access_public->Name = get_cached_string( txt("public:\n") );
set_global(access_public);
attrib_api_export = def_attributes( code(GEN_API_Export_Code));
set_global(attrib_api_export);
attrib_api_import = def_attributes( code(GEN_API_Import_Code));
set_global(attrib_api_import);
module_global_fragment = make_code();
module_global_fragment->Type = ECode::Untyped;
module_global_fragment->Name = get_cached_string( txt("module;") );
module_global_fragment->Content = module_global_fragment->Name;
set_global(module_global_fragment);
module_private_fragment = make_code();
module_private_fragment->Type = ECode::Untyped;
module_private_fragment->Name = get_cached_string( txt("module : private;") );
module_private_fragment->Content = module_private_fragment->Name;
set_global(module_private_fragment);
fmt_newline = make_code();
fmt_newline->Type = ECode::NewLine;
set_global(fmt_newline);
pragma_once = (CodePragma) make_code();
pragma_once->Type = ECode::Preprocess_Pragma;
pragma_once->Name = get_cached_string( txt("once") );
pragma_once->Content = pragma_once->Name;
set_global(pragma_once);
param_varadic = (CodeType) make_code();
param_varadic->Type = ECode::Parameters;
param_varadic->Name = get_cached_string( txt("...") );
param_varadic->ValueType = t_empty;
set_global(param_varadic);
preprocess_else = (CodePreprocessCond) make_code();
preprocess_else->Type = ECode::Preprocess_Else;
set_global(preprocess_else);
preprocess_endif = (CodePreprocessCond) make_code();
preprocess_endif->Type = ECode::Preprocess_EndIf;
set_global(preprocess_endif);
# define def_constant_code_type( Type_ ) \ # define def_constant_code_type( Type_ ) \
t_##Type_ = def_type( name(Type_) ); \ t_##Type_ = def_type( name(Type_) ); \
@ -97,88 +169,46 @@ void define_constants()
def_constant_code_type( u32 ); def_constant_code_type( u32 );
def_constant_code_type( u64 ); def_constant_code_type( u64 );
def_constant_code_type( sw ); def_constant_code_type( ssize );
def_constant_code_type( uw ); def_constant_code_type( usize );
def_constant_code_type( f32 ); def_constant_code_type( f32 );
def_constant_code_type( f64 ); def_constant_code_type( f64 );
#endif #endif
# undef def_constant_code_type # undef def_constant_code_type
t_empty = (CodeType) make_code();
t_empty->Type = ECode::Typename;
t_empty->Name = get_cached_string( txt_StrC("") );
t_empty.set_global();
param_varadic = (CodeType) make_code();
param_varadic->Type = ECode::Parameters;
param_varadic->Name = get_cached_string( txt_StrC("...") );
param_varadic->ValueType = t_empty;
param_varadic.set_global();
attrib_api_export = def_attributes( code(GEN_API_Export_Code));
attrib_api_export.set_global();
attrib_api_import = def_attributes( code(GEN_API_Import_Code));
attrib_api_import.set_global();
access_private = make_code();
access_private->Type = ECode::Access_Private;
access_private->Name = get_cached_string( txt_StrC("private:") );
access_private.set_global();
access_protected = make_code();
access_protected->Type = ECode::Access_Protected;
access_protected->Name = get_cached_string( txt_StrC("protected:") );
access_protected.set_global();
access_public = make_code();
access_public->Type = ECode::Access_Public;
access_public->Name = get_cached_string( txt_StrC("public:") );
access_public.set_global();
module_global_fragment = make_code();
module_global_fragment->Type = ECode::Untyped;
module_global_fragment->Name = get_cached_string( txt_StrC("module;") );
module_global_fragment->Content = module_global_fragment->Name;
module_global_fragment.set_global();
module_private_fragment = make_code();
module_private_fragment->Type = ECode::Untyped;
module_private_fragment->Name = get_cached_string( txt_StrC("module : private;") );
module_private_fragment->Content = module_private_fragment->Name;
module_private_fragment.set_global();
pragma_once = make_code();
pragma_once->Type = ECode::Untyped;
pragma_once->Name = get_cached_string( txt_StrC("#pragma once") );
pragma_once->Content = pragma_once->Name;
pragma_once.set_global();
# pragma push_macro( "global" )
# pragma push_macro( "internal" )
# pragma push_macro( "local_persist" )
# undef global
# undef internal
# undef local_persist
# define def_constant_spec( Type_, ... ) \ # define def_constant_spec( Type_, ... ) \
spec_##Type_ = def_specifiers( num_args(__VA_ARGS__), __VA_ARGS__); \ spec_##Type_ = def_specifiers( num_args(__VA_ARGS__), __VA_ARGS__); \
spec_##Type_.set_global(); set_global(spec_##Type_);
# pragma push_macro("forceinline")
# pragma push_macro("global")
# pragma push_macro("internal")
# pragma push_macro("local_persist")
# pragma push_macro("neverinline")
# undef forceinline
# undef global
# undef internal
# undef local_persist
# undef neverinline
def_constant_spec( const, ESpecifier::Const ); def_constant_spec( const, ESpecifier::Const );
def_constant_spec( consteval, ESpecifier::Consteval ); def_constant_spec( consteval, ESpecifier::Consteval );
def_constant_spec( constexpr, ESpecifier::Constexpr ); def_constant_spec( constexpr, ESpecifier::Constexpr );
def_constant_spec( constinit, ESpecifier::Constinit ); def_constant_spec( constinit, ESpecifier::Constinit );
def_constant_spec( extern_linkage, ESpecifier::External_Linkage ); def_constant_spec( extern_linkage, ESpecifier::External_Linkage );
def_constant_spec( final, ESpecifier::Final ); def_constant_spec( final, ESpecifier::Final );
def_constant_spec( forceinline, ESpecifier::ForceInline );
def_constant_spec( global, ESpecifier::Global ); def_constant_spec( global, ESpecifier::Global );
def_constant_spec( inline, ESpecifier::Inline ); def_constant_spec( inline, ESpecifier::Inline );
def_constant_spec( internal_linkage, ESpecifier::Internal_Linkage ); def_constant_spec( internal_linkage, ESpecifier::Internal_Linkage );
def_constant_spec( local_persist, ESpecifier::Local_Persist ); def_constant_spec( local_persist, ESpecifier::Local_Persist );
def_constant_spec( mutable, ESpecifier::Mutable ); def_constant_spec( mutable, ESpecifier::Mutable );
def_constant_spec( neverinline, ESpecifier::NeverInline );
def_constant_spec( noexcept, ESpecifier::NoExceptions );
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 );
@ -188,11 +218,17 @@ void define_constants()
def_constant_spec( volatile, ESpecifier::Volatile) def_constant_spec( volatile, ESpecifier::Volatile)
spec_local_persist = def_specifiers( 1, ESpecifier::Local_Persist ); spec_local_persist = def_specifiers( 1, ESpecifier::Local_Persist );
spec_local_persist.set_global(); set_global(spec_local_persist);
# pragma pop_macro( "global" ) # pragma pop_macro("forceinline")
# pragma pop_macro( "internal" ) # pragma pop_macro("global")
# pragma pop_macro( "local_persist" ) # pragma pop_macro("internal")
# pragma pop_macro("local_persist")
# pragma pop_macro("neverinline")
# pragma push_macro("enum_underlying")
# pragma pop_macro("enum_underlying")
# undef def_constant_spec # undef def_constant_spec
} }
@ -203,119 +239,125 @@ void init()
{ {
GlobalAllocator = AllocatorInfo { & Global_Allocator_Proc, nullptr }; GlobalAllocator = AllocatorInfo { & Global_Allocator_Proc, nullptr };
Global_AllocatorBuckets = Array<Arena>::init_reserve( heap(), 128 ); Global_AllocatorBuckets = array_init_reserve<Arena>( heap(), 128 );
if ( Global_AllocatorBuckets == nullptr ) if ( Global_AllocatorBuckets == nullptr )
fatal( "Failed to reserve memory for Global_AllocatorBuckets"); GEN_FATAL( "Failed to reserve memory for Global_AllocatorBuckets");
Arena bucket = Arena::init_from_allocator( heap(), Global_BucketSize ); Arena bucket = arena_init_from_allocator( heap(), Global_BucketSize );
if ( bucket.PhysicalStart == nullptr ) if ( bucket.PhysicalStart == nullptr )
fatal( "Failed to create first bucket for Global_AllocatorBuckets"); GEN_FATAL( "Failed to create first bucket for Global_AllocatorBuckets");
Global_AllocatorBuckets.append( bucket );
append( & Global_AllocatorBuckets, bucket );
} }
// Setup the arrays // Setup the arrays
{ {
CodePools = Array<Pool>::init_reserve( Allocator_DataArrays, InitSize_DataArrays ); CodePools = array_init_reserve<Pool>( Allocator_DataArrays, InitSize_DataArrays );
if ( CodePools == nullptr ) if ( CodePools == nullptr )
fatal( "gen::init: Failed to initialize the CodePools array" ); GEN_FATAL( "gen::init: Failed to initialize the CodePools array" );
StringArenas = Array<Arena>::init_reserve( Allocator_DataArrays, InitSize_DataArrays ); StringArenas = array_init_reserve<Arena>( Allocator_DataArrays, InitSize_DataArrays );
if ( StringArenas == nullptr ) if ( StringArenas == nullptr )
fatal( "gen::init: Failed to initialize the StringArenas array" ); GEN_FATAL( "gen::init: Failed to initialize the StringArenas array" );
} }
// Setup the code pool and code entries arena. // Setup the code pool and code entries arena.
{ {
Pool code_pool = Pool::init( Allocator_CodePool, CodePool_NumBlocks, sizeof(AST) ); Pool code_pool = pool_init( Allocator_CodePool, CodePool_NumBlocks, sizeof(AST) );
if ( code_pool.PhysicalStart == nullptr ) if ( code_pool.PhysicalStart == nullptr )
fatal( "gen::init: Failed to initialize the code pool" ); GEN_FATAL( "gen::init: Failed to initialize the code pool" );
CodePools.append( code_pool ); append( & CodePools, code_pool );
LexArena = Arena::init_from_allocator( Allocator_Lexer, LexAllocator_Size ); LexArena = arena_init_from_allocator( Allocator_Lexer, LexAllocator_Size );
Arena string_arena = Arena::init_from_allocator( Allocator_StringArena, SizePer_StringArena ); Arena string_arena = arena_init_from_allocator( Allocator_StringArena, SizePer_StringArena );
if ( string_arena.PhysicalStart == nullptr ) if ( string_arena.PhysicalStart == nullptr )
fatal( "gen::init: Failed to initialize the string arena" ); GEN_FATAL( "gen::init: Failed to initialize the string arena" );
StringArenas.append( string_arena ); append( & StringArenas, string_arena );
} }
// Setup the hash tables // Setup the hash tables
{ {
StringCache = StringTable::init( Allocator_StringTable ); StringCache = hashtable_init<StringCached>(Allocator_StringTable);
if ( StringCache.Entries == nullptr ) if ( StringCache.Entries == nullptr )
fatal( "gen::init: Failed to initialize the StringCache"); GEN_FATAL( "gen::init: Failed to initialize the StringCache");
} }
// Preprocessor Defines
PreprocessorDefines = array_init_reserve<StringCached>( GlobalAllocator, kilobytes(1) );
define_constants(); define_constants();
parser::init();
} }
void deinit() void deinit()
{ {
uw index = 0; usize index = 0;
uw left = CodePools.num(); usize left = num(CodePools);
do do
{ {
Pool* code_pool = & CodePools[index]; Pool* code_pool = & CodePools[index];
code_pool->free(); free(code_pool);
index++; index++;
} }
while ( left--, left ); while ( left--, left );
index = 0; index = 0;
left = StringArenas.num(); left = num(StringArenas);
do do
{ {
Arena* string_arena = & StringArenas[index]; Arena* string_arena = & StringArenas[index];
string_arena->free(); free(string_arena);
index++; index++;
} }
while ( left--, left ); while ( left--, left );
StringCache.destroy(); destroy(& StringCache);
CodePools.free(); free( & CodePools);
StringArenas.free(); free( & StringArenas);
LexArena.free(); free(& LexArena);
free(& PreprocessorDefines);
index = 0; index = 0;
left = Global_AllocatorBuckets.num(); left = num(Global_AllocatorBuckets);
do do
{ {
Arena* bucket = & Global_AllocatorBuckets[ index ]; Arena* bucket = & Global_AllocatorBuckets[ index ];
bucket->free(); free(bucket);
index++; index++;
} }
while ( left--, left ); while ( left--, left );
Global_AllocatorBuckets.free(); free(Global_AllocatorBuckets);
parser::deinit();
} }
void reset() void reset()
{ {
s32 index = 0; s32 index = 0;
s32 left = CodePools.num(); s32 left = num(CodePools);
do do
{ {
Pool* code_pool = & CodePools[index]; Pool* code_pool = & CodePools[index];
code_pool->clear(); clear(* code_pool);
index++; index++;
} }
while ( left--, left ); while ( left--, left );
index = 0; index = 0;
left = StringArenas.num(); left = num(StringArenas);
do do
{ {
Arena* string_arena = & StringArenas[index]; Arena* string_arena = & StringArenas[index];
@ -324,28 +366,28 @@ void reset()
} }
while ( left--, left ); while ( left--, left );
StringCache.clear(); clear(StringCache);
define_constants(); define_constants();
} }
AllocatorInfo get_string_allocator( s32 str_length ) AllocatorInfo get_string_allocator( s32 str_length )
{ {
Arena* last = & StringArenas.back(); Arena* last = back(& StringArenas);
uw size_req = str_length + sizeof(String::Header) + sizeof(char*); usize size_req = str_length + sizeof(StringHeader) + sizeof(char*);
if ( last->TotalUsed + size_req > last->TotalSize ) if ( last->TotalUsed + ssize(size_req) > last->TotalSize )
{ {
Arena new_arena = Arena::init_from_allocator( Allocator_StringArena, SizePer_StringArena ); Arena new_arena = arena_init_from_allocator( Allocator_StringArena, SizePer_StringArena );
if ( ! StringArenas.append( new_arena ) ) if ( ! append( & StringArenas, new_arena ) )
fatal( "gen::get_string_allocator: Failed to allocate a new string arena" ); GEN_FATAL( "gen::get_string_allocator: Failed to allocate a new string arena" );
last = & StringArenas.back(); last = back(& StringArenas);
} }
return * last; return allocator_info(last);
} }
// Will either make or retrive a code string. // Will either make or retrive a code string.
@ -354,47 +396,48 @@ StringCached get_cached_string( StrC str )
s32 hash_length = str.Len > kilobytes(1) ? kilobytes(1) : str.Len; s32 hash_length = str.Len > kilobytes(1) ? kilobytes(1) : str.Len;
u64 key = crc32( str.Ptr, hash_length ); u64 key = crc32( str.Ptr, hash_length );
{ {
StringCached* result = StringCache.get( key ); StringCached* result = get(StringCache, key );
if ( result ) if ( result )
return * result; return * result;
} }
String result = String::make( get_string_allocator( str.Len ), str ); String result = string_make( get_string_allocator( str.Len ), str );
StringCache.set( key, result ); set<StringCached>(& StringCache, key, result );
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 = back( & CodePools);
if ( allocator->FreeList == nullptr ) if ( allocator->FreeList == nullptr )
{ {
Pool code_pool = Pool::init( Allocator_CodePool, CodePool_NumBlocks, sizeof(AST) ); Pool code_pool = pool_init( Allocator_CodePool, CodePool_NumBlocks, sizeof(AST) );
if ( code_pool.PhysicalStart == nullptr ) if ( code_pool.PhysicalStart == nullptr )
fatal( "gen::make_code: Failed to allocate a new code pool - CodePool allcoator returned nullptr." ); GEN_FATAL( "gen::make_code: Failed to allocate a new code pool - CodePool allcoator returned nullptr." );
if ( ! CodePools.append( code_pool ) ) if ( ! append( & CodePools, code_pool ) )
fatal( "gen::make_code: Failed to allocate a new code pool - CodePools failed to append new pool." ); GEN_FATAL( "gen::make_code: Failed to allocate a new code pool - CodePools failed to append new pool." );
allocator = & CodePools.back(); allocator = back( & CodePools);
} }
Code result { rcast( AST*, alloc( * allocator, sizeof(AST) )) }; Code result { rcast( AST*, alloc( allocator_info(allocator), sizeof(AST) )) };
mem_set( result.ast, 0, sizeof(AST) );
// result->Type = ECode::Invalid;
result->Content = { nullptr }; // result->Content = { nullptr };
result->Prev = { nullptr }; // result->Prev = { nullptr };
result->Next = { nullptr }; // result->Next = { nullptr };
result->Parent = { nullptr }; // result->Token = nullptr;
result->Name = { nullptr }; // result->Parent = { nullptr };
result->Type = ECode::Invalid; // result->Name = { nullptr };
result->ModuleFlags = ModuleFlag::Invalid; // result->Type = ECode::Invalid;
result->NumEntries = 0; // result->ModuleFlags = ModuleFlag::Invalid;
// result->NumEntries = 0;
return result; return result;
} }

View File

@ -1,3 +1,8 @@
#ifdef GEN_INTELLISENSE_DIRECTIVES
#pragma once
#include "ast_types.hpp"
#endif
#pragma region Gen Interface #pragma region Gen Interface
// Initialize the library. // Initialize the library.
@ -24,7 +29,6 @@ StringCached get_cached_string( StrC str );
Code make_code(); Code make_code();
// Set these before calling gen's init() procedure. // Set these before calling gen's init() procedure.
// Data
void set_allocator_data_arrays ( AllocatorInfo data_array_allocator ); void set_allocator_data_arrays ( AllocatorInfo data_array_allocator );
void set_allocator_code_pool ( AllocatorInfo pool_allocator ); void set_allocator_code_pool ( AllocatorInfo pool_allocator );
@ -40,15 +44,21 @@ CodeComment def_comment ( StrC content );
CodeClass def_class( StrC name CodeClass def_class( StrC name
, Code body = NoCode , Code body = NoCode
, CodeType parent = NoCode, AccessSpec access = AccessSpec::Default , CodeType parent = NoCode, AccessSpec access = AccessSpec_Default
, CodeAttributes attributes = NoCode , CodeAttributes attributes = NoCode
, 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 );
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 = EnumDecl_Regular, CodeAttributes attributes = NoCode
, ModuleFlag mflags = ModuleFlag::None ); , ModuleFlag mflags = ModuleFlag_None );
CodeExec def_execution ( StrC content ); CodeExec def_execution ( StrC content );
CodeExtern def_extern_link( StrC name, Code body ); CodeExtern def_extern_link( StrC name, Code body );
@ -57,45 +67,49 @@ CodeFriend def_friend ( Code symbol );
CodeFn def_function( StrC name CodeFn def_function( StrC name
, CodeParam params = NoCode, CodeType ret_type = NoCode, Code body = NoCode , CodeParam params = NoCode, CodeType ret_type = NoCode, Code body = NoCode
, CodeSpecifiers specifiers = NoCode, CodeAttributes attributes = NoCode , CodeSpecifiers specifiers = NoCode, CodeAttributes attributes = NoCode
, ModuleFlag mflags = ModuleFlag::None ); , ModuleFlag mflags = ModuleFlag_None );
CodeInclude def_include ( StrC content ); CodeInclude def_include ( StrC content, bool foreign = false );
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 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
, CodeSpecifiers specifiers = NoCode, CodeAttributes attributes = NoCode , CodeSpecifiers specifiers = NoCode, CodeAttributes attributes = NoCode
, ModuleFlag mflags = ModuleFlag::None ); , ModuleFlag mflags = ModuleFlag_None );
CodeOpCast def_operator_cast( CodeType type, Code body = NoCode, CodeSpecifiers specs = NoCode ); CodeOpCast def_operator_cast( CodeType type, Code body = NoCode, CodeSpecifiers specs = NoCode );
CodeParam def_param ( CodeType type, StrC name, Code value = NoCode ); CodeParam def_param ( CodeType type, StrC name, Code value = NoCode );
CodePragma def_pragma( StrC directive );
CodePreprocessCond def_preprocess_cond( EPreprocessCond type, StrC content );
CodeSpecifiers def_specifier( SpecifierT specifier ); CodeSpecifiers def_specifier( SpecifierT specifier );
CodeStruct def_struct( StrC name CodeStruct def_struct( StrC name
, Code body = NoCode , Code body = NoCode
, CodeType parent = NoCode, AccessSpec access = AccessSpec::Default , CodeType parent = NoCode, AccessSpec access = AccessSpec_Default
, CodeAttributes attributes = NoCode , CodeAttributes attributes = NoCode
, ModuleFlag mflags = ModuleFlag::None , ModuleFlag mflags = ModuleFlag_None
, CodeType* interfaces = nullptr, s32 num_interfaces = 0 ); , CodeType* interfaces = nullptr, s32 num_interfaces = 0 );
CodeTemplate def_template( CodeParam params, Code definition, ModuleFlag mflags = ModuleFlag::None ); CodeTemplate def_template( CodeParam params, Code definition, ModuleFlag mflags = ModuleFlag_None );
CodeType def_type ( StrC name, Code arrayexpr = NoCode, CodeSpecifiers specifiers = NoCode, CodeAttributes attributes = NoCode ); CodeType def_type ( StrC name, Code arrayexpr = NoCode, CodeSpecifiers specifiers = NoCode, CodeAttributes attributes = NoCode );
CodeTypedef def_typedef( StrC name, Code type, CodeAttributes attributes = NoCode, ModuleFlag mflags = ModuleFlag::None ); CodeTypedef def_typedef( StrC name, Code type, CodeAttributes attributes = NoCode, ModuleFlag mflags = ModuleFlag_None );
CodeUnion def_union( StrC name, Code body, CodeAttributes attributes = NoCode, ModuleFlag mflags = ModuleFlag::None ); CodeUnion def_union( StrC name, Code body, CodeAttributes attributes = NoCode, ModuleFlag mflags = ModuleFlag_None );
CodeUsing def_using( StrC name, CodeType type = NoCode CodeUsing def_using( StrC name, CodeType type = NoCode
, CodeAttributes attributess = NoCode , CodeAttributes attributess = NoCode
, ModuleFlag mflags = ModuleFlag::None ); , ModuleFlag mflags = ModuleFlag_None );
CodeUsing def_using_namespace( StrC name ); CodeUsing def_using_namespace( StrC name );
CodeVar def_variable( CodeType type, StrC name, Code value = NoCode CodeVar def_variable( CodeType type, StrC name, Code value = NoCode
, CodeSpecifiers specifiers = NoCode, CodeAttributes attributes = NoCode , CodeSpecifiers specifiers = NoCode, CodeAttributes attributes = NoCode
, ModuleFlag mflags = ModuleFlag::None ); , ModuleFlag mflags = ModuleFlag_None );
// Constructs an empty body. Use AST::validate_body() to check if the body is was has valid entries. // Constructs an empty body. Use AST::validate_body() to check if the body is was has valid entries.
CodeBody def_body( CodeT type ); CodeBody def_body( CodeT type );
@ -130,30 +144,69 @@ CodeBody def_union_body ( s32 num, Code* codes );
#pragma region Parsing #pragma region Parsing
CodeClass parse_class ( StrC class_def ); // TODO(Ed) : Implmeent the new parser API design.
CodeEnum parse_enum ( StrC enum_def );
CodeBody parse_export_body ( StrC export_def ); #if 0
CodeExtern parse_extern_link ( StrC exten_link_def); namespace parser {
CodeFriend parse_friend ( StrC friend_def ); struct StackNode
CodeFn parse_function ( StrC fn_def ); {
CodeBody parse_global_body ( StrC body_def ); StackNode* Prev;
CodeNamespace parse_namespace ( StrC namespace_def );
CodeOperator parse_operator ( StrC operator_def ); Token Start;
CodeOpCast parse_operator_cast( StrC operator_def ); Token Name; // The name of the AST node (if parsed)
CodeStruct parse_struct ( StrC struct_def ); StrC FailedProc; // The name of the procedure that failed
CodeTemplate parse_template ( StrC template_def ); };
CodeType parse_type ( StrC type_def ); // Stack nodes are allocated the error's allocator
CodeTypedef parse_typedef ( StrC typedef_def );
CodeUnion parse_union ( StrC union_def ); struct Error
CodeUsing parse_using ( StrC using_def ); {
CodeVar parse_variable ( StrC var_def ); String message;
StackNode* context_stack;
};
}
struct ParseInfo
{
Arena FileMem;
Arena TokMem;
Arena CodeMem;
FileContents FileContent;
Array<parser::Token> Tokens;
Array<parser::Error> Errors;
// Errors are allocated to a dedicated general arena.
};
CodeBody parse_file( StrC path );
#endif
CodeClass parse_class ( StrC class_def );
CodeConstructor parse_constructor ( StrC constructor_def );
CodeDestructor parse_destructor ( StrC destructor_def );
CodeEnum parse_enum ( StrC enum_def );
CodeBody parse_export_body ( StrC export_def );
CodeExtern parse_extern_link ( StrC exten_link_def );
CodeFriend parse_friend ( StrC friend_def );
CodeFn parse_function ( StrC fn_def );
CodeBody parse_global_body ( StrC body_def );
CodeNS parse_namespace ( StrC namespace_def );
CodeOperator parse_operator ( StrC operator_def );
CodeOpCast parse_operator_cast( StrC operator_def );
CodeStruct parse_struct ( StrC struct_def );
CodeTemplate parse_template ( StrC template_def );
CodeType parse_type ( StrC type_def );
CodeTypedef parse_typedef ( StrC typedef_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 ); ssize token_fmt_va( char* buf, usize buf_size, s32 num_tokens, va_list va );
StrC token_fmt_impl( sw, ... ); //! Do not use directly. Use the token_fmt macro instead.
StrC token_fmt_impl( ssize, ... );
Code untyped_str ( StrC content); Code untyped_str ( StrC content);
Code untyped_fmt ( char const* fmt, ... ); Code untyped_fmt ( char const* fmt, ... );

File diff suppressed because it is too large Load Diff

View File

@ -1,7 +1,12 @@
sw token_fmt_va( char* buf, uw buf_size, s32 num_tokens, va_list va ) #ifdef GEN_INTELLISENSE_DIRECTIVES
#pragma once
#include "interface.parsing.cpp"
#endif
ssize token_fmt_va( char* buf, usize buf_size, s32 num_tokens, va_list va )
{ {
char const* buf_begin = buf; char const* buf_begin = buf;
sw remaining = buf_size; ssize remaining = buf_size;
local_persist local_persist
Arena tok_map_arena; Arena tok_map_arena;
@ -11,8 +16,8 @@ sw token_fmt_va( char* buf, uw buf_size, s32 num_tokens, va_list va )
local_persist local_persist
char tok_map_mem[ TokenFmt_TokenMap_MemSize ]; char tok_map_mem[ TokenFmt_TokenMap_MemSize ];
tok_map_arena = Arena::init_from_memory( tok_map_mem, sizeof(tok_map_mem) ); tok_map_arena = arena_init_from_memory( tok_map_mem, sizeof(tok_map_mem) );
tok_map = HashTable<StrC>::init( tok_map_arena ); tok_map = hashtable_init<StrC>( allocator_info(& tok_map_arena) );
s32 left = num_tokens - 1; s32 left = num_tokens - 1;
@ -22,8 +27,7 @@ sw token_fmt_va( char* buf, uw buf_size, s32 num_tokens, va_list va )
StrC value = va_arg( va, StrC ); StrC value = va_arg( va, StrC );
u32 key = crc32( token, str_len(token) ); u32 key = crc32( token, str_len(token) );
set(& tok_map, key, value );
tok_map.set( key, value );
} }
} }
@ -32,7 +36,7 @@ sw token_fmt_va( char* buf, uw buf_size, s32 num_tokens, va_list va )
while ( current ) while ( current )
{ {
sw len = 0; ssize len = 0;
while ( current && current != '<' && remaining ) while ( current && current != '<' && remaining )
{ {
@ -59,11 +63,11 @@ sw token_fmt_va( char* buf, uw buf_size, s32 num_tokens, va_list va )
char const* token = fmt + 1; char const* token = fmt + 1;
u32 key = crc32( token, tok_len ); u32 key = crc32( token, tok_len );
StrC* value = tok_map.get( key ); StrC* value = get(tok_map, key );
if ( value ) if ( value )
{ {
sw left = value->Len; ssize left = value->Len;
char const* str = value->Ptr; char const* str = value->Ptr;
while ( left-- ) while ( left-- )
@ -89,33 +93,51 @@ sw token_fmt_va( char* buf, uw buf_size, s32 num_tokens, va_list va )
} }
} }
tok_map.clear(); clear(tok_map);
tok_map_arena.free(); free(& tok_map_arena);
sw result = buf_size - remaining; ssize result = buf_size - remaining;
return result; return result;
} }
Code untyped_str( StrC content ) Code untyped_str( StrC content )
{ {
if ( content.Len == 0 )
{
log_failure( "untyped_str: empty string" );
return InvalidCode;
}
Code Code
result = make_code(); result = make_code();
result->Name = get_cached_string( content ); result->Name = get_cached_string( content );
result->Type = ECode::Untyped; result->Type = ECode::Untyped;
result->Content = result->Name; result->Content = result->Name;
if ( result->Name == nullptr )
{
log_failure( "untyped_str: could not cache string" );
return InvalidCode;
}
return result; return result;
} }
Code untyped_fmt( char const* fmt, ...) Code untyped_fmt( char const* fmt, ...)
{ {
if ( fmt == nullptr )
{
log_failure( "untyped_fmt: null format string" );
return InvalidCode;
}
local_persist thread_local local_persist thread_local
char buf[GEN_PRINTF_MAXLEN] = { 0 }; char buf[GEN_PRINTF_MAXLEN] = { 0 };
va_list va; va_list va;
va_start(va, fmt); va_start(va, fmt);
sw length = str_fmt_va(buf, GEN_PRINTF_MAXLEN, fmt, va); ssize length = str_fmt_va(buf, GEN_PRINTF_MAXLEN, fmt, va);
va_end(va); va_end(va);
Code Code
@ -124,17 +146,29 @@ Code untyped_fmt( char const* fmt, ...)
result->Type = ECode::Untyped; result->Type = ECode::Untyped;
result->Content = get_cached_string( { length, buf } ); result->Content = get_cached_string( { length, buf } );
if ( result->Name == nullptr )
{
log_failure( "untyped_fmt: could not cache string" );
return InvalidCode;
}
return result; return result;
} }
Code untyped_token_fmt( s32 num_tokens, ... ) Code untyped_token_fmt( s32 num_tokens, ... )
{ {
if ( num_tokens == 0 )
{
log_failure( "untyped_token_fmt: zero tokens" );
return InvalidCode;
}
local_persist thread_local local_persist thread_local
char buf[GEN_PRINTF_MAXLEN] = { 0 }; char buf[GEN_PRINTF_MAXLEN] = { 0 };
va_list va; va_list va;
va_start(va, num_tokens); va_start(va, num_tokens);
sw length = token_fmt_va(buf, GEN_PRINTF_MAXLEN, num_tokens, va); ssize length = token_fmt_va(buf, GEN_PRINTF_MAXLEN, num_tokens, va);
va_end(va); va_end(va);
Code Code
@ -143,5 +177,11 @@ Code untyped_token_fmt( s32 num_tokens, ... )
result->Type = ECode::Untyped; result->Type = ECode::Untyped;
result->Content = result->Name; result->Content = result->Name;
if ( result->Name == nullptr )
{
log_failure( "untyped_fmt: could not cache string" );
return InvalidCode;
}
return result; return result;
} }

File diff suppressed because it is too large Load Diff

1271
project/components/lexer.cpp Normal file

File diff suppressed because it is too large Load Diff

File diff suppressed because it is too large Load Diff

View File

@ -2,6 +2,8 @@
# 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

View File

@ -1,9 +1,15 @@
#ifdef GEN_INTELLISENSE_DIRECTIVES
#pragma once
#include "gen.hpp"
#endif
#pragma region StaticData #pragma region StaticData
// TODO : Convert global allocation strategy to use a slab allocation strategy. // TODO : Convert global allocation strategy to use a slab allocation strategy.
global AllocatorInfo GlobalAllocator; global AllocatorInfo GlobalAllocator;
global Array<Arena> Global_AllocatorBuckets; global Array<Arena> Global_AllocatorBuckets;
// TODO(Ed) : Make the code pool a dynamic arena
global Array< Pool > CodePools = { nullptr }; global Array< Pool > CodePools = { nullptr };
global Array< Arena > StringArenas = { nullptr }; global Array< Arena > StringArenas = { nullptr };
@ -22,6 +28,50 @@ global AllocatorInfo Allocator_TypeTable = heap();
#pragma region Constants #pragma region Constants
global Code access_public;
global Code access_protected;
global Code access_private;
global CodeAttributes attrib_api_export;
global CodeAttributes attrib_api_import;
global Code module_global_fragment;
global Code module_private_fragment;
global Code fmt_newline;
global CodeParam param_varadic;
global CodePragma pragma_once;
global CodePreprocessCond preprocess_else;
global CodePreprocessCond preprocess_endif;
global CodeSpecifiers spec_const;
global CodeSpecifiers spec_consteval;
global CodeSpecifiers spec_constexpr;
global CodeSpecifiers spec_constinit;
global CodeSpecifiers spec_extern_linkage;
global CodeSpecifiers spec_final;
global CodeSpecifiers spec_forceinline;
global CodeSpecifiers spec_global;
global CodeSpecifiers spec_inline;
global CodeSpecifiers spec_internal_linkage;
global CodeSpecifiers spec_local_persist;
global CodeSpecifiers spec_mutable;
global CodeSpecifiers spec_noexcept;
global CodeSpecifiers spec_neverinline;
global CodeSpecifiers spec_override;
global CodeSpecifiers spec_ptr;
global CodeSpecifiers spec_pure;
global CodeSpecifiers spec_ref;
global CodeSpecifiers spec_register;
global CodeSpecifiers spec_rvalue;
global CodeSpecifiers spec_static_member;
global CodeSpecifiers spec_thread_local;
global CodeSpecifiers spec_virtual;
global CodeSpecifiers spec_volatile;
global CodeType t_empty; global CodeType t_empty;
global CodeType t_auto; global CodeType t_auto;
global CodeType t_void; global CodeType t_void;
@ -32,6 +82,8 @@ global CodeType t_wchar_t;
global CodeType t_class; global CodeType t_class;
global CodeType t_typename; global CodeType t_typename;
global Array< StringCached > PreprocessorDefines;
#ifdef GEN_DEFINE_LIBRARY_CODE_CONSTANTS #ifdef GEN_DEFINE_LIBRARY_CODE_CONSTANTS
global CodeType t_b32; global CodeType t_b32;
@ -45,46 +97,11 @@ global CodeType t_u16;
global CodeType t_u32; global CodeType t_u32;
global CodeType t_u64; global CodeType t_u64;
global CodeType t_sw; global CodeType t_ssize;
global CodeType t_uw; global CodeType t_usize;
global CodeType t_f32; global CodeType t_f32;
global CodeType t_f64; global CodeType t_f64;
#endif #endif
global CodeParam param_varadic;
global CodeAttributes attrib_api_export;
global CodeAttributes attrib_api_import;
global Code access_public;
global Code access_protected;
global Code access_private;
global Code module_global_fragment;
global Code module_private_fragment;
global Code pragma_once;
global CodeSpecifiers spec_const;
global CodeSpecifiers spec_consteval;
global CodeSpecifiers spec_constexpr;
global CodeSpecifiers spec_constinit;
global CodeSpecifiers spec_extern_linkage;
global CodeSpecifiers spec_final;
global CodeSpecifiers spec_global;
global CodeSpecifiers spec_inline;
global CodeSpecifiers spec_internal_linkage;
global CodeSpecifiers spec_local_persist;
global CodeSpecifiers spec_mutable;
global CodeSpecifiers spec_override;
global CodeSpecifiers spec_ptr;
global CodeSpecifiers spec_ref;
global CodeSpecifiers spec_register;
global CodeSpecifiers spec_rvalue;
global CodeSpecifiers spec_static_member;
global CodeSpecifiers spec_thread_local;
global CodeSpecifiers spec_virtual;
global CodeSpecifiers spec_volatile;
#pragma endregion Constants #pragma endregion Constants

View File

@ -1,102 +1,113 @@
using LogFailType = sw(*)(char const*, ...); #ifdef GEN_INTELLISENSE_DIRECTIVES
#pragma once
// By default this library will either crash or exit if an error is detected while generating codes. #include "header_start.hpp"
// 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
constexpr LogFailType log_failure = log_fmt;
#else
constexpr LogFailType log_failure = fatal;
#endif #endif
// Used to indicate if enum definitoin is an enum class or regular enum. using LogFailType = ssize(*)(char const*, ...);
enum class EnumT : u8
// By default this library will either crash or exit if an error is detected while generating codes.
// Even if set to not use GEN_FATAL, GEN_FATAL will still be used for memory failures as the library is unusable when they occur.
#ifdef GEN_DONT_USE_FATAL
#define log_failure log_fmt
#else
#define log_failure GEN_FATAL
#endif
enum AccessSpec enum_underlying(u32)
{ {
Regular, AccessSpec_Default,
Class AccessSpec_Private,
}; AccessSpec_Protected,
AccessSpec_Public,
constexpr EnumT EnumClass = EnumT::Class;
constexpr EnumT EnumRegular = EnumT::Regular; AccessSpec_Num_AccessSpec,
AccessSpec_Invalid,
enum class AccessSpec : u32
{ AccessSpec_SizeDef = GEN_U32_MAX,
Default,
Public,
Protected,
Private,
Num_AccessSpec,
Invalid,
}; };
static_assert( size_of(AccessSpec) == size_of(u32), "AccessSpec not u32 size" );
inline inline
char const* to_str( AccessSpec type ) char const* to_str( AccessSpec type )
{ {
local_persist local_persist
char const* lookup[ (u32)AccessSpec::Num_AccessSpec ] = { char const* lookup[ (u32)AccessSpec_Num_AccessSpec ] = {
"", "",
"public",
"protected",
"private", "private",
"protected",
"public",
}; };
if ( type > AccessSpec::Public ) if ( type > AccessSpec_Public )
return "Invalid"; return "Invalid";
return lookup[ (u32)type ]; return lookup[ (u32)type ];
} }
enum class ModuleFlag : u32 enum CodeFlag enum_underlying(u32)
{ {
None = 0, CodeFlag_None = 0,
Export = bit(0), CodeFlag_FunctionType = bit(0),
Import = bit(1), CodeFlag_ParamPack = bit(1),
// Private = bit(2), CodeFlag_Module_Export = bit(2),
CodeFlag_Module_Import = bit(3),
CodeFlag_SizeDef = GEN_U32_MAX,
};
static_assert( size_of(CodeFlag) == size_of(u32), "CodeFlag not u32 size" );
// Used to indicate if enum definitoin is an enum class or regular enum.
enum EnumDecl enum_underlying(u8)
{
EnumDecl_Regular,
EnumDecl_Class,
EnumT_SizeDef = GEN_U8_MAX,
};
typedef u8 EnumT;
enum ModuleFlag enum_underlying(u32)
{
ModuleFlag_None = 0,
ModuleFlag_Export = bit(0),
ModuleFlag_Import = bit(1),
Num_ModuleFlags, Num_ModuleFlags,
Invalid, ModuleFlag_Invalid,
};
ModuleFlag_SizeDef = GEN_U32_MAX,
};
static_assert( size_of(ModuleFlag) == size_of(u32), "ModuleFlag not u32 size" );
inline
StrC to_str( ModuleFlag flag )
{
local_persist
StrC lookup[ (u32)ModuleFlag::Num_ModuleFlags ] = {
{ sizeof("__none__"), "__none__" },
{ sizeof("export"), "export" },
{ sizeof("import"), "import" },
};
if ( flag > ModuleFlag_Import )
return { sizeof("invalid"), "invalid" };
return lookup[ (u32)flag ];
}
inline
ModuleFlag operator|( ModuleFlag A, ModuleFlag B) ModuleFlag operator|( ModuleFlag A, ModuleFlag B)
{ {
return (ModuleFlag)( (u32)A | (u32)B ); return (ModuleFlag)( (u32)A | (u32)B );
} }
/* enum EPreprocessCond enum_underlying(u32)
Predefined attributes {
Used for the parser constructors to identify non-standard attributes PreprocessCond_If,
PreprocessCond_IfDef,
PreprocessCond_IfNotDef,
PreprocessCond_ElIf,
Override these to change the attribute to your own unique identifier convention. EPreprocessCond_SizeDef = GEN_U32_MAX,
};
The tokenizer identifies attribute defines with the GEN_DEFINE_ATTRIBUTE_TOKENS macros. static_assert( size_of(EPreprocessCond) == size_of(u32), "EPreprocessCond not u32 size" );
See the example below and the Define_TokType macro used in gen.cpp to know the format.
While the library can parse raw attributes, most projects use defines to wrap them for compiler
platform indendence. The token define allows support for them without having to modify the library.
*/
#if defined(GEN_SYSTEM_WINDOWS) || defined( __CYGWIN__ )
#ifndef GEN_Attribute_Keyword
# define GEN_API_Export_Code __declspec(dllexport)
# define GEN_API_Import_Code __declspec(dllimport)
# define GEN_Attribute_Keyword __declspec
#endif
constexpr char const* Attribute_Keyword = stringize( GEN_Attribute_Keyword);
#elif GEN_HAS_ATTRIBUTE( visibility ) || GEN_GCC_VERSION_CHECK( 3, 3, 0 )
#ifndef GEN_Attribute_Keyword
# define GEN_API_Export_Code __attribute__ ((visibility ("default")))
# define GEN_API_Import_Code __attribute__ ((visibility ("default")))
# define GEN_Attribute_Keyword __attribute__
#endif
constexpr char const* Attribute_Keyword = stringize( GEN_Attribute_Keyword );
#else
#ifndef GEN_Attribute_Keyword
# define GEN_API_Export_Code
# define GEN_API_Import_Code
# define GEN_Attribute_Keyword
#endif
constexpr char const* Attribute_Keyword = "";
#endif

View File

@ -1,3 +1,9 @@
#ifdef GEN_INTELLISENSE_DIRECTIVES
# pragma once
# include "platform.hpp"
# include "macros.hpp"
#endif
#pragma region Basic Types #pragma region Basic Types
#define GEN_U8_MIN 0u #define GEN_U8_MIN 0u
@ -42,21 +48,21 @@
#if defined( GEN_COMPILER_MSVC ) #if defined( GEN_COMPILER_MSVC )
# if _MSC_VER < 1300 # if _MSC_VER < 1300
typedef unsigned char u8; typedef unsigned char u8;
typedef signed char s8; typedef signed char s8;
typedef unsigned short u16; typedef unsigned short u16;
typedef signed short s16; typedef signed short s16;
typedef unsigned int u32; typedef unsigned int u32;
typedef signed int s32; typedef signed int s32;
# else # else
typedef unsigned __int8 u8; typedef unsigned __int8 u8;
typedef signed __int8 s8; typedef signed __int8 s8;
typedef unsigned __int16 u16; typedef unsigned __int16 u16;
typedef signed __int16 s16; typedef signed __int16 s16;
typedef unsigned __int32 u32; typedef unsigned __int32 u32;
typedef signed __int32 s32; typedef signed __int32 s32;
# endif # endif
typedef unsigned __int64 u64; typedef unsigned __int64 u64;
typedef signed __int64 s64; typedef signed __int64 s64;
#else #else
# include <stdint.h> # include <stdint.h>
@ -80,10 +86,10 @@ static_assert( sizeof( u16 ) == 2, "sizeof(u16) != 2" );
static_assert( sizeof( u32 ) == 4, "sizeof(u32) != 4" ); static_assert( sizeof( u32 ) == 4, "sizeof(u32) != 4" );
static_assert( sizeof( u64 ) == 8, "sizeof(u64) != 8" ); static_assert( sizeof( u64 ) == 8, "sizeof(u64) != 8" );
typedef size_t uw; typedef size_t usize;
typedef ptrdiff_t sw; typedef ptrdiff_t ssize;
static_assert( sizeof( uw ) == sizeof( sw ), "sizeof(uw) != sizeof(sw)" ); static_assert( sizeof( usize ) == sizeof( ssize ), "sizeof(usize) != sizeof(ssize)" );
// NOTE: (u)zpl_intptr is only here for semantic reasons really as this library will only support 32/64 bit OSes. // NOTE: (u)zpl_intptr is only here for semantic reasons really as this library will only support 32/64 bit OSes.
#if defined( _WIN64 ) #if defined( _WIN64 )
@ -117,4 +123,21 @@ typedef s8 b8;
typedef s16 b16; typedef s16 b16;
typedef s32 b32; typedef s32 b32;
#pragma region Basic Types typedef void* mem_ptr;
typedef void const* mem_ptr_const ;
#if ! GEN_COMPILER_C
template<typename Type> uptr to_uptr( Type* ptr ) { return (uptr)ptr; }
template<typename Type> sptr to_sptr( Type* ptr ) { return (sptr)ptr; }
template<typename Type> mem_ptr to_mem_ptr ( Type ptr ) { return (mem_ptr) ptr; }
template<typename Type> mem_ptr_const to_mem_ptr_const( Type ptr ) { return (mem_ptr_const)ptr; }
#else
#define to_utpr( ptr ) ((uptr)(ptr))
#define to_stpr( ptr ) ((sptr)(ptr))
#define to_mem_ptr( ptr) ((mem_ptr)ptr)
#define to_mem_ptr_const( ptr) ((mem_ptr)ptr)
#endif
#pragma endregion Basic Types

File diff suppressed because it is too large Load Diff

View File

@ -1,4 +1,11 @@
#pragma endregion Debug #ifdef GEN_INTELLISENSE_DIRECTIVES
# pragma once
# include "debug.hpp"
# include "basic_types.hpp"
# include "src_start.cpp"
#endif
#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, ... )
{ {

View File

@ -1,4 +1,9 @@
#pragma endregion Debug #ifdef GEN_INTELLISENSE_DIRECTIVES
# pragma once
# include "basic_types.hpp"
#endif
#pragma region Debug
#if defined( _MSC_VER ) #if defined( _MSC_VER )
# if _MSC_VER < 1300 # if _MSC_VER < 1300
@ -19,7 +24,7 @@
{ \ { \
if ( ! ( cond ) ) \ if ( ! ( cond ) ) \
{ \ { \
assert_handler( #cond, __FILE__, zpl_cast( s64 ) __LINE__, msg, ##__VA_ARGS__ ); \ assert_handler( #cond, __FILE__, scast( s64, __LINE__ ), msg, ##__VA_ARGS__ ); \
GEN_DEBUG_TRAP(); \ GEN_DEBUG_TRAP(); \
} \ } \
} while ( 0 ) } while ( 0 )
@ -29,6 +34,28 @@
// NOTE: Things that shouldn't happen with a message! // NOTE: Things that shouldn't happen with a message!
#define GEN_PANIC( msg, ... ) GEN_ASSERT_MSG( 0, msg, ##__VA_ARGS__ ) #define GEN_PANIC( msg, ... ) GEN_ASSERT_MSG( 0, msg, ##__VA_ARGS__ )
#if Build_Debug
#define GEN_FATAL( ... ) \
do \
{ \
local_persist thread_local \
char buf[GEN_PRINTF_MAXLEN] = { 0 }; \
\
str_fmt(buf, GEN_PRINTF_MAXLEN, __VA_ARGS__); \
GEN_PANIC(buf); \
} \
while (0)
#else
# define GEN_FATAL( ... ) \
do \
{ \
str_fmt_out_err( __VA_ARGS__ ); \
process_exit(1); \
} \
while (0)
#endif
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, ... );
s32 assert_crash( char const* condition ); s32 assert_crash( char const* condition );
void process_exit( u32 code ); void process_exit( u32 code );

View File

@ -1,11 +1,17 @@
#ifdef GEN_INTELLISENSE_DIRECTIVES
# pragma once
# include "strings.cpp"
#endif
#pragma region File Handling #pragma region File Handling
#if defined( GEN_SYSTEM_WINDOWS ) || defined( GEN_SYSTEM_CYGWIN ) #if defined( GEN_SYSTEM_WINDOWS ) || defined( GEN_SYSTEM_CYGWIN )
internal wchar_t* _alloc_utf8_to_ucs2( AllocatorInfo a, char const* text, sw* w_len_ ) internal
wchar_t* _alloc_utf8_to_ucs2( AllocatorInfo a, char const* text, ssize* w_len_ )
{ {
wchar_t* w_text = NULL; wchar_t* w_text = NULL;
sw len = 0, w_len = 0, w_len1 = 0; ssize len = 0, w_len = 0, w_len1 = 0;
if ( text == NULL ) if ( text == NULL )
{ {
if ( w_len_ ) if ( w_len_ )
@ -19,7 +25,7 @@ internal wchar_t* _alloc_utf8_to_ucs2( AllocatorInfo a, char const* text, sw* w_
*w_len_ = w_len; *w_len_ = w_len;
return NULL; return NULL;
} }
w_len = MultiByteToWideChar( CP_UTF8, MB_ERR_INVALID_CHARS, text, zpl_cast( int ) len, NULL, 0 ); w_len = MultiByteToWideChar( CP_UTF8, MB_ERR_INVALID_CHARS, text, scast( int, len), NULL, 0 );
if ( w_len == 0 ) if ( w_len == 0 )
{ {
if ( w_len_ ) if ( w_len_ )
@ -27,7 +33,7 @@ internal wchar_t* _alloc_utf8_to_ucs2( AllocatorInfo a, char const* text, sw* w_
return NULL; return NULL;
} }
w_text = alloc_array( a, wchar_t, w_len + 1 ); w_text = alloc_array( a, wchar_t, w_len + 1 );
w_len1 = MultiByteToWideChar( CP_UTF8, MB_ERR_INVALID_CHARS, text, zpl_cast( int ) len, w_text, zpl_cast( int ) w_len ); w_len1 = MultiByteToWideChar( CP_UTF8, MB_ERR_INVALID_CHARS, text, scast( int, len), w_text, scast( int, w_len) );
if ( w_len1 == 0 ) if ( w_len1 == 0 )
{ {
free( a, w_text ); free( a, w_text );
@ -41,7 +47,8 @@ internal wchar_t* _alloc_utf8_to_ucs2( AllocatorInfo a, char const* text, sw* w_
return w_text; return w_text;
} }
internal GEN_FILE_SEEK_PROC( _win32_file_seek ) internal
GEN_FILE_SEEK_PROC( _win32_file_seek )
{ {
LARGE_INTEGER li_offset; LARGE_INTEGER li_offset;
li_offset.QuadPart = offset; li_offset.QuadPart = offset;
@ -55,12 +62,13 @@ internal GEN_FILE_SEEK_PROC( _win32_file_seek )
return true; return true;
} }
internal GEN_FILE_READ_AT_PROC( _win32_file_read ) internal
GEN_FILE_READ_AT_PROC( _win32_file_read )
{ {
// unused( stop_at_newline ); // unused( stop_at_newline );
b32 result = false; b32 result = false;
_win32_file_seek( fd, offset, ESeekWhence_BEGIN, NULL ); _win32_file_seek( fd, offset, ESeekWhence_BEGIN, NULL );
DWORD size_ = zpl_cast( DWORD )( size > GEN_I32_MAX ? GEN_I32_MAX : size ); DWORD size_ = scast( DWORD, ( size > GEN_I32_MAX ? GEN_I32_MAX : size ));
DWORD bytes_read_; DWORD bytes_read_;
if ( ReadFile( fd.p, buffer, size_, &bytes_read_, NULL ) ) if ( ReadFile( fd.p, buffer, size_, &bytes_read_, NULL ) )
{ {
@ -72,9 +80,10 @@ internal GEN_FILE_READ_AT_PROC( _win32_file_read )
return result; return result;
} }
internal GEN_FILE_WRITE_AT_PROC( _win32_file_write ) internal
GEN_FILE_WRITE_AT_PROC( _win32_file_write )
{ {
DWORD size_ = zpl_cast( DWORD )( size > GEN_I32_MAX ? GEN_I32_MAX : size ); DWORD size_ = scast( DWORD, ( size > GEN_I32_MAX ? GEN_I32_MAX : size ));
DWORD bytes_written_; DWORD bytes_written_;
_win32_file_seek( fd, offset, ESeekWhence_BEGIN, NULL ); _win32_file_seek( fd, offset, ESeekWhence_BEGIN, NULL );
if ( WriteFile( fd.p, buffer, size_, &bytes_written_, NULL ) ) if ( WriteFile( fd.p, buffer, size_, &bytes_written_, NULL ) )
@ -86,14 +95,16 @@ internal GEN_FILE_WRITE_AT_PROC( _win32_file_write )
return false; return false;
} }
internal GEN_FILE_CLOSE_PROC( _win32_file_close ) internal
GEN_FILE_CLOSE_PROC( _win32_file_close )
{ {
CloseHandle( fd.p ); CloseHandle( fd.p );
} }
FileOperations const default_file_operations = { _win32_file_read, _win32_file_write, _win32_file_seek, _win32_file_close }; FileOperations const default_file_operations = { _win32_file_read, _win32_file_write, _win32_file_seek, _win32_file_close };
neverinline GEN_FILE_OPEN_PROC( _win32_file_open ) neverinline
GEN_FILE_OPEN_PROC( _win32_file_open )
{ {
DWORD desired_access; DWORD desired_access;
DWORD creation_disposition; DWORD creation_disposition;
@ -171,7 +182,8 @@ neverinline GEN_FILE_OPEN_PROC( _win32_file_open )
#else // POSIX #else // POSIX
# include <fcntl.h> # include <fcntl.h>
internal GEN_FILE_SEEK_PROC( _posix_file_seek ) internal
GEN_FILE_SEEK_PROC( _posix_file_seek )
{ {
# if defined( GEN_SYSTEM_OSX ) # if defined( GEN_SYSTEM_OSX )
s64 res = lseek( fd.i, offset, whence ); s64 res = lseek( fd.i, offset, whence );
@ -185,10 +197,11 @@ internal GEN_FILE_SEEK_PROC( _posix_file_seek )
return true; return true;
} }
internal GEN_FILE_READ_AT_PROC( _posix_file_read ) internal
GEN_FILE_READ_AT_PROC( _posix_file_read )
{ {
unused( stop_at_newline ); unused( stop_at_newline );
sw res = pread( fd.i, buffer, size, offset ); ssize res = pread( fd.i, buffer, size, offset );
if ( res < 0 ) if ( res < 0 )
return false; return false;
if ( bytes_read ) if ( bytes_read )
@ -196,19 +209,20 @@ internal GEN_FILE_READ_AT_PROC( _posix_file_read )
return true; return true;
} }
internal GEN_FILE_WRITE_AT_PROC( _posix_file_write ) internal
GEN_FILE_WRITE_AT_PROC( _posix_file_write )
{ {
sw res; ssize res;
s64 curr_offset = 0; s64 curr_offset = 0;
_posix_file_seek( fd, 0, ESeekWhence_CURRENT, &curr_offset ); _posix_file_seek( fd, 0, ESeekWhence_CURRENT, &curr_offset );
if ( curr_offset == offset ) if ( curr_offset == offset )
{ {
// NOTE: Writing to stdout et al. doesn't like pwrite for numerous reasons // NOTE: Writing to stdout et al. doesn't like pwrite for numerous reasons
res = write( zpl_cast( int ) fd.i, buffer, size ); res = write( scast( int, fd.i), buffer, size );
} }
else else
{ {
res = pwrite( zpl_cast( int ) fd.i, buffer, size, offset ); res = pwrite( scast( int, fd.i), buffer, size, offset );
} }
if ( res < 0 ) if ( res < 0 )
return false; return false;
@ -217,14 +231,16 @@ internal GEN_FILE_WRITE_AT_PROC( _posix_file_write )
return true; return true;
} }
internal GEN_FILE_CLOSE_PROC( _posix_file_close ) internal
GEN_FILE_CLOSE_PROC( _posix_file_close )
{ {
close( fd.i ); close( fd.i );
} }
FileOperations const default_file_operations = { _posix_file_read, _posix_file_write, _posix_file_seek, _posix_file_close }; FileOperations const default_file_operations = { _posix_file_read, _posix_file_write, _posix_file_seek, _posix_file_close };
GEN_NEVER_INLINE GEN_FILE_OPEN_PROC( _posix_file_open ) neverinline
GEN_FILE_OPEN_PROC( _posix_file_open )
{ {
s32 os_mode; s32 os_mode;
switch ( mode & GEN_FILE_MODES ) switch ( mode & GEN_FILE_MODES )
@ -287,7 +303,7 @@ FileInfo* file_get_standard( FileStandardType std )
if ( ! _std_file_set ) if ( ! _std_file_set )
{ {
# define GEN__SET_STD_FILE( type, v ) \ # define GEN__SET_STD_FILE( type, v ) \
_std_files[ type ].fd.p = v; \ _std_files[ type ].fd.p = v; \
_std_files[ type ].ops = default_file_operations _std_files[ type ].ops = default_file_operations
GEN__SET_STD_FILE( EFileStandard_INPUT, GetStdHandle( STD_INPUT_HANDLE ) ); GEN__SET_STD_FILE( EFileStandard_INPUT, GetStdHandle( STD_INPUT_HANDLE ) );
GEN__SET_STD_FILE( EFileStandard_OUTPUT, GetStdHandle( STD_OUTPUT_HANDLE ) ); GEN__SET_STD_FILE( EFileStandard_OUTPUT, GetStdHandle( STD_OUTPUT_HANDLE ) );
@ -324,7 +340,7 @@ FileError file_close( FileInfo* f )
return EFileError_INVALID; return EFileError_INVALID;
if ( f->filename ) if ( f->filename )
free( heap(), zpl_cast( char* ) f->filename ); free( heap(), ccast( char*, f->filename ));
#if defined( GEN_SYSTEM_WINDOWS ) #if defined( GEN_SYSTEM_WINDOWS )
if ( f->fd.p == INVALID_HANDLE_VALUE ) if ( f->fd.p == INVALID_HANDLE_VALUE )
@ -359,14 +375,14 @@ FileError file_close( FileInfo* f )
FileError file_new( FileInfo* f, FileDescriptor fd, FileOperations ops, char const* filename ) FileError file_new( FileInfo* f, FileDescriptor fd, FileOperations ops, char const* filename )
{ {
FileError err = EFileError_NONE; FileError err = EFileError_NONE;
sw len = str_len( filename ); ssize len = str_len( filename );
f->ops = ops; f->ops = ops;
f->fd = fd; f->fd = fd;
f->dir = nullptr; f->dir = nullptr;
f->last_write_time = 0; f->last_write_time = 0;
f->filename = alloc_array( heap(), char, len + 1 ); f->filename = alloc_array( heap(), char, len + 1 );
mem_copy( zpl_cast( char* ) f->filename, zpl_cast( char* ) filename, len + 1 ); mem_copy( ccast( char*, f->filename), ccast( char*, filename), len + 1 );
return err; return err;
} }
@ -425,7 +441,7 @@ FileContents file_read_contents( AllocatorInfo a, b32 zero_terminate, char const
if ( file_open( &file, filepath ) == EFileError_NONE ) if ( file_open( &file, filepath ) == EFileError_NONE )
{ {
sw fsize = zpl_cast( sw ) file_size( &file ); ssize fsize = scast( ssize , file_size( &file ));
if ( fsize > 0 ) if ( fsize > 0 )
{ {
result.data = alloc( a, zero_terminate ? fsize + 1 : fsize ); result.data = alloc( a, zero_terminate ? fsize + 1 : fsize );
@ -433,7 +449,7 @@ FileContents file_read_contents( AllocatorInfo a, b32 zero_terminate, char const
file_read_at( &file, result.data, result.size, 0 ); file_read_at( &file, result.data, result.size, 0 );
if ( zero_terminate ) if ( zero_terminate )
{ {
u8* str = zpl_cast( u8* ) result.data; u8* str = rcast( u8*, result.data);
str[ fsize ] = '\0'; str[ fsize ] = '\0';
} }
} }
@ -447,26 +463,28 @@ struct _memory_fd
{ {
u8 magic; u8 magic;
u8* buf; //< zpl_array OR plain buffer if we can't write u8* buf; //< zpl_array OR plain buffer if we can't write
sw cursor; ssize cursor;
AllocatorInfo allocator; AllocatorInfo allocator;
FileStreamFlags flags; FileStreamFlags flags;
sw cap; ssize cap;
}; };
#define GEN__FILE_STREAM_FD_MAGIC 37 #define GEN__FILE_STREAM_FD_MAGIC 37
GEN_DEF_INLINE FileDescriptor _file_stream_fd_make( _memory_fd* d ); FileDescriptor _file_stream_fd_make( _memory_fd* d );
GEN_DEF_INLINE _memory_fd* _file_stream_from_fd( FileDescriptor fd ); _memory_fd* _file_stream_from_fd( FileDescriptor fd );
GEN_IMPL_INLINE FileDescriptor _file_stream_fd_make( _memory_fd* d ) inline
FileDescriptor _file_stream_fd_make( _memory_fd* d )
{ {
FileDescriptor fd = { 0 }; FileDescriptor fd = { 0 };
fd.p = ( void* )d; fd.p = ( void* )d;
return fd; return fd;
} }
GEN_IMPL_INLINE _memory_fd* _file_stream_from_fd( FileDescriptor fd ) inline
_memory_fd* _file_stream_from_fd( FileDescriptor fd )
{ {
_memory_fd* d = ( _memory_fd* )fd.p; _memory_fd* d = ( _memory_fd* )fd.p;
GEN_ASSERT( d->magic == GEN__FILE_STREAM_FD_MAGIC ); GEN_ASSERT( d->magic == GEN__FILE_STREAM_FD_MAGIC );
@ -487,7 +505,7 @@ b8 file_stream_new( FileInfo* file, AllocatorInfo allocator )
d->allocator = allocator; d->allocator = allocator;
d->flags = EFileStream_CLONE_WRITABLE; d->flags = EFileStream_CLONE_WRITABLE;
d->cap = 0; d->cap = 0;
d->buf = Array<u8>::init( allocator ); d->buf = array_init<u8>( allocator );
if ( ! d->buf ) if ( ! d->buf )
return false; return false;
@ -501,7 +519,7 @@ b8 file_stream_new( FileInfo* file, AllocatorInfo allocator )
return true; return true;
} }
b8 file_stream_open( FileInfo* file, AllocatorInfo allocator, u8* buffer, sw size, FileStreamFlags flags ) b8 file_stream_open( FileInfo* file, AllocatorInfo allocator, u8* buffer, ssize size, FileStreamFlags flags )
{ {
GEN_ASSERT_NOT_NULL( file ); GEN_ASSERT_NOT_NULL( file );
_memory_fd* d = ( _memory_fd* )alloc( allocator, size_of( _memory_fd ) ); _memory_fd* d = ( _memory_fd* )alloc( allocator, size_of( _memory_fd ) );
@ -513,7 +531,7 @@ b8 file_stream_open( FileInfo* file, AllocatorInfo allocator, u8* buffer, sw siz
d->flags = flags; d->flags = flags;
if ( d->flags & EFileStream_CLONE_WRITABLE ) if ( d->flags & EFileStream_CLONE_WRITABLE )
{ {
Array<u8> arr = Array<u8>::init_reserve( allocator, size ); Array<u8> arr = array_init_reserve<u8>( allocator, size );
d->buf = arr; d->buf = arr;
if ( ! d->buf ) if ( ! d->buf )
@ -522,7 +540,7 @@ b8 file_stream_open( FileInfo* file, AllocatorInfo allocator, u8* buffer, sw siz
mem_copy( d->buf, buffer, size ); mem_copy( d->buf, buffer, size );
d->cap = size; d->cap = size;
arr.get_header()->Num = size; get_header(arr)->Num = size;
} }
else else
{ {
@ -538,7 +556,7 @@ b8 file_stream_open( FileInfo* file, AllocatorInfo allocator, u8* buffer, sw siz
return true; return true;
} }
u8* file_stream_buf( FileInfo* file, sw* size ) u8* file_stream_buf( FileInfo* file, ssize* size )
{ {
GEN_ASSERT_NOT_NULL( file ); GEN_ASSERT_NOT_NULL( file );
_memory_fd* d = _file_stream_from_fd( file->fd ); _memory_fd* d = _file_stream_from_fd( file->fd );
@ -547,10 +565,11 @@ u8* file_stream_buf( FileInfo* file, sw* size )
return d->buf; return d->buf;
} }
internal GEN_FILE_SEEK_PROC( _memory_file_seek ) internal
GEN_FILE_SEEK_PROC( _memory_file_seek )
{ {
_memory_fd* d = _file_stream_from_fd( fd ); _memory_fd* d = _file_stream_from_fd( fd );
sw buflen = d->cap; ssize buflen = d->cap;
if ( whence == ESeekWhence_BEGIN ) if ( whence == ESeekWhence_BEGIN )
d->cursor = 0; d->cursor = 0;
@ -563,7 +582,8 @@ internal GEN_FILE_SEEK_PROC( _memory_file_seek )
return true; return true;
} }
internal GEN_FILE_READ_AT_PROC( _memory_file_read ) internal
GEN_FILE_READ_AT_PROC( _memory_file_read )
{ {
// unused( stop_at_newline ); // unused( stop_at_newline );
_memory_fd* d = _file_stream_from_fd( fd ); _memory_fd* d = _file_stream_from_fd( fd );
@ -573,25 +593,26 @@ internal GEN_FILE_READ_AT_PROC( _memory_file_read )
return true; return true;
} }
internal GEN_FILE_WRITE_AT_PROC( _memory_file_write ) internal
GEN_FILE_WRITE_AT_PROC( _memory_file_write )
{ {
_memory_fd* d = _file_stream_from_fd( fd ); _memory_fd* d = _file_stream_from_fd( fd );
if ( ! ( d->flags & ( EFileStream_CLONE_WRITABLE | EFileStream_WRITABLE ) ) ) if ( ! ( d->flags & ( EFileStream_CLONE_WRITABLE | EFileStream_WRITABLE ) ) )
return false; return false;
sw buflen = d->cap; ssize buflen = d->cap;
sw extralen = max( 0, size - ( buflen - offset ) ); ssize extralen = max( 0, size - ( buflen - offset ) );
sw rwlen = size - extralen; ssize rwlen = size - extralen;
sw new_cap = buflen + extralen; ssize new_cap = buflen + extralen;
if ( d->flags & EFileStream_CLONE_WRITABLE ) if ( d->flags & EFileStream_CLONE_WRITABLE )
{ {
Array<u8> arr = { d->buf }; Array<u8> arr = { d->buf };
if ( arr.get_header()->Capacity < new_cap ) if ( get_header(arr)->Capacity < usize(new_cap) )
{ {
if ( ! arr.grow( ( s64 )( new_cap ) ) ) if ( ! grow( & arr, ( s64 )( new_cap ) ) )
return false; return false;
d->buf = arr; d->buf = arr;
} }
@ -605,7 +626,7 @@ internal GEN_FILE_WRITE_AT_PROC( _memory_file_write )
mem_copy( d->buf + offset + rwlen, pointer_add_const( buffer, rwlen ), extralen ); mem_copy( d->buf + offset + rwlen, pointer_add_const( buffer, rwlen ), extralen );
d->cap = new_cap; d->cap = new_cap;
arr.get_header()->Capacity = new_cap; get_header(arr)->Capacity = new_cap;
} }
else else
{ {
@ -617,7 +638,8 @@ internal GEN_FILE_WRITE_AT_PROC( _memory_file_write )
return true; return true;
} }
internal GEN_FILE_CLOSE_PROC( _memory_file_close ) internal
GEN_FILE_CLOSE_PROC( _memory_file_close )
{ {
_memory_fd* d = _file_stream_from_fd( fd ); _memory_fd* d = _file_stream_from_fd( fd );
AllocatorInfo allocator = d->allocator; AllocatorInfo allocator = d->allocator;
@ -625,7 +647,7 @@ internal GEN_FILE_CLOSE_PROC( _memory_file_close )
if ( d->flags & EFileStream_CLONE_WRITABLE ) if ( d->flags & EFileStream_CLONE_WRITABLE )
{ {
Array<u8> arr = { d->buf }; Array<u8> arr = { d->buf };
arr.free(); free(& arr);
} }
free( allocator, d ); free( allocator, d );

View File

@ -1,3 +1,8 @@
#ifdef GEN_INTELLISENSE_DIRECTIVES
# pragma once
# include "strings.hpp"
#endif
#pragma region File Handling #pragma region File Handling
typedef u32 FileMode; typedef u32 FileMode;
@ -43,8 +48,8 @@ union FileDescriptor
typedef struct FileOperations FileOperations; typedef struct FileOperations FileOperations;
#define GEN_FILE_OPEN_PROC( name ) FileError name( FileDescriptor* fd, FileOperations* ops, FileMode mode, char const* filename ) #define GEN_FILE_OPEN_PROC( name ) FileError name( FileDescriptor* fd, FileOperations* ops, FileMode mode, char const* filename )
#define GEN_FILE_READ_AT_PROC( name ) b32 name( FileDescriptor fd, void* buffer, sw size, s64 offset, sw* bytes_read, b32 stop_at_newline ) #define GEN_FILE_READ_AT_PROC( name ) b32 name( FileDescriptor fd, void* buffer, ssize size, s64 offset, ssize* bytes_read, b32 stop_at_newline )
#define GEN_FILE_WRITE_AT_PROC( name ) b32 name( FileDescriptor fd, void const* buffer, sw size, s64 offset, sw* bytes_written ) #define GEN_FILE_WRITE_AT_PROC( name ) b32 name( FileDescriptor fd, void const* buffer, ssize size, s64 offset, ssize* bytes_written )
#define GEN_FILE_SEEK_PROC( name ) b32 name( FileDescriptor fd, s64 offset, SeekWhenceType whence, s64* new_offset ) #define GEN_FILE_SEEK_PROC( name ) b32 name( FileDescriptor fd, s64 offset, SeekWhenceType whence, s64* new_offset )
#define GEN_FILE_CLOSE_PROC( name ) void name( FileDescriptor fd ) #define GEN_FILE_CLOSE_PROC( name ) void name( FileDescriptor fd )
@ -156,7 +161,7 @@ FileError file_open_mode( FileInfo* file, FileMode mode, char const* filename );
* @param buffer Buffer to read to * @param buffer Buffer to read to
* @param size Size to read * @param size Size to read
*/ */
GEN_DEF_INLINE b32 file_read( FileInfo* file, void* buffer, sw size ); b32 file_read( FileInfo* file, void* buffer, ssize size );
/** /**
* Reads file at a specific offset * Reads file at a specific offset
@ -166,7 +171,7 @@ GEN_DEF_INLINE b32 file_read( FileInfo* file, void* buffer, sw size );
* @param offset Offset to read from * @param offset Offset to read from
* @param bytes_read How much data we've actually read * @param bytes_read How much data we've actually read
*/ */
GEN_DEF_INLINE b32 file_read_at( FileInfo* file, void* buffer, sw size, s64 offset ); b32 file_read_at( FileInfo* file, void* buffer, ssize size, s64 offset );
/** /**
* Reads file safely * Reads file safely
@ -176,13 +181,13 @@ GEN_DEF_INLINE b32 file_read_at( FileInfo* file, void* buffer, sw size, s64 offs
* @param offset Offset to read from * @param offset Offset to read from
* @param bytes_read How much data we've actually read * @param bytes_read How much data we've actually read
*/ */
GEN_DEF_INLINE b32 file_read_at_check( FileInfo* file, void* buffer, sw size, s64 offset, sw* bytes_read ); b32 file_read_at_check( FileInfo* file, void* buffer, ssize size, s64 offset, ssize* bytes_read );
struct FileContents struct FileContents
{ {
AllocatorInfo allocator; AllocatorInfo allocator;
void* data; void* data;
sw size; ssize size;
}; };
constexpr b32 zero_terminate = true; constexpr b32 zero_terminate = true;
@ -209,20 +214,20 @@ s64 file_size( FileInfo* file );
* @param file * @param file
* @param offset Offset to seek to * @param offset Offset to seek to
*/ */
GEN_DEF_INLINE s64 file_seek( FileInfo* file, s64 offset ); s64 file_seek( FileInfo* file, s64 offset );
/** /**
* Seeks the file cursor to the end of the file * Seeks the file cursor to the end of the file
* @param file * @param file
*/ */
GEN_DEF_INLINE s64 file_seek_to_end( FileInfo* file ); s64 file_seek_to_end( FileInfo* file );
/** /**
* Returns the length from the beginning of the file we've read so far * Returns the length from the beginning of the file we've read so far
* @param file * @param file
* @return Our current position in file * @return Our current position in file
*/ */
GEN_DEF_INLINE s64 file_tell( FileInfo* file ); s64 file_tell( FileInfo* file );
/** /**
* Writes to a file * Writes to a file
@ -230,7 +235,7 @@ GEN_DEF_INLINE s64 file_tell( FileInfo* file );
* @param buffer Buffer to read from * @param buffer Buffer to read from
* @param size Size to read * @param size Size to read
*/ */
GEN_DEF_INLINE b32 file_write( FileInfo* file, void const* buffer, sw size ); b32 file_write( FileInfo* file, void const* buffer, ssize size );
/** /**
* Writes to file at a specific offset * Writes to file at a specific offset
@ -240,7 +245,7 @@ GEN_DEF_INLINE b32 file_write( FileInfo* file, void const* buffer, sw size );
* @param offset Offset to write to * @param offset Offset to write to
* @param bytes_written How much data we've actually written * @param bytes_written How much data we've actually written
*/ */
GEN_DEF_INLINE b32 file_write_at( FileInfo* file, void const* buffer, sw size, s64 offset ); b32 file_write_at( FileInfo* file, void const* buffer, ssize size, s64 offset );
/** /**
* Writes to file safely * Writes to file safely
@ -250,88 +255,7 @@ GEN_DEF_INLINE b32 file_write_at( FileInfo* file, void const* buffer, sw size, s
* @param offset Offset to write to * @param offset Offset to write to
* @param bytes_written How much data we've actually written * @param bytes_written How much data we've actually written
*/ */
GEN_DEF_INLINE b32 file_write_at_check( FileInfo* file, void const* buffer, sw size, s64 offset, sw* bytes_written ); b32 file_write_at_check( FileInfo* file, void const* buffer, ssize size, s64 offset, ssize* bytes_written );
GEN_IMPL_INLINE s64 file_seek( FileInfo* f, s64 offset )
{
s64 new_offset = 0;
if ( ! f->ops.read_at )
f->ops = default_file_operations;
f->ops.seek( f->fd, offset, ESeekWhence_BEGIN, &new_offset );
return new_offset;
}
GEN_IMPL_INLINE s64 file_seek_to_end( FileInfo* f )
{
s64 new_offset = 0;
if ( ! f->ops.read_at )
f->ops = default_file_operations;
f->ops.seek( f->fd, 0, ESeekWhence_END, &new_offset );
return new_offset;
}
GEN_IMPL_INLINE s64 file_tell( FileInfo* f )
{
s64 new_offset = 0;
if ( ! f->ops.read_at )
f->ops = default_file_operations;
f->ops.seek( f->fd, 0, ESeekWhence_CURRENT, &new_offset );
return new_offset;
}
GEN_IMPL_INLINE b32 file_read( FileInfo* f, void* buffer, sw size )
{
s64 cur_offset = file_tell( f );
b32 result = file_read_at( f, buffer, size, file_tell( f ) );
file_seek( f, cur_offset + size );
return result;
}
GEN_IMPL_INLINE b32 file_read_at( FileInfo* f, void* buffer, sw size, s64 offset )
{
return file_read_at_check( f, buffer, size, offset, NULL );
}
GEN_IMPL_INLINE b32 file_read_at_check( FileInfo* f, void* buffer, sw size, s64 offset, sw* bytes_read )
{
if ( ! f->ops.read_at )
f->ops = default_file_operations;
return f->ops.read_at( f->fd, buffer, size, offset, bytes_read, false );
}
GEN_IMPL_INLINE b32 file_write( FileInfo* f, void const* buffer, sw size )
{
s64 cur_offset = file_tell( f );
b32 result = file_write_at( f, buffer, size, file_tell( f ) );
file_seek( f, cur_offset + size );
return result;
}
GEN_IMPL_INLINE b32 file_write_at( FileInfo* f, void const* buffer, sw size, s64 offset )
{
return file_write_at_check( f, buffer, size, offset, NULL );
}
GEN_IMPL_INLINE b32 file_write_at_check( FileInfo* f, void const* buffer, sw size, s64 offset, sw* bytes_written )
{
if ( ! f->ops.read_at )
f->ops = default_file_operations;
return f->ops.write_at( f->fd, buffer, size, offset, bytes_written );
}
enum FileStreamFlags : u32 enum FileStreamFlags : u32
{ {
@ -358,15 +282,103 @@ b8 file_stream_new( FileInfo* file, AllocatorInfo allocator );
* @param size Buffer's size * @param size Buffer's size
* @param flags * @param flags
*/ */
b8 file_stream_open( FileInfo* file, AllocatorInfo allocator, u8* buffer, sw size, FileStreamFlags flags ); b8 file_stream_open( FileInfo* file, AllocatorInfo allocator, u8* buffer, ssize size, FileStreamFlags flags );
/** /**
* Retrieves the stream's underlying buffer and buffer size. * Retrieves the stream's underlying buffer and buffer size.
* @param file memory stream * @param file memory stream
* @param size (Optional) buffer size * @param size (Optional) buffer size
*/ */
u8* file_stream_buf( FileInfo* file, sw* size ); u8* file_stream_buf( FileInfo* file, ssize* size );
extern FileOperations const memory_file_operations; extern FileOperations const memory_file_operations;
inline
s64 file_seek( FileInfo* f, s64 offset )
{
s64 new_offset = 0;
if ( ! f->ops.read_at )
f->ops = default_file_operations;
f->ops.seek( f->fd, offset, ESeekWhence_BEGIN, &new_offset );
return new_offset;
}
inline
s64 file_seek_to_end( FileInfo* f )
{
s64 new_offset = 0;
if ( ! f->ops.read_at )
f->ops = default_file_operations;
f->ops.seek( f->fd, 0, ESeekWhence_END, &new_offset );
return new_offset;
}
inline
s64 file_tell( FileInfo* f )
{
s64 new_offset = 0;
if ( ! f->ops.read_at )
f->ops = default_file_operations;
f->ops.seek( f->fd, 0, ESeekWhence_CURRENT, &new_offset );
return new_offset;
}
inline
b32 file_read( FileInfo* f, void* buffer, ssize size )
{
s64 cur_offset = file_tell( f );
b32 result = file_read_at( f, buffer, size, file_tell( f ) );
file_seek( f, cur_offset + size );
return result;
}
inline
b32 file_read_at( FileInfo* f, void* buffer, ssize size, s64 offset )
{
return file_read_at_check( f, buffer, size, offset, NULL );
}
inline
b32 file_read_at_check( FileInfo* f, void* buffer, ssize size, s64 offset, ssize* bytes_read )
{
if ( ! f->ops.read_at )
f->ops = default_file_operations;
return f->ops.read_at( f->fd, buffer, size, offset, bytes_read, false );
}
inline
b32 file_write( FileInfo* f, void const* buffer, ssize size )
{
s64 cur_offset = file_tell( f );
b32 result = file_write_at( f, buffer, size, file_tell( f ) );
file_seek( f, cur_offset + size );
return result;
}
inline
b32 file_write_at( FileInfo* f, void const* buffer, ssize size, s64 offset )
{
return file_write_at_check( f, buffer, size, offset, NULL );
}
inline
b32 file_write_at_check( FileInfo* f, void const* buffer, ssize size, s64 offset, ssize* bytes_written )
{
if ( ! f->ops.read_at )
f->ops = default_file_operations;
return f->ops.write_at( f->fd, buffer, size, offset, bytes_written );
}
#pragma endregion File Handling #pragma endregion File Handling

View File

@ -1,3 +1,8 @@
#ifdef GEN_INTELLISENSE_DIRECTIVES
# pragma once
# include "memory.cpp"
#endif
#pragma region Hashing #pragma region Hashing
global u32 const _crc32_table[ 256 ] = { global u32 const _crc32_table[ 256 ] = {
@ -22,11 +27,11 @@ global u32 const _crc32_table[ 256 ] = {
0xb40bbe37, 0xc30c8ea1, 0x5a05df1b, 0x2d02ef8d, 0xb40bbe37, 0xc30c8ea1, 0x5a05df1b, 0x2d02ef8d,
}; };
u32 crc32( void const* data, sw len ) u32 crc32( void const* data, ssize len )
{ {
sw remaining; ssize remaining;
u32 result = ~( zpl_cast( u32 ) 0 ); u32 result = ~( scast( u32, 0) );
u8 const* c = zpl_cast( u8 const* ) data; u8 const* c = rcast( u8 const*, data);
for ( remaining = len; remaining--; c++ ) for ( remaining = len; remaining--; c++ )
result = ( result >> 8 ) ^ ( _crc32_table[ ( result ^ *c ) & 0xff ] ); result = ( result >> 8 ) ^ ( _crc32_table[ ( result ^ *c ) & 0xff ] );
return ~result; return ~result;
@ -72,14 +77,14 @@ global u64 const _crc64_table[ 256 ] = {
0xa6df411fbfb21ca3ull, 0xdc0731d78f8795daull, 0x536fa08fdfd90e51ull, 0x29b7d047efec8728ull, 0xa6df411fbfb21ca3ull, 0xdc0731d78f8795daull, 0x536fa08fdfd90e51ull, 0x29b7d047efec8728ull,
}; };
u64 crc64( void const* data, sw len ) u64 crc64( void const* data, ssize len )
{ {
sw remaining; ssize remaining;
u64 result = ( zpl_cast( u64 ) 0 ); u64 result = ( scast( u64, 0) );
u8 const* c = zpl_cast( u8 const* ) data; u8 const* c = rcast( u8 const*, data);
for ( remaining = len; remaining--; c++ ) for ( remaining = len; remaining--; c++ )
result = ( result >> 8 ) ^ ( _crc64_table[ ( result ^ *c ) & 0xff ] ); result = ( result >> 8 ) ^ ( _crc64_table[ ( result ^ *c ) & 0xff ] );
return result; return result;
} }
#pragma region Hashing #pragma endregion Hashing

View File

@ -1,6 +1,11 @@
#ifdef GEN_INTELLISENSE_DIRECTIVES
#pragma once
#include "containers.hpp"
#endif
#pragma region Hashing #pragma region Hashing
u32 crc32( void const* data, sw len ); u32 crc32( void const* data, ssize len );
u64 crc64( void const* data, sw len ); u64 crc64( void const* data, ssize len );
#pragma endregion Hashing #pragma endregion Hashing

View File

@ -1,104 +1,237 @@
#pragma region Macros #ifdef GEN_INTELLISENSE_DIRECTIVES
# pragma once
#define zpl_cast( Type ) ( Type )
// Keywords
#define global static // Global variables
#define internal static // Internal linkage
#define local_persist static // Local Persisting variables
// Bits
#define bit( Value ) ( 1 << Value )
#define bitfield_is_equal( Type, Field, Mask ) ( (Type(Mask) & Type(Field)) == Type(Mask) )
// Casting
#define ccast( Type, Value ) ( * const_cast< Type* >( & (Value) ) )
#define pcast( Type, Value ) ( * reinterpret_cast< Type* >( & ( Value ) ) )
#define rcast( Type, Value ) reinterpret_cast< Type >( Value )
#define scast( Type, Value ) static_cast< Type >( Value )
// Num Arguments (Varadics)
#if defined(__GNUC__) || defined(__clang__)
// Supports 0-10 arguments
#define num_args_impl( _0, \
_1, _2, _3, _4, _5, _6, _7, _8, _9, _10, \
_11, _12, _13, _14, _15, _16, _17, _18, _19, _20, \
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)
#define num_args(...) \
num_args_impl(_, ## __VA_ARGS__, \
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
// Supports 1-10 arguments
#define num_args_impl( \
_1, _2, _3, _4, _5, _6, _7, _8, _9, _10, \
_11, _12, _13, _14, _15, _16, _17, _18, _19, _20, \
N, ... \
) N
#define num_args(...) \
num_args_impl( __VA_ARGS__, \
20, 19, 18, 17, 16, 15, 14, 13, 12, 11, \
10, 9, 8, 7, 6, 5, 4, 3, 2, 1 \
)
#endif #endif
// Stringizing #pragma region Macros
#ifndef global
#define global static // Global variables
#endif
#ifndef internal
#define internal static // Internal linkage
#endif
#ifndef local_persist
#define local_persist static // Local Persisting variables
#endif
#ifndef api_c
#define api_c extern "C"
#endif
#ifndef bit
#define bit( Value ) ( 1 << Value )
#define bitfield_is_equal( Type, Field, Mask ) ( (Type(Mask) & Type(Field)) == Type(Mask) )
#endif
#if ! GEN_C_COMPILER
# ifndef cast
# define cast( type, value ) (tmpl_cast<type>( value ))
# endif
# ifndef ccast
# define ccast( type, value ) ( const_cast< type >( (value) ) )
# endif
# ifndef pcast
# define pcast( type, value ) ( * reinterpret_cast< type* >( & ( value ) ) )
# endif
# ifndef rcast
# define rcast( type, value ) reinterpret_cast< type >( value )
# endif
# ifndef scast
# define scast( type, value ) static_cast< type >( value )
# endif
#else
# ifndef cast
# define cast( type, value ) ( (type)(value) )
# endif
# ifndef ccast
# define ccast( type, value ) ( (type)(value) )
# endif
# ifndef pcast
# define pcast( type, value ) ( * (type*)(value) )
# endif
# ifndef rcast
# define rcast( type, value ) ( (type)(value) )
# endif
# ifndef scast
# define scast( type, value ) ( (type)(value) )
# endif
#endif
#ifndef stringize
#define stringize_va( ... ) #__VA_ARGS__ #define stringize_va( ... ) #__VA_ARGS__
#define stringize( ... ) stringize_va( __VA_ARGS__ ) #define stringize( ... ) stringize_va( __VA_ARGS__ )
#endif
// Function do once #ifndef do_once
#define do_once( statement ) for ( local_persist b32 once = true; once; once = false, (statement) )
#define do_once() \
do \
{ \
static \
bool Done = false; \
if ( Done ) \
return; \
Done = true; \
} \
while(0)
#define do_once_start \ #define do_once_start \
do \ do \
{ \ { \
static \ local_persist \
bool Done = false; \ bool done = false; \
if ( Done ) \ if ( done ) \
break; \ break; \
Done = true; done = true;
#define do_once_end \ #define do_once_end \
} \ } \
while(0); while(0);
#endif
#ifndef labeled_scope_start
#define labeled_scope_start if ( false ) {
#define labeled_scope_end }
#endif
#ifndef compiler_decorated_func_name
# ifdef COMPILER_CLANG
# define compiler_decorated_func_name __PRETTY_NAME__
# elif defined(COMPILER_MSVC)
# define compiler_decorated_func_name __FUNCDNAME__
# endif
#endif
#ifndef num_args_impl
#define num_args_impl( _0, \
_1, _2, _3, _4, _5, _6, _7, _8, _9, _10, \
_11, _12, _13, _14, _15, _16, _17, _18, _19, _20, \
_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
// ## deletes preceding comma if _VA_ARGS__ is empty (GCC, Clang)
#define num_args(...) \
num_args_impl(_, ## __VA_ARGS__, \
100, 99, 98, 97, 96, 95, 94, 93, 92, 91, \
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, \
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 \
)
#endif
#ifndef clamp
#define clamp( x, lower, upper ) min( max( ( x ), ( lower ) ), ( upper ) ) #define clamp( x, lower, upper ) min( max( ( x ), ( lower ) ), ( upper ) )
#define count_of( x ) ( ( size_of( x ) / size_of( 0 [ x ] ) ) / ( ( sw )( ! ( size_of( x ) % size_of( 0 [ x ] ) ) ) ) ) #endif
#ifndef count_of
#define count_of( x ) ( ( size_of( x ) / size_of( 0 [ x ] ) ) / ( ( ssize )( ! ( size_of( x ) % size_of( 0 [ x ] ) ) ) ) )
#endif
#ifndef is_between
#define is_between( x, lower, upper ) ( ( ( lower ) <= ( x ) ) && ( ( x ) <= ( upper ) ) ) #define is_between( x, lower, upper ) ( ( ( lower ) <= ( x ) ) && ( ( x ) <= ( upper ) ) )
#define max( a, b ) ( ( a ) > ( b ) ? ( a ) : ( b ) ) #endif
#define min( a, b ) ( ( a ) < ( b ) ? ( a ) : ( b ) ) #ifndef size_of
#define size_of( x ) ( sw )( sizeof( x ) ) #define size_of( x ) ( ssize )( sizeof( x ) )
#endif
template< class Type > #ifndef max
void swap( Type& a, Type& b ) #define max( a, b ) ( (a > b) ? (a) : (b) )
{ #endif
Type tmp = a; #ifndef min
a = b; #define min( a, b ) ( (a < b) ? (a) : (b) )
b = tmp; #endif
}
#if GEN_COMPILER_MSVC || GEN_COMPILER_TINYC
# define offset_of( Type, element ) ( ( GEN_NS( ssize ) ) & ( ( ( Type* )0 )->element ) )
#else
# define offset_of( Type, element ) __builtin_offsetof( Type, element )
#endif
#ifndef forceinline
# if GEN_COMPILER_MSVC
# define forceinline __forceinline
# define neverinline __declspec( noinline )
# elif GEN_COMPILER_GCC
# define forceinline inline __attribute__((__always_inline__))
# define neverinline __attribute__( ( __noinline__ ) )
# elif GEN_COMPILER_CLANG
# if __has_attribute(__always_inline__)
# define forceinline inline __attribute__((__always_inline__))
# define neverinline __attribute__( ( __noinline__ ) )
# else
# define forceinline
# define neverinline
# endif
# else
# define forceinline
# define neverinline
# endif
#endif
#ifndef neverinline
# if GEN_COMPILER_MSVC
# define neverinline __declspec( noinline )
# elif GEN_COMPILER_GCC
# define neverinline __attribute__( ( __noinline__ ) )
# elif GEN_COMPILER_CLANG
# if __has_attribute(__always_inline__)
# define neverinline __attribute__( ( __noinline__ ) )
# else
# define neverinline
# endif
# else
# define neverinline
# endif
#endif
#if !defined(GEN_SUPPORT_CPP_REFERENCES) && (GEN_COMPILER_C || __STDC_VERSION__ < 202311L)
# undef GEN_SUPPORT_CPP_REFERENCES
# define GEN_SUPPORT_CPP_REFERENCES 0
#endif
#if !defined(GEN_SUPPORT_CPP_MEMBER_FEATURES) && (GEN_COMPILER_C || __STDC_VERSION__ < 202311L)
# undef GEN_SUPPORT_CPP_MEMBER_FEATURES
# define GEN_SUPPORT_CPP_MEMBER_FEATURES 0
#endif
#if !defined(typeof) && (!GEN_COMPILER_C || __STDC_VERSION__ < 202311L)
# if ! GEN_COMPILER_C
# define typeof decltype
# elif defined(_MSC_VER)
# define typeof(x) __typeof(x)
# elif defined(__GNUC__) || defined(__clang__)
# define typeof(x) __typeof__(x)
# else
# error "Compiler not supported"
# endif
#endif
// This is intended to only really be used internally or with the C-library variant
// C++ users can just use the for-range directly.
#if GEN_COMPILER_C
# define foreach(Type, entry_id, iterable) for ( Type entry_id = begin(iterable); entry_id != end(iterable); entry_id = next(iterable, entry_id) )
#else
# define foreach(Type, entry_id, iterable) for ( Type entry_id : iterable )
#endif
#if GEN_COMPILER_C
# if __STDC_VERSION__ >= 202311L
# define enum_underlying(type) : type
# else
# define enum_underlying(type)
# endif
#else
# define enum_underlying(type) : type
#endif
#if GEN_COMPILER_C
# ifndef nullptr
# define nullptr NULL
# endif
#endif
#pragma endregion Macros #pragma endregion Macros

View File

@ -1,6 +1,11 @@
#ifdef GEN_INTELLISENSE_DIRECTIVES
# pragma once
# include "printing.cpp"
#endif
#pragma region Memory #pragma region Memory
void* mem_copy( void* dest, void const* source, sw n ) void* mem_copy( void* dest, void const* source, ssize n )
{ {
if ( dest == NULL ) if ( dest == NULL )
{ {
@ -10,25 +15,25 @@ void* mem_copy( void* dest, void const* source, sw n )
return memcpy( dest, source, n ); return memcpy( dest, source, n );
} }
void const* mem_find( void const* data, u8 c, sw n ) void const* mem_find( void const* data, u8 c, ssize n )
{ {
u8 const* s = zpl_cast( u8 const* ) data; u8 const* s = rcast( u8 const*, data);
while ( ( zpl_cast( uptr ) s & ( sizeof( uw ) - 1 ) ) && n && *s != c ) while ( ( rcast( uptr, s) & ( sizeof( usize ) - 1 ) ) && n && *s != c )
{ {
s++; s++;
n--; n--;
} }
if ( n && *s != c ) if ( n && *s != c )
{ {
sw const* w; ssize const* w;
sw k = GEN__ONES * c; ssize k = GEN__ONES * c;
w = zpl_cast( sw const* ) s; w = rcast( ssize const*, s);
while ( n >= size_of( sw ) && ! GEN__HAS_ZERO( *w ^ k ) ) while ( n >= size_of( ssize ) && ! GEN__HAS_ZERO( *w ^ k ) )
{ {
w++; w++;
n -= size_of( sw ); n -= size_of( ssize );
} }
s = zpl_cast( u8 const* ) w; s = rcast( u8 const*, w);
while ( n && *s != c ) while ( n && *s != c )
{ {
s++; s++;
@ -36,7 +41,7 @@ void const* mem_find( void const* data, u8 c, sw n )
} }
} }
return n ? zpl_cast( void const* ) s : NULL; return n ? rcast( void const*, s ) : NULL;
} }
#define GEN_HEAP_STATS_MAGIC 0xDEADC0DE #define GEN_HEAP_STATS_MAGIC 0xDEADC0DE
@ -44,8 +49,8 @@ void const* mem_find( void const* data, u8 c, sw n )
struct _heap_stats struct _heap_stats
{ {
u32 magic; u32 magic;
sw used_memory; ssize used_memory;
sw alloc_count; ssize alloc_count;
}; };
global _heap_stats _heap_stats_info; global _heap_stats _heap_stats_info;
@ -56,13 +61,13 @@ void heap_stats_init( void )
_heap_stats_info.magic = GEN_HEAP_STATS_MAGIC; _heap_stats_info.magic = GEN_HEAP_STATS_MAGIC;
} }
sw heap_stats_used_memory( void ) ssize heap_stats_used_memory( void )
{ {
GEN_ASSERT_MSG( _heap_stats_info.magic == GEN_HEAP_STATS_MAGIC, "heap_stats is not initialised yet, call heap_stats_init first!" ); GEN_ASSERT_MSG( _heap_stats_info.magic == GEN_HEAP_STATS_MAGIC, "heap_stats is not initialised yet, call heap_stats_init first!" );
return _heap_stats_info.used_memory; return _heap_stats_info.used_memory;
} }
sw heap_stats_alloc_count( void ) ssize heap_stats_alloc_count( void )
{ {
GEN_ASSERT_MSG( _heap_stats_info.magic == GEN_HEAP_STATS_MAGIC, "heap_stats is not initialised yet, call heap_stats_init first!" ); GEN_ASSERT_MSG( _heap_stats_info.magic == GEN_HEAP_STATS_MAGIC, "heap_stats is not initialised yet, call heap_stats_init first!" );
return _heap_stats_info.alloc_count; return _heap_stats_info.alloc_count;
@ -77,11 +82,11 @@ void heap_stats_check( void )
struct _heap_alloc_info struct _heap_alloc_info
{ {
sw size; ssize size;
void* physical_start; void* physical_start;
}; };
void* heap_allocator_proc( void* allocator_data, AllocType type, sw size, sw alignment, void* old_memory, sw old_size, u64 flags ) void* heap_allocator_proc( void* allocator_data, AllocType type, ssize size, ssize alignment, void* old_memory, ssize old_size, u64 flags )
{ {
void* ptr = NULL; void* ptr = NULL;
// unused( allocator_data ); // unused( allocator_data );
@ -90,16 +95,16 @@ void* heap_allocator_proc( void* allocator_data, AllocType type, sw size, sw ali
alignment = GEN_DEFAULT_MEMORY_ALIGNMENT; alignment = GEN_DEFAULT_MEMORY_ALIGNMENT;
#ifdef GEN_HEAP_ANALYSIS #ifdef GEN_HEAP_ANALYSIS
sw alloc_info_size = size_of( _heap_alloc_info ); ssize alloc_info_size = size_of( _heap_alloc_info );
sw alloc_info_remainder = ( alloc_info_size % alignment ); ssize alloc_info_remainder = ( alloc_info_size % alignment );
sw track_size = max( alloc_info_size, alignment ) + alloc_info_remainder; ssize track_size = max( alloc_info_size, alignment ) + alloc_info_remainder;
switch ( type ) switch ( type )
{ {
case EAllocation_FREE : case EAllocation_FREE :
{ {
if ( ! old_memory ) if ( ! old_memory )
break; break;
_heap_alloc_info* alloc_info = zpl_cast( _heap_alloc_info* ) old_memory - 1; _heap_alloc_info* alloc_info = rcast( _heap_alloc_info*, old_memory) - 1;
_heap_stats_info.used_memory -= alloc_info->size; _heap_stats_info.used_memory -= alloc_info->size;
_heap_stats_info.alloc_count--; _heap_stats_info.alloc_count--;
old_memory = alloc_info->physical_start; old_memory = alloc_info->physical_start;
@ -190,11 +195,11 @@ void* heap_allocator_proc( void* allocator_data, AllocType type, sw size, sw ali
#ifdef GEN_HEAP_ANALYSIS #ifdef GEN_HEAP_ANALYSIS
if ( type == EAllocation_ALLOC ) if ( type == EAllocation_ALLOC )
{ {
_heap_alloc_info* alloc_info = zpl_cast( _heap_alloc_info* )( zpl_cast( char* ) ptr + alloc_info_remainder ); _heap_alloc_info* alloc_info = rcast( _heap_alloc_info*, rcast( char*, ptr) + alloc_info_remainder );
zero_item( alloc_info ); zero_item( alloc_info );
alloc_info->size = size - track_size; alloc_info->size = size - track_size;
alloc_info->physical_start = ptr; alloc_info->physical_start = ptr;
ptr = zpl_cast( void* )( alloc_info + 1 ); ptr = rcast( void*, alloc_info + 1 );
_heap_stats_info.used_memory += alloc_info->size; _heap_stats_info.used_memory += alloc_info->size;
_heap_stats_info.alloc_count++; _heap_stats_info.alloc_count++;
} }
@ -203,7 +208,133 @@ void* heap_allocator_proc( void* allocator_data, AllocType type, sw size, sw ali
return ptr; return ptr;
} }
void* Arena::allocator_proc( void* allocator_data, AllocType type, sw size, sw alignment, void* old_memory, sw old_size, u64 flags ) #pragma region VirtualMemory
VirtualMemory vm_from_memory( void* data, ssize size )
{
VirtualMemory vm;
vm.data = data;
vm.size = size;
return vm;
}
#if defined( GEN_SYSTEM_WINDOWS )
VirtualMemory vm_alloc( void* addr, ssize size )
{
VirtualMemory vm;
GEN_ASSERT( size > 0 );
vm.data = VirtualAlloc( addr, size, MEM_COMMIT | MEM_RESERVE, PAGE_READWRITE );
vm.size = size;
return vm;
}
b32 vm_free( VirtualMemory vm )
{
MEMORY_BASIC_INFORMATION info;
while ( vm.size > 0 )
{
if ( VirtualQuery( vm.data, &info, size_of( info ) ) == 0 )
return false;
if ( info.BaseAddress != vm.data || info.AllocationBase != vm.data || info.State != MEM_COMMIT || info.RegionSize > scast( usize, vm.size) )
{
return false;
}
if ( VirtualFree( vm.data, 0, MEM_RELEASE ) == 0 )
return false;
vm.data = pointer_add( vm.data, info.RegionSize );
vm.size -= info.RegionSize;
}
return true;
}
VirtualMemory vm_trim( VirtualMemory vm, ssize lead_size, ssize size )
{
VirtualMemory new_vm = { 0 };
void* ptr;
GEN_ASSERT( vm.size >= lead_size + size );
ptr = pointer_add( vm.data, lead_size );
vm_free( vm );
new_vm = vm_alloc( ptr, size );
if ( new_vm.data == ptr )
return new_vm;
if ( new_vm.data )
vm_free( new_vm );
return new_vm;
}
b32 vm_purge( VirtualMemory vm )
{
VirtualAlloc( vm.data, vm.size, MEM_RESET, PAGE_READWRITE );
// NOTE: Can this really fail?
return true;
}
ssize virtual_memory_page_size( ssize* alignment_out )
{
SYSTEM_INFO info;
GetSystemInfo( &info );
if ( alignment_out )
*alignment_out = info.dwAllocationGranularity;
return info.dwPageSize;
}
#else
# include <sys/mman.h>
# ifndef MAP_ANONYMOUS
# define MAP_ANONYMOUS MAP_ANON
# endif
VirtualMemory vm_alloc( void* addr, ssize size )
{
VirtualMemory vm;
GEN_ASSERT( size > 0 );
vm.data = mmap( addr, size, PROT_READ | PROT_WRITE, MAP_ANONYMOUS | MAP_PRIVATE, -1, 0 );
vm.size = size;
return vm;
}
b32 vm_free( VirtualMemory vm )
{
munmap( vm.data, vm.size );
return true;
}
VirtualMemory vm_trim( VirtualMemory vm, ssize lead_size, ssize size )
{
void* ptr;
ssize trail_size;
GEN_ASSERT( vm.size >= lead_size + size );
ptr = pointer_add( vm.data, lead_size );
trail_size = vm.size - lead_size - size;
if ( lead_size != 0 )
vm_free( vm_from_memory(( vm.data, lead_size ) );
if ( trail_size != 0 )
vm_free( vm_from_memory( ptr, trail_size ) );
return vm_from_memory( ptr, size );
}
b32 vm_purge( VirtualMemory vm )
{
int err = madvise( vm.data, vm.size, MADV_DONTNEED );
return err != 0;
}
ssize virtual_memory_page_size( ssize* alignment_out )
{
// TODO: Is this always true?
ssize result = scast( ssize, sysconf( _SC_PAGE_SIZE ));
if ( alignment_out )
*alignment_out = result;
return result;
}
#endif
#pragma endregion VirtualMemory
void* arena_allocator_proc( void* allocator_data, AllocType type, ssize size, ssize alignment, void* old_memory, ssize old_size, u64 flags )
{ {
Arena* arena = rcast(Arena*, allocator_data); Arena* arena = rcast(Arena*, allocator_data);
void* ptr = NULL; void* ptr = NULL;
@ -215,13 +346,13 @@ void* Arena::allocator_proc( void* allocator_data, AllocType type, sw size, sw a
case EAllocation_ALLOC : case EAllocation_ALLOC :
{ {
void* end = pointer_add( arena->PhysicalStart, arena->TotalUsed ); void* end = pointer_add( arena->PhysicalStart, arena->TotalUsed );
sw total_size = align_forward_i64( size, alignment ); ssize total_size = align_forward_i64( size, alignment );
// NOTE: Out of memory // NOTE: Out of memory
if ( arena->TotalUsed + total_size > (sw) arena->TotalSize ) if ( arena->TotalUsed + total_size > (ssize) arena->TotalSize )
{ {
// zpl__printf_err("%s", "Arena out of memory\n"); // zpl__printf_err("%s", "Arena out of memory\n");
fatal("Arena out of memory! (Possibly could not fit for the largest size Arena!!)"); GEN_FATAL("Arena out of memory! (Possibly could not fit for the largest size Arena!!)");
return nullptr; return nullptr;
} }
@ -253,9 +384,9 @@ void* Arena::allocator_proc( void* allocator_data, AllocType type, sw size, sw a
return ptr; return ptr;
} }
void* Pool::allocator_proc( void* allocator_data, AllocType type, sw size, sw alignment, void* old_memory, sw old_size, u64 flags ) void* pool_allocator_proc( void* allocator_data, AllocType type, ssize size, ssize alignment, void* old_memory, ssize old_size, u64 flags )
{ {
Pool* pool = zpl_cast( Pool* ) allocator_data; Pool* pool = rcast( Pool*, allocator_data);
void* ptr = NULL; void* ptr = NULL;
// unused( old_size ); // unused( old_size );
@ -270,9 +401,9 @@ void* Pool::allocator_proc( void* allocator_data, AllocType type, sw size, sw al
GEN_ASSERT( alignment == pool->BlockAlign ); GEN_ASSERT( alignment == pool->BlockAlign );
GEN_ASSERT( pool->FreeList != NULL ); GEN_ASSERT( pool->FreeList != NULL );
next_free = *zpl_cast( uptr* ) pool->FreeList; next_free = * rcast( uptr*, pool->FreeList);
ptr = pool->FreeList; ptr = pool->FreeList;
pool->FreeList = zpl_cast( void* ) next_free; pool->FreeList = rcast( void*, next_free);
pool->TotalSize += pool->BlockSize; pool->TotalSize += pool->BlockSize;
if ( flags & ALLOCATOR_FLAG_CLEAR_TO_ZERO ) if ( flags & ALLOCATOR_FLAG_CLEAR_TO_ZERO )
@ -286,8 +417,8 @@ void* Pool::allocator_proc( void* allocator_data, AllocType type, sw size, sw al
if ( old_memory == NULL ) if ( old_memory == NULL )
return NULL; return NULL;
next = zpl_cast( uptr* ) old_memory; next = rcast( uptr*, old_memory);
*next = zpl_cast( uptr ) pool->FreeList; *next = rcast( uptr, pool->FreeList);
pool->FreeList = old_memory; pool->FreeList = old_memory;
pool->TotalSize -= pool->BlockSize; pool->TotalSize -= pool->BlockSize;
} }
@ -295,7 +426,7 @@ void* Pool::allocator_proc( void* allocator_data, AllocType type, sw size, sw al
case EAllocation_FREE_ALL : case EAllocation_FREE_ALL :
{ {
sw actual_block_size, block_index; ssize actual_block_size, block_index;
void* curr; void* curr;
uptr* end; uptr* end;
@ -306,13 +437,13 @@ void* Pool::allocator_proc( void* allocator_data, AllocType type, sw size, sw al
curr = pool->PhysicalStart; curr = pool->PhysicalStart;
for ( block_index = 0; block_index < pool->NumBlocks - 1; block_index++ ) for ( block_index = 0; block_index < pool->NumBlocks - 1; block_index++ )
{ {
uptr* next = zpl_cast( uptr* ) curr; uptr* next = rcast( uptr*, curr);
*next = zpl_cast( uptr ) curr + actual_block_size; * next = rcast( uptr, curr) + actual_block_size;
curr = pointer_add( curr, actual_block_size ); curr = pointer_add( curr, actual_block_size );
} }
end = zpl_cast( uptr* ) curr; end = rcast( uptr*, curr);
*end = zpl_cast( uptr ) NULL; * end = scast( uptr, NULL);
pool->FreeList = pool->PhysicalStart; pool->FreeList = pool->PhysicalStart;
} }
break; break;
@ -326,11 +457,11 @@ void* Pool::allocator_proc( void* allocator_data, AllocType type, sw size, sw al
return ptr; return ptr;
} }
Pool Pool::init_align( AllocatorInfo backing, sw num_blocks, sw block_size, sw block_align ) Pool pool_init_align( AllocatorInfo backing, ssize num_blocks, ssize block_size, ssize block_align )
{ {
Pool pool = {}; Pool pool = {};
sw actual_block_size, pool_size, block_index; ssize actual_block_size, pool_size, block_index;
void *data, *curr; void *data, *curr;
uptr* end; uptr* end;
@ -364,16 +495,16 @@ Pool Pool::init_align( AllocatorInfo backing, sw num_blocks, sw block_size, sw b
return pool; return pool;
} }
void Pool::clear() void clear(Pool& pool)
{ {
sw actual_block_size, block_index; ssize actual_block_size, block_index;
void* curr; void* curr;
uptr* end; uptr* end;
actual_block_size = BlockSize + BlockAlign; actual_block_size = pool.BlockSize + pool.BlockAlign;
curr = PhysicalStart; curr = pool.PhysicalStart;
for ( block_index = 0; block_index < NumBlocks - 1; block_index++ ) for ( block_index = 0; block_index < pool.NumBlocks - 1; block_index++ )
{ {
uptr* next = ( uptr* ) curr; uptr* next = ( uptr* ) curr;
*next = ( uptr ) curr + actual_block_size; *next = ( uptr ) curr + actual_block_size;
@ -383,7 +514,7 @@ void Pool::clear()
end = ( uptr* ) curr; end = ( uptr* ) curr;
*end = ( uptr ) NULL; *end = ( uptr ) NULL;
FreeList = PhysicalStart; pool.FreeList = pool.PhysicalStart;
} }
#pragma endregion Memory #pragma endregion Memory

View File

@ -1,47 +1,60 @@
#ifdef GEN_INTELLISENSE_DIRECTIVES
# pragma once
# include "debug.hpp"
#endif
#pragma region Memory #pragma region Memory
#define kilobytes( x ) ( ( x ) * ( s64 )( 1024 ) ) #define kilobytes( x ) ( ( x ) * ( s64 )( 1024 ) )
#define megabytes( x ) ( kilobytes( x ) * ( s64 )( 1024 ) ) #define megabytes( x ) ( kilobytes( x ) * ( s64 )( 1024 ) )
#define gigabytes( x ) ( megabytes( x ) * ( s64 )( 1024 ) ) #define gigabytes( x ) ( megabytes( x ) * ( s64 )( 1024 ) )
#define terabytes( x ) ( gigabytes( x ) * ( s64 )( 1024 ) ) #define terabytes( x ) ( gigabytes( x ) * ( s64 )( 1024 ) )
#define GEN__ONES ( zpl_cast( uw ) - 1 / GEN_U8_MAX ) #define GEN__ONES ( scast( GEN_NS usize, - 1) / GEN_U8_MAX )
#define GEN__HIGHS ( GEN__ONES * ( GEN_U8_MAX / 2 + 1 ) ) #define GEN__HIGHS ( GEN__ONES * ( GEN_U8_MAX / 2 + 1 ) )
#define GEN__HAS_ZERO( x ) ( ( ( x )-GEN__ONES ) & ~( x )&GEN__HIGHS ) #define GEN__HAS_ZERO( x ) ( ( ( x ) - GEN__ONES ) & ~( x ) & GEN__HIGHS )
template< class Type >
void swap( Type& a, Type& b )
{
Type tmp = a;
a = b;
b = tmp;
}
//! Checks if value is power of 2. //! Checks if value is power of 2.
GEN_DEF_INLINE b32 is_power_of_two( sw x ); b32 is_power_of_two( ssize x );
//! Aligns address to specified alignment. //! Aligns address to specified alignment.
GEN_DEF_INLINE void* align_forward( void* ptr, sw alignment ); void* align_forward( void* ptr, ssize alignment );
//! Aligns value to a specified alignment. //! Aligns value to a specified alignment.
GEN_DEF_INLINE s64 align_forward_i64( s64 value, sw alignment ); s64 align_forward_i64( s64 value, ssize alignment );
//! Moves pointer forward by bytes. //! Moves pointer forward by bytes.
GEN_DEF_INLINE void* pointer_add( void* ptr, sw bytes ); void* pointer_add( void* ptr, ssize bytes );
//! Moves pointer forward by bytes. //! Moves pointer forward by bytes.
GEN_DEF_INLINE void const* pointer_add_const( void const* ptr, sw bytes ); void const* pointer_add_const( void const* ptr, ssize bytes );
//! Calculates difference between two addresses. //! Calculates difference between two addresses.
GEN_DEF_INLINE sw pointer_diff( void const* begin, void const* end ); ssize pointer_diff( void const* begin, void const* end );
//! Copy non-overlapping memory from source to destination. //! Copy non-overlapping memory from source to destination.
void* mem_copy( void* dest, void const* source, sw size ); void* mem_copy( void* dest, void const* source, ssize size );
//! Search for a constant value within the size limit at memory location. //! Search for a constant value within the size limit at memory location.
void const* mem_find( void const* data, u8 byte_value, sw size ); void const* mem_find( void const* data, u8 byte_value, ssize size );
//! Copy memory from source to destination. //! Copy memory from source to destination.
GEN_DEF_INLINE void* mem_move( void* dest, void const* source, sw size ); void* mem_move( void* dest, void const* source, ssize size );
//! Set constant value at memory location with specified size. //! Set constant value at memory location with specified size.
GEN_DEF_INLINE void* mem_set( void* data, u8 byte_value, sw size ); void* mem_set( void* data, u8 byte_value, ssize size );
//! @param ptr Memory location to clear up. //! @param ptr Memory location to clear up.
//! @param size The size to clear up with. //! @param size The size to clear up with.
GEN_DEF_INLINE void zero_size( void* ptr, sw size ); void zero_size( void* ptr, ssize size );
//! Clears up an item. //! Clears up an item.
#define zero_item( t ) zero_size( ( t ), size_of( *( t ) ) ) // NOTE: Pass pointer of struct #define zero_item( t ) zero_size( ( t ), size_of( *( t ) ) ) // NOTE: Pass pointer of struct
@ -49,24 +62,23 @@ GEN_DEF_INLINE void zero_size( void* ptr, sw size );
//! Clears up an array. //! Clears up an array.
#define zero_array( a, count ) zero_size( ( a ), size_of( *( a ) ) * count ) #define zero_array( a, count ) zero_size( ( a ), size_of( *( a ) ) * count )
enum AllocType : u8 enum AllocType_Def //enum_underlying(u8)
{ {
EAllocation_ALLOC, EAllocation_ALLOC,
EAllocation_FREE, EAllocation_FREE,
EAllocation_FREE_ALL, EAllocation_FREE_ALL,
EAllocation_RESIZE, EAllocation_RESIZE,
}; };
typedef enum AllocType_Def AllocType;
using AllocatorProc = void* ( void* allocator_data, AllocType type typedef void*(AllocatorProc)( void* allocator_data, AllocType type, ssize size, ssize alignment, void* old_memory, ssize old_size, u64 flags );
, sw size, sw alignment
, void* old_memory, sw old_size
, u64 flags );
struct AllocatorInfo struct AllocatorInfo_Def
{ {
AllocatorProc* Proc; AllocatorProc* Proc;
void* Data; void* Data;
}; };
typedef struct AllocatorInfo_Def AllocatorInfo;
enum AllocFlag enum AllocFlag
{ {
@ -82,22 +94,22 @@ enum AllocFlag
#endif #endif
//! Allocate memory with default alignment. //! Allocate memory with default alignment.
GEN_DEF_INLINE void* alloc( AllocatorInfo a, sw size ); void* alloc( AllocatorInfo a, ssize size );
//! Allocate memory with specified alignment. //! Allocate memory with specified alignment.
GEN_DEF_INLINE void* alloc_align( AllocatorInfo a, sw size, sw alignment ); void* alloc_align( AllocatorInfo a, ssize size, ssize alignment );
//! Free allocated memory. //! Free allocated memory.
GEN_DEF_INLINE void free( AllocatorInfo a, void* ptr ); void free( AllocatorInfo a, void* ptr );
//! Free all memory allocated by an allocator. //! Free all memory allocated by an allocator.
GEN_DEF_INLINE void free_all( AllocatorInfo a ); void free_all( AllocatorInfo a );
//! Resize an allocated memory. //! Resize an allocated memory.
GEN_DEF_INLINE void* resize( AllocatorInfo a, void* ptr, sw old_size, sw new_size ); void* resize( AllocatorInfo a, void* ptr, ssize old_size, ssize new_size );
//! Resize an allocated memory with specified alignment. //! Resize an allocated memory with specified alignment.
GEN_DEF_INLINE void* resize_align( AllocatorInfo a, void* ptr, sw old_size, sw new_size, sw alignment ); void* resize_align( AllocatorInfo a, void* ptr, ssize old_size, ssize new_size, ssize alignment );
//! Allocate memory for an item. //! Allocate memory for an item.
#define alloc_item( allocator_, Type ) ( Type* )alloc( allocator_, size_of( Type ) ) #define alloc_item( allocator_, Type ) ( Type* )alloc( allocator_, size_of( Type ) )
@ -109,20 +121,20 @@ GEN_DEF_INLINE void* resize_align( AllocatorInfo a, void* ptr, sw old_size, sw n
/* define GEN_HEAP_ANALYSIS to enable this feature */ /* define GEN_HEAP_ANALYSIS to enable this feature */
/* call zpl_heap_stats_init at the beginning of the entry point */ /* call zpl_heap_stats_init at the beginning of the entry point */
/* you can call zpl_heap_stats_check near the end of the execution to validate any possible leaks */ /* you can call zpl_heap_stats_check near the end of the execution to validate any possible leaks */
void heap_stats_init( void ); void heap_stats_init( void );
sw heap_stats_used_memory( void ); ssize heap_stats_used_memory( void );
sw heap_stats_alloc_count( void ); ssize heap_stats_alloc_count( void );
void heap_stats_check( void ); void heap_stats_check( void );
//! Allocate/Resize memory using default options. //! Allocate/Resize memory using default options.
//! Use this if you don't need a "fancy" resize allocation //! Use this if you don't need a "fancy" resize allocation
GEN_DEF_INLINE void* default_resize_align( AllocatorInfo a, void* ptr, sw old_size, sw new_size, sw alignment ); void* default_resize_align( AllocatorInfo a, void* ptr, ssize old_size, ssize new_size, ssize alignment );
void* heap_allocator_proc( void* allocator_data, AllocType type, sw size, sw alignment, void* old_memory, sw old_size, u64 flags ); void* heap_allocator_proc( void* allocator_data, AllocType type, ssize size, ssize alignment, void* old_memory, ssize old_size, u64 flags );
//! The heap allocator backed by operating system's memory manager. //! The heap allocator backed by operating system's memory manager.
constexpr AllocatorInfo heap( void ) { return { heap_allocator_proc, nullptr }; } constexpr AllocatorInfo heap( void ) { AllocatorInfo allocator = { heap_allocator_proc, nullptr }; return allocator; }
//! Helper to allocate memory using heap allocator. //! Helper to allocate memory using heap allocator.
#define malloc( sz ) alloc( heap(), sz ) #define malloc( sz ) alloc( heap(), sz )
@ -130,203 +142,502 @@ constexpr AllocatorInfo heap( void ) { return { heap_allocator_proc, nullptr };
//! Helper to free memory allocated by heap allocator. //! Helper to free memory allocated by heap allocator.
#define mfree( ptr ) free( heap(), ptr ) #define mfree( ptr ) free( heap(), ptr )
GEN_IMPL_INLINE b32 is_power_of_two( sw x ) struct VirtualMemory
{ {
void* data;
ssize size;
};
//! Initialize virtual memory from existing data.
VirtualMemory vm_from_memory( void* data, ssize size );
//! Allocate virtual memory at address with size.
//! @param addr The starting address of the region to reserve. If NULL, it lets operating system to decide where to allocate it.
//! @param size The size to serve.
VirtualMemory vm_alloc( void* addr, ssize size );
//! Release the virtual memory.
b32 vm_free( VirtualMemory vm );
//! Trim virtual memory.
VirtualMemory vm_trim( VirtualMemory vm, ssize lead_size, ssize size );
//! Purge virtual memory.
b32 gen_vm_purge( VirtualMemory vm );
//! Retrieve VM's page size and alignment.
ssize gen_virtual_memory_page_size( ssize* alignment_out );
#pragma region Arena
struct Arena;
AllocatorInfo allocator_info( Arena* arena );
// Remove static keyword and rename allocator_proc
void* arena_allocator_proc(void* allocator_data, AllocType type, ssize size, ssize alignment, void* old_memory, ssize old_size, u64 flags);
// Add these declarations after the Arena struct
Arena arena_init_from_allocator(AllocatorInfo backing, ssize size);
Arena arena_init_from_memory ( void* start, ssize size );
Arena init_sub (Arena* parent, ssize size);
ssize alignment_of (Arena* arena, ssize alignment);
void free (Arena* arena);
ssize size_remaining(Arena* arena, ssize alignment);
// This id is defined by Unreal for asserts
#pragma push_macro("check")
#undef check
void check(Arena* arena);
#pragma pop_macro("check")
struct Arena
{
AllocatorInfo Backing;
void* PhysicalStart;
ssize TotalSize;
ssize TotalUsed;
ssize TempCount;
#if GEN_SUPPORT_CPP_MEMBER_FEATURES
#pragma region Member Mapping
forceinline operator AllocatorInfo() { return GEN_NS allocator_info(this); }
forceinline static void* allocator_proc( void* allocator_data, AllocType type, ssize size, ssize alignment, void* old_memory, ssize old_size, u64 flags ) { return GEN_NS arena_allocator_proc( allocator_data, type, size, alignment, old_memory, old_size, flags ); }
forceinline static Arena init_from_memory( void* start, ssize size ) { return GEN_NS arena_init_from_memory( start, size ); }
forceinline static Arena init_from_allocator( AllocatorInfo backing, ssize size ) { return GEN_NS arena_init_from_allocator( backing, size ); }
forceinline static Arena init_sub( Arena& parent, ssize size ) { return GEN_NS arena_init_from_allocator( parent.Backing, size ); }
forceinline ssize alignment_of( ssize alignment ) { return GEN_NS alignment_of(this, alignment); }
forceinline void free() { return GEN_NS free(this); }
forceinline ssize size_remaining( ssize alignment ) { return GEN_NS size_remaining(this, alignment); }
// This id is defined by Unreal for asserts
#pragma push_macro("check")
#undef check
forceinline void check() { GEN_NS check(this); }
#pragma pop_macro("check")
#pragma endregion Member Mapping
#endif
};
#if GEN_SUPPORT_CPP_REFERENCES
forceinline AllocatorInfo allocator_info(Arena& arena ) { return allocator_info(& arena); }
forceinline Arena init_sub (Arena& parent, ssize size) { return init_sub( & parent, size); }
forceinline ssize alignment_of (Arena& arena, ssize alignment) { return alignment_of( & arena, alignment); }
forceinline void free (Arena& arena) { return free(& arena); }
forceinline ssize size_remaining(Arena& arena, ssize alignment) { return size_remaining(& arena, alignment); }
// This id is defined by Unreal for asserts
#pragma push_macro("check")
#undef check
forceinline void check(Arena& arena) { return check(& arena); };
#pragma pop_macro("check")
#endif
inline
AllocatorInfo allocator_info( Arena* arena ) {
GEN_ASSERT(arena != nullptr);
return { arena_allocator_proc, arena };
}
inline
Arena arena_init_from_memory( void* start, ssize size )
{
Arena arena = {
{ nullptr, nullptr },
start,
size,
0,
0
};
return arena;
}
inline
Arena arena_init_from_allocator(AllocatorInfo backing, ssize size) {
Arena result = {
backing,
alloc(backing, size),
size,
0,
0
};
return result;
}
inline
Arena init_sub(Arena* parent, ssize size) {
GEN_ASSERT(parent != nullptr);
return arena_init_from_allocator(parent->Backing, size);
}
inline
ssize alignment_of(Arena* arena, ssize alignment)
{
GEN_ASSERT(arena != nullptr);
ssize alignment_offset, result_pointer, mask;
GEN_ASSERT(is_power_of_two(alignment));
alignment_offset = 0;
result_pointer = (ssize)arena->PhysicalStart + arena->TotalUsed;
mask = alignment - 1;
if (result_pointer & mask)
alignment_offset = alignment - (result_pointer & mask);
return alignment_offset;
}
#pragma push_macro("check")
#undef check
inline
void check(Arena* arena)
{
GEN_ASSERT(arena != nullptr );
GEN_ASSERT(arena->TempCount == 0);
}
#pragma pop_macro("check")
inline
void free(Arena* arena)
{
GEN_ASSERT(arena != nullptr);
if (arena->Backing.Proc)
{
GEN_NS free(arena->Backing, arena->PhysicalStart);
arena->PhysicalStart = nullptr;
}
}
inline
ssize size_remaining(Arena* arena, ssize alignment)
{
GEN_ASSERT(arena != nullptr);
ssize result = arena->TotalSize - (arena->TotalUsed + alignment_of(arena, alignment));
return result;
}
#pragma endregion Arena
#pragma region FixedArena
template<s32 Size>
struct FixedArena;
template<s32 Size> FixedArena<Size> fixed_arena_init();
template<s32 Size> AllocatorInfo allocator_info(FixedArena<Size>* fixed_arena );
template<s32 Size> ssize size_remaining(FixedArena<Size>* fixed_arena, ssize alignment);
#if GEN_SUPPORT_CPP_REFERENCES
template<s32 Size> AllocatorInfo allocator_info( FixedArena<Size>& fixed_arena ) { return allocator_info(& fixed_arena); }
template<s32 Size> ssize size_remaining(FixedArena<Size>& fixed_arena, ssize alignment) { return size_remaining( & fixed_arena, alignment); }
#endif
// Just a wrapper around using an arena with memory associated with its scope instead of from an allocator.
// Used for static segment or stack allocations.
template< s32 Size >
struct FixedArena
{
char memory[Size];
Arena arena;
#if GEN_SUPPORT_CPP_MEMBER_FEATURES
#pragma region Member Mapping
forceinline operator AllocatorInfo() { return GEN_NS allocator_info(this); }
forceinline static FixedArena init() { FixedArena result; GEN_NS fixed_arena_init<Size>(result); return result; }
forceinline ssize size_remaining(ssize alignment) { GEN_NS size_remaining(this, alignment); }
#pragma endregion Member Mapping
#endif
};
template<s32 Size> inline
AllocatorInfo allocator_info( FixedArena<Size>* fixed_arena ) {
GEN_ASSERT(fixed_arena);
return { arena_allocator_proc, & fixed_arena->arena };
}
template<s32 Size> inline
void fixed_arena_init(FixedArena<Size>* result) {
zero_size(& result->memory[0], Size);
result->arena = arena_init_from_memory(& result->memory[0], Size);
}
template<s32 Size> inline
ssize size_remaining(FixedArena<Size>* fixed_arena, ssize alignment) {
return size_remaining(fixed_arena->arena, alignment);
}
using Arena_1KB = FixedArena< kilobytes( 1 ) >;
using Arena_4KB = FixedArena< kilobytes( 4 ) >;
using Arena_8KB = FixedArena< kilobytes( 8 ) >;
using Arena_16KB = FixedArena< kilobytes( 16 ) >;
using Arena_32KB = FixedArena< kilobytes( 32 ) >;
using Arena_64KB = FixedArena< kilobytes( 64 ) >;
using Arena_128KB = FixedArena< kilobytes( 128 ) >;
using Arena_256KB = FixedArena< kilobytes( 256 ) >;
using Arena_512KB = FixedArena< kilobytes( 512 ) >;
using Arena_1MB = FixedArena< megabytes( 1 ) >;
using Arena_2MB = FixedArena< megabytes( 2 ) >;
using Arena_4MB = FixedArena< megabytes( 4 ) >;
#pragma endregion FixedArena
#pragma region Pool
struct Pool;
void* pool_allocator_proc(void* allocator_data, AllocType type, ssize size, ssize alignment, void* old_memory, ssize old_size, u64 flags);
Pool pool_init(AllocatorInfo backing, ssize num_blocks, ssize block_size);
Pool pool_init_align(AllocatorInfo backing, ssize num_blocks, ssize block_size, ssize block_align);
AllocatorInfo allocator_info(Pool* pool);
void clear(Pool* pool);
void free(Pool* pool);
#if GEN_SUPPORT_CPP_REFERENCES
AllocatorInfo allocator_info(Pool& pool);
void clear(Pool& pool);
void free(Pool& pool);
#endif
struct Pool
{
AllocatorInfo Backing;
void* PhysicalStart;
void* FreeList;
ssize BlockSize;
ssize BlockAlign;
ssize TotalSize;
ssize NumBlocks;
#if GEN_SUPPORT_CPP_MEMBER_FEATURES
#pragma region Member Mapping
forceinline operator AllocatorInfo() { return GEN_NS allocator_info(this); }
forceinline static void* allocator_proc(void* allocator_data, AllocType type, ssize size, ssize alignment, void* old_memory, ssize old_size, u64 flags) { return GEN_NS pool_allocator_proc(allocator_data, type, size, alignment, old_memory, old_size, flags); }
forceinline static Pool init(AllocatorInfo backing, ssize num_blocks, ssize block_size) { return GEN_NS pool_init(backing, num_blocks, block_size); }
forceinline static Pool init_align(AllocatorInfo backing, ssize num_blocks, ssize block_size, ssize block_align) { return GEN_NS pool_init_align(backing, num_blocks, block_size, block_align); }
forceinline void clear() { GEN_NS clear( this); }
forceinline void free() { GEN_NS free( this); }
#pragma endregion
#endif
};
inline
AllocatorInfo allocator_info(Pool* pool) {
return { pool_allocator_proc, pool };
}
inline
Pool pool_init(AllocatorInfo backing, ssize num_blocks, ssize block_size) {
return pool_init_align(backing, num_blocks, block_size, GEN_DEFAULT_MEMORY_ALIGNMENT);
}
inline
void free(Pool* pool) {
if(pool->Backing.Proc) {
GEN_NS free(pool->Backing, pool->PhysicalStart);
}
}
#pragma endregion Pool
inline
b32 is_power_of_two( ssize x ) {
if ( x <= 0 ) if ( x <= 0 )
return false; return false;
return ! ( x & ( x - 1 ) ); return ! ( x & ( x - 1 ) );
} }
GEN_IMPL_INLINE void* align_forward( void* ptr, sw alignment ) inline
mem_ptr align_forward( void* ptr, ssize alignment )
{ {
uptr p;
GEN_ASSERT( is_power_of_two( alignment ) ); GEN_ASSERT( is_power_of_two( alignment ) );
uptr p = to_uptr(ptr);
uptr forward = (p + ( alignment - 1 ) ) & ~( alignment - 1 );
p = zpl_cast( uptr ) ptr; return to_mem_ptr(forward);
return zpl_cast( void* )( ( p + ( alignment - 1 ) ) & ~( alignment - 1 ) );
} }
GEN_IMPL_INLINE s64 align_forward_i64( s64 value, sw alignment ) inline s64 align_forward_i64( s64 value, ssize alignment ) { return value + ( alignment - value % alignment ) % alignment; }
{
return value + ( alignment - value % alignment ) % alignment; inline void* pointer_add ( void* ptr, ssize bytes ) { return rcast(void*, rcast( u8*, ptr) + bytes ); }
inline void const* pointer_add_const( void const* ptr, ssize bytes ) { return rcast(void const*, rcast( u8 const*, ptr) + bytes ); }
inline sptr pointer_diff( mem_ptr_const begin, mem_ptr_const end ) {
return scast( ssize, rcast( u8 const*, end) - rcast(u8 const*, begin) );
} }
GEN_IMPL_INLINE void* pointer_add( void* ptr, sw bytes ) inline
void* mem_move( void* destination, void const* source, ssize byte_count )
{ {
return zpl_cast( void* )( zpl_cast( u8* ) ptr + bytes ); if ( destination == NULL )
}
GEN_IMPL_INLINE void const* pointer_add_const( void const* ptr, sw bytes )
{
return zpl_cast( void const* )( zpl_cast( u8 const* ) ptr + bytes );
}
GEN_IMPL_INLINE sw pointer_diff( void const* begin, void const* end )
{
return zpl_cast( sw )( zpl_cast( u8 const* ) end - zpl_cast( u8 const* ) begin );
}
GEN_IMPL_INLINE void* mem_move( void* dest, void const* source, sw n )
{
if ( dest == NULL )
{ {
return NULL; return NULL;
} }
u8* d = zpl_cast( u8* ) dest; u8* dest_ptr = rcast( u8*, destination);
u8 const* s = zpl_cast( u8 const* ) source; u8 const* src_ptr = rcast( u8 const*, source);
if ( d == s ) if ( dest_ptr == src_ptr )
return d; return dest_ptr;
if ( s + n <= d || d + n <= s ) // NOTE: Non-overlapping
return mem_copy( d, s, n );
if ( d < s ) if ( src_ptr + byte_count <= dest_ptr || dest_ptr + byte_count <= src_ptr ) // NOTE: Non-overlapping
return mem_copy( dest_ptr, src_ptr, byte_count );
if ( dest_ptr < src_ptr )
{ {
if ( zpl_cast( uptr ) s % size_of( sw ) == zpl_cast( uptr ) d % size_of( sw ) ) if ( to_uptr(src_ptr) % size_of( ssize ) == to_uptr(dest_ptr) % size_of( ssize ) )
{ {
while ( zpl_cast( uptr ) d % size_of( sw ) ) while ( pcast( uptr, dest_ptr) % size_of( ssize ) )
{ {
if ( ! n-- ) if ( ! byte_count-- )
return dest; return destination;
*d++ = *s++;
*dest_ptr++ = *src_ptr++;
} }
while ( n >= size_of( sw ) ) while ( byte_count >= size_of( ssize ) )
{ {
*zpl_cast( sw* ) d = *zpl_cast( sw* ) s; * rcast(ssize*, dest_ptr) = * rcast(ssize const*, src_ptr);
n -= size_of( sw ); byte_count -= size_of( ssize );
d += size_of( sw ); dest_ptr += size_of( ssize );
s += size_of( sw ); src_ptr += size_of( ssize );
} }
} }
for ( ; n; n-- ) for ( ; byte_count; byte_count-- )
*d++ = *s++; *dest_ptr++ = *src_ptr++;
} }
else else
{ {
if ( ( zpl_cast( uptr ) s % size_of( sw ) ) == ( zpl_cast( uptr ) d % size_of( sw ) ) ) if ( ( to_uptr(src_ptr) % size_of( ssize ) ) == ( to_uptr(dest_ptr) % size_of( ssize ) ) )
{ {
while ( zpl_cast( uptr )( d + n ) % size_of( sw ) ) while ( to_uptr( dest_ptr + byte_count ) % size_of( ssize ) )
{ {
if ( ! n-- ) if ( ! byte_count-- )
return dest; return destination;
d[ n ] = s[ n ];
dest_ptr[ byte_count ] = src_ptr[ byte_count ];
} }
while ( n >= size_of( sw ) ) while ( byte_count >= size_of( ssize ) )
{ {
n -= size_of( sw ); byte_count -= size_of( ssize );
*zpl_cast( sw* )( d + n ) = *zpl_cast( sw* )( s + n ); * rcast(ssize*, dest_ptr + byte_count ) = * rcast( ssize const*, src_ptr + byte_count );
} }
} }
while ( n ) while ( byte_count )
n--, d[ n ] = s[ n ]; byte_count--, dest_ptr[ byte_count ] = src_ptr[ byte_count ];
} }
return dest; return destination;
} }
GEN_IMPL_INLINE void* mem_set( void* dest, u8 c, sw n ) inline
void* mem_set( void* destination, u8 fill_byte, ssize byte_count )
{ {
if ( dest == NULL ) if ( destination == NULL )
{ {
return NULL; return NULL;
} }
u8* s = zpl_cast( u8* ) dest; ssize align_offset;
sw k; u8* dest_ptr = rcast( u8*, destination);
u32 c32 = ( ( u32 )-1 ) / 255 * c; u32 fill_word = ( ( u32 )-1 ) / 255 * fill_byte;
if ( n == 0 ) if ( byte_count == 0 )
return dest; return destination;
s[ 0 ] = s[ n - 1 ] = c;
if ( n < 3 )
return dest;
s[ 1 ] = s[ n - 2 ] = c;
s[ 2 ] = s[ n - 3 ] = c;
if ( n < 7 )
return dest;
s[ 3 ] = s[ n - 4 ] = c;
if ( n < 9 )
return dest;
k = -zpl_cast( sptr ) s & 3; dest_ptr[ 0 ] = dest_ptr[ byte_count - 1 ] = fill_byte;
s += k; if ( byte_count < 3 )
n -= k; return destination;
n &= -4;
*zpl_cast( u32* )( s + 0 ) = c32; dest_ptr[ 1 ] = dest_ptr[ byte_count - 2 ] = fill_byte;
*zpl_cast( u32* )( s + n - 4 ) = c32; dest_ptr[ 2 ] = dest_ptr[ byte_count - 3 ] = fill_byte;
if ( n < 9 ) if ( byte_count < 7 )
return dest; return destination;
*zpl_cast( u32* )( s + 4 ) = c32;
*zpl_cast( u32* )( s + 8 ) = c32;
*zpl_cast( u32* )( s + n - 12 ) = c32;
*zpl_cast( u32* )( s + n - 8 ) = c32;
if ( n < 25 )
return dest;
*zpl_cast( u32* )( s + 12 ) = c32;
*zpl_cast( u32* )( s + 16 ) = c32;
*zpl_cast( u32* )( s + 20 ) = c32;
*zpl_cast( u32* )( s + 24 ) = c32;
*zpl_cast( u32* )( s + n - 28 ) = c32;
*zpl_cast( u32* )( s + n - 24 ) = c32;
*zpl_cast( u32* )( s + n - 20 ) = c32;
*zpl_cast( u32* )( s + n - 16 ) = c32;
k = 24 + ( zpl_cast( uptr ) s & 4 ); dest_ptr[ 3 ] = dest_ptr[ byte_count - 4 ] = fill_byte;
s += k; if ( byte_count < 9 )
n -= k; return destination;
align_offset = -to_sptr( dest_ptr ) & 3;
dest_ptr += align_offset;
byte_count -= align_offset;
byte_count &= -4;
* rcast( u32*, ( dest_ptr + 0 ) ) = fill_word;
* rcast( u32*, ( dest_ptr + byte_count - 4 ) ) = fill_word;
if ( byte_count < 9 )
return destination;
* rcast( u32*, dest_ptr + 4 ) = fill_word;
* rcast( u32*, dest_ptr + 8 ) = fill_word;
* rcast( u32*, dest_ptr + byte_count - 12 ) = fill_word;
* rcast( u32*, dest_ptr + byte_count - 8 ) = fill_word;
if ( byte_count < 25 )
return destination;
* rcast( u32*, dest_ptr + 12 ) = fill_word;
* rcast( u32*, dest_ptr + 16 ) = fill_word;
* rcast( u32*, dest_ptr + 20 ) = fill_word;
* rcast( u32*, dest_ptr + 24 ) = fill_word;
* rcast( u32*, dest_ptr + byte_count - 28 ) = fill_word;
* rcast( u32*, dest_ptr + byte_count - 24 ) = fill_word;
* rcast( u32*, dest_ptr + byte_count - 20 ) = fill_word;
* rcast( u32*, dest_ptr + byte_count - 16 ) = fill_word;
align_offset = 24 + to_uptr( dest_ptr ) & 4;
dest_ptr += align_offset;
byte_count -= align_offset;
{ {
u64 c64 = ( zpl_cast( u64 ) c32 << 32 ) | c32; u64 fill_doubleword = ( scast( u64, fill_word) << 32 ) | fill_word;
while ( n > 31 ) while ( byte_count > 31 )
{ {
*zpl_cast( u64* )( s + 0 ) = c64; * rcast( u64*, dest_ptr + 0 ) = fill_doubleword;
*zpl_cast( u64* )( s + 8 ) = c64; * rcast( u64*, dest_ptr + 8 ) = fill_doubleword;
*zpl_cast( u64* )( s + 16 ) = c64; * rcast( u64*, dest_ptr + 16 ) = fill_doubleword;
*zpl_cast( u64* )( s + 24 ) = c64; * rcast( u64*, dest_ptr + 24 ) = fill_doubleword;
n -= 32; byte_count -= 32;
s += 32; dest_ptr += 32;
} }
} }
return dest; return destination;
} }
GEN_IMPL_INLINE void* alloc_align( AllocatorInfo a, sw size, sw alignment ) inline
{ void* alloc_align( AllocatorInfo a, ssize size, ssize alignment ) {
return a.Proc( a.Data, EAllocation_ALLOC, size, alignment, nullptr, 0, GEN_DEFAULT_ALLOCATOR_FLAGS ); return a.Proc( a.Data, EAllocation_ALLOC, size, alignment, nullptr, 0, GEN_DEFAULT_ALLOCATOR_FLAGS );
} }
GEN_IMPL_INLINE void* alloc( AllocatorInfo a, sw size ) inline
{ void* alloc( AllocatorInfo a, ssize size ) {
return alloc_align( a, size, GEN_DEFAULT_MEMORY_ALIGNMENT ); return alloc_align( a, size, GEN_DEFAULT_MEMORY_ALIGNMENT );
} }
GEN_IMPL_INLINE void free( AllocatorInfo a, void* ptr ) inline
{ void free( AllocatorInfo a, void* ptr ) {
if ( ptr != nullptr ) if ( ptr != nullptr )
a.Proc( a.Data, EAllocation_FREE, 0, 0, ptr, 0, GEN_DEFAULT_ALLOCATOR_FLAGS ); a.Proc( a.Data, EAllocation_FREE, 0, 0, ptr, 0, GEN_DEFAULT_ALLOCATOR_FLAGS );
} }
GEN_IMPL_INLINE void free_all( AllocatorInfo a ) inline
{ void free_all( AllocatorInfo a ) {
a.Proc( a.Data, EAllocation_FREE_ALL, 0, 0, nullptr, 0, GEN_DEFAULT_ALLOCATOR_FLAGS ); a.Proc( a.Data, EAllocation_FREE_ALL, 0, 0, nullptr, 0, GEN_DEFAULT_ALLOCATOR_FLAGS );
} }
GEN_IMPL_INLINE void* resize( AllocatorInfo a, void* ptr, sw old_size, sw new_size ) inline
{ void* resize( AllocatorInfo a, void* ptr, ssize old_size, ssize new_size ) {
return resize_align( a, ptr, old_size, new_size, GEN_DEFAULT_MEMORY_ALIGNMENT ); return resize_align( a, ptr, old_size, new_size, GEN_DEFAULT_MEMORY_ALIGNMENT );
} }
GEN_IMPL_INLINE void* resize_align( AllocatorInfo a, void* ptr, sw old_size, sw new_size, sw alignment ) inline
{ void* resize_align( AllocatorInfo a, void* ptr, ssize old_size, ssize new_size, ssize alignment ) {
return a.Proc( a.Data, EAllocation_RESIZE, new_size, alignment, ptr, old_size, GEN_DEFAULT_ALLOCATOR_FLAGS ); return a.Proc( a.Data, EAllocation_RESIZE, new_size, alignment, ptr, old_size, GEN_DEFAULT_ALLOCATOR_FLAGS );
} }
GEN_IMPL_INLINE void* default_resize_align( AllocatorInfo a, void* old_memory, sw old_size, sw new_size, sw alignment ) inline
void* default_resize_align( AllocatorInfo a, void* old_memory, ssize old_size, ssize new_size, ssize alignment )
{ {
if ( ! old_memory ) if ( ! old_memory )
return alloc_align( a, new_size, alignment ); return alloc_align( a, new_size, alignment );
@ -346,141 +657,19 @@ GEN_IMPL_INLINE void* default_resize_align( AllocatorInfo a, void* old_memory, s
} }
else else
{ {
void* new_memory = alloc_align( a, new_size, alignment ); void* new_memory = alloc_align( a, new_size, alignment );
if ( ! new_memory ) if ( ! new_memory )
return nullptr; return nullptr;
mem_move( new_memory, old_memory, min( new_size, old_size ) ); mem_move( new_memory, old_memory, min( new_size, old_size ) );
free( a, old_memory ); free( a, old_memory );
return new_memory; return new_memory;
} }
} }
GEN_IMPL_INLINE void zero_size( void* ptr, sw size ) inline
{ void zero_size( void* ptr, ssize size ) {
mem_set( ptr, 0, size ); mem_set( ptr, 0, size );
} }
struct Arena
{
static
void* allocator_proc( void* allocator_data, AllocType type, sw size, sw alignment, void* old_memory, sw old_size, u64 flags );
static
Arena init_from_memory( void* start, sw size )
{
return
{
{ nullptr, nullptr },
start,
size,
0,
0
};
}
static
Arena init_from_allocator( AllocatorInfo backing, sw size )
{
Arena result =
{
backing,
alloc( backing, size),
size,
0,
0
};
return result;
}
static
Arena init_sub( Arena& parent, sw size )
{
return init_from_allocator( parent.Backing, size );
}
sw alignment_of( sw alignment )
{
sw alignment_offset, result_pointer, mask;
GEN_ASSERT( is_power_of_two( alignment ) );
alignment_offset = 0;
result_pointer = (sw) PhysicalStart + TotalUsed;
mask = alignment - 1;
if ( result_pointer & mask )
alignment_offset = alignment - ( result_pointer & mask );
return alignment_offset;
}
void check()
{
GEN_ASSERT( TempCount == 0 );
}
void free()
{
if ( Backing.Proc )
{
gen::free( Backing, PhysicalStart );
PhysicalStart = nullptr;
}
}
sw size_remaining( sw alignment )
{
sw result = TotalSize - ( TotalUsed + alignment_of( alignment ) );
return result;
}
AllocatorInfo Backing;
void* PhysicalStart;
sw TotalSize;
sw TotalUsed;
sw TempCount;
operator AllocatorInfo()
{
return { allocator_proc, this };
}
};
struct Pool
{
static
void* allocator_proc( void* allocator_data, AllocType type, sw size, sw alignment, void* old_memory, sw old_size, u64 flags );
static
Pool init( AllocatorInfo backing, sw num_blocks, sw block_size )
{
return init_align( backing, num_blocks, block_size, GEN_DEFAULT_MEMORY_ALIGNMENT );
}
static
Pool init_align( AllocatorInfo backing, sw num_blocks, sw block_size, sw block_align );
void clear();
void free()
{
if ( Backing.Proc )
{
gen::free( Backing, PhysicalStart );
}
}
AllocatorInfo Backing;
void* PhysicalStart;
void* FreeList;
sw BlockSize;
sw BlockAlign;
sw TotalSize;
sw NumBlocks;
operator AllocatorInfo()
{
return { allocator_proc, this };
}
};
#pragma endregion Memory #pragma endregion Memory

View File

@ -1,3 +1,7 @@
#ifdef GEN_INTELLISENSE_DIRECTIVES
# pragma once
#endif
#pragma region ADT #pragma region ADT
#define _adt_fprintf( s_, fmt_, ... ) \ #define _adt_fprintf( s_, fmt_, ... ) \
@ -19,7 +23,7 @@ u8 adt_make_branch( ADT_Node* node, AllocatorInfo backing, char const* name, b32
node->type = type; node->type = type;
node->name = name; node->name = name;
node->parent = parent; node->parent = parent;
node->nodes = Array<ADT_Node>::init( backing ); node->nodes = array_init<ADT_Node>( backing );
if ( ! node->nodes ) if ( ! node->nodes )
return EADT_ERROR_OUT_OF_MEMORY; return EADT_ERROR_OUT_OF_MEMORY;
@ -32,12 +36,12 @@ u8 adt_destroy_branch( ADT_Node* node )
GEN_ASSERT_NOT_NULL( node ); GEN_ASSERT_NOT_NULL( node );
if ( ( node->type == EADT_TYPE_OBJECT || node->type == EADT_TYPE_ARRAY ) && node->nodes ) if ( ( node->type == EADT_TYPE_OBJECT || node->type == EADT_TYPE_ARRAY ) && node->nodes )
{ {
for ( sw i = 0; i < node->nodes.num(); ++i ) for ( ssize i = 0; i < scast(ssize, num(node->nodes)); ++i )
{ {
adt_destroy_branch( node->nodes + i ); adt_destroy_branch( node->nodes + i );
} }
node->nodes.free(); free(& node->nodes);
} }
return 0; return 0;
} }
@ -62,7 +66,7 @@ ADT_Node* adt_find( ADT_Node* node, char const* name, b32 deep_search )
return NULL; return NULL;
} }
for ( sw i = 0; i < node->nodes.num(); i++ ) for ( ssize i = 0; i < scast(ssize, num(node->nodes)); i++ )
{ {
if ( ! str_compare( node->nodes[ i ].name, name ) ) if ( ! str_compare( node->nodes[ i ].name, name ) )
{ {
@ -72,7 +76,7 @@ ADT_Node* adt_find( ADT_Node* node, char const* name, b32 deep_search )
if ( deep_search ) if ( deep_search )
{ {
for ( sw i = 0; i < node->nodes.num(); i++ ) for ( ssize i = 0; i < scast(ssize, num(node->nodes)); i++ )
{ {
ADT_Node* res = adt_find( node->nodes + i, name, deep_search ); ADT_Node* res = adt_find( node->nodes + i, name, deep_search );
@ -107,7 +111,7 @@ internal ADT_Node* _adt_get_value( ADT_Node* node, char const* value )
file_stream_open( &tmp, heap(), ( u8* )back, size_of( back ), EFileStream_WRITABLE ); file_stream_open( &tmp, heap(), ( u8* )back, size_of( back ), EFileStream_WRITABLE );
adt_print_number( &tmp, node ); adt_print_number( &tmp, node );
sw fsize = 0; ssize fsize = 0;
u8* buf = file_stream_buf( &tmp, &fsize ); u8* buf = file_stream_buf( &tmp, &fsize );
if ( ! str_compare( ( char const* )buf, value ) ) if ( ! str_compare( ( char const* )buf, value ) )
@ -128,7 +132,7 @@ internal ADT_Node* _adt_get_value( ADT_Node* node, char const* value )
internal ADT_Node* _adt_get_field( ADT_Node* node, char* name, char* value ) internal ADT_Node* _adt_get_field( ADT_Node* node, char* name, char* value )
{ {
for ( sw i = 0; i < node->nodes.num(); i++ ) for ( ssize i = 0; i < scast(ssize, num(node->nodes)); i++ )
{ {
if ( ! str_compare( node->nodes[ i ].name, name ) ) if ( ! str_compare( node->nodes[ i ].name, name ) )
{ {
@ -203,7 +207,7 @@ ADT_Node* adt_query( ADT_Node* node, char const* uri )
/* run a value comparison against any child that is an object node */ /* run a value comparison against any child that is an object node */
else if ( node->type == EADT_TYPE_ARRAY ) else if ( node->type == EADT_TYPE_ARRAY )
{ {
for ( sw i = 0; i < node->nodes.num(); i++ ) for ( ssize i = 0; i < scast(ssize, num(node->nodes)); i++ )
{ {
ADT_Node* child = &node->nodes[ i ]; ADT_Node* child = &node->nodes[ i ];
if ( child->type != EADT_TYPE_OBJECT ) if ( child->type != EADT_TYPE_OBJECT )
@ -221,7 +225,7 @@ ADT_Node* adt_query( ADT_Node* node, char const* uri )
/* [value] */ /* [value] */
else else
{ {
for ( sw i = 0; i < node->nodes.num(); i++ ) for ( ssize i = 0; i < scast(ssize, num(node->nodes)); i++ )
{ {
ADT_Node* child = &node->nodes[ i ]; ADT_Node* child = &node->nodes[ i ];
if ( _adt_get_value( child, l_b2 ) ) if ( _adt_get_value( child, l_b2 ) )
@ -252,8 +256,8 @@ ADT_Node* adt_query( ADT_Node* node, char const* uri )
/* handle array index lookup */ /* handle array index lookup */
else else
{ {
sw idx = ( sw )str_to_i64( buf, NULL, 10 ); ssize idx = ( ssize )str_to_i64( buf, NULL, 10 );
if ( idx >= 0 && idx < node->nodes.num() ) if ( idx >= 0 && idx < scast(ssize, num(node->nodes)) )
{ {
found_node = &node->nodes[ idx ]; found_node = &node->nodes[ idx ];
@ -268,7 +272,7 @@ ADT_Node* adt_query( ADT_Node* node, char const* uri )
return found_node; return found_node;
} }
ADT_Node* adt_alloc_at( ADT_Node* parent, sw index ) ADT_Node* adt_alloc_at( ADT_Node* parent, ssize index )
{ {
if ( ! parent || ( parent->type != EADT_TYPE_OBJECT && parent->type != EADT_TYPE_ARRAY ) ) if ( ! parent || ( parent->type != EADT_TYPE_OBJECT && parent->type != EADT_TYPE_ARRAY ) )
{ {
@ -278,15 +282,16 @@ ADT_Node* adt_alloc_at( ADT_Node* parent, sw index )
if ( ! parent->nodes ) if ( ! parent->nodes )
return NULL; return NULL;
if ( index < 0 || index > parent->nodes.num() ) if ( index < 0 || index > scast(ssize, num(parent->nodes)) )
return NULL; return NULL;
ADT_Node o = { 0 }; ADT_Node o = { 0 };
o.parent = parent; o.parent = parent;
if ( ! parent->nodes.append_at( o, index ) ) if ( ! append_at( & parent->nodes, o, index ) )
return NULL; return NULL;
return parent->nodes + index; ADT_Node* node = & parent->nodes[index];
return node;
} }
ADT_Node* adt_alloc( ADT_Node* parent ) ADT_Node* adt_alloc( ADT_Node* parent )
@ -299,7 +304,7 @@ ADT_Node* adt_alloc( ADT_Node* parent )
if ( ! parent->nodes ) if ( ! parent->nodes )
return NULL; return NULL;
return adt_alloc_at( parent, parent->nodes.num() ); return adt_alloc_at( parent, num(parent->nodes) );
} }
b8 adt_set_obj( ADT_Node* obj, char const* name, AllocatorInfo backing ) b8 adt_set_obj( ADT_Node* obj, char const* name, AllocatorInfo backing )
@ -333,7 +338,7 @@ b8 adt_set_int( ADT_Node* obj, char const* name, s64 value )
return true; return true;
} }
ADT_Node* adt_move_node_at( ADT_Node* node, ADT_Node* new_parent, sw index ) ADT_Node* adt_move_node_at( ADT_Node* node, ADT_Node* new_parent, ssize index )
{ {
GEN_ASSERT_NOT_NULL( node ); GEN_ASSERT_NOT_NULL( node );
GEN_ASSERT_NOT_NULL( new_parent ); GEN_ASSERT_NOT_NULL( new_parent );
@ -353,7 +358,7 @@ ADT_Node* adt_move_node( ADT_Node* node, ADT_Node* new_parent )
GEN_ASSERT_NOT_NULL( node ); GEN_ASSERT_NOT_NULL( node );
GEN_ASSERT_NOT_NULL( new_parent ); GEN_ASSERT_NOT_NULL( new_parent );
GEN_ASSERT( new_parent->type == EADT_TYPE_ARRAY || new_parent->type == EADT_TYPE_OBJECT ); GEN_ASSERT( new_parent->type == EADT_TYPE_ARRAY || new_parent->type == EADT_TYPE_OBJECT );
return adt_move_node_at( node, new_parent, new_parent->nodes.num() ); return adt_move_node_at( node, new_parent, num(new_parent->nodes) );
} }
void adt_swap_nodes( ADT_Node* node, ADT_Node* other_node ) void adt_swap_nodes( ADT_Node* node, ADT_Node* other_node )
@ -362,8 +367,8 @@ void adt_swap_nodes( ADT_Node* node, ADT_Node* other_node )
GEN_ASSERT_NOT_NULL( other_node ); GEN_ASSERT_NOT_NULL( other_node );
ADT_Node* parent = node->parent; ADT_Node* parent = node->parent;
ADT_Node* other_parent = other_node->parent; ADT_Node* other_parent = other_node->parent;
sw index = ( pointer_diff( parent->nodes, node ) / size_of( ADT_Node ) ); ssize index = ( pointer_diff( parent->nodes, node ) / size_of( ADT_Node ) );
sw index2 = ( pointer_diff( other_parent->nodes, other_node ) / size_of( ADT_Node ) ); ssize index2 = ( pointer_diff( other_parent->nodes, other_node ) / size_of( ADT_Node ) );
ADT_Node temp = parent->nodes[ index ]; ADT_Node temp = parent->nodes[ index ];
temp.parent = other_parent; temp.parent = other_parent;
other_parent->nodes[ index2 ].parent = parent; other_parent->nodes[ index2 ].parent = parent;
@ -376,8 +381,8 @@ void adt_remove_node( ADT_Node* node )
GEN_ASSERT_NOT_NULL( node ); GEN_ASSERT_NOT_NULL( node );
GEN_ASSERT_NOT_NULL( node->parent ); GEN_ASSERT_NOT_NULL( node->parent );
ADT_Node* parent = node->parent; ADT_Node* parent = node->parent;
sw index = ( pointer_diff( parent->nodes, node ) / size_of( ADT_Node ) ); ssize index = ( pointer_diff( parent->nodes, node ) / size_of( ADT_Node ) );
parent->nodes.remove_at( index ); remove_at( parent->nodes, index );
} }
ADT_Node* adt_append_obj( ADT_Node* parent, char const* name ) ADT_Node* adt_append_obj( ADT_Node* parent, char const* name )
@ -385,7 +390,7 @@ ADT_Node* adt_append_obj( ADT_Node* parent, char const* name )
ADT_Node* o = adt_alloc( parent ); ADT_Node* o = adt_alloc( parent );
if ( ! o ) if ( ! o )
return NULL; return NULL;
if ( adt_set_obj( o, name, parent->nodes.get_header()->Allocator ) ) if ( adt_set_obj( o, name, get_header(parent->nodes)->Allocator ) )
{ {
adt_remove_node( o ); adt_remove_node( o );
return NULL; return NULL;
@ -398,7 +403,9 @@ ADT_Node* adt_append_arr( ADT_Node* parent, char const* name )
ADT_Node* o = adt_alloc( parent ); ADT_Node* o = adt_alloc( parent );
if ( ! o ) if ( ! o )
return NULL; return NULL;
if ( adt_set_arr( o, name, parent->nodes.get_header()->Allocator ) )
ArrayHeader* node_header = get_header(parent->nodes);
if ( adt_set_arr( o, name, node_header->Allocator ) )
{ {
adt_remove_node( o ); adt_remove_node( o );
return NULL; return NULL;
@ -443,7 +450,7 @@ char* adt_parse_number_strict( ADT_Node* node, char* base_str )
while ( *e ) while ( *e )
++e; ++e;
while ( *p && ( str_find( "eE.+-", *p ) || char_is_hex_digit( *p ) ) ) while ( *p && ( char_first_occurence( "eE.+-", *p ) || char_is_hex_digit( *p ) ) )
{ {
++p; ++p;
} }
@ -472,7 +479,7 @@ char* adt_parse_number( ADT_Node* node, char* base_str )
u8 node_props = 0; u8 node_props = 0;
/* skip false positives and special cases */ /* skip false positives and special cases */
if ( ! ! str_find( "eE", *p ) || ( ! ! str_find( ".+-", *p ) && ! char_is_hex_digit( *( p + 1 ) ) && *( p + 1 ) != '.' ) ) if ( ! ! char_first_occurence( "eE", *p ) || ( ! ! char_first_occurence( ".+-", *p ) && ! char_is_hex_digit( *( p + 1 ) ) && *( p + 1 ) != '.' ) )
{ {
return ++base_str; return ++base_str;
} }
@ -480,7 +487,7 @@ char* adt_parse_number( ADT_Node* node, char* base_str )
node_type = EADT_TYPE_INTEGER; node_type = EADT_TYPE_INTEGER;
neg_zero = false; neg_zero = false;
sw ib = 0; ssize ib = 0;
char buf[ 48 ] = { 0 }; char buf[ 48 ] = { 0 };
if ( *e == '+' ) if ( *e == '+' )
@ -546,9 +553,9 @@ char* adt_parse_number( ADT_Node* node, char* base_str )
f32 eb = 10; f32 eb = 10;
char expbuf[ 6 ] = { 0 }; char expbuf[ 6 ] = { 0 };
sw expi = 0; ssize expi = 0;
if ( *e && ! ! str_find( "eE", *e ) ) if ( *e && ! ! char_first_occurence( "eE", *e ) )
{ {
++e; ++e;
if ( *e == '+' || *e == '-' || char_is_digit( *e ) ) if ( *e == '+' || *e == '-' || char_is_digit( *e ) )
@ -591,7 +598,7 @@ char* adt_parse_number( ADT_Node* node, char* base_str )
#ifndef GEN_PARSER_DISABLE_ANALYSIS #ifndef GEN_PARSER_DISABLE_ANALYSIS
char *q = buf, *base_string = q, *base_string2 = q; char *q = buf, *base_string = q, *base_string2 = q;
base_string = zpl_cast( char* ) str_skip( base_string, '.' ); base_string = ccast( char*, str_skip( base_string, '.' ));
*base_string = '\0'; *base_string = '\0';
base_string2 = base_string + 1; base_string2 = base_string + 1;
char* base_string_off = base_string2; char* base_string_off = base_string2;
@ -744,7 +751,7 @@ ADT_Error adt_print_string( FileInfo* file, ADT_Node* node, char const* escaped_
{ {
p = str_skip_any( p, escaped_chars ); p = str_skip_any( p, escaped_chars );
_adt_fprintf( file, "%.*s", pointer_diff( b, p ), b ); _adt_fprintf( file, "%.*s", pointer_diff( b, p ), b );
if ( *p && ! ! str_find( escaped_chars, *p ) ) if ( *p && ! ! char_first_occurence( escaped_chars, *p ) )
{ {
_adt_fprintf( file, "%s%c", escape_symbol, *p ); _adt_fprintf( file, "%s%c", escape_symbol, *p );
p++; p++;
@ -799,7 +806,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;
@ -813,13 +819,13 @@ u8 csv_parse_delimiter( CSV_Object* root, char* text, AllocatorInfo allocator, b
char* beginChar; char* beginChar;
char* endChar; char* endChar;
sw columnIndex = 0; ssize columnIndex = 0;
sw totalColumnIndex = 0; ssize totalColumnIndex = 0;
do do
{ {
char delimiter = 0; char delimiter = 0;
currentChar = zpl_cast( char* ) str_trim( currentChar, false ); currentChar = ccast( char*, str_trim( currentChar, false ));
if ( *currentChar == 0 ) if ( *currentChar == 0 )
break; break;
@ -843,7 +849,7 @@ u8 csv_parse_delimiter( CSV_Object* root, char* text, AllocatorInfo allocator, b
#endif #endif
do do
{ {
endChar = zpl_cast( char* ) str_skip( endChar, '"' ); endChar = ccast( char*, str_skip( endChar, '"' ));
if ( *endChar && *( endChar + 1 ) == '"' ) if ( *endChar && *( endChar + 1 ) == '"' )
{ {
@ -862,7 +868,7 @@ u8 csv_parse_delimiter( CSV_Object* root, char* text, AllocatorInfo allocator, b
} }
*endChar = 0; *endChar = 0;
currentChar = zpl_cast( char* ) str_trim( endChar + 1, true ); currentChar = ccast( char*, str_trim( endChar + 1, true ));
delimiter = * currentChar; delimiter = * currentChar;
/* unescape escaped quotes (so that unescaped text escapes :) */ /* unescape escaped quotes (so that unescaped text escapes :) */
@ -899,7 +905,7 @@ u8 csv_parse_delimiter( CSV_Object* root, char* text, AllocatorInfo allocator, b
if ( * endChar ) if ( * endChar )
{ {
currentChar = zpl_cast( char* ) str_trim( endChar, true ); currentChar = ccast( char*, str_trim( endChar, true ));
while ( char_is_space( *( endChar - 1 ) ) ) while ( char_is_space( *( endChar - 1 ) ) )
{ {
@ -943,12 +949,12 @@ u8 csv_parse_delimiter( CSV_Object* root, char* text, AllocatorInfo allocator, b
} }
} }
if ( columnIndex >= root->nodes.num() ) if ( columnIndex >= scast(ssize, num(root->nodes)) )
{ {
adt_append_arr( root, NULL ); adt_append_arr( root, NULL );
} }
root->nodes[ columnIndex ].nodes.append( rowItem ); append( & root->nodes[ columnIndex ].nodes, rowItem );
if ( delimiter == delim ) if ( delimiter == delim )
{ {
@ -976,7 +982,7 @@ u8 csv_parse_delimiter( CSV_Object* root, char* text, AllocatorInfo allocator, b
} }
while ( *currentChar ); while ( *currentChar );
if ( root->nodes.num() == 0 ) if (num( root->nodes) == 0 )
{ {
GEN_CSV_ASSERT( "unexpected end of input. stream is empty." ); GEN_CSV_ASSERT( "unexpected end of input. stream is empty." );
error = ECSV_Error__UNEXPECTED_END_OF_INPUT; error = ECSV_Error__UNEXPECTED_END_OF_INPUT;
@ -986,12 +992,12 @@ u8 csv_parse_delimiter( CSV_Object* root, char* text, AllocatorInfo allocator, b
/* consider first row as a header. */ /* consider first row as a header. */
if ( has_header ) if ( has_header )
{ {
for ( sw i = 0; i < root->nodes.num(); i++ ) for ( ssize i = 0; i < scast(ssize, num(root->nodes)); i++ )
{ {
CSV_Object* col = root->nodes + i; CSV_Object* col = root->nodes + i;
CSV_Object* hdr = col->nodes; CSV_Object* hdr = col->nodes;
col->name = hdr->string; col->name = hdr->string;
col->nodes.remove_at( 0 ); remove_at(col->nodes, 0 );
} }
} }
@ -1054,11 +1060,11 @@ void csv_write_delimiter( FileInfo* file, CSV_Object* obj, char delimiter )
GEN_ASSERT_NOT_NULL( file ); GEN_ASSERT_NOT_NULL( file );
GEN_ASSERT_NOT_NULL( obj ); GEN_ASSERT_NOT_NULL( obj );
GEN_ASSERT( obj->nodes ); GEN_ASSERT( obj->nodes );
sw cols = obj->nodes.num(); ssize cols = num(obj->nodes);
if ( cols == 0 ) if ( cols == 0 )
return; return;
sw rows = obj->nodes[ 0 ].nodes.num(); ssize rows = num(obj->nodes[ 0 ].nodes);
if ( rows == 0 ) if ( rows == 0 )
return; return;
@ -1066,7 +1072,7 @@ void csv_write_delimiter( FileInfo* file, CSV_Object* obj, char delimiter )
if ( has_headers ) if ( has_headers )
{ {
for ( sw i = 0; i < cols; i++ ) for ( ssize i = 0; i < cols; i++ )
{ {
_csv_write_header( file, &obj->nodes[ i ] ); _csv_write_header( file, &obj->nodes[ i ] );
if ( i + 1 != cols ) if ( i + 1 != cols )
@ -1077,9 +1083,9 @@ void csv_write_delimiter( FileInfo* file, CSV_Object* obj, char delimiter )
str_fmt_file( file, "\n" ); str_fmt_file( file, "\n" );
} }
for ( sw r = 0; r < rows; r++ ) for ( ssize r = 0; r < rows; r++ )
{ {
for ( sw i = 0; i < cols; i++ ) for ( ssize i = 0; i < cols; i++ )
{ {
_csv_write_record( file, &obj->nodes[ i ].nodes[ r ] ); _csv_write_record( file, &obj->nodes[ i ].nodes[ r ] );
if ( i + 1 != cols ) if ( i + 1 != cols )
@ -1096,11 +1102,13 @@ String csv_write_string_delimiter( AllocatorInfo a, CSV_Object* obj, char delimi
FileInfo tmp; FileInfo tmp;
file_stream_new( &tmp, a ); file_stream_new( &tmp, a );
csv_write_delimiter( &tmp, obj, delimiter ); csv_write_delimiter( &tmp, obj, delimiter );
sw fsize;
ssize fsize;
u8* buf = file_stream_buf( &tmp, &fsize ); u8* buf = file_stream_buf( &tmp, &fsize );
String output = String::make_length( a, ( char* )buf, fsize ); String output = string_make_length( a, ( char* )buf, fsize );
file_close( &tmp ); file_close( &tmp );
return output; return output;
} }
#pragma endregion CSV #pragma endregion CSV

View File

@ -1,3 +1,7 @@
#ifdef GEN_INTELLISENSE_DIRECTIVES
# pragma once
#endif
#pragma region ADT #pragma region ADT
enum ADT_Type : u32 enum ADT_Type : u32
@ -79,7 +83,7 @@ struct ADT_Node
union union
{ {
char const* string; char const* string;
Array<ADT_Node> nodes; ///< zpl_array Array(ADT_Node) nodes; ///< zpl_array
struct struct
{ {
@ -174,7 +178,7 @@ ADT_Node* adt_find( ADT_Node* node, char const* name, b32 deep_search );
* @param index * @param index
* @return zpl_adt_node * node * @return zpl_adt_node * node
*/ */
ADT_Node* adt_alloc_at( ADT_Node* parent, sw index ); ADT_Node* adt_alloc_at( ADT_Node* parent, ssize index );
/** /**
* @brief Allocate an unitialised node within a container. * @brief Allocate an unitialised node within a container.
@ -192,7 +196,7 @@ ADT_Node* adt_alloc( ADT_Node* parent );
* @param index * @param index
* @return zpl_adt_node * node * @return zpl_adt_node * node
*/ */
ADT_Node* adt_move_node_at( ADT_Node* node, ADT_Node* new_parent, sw index ); ADT_Node* adt_move_node_at( ADT_Node* node, ADT_Node* new_parent, ssize index );
/** /**
* @brief Move an existing node to a new container. * @brief Move an existing node to a new container.
@ -396,28 +400,31 @@ enum CSV_Error : u32
typedef ADT_Node CSV_Object; typedef ADT_Node CSV_Object;
GEN_DEF_INLINE u8 csv_parse( CSV_Object* root, char* text, AllocatorInfo allocator, b32 has_header ); u8 csv_parse( CSV_Object* root, char* text, AllocatorInfo allocator, b32 has_header );
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 );
void csv_free( CSV_Object* obj ); void csv_free( CSV_Object* obj );
GEN_DEF_INLINE void csv_write( FileInfo* file, CSV_Object* obj ); void csv_write( FileInfo* file, CSV_Object* obj );
GEN_DEF_INLINE String csv_write_string( AllocatorInfo a, CSV_Object* obj ); String csv_write_string( AllocatorInfo a, CSV_Object* obj );
void csv_write_delimiter( FileInfo* file, CSV_Object* obj, char delim ); void csv_write_delimiter( FileInfo* file, CSV_Object* obj, char delim );
String csv_write_string_delimiter( AllocatorInfo a, CSV_Object* obj, char delim ); String csv_write_string_delimiter( AllocatorInfo a, CSV_Object* obj, char delim );
/* inline */ /* inline */
GEN_IMPL_INLINE u8 csv_parse( CSV_Object* root, char* text, AllocatorInfo allocator, b32 has_header ) inline
u8 csv_parse( CSV_Object* root, char* text, AllocatorInfo allocator, b32 has_header )
{ {
return csv_parse_delimiter( root, text, allocator, has_header, ',' ); return csv_parse_delimiter( root, text, allocator, has_header, ',' );
} }
GEN_IMPL_INLINE void csv_write( FileInfo* file, CSV_Object* obj ) inline
void csv_write( FileInfo* file, CSV_Object* obj )
{ {
csv_write_delimiter( file, obj, ',' ); csv_write_delimiter( file, obj, ',' );
} }
GEN_IMPL_INLINE String csv_write_string( AllocatorInfo a, CSV_Object* obj ) inline
String csv_write_string( AllocatorInfo a, CSV_Object* obj )
{ {
return csv_write_string_delimiter( a, obj, ',' ); return csv_write_string_delimiter( a, obj, ',' );
} }

View File

@ -1,3 +1,7 @@
#ifdef GEN_INTELLISENSE_DIRECTIVES
# pragma once
#endif
#pragma region Platform Detection #pragma region Platform Detection
/* Platform architecture */ /* Platform architecture */
@ -72,13 +76,18 @@
/* Platform compiler */ /* Platform compiler */
#if defined( _MSC_VER ) #if defined( _MSC_VER )
# define GEN_COMPILER_MSVC 1 # define GEN_COMPILER_CLANG 0
# define GEN_COMPILER_MSVC 1
# define GEN_COMPILER_GCC 0
#elif defined( __GNUC__ ) #elif defined( __GNUC__ )
# define GEN_COMPILER_GCC 1 # define GEN_COMPILER_CLANG 0
# define GEN_COMPILER_MSVC 0
# define GEN_COMPILER_GCC 1
#elif defined( __clang__ ) #elif defined( __clang__ )
# define GEN_COMPILER_CLANG 1 # define GEN_COMPILER_CLANG 1
#elif defined( __MINGW32__ ) # define GEN_COMPILER_MSVC 0
# define GEN_COMPILER_MINGW 1 # define GEN_COMPILER_GCC 1
#else
# error Unknown compiler # error Unknown compiler
#endif #endif
@ -97,35 +106,49 @@
# define GEN_GCC_VERSION_CHECK(major,minor,patch) (0) # define GEN_GCC_VERSION_CHECK(major,minor,patch) (0)
#endif #endif
#define GEN_DEF_INLINE static #ifndef GEN_COMPILER_C
#define GEN_IMPL_INLINE static inline # if defined(__STDC_VERSION__)
# define GEN_COMPILER_C 1
#ifdef GEN_COMPILER_MSVC # else
# define forceinline __forceinline # define GEN_COMPILER_C 0
# define neverinline __declspec( noinline ) # endif
#elif defined(GEN_COMPILER_GCC)
# define forceinline inline __attribute__((__always_inline__))
# define neverinline __attribute__( ( __noinline__ ) )
#elif defined(GEN_COMPILER_CLANG)
#if __has_attribute(__always_inline__)
# define forceinline inline __attribute__((__always_inline__))
# define neverinline __attribute__( ( __noinline__ ) )
#else
# define forceinline
# define neverinline
#endif
#else
# define forceinline
# define neverinline
#endif #endif
#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
#if GEN_COMPILER_C
#include <assert.h>
#endif
#pragma endregion Mandatory Includes #pragma endregion Mandatory Includes
#if GEN_DONT_USE_NAMESPACE || GEN_COMPILER_C
# if GEN_COMPILER_C
# define GEN_NS_ENUM_BEGIN
# define GEN_NS_ENUM_END
# define GEN_NS
# define GEN_NS_BEGIN
# define GEN_NS_END
# else
# define GEN_NS_ENUM_BEGIN namespace gen_internal_enums {
# define GEN_NS_ENUM_END }
# define GEN_NS ::
# define GEN_NS_BEGIN
# define GEN_NS_END
# endif
#else
# define GEN_NS_ENUM_BEGIN namespace gen_internal_enums {
# define GEN_NS_ENUM_END }
# define GEN_NS gen::
# define GEN_NS_BEGIN namespace gen {
# define GEN_NS_END }
#endif

View File

@ -1,3 +1,10 @@
#ifdef GEN_INTELLISENSE_DIRECTIVES
# pragma once
# include "filesystem.hpp"
# include "strings.hpp"
# include "string_ops.cpp"
#endif
#pragma region Printing #pragma region Printing
enum enum
@ -34,10 +41,10 @@ struct _format_info
s32 precision; s32 precision;
}; };
internal sw _print_string( char* text, sw max_len, _format_info* info, char const* str ) internal ssize _print_string( char* text, ssize max_len, _format_info* info, char const* str )
{ {
sw res = 0, len = 0; ssize res = 0, len = 0;
sw remaining = max_len; ssize remaining = max_len;
char* begin = text; char* begin = text;
if ( str == NULL && max_len >= 6 ) if ( str == NULL && max_len >= 6 )
@ -47,7 +54,8 @@ internal sw _print_string( char* text, sw max_len, _format_info* info, char cons
} }
if ( info && info->precision >= 0 ) if ( info && info->precision >= 0 )
len = str_len( str, info->precision ); // Made the design decision for this library that precision is the length of the string.
len = info->precision;
else else
len = str_len( str ); len = str_len( str );
@ -67,7 +75,7 @@ internal sw _print_string( char* text, sw max_len, _format_info* info, char cons
if ( info->width > res ) if ( info->width > res )
{ {
sw padding = info->width - len; ssize padding = info->width - len;
char pad = ( info->flags & GEN_FMT_ZERO ) ? '0' : ' '; char pad = ( info->flags & GEN_FMT_ZERO ) ? '0' : ' ';
while ( padding-- > 0 && remaining-- > 0 ) while ( padding-- > 0 && remaining-- > 0 )
@ -78,7 +86,7 @@ internal sw _print_string( char* text, sw max_len, _format_info* info, char cons
{ {
if ( info && ( info->width > res ) ) if ( info && ( info->width > res ) )
{ {
sw padding = info->width - len; ssize padding = info->width - len;
char pad = ( info->flags & GEN_FMT_ZERO ) ? '0' : ' '; char pad = ( info->flags & GEN_FMT_ZERO ) ? '0' : ' ';
while ( padding-- > 0 && remaining-- > 0 ) while ( padding-- > 0 && remaining-- > 0 )
*text++ = pad, res++; *text++ = pad, res++;
@ -100,16 +108,16 @@ internal sw _print_string( char* text, sw max_len, _format_info* info, char cons
return res; return res;
} }
internal sw _print_char( char* text, sw max_len, _format_info* info, char arg ) internal ssize _print_char( char* text, ssize max_len, _format_info* info, char arg )
{ {
char str[ 2 ] = ""; char str[ 2 ] = "";
str[ 0 ] = arg; str[ 0 ] = arg;
return _print_string( text, max_len, info, str ); return _print_string( text, max_len, info, str );
} }
internal sw _print_repeated_char( char* text, sw max_len, _format_info* info, char arg ) internal ssize _print_repeated_char( char* text, ssize max_len, _format_info* info, char arg )
{ {
sw res = 0; ssize res = 0;
s32 rem = ( info ) ? ( info->width > 0 ) ? info->width : 1 : 1; s32 rem = ( info ) ? ( info->width > 0 ) ? info->width : 1 : 1;
res = rem; res = rem;
while ( rem-- > 0 ) while ( rem-- > 0 )
@ -118,24 +126,24 @@ internal sw _print_repeated_char( char* text, sw max_len, _format_info* info, ch
return res; return res;
} }
internal sw _print_i64( char* text, sw max_len, _format_info* info, s64 value ) internal ssize _print_i64( char* text, ssize max_len, _format_info* info, s64 value )
{ {
char num[ 130 ]; char num[ 130 ];
i64_to_str( value, num, info ? info->base : 10 ); i64_to_str( value, num, info ? info->base : 10 );
return _print_string( text, max_len, info, num ); return _print_string( text, max_len, info, num );
} }
internal sw _print_u64( char* text, sw max_len, _format_info* info, u64 value ) internal ssize _print_u64( char* text, ssize max_len, _format_info* info, u64 value )
{ {
char num[ 130 ]; char num[ 130 ];
u64_to_str( value, num, info ? info->base : 10 ); u64_to_str( value, num, info ? info->base : 10 );
return _print_string( text, max_len, info, num ); return _print_string( text, max_len, info, num );
} }
internal sw _print_f64( char* text, sw max_len, _format_info* info, b32 is_hexadecimal, f64 arg ) internal ssize _print_f64( char* text, ssize max_len, _format_info* info, b32 is_hexadecimal, f64 arg )
{ {
// TODO: Handle exponent notation // TODO: Handle exponent notation
sw width, len, remaining = max_len; ssize width, len, remaining = max_len;
char* text_begin = text; char* text_begin = text;
if ( arg ) if ( arg )
@ -155,7 +163,7 @@ internal sw _print_f64( char* text, sw max_len, _format_info* info, b32 is_hexad
text++; text++;
} }
value = zpl_cast( u64 ) arg; value = scast( u64, arg);
len = _print_u64( text, remaining, NULL, value ); len = _print_u64( text, remaining, NULL, value );
text += len; text += len;
@ -176,14 +184,14 @@ internal sw _print_f64( char* text, sw max_len, _format_info* info, b32 is_hexad
text++; text++;
while ( info->precision-- > 0 ) while ( info->precision-- > 0 )
{ {
value = zpl_cast( u64 )( arg * mult ); value = scast( u64, arg * mult );
len = _print_u64( text, remaining, NULL, value ); len = _print_u64( text, remaining, NULL, value );
text += len; text += len;
if ( len >= remaining ) if ( len >= remaining )
remaining = min( remaining, 1 ); remaining = min( remaining, 1 );
else else
remaining -= len; remaining -= len;
arg -= zpl_cast( f64 ) value / mult; arg -= scast( f64, value / mult);
mult *= 10; mult *= 10;
} }
} }
@ -231,15 +239,15 @@ internal sw _print_f64( char* text, sw max_len, _format_info* info, b32 is_hexad
return ( text - text_begin ); return ( text - text_begin );
} }
neverinline sw str_fmt_va( char* text, sw max_len, char const* fmt, va_list va ) neverinline ssize str_fmt_va( char* text, ssize max_len, char const* fmt, va_list va )
{ {
char const* text_begin = text; char const* text_begin = text;
sw remaining = max_len, res; ssize remaining = max_len, res;
while ( *fmt ) while ( *fmt )
{ {
_format_info info = { 0 }; _format_info info = { 0 };
sw len = 0; ssize len = 0;
info.precision = -1; info.precision = -1;
while ( *fmt && *fmt != '%' && remaining ) while ( *fmt && *fmt != '%' && remaining )
@ -303,7 +311,7 @@ neverinline sw str_fmt_va( char* text, sw max_len, char const* fmt, va_list va )
} }
else else
{ {
info.width = zpl_cast( s32 ) str_to_i64( fmt, zpl_cast( char** ) & fmt, 10 ); info.width = scast( s32, str_to_i64( fmt, ccast( char**, & fmt), 10 ));
if ( info.width != 0 ) if ( info.width != 0 )
{ {
info.flags |= GEN_FMT_WIDTH; info.flags |= GEN_FMT_WIDTH;
@ -321,7 +329,7 @@ neverinline sw str_fmt_va( char* text, sw max_len, char const* fmt, va_list va )
} }
else else
{ {
info.precision = zpl_cast( s32 ) str_to_i64( fmt, zpl_cast( char** ) & fmt, 10 ); info.precision = scast( s32, str_to_i64( fmt, ccast( char**, & fmt), 10 ));
} }
info.flags &= ~GEN_FMT_ZERO; info.flags &= ~GEN_FMT_ZERO;
} }
@ -403,13 +411,22 @@ neverinline sw str_fmt_va( char* text, sw max_len, char const* fmt, va_list va )
break; break;
case 'c' : case 'c' :
len = _print_char( text, remaining, &info, zpl_cast( char ) va_arg( va, int ) ); len = _print_char( text, remaining, &info, scast( char, va_arg( va, int ) ));
break; break;
case 's' : case 's' :
len = _print_string( text, remaining, &info, va_arg( va, char* ) ); len = _print_string( text, remaining, &info, va_arg( va, char* ) );
break; break;
case 'S':
{
String gen_str = String { va_arg( va, char*) };
info.precision = length(gen_str);
len = _print_string( text, remaining, &info, gen_str );
}
break;
case 'r' : case 'r' :
len = _print_repeated_char( text, remaining, &info, va_arg( va, int ) ); len = _print_repeated_char( text, remaining, &info, va_arg( va, int ) );
break; break;
@ -438,25 +455,25 @@ neverinline sw str_fmt_va( char* text, sw max_len, char const* fmt, va_list va )
switch ( info.flags & GEN_FMT_INTS ) switch ( info.flags & GEN_FMT_INTS )
{ {
case GEN_FMT_CHAR : case GEN_FMT_CHAR :
value = zpl_cast( u64 ) zpl_cast( u8 ) va_arg( va, int ); value = scast( u64, scast( u8, va_arg( va, int )));
break; break;
case GEN_FMT_SHORT : case GEN_FMT_SHORT :
value = zpl_cast( u64 ) zpl_cast( u16 ) va_arg( va, int ); value = scast( u64, scast( u16, va_arg( va, int )));
break; break;
case GEN_FMT_LONG : case GEN_FMT_LONG:
value = zpl_cast( u64 ) va_arg( va, unsigned long ); value = scast( u64, va_arg( va, unsigned long ));
break; break;
case GEN_FMT_LLONG : case GEN_FMT_LLONG :
value = zpl_cast( u64 ) va_arg( va, unsigned long long ); value = scast( u64, va_arg( va, unsigned long long ));
break; break;
case GEN_FMT_SIZE : case GEN_FMT_SIZE :
value = zpl_cast( u64 ) va_arg( va, uw ); value = scast( u64, va_arg( va, usize ));
break; break;
case GEN_FMT_INTPTR : case GEN_FMT_INTPTR :
value = zpl_cast( u64 ) va_arg( va, uptr ); value = scast( u64, va_arg( va, uptr ));
break; break;
default : default :
value = zpl_cast( u64 ) va_arg( va, unsigned int ); value = scast( u64, va_arg( va, unsigned int ));
break; break;
} }
@ -468,25 +485,25 @@ neverinline sw str_fmt_va( char* text, sw max_len, char const* fmt, va_list va )
switch ( info.flags & GEN_FMT_INTS ) switch ( info.flags & GEN_FMT_INTS )
{ {
case GEN_FMT_CHAR : case GEN_FMT_CHAR :
value = zpl_cast( s64 ) zpl_cast( s8 ) va_arg( va, int ); value = scast( s64, scast( s8, va_arg( va, int )));
break; break;
case GEN_FMT_SHORT : case GEN_FMT_SHORT :
value = zpl_cast( s64 ) zpl_cast( s16 ) va_arg( va, int ); value = scast( s64, scast( s16, va_arg( va, int )));
break; break;
case GEN_FMT_LONG : case GEN_FMT_LONG :
value = zpl_cast( s64 ) va_arg( va, long ); value = scast( s64, va_arg( va, long ));
break; break;
case GEN_FMT_LLONG : case GEN_FMT_LLONG :
value = zpl_cast( s64 ) va_arg( va, long long ); value = scast( s64, va_arg( va, long long ));
break; break;
case GEN_FMT_SIZE : case GEN_FMT_SIZE :
value = zpl_cast( s64 ) va_arg( va, uw ); value = scast( s64, va_arg( va, usize ));
break; break;
case GEN_FMT_INTPTR : case GEN_FMT_INTPTR :
value = zpl_cast( s64 ) va_arg( va, uptr ); value = scast( s64, va_arg( va, uptr ));
break; break;
default : default :
value = zpl_cast( s64 ) va_arg( va, int ); value = scast( s64, va_arg( va, int ));
break; break;
} }
@ -523,17 +540,17 @@ char* str_fmt_buf( char const* fmt, ... )
return str; return str;
} }
sw str_fmt_file_va( struct FileInfo* f, char const* fmt, va_list va ) ssize str_fmt_file_va( struct FileInfo* f, char const* fmt, va_list va )
{ {
local_persist thread_local char buf[ GEN_PRINTF_MAXLEN ]; local_persist thread_local char buf[ GEN_PRINTF_MAXLEN ];
sw len = str_fmt_va( buf, size_of( buf ), fmt, va ); ssize len = str_fmt_va( buf, size_of( buf ), fmt, va );
b32 res = file_write( f, buf, len - 1 ); // NOTE: prevent extra whitespace b32 res = file_write( f, buf, len - 1 ); // NOTE: prevent extra whitespace
return res ? len : -1; return res ? len : -1;
} }
sw str_fmt_file( struct FileInfo* f, char const* fmt, ... ) ssize str_fmt_file( struct FileInfo* f, char const* fmt, ... )
{ {
sw res; ssize res;
va_list va; va_list va;
va_start( va, fmt ); va_start( va, fmt );
res = str_fmt_file_va( f, fmt, va ); res = str_fmt_file_va( f, fmt, va );
@ -541,19 +558,29 @@ sw str_fmt_file( struct FileInfo* f, char const* fmt, ... )
return res; return res;
} }
sw str_fmt_out_va( char const* fmt, va_list va ) ssize str_fmt( char* str, ssize n, char const* fmt, ... )
{
ssize res;
va_list va;
va_start( va, fmt );
res = str_fmt_va( str, n, fmt, va );
va_end( va );
return res;
}
ssize 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 );
} }
sw str_fmt_out_err_va( char const* fmt, va_list va ) ssize str_fmt_out_err_va( char const* fmt, va_list va )
{ {
return str_fmt_file_va( file_get_standard( EFileStandard_ERROR ), fmt, va ); return str_fmt_file_va( file_get_standard( EFileStandard_ERROR ), fmt, va );
} }
sw str_fmt_out_err( char const* fmt, ... ) ssize str_fmt_out_err( char const* fmt, ... )
{ {
sw res; ssize res;
va_list va; va_list va;
va_start( va, fmt ); va_start( va, fmt );
res = str_fmt_out_err_va( fmt, va ); res = str_fmt_out_err_va( fmt, va );

View File

@ -1,3 +1,8 @@
#ifdef GEN_INTELLISENSE_DIRECTIVES
# pragma once
# include "string_ops.hpp"
#endif
#pragma region Printing #pragma region Printing
struct FileInfo; struct FileInfo;
@ -7,25 +12,23 @@ struct FileInfo;
#endif #endif
// 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_va ( char* str, sw n, char const* fmt, va_list va ); ssize str_fmt ( char* str, ssize n, char const* fmt, ... );
sw str_fmt_out_va ( char const* fmt, va_list va ); ssize str_fmt_va ( char* str, ssize n, char const* fmt, va_list va );
sw str_fmt_out_err ( char const* fmt, ... ); ssize str_fmt_out_va ( char const* fmt, va_list va );
sw str_fmt_out_err_va( char const* fmt, va_list va ); ssize str_fmt_out_err ( char const* fmt, ... );
ssize str_fmt_out_err_va( char const* fmt, va_list va );
// TODO : Move these to file handling. ssize str_fmt_file ( FileInfo* f, char const* fmt, ... );
ssize str_fmt_file_va ( FileInfo* f, char const* fmt, va_list va );
sw str_fmt_file ( FileInfo* f, char const* fmt, ... );
sw str_fmt_file_va ( FileInfo* f, char const* fmt, va_list va );
constexpr constexpr
char const* Msg_Invalid_Value = "INVALID VALUE PROVIDED"; char const* Msg_Invalid_Value = "INVALID VALUE PROVIDED";
inline inline
sw log_fmt(char const* fmt, ...) ssize log_fmt(char const* fmt, ...)
{ {
sw res; ssize res;
va_list va; va_list va;
va_start(va, fmt); va_start(va, fmt);
@ -35,29 +38,4 @@ 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

@ -1,3 +1,7 @@
#ifdef GEN_INTELLISENSE_DIRECTIVES
# include "header_start.hpp"
#endif
#pragma region Macros and Includes #pragma region Macros and Includes
# include <stdio.h> # include <stdio.h>

View File

@ -1,39 +0,0 @@
#pragma region String
String String::fmt( AllocatorInfo allocator, char* buf, sw buf_size, char const* fmt, ... )
{
va_list va;
va_start( va, fmt );
str_fmt_va( buf, buf_size, fmt, va );
va_end( va );
return make( allocator, buf );
}
String String::fmt_buf( AllocatorInfo allocator, char const* fmt, ... )
{
local_persist thread_local
char buf[ GEN_PRINTF_MAXLEN ] = { 0 };
va_list va;
va_start( va, fmt );
str_fmt_va( buf, GEN_PRINTF_MAXLEN, fmt, va );
va_end( va );
return make( allocator, buf );
}
bool String::append_fmt( char const* fmt, ... )
{
sw res;
char buf[ GEN_PRINTF_MAXLEN ] = { 0 };
va_list va;
va_start( va, fmt );
res = str_fmt_va( buf, count_of( buf ) - 1, fmt, va ) - 1;
va_end( va );
return append( buf, res );
}
#pragma endregion String

View File

@ -1,375 +0,0 @@
#pragma region String
// Constant string with length.
struct StrC
{
sw Len;
char const* Ptr;
operator char const* () const
{
return Ptr;
}
};
#define txt_StrC( text ) StrC { sizeof( text ) - 1, text }
StrC to_StrC( char const* str )
{
return { str_len( str ), str };
}
sw StrC_len( char const* str )
{
return (sw) ( str - 1 );
}
// Dynamic String
// This is directly based off the ZPL string api.
// They used a header pattern
// I kept it for simplicty of porting but its not necessary to keep it that way.
struct String
{
struct Header
{
AllocatorInfo Allocator;
sw Length;
sw Capacity;
};
static
uw grow_formula( uw value )
{
// Using a very aggressive growth formula to reduce time mem_copying with recursive calls to append in this library.
return 4 * value + 8;
}
static
String make( AllocatorInfo allocator, char const* str )
{
sw length = str ? str_len( str ) : 0;
return make_length( allocator, str, length );
}
static
String make( AllocatorInfo allocator, StrC str )
{
return make_length( allocator, str.Ptr, str.Len );
}
static
String make_reserve( AllocatorInfo allocator, sw capacity )
{
constexpr sw header_size = sizeof( Header );
s32 alloc_size = header_size + capacity + 1;
void* allocation = alloc( allocator, alloc_size );
if ( allocation == nullptr )
return { nullptr };
mem_set( allocation, 0, alloc_size );
Header*
header = rcast(Header*, allocation);
header->Allocator = allocator;
header->Capacity = capacity;
header->Length = 0;
String result = { (char*)allocation + header_size };
return result;
}
static
String make_length( AllocatorInfo allocator, char const* str, sw length )
{
constexpr sw header_size = sizeof( Header );
s32 alloc_size = header_size + length + 1;
void* allocation = alloc( allocator, alloc_size );
if ( allocation == nullptr )
return { nullptr };
Header&
header = * rcast(Header*, allocation);
header = { allocator, length, length };
String result = { rcast( char*, allocation) + header_size };
if ( length && str )
mem_copy( result, str, length );
else
mem_set( result, 0, alloc_size - header_size );
result[ length ] = '\0';
return result;
}
static
String fmt( AllocatorInfo allocator, char* buf, sw buf_size, char const* fmt, ... );
static
String fmt_buf( AllocatorInfo allocator, char const* fmt, ... );
static
String join( AllocatorInfo allocator, char const** parts, sw num_parts, char const* glue )
{
String result = make( allocator, "" );
for ( sw idx = 0; idx < num_parts; ++idx )
{
result.append( parts[ idx ] );
if ( idx < num_parts - 1 )
result.append( glue );
}
return result;
}
static
bool are_equal( String lhs, String rhs )
{
if ( lhs.length() != rhs.length() )
return false;
for ( sw idx = 0; idx < lhs.length(); ++idx )
if ( lhs[ idx ] != rhs[ idx ] )
return false;
return true;
}
bool make_space_for( char const* str, sw add_len )
{
sw available = avail_space();
// NOTE: Return if there is enough space left
if ( available >= add_len )
{
return true;
}
else
{
sw new_len, old_size, new_size;
void* ptr;
void* new_ptr;
AllocatorInfo allocator = get_header().Allocator;
Header* header = nullptr;
new_len = grow_formula( length() + add_len );
ptr = & get_header();
old_size = size_of( Header ) + length() + 1;
new_size = size_of( Header ) + new_len + 1;
new_ptr = resize( allocator, ptr, old_size, new_size );
if ( new_ptr == nullptr )
return false;
header = zpl_cast( Header* ) new_ptr;
header->Allocator = allocator;
header->Capacity = new_len;
Data = rcast( char*, header + 1 );
return str;
}
}
bool append( char const* str )
{
return append( str, str_len( str ) );
}
bool append( char const* str, sw length )
{
if ( sptr(str) > 0 )
{
sw curr_len = this->length();
if ( ! make_space_for( str, length ) )
return false;
Header& header = get_header();
mem_copy( Data + curr_len, str, length );
Data[ curr_len + length ] = '\0';
header.Length = curr_len + length;
}
return str;
}
bool append( StrC str)
{
return append( str.Ptr, str.Len );
}
bool append( const String other )
{
return append( other.Data, other.length() );;
}
bool append_fmt( char const* fmt, ... );
sw avail_space() const
{
Header const&
header = * rcast( Header const*, Data - sizeof( Header ));
return header.Capacity - header.Length;
}
sw capacity() const
{
Header const&
header = * rcast( Header const*, Data - sizeof( Header ));
return header.Capacity;
}
void clear()
{
get_header().Length = 0;
}
String duplicate( AllocatorInfo allocator )
{
return make_length( allocator, Data, length() );
}
void free()
{
if ( ! Data )
return;
Header& header = get_header();
gen::free( header.Allocator, & header );
}
Header& get_header()
{
return *(Header*)(Data - sizeof(Header));
}
sw length() const
{
Header const&
header = * rcast( Header const*, Data - sizeof( Header ));
return header.Length;
}
void trim( char const* cut_set )
{
sw len = 0;
char* start_pos = Data;
char* end_pos = Data + length() - 1;
while ( start_pos <= end_pos && char_first_occurence( cut_set, *start_pos ) )
start_pos++;
while ( end_pos > start_pos && char_first_occurence( cut_set, *end_pos ) )
end_pos--;
len = scast( sw, ( start_pos > end_pos ) ? 0 : ( ( end_pos - start_pos ) + 1 ) );
if ( Data != start_pos )
mem_move( Data, start_pos, len );
Data[ len ] = '\0';
get_header().Length = len;
}
void trim_space()
{
return trim( " \t\r\n\v\f" );
}
// For-range support
char* begin()
{
return Data;
}
char* end()
{
Header const&
header = * rcast( Header const*, Data - sizeof( Header ));
return Data + header.Length;
}
operator bool()
{
return Data;
}
operator char* ()
{
return Data;
}
operator char const* () const
{
return Data;
}
operator StrC() const
{
return
{
length(),
Data
};
}
// Used with cached strings
// Essentially makes the string a string view.
String const& operator = ( String const& other ) const
{
if ( this == & other )
return *this;
String& this_ = ccast( String, *this );
this_.Data = other.Data;
return this_;
}
char& operator [] ( sw index )
{
return Data[ index ];
}
char const& operator [] ( sw index ) const
{
return Data[ index ];
}
char* Data = nullptr;
};
struct String_POD
{
char* Data;
operator String()
{
return * rcast(String*, this);
}
};
static_assert( sizeof( String_POD ) == sizeof( String ), "String is not a POD" );
#pragma endregion String

View File

@ -1,7 +1,13 @@
#ifdef GEN_INTELLISENSE_DIRECTIVES
# pragma once
# include "string_ops.hpp"
# include "debug.cpp"
#endif
#pragma region String Ops #pragma region String Ops
internal internal
sw _scan_zpl_i64( const char* text, s32 base, s64* value ) ssize _scan_zpl_i64( const char* text, s32 base, s64* value )
{ {
const char* text_begin = text; const char* text_begin = text;
s64 result = 0; s64 result = 0;
@ -50,7 +56,7 @@ global const char _num_to_char_table[] =
s64 str_to_i64( const char* str, char** end_ptr, s32 base ) s64 str_to_i64( const char* str, char** end_ptr, s32 base )
{ {
sw len; ssize len;
s64 value; s64 value;
if ( ! base ) if ( ! base )
@ -79,7 +85,7 @@ void i64_to_str( s64 value, char* string, s32 base )
value = -value; value = -value;
} }
v = zpl_cast( u64 ) value; v = scast( u64, value);
if ( v != 0 ) if ( v != 0 )
{ {
while ( v > 0 ) while ( v > 0 )
@ -201,7 +207,7 @@ f64 str_to_f64( const char* str, char** end_ptr )
result = sign * ( frac ? ( value / scale ) : ( value * scale ) ); result = sign * ( frac ? ( value / scale ) : ( value * scale ) );
if ( end_ptr ) if ( end_ptr )
*end_ptr = zpl_cast( char* ) str; * end_ptr = rcast( char*, ccast(char*, str) );
return result; return result;
} }

View File

@ -1,40 +1,45 @@
#ifdef GEN_INTELLISENSE_DIRECTIVES
# pragma once
# include "memory.hpp"
#endif
#pragma region String Ops #pragma region String Ops
GEN_DEF_INLINE const char* char_first_occurence( const char* str, char c ); const char* char_first_occurence( const char* str, char c );
constexpr auto str_find = &char_first_occurence;
GEN_DEF_INLINE b32 char_is_alpha( char c ); b32 char_is_alpha( char c );
GEN_DEF_INLINE b32 char_is_alphanumeric( char c ); b32 char_is_alphanumeric( char c );
GEN_DEF_INLINE b32 char_is_digit( char c ); b32 char_is_digit( char c );
GEN_DEF_INLINE b32 char_is_hex_digit( char c ); b32 char_is_hex_digit( char c );
GEN_DEF_INLINE b32 char_is_space( char c ); b32 char_is_space( char c );
GEN_DEF_INLINE char char_to_lower( char c ); char char_to_lower( char c );
GEN_DEF_INLINE char char_to_upper( char c ); char char_to_upper( char c );
GEN_DEF_INLINE s32 digit_to_int( char c ); s32 digit_to_int( char c );
GEN_DEF_INLINE s32 hex_digit_to_int( char c ); s32 hex_digit_to_int( char c );
GEN_DEF_INLINE s32 str_compare( const char* s1, const char* s2 ); s32 str_compare( const char* s1, const char* s2 );
GEN_DEF_INLINE s32 str_compare( const char* s1, const char* s2, sw len ); s32 str_compare( const char* s1, const char* s2, ssize len );
GEN_DEF_INLINE char* str_copy( char* dest, const char* source, sw len ); char* str_copy( char* dest, const char* source, ssize len );
GEN_DEF_INLINE sw str_copy_nulpad( char* dest, const char* source, sw len ); ssize str_copy_nulpad( char* dest, const char* source, ssize len );
GEN_DEF_INLINE sw str_len( const char* str ); ssize str_len( const char* str );
GEN_DEF_INLINE sw str_len( const char* str, sw max_len ); ssize str_len( const char* str, ssize max_len );
GEN_DEF_INLINE char* str_reverse( char* str ); // NOTE: ASCII only char* str_reverse( char* str ); // NOTE: ASCII only
GEN_DEF_INLINE char const* str_skip( char const* str, char c ); char const* str_skip( char const* str, char c );
GEN_DEF_INLINE char const* str_skip_any( char const* str, char const* char_list ); char const* str_skip_any( char const* str, char const* char_list );
GEN_DEF_INLINE char const* str_trim( char const* str, b32 catch_newline ); char const* str_trim( char const* str, b32 catch_newline );
// NOTE: ASCII only // NOTE: ASCII only
GEN_DEF_INLINE void str_to_lower( char* str ); void str_to_lower( char* str );
GEN_DEF_INLINE void str_to_upper( char* str ); void str_to_upper( char* str );
s64 str_to_i64( const char* str, char** end_ptr, s32 base ); s64 str_to_i64( const char* str, char** end_ptr, s32 base );
void i64_to_str( s64 value, char* string, s32 base ); void i64_to_str( s64 value, char* string, s32 base );
void u64_to_str( u64 value, char* string, s32 base ); void u64_to_str( u64 value, char* string, s32 base );
f64 str_to_f64( const char* str, char** end_ptr ); f64 str_to_f64( const char* str, char** end_ptr );
GEN_IMPL_INLINE const char* char_first_occurence( const char* s, char c ) inline
const char* char_first_occurence( const char* s, char c )
{ {
char ch = c; char ch = c;
for ( ; *s != ch; s++ ) for ( ; *s != ch; s++ )
@ -45,59 +50,67 @@ GEN_IMPL_INLINE const char* char_first_occurence( const char* s, char c )
return s; return s;
} }
GEN_IMPL_INLINE b32 char_is_alpha( char c ) inline
b32 char_is_alpha( char c )
{ {
if ( ( c >= 'A' && c <= 'Z' ) || ( c >= 'a' && c <= 'z' ) ) if ( ( c >= 'A' && c <= 'Z' ) || ( c >= 'a' && c <= 'z' ) )
return true; return true;
return false; return false;
} }
GEN_IMPL_INLINE b32 char_is_alphanumeric( char c ) inline
b32 char_is_alphanumeric( char c )
{ {
return char_is_alpha( c ) || char_is_digit( c ); return char_is_alpha( c ) || char_is_digit( c );
} }
GEN_IMPL_INLINE b32 char_is_digit( char c ) inline
b32 char_is_digit( char c )
{ {
if ( c >= '0' && c <= '9' ) if ( c >= '0' && c <= '9' )
return true; return true;
return false; return false;
} }
GEN_IMPL_INLINE b32 char_is_hex_digit( char c ) inline
b32 char_is_hex_digit( char c )
{ {
if ( char_is_digit( c ) || ( c >= 'a' && c <= 'f' ) || ( c >= 'A' && c <= 'F' ) ) if ( char_is_digit( c ) || ( c >= 'a' && c <= 'f' ) || ( c >= 'A' && c <= 'F' ) )
return true; return true;
return false; return false;
} }
GEN_IMPL_INLINE b32 char_is_space( char c ) inline
b32 char_is_space( char c )
{ {
if ( c == ' ' || c == '\t' || c == '\n' || c == '\r' || c == '\f' || c == '\v' ) if ( c == ' ' || c == '\t' || c == '\n' || c == '\r' || c == '\f' || c == '\v' )
return true; return true;
return false; return false;
} }
GEN_IMPL_INLINE char char_to_lower( char c ) inline
char char_to_lower( char c )
{ {
if ( c >= 'A' && c <= 'Z' ) if ( c >= 'A' && c <= 'Z' )
return 'a' + ( c - 'A' ); return 'a' + ( c - 'A' );
return c; return c;
} }
GEN_IMPL_INLINE char char_to_upper( char c ) inline char char_to_upper( char c )
{ {
if ( c >= 'a' && c <= 'z' ) if ( c >= 'a' && c <= 'z' )
return 'A' + ( c - 'a' ); return 'A' + ( c - 'a' );
return c; return c;
} }
GEN_IMPL_INLINE s32 digit_to_int( char c ) inline
s32 digit_to_int( char c )
{ {
return char_is_digit( c ) ? c - '0' : c - 'W'; return char_is_digit( c ) ? c - '0' : c - 'W';
} }
GEN_IMPL_INLINE s32 hex_digit_to_int( char c ) inline
s32 hex_digit_to_int( char c )
{ {
if ( char_is_digit( c ) ) if ( char_is_digit( c ) )
return digit_to_int( c ); return digit_to_int( c );
@ -108,7 +121,8 @@ GEN_IMPL_INLINE s32 hex_digit_to_int( char c )
return -1; return -1;
} }
GEN_IMPL_INLINE s32 str_compare( const char* s1, const char* s2 ) inline
s32 str_compare( const char* s1, const char* s2 )
{ {
while ( *s1 && ( *s1 == *s2 ) ) while ( *s1 && ( *s1 == *s2 ) )
{ {
@ -117,7 +131,8 @@ GEN_IMPL_INLINE s32 str_compare( const char* s1, const char* s2 )
return *( u8* )s1 - *( u8* )s2; return *( u8* )s1 - *( u8* )s2;
} }
GEN_IMPL_INLINE s32 str_compare( const char* s1, const char* s2, sw len ) inline
s32 str_compare( const char* s1, const char* s2, ssize len )
{ {
for ( ; len > 0; s1++, s2++, len-- ) for ( ; len > 0; s1++, s2++, len-- )
{ {
@ -129,7 +144,8 @@ GEN_IMPL_INLINE s32 str_compare( const char* s1, const char* s2, sw len )
return 0; return 0;
} }
GEN_IMPL_INLINE char* str_copy( char* dest, const char* source, sw len ) inline
char* str_copy( char* dest, const char* source, ssize len )
{ {
GEN_ASSERT_NOT_NULL( dest ); GEN_ASSERT_NOT_NULL( dest );
if ( source ) if ( source )
@ -149,9 +165,10 @@ GEN_IMPL_INLINE char* str_copy( char* dest, const char* source, sw len )
return dest; return dest;
} }
GEN_IMPL_INLINE sw str_copy_nulpad( char* dest, const char* source, sw len ) inline
ssize str_copy_nulpad( char* dest, const char* source, ssize len )
{ {
sw result = 0; ssize result = 0;
GEN_ASSERT_NOT_NULL( dest ); GEN_ASSERT_NOT_NULL( dest );
if ( source ) if ( source )
{ {
@ -173,7 +190,8 @@ GEN_IMPL_INLINE sw str_copy_nulpad( char* dest, const char* source, sw len )
return result; return result;
} }
GEN_IMPL_INLINE sw str_len( const char* str ) inline
ssize str_len( const char* str )
{ {
if ( str == NULL ) if ( str == NULL )
{ {
@ -185,17 +203,19 @@ GEN_IMPL_INLINE sw str_len( const char* str )
return str - p; return str - p;
} }
GEN_IMPL_INLINE sw str_len( const char* str, sw max_len ) inline
ssize str_len( const char* str, ssize max_len )
{ {
const char* end = zpl_cast( const char* ) mem_find( str, 0, max_len ); const char* end = rcast(const char*, mem_find( str, 0, max_len ));
if ( end ) if ( end )
return end - str; return end - str;
return max_len; return max_len;
} }
GEN_IMPL_INLINE char* str_reverse( char* str ) inline
char* str_reverse( char* str )
{ {
sw len = str_len( str ); ssize len = str_len( str );
char* a = str + 0; char* a = str + 0;
char* b = str + len - 1; char* b = str + len - 1;
len /= 2; len /= 2;
@ -207,7 +227,8 @@ GEN_IMPL_INLINE char* str_reverse( char* str )
return str; return str;
} }
GEN_IMPL_INLINE char const* str_skip( char const* str, char c ) inline
char const* str_skip( char const* str, char c )
{ {
while ( *str && *str != c ) while ( *str && *str != c )
{ {
@ -216,11 +237,12 @@ GEN_IMPL_INLINE char const* str_skip( char const* str, char c )
return str; return str;
} }
GEN_IMPL_INLINE char const* str_skip_any( char const* str, char const* char_list ) inline
char const* str_skip_any( char const* str, char const* char_list )
{ {
char const* closest_ptr = zpl_cast( char const* ) pointer_add( ( void* )str, str_len( str ) ); char const* closest_ptr = rcast( char const*, pointer_add_const( rcast(void const*, str), str_len( str ) ));
sw char_list_count = str_len( char_list ); ssize char_list_count = str_len( char_list );
for ( sw i = 0; i < char_list_count; i++ ) for ( ssize i = 0; i < char_list_count; i++ )
{ {
char const* p = str_skip( str, char_list[ i ] ); char const* p = str_skip( str, char_list[ i ] );
closest_ptr = min( closest_ptr, p ); closest_ptr = min( closest_ptr, p );
@ -228,7 +250,8 @@ GEN_IMPL_INLINE char const* str_skip_any( char const* str, char const* char_list
return closest_ptr; return closest_ptr;
} }
GEN_IMPL_INLINE char const* str_trim( char const* str, b32 catch_newline ) inline
char const* str_trim( char const* str, b32 catch_newline )
{ {
while ( *str && char_is_space( *str ) && ( ! catch_newline || ( catch_newline && *str != '\n' ) ) ) while ( *str && char_is_space( *str ) && ( ! catch_newline || ( catch_newline && *str != '\n' ) ) )
{ {
@ -237,7 +260,8 @@ GEN_IMPL_INLINE char const* str_trim( char const* str, b32 catch_newline )
return str; return str;
} }
GEN_IMPL_INLINE void str_to_lower( char* str ) inline
void str_to_lower( char* str )
{ {
if ( ! str ) if ( ! str )
return; return;
@ -248,7 +272,8 @@ GEN_IMPL_INLINE void str_to_lower( char* str )
} }
} }
GEN_IMPL_INLINE void str_to_upper( char* str ) inline
void str_to_upper( char* str )
{ {
if ( ! str ) if ( ! str )
return; return;

View File

@ -0,0 +1,54 @@
#ifdef GEN_INTELLISENSE_DIRECTIVES
# pragma once
# include "hashing.cpp"
#endif
#pragma region String
String string_make_length( AllocatorInfo allocator, char const* str, ssize length )
{
constexpr ssize header_size = sizeof( StringHeader );
s32 alloc_size = header_size + length + 1;
void* allocation = alloc( allocator, alloc_size );
if ( allocation == nullptr )
return { nullptr };
StringHeader&
header = * rcast(StringHeader*, allocation);
header = { allocator, length, length };
String result = { rcast( char*, allocation) + header_size };
if ( length && str )
mem_copy( result, str, length );
else
mem_set( result, 0, alloc_size - header_size );
result[ length ] = '\0';
return result;
}
String string_make_reserve( AllocatorInfo allocator, ssize capacity )
{
constexpr ssize header_size = sizeof( StringHeader );
s32 alloc_size = header_size + capacity + 1;
void* allocation = alloc( allocator, alloc_size );
if ( allocation == nullptr )
return { nullptr };
mem_set( allocation, 0, alloc_size );
StringHeader*
header = rcast(StringHeader*, allocation);
header->Allocator = allocator;
header->Capacity = capacity;
header->Length = 0;
String result = { rcast(char*, allocation) + header_size };
return result;
}
#pragma endregion String

View File

@ -0,0 +1,600 @@
#ifdef GEN_INTELLISENSE_DIRECTIVES
# pragma once
# include "hashing.hpp"
#endif
#pragma region Strings
// Constant string with length.
struct StrC
{
ssize Len;
char const* Ptr;
#if ! GEN_COMPILER_C
operator char const* () const { return Ptr; }
char const& operator[]( ssize index ) const { return Ptr[index]; }
#endif
};
#define cast_to_strc( str ) * rcast( StrC*, (str) - sizeof(ssize) )
#define txt( text ) StrC { sizeof( text ) - 1, ( text ) }
inline
StrC to_str( char const* str ) {
return { str_len( str ), str };
}
// Dynamic String
// This is directly based off the ZPL string api.
// They used a header pattern
// I kept it for simplicty of porting but its not necessary to keep it that way.
#pragma region String
struct StringHeader;
struct String;
usize string_grow_formula(usize value);
String string_make (AllocatorInfo allocator, char const* str);
String string_make (AllocatorInfo allocator, StrC str);
String string_make_reserve (AllocatorInfo allocator, ssize capacity);
String string_make_length (AllocatorInfo allocator, char const* str, ssize length);
String string_fmt (AllocatorInfo allocator, char* buf, ssize buf_size, char const* fmt, ...);
String string_fmt_buf (AllocatorInfo allocator, char const* fmt, ...);
String string_join (AllocatorInfo allocator, char const** parts, ssize num_parts, char const* glue);
bool are_equal (String const lhs, String const rhs);
bool are_equal (String const lhs, StrC rhs);
bool make_space_for (String* str, char const* to_append, ssize add_len);
bool append (String* str, char c);
bool append (String* str, char const* str_to_append);
bool append (String* str, char const* str_to_append, ssize length);
bool append (String* str, StrC str_to_append);
bool append (String* str, String const other);
bool append_fmt (String* str, char const* fmt, ...);
ssize avail_space (String const str);
char* back (String str);
bool contains (String const str, StrC substring);
bool contains (String const str, String const substring);
ssize capacity (String const str);
void clear (String str);
String duplicate (String const str, AllocatorInfo allocator);
void free (String* str);
StringHeader* get_header (String str);
ssize length (String const str);
b32 starts_with (String const str, StrC substring);
b32 starts_with (String const str, String substring);
void skip_line (String str);
void strip_space (String str);
void trim (String str, char const* cut_set);
void trim_space (String str);
String visualize_whitespace(String const str);
struct StringHeader {
AllocatorInfo Allocator;
ssize Capacity;
ssize Length;
};
#if ! GEN_COMPILER_C
struct String
{
char* Data;
forceinline operator bool() { return Data != nullptr; }
forceinline operator char*() { return Data; }
forceinline operator char const*() const { return Data; }
forceinline operator StrC() const { return { GEN_NS length(* this), Data }; }
String const& operator=(String const& other) const {
if (this == &other)
return *this;
String* this_ = ccast(String*, this);
this_->Data = other.Data;
return *this;
}
forceinline char& operator[](ssize index) { return Data[index]; }
forceinline char const& operator[](ssize index) const { return Data[index]; }
forceinline char* begin() const { return Data; }
forceinline char* end() const { return Data + GEN_NS length(* this); }
#if GEN_SUPPORT_CPP_MEMBER_FEATURES
#pragma region Member Mapping
forceinline static String make(AllocatorInfo allocator, char const* str) { return GEN_NS string_make(allocator, str); }
forceinline static String make(AllocatorInfo allocator, StrC str) { return GEN_NS string_make(allocator, str); }
forceinline static String make_reserve(AllocatorInfo allocator, ssize cap) { return GEN_NS string_make_reserve(allocator, cap); }
forceinline static String make_length(AllocatorInfo a, char const* s, ssize l) { return GEN_NS string_make_length(a, s, l); }
forceinline static String join(AllocatorInfo a, char const** p, ssize n, char const* g) { return GEN_NS string_join(a, p, n, g); }
forceinline static usize grow_formula(usize value) { return GEN_NS string_grow_formula(value); }
static
String fmt(AllocatorInfo allocator, char* buf, ssize buf_size, char const* fmt, ...) {
va_list va;
va_start(va, fmt);
str_fmt_va(buf, buf_size, fmt, va);
va_end(va);
return GEN_NS string_make(allocator, buf);
}
static
String fmt_buf(AllocatorInfo allocator, char const* fmt, ...) {
local_persist thread_local
char buf[GEN_PRINTF_MAXLEN] = { 0 };
va_list va;
va_start(va, fmt);
str_fmt_va(buf, GEN_PRINTF_MAXLEN, fmt, va);
va_end(va);
return GEN_NS string_make(allocator, buf);
}
forceinline bool make_space_for(char const* str, ssize add_len) { return GEN_NS make_space_for(this, str, add_len); }
forceinline bool append(char c) { return GEN_NS append(this, c); }
forceinline bool append(char const* str) { return GEN_NS append(this, str); }
forceinline bool append(char const* str, ssize length) { return GEN_NS append(this, str, length); }
forceinline bool append(StrC str) { return GEN_NS append(this, str); }
forceinline bool append(const String other) { return GEN_NS append(this, other); }
forceinline ssize avail_space() const { return GEN_NS avail_space(* this); }
forceinline char* back() { return GEN_NS back(* this); }
forceinline bool contains(StrC substring) const { return GEN_NS contains(* this, substring); }
forceinline bool contains(String const& substring) const { return GEN_NS contains(* this, substring); }
forceinline ssize capacity() const { return GEN_NS capacity(* this); }
forceinline void clear() { GEN_NS clear(* this); }
forceinline String duplicate(AllocatorInfo allocator) const { return GEN_NS duplicate(* this, allocator); }
forceinline void free() { GEN_NS free(this); }
forceinline bool is_equal(String const& other) const { return GEN_NS are_equal(* this, other); }
forceinline bool is_equal(StrC other) const { return GEN_NS are_equal(* this, other); }
forceinline ssize length() const { return GEN_NS length(* this); }
forceinline b32 starts_with(StrC substring) const { return GEN_NS starts_with(* this, substring); }
forceinline b32 starts_with(String substring) const { return GEN_NS starts_with(* this, substring); }
forceinline void skip_line() { GEN_NS skip_line(* this); }
forceinline void strip_space() { GEN_NS strip_space(* this); }
forceinline void trim(char const* cut_set) { GEN_NS trim(* this, cut_set); }
forceinline void trim_space() { GEN_NS trim_space(* this); }
forceinline String visualize_whitespace() const { return GEN_NS visualize_whitespace(* this); }
forceinline StringHeader& get_header() { return * GEN_NS get_header(* this); }
bool append_fmt(char const* fmt, ...) {
ssize res;
char buf[GEN_PRINTF_MAXLEN] = { 0 };
va_list va;
va_start(va, fmt);
res = str_fmt_va(buf, count_of(buf) - 1, fmt, va) - 1;
va_end(va);
return GEN_NS append(this, buf, res);
}
#pragma endregion Member Mapping
#endif
};
#endif
#if GEN_SUPPORT_CPP_REFERENCES
bool make_space_for(String& str, char const* to_append, ssize add_len);
bool append(String& str, char c);
bool append(String& str, char const* str_to_append);
bool append(String& str, char const* str_to_append, ssize length);
bool append(String& str, StrC str_to_append);
bool append(String& str, const String other);
bool append_fmt(String& str, char const* fmt, ...);
char& back(String& str);
void clear(String& str);
void free(String& str);
#endif
inline char* begin(String str) { return str; }
inline char* end(String str) { return scast(char*, str) + length(str); }
inline char* next(String str) { return scast(char*, str) + 1; }
inline
usize string_grow_formula(usize value) {
// Using a very aggressive growth formula to reduce time mem_copying with recursive calls to append in this library.
return 4 * value + 8;
}
inline
String string_make(AllocatorInfo allocator, char const* str) {
ssize length = str ? str_len(str) : 0;
return string_make_length(allocator, str, length);
}
inline
String string_make(AllocatorInfo allocator, StrC str) {
return string_make_length(allocator, str.Ptr, str.Len);
}
inline
String string_fmt(AllocatorInfo allocator, char* buf, ssize buf_size, char const* fmt, ...) {
va_list va;
va_start(va, fmt);
str_fmt_va(buf, buf_size, fmt, va);
va_end(va);
return string_make(allocator, buf);
}
inline
String string_fmt_buf(AllocatorInfo allocator, char const* fmt, ...)
{
local_persist thread_local
char buf[GEN_PRINTF_MAXLEN] = { 0 };
va_list va;
va_start(va, fmt);
str_fmt_va(buf, GEN_PRINTF_MAXLEN, fmt, va);
va_end(va);
return string_make(allocator, buf);
}
inline
String string_join(AllocatorInfo allocator, char const** parts, ssize num_parts, char const* glue)
{
String result = string_make(allocator, "");
for (ssize idx = 0; idx < num_parts; ++idx)
{
append(& result, parts[idx]);
if (idx < num_parts - 1)
append(& result, glue);
}
return result;
}
inline
bool append(String* str, char c) {
GEN_ASSERT(str != nullptr);
return append(str, &c, 1);
}
inline
bool append(String* str, char const* str_to_append) {
GEN_ASSERT(str != nullptr);
return append(str, str_to_append, str_len(str_to_append));
}
inline
bool append(String* str, char const* str_to_append, ssize append_length)
{
GEN_ASSERT(str != nullptr);
if (sptr(str_to_append) > 0)
{
ssize curr_len = length(* str);
if ( ! make_space_for(str, str_to_append, append_length))
return false;
StringHeader* header = get_header(* str);
char* Data = * str;
mem_copy( Data + curr_len, str_to_append, append_length);
Data[curr_len + append_length] = '\0';
header->Length = curr_len + append_length;
}
return str_to_append != nullptr;
}
inline
bool append(String* str, StrC str_to_append) {
GEN_ASSERT(str != nullptr);
return append(str, str_to_append.Ptr, str_to_append.Len);
}
inline
bool append(String* str, const String other) {
GEN_ASSERT(str != nullptr);
return append(str, other, length(other));
}
bool append_fmt(String* str, char const* fmt, ...) {
GEN_ASSERT(str != nullptr);
ssize res;
char buf[GEN_PRINTF_MAXLEN] = { 0 };
va_list va;
va_start(va, fmt);
res = str_fmt_va(buf, count_of(buf) - 1, fmt, va) - 1;
va_end(va);
return append(str, buf, res);
}
inline
bool are_equal(String const lhs, String const rhs)
{
if (length(lhs) != length(rhs))
return false;
for (ssize idx = 0; idx < length(lhs); ++idx)
if (lhs[idx] != rhs[idx])
return false;
return true;
}
inline
bool are_equal(String const lhs, StrC rhs)
{
if (length(lhs) != (rhs.Len))
return false;
for (ssize idx = 0; idx < length(lhs); ++idx)
if (lhs[idx] != rhs.Ptr[idx])
return false;
return true;
}
inline
ssize avail_space(String const str) {
StringHeader const* header = rcast(StringHeader const*, scast(char const*, str) - sizeof(StringHeader));
return header->Capacity - header->Length;
}
inline
char* back(String* str) {
return & (*str)[length(* str) - 1];
}
inline
bool contains(String const str, StrC substring)
{
StringHeader const* header = rcast(StringHeader const*, scast(char const*, str) - sizeof(StringHeader));
if (substring.Len > header->Length)
return false;
ssize main_len = header->Length;
ssize sub_len = substring.Len;
for (ssize idx = 0; idx <= main_len - sub_len; ++idx)
{
if (str_compare(str + idx, substring.Ptr, sub_len) == 0)
return true;
}
return false;
}
inline
bool contains(String const str, String const substring)
{
StringHeader const* header = rcast(StringHeader const*, scast(char const*, str) - sizeof(StringHeader));
if (length(substring) > header->Length)
return false;
ssize main_len = header->Length;
ssize sub_len = length(substring);
for (ssize idx = 0; idx <= main_len - sub_len; ++idx)
{
if (str_compare(str + idx, substring, sub_len) == 0)
return true;
}
return false;
}
inline
ssize capacity(String const str) {
StringHeader const* header = rcast(StringHeader const*, scast(char const*, str) - sizeof(StringHeader));
return header->Capacity;
}
inline
void clear(String str) {
get_header(str)->Length = 0;
}
inline
String duplicate(String const str, AllocatorInfo allocator) {
return string_make_length(allocator, str, length(str));
}
inline
void free(String* str) {
GEN_ASSERT(str != nullptr);
if (! (* str))
return;
StringHeader* header = get_header(* str);
GEN_NS free(header->Allocator, header);
}
inline
StringHeader* get_header(String str) {
return (StringHeader*)(scast(char*, str) - sizeof(StringHeader));
}
inline
ssize length(String const str)
{
StringHeader const& header = *rcast(StringHeader const*, scast(char const*, str) - sizeof(StringHeader));
return header.Length;
}
inline
bool make_space_for(String* str, char const* to_append, ssize add_len)
{
ssize available = avail_space(* str);
if (available >= add_len) {
return true;
}
else
{
ssize new_len, old_size, new_size;
void* ptr;
void* new_ptr;
AllocatorInfo allocator = get_header(* str)->Allocator;
StringHeader* header = nullptr;
new_len = string_grow_formula(length(* str) + add_len);
ptr = get_header(* str);
old_size = size_of(StringHeader) + length(* str) + 1;
new_size = size_of(StringHeader) + new_len + 1;
new_ptr = resize(allocator, ptr, old_size, new_size);
if (new_ptr == nullptr)
return false;
header = rcast(StringHeader*, new_ptr);
header->Allocator = allocator;
header->Capacity = new_len;
char** Data = rcast(char**, str);
* Data = rcast(char*, header + 1);
return true;
}
}
inline
b32 starts_with(String const str, StrC substring) {
if (substring.Len > length(str))
return false;
b32 result = str_compare(str.Data, substring.Ptr, substring.Len) == 0;
return result;
}
inline
b32 starts_with(String const str, String substring) {
if (length(substring) > length(str))
return false;
b32 result = str_compare(str.Data, substring.Data, length(substring) - 1) == 0;
return result;
}
inline
void skip_line(String str)
{
#define current (*scanner)
char* scanner = str.Data;
while (current != '\r' && current != '\n') {
++scanner;
}
s32 new_length = scanner - str.Data;
if (current == '\r') {
new_length += 1;
}
mem_move(str.Data, scanner, new_length);
StringHeader* header = get_header(str);
header->Length = new_length;
#undef current
}
inline
void strip_space(String str)
{
char* write_pos = str;
char* read_pos = str;
while (* read_pos)
{
if (! char_is_space(* read_pos))
{
* write_pos = * read_pos;
write_pos++;
}
read_pos++;
}
write_pos[0] = '\0'; // Null-terminate the modified string
// Update the length if needed
get_header(str)->Length = write_pos - str.Data;
}
inline
void trim(String str, char const* cut_set)
{
ssize len = 0;
char* start_pos = str.Data;
char* end_pos = str.Data + length(str) - 1;
while (start_pos <= end_pos && char_first_occurence(cut_set, *start_pos))
start_pos++;
while (end_pos > start_pos && char_first_occurence(cut_set, *end_pos))
end_pos--;
len = scast(ssize, (start_pos > end_pos) ? 0 : ((end_pos - start_pos) + 1));
if (str.Data != start_pos)
mem_move(str.Data, start_pos, len);
str.Data[len] = '\0';
get_header(str)->Length = len;
}
inline
void trim_space(String str) {
trim(str, " \t\r\n\v\f");
}
inline
String visualize_whitespace(String const str)
{
StringHeader* header = (StringHeader*)(scast(char const*, str) - sizeof(StringHeader));
String result = string_make_reserve(header->Allocator, length(str) * 2); // Assume worst case for space requirements.
for (auto c : str) switch (c)
{
case ' ':
append(& result, txt("·"));
break;
case '\t':
append(& result, txt(""));
break;
case '\n':
append(& result, txt(""));
break;
case '\r':
append(& result, txt(""));
break;
case '\v':
append(& result, txt(""));
break;
case '\f':
append(& result, txt(""));
break;
default:
append(& result, c);
break;
}
return result;
}
#pragma endregion String
struct String_POD {
char* Data;
};
static_assert( sizeof( String_POD ) == sizeof( String ), "String is not a POD" );
// Implements basic string interning. Data structure is based off the ZPL Hashtable.
typedef HashTable<String const> StringTable;
// Represents strings cached with the string table.
// Should never be modified, if changed string is desired, cache_string( str ) another.
typedef String const StringCached;
#pragma endregion Strings

View File

@ -1,3 +1,8 @@
#ifdef GEN_INTELLISENSE_DIRECTIVES
# pragma once
# include "filesystem.cpp"
#endif
#pragma region Timing #pragma region Timing
#ifdef GEN_BENCHMARK #ifdef GEN_BENCHMARK
@ -18,7 +23,7 @@
{ {
u32 hi, lo; u32 hi, lo;
__asm__ __volatile__( "rdtsc" : "=a"( lo ), "=d"( hi ) ); __asm__ __volatile__( "rdtsc" : "=a"( lo ), "=d"( hi ) );
return ( zpl_cast( u64 ) lo ) | ( ( zpl_cast( u64 ) hi ) << 32 ); return scast( u64, lo ) | ( scast( u64, hi ) << 32 );
} }
#elif defined( __powerpc__ ) #elif defined( __powerpc__ )
u64 read_cpu_time_stamp_counter( void ) u64 read_cpu_time_stamp_counter( void )

View File

@ -1,3 +1,8 @@
#ifdef GEN_INTELLISENSE_DIRECTIVES
# pragma once
# include "filesystem.hpp"
#endif
#pragma region Timing #pragma region Timing
#ifdef GEN_BENCHMARK #ifdef GEN_BENCHMARK

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
@ -32,7 +37,15 @@ Operator_Member_Fwd
Operator_Cast Operator_Cast
Operator_Cast_Fwd Operator_Cast_Fwd
Parameters Parameters
Preprocessor_Include Preprocess_Define
Preprocess_Include
Preprocess_If
Preprocess_IfDef
Preprocess_IfNotDef
Preprocess_ElIf
Preprocess_Else
Preprocess_EndIf
Preprocess_Pragma
Specifiers Specifiers
Struct Struct
Struct_Fwd Struct_Fwd

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
37 Operator_Cast
38 Operator_Cast_Fwd
39 Parameters
40 Preprocessor_Include Preprocess_Define
41 Preprocess_Include
42 Preprocess_If
43 Preprocess_IfDef
44 Preprocess_IfNotDef
45 Preprocess_ElIf
46 Preprocess_Else
47 Preprocess_EndIf
48 Preprocess_Pragma
49 Specifiers
50 Struct
51 Struct_Fwd

View File

@ -0,0 +1,144 @@
Invalid
Untyped
NewLine
Comment
Access_Private
Access_Protected
Access_Public
PlatformAttributes
Class
Class_Fwd
Class_Body
Constructor
Constructor_Fwd
Destructor
Destructor_Fwd
Enum
Enum_Fwd
Enum_Body
Enum_Class
Enum_Class_Fwd
Execution
Expression
Expr_Identifier
Expr_NumericLiteral
Expr_StringLiteral
Expr_Alignof
Expr_ProcCall
Expr_Assign_Add
Expr_Assign_Subtract
Expr_Assign_Multiply
Expr_Assign_Divide
Expr_Assign_Modulo
Expr_Assign_Bitwise_And
Expr_Assign_Bitwise_Or
Expr_Assign_Bitwise_XOr
Expr_Assign_LeftShift
Expr_Assign_RightShift
Expr_CStyleCast
Expr_FunctionalCast
Expr_ConstCast
Expr_DynamicCast
Expr_ReinterpretCast
Expr_StaticCast
Expr_Unary_Add
Expr_Unary_Minus
Expr_Unary_Not
Expr_Unary_Increment
Expr_Unary_Decrement
Expr_Indirection
Expr_AddressOf,
Expr_UnaryPost_Increment
Expr_UnaryPost_Decrement
Expr_Subscript
Expr_Binary_Add
Expr_Binary_Subtract
Expr_Binary_Multiply
Expr_Binary_Divide
Expr_Binary_Modulo
Expr_Binary_Bitwise_And
Expr_Binary_Bitwise_Or
Expr_Binary_Bitwise_XOr
Expr_Binary_Bitwise_LeftShift
Expr_Binary_Bitwise_RightShift
Expr_Binary_Logical_Not
Expr_Binary_Logical_And
Expr_Binary_Logical_Or
Expr_Binary_Equal
Expr_Binary_NotEqual
Expr_Binary_Lesser
Expr_Binary_Greater
Expr_Binary_LesserEqual
Expr_Binary_GreaterEqual
Expr_MemberOfObject,
Expr_MemberOfPointer,
Expr_PointerToMemberOfObject,
Expr_PointerToMemberOfPointer,
Expr_Comma,
Expr_Tenary,
Export_Body
Extern_Linkage
Extern_Linkage_Body
Friend
Function
Function_Fwd
Function_Body
Global_Body
Module
Namespace
Namespace_Body
Operator
Operator_Fwd
Operator_Member
Operator_Member_Fwd
Operator_Cast
Operator_Cast_Fwd
Parameters
Preprocess_Define
Preprocess_Include
Preprocess_If
Preprocess_IfDef
Preprocess_IfNotDef
Preprocess_ElIf
Preprocess_Else
Preprocess_EndIf
Preprocess_Pragma
Specifiers
Statement
Stmt_Break
Stmt_Case
Stmt_Continue
Stmt_Declaration
Stmt_Do
Stmt_Expr
Stmt_Else
Stmt_If
Stmt_For
Stmt_Goto
Stmt_Label
Stmt_Switch
Stmt_Switch
Stmt_While
Struct
Struct_Fwd
Struct_Body
Template
Typedef
Typename
Union
Union_Body
Using
Using_Namespace
Variable
Can't render this file because it has a wrong number of fields in line 56.

View File

@ -41,3 +41,7 @@ MemberOfPointer, "->"
PtrToMemOfPtr, "->*" PtrToMemOfPtr, "->*"
FunctionCall, "()" FunctionCall, "()"
Comma, "," Comma, ","
New, "new"
NewArray, "new[]"
Delete, "delete"
DeleteArray, "delete[]"

1 Invalid INVALID
41 PtrToMemOfPtr ->*
42 FunctionCall ()
43 Comma ,
44 New new
45 NewArray new[]
46 Delete delete
47 DeleteArray delete[]

View File

@ -4,19 +4,23 @@ Constexpr, constexpr
Constinit, constinit Constinit, constinit
Explicit, explicit Explicit, explicit
External_Linkage, extern External_Linkage, extern
ForceInline, forceinline
Global, global Global, global
Inline, inline Inline, inline
Internal_Linkage, internal Internal_Linkage, internal
Local_Persist, local_persist Local_Persist, local_persist
Mutable, mutable Mutable, mutable
NeverInline, neverinline
Ptr, * Ptr, *
Ref, & Ref, &
Register, register Register, register
RValue, && RValue, &&
Static, static Static, static
Thread_Local, thread_local Thread_Local, thread_local
Volatile, volatile
Virtual, virtual Virtual, virtual
Const, const Const, const
Final, final Final, final
NoExceptions, noexcept
Override, override Override, override
Pure, = 0
Volatile, volatile

1 Invalid INVALID
4 Constinit constinit
5 Explicit explicit
6 External_Linkage extern
7 ForceInline forceinline
8 Global global
9 Inline inline
10 Internal_Linkage internal
11 Local_Persist local_persist
12 Mutable mutable
13 NeverInline neverinline
14 Ptr *
15 Ref &
16 Register register
17 RValue &&
18 Static static
19 Thread_Local thread_local
Volatile volatile
20 Virtual virtual
21 Const const
22 Final final
23 NoExceptions noexcept
24 Override override
25 Pure = 0
26 Volatile volatile

View File

@ -7,16 +7,18 @@ Access_StaticSymbol, "::"
Ampersand, "&" Ampersand, "&"
Ampersand_DBL, "&&" Ampersand_DBL, "&&"
Assign_Classifer, ":" Assign_Classifer, ":"
Attribute_Open, "[[" Attribute_Open, "[["
Attribute_Close, "]]" Attribute_Close, "]]"
BraceCurly_Open, "{" BraceCurly_Open, "{"
BraceCurly_Close, "}" BraceCurly_Close, "}"
BraceSquare_Open, "[" BraceSquare_Open, "["
BraceSquare_Close, "]" BraceSquare_Close, "]"
Capture_Start, "(" Capture_Start, "("
Capture_End, ")" Capture_End, ")"
Comment, "comemnt" Comment, "__comment__"
Char, "character" Comment_End, "__comment_end__"
Comment_Start, "__comment_start__"
Char, "__character__"
Comma, "," Comma, ","
Decl_Class, "class" Decl_Class, "class"
Decl_GNU_Attribute, "__attribute__" Decl_GNU_Attribute, "__attribute__"
@ -32,43 +34,62 @@ Decl_Template, "template"
Decl_Typedef, "typedef" Decl_Typedef, "typedef"
Decl_Using, "using" Decl_Using, "using"
Decl_Union, "union" Decl_Union, "union"
Identifier, "identifier" Identifier, "__identifier__"
Module_Import, "import" Module_Import, "import"
Module_Export, "export" Module_Export, "export"
Number, "number" NewLine, "__new_line__"
Operator, "operator" Number, "__number__"
Preprocess_Define, "#define" Operator, "__operator__"
Preprocess_Include, "#include" Preprocess_Hash, "#"
Preprocess_If, "#if" Preprocess_Define, "define"
Preprocess_ElIF, "#elif" Preprocess_If, "if"
Preprocess_Else, "#else" Preprocess_IfDef, "ifdef"
Preprocess_EndIf, "#endif" Preprocess_IfNotDef, "ifndef"
Preprocess_ElIf, "elif"
Preprocess_Else, "else"
Preprocess_EndIf, "endif"
Preprocess_Include, "include"
Preprocess_Pragma, "pragma"
Preprocess_Content, "__macro_content__"
Preprocess_Macro, "__macro__"
Preprocess_Unsupported, "__unsupported__"
Spec_Alignas, "alignas" Spec_Alignas, "alignas"
Spec_Const, "const" Spec_Const, "const"
Spec_Consteval, "consteval" Spec_Consteval, "consteval"
Spec_Constexpr, "constexpr" Spec_Constexpr, "constexpr"
Spec_Constinit, "constinit" Spec_Constinit, "constinit"
Spec_Explicit, "explicit" Spec_Explicit, "explicit"
Spec_Extern, "extern" Spec_Extern, "extern"
Spec_Final, "final" Spec_Final, "final"
Spec_Global, "global" Spec_ForceInline, "forceinline"
Spec_Global, "global"
Spec_Inline, "inline" Spec_Inline, "inline"
Spec_Internal_Linkage, "internal" Spec_Internal_Linkage, "internal"
Spec_LocalPersist, "local_persist" Spec_LocalPersist, "local_persist"
Spec_Mutable, "mutable" Spec_Mutable, "mutable"
Spec_NeverInline, "neverinline"
Spec_Override, "override" 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, ";"
String, "string" StaticAssert, "static_assert"
Type_Unsigned, "unsigned" String, "__string__"
Type_Typename, "typename"
Type_Unsigned, "unsigned"
Type_Signed, "signed" Type_Signed, "signed"
Type_Short, "short" Type_Short, "short"
Type_Long, "long" Type_Long, "long"
Type_char, "char" Type_bool, "bool"
Type_int, "int" Type_char, "char"
Type_double, "double" Type_int, "int"
Type_double, "double"
Type_MS_int8, "__int8"
Type_MS_int16, "__int16"
Type_MS_int32, "__int32"
Type_MS_int64, "__int64"
Type_MS_W64, "_W64"
Varadic_Argument, "..." Varadic_Argument, "..."
Attributes_Start, "__attrib_start__" __Attributes_Start, "__attrib_start__"

1 Invalid __invalid__
7 Ampersand &
8 Ampersand_DBL &&
9 Assign_Classifer :
10 Attribute_Open [[
11 Attribute_Close ]]
12 BraceCurly_Open {
13 BraceCurly_Close }
14 BraceSquare_Open [
15 BraceSquare_Close ]
16 Capture_Start (
17 Capture_End )
18 Comment comemnt __comment__
19 Char Comment_End character __comment_end__
20 Comment_Start __comment_start__
21 Char __character__
22 Comma ,
23 Decl_Class class
24 Decl_GNU_Attribute __attribute__
34 Decl_Typedef typedef
35 Decl_Using using
36 Decl_Union union
37 Identifier identifier __identifier__
38 Module_Import import
39 Module_Export export
40 Number NewLine number __new_line__
41 Operator Number operator __number__
42 Preprocess_Define Operator #define __operator__
43 Preprocess_Include Preprocess_Hash #include #
44 Preprocess_If Preprocess_Define #if define
45 Preprocess_ElIF Preprocess_If #elif if
46 Preprocess_Else Preprocess_IfDef #else ifdef
47 Preprocess_EndIf Preprocess_IfNotDef #endif 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
57 Spec_Const const
58 Spec_Consteval consteval
59 Spec_Constexpr constexpr
60 Spec_Constinit constinit
61 Spec_Explicit explicit
62 Spec_Extern extern
63 Spec_Final final
64 Spec_Global Spec_ForceInline global forceinline
65 Spec_Global global
66 Spec_Inline inline
67 Spec_Internal_Linkage internal
68 Spec_LocalPersist local_persist
69 Spec_Mutable mutable
70 Spec_NeverInline neverinline
71 Spec_Override override
72 Spec_Static static
73 Spec_ThreadLocal thread_local
74 Spec_Volatile volatile
75 Spec_Virtual virtual
76 Star *
77 Statement_End ;
78 String StaticAssert string static_assert
79 Type_Unsigned String unsigned __string__
80 Type_Typename typename
81 Type_Unsigned unsigned
82 Type_Signed signed
83 Type_Short short
84 Type_Long long
85 Type_char Type_bool char bool
86 Type_int Type_char int char
87 Type_double Type_int double int
88 Type_double double
89 Type_MS_int8 __int8
90 Type_MS_int16 __int16
91 Type_MS_int32 __int32
92 Type_MS_int64 __int64
93 Type_MS_W64 _W64
94 Varadic_Argument ...
95 Attributes_Start __Attributes_Start __attrib_start__

View File

@ -0,0 +1,183 @@
Invalid, "__invalid__"
Access_Private, "private"
Access_Protected, "protected"
Access_Public, "public"
Access_MemberSymbol, "."
Access_StaticSymbol, "::"
Ampersand, "&"
Ampersand_DBL, "&&"
Assign_Classifer, ":"
Attribute_Open, "[["
Attribute_Close, "]]"
BraceCurly_Open, "{"
BraceCurly_Close, "}"
BraceSquare_Open, "["
BraceSquare_Close, "]"
Capture_Start, "("
Capture_End, ")"
Comment, "__comment__"
Comment_End, "__comment_end__"
Comment_Start, "__comment_start__"
Char, "__character__"
Comma, ","
Decl_Class, "class"
Decl_Default "default"
Decl_GNU_Attribute, "__attribute__"
Decl_MSVC_Attribute, "__declspec"
Decl_Enum, "enum"
Decl_Extern_Linkage, "extern"
Decl_Friend, "friend"
Decl_Module, "module"
Decl_Namespace, "namespace"
Decl_Operator, "operator"
Decl_Struct, "struct"
Decl_Template, "template"
Decl_Type, "decltype"
Decl_Typedef, "typedef"
Decl_Using, "using"
Decl_Union, "union"
Expr_AlignOf, "alignof"
Identifier, "__identifier__"
Module_Import, "import"
Module_Export, "export"
NewLine, "__new_line__"
Number, "__number__"
Operator, "__operator__"
Op_Assign, "="
Op_Assign_Add, "+="
Op_Assign_Subtract, "-="
Op_Assign_Multiply, "*="
Op_Assign_Divide, "/="
Op_Assign_Modulo, "%="
Op_Assign_Bitwise_And, "&="
Op_Assign_Bitwise_Or, "|="
Op_Assign_Bitwise_XOr, "^="
Op_Assign_Bitwise_LeftShift, "<<="
Op_Assign_Bitwise_RightShift, ">>="
Op_Increment, "++"
Op_Decrement, "--"
Op_Add, "+"
Op_Subtract, "-"
Op_Multiply, "*"
Op_Divide, "/"
Op_Modulo, "%"
Op_Bitwise_And, "&"
Op_Bitwise_Or, "|"
Op_Bitwise_XOr, "^"
Op_Bitwise_LeftShitf, "<<"
Op_Bitwise_RightShift, ">>"
Op_UnaryAdd, "+"
Op_UnaryMinus, "-"
Op_UnaryNot, "~"
Op_Logical_Not, "!"
Op_Logical_And, "&&"
Op_Logical_Or, "||"
Op_Equal, "=="
Op_NotEqual, "!="
Op_Lesser, "<"
Op_Greater, ">"
Op_LesserEqual, "<="
Op_GreaterEqual", ">=
Op_Subscript, "[]"
Op_Indirection, "*"
Op_AddressOf, "&"
Op_MemberOfObject, "."
Op_MemberOfPointer", "->"
Op_PointerToMemberOfObject, ".*"
Op_PointerToMemberOfPointer, "->*"
Op_Comma, ","
Op_Ternary, "?"
Preprocess_Hash, "#"
Preprocess_Define, "define"
Preprocess_If, "if"
Preprocess_IfDef, "ifdef"
Preprocess_IfNotDef, "ifndef"
Preprocess_ElIf, "elif"
Preprocess_Else, "else"
Preprocess_EndIf, "endif"
Preprocess_Include, "include"
Preprocess_Pragma, "pragma"
Preprocess_Content, "__macro_content__"
Preprocess_Macro, "__macro__"
Preprocess_Generic, "_Generic"
Preprocess_Unsupported, "__unsupported__"
Spec_Alignof, "alignof"
Spec_Const, "const"
Spec_Consteval, "consteval"
Spec_Constexpr, "constexpr"
Spec_Constinit, "constinit"
Spec_Explicit, "explicit"
Spec_Extern, "extern"
Spec_Final, "final"
Spec_ForceInline, "forceinline"
Spec_Global, "global"
Spec_Inline, "inline"
Spec_Internal_Linkage, "internal"
Spec_LocalPersist, "local_persist"
Spec_Mutable, "mutable"
Spec_NeverInline, "neverinline"
Spec_Override, "override"
Spec_Static, "static"
Spec_ThreadLocal, "thread_local"
Spec_Volatile, "volatile"
Spec_Virtual, "virtual"
Star, "*"
Stmt_Break, "break"
Stmt_Case, "case"
Stmt_Continue, "continue"
Stmt_Default, "default"
Stmt_Do, "do"
Stmt_Else, "else"
Stmt_End, ";"
Stmt_If, "if"
Stmt_For, "for"
Stmt_Goto, "goto"
Stmt_Return, "return"
Stmt_Switch, "switch"
Stmt_While, "while"
StaticAssert, "static_assert"
String, "__string__"
Type_Auto, "auto"
Type_Unsigned, "unsigned"
Type_Signed, "signed"
Type_Short, "short"
Type_Long, "long"
Type_bool, "bool"
Type_char, "char"
Type_int, "int"
Type_float, "float"
Type_double, "double"
Type_MS_int8, "__int8"
Type_MS_int16, "__int16"
Type_MS_int32, "__int32"
Type_MS_int64, "__int64"
Type_MS_W64, "_W64"
Varadic_Argument, "..."
__Attributes_Start, "__attrib_start__"
Can't render this file because it contains an unexpected character in line 25 and column 25.

View File

@ -1,43 +0,0 @@
void Builder::print( Code code )
{
Buffer.append_fmt( "%s\n", code->to_string() );
}
void Builder::print_fmt( char const* fmt, ... )
{
sw res;
char buf[ GEN_PRINTF_MAXLEN ] = { 0 };
va_list va;
va_start( va, fmt );
res = str_fmt_va( buf, count_of( buf ) - 1, fmt, va ) - 1;
va_end( va );
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()
{
bool result = file_write( & File, Buffer, Buffer.length() );
if ( result == false )
log_failure("gen::File::write - Failed to write to file: %s", file_name( & File ) );
file_close( & File );
Buffer.free();
}

View File

@ -1,11 +0,0 @@
struct Builder
{
FileInfo File;
String Buffer;
void print( Code );
void print_fmt( char const* fmt, ... );
bool open( char const* path );
void write();
};

Some files were not shown because too many files have changed in this diff Show More