1
0
mirror of https://github.com/Ed94/gencpp.git synced 2025-07-09 15:15:43 -07:00

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.
This commit is contained in:
2023-08-21 20:30:13 -04:00
parent 1241f44fd4
commit 050b00f28a
45 changed files with 326 additions and 165 deletions

@ -1,3 +1,6 @@
#pragma once
#include "static_data.cpp"
Code Code::Global;
Code Code::Invalid;
@ -41,36 +44,34 @@ String AST::to_string()
case Comment:
{
if ( Prev && Prev->Type != Comment && Prev->Type != NewLine )
result.append("\n");
result.append( "\n" );
static char line[MaxCommentLineLength];
static char line[ MaxCommentLineLength ];
s32 left = Content.length();
s32 index = 0;
s32 curr = 0;
char const* end = & scast(String, Content).back();
char* scanner = Content.Data;
s32 curr = 0;
do
{
s32 length = 1;
while ( left && Content[index] != '\n' )
char const* next = scanner;
s32 length = 0;
while ( next != end && scanner[ length ] != '\n' )
{
next = scanner + length;
length++;
left--;
index++;
}
index++;
str_copy( line, (char const*)Content + curr, length );
result.append_fmt( "//%.*s", length, line );
mem_set( line, 0, MaxCommentLineLength);
length++;
left--;
curr = index;
str_copy( line, scanner, length );
result.append_fmt( "//%.*s", length, line );
mem_set( line, 0, MaxCommentLineLength );
scanner += length;
}
while ( left--, left > 0 );
while ( scanner <= end );
if ( result.back() != '\n' )
result.append("\n");
result.append( "\n" );
}
break;

@ -1,3 +1,9 @@
#pragma once
#include "types.hpp"
#include "temp/ecode.hpp"
#include "temp/eoperator.hpp"
#include "temp/especifier.hpp"
struct AST;
struct AST_Body;
struct AST_Attributes;
@ -163,10 +169,11 @@ struct AST
Code& entry ( u32 idx );
bool has_entries();
bool is_equal ( AST* other );
String to_string ();
char const* type_str();
bool validate_body();
neverinline String to_string();
template< class Type >
Type cast()
{
@ -574,4 +581,3 @@ Define_CodeType( Var );
#undef Using_Code
#pragma endregion Code Types

@ -1,3 +1,6 @@
#pragma once
#include "ast.hpp"
#pragma region AST Types
/*
Show only relevant members of the AST for its type.
@ -539,4 +542,3 @@ struct AST_Var
};
static_assert( sizeof(AST_Var) == sizeof(AST), "ERROR: AST_Var is not the same size as AST");
#pragma endregion AST Types

@ -1,3 +1,7 @@
#pragma once
#include "inlines.hpp"
#include "temp/ast_inlines.hpp"
#pragma region Constants
#ifndef GEN_GLOBAL_BUCKET_SIZE
@ -73,6 +77,7 @@ extern CodeSpecifiers spec_constexpr;
extern CodeSpecifiers spec_constinit;
extern CodeSpecifiers spec_extern_linkage;
extern CodeSpecifiers spec_final;
extern CodeSpeciifers spec_forceinline;
extern CodeSpecifiers spec_global;
extern CodeSpecifiers spec_inline;
extern CodeSpecifiers spec_internal_linkage;
@ -168,4 +173,3 @@ extern CodeType t_typename;
extern AllocatorInfo Allocator_TypeTable;
#endif

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

@ -1,3 +1,6 @@
#pragma once
#include "interface.hpp"
void AST::append( AST* other )
{
if ( other->Parent )
@ -211,4 +214,3 @@ StrC token_fmt_impl( sw num, ... )
return { result, buf };
}

@ -1,3 +1,6 @@
#pragma once
#include "ast.cpp"
internal void init_parser();
internal void deinit_parser();
@ -170,10 +173,12 @@ void define_constants()
#endif
# undef def_constant_code_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
@ -189,6 +194,7 @@ void define_constants()
def_constant_spec( constinit, ESpecifier::Constinit );
def_constant_spec( extern_linkage, ESpecifier::External_Linkage );
def_constant_spec( final, ESpecifier::Final );
def_constant_spec( forceinline, ESpecifier::ForceInline );
def_constant_spec( global, ESpecifier::Global );
def_constant_spec( inline, ESpecifier::Inline );
def_constant_spec( internal_linkage, ESpecifier::Internal_Linkage );
@ -209,6 +215,7 @@ void define_constants()
spec_local_persist = def_specifiers( 1, ESpecifier::Local_Persist );
spec_local_persist.set_global();
# pragma pop_macro( "forceinline" )
# pragma pop_macro( "global" )
# pragma pop_macro( "internal" )
# pragma pop_macro( "local_persist" )
@ -443,4 +450,3 @@ void set_allocator_string_table( AllocatorInfo allocator )
{
Allocator_StringArena = allocator;
}

@ -1,3 +1,6 @@
#pragma once
#include "ast_types.hpp"
#pragma region Gen Interface
// Initialize the library.
@ -175,4 +178,3 @@ Code untyped_token_fmt( char const* fmt, s32 num_tokens, ... );
#pragma endregion Untyped text
#pragma endregion Gen Interface

@ -1,3 +1,7 @@
#pragma once
#include "temp/etoktype.cpp"
#include "interface.upfront.cpp"
namespace Parser
{
struct Token
@ -1140,7 +1144,7 @@ internal CodeExtern parse_exten_link ();
internal CodeFriend parse_friend ();
internal CodeFn parse_function ();
internal CodeNS parse_namespace ();
internal CodeOpCast parse_operator_cast ();
internal CodeOpCast parse_operator_cast ( CodeSpecifiers specifiers = NoCode );
internal CodeStruct parse_struct ( bool inplace_def = false );
internal CodeVar parse_variable ();
internal CodeTemplate parse_template ();
@ -2259,6 +2263,27 @@ Code parse_simple_preprocess( Parser::TokType which )
}
eat( TokType::BraceCurly_Close );
StrC prev_proc = Context.Scope->Prev->ProcName;
if ( str_compare( prev_proc.Ptr, "parse_typedef", prev_proc.Len ) != 0 )
{
if ( check( TokType::Statement_End ))
{
eat( TokType::Statement_End );
}
}
tok.Length = ( (sptr)prevtok.Text + prevtok.Length ) - (sptr)tok.Text;
}
else
{
if ( str_compare( Context.Scope->Prev->ProcName.Ptr, "parse_typedef", Context.Scope->Prev->ProcName.Len ) != 0 )
{
if ( check( TokType::Statement_End ))
{
eat( TokType::Statement_End );
}
}
tok.Length = ( (sptr)prevtok.Text + prevtok.Length ) - (sptr)tok.Text;
}
@ -2267,14 +2292,6 @@ Code parse_simple_preprocess( Parser::TokType which )
Code result = untyped_str( to_str( content ) );
Context.Scope->Name = tok;
if ( str_compare( Context.Scope->Prev->ProcName.Ptr, "parse_typedef", Context.Scope->Prev->ProcName.Len ) != 0 )
{
if ( check( TokType::Statement_End ))
{
eat( TokType::Statement_End );
}
}
Context.pop();
return result;
}
@ -2654,8 +2671,10 @@ CodeBody parse_class_struct_body( Parser::TokType which, Parser::Token name = Pa
case TokType::Spec_Consteval:
case TokType::Spec_Constexpr:
case TokType::Spec_Constinit:
case TokType::Spec_ForceInline:
case TokType::Spec_Inline:
case TokType::Spec_Mutable:
case TokType::Spec_NeverInline:
case TokType::Spec_Static:
case TokType::Spec_Volatile:
{
@ -2671,7 +2690,9 @@ CodeBody parse_class_struct_body( Parser::TokType which, Parser::Token name = Pa
case ESpecifier::Constexpr:
case ESpecifier::Constinit:
case ESpecifier::Inline:
case ESpecifier::ForceInline:
case ESpecifier::Mutable:
case ESpecifier::NeverInline:
case ESpecifier::Static:
case ESpecifier::Volatile:
break;
@ -2701,6 +2722,12 @@ CodeBody parse_class_struct_body( Parser::TokType which, Parser::Token name = Pa
member = parse_destructor( specifiers );
break;
}
if ( currtok.Type == TokType::Decl_Operator )
{
member = parse_operator_cast( specifiers );
break;
}
}
//! Fallthrough intentional
case TokType::Identifier:
@ -3028,6 +3055,7 @@ CodeBody parse_global_nspace( CodeT which )
case TokType::Spec_Constexpr:
case TokType::Spec_Constinit:
case TokType::Spec_Extern:
case TokType::Spec_ForceInline:
case TokType::Spec_Global:
case TokType::Spec_Inline:
case TokType::Spec_Internal_Linkage:
@ -3047,6 +3075,7 @@ CodeBody parse_global_nspace( CodeT which )
{
case ESpecifier::Constexpr:
case ESpecifier::Constinit:
case ESpecifier::ForceInline:
case ESpecifier::Global:
case ESpecifier::External_Linkage:
case ESpecifier::Internal_Linkage:
@ -3696,7 +3725,9 @@ CodeFn parse_functon()
case ESpecifier::Consteval:
case ESpecifier::Constexpr:
case ESpecifier::External_Linkage:
case ESpecifier::ForceInline:
case ESpecifier::Inline:
case ESpecifier::NeverInline:
case ESpecifier::Static:
break;
@ -3840,7 +3871,9 @@ CodeOperator parse_operator()
{
case ESpecifier::Const:
case ESpecifier::Constexpr:
case ESpecifier::ForceInline:
case ESpecifier::Inline:
case ESpecifier::NeverInline:
case ESpecifier::Static:
break;
@ -3885,11 +3918,14 @@ CodeOperator parse_operator( StrC def )
return (CodeOperator) parse_operator();
}
CodeOpCast parse_operator_cast()
CodeOpCast parse_operator_cast( CodeSpecifiers specifiers )
{
using namespace Parser;
push_scope();
// Specifiers attributed to the cast
// Operator's namespace if not within same class.
Token name = NullToken;
if ( check( TokType::Identifier ) )
{
@ -3914,11 +3950,14 @@ CodeOpCast parse_operator_cast()
eat( TokType::Capture_Start );
eat( TokType::Capture_End );
CodeSpecifiers specifiers = { nullptr };
if ( check(TokType::Spec_Const))
{
specifiers = spec_const;
if ( specifiers.ast == nullptr )
specifiers = def_specifier( ESpecifier::Const );
else
specifiers.append( ESpecifier::Const );
eat( TokType::Spec_Const );
}
@ -4852,4 +4891,3 @@ CodeVar parse_variable( StrC def )
# undef left
# undef check
# undef push_scope

@ -1,3 +1,6 @@
#pragma once
#include "interface.parsing.cpp"
sw token_fmt_va( char* buf, uw buf_size, s32 num_tokens, va_list va )
{
char const* buf_begin = buf;
@ -181,4 +184,3 @@ Code untyped_token_fmt( s32 num_tokens, ... )
return result;
}

@ -1,3 +1,6 @@
#pragma once
#include "interface.cpp"
#pragma region Upfront
enum class OpValidateResult : u32
@ -2255,4 +2258,3 @@ CodeBody def_union_body( s32 num, CodeUnion* codes )
#pragma endregion Upfront

@ -1,3 +1,6 @@
#pragma once
#include "src_start.cpp"
#pragma region StaticData
// TODO : Convert global allocation strategy to use a slab allocation strategy.
@ -47,6 +50,7 @@ global CodeSpecifiers spec_constexpr;
global CodeSpecifiers spec_constinit;
global CodeSpecifiers spec_extern_linkage;
global CodeSpecifiers spec_final;
global CodeSpeciifers spec_forceinline;
global CodeSpecifiers spec_global;
global CodeSpecifiers spec_inline;
global CodeSpecifiers spec_internal_linkage;
@ -95,4 +99,3 @@ global CodeType t_f64;
#endif
#pragma endregion Constants

@ -1,3 +1,5 @@
#pragma once
// This is the non-bootstraped version of the Common AST Implementation. This will be obsolete once bootstrap is stress tested.
#pragma region AST Common

@ -1,3 +1,5 @@
#pragma once
// This is the non-bootstraped version of the ECode. This will be obsolete once bootstrap is stress tested.
namespace ECode

@ -1,3 +1,5 @@
#pragma once
// This is the non-bootstraped version of the EOperator. This will be obsolete once bootstrap is stress tested.
namespace EOperator

@ -1,3 +1,5 @@
#pragma once
// This is the non-bootstraped version of the ESpecifier. This will be obsolete once bootstrap is stress tested.
namespace ESpecifier
@ -15,6 +17,7 @@ namespace ESpecifier
Entry( Constinit, constinit ) \
Entry( Explicit, explicit ) \
Entry( External_Linkage, extern ) \
Entry( ForceInline, forceinline ) \
Entry( Global, global ) \
Entry( Inline, inline ) \
Entry( Internal_Linkage, internal ) \

@ -1,3 +1,5 @@
#pragma once
namespace Parser
{
/*
@ -78,6 +80,7 @@ namespace Parser
Entry( Spec_Constinit, "constinit" ) \
Entry( Spec_Explicit, "explicit" ) \
Entry( Spec_Extern, "extern" ) \
Entry( Spec_ForceInline, "forceinline" ) \
Entry( Spec_Final, "final" ) \
Entry( Spec_Global, "global" ) \
Entry( Spec_Inline, "inline" ) \

@ -1,3 +1,6 @@
#pragma once
#include "header_start.hpp"
using LogFailType = sw(*)(char const*, ...);
// By default this library will either crash or exit if an error is detected while generating codes.
@ -74,4 +77,3 @@ constexpr EPreprocessCond PreprocessCond_If = EPreprocessCond::If;
constexpr EPreprocessCond PreprocessCond_IfDef = EPreprocessCond::IfDef;
constexpr EPreprocessCond PreprocessCond_IfNotDef = EPreprocessCond::IfNotDef;
constexpr EPreprocessCond PreprocessCond_ElIf = EPreprocessCond::ElIf;