2023-04-10 18:33:06 -07:00
|
|
|
// ReSharper disable CppClangTidyClangDiagnosticSwitchEnum
|
2023-04-01 19:21:46 -07:00
|
|
|
#include "Bloat.hpp"
|
|
|
|
#include "gen.hpp"
|
|
|
|
|
2023-04-10 18:33:06 -07:00
|
|
|
#ifdef gen_time
|
2023-04-01 19:21:46 -07:00
|
|
|
namespace gen
|
|
|
|
{
|
2023-06-29 19:48:47 -07:00
|
|
|
ZPL_TABLE_DEFINE( StringTable, str_tbl_, String );
|
2023-04-05 00:03:56 -07:00
|
|
|
|
2023-04-03 00:55:28 -07:00
|
|
|
namespace StaticData
|
|
|
|
{
|
2023-04-09 21:38:47 -07:00
|
|
|
static Array(Pool) CodePools = nullptr;
|
|
|
|
static Array(Arena) CodeEntriesArenas = nullptr;
|
|
|
|
static Array(Arena) StringArenas = nullptr;
|
2023-04-05 00:03:56 -07:00
|
|
|
|
2023-04-05 23:21:23 -07:00
|
|
|
static StringTable StringMap;
|
2023-05-09 18:54:54 -07:00
|
|
|
// static TypeTable TypeMap;
|
2023-04-05 00:03:56 -07:00
|
|
|
|
2023-04-09 21:38:47 -07:00
|
|
|
static AllocatorInfo Allocator_DataArrays = heap();
|
|
|
|
static AllocatorInfo Allocator_CodePool = heap();
|
|
|
|
static AllocatorInfo Allocator_CodeEntriesArena = heap();
|
|
|
|
static AllocatorInfo Allocator_StringArena = heap();
|
|
|
|
static AllocatorInfo Allocator_StringTable = heap();
|
|
|
|
static AllocatorInfo Allocator_TypeTable = heap();
|
2023-04-03 00:55:28 -07:00
|
|
|
}
|
|
|
|
|
2023-04-09 21:38:47 -07:00
|
|
|
#pragma region Constants
|
2023-04-22 19:24:55 -07:00
|
|
|
#ifdef GEN_DEFINE_LIBRARY_CODE_CONSTANTS
|
2023-06-30 08:54:27 -07:00
|
|
|
Code type_ns(auto);
|
2023-04-06 16:19:11 -07:00
|
|
|
Code type_ns(void);
|
2023-05-07 12:03:24 -07:00
|
|
|
Code type_ns(int);
|
2023-04-06 16:19:11 -07:00
|
|
|
Code type_ns(bool);
|
|
|
|
Code type_ns(char);
|
2023-05-07 12:03:24 -07:00
|
|
|
Code type_ns(wchar_t);
|
2023-04-03 23:04:19 -07:00
|
|
|
|
2023-04-06 16:19:11 -07:00
|
|
|
Code type_ns(s8);
|
|
|
|
Code type_ns(s16);
|
|
|
|
Code type_ns(s32);
|
|
|
|
Code type_ns(s64);
|
2023-04-03 23:04:19 -07:00
|
|
|
|
2023-04-06 16:19:11 -07:00
|
|
|
Code type_ns(u8);
|
|
|
|
Code type_ns(u16);
|
|
|
|
Code type_ns(u32);
|
|
|
|
Code type_ns(u64);
|
2023-04-03 23:04:19 -07:00
|
|
|
|
2023-04-06 16:19:11 -07:00
|
|
|
Code type_ns(sw);
|
|
|
|
Code type_ns(uw);
|
2023-04-03 23:04:19 -07:00
|
|
|
|
2023-04-06 16:19:11 -07:00
|
|
|
Code type_ns(f32);
|
|
|
|
Code type_ns(f64);
|
2023-04-22 19:24:55 -07:00
|
|
|
#endif
|
2023-04-03 23:04:19 -07:00
|
|
|
|
2023-04-09 21:38:47 -07:00
|
|
|
Code access_public;
|
|
|
|
Code access_protected;
|
|
|
|
Code access_private;
|
|
|
|
|
2023-06-28 21:20:23 -07:00
|
|
|
Code module_global_fragment;
|
|
|
|
Code module_private_fragment;
|
|
|
|
|
2023-06-28 22:37:42 -07:00
|
|
|
Code pragma_once;
|
|
|
|
|
2023-04-09 21:38:47 -07:00
|
|
|
Code spec_const;
|
2023-05-09 18:54:54 -07:00
|
|
|
Code spec_consteval;
|
|
|
|
Code spec_constexpr;
|
|
|
|
Code spec_constinit;
|
|
|
|
Code spec_extern_linkage;
|
2023-04-05 00:03:56 -07:00
|
|
|
Code spec_inline;
|
2023-05-09 18:54:54 -07:00
|
|
|
Code spec_internal_linkage;
|
|
|
|
Code spec_local_persist;
|
|
|
|
Code spec_mutable;
|
|
|
|
Code spec_ptr;
|
2023-04-09 21:38:47 -07:00
|
|
|
Code spec_ref;
|
2023-05-09 18:54:54 -07:00
|
|
|
Code spec_register;
|
|
|
|
Code spec_rvalue;
|
|
|
|
Code spec_static_member;
|
|
|
|
Code spec_thread_local;
|
|
|
|
Code spec_volatile;
|
|
|
|
Code spec_type_signed;
|
|
|
|
Code spec_type_unsigned;
|
|
|
|
Code spec_type_short;
|
|
|
|
Code spec_type_long;
|
2023-04-09 21:38:47 -07:00
|
|
|
#pragma endregion Constants
|
2023-04-03 23:04:19 -07:00
|
|
|
|
2023-04-22 19:24:55 -07:00
|
|
|
#pragma region AST Body Case Macros
|
|
|
|
# define AST_BODY_CLASS_UNALLOWED_TYPES \
|
2023-05-01 11:12:07 -07:00
|
|
|
case Attributes: \
|
2023-04-22 19:24:55 -07:00
|
|
|
case Class_Body: \
|
|
|
|
case Enum_Body: \
|
2023-04-23 16:04:43 -07:00
|
|
|
case Extern_Linkage: \
|
2023-04-22 19:24:55 -07:00
|
|
|
case Function_Body: \
|
|
|
|
case Function_Fwd: \
|
|
|
|
case Global_Body: \
|
|
|
|
case Namespace: \
|
|
|
|
case Namespace_Body: \
|
|
|
|
case Operator: \
|
|
|
|
case Operator_Fwd: \
|
|
|
|
case Parameters: \
|
|
|
|
case Specifiers: \
|
|
|
|
case Struct_Body: \
|
|
|
|
case Typename:
|
|
|
|
|
|
|
|
# define AST_BODY_FUNCTION_UNALLOWED_TYPES \
|
|
|
|
case Access_Public: \
|
|
|
|
case Access_Protected: \
|
|
|
|
case Access_Private: \
|
2023-05-01 11:12:07 -07:00
|
|
|
case Attributes: \
|
2023-04-22 19:24:55 -07:00
|
|
|
case Class_Body: \
|
|
|
|
case Enum_Body: \
|
2023-04-23 16:04:43 -07:00
|
|
|
case Extern_Linkage: \
|
2023-04-22 19:24:55 -07:00
|
|
|
case Friend: \
|
|
|
|
case Function_Body: \
|
|
|
|
case Function_Fwd: \
|
|
|
|
case Global_Body: \
|
|
|
|
case Namespace: \
|
|
|
|
case Namespace_Body: \
|
|
|
|
case Operator: \
|
|
|
|
case Operator_Fwd: \
|
|
|
|
case Operator_Member: \
|
|
|
|
case Operator_Member_Fwd: \
|
|
|
|
case Parameters: \
|
|
|
|
case Specifiers: \
|
|
|
|
case Struct_Body: \
|
|
|
|
case Typename:
|
|
|
|
|
|
|
|
# define AST_BODY_GLOBAL_UNALLOWED_TYPES \
|
|
|
|
case Access_Public: \
|
|
|
|
case Access_Protected: \
|
|
|
|
case Access_Private: \
|
2023-05-01 11:12:07 -07:00
|
|
|
case Attributes: \
|
2023-04-22 19:24:55 -07:00
|
|
|
case Class_Body: \
|
|
|
|
case Enum_Body: \
|
|
|
|
case Execution: \
|
|
|
|
case Friend: \
|
|
|
|
case Function_Body: \
|
|
|
|
case Global_Body: \
|
|
|
|
case Namespace_Body: \
|
|
|
|
case Operator_Member: \
|
|
|
|
case Operator_Member_Fwd: \
|
|
|
|
case Parameters: \
|
|
|
|
case Specifiers: \
|
|
|
|
case Struct_Body: \
|
|
|
|
case Typename:
|
|
|
|
|
2023-05-01 11:12:07 -07:00
|
|
|
# define AST_BODY_EXPORT_UNALLOWED_TYPES AST_BODY_GLOBAL_UNALLOWED_TYPES
|
|
|
|
# define AST_BODY_NAMESPACE_UNALLOWED_TYPES AST_BODY_GLOBAL_UNALLOWED_TYPES
|
|
|
|
# define AST_BODY_EXTERN_LINKAGE_UNALLOWED_TYPES AST_BODY_GLOBAL_UNALLOWED_TYPES
|
2023-04-23 16:04:43 -07:00
|
|
|
|
2023-05-05 21:18:44 -07:00
|
|
|
# define AST_BODY_STRUCT_UNALLOWED_TYPES AST_BODY_CLASS_UNALLOWED_TYPES
|
2023-04-22 19:24:55 -07:00
|
|
|
#pragma endregion AST Body Case Macros
|
|
|
|
|
|
|
|
#pragma region AST
|
2023-04-10 18:33:06 -07:00
|
|
|
Code Code::Invalid;
|
2023-04-09 21:38:47 -07:00
|
|
|
|
2023-04-07 21:29:09 -07:00
|
|
|
AST* AST::duplicate()
|
|
|
|
{
|
|
|
|
using namespace ECode;
|
|
|
|
|
|
|
|
Code
|
2023-04-10 18:33:06 -07:00
|
|
|
result = make_code();
|
2023-04-07 21:29:09 -07:00
|
|
|
result->Parent = Parent;
|
|
|
|
result->Name = Name;
|
|
|
|
result->Type = Type;
|
|
|
|
result->Op = Op;
|
|
|
|
result->Readonly = Readonly;
|
|
|
|
|
|
|
|
switch ( Type )
|
|
|
|
{
|
2023-04-09 21:38:47 -07:00
|
|
|
case Invalid:
|
2023-05-07 12:03:24 -07:00
|
|
|
log_failure( "AST::duplicate: Cannot duplicate an invalid AST." );
|
|
|
|
return nullptr;
|
2023-04-09 21:38:47 -07:00
|
|
|
|
2023-04-07 21:29:09 -07:00
|
|
|
case Untyped:
|
2023-05-01 11:12:07 -07:00
|
|
|
case Comment:
|
|
|
|
case Preprocessor_Include:
|
2023-04-07 21:29:09 -07:00
|
|
|
case Access_Public:
|
|
|
|
case Access_Protected:
|
|
|
|
case Access_Private:
|
2023-05-01 11:12:07 -07:00
|
|
|
case Attributes:
|
|
|
|
case Module:
|
2023-04-07 21:29:09 -07:00
|
|
|
// Can just be the same, as its a cached string.
|
|
|
|
result->Content = Content;
|
2023-04-10 18:33:06 -07:00
|
|
|
return result;
|
2023-04-07 21:29:09 -07:00
|
|
|
|
2023-06-30 00:13:41 -07:00
|
|
|
case Specifiers:
|
|
|
|
// Need to memcpy the specifiers.
|
|
|
|
mem_copy( result->ArrSpecs, ArrSpecs, sizeof( ArrSpecs ) );
|
|
|
|
result->StaticIndex = StaticIndex;
|
|
|
|
return result;
|
|
|
|
|
2023-04-07 21:29:09 -07:00
|
|
|
// The main purpose of this is to make sure entires in the AST are unique,
|
|
|
|
// So that we can assign the new parent without corrupting the existing AST.
|
|
|
|
case Class:
|
2023-04-23 16:04:43 -07:00
|
|
|
case Class_Fwd:
|
2023-04-07 21:29:09 -07:00
|
|
|
case Class_Body:
|
|
|
|
case Enum:
|
2023-04-23 16:04:43 -07:00
|
|
|
case Enum_Fwd:
|
2023-04-07 21:29:09 -07:00
|
|
|
case Enum_Body:
|
2023-04-23 16:04:43 -07:00
|
|
|
case Enum_Class:
|
|
|
|
case Enum_Class_Fwd:
|
2023-05-01 11:12:07 -07:00
|
|
|
case Export_Body:
|
|
|
|
case Execution:
|
2023-04-23 16:04:43 -07:00
|
|
|
case Extern_Linkage:
|
|
|
|
case Extern_Linkage_Body:
|
2023-04-07 21:29:09 -07:00
|
|
|
case Friend:
|
2023-04-23 16:04:43 -07:00
|
|
|
case Function:
|
|
|
|
case Function_Fwd:
|
|
|
|
case Function_Body:
|
2023-04-07 21:29:09 -07:00
|
|
|
case Global_Body:
|
|
|
|
case Namespace:
|
|
|
|
case Namespace_Body:
|
2023-04-23 16:04:43 -07:00
|
|
|
case Operator:
|
|
|
|
case Operator_Fwd:
|
|
|
|
case Operator_Member:
|
|
|
|
case Operator_Member_Fwd:
|
2023-04-07 21:29:09 -07:00
|
|
|
case Parameters:
|
|
|
|
case Struct:
|
2023-04-10 18:33:06 -07:00
|
|
|
case Struct_Fwd:
|
2023-04-07 21:29:09 -07:00
|
|
|
case Struct_Body:
|
|
|
|
case Typedef:
|
|
|
|
case Typename:
|
2023-05-01 11:12:07 -07:00
|
|
|
case Union:
|
|
|
|
case Union_Body:
|
2023-04-07 21:29:09 -07:00
|
|
|
case Using:
|
2023-04-23 16:04:43 -07:00
|
|
|
case Using_Namespace:
|
2023-05-05 15:10:03 -07:00
|
|
|
case Variable:
|
2023-04-10 18:33:06 -07:00
|
|
|
s32 index = 0;
|
|
|
|
s32 left = num_entries();
|
2023-04-07 21:29:09 -07:00
|
|
|
while ( left -- )
|
|
|
|
{
|
|
|
|
// This will naturally duplicate the entire chain duplicate all of the ast nodes.
|
|
|
|
// It may not be the most optimal way for memory reasons, however figuring out the heuristic
|
|
|
|
// For when it should reparent all nodes or not is not within the simple scope of this library.
|
2023-06-28 18:20:29 -07:00
|
|
|
result->add_entry( entry(index)->duplicate() );
|
2023-04-07 21:29:09 -07:00
|
|
|
index++;
|
|
|
|
}
|
2023-04-10 18:33:06 -07:00
|
|
|
return result;
|
2023-04-07 21:29:09 -07:00
|
|
|
}
|
2023-05-07 12:03:24 -07:00
|
|
|
|
|
|
|
log_failure( "AST::duplicate: Unknown AST type %s.", type_str() );
|
|
|
|
return nullptr;
|
2023-04-07 21:29:09 -07:00
|
|
|
}
|
|
|
|
|
2023-04-22 21:43:31 -07:00
|
|
|
String AST::to_string()
|
2023-06-28 18:20:29 -07:00
|
|
|
{
|
2023-06-28 11:43:21 -07:00
|
|
|
# define ProcessModuleFlags() \
|
2023-05-05 21:18:44 -07:00
|
|
|
if ( bitfield_is_equal( u32, ModuleFlags, ModuleFlag::Export )) \
|
2023-05-06 12:49:43 -07:00
|
|
|
result.append( "export " ); \
|
2023-05-05 21:18:44 -07:00
|
|
|
\
|
|
|
|
if ( bitfield_is_equal( u32, ModuleFlags, ModuleFlag::Import )) \
|
2023-05-06 12:49:43 -07:00
|
|
|
result.append( "import " ); \
|
2023-05-05 21:18:44 -07:00
|
|
|
|
2023-05-06 12:49:43 -07:00
|
|
|
String result = String::make( g_allocator, "" );
|
2023-04-06 16:19:11 -07:00
|
|
|
|
2023-05-05 21:18:44 -07:00
|
|
|
// Scope indentation.
|
|
|
|
char indent_str[128] = {0}; // Should be more than enough...
|
|
|
|
s32 tab_count = 0;
|
|
|
|
{
|
|
|
|
AST* curr_parent = Parent;
|
2023-06-28 11:43:21 -07:00
|
|
|
while ( curr_parent )
|
2023-05-05 21:18:44 -07:00
|
|
|
{
|
2023-06-28 11:43:21 -07:00
|
|
|
switch ( curr_parent->Type )
|
|
|
|
{
|
|
|
|
using namespace ECode;
|
|
|
|
|
|
|
|
case Class_Body:
|
|
|
|
case Enum_Body:
|
|
|
|
case Extern_Linkage_Body:
|
|
|
|
case Function_Body:
|
|
|
|
case Global_Body:
|
|
|
|
case Namespace_Body:
|
|
|
|
case Struct_Body:
|
|
|
|
case Union_Body:
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
{
|
|
|
|
curr_parent = curr_parent->Parent;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-05-05 21:18:44 -07:00
|
|
|
indent_str[tab_count] = '\t';
|
|
|
|
tab_count++;
|
2023-06-28 11:43:21 -07:00
|
|
|
|
|
|
|
curr_parent = curr_parent->Parent;
|
2023-05-05 21:18:44 -07:00
|
|
|
}
|
|
|
|
}
|
2023-05-06 12:49:43 -07:00
|
|
|
StrC indent = { tab_count, indent_str };
|
2023-05-05 21:18:44 -07:00
|
|
|
|
2023-04-06 16:19:11 -07:00
|
|
|
switch ( Type )
|
|
|
|
{
|
|
|
|
using namespace ECode;
|
|
|
|
|
|
|
|
case Invalid:
|
2023-04-09 10:59:39 -07:00
|
|
|
log_failure("Attempted to serialize invalid code! - %s", Name);
|
2023-04-06 16:19:11 -07:00
|
|
|
break;
|
|
|
|
|
|
|
|
case Untyped:
|
2023-05-06 12:49:43 -07:00
|
|
|
result.append( Content );
|
2023-04-06 16:19:11 -07:00
|
|
|
break;
|
|
|
|
|
2023-05-01 11:12:07 -07:00
|
|
|
case Comment:
|
|
|
|
{
|
2023-05-06 12:49:43 -07:00
|
|
|
result.append( indent );
|
2023-05-05 21:18:44 -07:00
|
|
|
|
2023-05-01 11:12:07 -07:00
|
|
|
static char line[MaxCommentLineLength];
|
|
|
|
|
2023-05-06 12:49:43 -07:00
|
|
|
s32 left = Content.length();
|
2023-05-01 11:12:07 -07:00
|
|
|
s32 index = 0;
|
|
|
|
do
|
|
|
|
{
|
|
|
|
s32 length = 0;
|
|
|
|
while ( left && Content[index] != '\n' )
|
2023-06-28 11:43:21 -07:00
|
|
|
{
|
2023-05-01 11:12:07 -07:00
|
|
|
length++;
|
2023-06-28 11:43:21 -07:00
|
|
|
left--;
|
|
|
|
}
|
2023-05-01 11:12:07 -07:00
|
|
|
|
2023-05-06 12:49:43 -07:00
|
|
|
str_copy( line, Content, length );
|
2023-05-01 11:12:07 -07:00
|
|
|
line[length] = '\0';
|
|
|
|
|
2023-06-28 11:43:21 -07:00
|
|
|
result.append_fmt( "// %s", line );
|
2023-05-01 11:12:07 -07:00
|
|
|
}
|
2023-06-28 11:43:21 -07:00
|
|
|
while ( left--, left > 0 );
|
2023-05-01 11:12:07 -07:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
2023-04-09 10:59:39 -07:00
|
|
|
case Access_Private:
|
2023-05-01 11:12:07 -07:00
|
|
|
case Access_Protected:
|
|
|
|
case Access_Public:
|
2023-05-06 12:49:43 -07:00
|
|
|
result.append( indent );
|
|
|
|
result.append( Name );
|
2023-04-09 10:59:39 -07:00
|
|
|
break;
|
|
|
|
|
2023-05-01 11:12:07 -07:00
|
|
|
case Attributes:
|
2023-05-06 12:49:43 -07:00
|
|
|
result.append( Content );
|
2023-05-01 11:12:07 -07:00
|
|
|
|
2023-04-09 10:59:39 -07:00
|
|
|
case Class:
|
2023-04-22 21:43:31 -07:00
|
|
|
{
|
2023-05-06 12:49:43 -07:00
|
|
|
result.append( indent );
|
2023-05-01 11:12:07 -07:00
|
|
|
|
2023-05-05 21:18:44 -07:00
|
|
|
ProcessModuleFlags();
|
|
|
|
|
2023-06-28 11:43:21 -07:00
|
|
|
if ( num_entries() > 1 )
|
|
|
|
{
|
|
|
|
result.append( "class " );
|
2023-04-22 21:43:31 -07:00
|
|
|
|
2023-06-28 11:43:21 -07:00
|
|
|
s32 idx = 1;
|
2023-04-22 21:43:31 -07:00
|
|
|
|
2023-06-28 18:20:29 -07:00
|
|
|
if ( entry( idx )->Type == Attributes )
|
2023-06-28 11:43:21 -07:00
|
|
|
{
|
2023-06-28 18:20:29 -07:00
|
|
|
result.append_fmt( "%s ", entry( idx )->to_string() );
|
2023-06-28 11:43:21 -07:00
|
|
|
idx++;
|
|
|
|
}
|
2023-04-22 21:43:31 -07:00
|
|
|
|
2023-06-28 11:43:21 -07:00
|
|
|
result.append( Name );
|
2023-04-22 21:43:31 -07:00
|
|
|
|
2023-06-29 19:48:47 -07:00
|
|
|
AST const* parent = entry( idx );
|
2023-04-22 21:43:31 -07:00
|
|
|
|
2023-06-28 11:43:21 -07:00
|
|
|
if ( parent )
|
|
|
|
{
|
|
|
|
char const* access_level = to_str( ParentAccess );
|
2023-05-05 21:18:44 -07:00
|
|
|
|
2023-06-28 11:43:21 -07:00
|
|
|
result.append_fmt( ": %s %s\n%s{\n"
|
|
|
|
, access_level
|
|
|
|
, parent
|
|
|
|
, indent_str
|
|
|
|
);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
result.append( "\n{\n" );
|
|
|
|
}
|
2023-05-05 21:18:44 -07:00
|
|
|
|
2023-06-28 21:20:23 -07:00
|
|
|
result.append_fmt( "%s\n%s};", body()->to_string(), indent_str );
|
2023-06-28 11:43:21 -07:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2023-06-28 21:20:23 -07:00
|
|
|
result.append_fmt( "class %s\n{\n%s\n%s};", Name, body()->to_string(), indent_str );
|
2023-06-28 11:43:21 -07:00
|
|
|
}
|
2023-04-22 21:43:31 -07:00
|
|
|
}
|
2023-04-09 10:59:39 -07:00
|
|
|
break;
|
|
|
|
|
2023-04-10 18:33:06 -07:00
|
|
|
case Class_Fwd:
|
2023-04-22 21:43:31 -07:00
|
|
|
{
|
2023-05-06 12:49:43 -07:00
|
|
|
result.append( indent );
|
2023-05-05 21:18:44 -07:00
|
|
|
|
|
|
|
ProcessModuleFlags();
|
|
|
|
|
2023-05-06 12:49:43 -07:00
|
|
|
result.append_fmt( "class %s;", Name );
|
2023-05-05 21:18:44 -07:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case Enum:
|
2023-05-07 12:03:24 -07:00
|
|
|
{
|
2023-05-06 12:49:43 -07:00
|
|
|
result.append( indent );
|
2023-05-05 21:18:44 -07:00
|
|
|
|
|
|
|
ProcessModuleFlags();
|
|
|
|
|
2023-05-06 12:49:43 -07:00
|
|
|
result.append( "enum " );
|
2023-04-09 10:59:39 -07:00
|
|
|
|
2023-06-28 18:20:29 -07:00
|
|
|
if ( num_entries() > 1 )
|
2023-04-22 21:43:31 -07:00
|
|
|
{
|
2023-06-28 18:20:29 -07:00
|
|
|
s32 idx = 1;
|
2023-04-22 21:43:31 -07:00
|
|
|
|
2023-06-30 00:13:41 -07:00
|
|
|
AST const* current = entry( idx);
|
2023-06-28 18:20:29 -07:00
|
|
|
|
2023-06-30 00:13:41 -07:00
|
|
|
if ( current->Type == Attributes )
|
2023-06-28 18:20:29 -07:00
|
|
|
{
|
|
|
|
result.append_fmt( "%s ", entry( idx )->to_string() );
|
|
|
|
idx++;
|
|
|
|
}
|
|
|
|
|
2023-06-30 00:13:41 -07:00
|
|
|
if ( current->Type == Typename)
|
2023-06-28 18:20:29 -07:00
|
|
|
{
|
|
|
|
result.append_fmt( "%s : %s\n{\n"
|
|
|
|
, Name
|
|
|
|
, entry( idx )->to_string()
|
|
|
|
);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
result.append_fmt( "%s\n{\n"
|
|
|
|
, Name
|
|
|
|
);
|
|
|
|
}
|
2023-04-22 21:43:31 -07:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2023-06-28 18:20:29 -07:00
|
|
|
result.append_fmt( "%s\n{\n"
|
2023-04-22 21:43:31 -07:00
|
|
|
, Name
|
|
|
|
);
|
|
|
|
}
|
2023-05-05 21:18:44 -07:00
|
|
|
|
2023-06-28 18:20:29 -07:00
|
|
|
result.append_fmt( "%s};"
|
2023-05-05 21:18:44 -07:00
|
|
|
, body()->to_string()
|
|
|
|
);
|
2023-05-07 12:03:24 -07:00
|
|
|
}
|
2023-04-09 10:59:39 -07:00
|
|
|
break;
|
|
|
|
|
2023-04-10 18:33:06 -07:00
|
|
|
case Enum_Fwd:
|
2023-06-28 18:20:29 -07:00
|
|
|
{
|
2023-05-06 12:49:43 -07:00
|
|
|
result.append( indent );
|
2023-05-05 21:18:44 -07:00
|
|
|
|
|
|
|
ProcessModuleFlags();
|
|
|
|
|
2023-06-28 18:20:29 -07:00
|
|
|
result.append_fmt( "enum %s : %s;", Name, entry( 0 )->to_string() );
|
|
|
|
}
|
2023-04-09 10:59:39 -07:00
|
|
|
break;
|
|
|
|
|
|
|
|
case Enum_Class:
|
2023-05-07 12:03:24 -07:00
|
|
|
{
|
2023-05-06 12:49:43 -07:00
|
|
|
result.append( indent );
|
2023-05-05 21:18:44 -07:00
|
|
|
|
|
|
|
ProcessModuleFlags();
|
|
|
|
|
2023-05-06 12:49:43 -07:00
|
|
|
result.append( "enum class " );
|
2023-05-05 21:18:44 -07:00
|
|
|
|
2023-06-29 19:48:47 -07:00
|
|
|
if ( num_entries() > 1 )
|
2023-05-05 21:18:44 -07:00
|
|
|
{
|
2023-06-29 19:48:47 -07:00
|
|
|
s32 idx = 1;
|
2023-05-05 21:18:44 -07:00
|
|
|
|
2023-06-29 19:48:47 -07:00
|
|
|
if ( entry( idx )->Type == Attributes )
|
|
|
|
{
|
|
|
|
result.append_fmt( "%s ", entry( idx )->to_string() );
|
|
|
|
idx++;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ( entry( idx )->Type == Typename )
|
|
|
|
{
|
|
|
|
result.append_fmt( "%s : %s\n%s{\n"
|
|
|
|
, Name
|
|
|
|
, entry( idx )->to_string()
|
|
|
|
, indent_str
|
|
|
|
);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
result.append_fmt( "%s\n{\n"
|
|
|
|
, Name
|
|
|
|
);
|
|
|
|
}
|
2023-04-22 21:43:31 -07:00
|
|
|
}
|
2023-05-05 21:18:44 -07:00
|
|
|
|
2023-06-30 00:13:41 -07:00
|
|
|
result.append_fmt( "%s\n};"
|
2023-05-05 21:18:44 -07:00
|
|
|
, body()->to_string()
|
|
|
|
);
|
2023-05-07 12:03:24 -07:00
|
|
|
}
|
2023-04-09 10:59:39 -07:00
|
|
|
break;
|
|
|
|
|
2023-04-10 18:33:06 -07:00
|
|
|
case Enum_Class_Fwd:
|
2023-05-07 12:03:24 -07:00
|
|
|
{
|
2023-05-06 12:49:43 -07:00
|
|
|
result.append( indent );
|
2023-05-05 21:18:44 -07:00
|
|
|
|
|
|
|
ProcessModuleFlags();
|
|
|
|
|
2023-05-06 12:49:43 -07:00
|
|
|
result.append( "enum class " );
|
2023-05-05 21:18:44 -07:00
|
|
|
|
|
|
|
s32 idx = 0;
|
|
|
|
|
2023-06-28 18:20:29 -07:00
|
|
|
if ( entry( idx )->Type == Attributes )
|
2023-05-05 21:18:44 -07:00
|
|
|
{
|
2023-06-28 18:20:29 -07:00
|
|
|
result.append_fmt( "%s ", entry( idx )->to_string() );
|
2023-05-05 21:18:44 -07:00
|
|
|
idx++;
|
|
|
|
}
|
|
|
|
|
2023-06-28 21:20:23 -07:00
|
|
|
result.append_fmt( "%s : %s;", Name, entry( idx )->to_string() );
|
2023-05-07 12:03:24 -07:00
|
|
|
}
|
2023-04-09 10:59:39 -07:00
|
|
|
break;
|
|
|
|
|
2023-05-01 11:12:07 -07:00
|
|
|
case Execution:
|
2023-05-06 12:49:43 -07:00
|
|
|
result.append( Content );
|
2023-05-01 11:12:07 -07:00
|
|
|
break;
|
|
|
|
|
|
|
|
case Export_Body:
|
|
|
|
{
|
2023-05-06 12:49:43 -07:00
|
|
|
result.append_fmt( "%sexport\n%s{\n", indent_str, indent_str );
|
2023-05-01 11:12:07 -07:00
|
|
|
|
|
|
|
s32 index = 0;
|
|
|
|
s32 left = num_entries();
|
|
|
|
while ( left -- )
|
|
|
|
{
|
2023-06-28 18:20:29 -07:00
|
|
|
result.append_fmt( "%s\t%s\n", indent_str, entry( index )->to_string() );
|
2023-05-01 11:12:07 -07:00
|
|
|
index++;
|
|
|
|
}
|
|
|
|
|
2023-06-28 21:20:23 -07:00
|
|
|
result.append_fmt( "%s};", indent_str );
|
2023-05-01 11:12:07 -07:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
2023-04-23 16:04:43 -07:00
|
|
|
case Extern_Linkage:
|
2023-05-06 12:49:43 -07:00
|
|
|
result.append( indent );
|
2023-05-05 21:18:44 -07:00
|
|
|
|
|
|
|
ProcessModuleFlags();
|
|
|
|
|
2023-06-28 21:20:23 -07:00
|
|
|
result.append_fmt( "extern \"%s\"\n%s{\n%s\n%s}"
|
2023-05-05 21:18:44 -07:00
|
|
|
, Name
|
|
|
|
, indent_str
|
|
|
|
, body()->to_string()
|
|
|
|
, indent_str
|
|
|
|
);
|
2023-04-23 16:04:43 -07:00
|
|
|
break;
|
|
|
|
|
2023-04-09 10:59:39 -07:00
|
|
|
case Friend:
|
2023-06-28 21:20:23 -07:00
|
|
|
result.append_fmt( "%sfriend %s", indent_str, entry( 0 )->to_string() );
|
2023-04-09 10:59:39 -07:00
|
|
|
break;
|
|
|
|
|
|
|
|
case Function:
|
2023-04-06 16:19:11 -07:00
|
|
|
{
|
2023-05-06 12:49:43 -07:00
|
|
|
result.append( indent );
|
2023-05-05 21:18:44 -07:00
|
|
|
|
|
|
|
ProcessModuleFlags();
|
|
|
|
|
2023-04-22 21:43:31 -07:00
|
|
|
u32 idx = 1;
|
|
|
|
u32 left = num_entries();
|
2023-04-06 16:19:11 -07:00
|
|
|
|
2023-06-30 00:13:41 -07:00
|
|
|
AST* current = entry( idx );
|
2023-06-28 21:20:23 -07:00
|
|
|
|
2023-06-30 00:13:41 -07:00
|
|
|
if ( current && current->Type == Attributes )
|
2023-05-05 21:18:44 -07:00
|
|
|
{
|
2023-06-30 00:13:41 -07:00
|
|
|
result.append_fmt( "%s ", current->to_string() );
|
2023-05-05 21:18:44 -07:00
|
|
|
idx++;
|
|
|
|
left--;
|
2023-06-30 00:13:41 -07:00
|
|
|
current = entry( idx );
|
2023-05-05 21:18:44 -07:00
|
|
|
}
|
2023-04-06 16:19:11 -07:00
|
|
|
|
2023-06-30 00:13:41 -07:00
|
|
|
if ( current && current->Type == Specifiers )
|
2023-04-06 16:19:11 -07:00
|
|
|
{
|
2023-06-30 00:13:41 -07:00
|
|
|
result.append_fmt( "%s\n", current->to_string() );
|
2023-04-22 21:43:31 -07:00
|
|
|
idx++;
|
2023-04-06 16:19:11 -07:00
|
|
|
left--;
|
2023-06-30 00:13:41 -07:00
|
|
|
current = entry( idx );
|
2023-04-06 16:19:11 -07:00
|
|
|
}
|
|
|
|
|
2023-06-30 00:13:41 -07:00
|
|
|
if ( current && current->Type == Typename )
|
2023-06-28 21:20:23 -07:00
|
|
|
{
|
2023-06-30 00:13:41 -07:00
|
|
|
result.append_fmt( "%s ", current->to_string() );
|
2023-06-28 21:20:23 -07:00
|
|
|
idx++;
|
|
|
|
left--;
|
2023-06-30 00:13:41 -07:00
|
|
|
current = entry( idx );
|
2023-06-28 21:20:23 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
result.append_fmt( "%s(", Name );
|
2023-04-06 16:19:11 -07:00
|
|
|
|
2023-06-30 00:13:41 -07:00
|
|
|
if ( left && current && current->Type == Parameters )
|
2023-04-06 16:19:11 -07:00
|
|
|
{
|
2023-06-30 00:13:41 -07:00
|
|
|
result.append_fmt("%s", current->to_string() );
|
2023-04-22 21:43:31 -07:00
|
|
|
idx++;
|
2023-04-06 16:19:11 -07:00
|
|
|
left--;
|
2023-06-30 00:13:41 -07:00
|
|
|
current = entry( idx );
|
2023-04-06 16:19:11 -07:00
|
|
|
}
|
2023-04-22 21:43:31 -07:00
|
|
|
else
|
|
|
|
{
|
2023-06-28 21:20:23 -07:00
|
|
|
result.append( "void" );
|
2023-04-22 21:43:31 -07:00
|
|
|
}
|
2023-04-06 16:19:11 -07:00
|
|
|
|
2023-05-06 12:49:43 -07:00
|
|
|
result.append_fmt( ")\n%s{\n%s\n%s}"
|
2023-05-05 21:18:44 -07:00
|
|
|
, indent_str
|
|
|
|
, body()->to_string()
|
|
|
|
, indent_str
|
|
|
|
);
|
2023-04-06 16:19:11 -07:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
2023-04-10 18:33:06 -07:00
|
|
|
case Function_Fwd:
|
2023-04-06 16:19:11 -07:00
|
|
|
{
|
2023-05-06 12:49:43 -07:00
|
|
|
result.append( indent );
|
|
|
|
|
|
|
|
ProcessModuleFlags();
|
2023-05-05 21:18:44 -07:00
|
|
|
|
2023-04-22 21:43:31 -07:00
|
|
|
u32 idx = 0;
|
2023-06-28 11:43:21 -07:00
|
|
|
u32 left = num_entries();
|
2023-04-06 16:19:11 -07:00
|
|
|
|
2023-06-30 00:13:41 -07:00
|
|
|
AST* current = entry( idx );
|
2023-06-28 11:43:21 -07:00
|
|
|
|
2023-06-30 00:13:41 -07:00
|
|
|
if ( current && current->Type == Attributes )
|
2023-05-06 12:49:43 -07:00
|
|
|
{
|
2023-06-30 00:13:41 -07:00
|
|
|
result.append_fmt( "%s ", current->to_string() );
|
2023-05-06 12:49:43 -07:00
|
|
|
idx++;
|
|
|
|
left--;
|
2023-06-30 00:13:41 -07:00
|
|
|
current = entry( idx );
|
2023-05-06 12:49:43 -07:00
|
|
|
}
|
|
|
|
|
2023-06-30 00:13:41 -07:00
|
|
|
if ( current && current->Type == Specifiers )
|
2023-04-06 16:19:11 -07:00
|
|
|
{
|
2023-06-30 00:13:41 -07:00
|
|
|
result.append_fmt( "%s\n", current->to_string() );
|
2023-04-22 21:43:31 -07:00
|
|
|
idx++;
|
2023-04-06 16:19:11 -07:00
|
|
|
left--;
|
2023-06-30 00:13:41 -07:00
|
|
|
current = entry( idx );
|
2023-04-06 16:19:11 -07:00
|
|
|
}
|
|
|
|
|
2023-06-30 00:13:41 -07:00
|
|
|
if ( current && current->Type == Typename )
|
2023-06-28 11:43:21 -07:00
|
|
|
{
|
2023-06-30 00:13:41 -07:00
|
|
|
result.append_fmt( "%s ", current->to_string() );
|
2023-06-28 11:43:21 -07:00
|
|
|
idx++;
|
|
|
|
left--;
|
2023-06-30 00:13:41 -07:00
|
|
|
current = entry( idx );
|
2023-06-28 11:43:21 -07:00
|
|
|
}
|
2023-04-06 16:19:11 -07:00
|
|
|
|
2023-06-28 11:43:21 -07:00
|
|
|
result.append_fmt( "%s(", Name );
|
|
|
|
|
2023-06-30 00:13:41 -07:00
|
|
|
if ( left && current && current->Type == Parameters )
|
2023-04-06 16:19:11 -07:00
|
|
|
{
|
2023-06-30 00:13:41 -07:00
|
|
|
result.append_fmt("%s", current->to_string() );
|
2023-04-22 21:43:31 -07:00
|
|
|
idx++;
|
2023-04-06 16:19:11 -07:00
|
|
|
left--;
|
2023-06-30 00:13:41 -07:00
|
|
|
current = entry( idx );
|
2023-04-06 16:19:11 -07:00
|
|
|
}
|
2023-04-22 21:43:31 -07:00
|
|
|
else
|
|
|
|
{
|
2023-05-06 12:49:43 -07:00
|
|
|
result.append( "void" );
|
2023-04-22 21:43:31 -07:00
|
|
|
}
|
2023-04-06 16:19:11 -07:00
|
|
|
|
2023-06-28 21:20:23 -07:00
|
|
|
result.append( ");" );
|
2023-04-06 16:19:11 -07:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
2023-05-01 11:12:07 -07:00
|
|
|
case Module:
|
2023-05-06 12:49:43 -07:00
|
|
|
result.append( indent );
|
2023-05-05 21:18:44 -07:00
|
|
|
|
2023-06-28 21:20:23 -07:00
|
|
|
if (((u32(ModuleFlag::Export) & u32(ModuleFlags)) == u32(ModuleFlag::Export)))
|
|
|
|
result.append("export ");
|
2023-05-01 11:12:07 -07:00
|
|
|
|
2023-06-28 21:20:23 -07:00
|
|
|
if (((u32(ModuleFlag::Import) & u32(ModuleFlags)) == u32(ModuleFlag::Import)))
|
|
|
|
result.append("import ");
|
|
|
|
|
|
|
|
result.append_fmt( "%s;", Name );
|
|
|
|
break;
|
2023-05-01 11:12:07 -07:00
|
|
|
|
2023-04-22 21:43:31 -07:00
|
|
|
case Namespace:
|
2023-05-06 12:49:43 -07:00
|
|
|
result.append( indent );
|
2023-05-05 21:18:44 -07:00
|
|
|
|
|
|
|
ProcessModuleFlags();
|
|
|
|
|
2023-06-28 21:20:23 -07:00
|
|
|
result.append_fmt( "namespace %s\n%s{\n%s\n%s};"
|
2023-05-05 21:18:44 -07:00
|
|
|
, Name
|
|
|
|
, indent_str
|
|
|
|
, body()->to_string()
|
|
|
|
, indent_str
|
|
|
|
);
|
2023-04-09 10:59:39 -07:00
|
|
|
break;
|
|
|
|
|
2023-04-22 21:43:31 -07:00
|
|
|
case Operator:
|
|
|
|
case Operator_Member:
|
|
|
|
{
|
2023-05-06 12:49:43 -07:00
|
|
|
result.append( indent );
|
2023-05-05 21:18:44 -07:00
|
|
|
|
|
|
|
ProcessModuleFlags();
|
|
|
|
|
2023-04-22 21:43:31 -07:00
|
|
|
s32 idx = 1;
|
2023-04-06 16:19:11 -07:00
|
|
|
|
2023-06-28 18:20:29 -07:00
|
|
|
if ( entry( idx )->Type == Attributes )
|
2023-05-05 21:18:44 -07:00
|
|
|
{
|
2023-06-28 18:20:29 -07:00
|
|
|
result.append_fmt( "%s ", entry( idx )->to_string() );
|
2023-05-05 21:18:44 -07:00
|
|
|
idx++;
|
|
|
|
}
|
|
|
|
|
2023-06-28 18:20:29 -07:00
|
|
|
if ( entry( idx )->Type == Specifiers )
|
2023-04-22 21:43:31 -07:00
|
|
|
{
|
2023-06-28 18:20:29 -07:00
|
|
|
result.append_fmt( "%s\n", entry( idx )->to_string() );
|
2023-04-22 21:43:31 -07:00
|
|
|
idx++;
|
|
|
|
}
|
2023-04-06 16:19:11 -07:00
|
|
|
|
2023-06-29 19:48:47 -07:00
|
|
|
result.append_fmt( "%s %s (", entry( idx )->to_string(), Name );
|
2023-04-22 21:43:31 -07:00
|
|
|
idx++;
|
2023-04-09 10:59:39 -07:00
|
|
|
|
2023-06-28 18:20:29 -07:00
|
|
|
if ( entry( idx )->Type == Parameters )
|
2023-04-22 21:43:31 -07:00
|
|
|
{
|
2023-06-28 18:20:29 -07:00
|
|
|
result.append_fmt( "%s", entry( idx )->to_string() );
|
2023-04-22 21:43:31 -07:00
|
|
|
idx++;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2023-05-06 12:49:43 -07:00
|
|
|
result.append_fmt( "void" );
|
2023-04-22 21:43:31 -07:00
|
|
|
}
|
|
|
|
|
2023-06-29 19:48:47 -07:00
|
|
|
result.append_fmt( ")\n%s{\n%s\n}"
|
2023-05-05 21:18:44 -07:00
|
|
|
, indent_str
|
|
|
|
, body()->to_string()
|
|
|
|
);
|
2023-04-22 21:43:31 -07:00
|
|
|
}
|
2023-04-09 10:59:39 -07:00
|
|
|
break;
|
|
|
|
|
2023-04-10 18:33:06 -07:00
|
|
|
case Operator_Fwd:
|
2023-04-22 21:43:31 -07:00
|
|
|
case Operator_Member_Fwd:
|
|
|
|
{
|
2023-05-06 12:49:43 -07:00
|
|
|
result.append( indent );
|
2023-05-05 21:18:44 -07:00
|
|
|
|
|
|
|
ProcessModuleFlags();
|
|
|
|
|
2023-05-07 12:03:24 -07:00
|
|
|
s32 idx = 0;
|
2023-04-22 21:43:31 -07:00
|
|
|
|
2023-06-28 18:20:29 -07:00
|
|
|
if ( entry( idx )->Type == Specifiers )
|
2023-04-22 21:43:31 -07:00
|
|
|
{
|
2023-06-28 18:20:29 -07:00
|
|
|
result.append_fmt( "%s", entry( idx )->to_string() );
|
2023-04-22 21:43:31 -07:00
|
|
|
idx++;
|
|
|
|
}
|
|
|
|
|
2023-06-29 19:48:47 -07:00
|
|
|
result.append_fmt( "%s %s (", entry( idx )->to_string(), Name );
|
2023-04-22 21:43:31 -07:00
|
|
|
idx++;
|
|
|
|
|
2023-06-28 18:20:29 -07:00
|
|
|
if ( entry( idx )->Type == Parameters )
|
2023-04-22 21:43:31 -07:00
|
|
|
{
|
2023-06-28 21:20:23 -07:00
|
|
|
result.append_fmt( "%s);", entry( idx )->to_string() );
|
2023-04-22 21:43:31 -07:00
|
|
|
idx++;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2023-06-28 21:20:23 -07:00
|
|
|
result.append_fmt( "void);" );
|
2023-04-22 21:43:31 -07:00
|
|
|
}
|
|
|
|
}
|
2023-04-06 16:19:11 -07:00
|
|
|
break;
|
|
|
|
|
|
|
|
case Parameters:
|
|
|
|
{
|
2023-06-28 18:20:29 -07:00
|
|
|
result.append_fmt( "%s %s", entry( 0 )->to_string(), Name );
|
2023-04-06 16:19:11 -07:00
|
|
|
|
|
|
|
s32 index = 1;
|
2023-06-30 00:13:41 -07:00
|
|
|
s32 left = num_entries() - 1;
|
2023-04-06 16:19:11 -07:00
|
|
|
|
2023-06-30 00:13:41 -07:00
|
|
|
while ( left-- )
|
|
|
|
{
|
2023-05-06 12:49:43 -07:00
|
|
|
result.append_fmt( ", %s %s"
|
2023-06-28 18:20:29 -07:00
|
|
|
, entry( index )->entry( 0 )->to_string()
|
|
|
|
, entry( index )->Name
|
2023-04-06 16:19:11 -07:00
|
|
|
);
|
2023-06-30 00:13:41 -07:00
|
|
|
index++;
|
|
|
|
}
|
2023-04-06 16:19:11 -07:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
2023-05-01 11:12:07 -07:00
|
|
|
case Preprocessor_Include:
|
2023-06-28 21:20:23 -07:00
|
|
|
result.append_fmt( "#include \"%s\"", Name );
|
2023-05-01 11:12:07 -07:00
|
|
|
break;
|
|
|
|
|
2023-04-06 16:19:11 -07:00
|
|
|
case Specifiers:
|
2023-04-22 21:43:31 -07:00
|
|
|
{
|
|
|
|
s32 idx = 0;
|
|
|
|
s32 left = StaticIndex;
|
2023-06-30 00:13:41 -07:00
|
|
|
while ( left-- )
|
|
|
|
{
|
|
|
|
result.append_fmt( "%s ", (char const*)ESpecifier::to_str( ArrSpecs[idx]) );
|
|
|
|
idx++;
|
|
|
|
}
|
2023-04-22 21:43:31 -07:00
|
|
|
}
|
2023-04-06 16:19:11 -07:00
|
|
|
break;
|
|
|
|
|
|
|
|
case Struct:
|
2023-04-22 21:43:31 -07:00
|
|
|
{
|
2023-05-06 12:49:43 -07:00
|
|
|
result.append( indent );
|
2023-05-05 21:18:44 -07:00
|
|
|
|
|
|
|
ProcessModuleFlags();
|
|
|
|
|
2023-05-06 12:49:43 -07:00
|
|
|
result.append( "struct ");
|
2023-04-22 21:43:31 -07:00
|
|
|
|
2023-06-30 00:13:41 -07:00
|
|
|
if ( num_entries() > 1 )
|
2023-04-22 21:43:31 -07:00
|
|
|
{
|
2023-06-30 00:13:41 -07:00
|
|
|
s32 idx = 1;
|
2023-04-22 21:43:31 -07:00
|
|
|
|
2023-06-30 00:13:41 -07:00
|
|
|
AST* current = entry( idx );
|
2023-05-05 21:18:44 -07:00
|
|
|
|
2023-06-30 00:13:41 -07:00
|
|
|
if ( current && current->Type == Attributes )
|
|
|
|
{
|
|
|
|
result.append_fmt( "%s ", entry( idx )->to_string() );
|
|
|
|
idx++;
|
|
|
|
current = entry( idx );
|
|
|
|
}
|
2023-04-22 21:43:31 -07:00
|
|
|
|
2023-06-30 00:13:41 -07:00
|
|
|
result.append_fmt( "%s", Name );
|
|
|
|
|
|
|
|
if ( current )
|
|
|
|
{
|
|
|
|
switch ( ParentAccess )
|
|
|
|
{
|
|
|
|
case AccessSpec::Private:
|
|
|
|
case AccessSpec::Protected:
|
|
|
|
case AccessSpec::Public:
|
|
|
|
result.append_fmt( " : %s %s", to_str(ParentAccess), current->to_string() );
|
|
|
|
idx++;
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
result.append_fmt( " : %s", current->to_string() );
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
result.append_fmt( "\n{\n%s\n};", body()->to_string() );
|
|
|
|
break;
|
2023-05-05 21:18:44 -07:00
|
|
|
}
|
2023-04-22 21:43:31 -07:00
|
|
|
|
2023-06-30 00:13:41 -07:00
|
|
|
result.append_fmt( "%s\n{\n%s\n};", Name, body()->to_string() );
|
2023-04-22 21:43:31 -07:00
|
|
|
}
|
2023-04-09 10:59:39 -07:00
|
|
|
break;
|
|
|
|
|
2023-04-10 18:33:06 -07:00
|
|
|
case Struct_Fwd:
|
2023-04-22 21:43:31 -07:00
|
|
|
{
|
2023-05-06 12:49:43 -07:00
|
|
|
result.append( indent );
|
2023-05-05 21:18:44 -07:00
|
|
|
|
|
|
|
ProcessModuleFlags();
|
|
|
|
|
2023-05-06 12:49:43 -07:00
|
|
|
result.append( "struct ");
|
2023-04-06 16:19:11 -07:00
|
|
|
|
2023-04-22 21:43:31 -07:00
|
|
|
s32 idx = 1;
|
|
|
|
|
2023-06-28 18:20:29 -07:00
|
|
|
if ( entry( idx )->Type == Specifiers )
|
2023-04-22 21:43:31 -07:00
|
|
|
{
|
2023-06-28 18:20:29 -07:00
|
|
|
result.append_fmt( "%s ", entry( idx )->to_string() );
|
2023-04-22 21:43:31 -07:00
|
|
|
idx++;
|
|
|
|
}
|
|
|
|
|
2023-05-06 12:49:43 -07:00
|
|
|
result.append_fmt( "%s;", Name );
|
2023-04-22 21:43:31 -07:00
|
|
|
}
|
2023-04-06 16:19:11 -07:00
|
|
|
break;
|
|
|
|
|
|
|
|
case Variable:
|
2023-04-22 21:43:31 -07:00
|
|
|
{
|
2023-05-06 12:49:43 -07:00
|
|
|
result.append( indent );
|
2023-05-05 21:18:44 -07:00
|
|
|
|
|
|
|
ProcessModuleFlags();
|
|
|
|
|
2023-04-22 21:43:31 -07:00
|
|
|
s32 idx = 1;
|
|
|
|
|
2023-06-28 21:20:23 -07:00
|
|
|
if ( num_entries() > 1 )
|
2023-04-22 21:43:31 -07:00
|
|
|
{
|
2023-06-28 21:20:23 -07:00
|
|
|
if ( entry( idx )->Type == Specifiers )
|
|
|
|
{
|
|
|
|
result.append_fmt( "%s", entry( idx )->to_string() );
|
|
|
|
idx++;
|
|
|
|
}
|
2023-04-22 21:43:31 -07:00
|
|
|
|
2023-06-28 21:20:23 -07:00
|
|
|
result.append_fmt( "%s %s", entry( 0 )->to_string(), Name );
|
2023-04-22 21:43:31 -07:00
|
|
|
|
2023-06-28 21:20:23 -07:00
|
|
|
if ( entry( idx ) )
|
|
|
|
result.append_fmt( " = %s;", entry( idx )->to_string() );
|
|
|
|
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
result.append_fmt( "%s %s;", entry( 0 )->to_string(), Name );
|
2023-04-22 21:43:31 -07:00
|
|
|
}
|
2023-04-06 16:19:11 -07:00
|
|
|
break;
|
|
|
|
|
|
|
|
case Typedef:
|
2023-05-07 12:03:24 -07:00
|
|
|
{
|
2023-05-06 12:49:43 -07:00
|
|
|
result.append( indent );
|
2023-05-05 21:18:44 -07:00
|
|
|
|
|
|
|
ProcessModuleFlags();
|
|
|
|
|
2023-06-28 18:20:29 -07:00
|
|
|
AST* type = entry( 0 );
|
2023-04-23 11:33:37 -07:00
|
|
|
|
2023-06-28 18:20:29 -07:00
|
|
|
if ( entry( 1 ) && entry( 1 )->Type == Attributes )
|
2023-05-05 21:18:44 -07:00
|
|
|
{
|
2023-06-28 18:20:29 -07:00
|
|
|
result.append_fmt( "%s ", entry( 1 )->to_string() );
|
2023-05-05 21:18:44 -07:00
|
|
|
}
|
|
|
|
|
2023-05-06 12:49:43 -07:00
|
|
|
result.append_fmt( "typedef %s %s", type->to_string(), Name );
|
2023-05-05 21:18:44 -07:00
|
|
|
|
2023-06-28 18:20:29 -07:00
|
|
|
if ( type->entry( 1 ) )
|
2023-05-05 21:18:44 -07:00
|
|
|
{
|
2023-06-28 18:20:29 -07:00
|
|
|
result.append_fmt( "[%s];", type->entry( 1 )->to_string() );
|
2023-05-05 21:18:44 -07:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2023-05-06 12:49:43 -07:00
|
|
|
result.append( ";" );
|
2023-05-05 21:18:44 -07:00
|
|
|
}
|
2023-05-07 12:03:24 -07:00
|
|
|
}
|
2023-04-06 16:19:11 -07:00
|
|
|
break;
|
|
|
|
|
|
|
|
case Typename:
|
2023-06-28 18:20:29 -07:00
|
|
|
if ( num_entries() && entry( 0 ) )
|
2023-05-05 21:18:44 -07:00
|
|
|
{
|
2023-06-28 18:20:29 -07:00
|
|
|
result.append_fmt( "%s %s", Name, entry( 0 )->to_string() );
|
2023-05-05 21:18:44 -07:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2023-05-06 12:49:43 -07:00
|
|
|
result.append_fmt( "%s", Name );
|
2023-05-05 21:18:44 -07:00
|
|
|
}
|
2023-04-06 16:19:11 -07:00
|
|
|
break;
|
|
|
|
|
2023-05-01 11:12:07 -07:00
|
|
|
case Union:
|
2023-05-07 12:03:24 -07:00
|
|
|
{
|
2023-05-06 12:49:43 -07:00
|
|
|
result.append( indent );
|
2023-05-05 21:18:44 -07:00
|
|
|
|
|
|
|
ProcessModuleFlags();
|
|
|
|
|
|
|
|
s32 idx = 0;
|
|
|
|
|
2023-05-06 12:49:43 -07:00
|
|
|
result.append( "union " );
|
2023-05-05 21:18:44 -07:00
|
|
|
|
2023-06-28 18:20:29 -07:00
|
|
|
if ( entry( idx )->Type == Attributes )
|
2023-05-05 21:18:44 -07:00
|
|
|
{
|
2023-06-28 18:20:29 -07:00
|
|
|
result.append_fmt( "%s ", entry( idx )->to_string() );
|
2023-05-05 21:18:44 -07:00
|
|
|
idx++;
|
|
|
|
}
|
|
|
|
|
2023-06-28 21:20:23 -07:00
|
|
|
result.append_fmt( "%s\n%s{\n%s\n%s};"
|
2023-05-05 21:18:44 -07:00
|
|
|
, Name
|
|
|
|
, indent_str
|
|
|
|
, body()->to_string()
|
|
|
|
, indent_str
|
|
|
|
);
|
2023-05-07 12:03:24 -07:00
|
|
|
}
|
2023-05-01 11:12:07 -07:00
|
|
|
break;
|
|
|
|
|
2023-04-06 16:19:11 -07:00
|
|
|
case Using:
|
2023-05-07 12:03:24 -07:00
|
|
|
{
|
2023-05-06 12:49:43 -07:00
|
|
|
result.append( indent );
|
2023-04-23 11:33:37 -07:00
|
|
|
|
2023-05-05 21:18:44 -07:00
|
|
|
ProcessModuleFlags();
|
|
|
|
|
2023-06-28 18:20:29 -07:00
|
|
|
AST* type = entry( 0 );
|
2023-05-05 21:18:44 -07:00
|
|
|
|
2023-06-28 18:20:29 -07:00
|
|
|
if ( entry( 1 ) && entry( 1 )->Type == Attributes )
|
2023-05-05 21:18:44 -07:00
|
|
|
{
|
2023-06-28 18:20:29 -07:00
|
|
|
result.append_fmt( "%s ", entry( 1 )->to_string() );
|
2023-05-05 21:18:44 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
if ( type )
|
|
|
|
{
|
2023-05-06 12:49:43 -07:00
|
|
|
result.append_fmt( "using %s = %s", Name, type->to_string() );
|
2023-05-05 21:18:44 -07:00
|
|
|
|
2023-06-28 18:20:29 -07:00
|
|
|
if ( type->entry( 1 ) )
|
|
|
|
result.append_fmt( "[%s]", type->entry( 1 )->to_string() );
|
2023-04-22 21:43:31 -07:00
|
|
|
|
2023-06-28 21:20:23 -07:00
|
|
|
result.append( ";" );
|
2023-05-05 21:18:44 -07:00
|
|
|
}
|
2023-04-22 21:43:31 -07:00
|
|
|
else
|
2023-06-28 21:20:23 -07:00
|
|
|
result.append_fmt( "using %s;", Name );
|
2023-05-07 12:03:24 -07:00
|
|
|
}
|
2023-04-22 21:43:31 -07:00
|
|
|
break;
|
|
|
|
|
|
|
|
case Using_Namespace:
|
2023-06-28 21:20:23 -07:00
|
|
|
result.append_fmt( "%susing namespace %s;", indent_str, Name );
|
2023-04-22 21:43:31 -07:00
|
|
|
break;
|
|
|
|
|
|
|
|
case Class_Body:
|
|
|
|
case Enum_Body:
|
2023-04-23 16:04:43 -07:00
|
|
|
case Extern_Linkage_Body:
|
2023-04-22 21:43:31 -07:00
|
|
|
case Function_Body:
|
|
|
|
case Global_Body:
|
|
|
|
case Namespace_Body:
|
|
|
|
case Struct_Body:
|
2023-05-01 11:12:07 -07:00
|
|
|
case Union_Body:
|
2023-04-22 21:43:31 -07:00
|
|
|
{
|
|
|
|
s32 index = 0;
|
|
|
|
s32 left = num_entries();
|
|
|
|
while ( left -- )
|
|
|
|
{
|
2023-06-28 18:20:29 -07:00
|
|
|
result.append_fmt( "%s%s\n", indent_str, entry( index )->to_string() );
|
2023-04-22 21:43:31 -07:00
|
|
|
index++;
|
|
|
|
}
|
|
|
|
}
|
2023-04-06 16:19:11 -07:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
return result;
|
2023-05-05 21:18:44 -07:00
|
|
|
|
|
|
|
#undef ProcessModuleFlags
|
2023-04-06 16:19:11 -07:00
|
|
|
}
|
2023-06-29 19:48:47 -07:00
|
|
|
|
|
|
|
bool AST::is_equal( AST* other )
|
|
|
|
{
|
|
|
|
if ( Type != other->Type )
|
|
|
|
return false;
|
|
|
|
|
|
|
|
switch ( Type )
|
|
|
|
{
|
|
|
|
case ECode::Typedef:
|
|
|
|
case ECode::Typename:
|
|
|
|
{
|
|
|
|
if ( Name != other->Name )
|
|
|
|
return false;
|
|
|
|
|
|
|
|
if ( num_entries() != other->num_entries() )
|
|
|
|
return false;
|
|
|
|
|
|
|
|
for ( s32 i = 0; i < num_entries(); ++i )
|
|
|
|
{
|
|
|
|
if ( entry( i ) != other->entry( i ) )
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if ( Name != other->Name )
|
|
|
|
return false;
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
2023-04-22 19:24:55 -07:00
|
|
|
#pragma endregion AST
|
2023-04-06 16:19:11 -07:00
|
|
|
|
|
|
|
#pragma region Gen Interface
|
2023-04-03 00:55:28 -07:00
|
|
|
void init()
|
2023-04-01 22:07:44 -07:00
|
|
|
{
|
2023-04-09 21:38:47 -07:00
|
|
|
using namespace StaticData;
|
|
|
|
|
|
|
|
// Setup the arrays
|
|
|
|
{
|
|
|
|
if (! array_init_reserve( CodePools, Allocator_DataArrays, InitSize_DataArrays ) )
|
|
|
|
fatal( "gen::init: Failed to initialize the CodePools array" );
|
|
|
|
|
|
|
|
if ( ! array_init_reserve( CodeEntriesArenas, Allocator_DataArrays, InitSize_DataArrays ) )
|
|
|
|
fatal( "gen::init: Failed to initialize the CodeEntriesPools array" );
|
|
|
|
|
|
|
|
if ( ! array_init_reserve( StringArenas, Allocator_DataArrays, InitSize_DataArrays ) )
|
|
|
|
fatal( "gen::init: Failed to initialize the StringArenas array" );
|
|
|
|
}
|
|
|
|
|
|
|
|
// Setup the code pool and code entries arena.
|
|
|
|
{
|
|
|
|
Pool code_pool;
|
|
|
|
pool_init( & code_pool, Allocator_CodePool, CodePool_NumBlocks, sizeof(AST) );
|
|
|
|
|
|
|
|
if ( code_pool.physical_start == nullptr )
|
|
|
|
fatal( "gen::init: Failed to initialize the code pool" );
|
|
|
|
|
|
|
|
array_append( CodePools, code_pool );
|
|
|
|
|
|
|
|
Arena code_entires_arena;
|
|
|
|
arena_init_from_allocator( & code_entires_arena, Allocator_CodeEntriesArena, SizePer_CodeEntriresArena );
|
|
|
|
|
|
|
|
if ( code_entires_arena.physical_start == nullptr )
|
|
|
|
fatal( "gen::init: Failed to initialize the code entries arena" );
|
|
|
|
|
|
|
|
array_append( CodeEntriesArenas, code_entires_arena );
|
2023-04-05 23:21:23 -07:00
|
|
|
|
2023-04-09 21:38:47 -07:00
|
|
|
Arena string_arena;
|
|
|
|
arena_init_from_allocator( & string_arena, Allocator_StringArena, SizePer_StringArena );
|
2023-04-03 23:04:19 -07:00
|
|
|
|
2023-04-09 21:38:47 -07:00
|
|
|
if ( string_arena.physical_start == nullptr )
|
|
|
|
fatal( "gen::init: Failed to initialize the string arena" );
|
|
|
|
|
|
|
|
array_append( StringArenas, string_arena );
|
|
|
|
}
|
|
|
|
|
|
|
|
// Setup the hash tables
|
|
|
|
{
|
|
|
|
str_tbl_init ( & StringMap, Allocator_StringTable );
|
|
|
|
if ( StringMap.entries == nullptr )
|
|
|
|
fatal( "gen::init: Failed to initialize the StringMap");
|
|
|
|
|
2023-05-09 18:54:54 -07:00
|
|
|
//type_tbl_init( & TypeMap, Allocator_TypeTable );
|
|
|
|
//if ( TypeMap.entries == nullptr )
|
|
|
|
// fatal( "gen::init: Failed to initialize the TypeMap" );
|
2023-04-09 21:38:47 -07:00
|
|
|
}
|
2023-04-05 23:21:23 -07:00
|
|
|
|
2023-04-10 18:33:06 -07:00
|
|
|
Code::Invalid = make_code();
|
|
|
|
Code::Invalid.lock();
|
2023-04-03 23:04:19 -07:00
|
|
|
|
2023-06-30 08:54:27 -07:00
|
|
|
type_ns(auto) = def_type( name(auto) );
|
|
|
|
|
2023-04-05 23:21:23 -07:00
|
|
|
Code&
|
2023-05-07 12:03:24 -07:00
|
|
|
t_void_write = ccast( Code, t_void );
|
|
|
|
t_void_write = def_type( name(void) );
|
2023-04-03 23:04:19 -07:00
|
|
|
|
2023-05-08 17:54:24 -07:00
|
|
|
# define def_constant_code_type( Type_ ) \
|
|
|
|
Code& \
|
|
|
|
t_##Type_##_write = ccast( Code, type_ns(Type_) ); \
|
|
|
|
t_##Type_##_write = def_type( name(Type_) ); \
|
2023-04-10 18:33:06 -07:00
|
|
|
|
|
|
|
def_constant_code_type( int );
|
2023-04-03 23:04:19 -07:00
|
|
|
def_constant_code_type( bool );
|
|
|
|
def_constant_code_type( char );
|
|
|
|
def_constant_code_type( wchar_t );
|
|
|
|
|
2023-05-09 18:54:54 -07:00
|
|
|
#ifdef GEN_DEFINE_LIBRARY_CODE_CONSTANTS
|
2023-04-03 23:04:19 -07:00
|
|
|
def_constant_code_type( s8 );
|
|
|
|
def_constant_code_type( s16 );
|
|
|
|
def_constant_code_type( s32 );
|
|
|
|
def_constant_code_type( s64 );
|
|
|
|
|
|
|
|
def_constant_code_type( u8 );
|
|
|
|
def_constant_code_type( u16 );
|
|
|
|
def_constant_code_type( u32 );
|
|
|
|
def_constant_code_type( u64 );
|
|
|
|
|
|
|
|
def_constant_code_type( sw );
|
|
|
|
def_constant_code_type( uw );
|
|
|
|
|
|
|
|
def_constant_code_type( f32 );
|
|
|
|
def_constant_code_type( f64 );
|
2023-04-22 19:24:55 -07:00
|
|
|
#endif
|
2023-05-09 18:54:54 -07:00
|
|
|
# undef def_constant_code_type
|
|
|
|
|
|
|
|
Code&
|
|
|
|
access_private_write = ccast( Code, access_private );
|
|
|
|
access_private_write = make_code();
|
|
|
|
access_private_write->Type = ECode::Access_Private;
|
2023-06-28 22:37:42 -07:00
|
|
|
access_private_write->Name = get_cached_string( StrC::from("private") );
|
2023-05-09 18:54:54 -07:00
|
|
|
access_private_write.lock();
|
|
|
|
|
|
|
|
Code&
|
|
|
|
access_protected_write = ccast( Code, access_protected );
|
|
|
|
access_protected_write = make_code();
|
|
|
|
access_protected_write->Type = ECode::Access_Protected;
|
2023-06-28 22:37:42 -07:00
|
|
|
access_protected_write->Name = get_cached_string( StrC::from("protected") );
|
2023-05-09 18:54:54 -07:00
|
|
|
access_protected_write.lock();
|
|
|
|
|
|
|
|
Code&
|
|
|
|
access_public_write = ccast( Code, access_public );
|
|
|
|
access_public_write = make_code();
|
|
|
|
access_public_write->Type = ECode::Access_Public;
|
2023-06-28 22:37:42 -07:00
|
|
|
access_public_write->Name = get_cached_string( StrC::from("public") );
|
2023-05-09 18:54:54 -07:00
|
|
|
access_public_write.lock();
|
2023-04-05 23:21:23 -07:00
|
|
|
|
2023-06-28 21:20:23 -07:00
|
|
|
module_global_fragment = make_code();
|
|
|
|
module_global_fragment->Type = ECode::Untyped;
|
|
|
|
module_global_fragment->Name = get_cached_string( StrC::from("module;") );
|
|
|
|
module_global_fragment->Content = module_global_fragment->Name;
|
|
|
|
module_global_fragment.lock();
|
|
|
|
|
|
|
|
module_private_fragment = make_code();
|
|
|
|
module_private_fragment->Type = ECode::Untyped;
|
|
|
|
module_private_fragment->Name = get_cached_string( StrC::from("module : private;") );
|
|
|
|
module_private_fragment->Content = module_private_fragment->Name;
|
|
|
|
module_private_fragment.lock();
|
|
|
|
|
2023-06-28 22:37:42 -07:00
|
|
|
pragma_once = make_code();
|
|
|
|
pragma_once->Type = ECode::Untyped;
|
|
|
|
pragma_once->Name = get_cached_string( StrC::from("#pragma once") );
|
|
|
|
pragma_once->Content = pragma_once->Name;
|
|
|
|
pragma_once.lock();
|
|
|
|
|
2023-04-03 23:04:19 -07:00
|
|
|
Code&
|
2023-05-09 18:54:54 -07:00
|
|
|
spec_local_persist_write = ccast( Code, spec_local_persist );
|
|
|
|
spec_local_persist_write = def_specifiers( 1, ESpecifier::Local_Persist );
|
2023-04-03 23:04:19 -07:00
|
|
|
|
2023-05-08 17:54:24 -07:00
|
|
|
# define def_constant_spec( Type_, ... ) \
|
|
|
|
Code& \
|
|
|
|
spec_##Type_##_write = ccast( Code, spec_##Type_); \
|
|
|
|
spec_##Type_##_write = def_specifiers( macro_num_args(__VA_ARGS__), __VA_ARGS__); \
|
2023-04-03 23:04:19 -07:00
|
|
|
|
2023-05-09 18:54:54 -07:00
|
|
|
def_constant_spec( consteval, ESpecifier::Consteval );
|
|
|
|
def_constant_spec( constexpr, ESpecifier::Constexpr );
|
|
|
|
def_constant_spec( constinit, ESpecifier::Constinit );
|
|
|
|
def_constant_spec( extern_linkage, ESpecifier::External_Linkage );
|
|
|
|
def_constant_spec( const, ESpecifier::Const );
|
|
|
|
def_constant_spec( inline, ESpecifier::Inline );
|
|
|
|
def_constant_spec( internal_linkage, ESpecifier::Internal_Linkage );
|
|
|
|
def_constant_spec( mutable, ESpecifier::Mutable );
|
|
|
|
def_constant_spec( ptr, ESpecifier::Ptr );
|
|
|
|
def_constant_spec( ref, ESpecifier::Ref );
|
|
|
|
def_constant_spec( register, ESpecifier::Register );
|
|
|
|
def_constant_spec( rvalue, ESpecifier::RValue );
|
|
|
|
def_constant_spec( static_member, ESpecifier::Static_Member );
|
|
|
|
def_constant_spec( thread_local, ESpecifier::Thread_Local );
|
|
|
|
def_constant_spec( volatile, ESpecifier::Volatile)
|
|
|
|
def_constant_spec( type_signed, ESpecifier::Type_Signed );
|
|
|
|
def_constant_spec( type_unsigned, ESpecifier::Type_Unsigned );
|
|
|
|
def_constant_spec( type_short, ESpecifier::Type_Short );
|
|
|
|
def_constant_spec( type_long, ESpecifier::Type_Long );
|
2023-04-22 19:24:55 -07:00
|
|
|
# undef def_constant_spec
|
2023-04-01 22:07:44 -07:00
|
|
|
}
|
|
|
|
|
2023-05-08 17:54:24 -07:00
|
|
|
void deinit()
|
|
|
|
{
|
|
|
|
using namespace StaticData;
|
|
|
|
|
2023-05-09 18:54:54 -07:00
|
|
|
s32 index = 0;
|
|
|
|
s32 left = array_count( CodePools );
|
2023-05-08 17:54:24 -07:00
|
|
|
do
|
|
|
|
{
|
2023-05-09 18:54:54 -07:00
|
|
|
Pool* code_pool = & CodePools[index];
|
2023-05-08 17:54:24 -07:00
|
|
|
pool_free( code_pool );
|
2023-05-09 18:54:54 -07:00
|
|
|
index++;
|
2023-05-08 17:54:24 -07:00
|
|
|
}
|
|
|
|
while ( left--, left );
|
|
|
|
|
2023-05-09 18:54:54 -07:00
|
|
|
index = 0;
|
|
|
|
left = array_count( CodeEntriesArenas );
|
2023-05-08 17:54:24 -07:00
|
|
|
do
|
|
|
|
{
|
2023-05-09 18:54:54 -07:00
|
|
|
Arena* code_entries_arena = & CodeEntriesArenas[index];
|
2023-05-08 17:54:24 -07:00
|
|
|
arena_free( code_entries_arena );
|
2023-05-09 18:54:54 -07:00
|
|
|
index++;
|
2023-05-08 17:54:24 -07:00
|
|
|
}
|
|
|
|
while ( left--, left );
|
|
|
|
|
2023-05-09 18:54:54 -07:00
|
|
|
index = 0;
|
|
|
|
left = array_count( StringArenas );
|
2023-05-08 17:54:24 -07:00
|
|
|
do
|
|
|
|
{
|
2023-05-09 18:54:54 -07:00
|
|
|
Arena* string_arena = & StringArenas[index];
|
2023-05-08 17:54:24 -07:00
|
|
|
arena_free( string_arena );
|
2023-05-09 18:54:54 -07:00
|
|
|
index++;
|
2023-05-08 17:54:24 -07:00
|
|
|
}
|
|
|
|
while ( left--, left );
|
|
|
|
|
|
|
|
str_tbl_destroy( & StringMap );
|
2023-05-09 18:54:54 -07:00
|
|
|
// type_tbl_destroy( & TypeMap );
|
2023-05-08 17:54:24 -07:00
|
|
|
|
|
|
|
array_free( CodePools );
|
|
|
|
array_free( CodeEntriesArenas );
|
2023-05-09 18:54:54 -07:00
|
|
|
array_free( StringArenas );
|
2023-05-08 17:54:24 -07:00
|
|
|
}
|
|
|
|
|
2023-04-10 18:33:06 -07:00
|
|
|
void clear_code_memory()
|
2023-04-01 19:21:46 -07:00
|
|
|
{
|
2023-04-09 21:38:47 -07:00
|
|
|
using namespace StaticData;
|
|
|
|
|
2023-04-07 21:29:09 -07:00
|
|
|
// Clear the code pools
|
|
|
|
{
|
|
|
|
s32 index = 0;
|
2023-04-10 18:33:06 -07:00
|
|
|
s32 left = array_count( CodePools );
|
|
|
|
while ( left-- )
|
2023-04-07 21:29:09 -07:00
|
|
|
{
|
2023-04-09 21:38:47 -07:00
|
|
|
Pool* code_pool = & CodePools[index];
|
2023-04-07 21:29:09 -07:00
|
|
|
pool_free( code_pool );
|
2023-04-10 18:33:06 -07:00
|
|
|
index++;
|
2023-04-07 21:29:09 -07:00
|
|
|
}
|
|
|
|
|
2023-04-09 21:38:47 -07:00
|
|
|
array_clear( CodePools );
|
2023-04-07 21:29:09 -07:00
|
|
|
}
|
2023-04-03 00:55:28 -07:00
|
|
|
|
2023-04-10 18:33:06 -07:00
|
|
|
// Clear the code entries arenas
|
2023-04-07 21:29:09 -07:00
|
|
|
{
|
|
|
|
s32 index = 0;
|
2023-04-10 18:33:06 -07:00
|
|
|
s32 left = array_count( CodeEntriesArenas );
|
|
|
|
while ( left-- )
|
2023-04-07 21:29:09 -07:00
|
|
|
{
|
2023-04-09 21:38:47 -07:00
|
|
|
Arena* code_entries_arena = & CodeEntriesArenas[index];
|
|
|
|
arena_free( code_entries_arena );
|
2023-04-10 18:33:06 -07:00
|
|
|
index++;
|
2023-04-07 21:29:09 -07:00
|
|
|
}
|
2023-04-03 00:55:28 -07:00
|
|
|
|
2023-04-09 21:38:47 -07:00
|
|
|
array_clear( CodeEntriesArenas );
|
2023-04-07 21:29:09 -07:00
|
|
|
}
|
2023-04-09 21:38:47 -07:00
|
|
|
|
2023-05-09 18:54:54 -07:00
|
|
|
// type_tbl_clear( & StaticData::TypeMap );
|
2023-04-05 00:03:56 -07:00
|
|
|
}
|
|
|
|
|
2023-04-09 21:38:47 -07:00
|
|
|
inline
|
2023-04-09 10:59:39 -07:00
|
|
|
AllocatorInfo get_string_allocator( s32 str_length )
|
2023-04-05 00:03:56 -07:00
|
|
|
{
|
|
|
|
using namespace StaticData;
|
|
|
|
|
|
|
|
if ( StringArenas->total_allocated + str_length > StringArenas->total_size )
|
2023-04-03 23:04:19 -07:00
|
|
|
{
|
2023-04-09 10:59:39 -07:00
|
|
|
Arena new_arena;
|
2023-04-07 09:31:50 -07:00
|
|
|
arena_init_from_allocator( & new_arena, Allocator_StringArena, SizePer_StringArena );
|
2023-04-03 00:55:28 -07:00
|
|
|
|
2023-04-09 21:38:47 -07:00
|
|
|
if ( array_append( StringArenas, new_arena ) )
|
|
|
|
fatal( "gen::get_string_allocator: Failed to allocate a new string arena" );
|
2023-04-01 22:07:44 -07:00
|
|
|
|
2023-04-05 00:03:56 -07:00
|
|
|
return arena_allocator( StringArenas );
|
|
|
|
}
|
2023-04-01 19:21:46 -07:00
|
|
|
|
2023-04-05 00:03:56 -07:00
|
|
|
return arena_allocator( StringArenas );
|
2023-04-01 19:21:46 -07:00
|
|
|
}
|
|
|
|
|
2023-04-05 00:03:56 -07:00
|
|
|
// Will either make or retrive a code string.
|
2023-05-06 12:49:43 -07:00
|
|
|
StringCached get_cached_string( StrC str )
|
2023-04-01 19:21:46 -07:00
|
|
|
{
|
2023-05-08 17:54:24 -07:00
|
|
|
s32 hash_length = str.Len > kilobytes(1) ? kilobytes(1) : str.Len;
|
|
|
|
u32 key = crc32( str.Ptr, hash_length );
|
|
|
|
{
|
|
|
|
String* result = str_tbl_get( & StaticData::StringMap, key );
|
2023-04-03 00:55:28 -07:00
|
|
|
|
2023-05-08 17:54:24 -07:00
|
|
|
if ( result )
|
|
|
|
return * result;
|
|
|
|
}
|
2023-04-03 00:55:28 -07:00
|
|
|
|
2023-05-08 17:54:24 -07:00
|
|
|
String result = String::make( get_string_allocator( str.Len ), str.Ptr );
|
2023-04-07 09:31:50 -07:00
|
|
|
|
2023-05-08 17:54:24 -07:00
|
|
|
str_tbl_set( & StaticData::StringMap, key, result );
|
2023-04-01 19:21:46 -07:00
|
|
|
|
2023-05-08 17:54:24 -07:00
|
|
|
return result;
|
2023-04-05 00:03:56 -07:00
|
|
|
}
|
2023-04-01 19:21:46 -07:00
|
|
|
|
2023-04-07 09:31:50 -07:00
|
|
|
/*
|
|
|
|
Used internally to retireve a Code object form the CodePool.
|
|
|
|
*/
|
|
|
|
Code make_code()
|
|
|
|
{
|
|
|
|
using namespace StaticData;
|
|
|
|
|
2023-04-09 21:38:47 -07:00
|
|
|
AllocatorInfo allocator = { nullptr, nullptr };
|
|
|
|
|
2023-05-09 18:54:54 -07:00
|
|
|
s32 index = 0;
|
|
|
|
s32 left = array_count( CodePools );
|
2023-04-09 21:38:47 -07:00
|
|
|
do
|
|
|
|
{
|
2023-05-09 18:54:54 -07:00
|
|
|
if ( CodePools[index].free_list != nullptr )
|
|
|
|
{
|
|
|
|
allocator = zpl::pool_allocator( & CodePools[index] );
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
index++;
|
2023-04-09 21:38:47 -07:00
|
|
|
}
|
|
|
|
while ( left--, left );
|
|
|
|
|
|
|
|
if ( allocator.data == nullptr )
|
|
|
|
{
|
|
|
|
Pool code_pool;
|
|
|
|
pool_init( & code_pool, Allocator_CodePool, CodePool_NumBlocks, sizeof(AST) );
|
|
|
|
|
|
|
|
if ( code_pool.physical_start == nullptr )
|
|
|
|
fatal( "gen::make_code: Failed to allocate a new code pool - CodePool allcoator returned nullptr." );
|
2023-04-07 21:29:09 -07:00
|
|
|
|
2023-04-09 21:38:47 -07:00
|
|
|
if ( ! array_append( CodePools, code_pool ) )
|
|
|
|
fatal( "gen::make_code: Failed to allocate a new code pool - CodePools failed to append new pool." );
|
2023-04-07 21:29:09 -07:00
|
|
|
|
2023-04-09 21:38:47 -07:00
|
|
|
allocator = pool_allocator( CodePools );
|
|
|
|
}
|
2023-04-07 09:31:50 -07:00
|
|
|
|
2023-04-09 21:38:47 -07:00
|
|
|
Code result { rcast( AST*, alloc( allocator, sizeof(AST) )) };
|
2023-04-07 09:31:50 -07:00
|
|
|
|
2023-05-06 12:49:43 -07:00
|
|
|
result->Content = { nullptr };
|
2023-04-10 18:33:06 -07:00
|
|
|
result->Parent = nullptr;
|
2023-05-06 12:49:43 -07:00
|
|
|
result->Name = { nullptr };
|
2023-04-10 18:33:06 -07:00
|
|
|
result->Type = ECode::Invalid;
|
|
|
|
result->Op = EOperator::Invalid;
|
2023-05-05 21:18:44 -07:00
|
|
|
result->ModuleFlags = ModuleFlag::Invalid;
|
|
|
|
result->ParentAccess = AccessSpec::Invalid;
|
|
|
|
result->StaticIndex = 0;
|
2023-04-10 18:33:06 -07:00
|
|
|
result->Readonly = false;
|
|
|
|
result->DynamicEntries = false;
|
2023-04-07 09:31:50 -07:00
|
|
|
|
2023-04-09 21:38:47 -07:00
|
|
|
return result;
|
2023-04-07 09:31:50 -07:00
|
|
|
}
|
|
|
|
|
2023-04-09 10:59:39 -07:00
|
|
|
Array(AST*) make_code_entries()
|
2023-04-07 21:29:09 -07:00
|
|
|
{
|
2023-04-09 21:38:47 -07:00
|
|
|
using namespace StaticData;
|
2023-04-07 21:29:09 -07:00
|
|
|
|
2023-04-09 21:38:47 -07:00
|
|
|
AllocatorInfo allocator = { nullptr, nullptr };
|
2023-04-07 21:29:09 -07:00
|
|
|
|
2023-05-09 18:54:54 -07:00
|
|
|
s32 index = 0;
|
|
|
|
s32 left = array_count( CodeEntriesArenas );
|
2023-04-09 21:38:47 -07:00
|
|
|
do
|
|
|
|
{
|
2023-05-09 18:54:54 -07:00
|
|
|
if ( arena_size_remaining( & CodeEntriesArenas[index], ZPL_DEFAULT_MEMORY_ALIGNMENT) >= InitSize_CodeEntiresArray )
|
|
|
|
allocator = arena_allocator( & CodeEntriesArenas[index] );
|
|
|
|
index++;
|
2023-04-09 21:38:47 -07:00
|
|
|
}
|
|
|
|
while( left--, left );
|
2023-04-07 21:29:09 -07:00
|
|
|
|
2023-04-09 21:38:47 -07:00
|
|
|
if ( allocator.data == nullptr )
|
|
|
|
{
|
|
|
|
Arena arena;
|
|
|
|
arena_init_from_allocator( & arena, Allocator_CodeEntriesArena, SizePer_CodeEntriresArena );
|
2023-04-07 21:29:09 -07:00
|
|
|
|
2023-04-09 21:38:47 -07:00
|
|
|
if ( arena.physical_start == nullptr )
|
|
|
|
fatal( "gen::make_code: Failed to allocate a new code entries arena - CodeEntriesArena allcoator returned nullptr." );
|
2023-04-01 19:21:46 -07:00
|
|
|
|
2023-05-09 18:54:54 -07:00
|
|
|
allocator = arena_allocator( & arena );
|
|
|
|
array_append( CodeEntriesArenas, arena );
|
2023-04-09 21:38:47 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
Array(AST*) entry_array;
|
|
|
|
array_init( entry_array, allocator );
|
|
|
|
|
|
|
|
return entry_array;
|
2023-04-05 00:03:56 -07:00
|
|
|
}
|
2023-04-01 19:21:46 -07:00
|
|
|
|
2023-04-11 19:18:02 -07:00
|
|
|
enum class OpValidateResult : u32
|
2023-04-05 00:03:56 -07:00
|
|
|
{
|
2023-04-11 19:18:02 -07:00
|
|
|
Fail,
|
|
|
|
Global,
|
|
|
|
Member
|
|
|
|
};
|
2023-04-05 00:03:56 -07:00
|
|
|
|
2023-04-11 19:18:02 -07:00
|
|
|
inline
|
2023-04-11 22:24:43 -07:00
|
|
|
OpValidateResult operator__validate( OperatorT op, Code params_code, Code ret_type, Code specifier )
|
2023-04-05 00:03:56 -07:00
|
|
|
{
|
2023-04-11 19:18:02 -07:00
|
|
|
using namespace EOperator;
|
2023-04-09 21:38:47 -07:00
|
|
|
|
2023-04-23 11:33:37 -07:00
|
|
|
if ( op == EOperator::Invalid )
|
2023-04-11 19:18:02 -07:00
|
|
|
{
|
|
|
|
log_failure("gen::def_operator: op cannot be invalid");
|
|
|
|
return OpValidateResult::Fail;
|
|
|
|
}
|
2023-04-09 10:59:39 -07:00
|
|
|
|
2023-04-22 19:24:55 -07:00
|
|
|
#pragma region Helper Macros
|
|
|
|
# define check_params() \
|
2023-04-11 19:18:02 -07:00
|
|
|
if ( ! params_code ) \
|
|
|
|
{ \
|
|
|
|
log_failure("gen::def_operator: params is null and operator%s requires it", to_str(op)); \
|
|
|
|
return OpValidateResult::Fail; \
|
|
|
|
} \
|
|
|
|
if ( params_code->Type != ECode::Parameters ) \
|
|
|
|
{ \
|
|
|
|
log_failure("gen::def_operator: params is not of Parameters type - %s", params_code->debug_str()); \
|
|
|
|
return OpValidateResult::Fail; \
|
|
|
|
}
|
2023-04-06 16:19:11 -07:00
|
|
|
|
2023-04-22 19:24:55 -07:00
|
|
|
# define check_param_eq_ret() \
|
2023-04-11 19:18:02 -07:00
|
|
|
if ( ! is_member_symbol && params_code->param_type() != ret_type ) \
|
|
|
|
{ \
|
|
|
|
log_failure("gen_def_operator: operator%s requires first parameter to equal return type\n" \
|
|
|
|
"param types: %s\n" \
|
|
|
|
"return type: %s", \
|
|
|
|
to_str(op), \
|
|
|
|
params_code->debug_str(), \
|
|
|
|
ret_type->debug_str() \
|
|
|
|
); \
|
|
|
|
return OpValidateResult::Fail; \
|
|
|
|
}
|
2023-04-22 19:24:55 -07:00
|
|
|
#pragma endregion Helper Macros
|
2023-04-06 16:19:11 -07:00
|
|
|
|
2023-04-11 19:18:02 -07:00
|
|
|
if ( ! ret_type )
|
2023-04-06 16:19:11 -07:00
|
|
|
{
|
2023-04-11 19:18:02 -07:00
|
|
|
log_failure("gen::def_operator: ret_type is null but is required by operator%s", to_str(op));
|
2023-04-06 16:19:11 -07:00
|
|
|
}
|
|
|
|
|
2023-04-11 19:18:02 -07:00
|
|
|
if ( ret_type->Type != ECode::Typename )
|
2023-04-06 16:19:11 -07:00
|
|
|
{
|
2023-04-11 19:18:02 -07:00
|
|
|
log_failure("gen::def_operator: ret_type is not of typename type - %s", ret_type->debug_str());
|
|
|
|
return OpValidateResult::Fail;
|
2023-04-06 16:19:11 -07:00
|
|
|
}
|
|
|
|
|
2023-04-11 19:18:02 -07:00
|
|
|
bool is_member_symbol = false;
|
2023-04-06 16:19:11 -07:00
|
|
|
|
2023-04-11 19:18:02 -07:00
|
|
|
switch ( op )
|
2023-04-06 16:19:11 -07:00
|
|
|
{
|
2023-04-22 19:24:55 -07:00
|
|
|
# define specs( ... ) macro_num_args( __VA_ARGS__ ), __VA_ARGS__
|
2023-04-11 19:18:02 -07:00
|
|
|
case Assign:
|
|
|
|
check_params();
|
2023-04-07 09:31:50 -07:00
|
|
|
|
2023-04-11 19:18:02 -07:00
|
|
|
if ( params_code->param_count() > 1 )
|
|
|
|
{
|
|
|
|
log_failure("gen::def_operator: "
|
|
|
|
"operator%s does not support non-member definition (more than one parameter provided) - %s",
|
|
|
|
to_str(op),
|
|
|
|
params_code->debug_str()
|
|
|
|
);
|
|
|
|
return OpValidateResult::Fail;
|
|
|
|
}
|
2023-04-07 09:31:50 -07:00
|
|
|
|
2023-04-11 19:18:02 -07:00
|
|
|
is_member_symbol = true;
|
|
|
|
break;
|
2023-04-06 16:19:11 -07:00
|
|
|
|
2023-04-11 19:18:02 -07:00
|
|
|
case Assign_Add:
|
|
|
|
case Assign_Subtract:
|
|
|
|
case Assgin_Multiply:
|
|
|
|
case Assgin_Divide:
|
|
|
|
case Assgin_Modulo:
|
|
|
|
case Assgin_BAnd:
|
|
|
|
case Assgin_BOr:
|
|
|
|
case Assign_BXOr:
|
|
|
|
case Assign_LShift:
|
|
|
|
case Assign_RShift:
|
|
|
|
check_params();
|
2023-04-06 16:19:11 -07:00
|
|
|
|
2023-04-11 19:18:02 -07:00
|
|
|
if ( params_code->param_count() == 1 )
|
|
|
|
is_member_symbol = true;
|
2023-04-06 16:19:11 -07:00
|
|
|
|
2023-04-11 19:18:02 -07:00
|
|
|
else
|
|
|
|
check_param_eq_ret();
|
2023-04-06 16:19:11 -07:00
|
|
|
|
2023-04-11 19:18:02 -07:00
|
|
|
if (params_code->param_count() > 2 )
|
|
|
|
{
|
|
|
|
log_failure("gen::def_operator: operator%s may not be defined with more than two parametes - param count; %d\n%s"
|
|
|
|
, to_str(op)
|
|
|
|
, params_code->param_count()
|
|
|
|
, params_code->debug_str()
|
|
|
|
);
|
|
|
|
return OpValidateResult::Fail;
|
|
|
|
}
|
|
|
|
break;
|
2023-04-07 09:31:50 -07:00
|
|
|
|
2023-04-11 19:18:02 -07:00
|
|
|
case Increment:
|
|
|
|
case Decrement:
|
|
|
|
// If its not set, it just means its a prefix member op.
|
|
|
|
if ( params_code )
|
|
|
|
{
|
|
|
|
if ( params_code->Type != ECode::Parameters )
|
|
|
|
{
|
|
|
|
log_failure("gen::def_operator: operator%s params code provided is not of Parameters type - %s"
|
|
|
|
, to_str(op)
|
|
|
|
, params_code->debug_str()
|
|
|
|
);
|
|
|
|
return OpValidateResult::Fail;
|
|
|
|
}
|
2023-04-07 09:31:50 -07:00
|
|
|
|
2023-04-11 19:18:02 -07:00
|
|
|
switch ( params_code->param_count() )
|
|
|
|
{
|
|
|
|
case 1:
|
2023-06-29 19:48:47 -07:00
|
|
|
if ( params_code->param_type()->is_equal( type_ns(int) ) )
|
2023-04-11 19:18:02 -07:00
|
|
|
is_member_symbol = true;
|
2023-04-06 16:19:11 -07:00
|
|
|
|
2023-04-11 19:18:02 -07:00
|
|
|
else
|
|
|
|
check_param_eq_ret();
|
|
|
|
break;
|
2023-04-07 09:31:50 -07:00
|
|
|
|
2023-04-11 19:18:02 -07:00
|
|
|
case 2:
|
|
|
|
check_param_eq_ret();
|
2023-04-10 18:33:06 -07:00
|
|
|
|
2023-06-29 19:48:47 -07:00
|
|
|
if ( ! params_code->get_param(1)->is_equal( type_ns(int) ) )
|
2023-04-11 19:18:02 -07:00
|
|
|
{
|
|
|
|
log_failure("gen::def_operator: "
|
|
|
|
"operator%s requires second parameter of non-member definition to be int for post-decrement",
|
|
|
|
to_str(op)
|
|
|
|
);
|
|
|
|
return OpValidateResult::Fail;
|
|
|
|
}
|
|
|
|
break;
|
2023-04-07 09:31:50 -07:00
|
|
|
|
2023-04-11 19:18:02 -07:00
|
|
|
default:
|
|
|
|
log_failure("gen::def_operator: operator%s recieved unexpected number of paramters recived %d instead of 0-2"
|
|
|
|
, to_str(op)
|
|
|
|
, params_code->param_count()
|
|
|
|
);
|
|
|
|
return OpValidateResult::Fail;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
break;
|
2023-04-07 09:31:50 -07:00
|
|
|
|
2023-04-11 19:18:02 -07:00
|
|
|
case Unary_Plus:
|
|
|
|
case Unary_Minus:
|
|
|
|
case BNot:
|
|
|
|
if ( ! params_code )
|
|
|
|
is_member_symbol = true;
|
2023-04-07 21:29:09 -07:00
|
|
|
|
2023-04-11 19:18:02 -07:00
|
|
|
else
|
|
|
|
{
|
|
|
|
if ( params_code->Type != ECode::Parameters )
|
|
|
|
{
|
|
|
|
log_failure("gen::def_operator: params is not of Parameters type - %s", params_code->debug_str());
|
|
|
|
return OpValidateResult::Fail;
|
|
|
|
}
|
|
|
|
|
2023-06-29 19:48:47 -07:00
|
|
|
if ( params_code->param_type()->is_equal( ret_type ) )
|
2023-04-11 19:18:02 -07:00
|
|
|
{
|
|
|
|
log_failure("gen::def_operator: "
|
|
|
|
"operator%s is non-member symbol yet first paramter does not equal return type\n"
|
|
|
|
"param type: %s\n"
|
|
|
|
"return type: %s\n"
|
|
|
|
, params_code->debug_str()
|
|
|
|
, ret_type->debug_str()
|
|
|
|
);
|
|
|
|
return OpValidateResult::Fail;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ( params_code->param_count() > 1 )
|
|
|
|
{
|
|
|
|
log_failure("gen::def_operator: operator%s may not have more than one parameter - param count: %d"
|
|
|
|
, to_str(op)
|
|
|
|
, params_code->param_count()
|
|
|
|
);
|
|
|
|
return OpValidateResult::Fail;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case Add:
|
|
|
|
case Subtract:
|
|
|
|
case Multiply:
|
|
|
|
case Divide:
|
|
|
|
case Modulo:
|
|
|
|
case BAnd:
|
|
|
|
case BOr:
|
|
|
|
case BXOr:
|
|
|
|
case LShift:
|
|
|
|
case RShift:
|
|
|
|
check_params();
|
|
|
|
|
|
|
|
switch ( params_code->param_count() )
|
|
|
|
{
|
|
|
|
case 1:
|
|
|
|
is_member_symbol = true;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 2:
|
2023-06-29 19:48:47 -07:00
|
|
|
if ( ! params_code->param_type()->is_equal( ret_type ) )
|
2023-04-11 19:18:02 -07:00
|
|
|
{
|
|
|
|
log_failure("gen::def_operator: "
|
|
|
|
"operator%s is non-member symbol yet first paramter does not equal return type\n"
|
|
|
|
"param type: %s\n"
|
|
|
|
"return type: %s\n"
|
|
|
|
, params_code->debug_str()
|
|
|
|
, ret_type->debug_str()
|
|
|
|
);
|
|
|
|
return OpValidateResult::Fail;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
log_failure("gen::def_operator: operator%s recieved unexpected number of paramters recived %d instead of 0-2"
|
|
|
|
, to_str(op)
|
|
|
|
, params_code->param_count()
|
|
|
|
);
|
|
|
|
return OpValidateResult::Fail;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case LNot:
|
|
|
|
if ( ! params_code )
|
|
|
|
is_member_symbol = true;
|
|
|
|
|
|
|
|
else
|
|
|
|
{
|
|
|
|
if ( params_code->Type != ECode::Parameters )
|
|
|
|
{
|
|
|
|
log_failure("gen::def_operator: params is not of Parameters type - %s", params_code->debug_str());
|
|
|
|
return OpValidateResult::Fail;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ( params_code->param_count() != 1 )
|
|
|
|
{
|
|
|
|
log_failure("gen::def_operator: operator%s recieved unexpected number of paramters recived %d instead of 0-1"
|
|
|
|
, to_str(op)
|
|
|
|
, params_code->param_count()
|
|
|
|
);
|
|
|
|
return OpValidateResult::Fail;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-06-29 19:48:47 -07:00
|
|
|
if ( ! ret_type->is_equal( type_ns(bool) ))
|
2023-04-11 19:18:02 -07:00
|
|
|
{
|
|
|
|
log_failure("gen::def_operator: operator%s return type must be of type bool - %s"
|
|
|
|
, to_str(op)
|
|
|
|
, ret_type->debug_str()
|
|
|
|
);
|
|
|
|
return OpValidateResult::Fail;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case LAnd:
|
|
|
|
case LOr:
|
|
|
|
case Equals:
|
|
|
|
case NotEquals:
|
|
|
|
case Lesser:
|
|
|
|
case Greater:
|
|
|
|
case LesserEqual:
|
|
|
|
case GreaterEqual:
|
|
|
|
check_params();
|
|
|
|
|
|
|
|
switch ( params_code->param_count() )
|
|
|
|
{
|
|
|
|
case 1:
|
|
|
|
is_member_symbol = true;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 2:
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
log_failure("gen::def_operator: operator%s recieved unexpected number of paramters recived %d instead of 1-2"
|
|
|
|
, to_str(op)
|
|
|
|
, params_code->param_count()
|
|
|
|
);
|
|
|
|
return OpValidateResult::Fail;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case Indirection:
|
|
|
|
case AddressOf:
|
|
|
|
case MemberOfPointer:
|
|
|
|
if ( params_code && params_code->param_count() > 1)
|
|
|
|
{
|
|
|
|
log_failure("gen::def_operator: operator%s recieved unexpected number of paramters recived %d instead of 0-1"
|
|
|
|
, to_str(op)
|
|
|
|
, params_code->param_count()
|
|
|
|
);
|
|
|
|
return OpValidateResult::Fail;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
is_member_symbol = true;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case PtrToMemOfPtr:
|
|
|
|
if ( params_code )
|
|
|
|
{
|
|
|
|
log_failure("gen::def_operator: operator%s expects no paramters - %s", to_str(op), params_code->debug_str());
|
|
|
|
return OpValidateResult::Fail;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case Subscript:
|
|
|
|
case FunctionCall:
|
|
|
|
case Comma:
|
|
|
|
check_params();
|
|
|
|
break;
|
2023-04-22 19:24:55 -07:00
|
|
|
# undef specs
|
2023-04-07 09:31:50 -07:00
|
|
|
}
|
|
|
|
|
2023-04-11 19:18:02 -07:00
|
|
|
return is_member_symbol ? OpValidateResult::Member : OpValidateResult::Global;
|
2023-04-22 19:24:55 -07:00
|
|
|
# undef check_params
|
|
|
|
# undef check_ret_type
|
|
|
|
# undef check_param_eq_ret
|
2023-04-06 16:19:11 -07:00
|
|
|
}
|
|
|
|
|
2023-04-11 19:18:02 -07:00
|
|
|
void set_allocator_data_arrays( AllocatorInfo allocator )
|
2023-04-09 10:59:39 -07:00
|
|
|
{
|
2023-04-11 19:18:02 -07:00
|
|
|
StaticData::Allocator_DataArrays = allocator;
|
|
|
|
}
|
2023-04-09 10:59:39 -07:00
|
|
|
|
2023-04-11 19:18:02 -07:00
|
|
|
void set_allocator_code_pool( AllocatorInfo allocator )
|
|
|
|
{
|
|
|
|
StaticData::Allocator_CodePool = allocator;
|
|
|
|
}
|
2023-04-09 10:59:39 -07:00
|
|
|
|
2023-04-11 19:18:02 -07:00
|
|
|
void set_allocator_code_entries_arena( AllocatorInfo allocator )
|
|
|
|
{
|
|
|
|
StaticData::Allocator_CodeEntriesArena = allocator;
|
|
|
|
}
|
2023-04-09 21:38:47 -07:00
|
|
|
|
2023-04-11 19:18:02 -07:00
|
|
|
void set_allocator_string_arena( AllocatorInfo allocator )
|
|
|
|
{
|
|
|
|
StaticData::Allocator_StringArena = allocator;
|
2023-04-09 21:38:47 -07:00
|
|
|
}
|
|
|
|
|
2023-04-11 19:18:02 -07:00
|
|
|
void set_allocator_string_table( AllocatorInfo allocator )
|
2023-04-09 21:38:47 -07:00
|
|
|
{
|
2023-04-11 19:18:02 -07:00
|
|
|
StaticData::Allocator_StringArena = allocator;
|
|
|
|
}
|
2023-04-09 21:38:47 -07:00
|
|
|
|
2023-04-22 19:24:55 -07:00
|
|
|
#pragma region Helper Marcos
|
2023-04-11 19:18:02 -07:00
|
|
|
// This snippet is used in nearly all the functions.
|
2023-05-06 12:49:43 -07:00
|
|
|
# define name_check( Context_, Name_ ) \
|
|
|
|
{ \
|
|
|
|
if ( Name_.Len <= 0 ) \
|
|
|
|
{ \
|
|
|
|
log_failure( "gen::" txt(Context_) ": Invalid name length provided - %d", Name_.Len ); \
|
|
|
|
return Code::Invalid; \
|
|
|
|
} \
|
|
|
|
\
|
|
|
|
if ( Name_.Ptr == nullptr ) \
|
|
|
|
{ \
|
|
|
|
log_failure( "gen::" txt(Context_) ": name is null" ); \
|
|
|
|
return Code::Invalid; \
|
|
|
|
} \
|
2023-04-11 19:18:02 -07:00
|
|
|
}
|
2023-04-09 10:59:39 -07:00
|
|
|
|
2023-04-19 12:40:23 -07:00
|
|
|
# define null_check( Context_, Code_ ) \
|
|
|
|
if ( ! Code_ ) \
|
|
|
|
{ \
|
|
|
|
log_failure( "gen::" txt(Context_) ": " txt(Code_) " provided is null" ); \
|
|
|
|
return Code::Invalid; \
|
|
|
|
}
|
2023-04-09 10:59:39 -07:00
|
|
|
|
2023-04-11 19:18:02 -07:00
|
|
|
# define null_or_invalid_check( Context_, Code_ ) \
|
|
|
|
{ \
|
|
|
|
if ( ! Code_ ) \
|
|
|
|
{ \
|
2023-04-11 22:24:43 -07:00
|
|
|
log_failure( "gen::" txt(Context_) ": " txt(Code_) " provided is null" ); \
|
2023-04-11 19:18:02 -07:00
|
|
|
return Code::Invalid; \
|
|
|
|
} \
|
|
|
|
\
|
|
|
|
if ( Code_->is_invalid() ) \
|
|
|
|
{ \
|
2023-04-11 22:24:43 -07:00
|
|
|
log_failure("gen::" txt(Context_) ": " txt(Code_) " provided is invalid" ); \
|
2023-04-11 19:18:02 -07:00
|
|
|
return Code::Invalid; \
|
|
|
|
} \
|
|
|
|
}
|
2023-04-09 10:59:39 -07:00
|
|
|
|
2023-04-11 19:18:02 -07:00
|
|
|
# define not_implemented( Context_ ) \
|
|
|
|
log_failure( "gen::%s: This function is not implemented" ); \
|
|
|
|
return Code::Invalid;
|
2023-04-22 19:24:55 -07:00
|
|
|
#pragma endregion Helper Marcos
|
2023-04-09 21:38:47 -07:00
|
|
|
|
2023-04-22 19:24:55 -07:00
|
|
|
#pragma region Upfront Constructors
|
2023-04-11 19:18:02 -07:00
|
|
|
/*
|
|
|
|
The implementaiton of the upfront constructors involves bascially doing three things:
|
|
|
|
* Validate the arguments given to construct the intended type of AST is valid.
|
|
|
|
* Construct said AST type.
|
|
|
|
* Lock the AST (set to readonly) and return the valid object.
|
2023-04-09 10:59:39 -07:00
|
|
|
|
2023-04-11 19:18:02 -07:00
|
|
|
If any of the validation fails, it triggers a call to log_failure with as much info the give the user so that they can hopefully
|
|
|
|
identify the issue without having to debug too much (at least they can debug though...)
|
2023-04-09 10:59:39 -07:00
|
|
|
|
2023-04-11 19:18:02 -07:00
|
|
|
The largest of the functions is related to operator overload definitions.
|
|
|
|
I decided to validate a good protion of their form and thus the argument processing for is quite a bit.
|
|
|
|
*/
|
2023-06-28 22:37:42 -07:00
|
|
|
Code def_attributes( StrC content )
|
2023-05-01 11:12:07 -07:00
|
|
|
{
|
2023-05-06 12:49:43 -07:00
|
|
|
if ( content.Len <= 0 || content.Ptr == nullptr )
|
2023-05-01 11:12:07 -07:00
|
|
|
{
|
2023-06-28 22:37:42 -07:00
|
|
|
log_failure( "gen::def_attributes: Invalid attributes provided" );
|
2023-05-01 11:12:07 -07:00
|
|
|
return Code::Invalid;
|
|
|
|
}
|
|
|
|
|
|
|
|
Code
|
|
|
|
result = make_code();
|
2023-06-28 22:37:42 -07:00
|
|
|
result->Type = ECode::Attributes;
|
2023-05-06 12:49:43 -07:00
|
|
|
result->Name = get_cached_string( content );
|
2023-05-01 11:12:07 -07:00
|
|
|
result->Content = result->Name;
|
|
|
|
|
|
|
|
result.lock();
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2023-06-28 22:37:42 -07:00
|
|
|
Code def_comment( StrC content )
|
2023-05-01 11:12:07 -07:00
|
|
|
{
|
2023-05-06 12:49:43 -07:00
|
|
|
if ( content.Len <= 0 || content.Ptr == nullptr )
|
2023-05-01 11:12:07 -07:00
|
|
|
{
|
2023-06-28 22:37:42 -07:00
|
|
|
log_failure( "gen::def_comment: Invalid comment provided:" );
|
2023-05-01 11:12:07 -07:00
|
|
|
return Code::Invalid;
|
|
|
|
}
|
|
|
|
|
|
|
|
Code
|
|
|
|
result = make_code();
|
2023-06-28 22:37:42 -07:00
|
|
|
result->Type = ECode::Comment;
|
2023-05-06 12:49:43 -07:00
|
|
|
result->Name = get_cached_string( content );
|
2023-05-01 11:12:07 -07:00
|
|
|
result->Content = result->Name;
|
|
|
|
|
|
|
|
result.lock();
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2023-05-06 12:49:43 -07:00
|
|
|
Code def_class( StrC name
|
2023-05-05 15:10:03 -07:00
|
|
|
, Code body
|
|
|
|
, Code parent, AccessSpec parent_access
|
2023-05-05 21:18:44 -07:00
|
|
|
, Code attributes
|
2023-05-05 15:10:03 -07:00
|
|
|
, ModuleFlag mflags )
|
2023-04-01 19:21:46 -07:00
|
|
|
{
|
2023-04-03 00:55:28 -07:00
|
|
|
using namespace ECode;
|
|
|
|
|
2023-05-06 12:49:43 -07:00
|
|
|
name_check( def_class, name );
|
2023-04-03 00:55:28 -07:00
|
|
|
|
2023-05-05 15:10:03 -07:00
|
|
|
if ( attributes && attributes->Type != Attributes )
|
2023-04-03 23:04:19 -07:00
|
|
|
{
|
2023-05-05 15:10:03 -07:00
|
|
|
log_failure( "gen::def_class: attributes was not a 'Attributes' type: %s", attributes->debug_str() );
|
2023-04-05 23:21:23 -07:00
|
|
|
return Code::Invalid;
|
2023-04-03 23:04:19 -07:00
|
|
|
}
|
2023-04-03 00:55:28 -07:00
|
|
|
|
2023-05-07 12:03:24 -07:00
|
|
|
if ( parent && (parent->Type != Class || parent->Type != Struct || parent->Type != Typename || parent->Type != Untyped) )
|
2023-05-05 15:10:03 -07:00
|
|
|
{
|
2023-05-05 21:18:44 -07:00
|
|
|
log_failure( "gen::def_class: parent provided is not type 'Class', 'Struct', 'Typeanme', or 'Untyped': %s", parent->debug_str() );
|
2023-04-07 09:31:50 -07:00
|
|
|
return Code::Invalid;
|
2023-04-03 00:55:28 -07:00
|
|
|
}
|
|
|
|
|
2023-04-11 19:18:02 -07:00
|
|
|
Code
|
2023-05-05 15:10:03 -07:00
|
|
|
result = make_code();
|
2023-05-06 12:49:43 -07:00
|
|
|
result->Name = get_cached_string( name );
|
2023-05-05 15:10:03 -07:00
|
|
|
result->ModuleFlags = mflags;
|
2023-04-11 19:18:02 -07:00
|
|
|
|
|
|
|
if ( body )
|
2023-04-07 09:31:50 -07:00
|
|
|
{
|
2023-04-11 19:18:02 -07:00
|
|
|
switch ( body->Type )
|
|
|
|
{
|
|
|
|
case Class_Body:
|
|
|
|
case Untyped:
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
log_failure("gen::def_class: body must be either of Class_Body or Untyped type - %s", body->debug_str());
|
|
|
|
return Code::Invalid;
|
|
|
|
}
|
|
|
|
|
|
|
|
result->Type = Class;
|
|
|
|
result->add_entry( body );
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
result->Type = Class_Fwd;
|
|
|
|
}
|
|
|
|
|
2023-05-05 15:10:03 -07:00
|
|
|
if ( attributes )
|
|
|
|
result->add_entry( attributes );
|
|
|
|
|
2023-04-22 21:43:31 -07:00
|
|
|
if ( parent )
|
2023-05-05 21:18:44 -07:00
|
|
|
{
|
|
|
|
result->ParentAccess = parent_access;
|
2023-04-22 21:43:31 -07:00
|
|
|
result->add_entry( parent );
|
2023-05-05 21:18:44 -07:00
|
|
|
}
|
2023-04-22 21:43:31 -07:00
|
|
|
|
2023-04-11 19:18:02 -07:00
|
|
|
result.lock();
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2023-05-06 12:49:43 -07:00
|
|
|
Code def_enum( StrC name
|
2023-05-05 15:10:03 -07:00
|
|
|
, Code body, Code type
|
|
|
|
, EnumT specifier, Code attributes
|
|
|
|
, ModuleFlag mflags )
|
2023-04-11 19:18:02 -07:00
|
|
|
{
|
|
|
|
using namespace ECode;
|
|
|
|
|
2023-05-06 12:49:43 -07:00
|
|
|
name_check( def_enum, name );
|
2023-04-11 19:18:02 -07:00
|
|
|
|
|
|
|
if ( type && type->Type != Typename )
|
|
|
|
{
|
|
|
|
log_failure( "gen::def_enum: enum underlying type provided was not of type Typename: %s", type->debug_str() );
|
2023-04-07 09:31:50 -07:00
|
|
|
return Code::Invalid;
|
|
|
|
}
|
2023-04-05 23:21:23 -07:00
|
|
|
|
2023-05-05 15:10:03 -07:00
|
|
|
if ( attributes && attributes->Type != Attributes )
|
|
|
|
{
|
|
|
|
log_failure( "gen::def_enum: attributes was not a 'Attributes' type: %s", attributes->debug_str() );
|
|
|
|
return Code::Invalid;
|
|
|
|
}
|
|
|
|
|
2023-04-05 23:21:23 -07:00
|
|
|
Code
|
2023-05-05 15:10:03 -07:00
|
|
|
result = make_code();
|
2023-05-06 12:49:43 -07:00
|
|
|
result->Name = get_cached_string( name );
|
2023-05-05 15:10:03 -07:00
|
|
|
result->ModuleFlags = mflags;
|
2023-04-01 19:21:46 -07:00
|
|
|
|
2023-04-07 09:31:50 -07:00
|
|
|
if ( body )
|
|
|
|
{
|
|
|
|
switch ( body->Type )
|
|
|
|
{
|
2023-04-11 19:18:02 -07:00
|
|
|
case Enum_Body:
|
2023-04-07 09:31:50 -07:00
|
|
|
case Untyped:
|
2023-04-11 19:18:02 -07:00
|
|
|
break;
|
2023-04-07 09:31:50 -07:00
|
|
|
|
|
|
|
default:
|
2023-04-11 19:18:02 -07:00
|
|
|
log_failure( "gen::def_enum: body must be of Enum_Body or Untyped type %s", body->debug_str());
|
2023-04-07 09:31:50 -07:00
|
|
|
return Code::Invalid;
|
|
|
|
}
|
|
|
|
|
2023-04-11 19:18:02 -07:00
|
|
|
result->Type = specifier == EnumClass ?
|
|
|
|
Enum_Class : Enum;
|
2023-04-01 19:21:46 -07:00
|
|
|
|
2023-04-11 19:18:02 -07:00
|
|
|
result->add_entry( body );
|
2023-04-07 09:31:50 -07:00
|
|
|
}
|
2023-06-28 18:20:29 -07:00
|
|
|
else
|
2023-04-07 09:31:50 -07:00
|
|
|
{
|
2023-04-11 19:18:02 -07:00
|
|
|
result->Type = specifier == EnumClass ?
|
|
|
|
Enum_Class_Fwd : Enum_Fwd;
|
2023-04-07 09:31:50 -07:00
|
|
|
}
|
2023-04-01 19:21:46 -07:00
|
|
|
|
2023-05-05 15:10:03 -07:00
|
|
|
if ( attributes )
|
|
|
|
result->add_entry( attributes );
|
|
|
|
|
2023-04-22 21:43:31 -07:00
|
|
|
if ( type )
|
|
|
|
{
|
|
|
|
result->add_entry( type );
|
|
|
|
}
|
2023-05-05 21:18:44 -07:00
|
|
|
else if ( result->Type != Enum_Class_Fwd && result->Type != Enum_Fwd )
|
2023-04-22 21:43:31 -07:00
|
|
|
{
|
|
|
|
log_failure( "gen::def_enum: enum forward declaration must have an underlying type" );
|
|
|
|
return Code::Invalid;
|
|
|
|
}
|
|
|
|
|
2023-04-03 23:04:19 -07:00
|
|
|
result.lock();
|
2023-04-01 19:21:46 -07:00
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2023-05-06 12:49:43 -07:00
|
|
|
Code def_execution( StrC content )
|
2023-04-07 09:31:50 -07:00
|
|
|
{
|
2023-05-06 12:49:43 -07:00
|
|
|
if ( content.Len <= 0 || content.Ptr == nullptr )
|
2023-04-07 09:31:50 -07:00
|
|
|
{
|
2023-05-06 12:49:43 -07:00
|
|
|
log_failure( "gen::def_execution: Invalid execution provided" );
|
2023-04-07 09:31:50 -07:00
|
|
|
return Code::Invalid;
|
|
|
|
}
|
|
|
|
|
2023-04-11 19:18:02 -07:00
|
|
|
Code
|
2023-05-01 11:12:07 -07:00
|
|
|
result = make_code();
|
|
|
|
result->Type = ECode::Execution;
|
2023-05-06 12:49:43 -07:00
|
|
|
result->Name = get_cached_string( content );
|
2023-05-01 11:12:07 -07:00
|
|
|
result->Content = result->Name;
|
2023-04-23 16:04:43 -07:00
|
|
|
|
|
|
|
result.lock();
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2023-05-06 12:49:43 -07:00
|
|
|
Code def_extern_link( StrC name, Code body, ModuleFlag mflags )
|
2023-04-23 16:04:43 -07:00
|
|
|
{
|
|
|
|
using namespace ECode;
|
|
|
|
|
2023-05-06 12:49:43 -07:00
|
|
|
name_check( def_extern_linkage, name );
|
2023-04-23 16:04:43 -07:00
|
|
|
null_check( def_extern_linkage, body );
|
|
|
|
|
2023-06-28 21:20:23 -07:00
|
|
|
if ( body->Type != Extern_Linkage_Body && body->Type != Untyped )
|
2023-04-23 16:04:43 -07:00
|
|
|
{
|
|
|
|
log_failure("gen::def_extern_linkage: body is not of extern_linkage or untyped type %s", body->debug_str());
|
|
|
|
return Code::Invalid;
|
|
|
|
}
|
|
|
|
|
|
|
|
Code
|
|
|
|
result = make_code();
|
2023-05-05 15:10:03 -07:00
|
|
|
result->Type = Extern_Linkage;
|
2023-05-06 12:49:43 -07:00
|
|
|
result->Name = get_cached_string( name );
|
2023-05-05 15:10:03 -07:00
|
|
|
result->ModuleFlags = mflags;
|
2023-04-23 16:04:43 -07:00
|
|
|
|
|
|
|
result->add_entry( body );
|
|
|
|
|
2023-04-10 18:33:06 -07:00
|
|
|
result.lock();
|
2023-04-07 09:31:50 -07:00
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2023-04-11 19:18:02 -07:00
|
|
|
Code def_friend( Code declaration )
|
2023-04-07 21:29:09 -07:00
|
|
|
{
|
2023-04-11 19:18:02 -07:00
|
|
|
using namespace ECode;
|
2023-04-10 18:33:06 -07:00
|
|
|
|
2023-04-11 19:18:02 -07:00
|
|
|
null_check( def_friend, declaration );
|
2023-04-10 18:33:06 -07:00
|
|
|
|
2023-04-11 19:18:02 -07:00
|
|
|
switch ( declaration->Type )
|
2023-04-07 21:29:09 -07:00
|
|
|
{
|
2023-04-11 19:18:02 -07:00
|
|
|
case Class_Fwd:
|
|
|
|
case Function_Fwd:
|
|
|
|
case Operator_Fwd:
|
|
|
|
case Struct_Fwd:
|
2023-04-10 18:33:06 -07:00
|
|
|
break;
|
|
|
|
|
2023-04-11 19:18:02 -07:00
|
|
|
default:
|
|
|
|
// Technically friends can have a function body. I will not support it so if you want that add it yourself. - Ed.
|
|
|
|
log_failure("gen::def_friend: declaration cannot be used with friend, must be a forward declare - %s", declaration->debug_str());
|
|
|
|
return Code::Invalid;
|
|
|
|
}
|
2023-04-10 18:33:06 -07:00
|
|
|
|
2023-04-11 19:18:02 -07:00
|
|
|
Code
|
|
|
|
result = make_code();
|
|
|
|
result->Type = Friend;
|
2023-04-10 18:33:06 -07:00
|
|
|
|
2023-04-11 19:18:02 -07:00
|
|
|
result->add_entry( declaration );
|
2023-04-10 18:33:06 -07:00
|
|
|
|
2023-04-11 19:18:02 -07:00
|
|
|
result.lock();
|
|
|
|
return result;
|
|
|
|
}
|
2023-04-10 18:33:06 -07:00
|
|
|
|
2023-05-06 12:49:43 -07:00
|
|
|
Code def_function( StrC name
|
2023-05-05 15:10:03 -07:00
|
|
|
, Code params , Code ret_type, Code body
|
|
|
|
, Code specifiers, Code attributes
|
|
|
|
, ModuleFlag mflags )
|
2023-04-11 19:18:02 -07:00
|
|
|
{
|
|
|
|
using namespace ECode;
|
2023-04-10 18:33:06 -07:00
|
|
|
|
2023-05-06 12:49:43 -07:00
|
|
|
name_check( def_function, name );
|
2023-04-10 18:33:06 -07:00
|
|
|
|
2023-05-05 15:10:03 -07:00
|
|
|
if ( params && params->Type != Parameters )
|
2023-04-11 19:18:02 -07:00
|
|
|
{
|
2023-05-05 15:10:03 -07:00
|
|
|
log_failure( "gen::def_function: params was not a `Parameters` type: %s", params->debug_str() );
|
2023-04-11 19:18:02 -07:00
|
|
|
return Code::Invalid;
|
|
|
|
}
|
2023-04-10 18:33:06 -07:00
|
|
|
|
2023-06-28 11:43:21 -07:00
|
|
|
if ( ret_type && ret_type->Type != Typename )
|
2023-04-11 19:18:02 -07:00
|
|
|
{
|
2023-05-05 15:10:03 -07:00
|
|
|
log_failure( "gen::def_function: ret_type was not a Typename: %s", ret_type->debug_str() );
|
2023-04-11 19:18:02 -07:00
|
|
|
return Code::Invalid;
|
|
|
|
}
|
2023-04-10 18:33:06 -07:00
|
|
|
|
2023-05-05 15:10:03 -07:00
|
|
|
if ( specifiers && specifiers->Type != Specifiers )
|
2023-04-11 19:18:02 -07:00
|
|
|
{
|
2023-05-05 15:10:03 -07:00
|
|
|
log_failure( "gen::def_function: specifiers was not a `Specifiers` type: %s", specifiers->debug_str() );
|
|
|
|
return Code::Invalid;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ( attributes && attributes->Type != Attributes )
|
|
|
|
{
|
|
|
|
log_failure( "gen::def_function: attributes was not a `Attributes` type: %s", attributes->debug_str() );
|
2023-04-11 19:18:02 -07:00
|
|
|
return Code::Invalid;
|
|
|
|
}
|
2023-04-10 18:33:06 -07:00
|
|
|
|
2023-04-11 19:18:02 -07:00
|
|
|
Code
|
2023-05-05 15:10:03 -07:00
|
|
|
result = make_code();
|
2023-05-06 12:49:43 -07:00
|
|
|
result->Name = get_cached_string( name );
|
2023-05-05 15:10:03 -07:00
|
|
|
result->ModuleFlags = mflags;
|
2023-04-10 18:33:06 -07:00
|
|
|
|
2023-04-11 19:18:02 -07:00
|
|
|
if ( body )
|
|
|
|
{
|
|
|
|
switch ( body->Type )
|
|
|
|
{
|
|
|
|
case Function_Body:
|
|
|
|
case Untyped:
|
|
|
|
break;
|
2023-04-10 18:33:06 -07:00
|
|
|
|
2023-04-11 19:18:02 -07:00
|
|
|
default:
|
2023-04-10 18:33:06 -07:00
|
|
|
{
|
2023-04-11 19:18:02 -07:00
|
|
|
log_failure("gen::def_function: body must be either of Function_Body or Untyped type. %s", body->debug_str());
|
2023-04-10 18:33:06 -07:00
|
|
|
return Code::Invalid;
|
|
|
|
}
|
2023-04-11 19:18:02 -07:00
|
|
|
}
|
2023-04-10 18:33:06 -07:00
|
|
|
|
2023-04-11 19:18:02 -07:00
|
|
|
result->Type = Function;
|
|
|
|
result->add_entry( body );
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
result->Type = Function_Fwd;
|
|
|
|
}
|
2023-04-10 18:33:06 -07:00
|
|
|
|
2023-05-05 15:10:03 -07:00
|
|
|
if ( attributes )
|
|
|
|
result->add_entry( attributes );
|
|
|
|
|
2023-04-11 19:18:02 -07:00
|
|
|
if ( specifiers )
|
|
|
|
result->add_entry( specifiers );
|
2023-04-10 18:33:06 -07:00
|
|
|
|
2023-04-11 19:18:02 -07:00
|
|
|
if ( ret_type )
|
|
|
|
{
|
|
|
|
result->add_entry( ret_type );
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
result->add_entry( type_ns(void) );
|
|
|
|
}
|
2023-04-10 18:33:06 -07:00
|
|
|
|
2023-04-11 19:18:02 -07:00
|
|
|
if ( params )
|
|
|
|
result->add_entry( params );
|
2023-04-10 18:33:06 -07:00
|
|
|
|
2023-04-11 19:18:02 -07:00
|
|
|
result.lock();
|
|
|
|
return result;
|
|
|
|
}
|
2023-04-10 18:33:06 -07:00
|
|
|
|
2023-05-06 12:49:43 -07:00
|
|
|
Code def_include ( StrC path )
|
2023-05-01 11:12:07 -07:00
|
|
|
{
|
2023-05-06 12:49:43 -07:00
|
|
|
if ( path.Len <= 0 || path.Ptr == nullptr )
|
2023-05-01 11:12:07 -07:00
|
|
|
{
|
2023-05-06 12:49:43 -07:00
|
|
|
log_failure( "gen::def_include: Invalid path provided - %d" );
|
2023-05-01 11:12:07 -07:00
|
|
|
return Code::Invalid;
|
|
|
|
}
|
|
|
|
|
|
|
|
Code
|
|
|
|
result = make_code();
|
|
|
|
result->Type = ECode::Preprocessor_Include;
|
2023-05-06 12:49:43 -07:00
|
|
|
result->Name = get_cached_string( path );
|
2023-05-01 11:12:07 -07:00
|
|
|
result->Content = result->Name;
|
|
|
|
|
|
|
|
result.lock();
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2023-05-06 12:49:43 -07:00
|
|
|
Code def_module( StrC name, ModuleFlag mflags )
|
2023-05-05 15:10:03 -07:00
|
|
|
{
|
2023-05-06 12:49:43 -07:00
|
|
|
name_check( def_module, name );
|
2023-05-05 15:10:03 -07:00
|
|
|
|
|
|
|
Code
|
|
|
|
result = make_code();
|
|
|
|
result->Type = ECode::Module;
|
2023-05-06 12:49:43 -07:00
|
|
|
result->Name = get_cached_string( name );
|
2023-06-28 21:20:23 -07:00
|
|
|
result->Content = result->Name;
|
2023-05-05 15:10:03 -07:00
|
|
|
result->ModuleFlags = mflags;
|
|
|
|
|
|
|
|
result.lock();
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2023-05-06 12:49:43 -07:00
|
|
|
Code def_namespace( StrC name, Code body, ModuleFlag mflags )
|
2023-04-11 19:18:02 -07:00
|
|
|
{
|
|
|
|
using namespace ECode;
|
2023-04-10 18:33:06 -07:00
|
|
|
|
2023-05-06 12:49:43 -07:00
|
|
|
name_check( def_namespace, name );
|
2023-04-11 19:18:02 -07:00
|
|
|
null_check( def_namespace, body );
|
|
|
|
|
2023-06-28 21:20:23 -07:00
|
|
|
if ( body->Type != Namespace_Body && body->Type != Untyped )
|
2023-04-11 19:18:02 -07:00
|
|
|
{
|
|
|
|
log_failure("gen::def_namespace: body is not of namespace or untyped type %s", body->debug_str());
|
|
|
|
return Code::Invalid;
|
2023-04-07 21:29:09 -07:00
|
|
|
}
|
|
|
|
|
2023-04-23 16:04:43 -07:00
|
|
|
Code
|
2023-05-05 15:10:03 -07:00
|
|
|
result = make_code();
|
|
|
|
result->Type = Namespace;
|
2023-05-06 12:49:43 -07:00
|
|
|
result->Name = get_cached_string( name );
|
2023-05-05 15:10:03 -07:00
|
|
|
result->ModuleFlags = mflags;
|
2023-04-23 16:04:43 -07:00
|
|
|
|
2023-04-11 19:18:02 -07:00
|
|
|
result->add_entry( body );
|
|
|
|
|
|
|
|
result.lock();
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2023-05-05 15:10:03 -07:00
|
|
|
Code def_operator( OperatorT op
|
|
|
|
, Code params_code, Code ret_type, Code body
|
|
|
|
, Code specifiers, Code attributes
|
|
|
|
, ModuleFlag mflags )
|
2023-04-11 19:18:02 -07:00
|
|
|
{
|
|
|
|
using namespace ECode;
|
|
|
|
|
2023-06-29 19:48:47 -07:00
|
|
|
if ( body && body->Type != Function_Body && body->Type != Untyped )
|
2023-04-11 19:18:02 -07:00
|
|
|
{
|
2023-05-05 15:10:03 -07:00
|
|
|
log_failure( "gen::def_operator: Body was provided but its not of function body type: %s", body->debug_str() );
|
|
|
|
return Code::Invalid;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ( attributes && attributes->Type != Attributes )
|
|
|
|
{
|
|
|
|
log_failure( "gen::def_operator: Attributes was provided but its not of attributes type: %s", attributes->debug_str() );
|
|
|
|
return Code::Invalid;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ( specifiers && specifiers->Type != Specifiers )
|
|
|
|
{
|
|
|
|
log_failure( "gen::def_operator: Specifiers was provided but its not of specifiers type: %s", specifiers->debug_str() );
|
2023-04-11 19:18:02 -07:00
|
|
|
return Code::Invalid;
|
|
|
|
}
|
|
|
|
|
2023-04-11 22:24:43 -07:00
|
|
|
OpValidateResult check_result = operator__validate( op, params_code, ret_type, specifiers );
|
2023-04-11 19:18:02 -07:00
|
|
|
|
|
|
|
if ( check_result == OpValidateResult::Fail )
|
|
|
|
{
|
|
|
|
return Code::Invalid;
|
|
|
|
}
|
2023-04-10 18:33:06 -07:00
|
|
|
|
2023-06-29 19:48:47 -07:00
|
|
|
char const* name = str_fmt_buf( "operator %s", to_str(op) );
|
2023-04-10 18:33:06 -07:00
|
|
|
|
2023-04-09 21:38:47 -07:00
|
|
|
Code
|
2023-05-05 15:10:03 -07:00
|
|
|
result = make_code();
|
2023-05-06 12:49:43 -07:00
|
|
|
result->Name = get_cached_string( { str_len(name), name } );
|
2023-05-05 15:10:03 -07:00
|
|
|
result->ModuleFlags = mflags;
|
2023-04-09 21:38:47 -07:00
|
|
|
|
|
|
|
if ( body )
|
|
|
|
{
|
2023-04-11 19:18:02 -07:00
|
|
|
result->Type = check_result == OpValidateResult::Global ?
|
|
|
|
Operator : Operator_Member;
|
2023-04-09 21:38:47 -07:00
|
|
|
|
2023-04-10 18:33:06 -07:00
|
|
|
result->add_entry( body );
|
2023-04-09 21:38:47 -07:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2023-04-11 19:18:02 -07:00
|
|
|
result->Type = check_result == OpValidateResult::Global ?
|
|
|
|
Operator_Fwd : Operator_Member_Fwd;
|
2023-04-09 21:38:47 -07:00
|
|
|
}
|
|
|
|
|
2023-05-05 15:10:03 -07:00
|
|
|
if ( attributes )
|
|
|
|
result->add_entry( attributes );
|
|
|
|
|
2023-04-22 21:43:31 -07:00
|
|
|
if ( specifiers )
|
|
|
|
result->add_entry( specifiers );
|
2023-04-10 18:33:06 -07:00
|
|
|
|
|
|
|
result->add_entry( ret_type );
|
|
|
|
|
2023-04-22 21:43:31 -07:00
|
|
|
if (params_code)
|
|
|
|
result->add_entry( params_code );
|
2023-04-10 18:33:06 -07:00
|
|
|
|
2023-04-09 21:38:47 -07:00
|
|
|
result.lock();
|
|
|
|
return result;
|
2023-04-07 21:29:09 -07:00
|
|
|
}
|
|
|
|
|
2023-05-06 12:49:43 -07:00
|
|
|
Code def_param( Code type, StrC name, Code value )
|
2023-04-07 09:31:50 -07:00
|
|
|
{
|
2023-04-09 21:38:47 -07:00
|
|
|
using namespace ECode;
|
|
|
|
|
2023-05-06 12:49:43 -07:00
|
|
|
name_check( def_param, name );
|
2023-04-09 21:38:47 -07:00
|
|
|
null_check( def_param, type );
|
2023-04-07 09:31:50 -07:00
|
|
|
|
2023-04-09 21:38:47 -07:00
|
|
|
if ( type->Type != Typename )
|
|
|
|
{
|
|
|
|
log_failure( "gen::def_param: type is not a typename - %s", type->debug_str() );
|
|
|
|
return Code::Invalid;
|
|
|
|
}
|
|
|
|
|
2023-04-23 16:04:43 -07:00
|
|
|
if ( value && value->Type != Untyped )
|
|
|
|
{
|
|
|
|
log_failure( "gen::def_param: value is not untyped - %s", value->debug_str() );
|
|
|
|
return Code::Invalid;
|
|
|
|
}
|
|
|
|
|
2023-04-09 21:38:47 -07:00
|
|
|
Code
|
2023-04-11 19:18:02 -07:00
|
|
|
result = make_code();
|
|
|
|
result->Type = Parameters;
|
2023-05-06 12:49:43 -07:00
|
|
|
result->Name = get_cached_string( name );
|
2023-04-09 21:38:47 -07:00
|
|
|
|
|
|
|
result->add_entry( type );
|
|
|
|
|
2023-04-23 16:04:43 -07:00
|
|
|
if ( value )
|
|
|
|
result->add_entry( value );
|
|
|
|
|
2023-04-09 21:38:47 -07:00
|
|
|
result.lock();
|
|
|
|
return result;
|
2023-04-07 09:31:50 -07:00
|
|
|
}
|
|
|
|
|
2023-06-30 00:13:41 -07:00
|
|
|
Code def_specifier( SpecifierT spec )
|
|
|
|
{
|
|
|
|
Code
|
|
|
|
result = make_code();
|
|
|
|
result->Type = ECode::Specifiers;
|
|
|
|
|
|
|
|
result->add_specifier( spec );
|
|
|
|
|
|
|
|
result.lock();
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2023-05-06 12:49:43 -07:00
|
|
|
Code def_struct( StrC name
|
2023-05-05 15:10:03 -07:00
|
|
|
, Code body
|
2023-05-05 21:18:44 -07:00
|
|
|
, Code parent, AccessSpec parent_access
|
|
|
|
, Code attributes
|
2023-05-05 15:10:03 -07:00
|
|
|
, ModuleFlag mflags )
|
2023-04-02 09:35:14 -07:00
|
|
|
{
|
2023-04-03 00:55:28 -07:00
|
|
|
using namespace ECode;
|
|
|
|
|
2023-05-06 12:49:43 -07:00
|
|
|
name_check( def_struct, name );
|
2023-04-07 21:29:09 -07:00
|
|
|
|
2023-05-05 15:10:03 -07:00
|
|
|
if ( attributes && attributes->Type != Attributes )
|
|
|
|
{
|
|
|
|
log_failure( "gen::def_struct: attributes was not a `Attributes` type" );
|
|
|
|
return Code::Invalid;
|
|
|
|
}
|
|
|
|
|
2023-06-30 00:13:41 -07:00
|
|
|
if ( parent && parent->Type != Typename )
|
2023-04-07 21:29:09 -07:00
|
|
|
{
|
|
|
|
log_failure( "gen::def_struct: parent was not a `Struct` type" );
|
|
|
|
return Code::Invalid;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ( body && body->Type != Struct_Body )
|
|
|
|
{
|
|
|
|
log_failure( "gen::def_struct: body was not a Struct_Body type" );
|
|
|
|
return Code::Invalid;
|
|
|
|
}
|
|
|
|
|
|
|
|
Code
|
2023-05-05 15:10:03 -07:00
|
|
|
result = make_code();
|
2023-05-06 12:49:43 -07:00
|
|
|
result->Name = get_cached_string( name );
|
2023-05-05 15:10:03 -07:00
|
|
|
result->ModuleFlags = mflags;
|
2023-04-07 21:29:09 -07:00
|
|
|
|
|
|
|
if ( body )
|
2023-04-09 21:38:47 -07:00
|
|
|
{
|
|
|
|
result->Type = Struct;
|
2023-04-07 21:29:09 -07:00
|
|
|
result->add_entry( body );
|
2023-04-09 21:38:47 -07:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2023-04-10 18:33:06 -07:00
|
|
|
result->Type = Struct_Fwd;
|
2023-04-09 21:38:47 -07:00
|
|
|
}
|
2023-04-07 21:29:09 -07:00
|
|
|
|
2023-05-05 15:10:03 -07:00
|
|
|
if ( attributes )
|
|
|
|
result->add_entry( attributes );
|
2023-04-07 21:29:09 -07:00
|
|
|
|
2023-05-05 15:10:03 -07:00
|
|
|
if ( parent )
|
2023-05-05 21:18:44 -07:00
|
|
|
{
|
|
|
|
result->ParentAccess = parent_access;
|
2023-05-05 15:10:03 -07:00
|
|
|
result->add_entry( parent );
|
2023-05-05 21:18:44 -07:00
|
|
|
}
|
2023-05-05 15:10:03 -07:00
|
|
|
|
2023-04-10 18:33:06 -07:00
|
|
|
result.lock();
|
2023-04-07 21:29:09 -07:00
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2023-05-06 12:49:43 -07:00
|
|
|
Code def_type( StrC name, Code ArrayExpr, Code specifiers )
|
|
|
|
{
|
|
|
|
name_check( def_type, name );
|
|
|
|
|
|
|
|
if ( specifiers && specifiers->Type != ECode::Specifiers )
|
|
|
|
{
|
|
|
|
log_failure( "gen::def_type: specifiers is not of specifiers type - %s", specifiers->debug_str() );
|
|
|
|
return Code::Invalid;
|
|
|
|
}
|
|
|
|
|
|
|
|
Code
|
|
|
|
result = make_code();
|
|
|
|
result->Name = get_cached_string( name );
|
|
|
|
result->Type = ECode::Typename;
|
|
|
|
|
|
|
|
if ( specifiers )
|
|
|
|
result->add_entry( specifiers );
|
|
|
|
|
|
|
|
if ( ArrayExpr )
|
|
|
|
result->add_entry( ArrayExpr );
|
|
|
|
|
|
|
|
result.lock();
|
2023-05-09 18:54:54 -07:00
|
|
|
|
2023-05-06 12:49:43 -07:00
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
Code def_typedef( StrC name, Code type, Code attributes, ModuleFlag mflags )
|
2023-04-18 19:47:59 -07:00
|
|
|
{
|
2023-05-06 12:49:43 -07:00
|
|
|
name_check( def_typedef, name );
|
2023-04-18 19:47:59 -07:00
|
|
|
null_check( def_typedef, type );
|
|
|
|
|
|
|
|
if ( type->Type != ECode::Typename )
|
|
|
|
{
|
|
|
|
log_failure( "gen::def_typedef: type was not a Typename" );
|
|
|
|
return Code::Invalid;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Registering the type.
|
2023-05-06 12:49:43 -07:00
|
|
|
Code registered_type = def_type( name );
|
2023-04-18 19:47:59 -07:00
|
|
|
|
|
|
|
if ( ! registered_type )
|
|
|
|
{
|
|
|
|
log_failure( "gen::def_typedef: failed to register type" );
|
|
|
|
return Code::Invalid;
|
|
|
|
}
|
|
|
|
|
|
|
|
Code
|
2023-05-05 15:10:03 -07:00
|
|
|
result = make_code();
|
2023-05-06 12:49:43 -07:00
|
|
|
result->Name = get_cached_string( name );
|
2023-05-05 15:10:03 -07:00
|
|
|
result->Type = ECode::Typedef;
|
|
|
|
result->ModuleFlags = mflags;
|
2023-04-18 19:47:59 -07:00
|
|
|
|
|
|
|
result->add_entry( type );
|
|
|
|
|
2023-05-05 15:10:03 -07:00
|
|
|
if ( attributes )
|
|
|
|
result->add_entry( attributes );
|
|
|
|
|
|
|
|
result.lock();
|
2023-04-18 19:47:59 -07:00
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2023-05-06 12:49:43 -07:00
|
|
|
Code def_union( StrC name, Code body, Code attributes, ModuleFlag mflags )
|
2023-04-23 19:53:30 -07:00
|
|
|
{
|
2023-05-06 12:49:43 -07:00
|
|
|
name_check( def_union, name );
|
2023-06-28 21:20:23 -07:00
|
|
|
null_check( def_union, body );
|
2023-04-23 19:53:30 -07:00
|
|
|
|
2023-06-28 21:20:23 -07:00
|
|
|
if ( body->Type != ECode::Union_Body )
|
2023-04-23 19:53:30 -07:00
|
|
|
{
|
|
|
|
log_failure( "gen::def_union: body was not a Union_Body type" );
|
|
|
|
return Code::Invalid;
|
|
|
|
}
|
|
|
|
|
|
|
|
Code
|
|
|
|
result = make_code();
|
2023-05-06 12:49:43 -07:00
|
|
|
result->Name = get_cached_string( name );
|
2023-05-05 15:10:03 -07:00
|
|
|
result->ModuleFlags = mflags;
|
2023-06-28 21:20:23 -07:00
|
|
|
result->Type = ECode::Union;
|
2023-04-23 19:53:30 -07:00
|
|
|
|
2023-06-28 21:20:23 -07:00
|
|
|
result->add_entry( body );
|
2023-04-23 19:53:30 -07:00
|
|
|
|
2023-05-05 15:10:03 -07:00
|
|
|
if ( attributes )
|
|
|
|
result->add_entry( attributes );
|
|
|
|
|
2023-04-23 19:53:30 -07:00
|
|
|
result.lock();
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2023-06-28 21:20:23 -07:00
|
|
|
Code def_using( StrC name, Code type
|
2023-05-05 15:10:03 -07:00
|
|
|
, Code attributes
|
|
|
|
, ModuleFlag mflags )
|
2023-04-07 21:29:09 -07:00
|
|
|
{
|
2023-05-06 12:49:43 -07:00
|
|
|
name_check( def_using, name );
|
2023-04-09 21:38:47 -07:00
|
|
|
null_check( def_using, type );
|
2023-04-07 21:29:09 -07:00
|
|
|
|
2023-05-06 12:49:43 -07:00
|
|
|
Code register_type = def_type( name );
|
2023-04-18 19:47:59 -07:00
|
|
|
|
|
|
|
if ( ! register_type )
|
|
|
|
{
|
|
|
|
log_failure( "gen::def_using: failed to register type" );
|
|
|
|
return Code::Invalid;
|
|
|
|
}
|
|
|
|
|
2023-04-07 21:29:09 -07:00
|
|
|
Code
|
2023-05-05 21:18:44 -07:00
|
|
|
result = make_code();
|
2023-05-06 12:49:43 -07:00
|
|
|
result->Name = get_cached_string( name );
|
2023-05-05 21:18:44 -07:00
|
|
|
result->ModuleFlags = mflags;
|
2023-06-28 21:20:23 -07:00
|
|
|
result->Type = ECode::Using;
|
2023-04-07 21:29:09 -07:00
|
|
|
|
2023-06-28 21:20:23 -07:00
|
|
|
if ( type )
|
|
|
|
result->add_entry( type );
|
2023-04-05 23:21:23 -07:00
|
|
|
|
2023-05-05 21:18:44 -07:00
|
|
|
if ( attributes )
|
|
|
|
result->add_entry( attributes );
|
|
|
|
|
2023-04-09 21:38:47 -07:00
|
|
|
result.lock();
|
2023-04-03 00:55:28 -07:00
|
|
|
return result;
|
2023-04-02 09:35:14 -07:00
|
|
|
}
|
|
|
|
|
2023-06-28 21:20:23 -07:00
|
|
|
Code def_using_namespace( StrC name )
|
|
|
|
{
|
|
|
|
name_check( def_using_namespace, name );
|
|
|
|
|
|
|
|
Code
|
|
|
|
result = make_code();
|
|
|
|
result->Name = get_cached_string( name );
|
|
|
|
result->Content = result->Name;
|
|
|
|
result->Type = ECode::Using_Namespace;
|
|
|
|
|
|
|
|
result.lock();
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2023-05-06 12:49:43 -07:00
|
|
|
Code def_variable( Code type, StrC name, Code value
|
2023-05-05 15:10:03 -07:00
|
|
|
, Code specifiers, Code attributes
|
|
|
|
, ModuleFlag mflags )
|
2023-05-01 11:12:07 -07:00
|
|
|
{
|
2023-05-06 12:49:43 -07:00
|
|
|
name_check( def_variable, name );
|
2023-05-01 11:12:07 -07:00
|
|
|
null_check( def_variable, type );
|
|
|
|
|
2023-05-05 15:10:03 -07:00
|
|
|
if ( attributes && attributes->Type != ECode::Attributes )
|
|
|
|
{
|
|
|
|
log_failure( "gen::def_variable: attributes was not a `Attributes` type" );
|
|
|
|
return Code::Invalid;
|
|
|
|
}
|
|
|
|
|
2023-05-01 11:12:07 -07:00
|
|
|
if ( specifiers && specifiers->Type != ECode::Specifiers )
|
|
|
|
{
|
|
|
|
log_failure( "gen::def_variable: specifiers was not a `Specifiers` type" );
|
|
|
|
return Code::Invalid;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ( type->Type != ECode::Typename )
|
|
|
|
{
|
|
|
|
log_failure( "gen::def_variable: type was not a Typename" );
|
|
|
|
return Code::Invalid;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ( value && value->Type != ECode::Untyped )
|
|
|
|
{
|
|
|
|
log_failure( "gen::def_variable: value was not a `Untyped` type" );
|
|
|
|
return Code::Invalid;
|
|
|
|
}
|
|
|
|
|
|
|
|
Code
|
2023-05-05 15:10:03 -07:00
|
|
|
result = make_code();
|
2023-05-06 12:49:43 -07:00
|
|
|
result->Name = get_cached_string( name );
|
2023-05-05 15:10:03 -07:00
|
|
|
result->Type = ECode::Variable;
|
|
|
|
result->ModuleFlags = mflags;
|
2023-05-01 11:12:07 -07:00
|
|
|
|
|
|
|
result->add_entry( type );
|
|
|
|
|
2023-05-05 15:10:03 -07:00
|
|
|
if ( attributes )
|
|
|
|
result->add_entry( attributes );
|
|
|
|
|
2023-05-01 11:12:07 -07:00
|
|
|
if ( specifiers )
|
|
|
|
result->add_entry( specifiers );
|
|
|
|
|
|
|
|
if ( value )
|
|
|
|
result->add_entry( value );
|
|
|
|
|
|
|
|
result.lock();
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2023-04-11 19:18:02 -07:00
|
|
|
/*
|
|
|
|
Body related functions typically follow the same implementation pattern.
|
|
|
|
So I opted to use inline helper macros to get the implementaiton done.
|
|
|
|
|
|
|
|
The implementation pattern is as follows:
|
|
|
|
* Validate a valid parameter num was provided, or code array
|
|
|
|
def_body_start or def_body_code_array_start( <name of function >)
|
|
|
|
|
|
|
|
* Begin the code entry do-while loop, make sure each entry is valid processing its type in the switc
|
|
|
|
def_body_code_validation_start( <name of function> )
|
|
|
|
|
|
|
|
* Define the switch case statements between the macros.
|
|
|
|
|
|
|
|
* Add the code entry, finish the closing implemenation for the do-while loop.
|
|
|
|
def_body_code_validation_end( <name of function> )
|
|
|
|
|
|
|
|
* Lock the body AST and return it.
|
|
|
|
|
|
|
|
If a function's implementation deviates from the macros then its just writen it out.
|
|
|
|
*/
|
|
|
|
|
2023-04-22 19:24:55 -07:00
|
|
|
#pragma region Helper Macros for def_**_body functions
|
2023-04-11 19:18:02 -07:00
|
|
|
# define def_body_start( Name_ ) \
|
|
|
|
using namespace ECode; \
|
|
|
|
\
|
|
|
|
if ( num <= 0 ) \
|
|
|
|
{ \
|
|
|
|
log_failure("gen::" txt(Name_) ": num cannot be zero or negative"); \
|
|
|
|
return Code::Invalid; \
|
|
|
|
}
|
|
|
|
|
|
|
|
# define def_body_code_array_start( Name_ ) \
|
|
|
|
using namespace ECode; \
|
|
|
|
\
|
|
|
|
if ( num <= 0 ) \
|
|
|
|
{ \
|
|
|
|
log_failure("gen::" txt(Name_) ": num cannot be zero or negative"); \
|
|
|
|
return Code::Invalid; \
|
|
|
|
} \
|
|
|
|
\
|
|
|
|
if ( codes == nullptr ) \
|
|
|
|
{ \
|
|
|
|
log_failure("gen::def_class_body: Provided a null array of codes"); \
|
|
|
|
return Code::Invalid; \
|
|
|
|
}
|
|
|
|
|
2023-05-07 12:46:28 -07:00
|
|
|
# define def_body_code_validation_start( Name_ ) \
|
2023-04-11 19:18:02 -07:00
|
|
|
do \
|
|
|
|
{ \
|
2023-05-07 12:46:28 -07:00
|
|
|
Code_POD pod = va_arg(va, Code_POD); \
|
|
|
|
Code entry = pcast(Code, pod); \
|
|
|
|
\
|
|
|
|
if ( ! entry ) \
|
|
|
|
{ \
|
|
|
|
log_failure("gen::" txt(Name_) ": Provided an null entry"); \
|
|
|
|
return Code::Invalid; \
|
|
|
|
} \
|
|
|
|
\
|
|
|
|
switch ( entry->Type ) \
|
|
|
|
{
|
|
|
|
|
|
|
|
# define def_body_code_array_validation_start( Name_ ) \
|
|
|
|
do \
|
|
|
|
{ \
|
|
|
|
Code entry = *codes; codes++; \
|
2023-04-11 19:18:02 -07:00
|
|
|
\
|
|
|
|
if ( ! entry ) \
|
|
|
|
{ \
|
|
|
|
log_failure("gen::" txt(Name_) ": Provided an null entry"); \
|
|
|
|
return Code::Invalid; \
|
|
|
|
} \
|
|
|
|
\
|
|
|
|
switch ( entry->Type ) \
|
|
|
|
{
|
|
|
|
|
|
|
|
# define def_body_code_validation_end( Name_ ) \
|
|
|
|
log_failure("gen::" txt(Name_) ": Entry type is not allowed: %s", entry->debug_str() ); \
|
|
|
|
return Code::Invalid; \
|
|
|
|
\
|
|
|
|
default: \
|
|
|
|
break; \
|
|
|
|
} \
|
|
|
|
\
|
|
|
|
result->add_entry( entry ); \
|
|
|
|
} \
|
|
|
|
while ( num--, num > 0 )
|
2023-04-22 19:24:55 -07:00
|
|
|
#pragma endregion Helper Macros for def_**_body functions
|
2023-04-10 18:33:06 -07:00
|
|
|
|
2023-04-11 19:18:02 -07:00
|
|
|
Code def_class_body( s32 num, ... )
|
2023-04-03 23:04:19 -07:00
|
|
|
{
|
2023-04-11 19:18:02 -07:00
|
|
|
def_body_start( def_class_body );
|
2023-04-03 23:04:19 -07:00
|
|
|
|
2023-04-10 18:33:06 -07:00
|
|
|
Code
|
|
|
|
result = make_code();
|
2023-04-11 19:18:02 -07:00
|
|
|
result->Type = Class_Body;
|
2023-04-07 21:29:09 -07:00
|
|
|
|
|
|
|
va_list va;
|
|
|
|
va_start(va, num);
|
2023-05-07 12:46:28 -07:00
|
|
|
def_body_code_validation_start( def_class_body );
|
2023-04-22 19:24:55 -07:00
|
|
|
AST_BODY_CLASS_UNALLOWED_TYPES
|
2023-04-11 19:18:02 -07:00
|
|
|
def_body_code_validation_end( def_class_body );
|
2023-04-07 21:29:09 -07:00
|
|
|
va_end(va);
|
2023-04-05 23:21:23 -07:00
|
|
|
|
2023-04-10 18:33:06 -07:00
|
|
|
result.lock();
|
2023-04-05 23:21:23 -07:00
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2023-04-11 19:18:02 -07:00
|
|
|
Code def_class_body( s32 num, Code* codes )
|
2023-04-07 21:29:09 -07:00
|
|
|
{
|
2023-04-11 19:18:02 -07:00
|
|
|
def_body_code_array_start( def_class_body );
|
2023-04-07 21:29:09 -07:00
|
|
|
|
2023-04-10 18:33:06 -07:00
|
|
|
Code
|
|
|
|
result = make_code();
|
|
|
|
result->Type = Function_Body;
|
2023-04-07 21:29:09 -07:00
|
|
|
|
2023-05-07 12:46:28 -07:00
|
|
|
def_body_code_array_validation_start( def_class_body );
|
2023-04-22 19:24:55 -07:00
|
|
|
AST_BODY_CLASS_UNALLOWED_TYPES
|
2023-04-11 19:18:02 -07:00
|
|
|
def_body_code_validation_end( def_class_body );
|
2023-04-07 21:29:09 -07:00
|
|
|
|
2023-04-10 18:33:06 -07:00
|
|
|
result.lock();
|
2023-04-07 21:29:09 -07:00
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2023-04-11 19:18:02 -07:00
|
|
|
Code def_enum_body( s32 num, ... )
|
2023-04-07 21:29:09 -07:00
|
|
|
{
|
2023-04-11 19:18:02 -07:00
|
|
|
def_body_start( def_enum_body );
|
2023-04-09 21:38:47 -07:00
|
|
|
|
2023-04-10 18:33:06 -07:00
|
|
|
Code
|
|
|
|
result = make_code();
|
2023-04-11 19:18:02 -07:00
|
|
|
result->Type = Enum_Body;
|
2023-04-09 21:38:47 -07:00
|
|
|
|
|
|
|
va_list va;
|
|
|
|
va_start(va, num);
|
|
|
|
do
|
|
|
|
{
|
2023-05-07 12:46:28 -07:00
|
|
|
Code_POD pod = va_arg(va, Code_POD);
|
|
|
|
Code entry = pcast(Code, pod);
|
2023-04-09 21:38:47 -07:00
|
|
|
|
|
|
|
if ( ! entry )
|
|
|
|
{
|
2023-04-11 19:18:02 -07:00
|
|
|
log_failure("gen::def_enum_body: Provided a null entry");
|
2023-04-09 21:38:47 -07:00
|
|
|
return Code::Invalid;
|
|
|
|
}
|
|
|
|
|
2023-04-11 19:18:02 -07:00
|
|
|
if ( entry->Type != Untyped )
|
2023-04-09 21:38:47 -07:00
|
|
|
{
|
2023-04-11 19:18:02 -07:00
|
|
|
log_failure("gen::def_enum_body: Entry type is not allowed - %s. Must be of untyped type.", entry->debug_str() ); \
|
|
|
|
return Code::Invalid;
|
2023-04-09 21:38:47 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
result->add_entry( entry );
|
|
|
|
}
|
|
|
|
while ( num--, num > 0 );
|
|
|
|
va_end(va);
|
|
|
|
|
2023-04-10 18:33:06 -07:00
|
|
|
result.lock();
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2023-04-11 19:18:02 -07:00
|
|
|
Code def_enum_body( s32 num, Code* codes )
|
2023-04-10 18:33:06 -07:00
|
|
|
{
|
2023-04-11 19:18:02 -07:00
|
|
|
def_body_code_array_start( def_enum_body );
|
2023-04-10 18:33:06 -07:00
|
|
|
|
|
|
|
Code
|
|
|
|
result = make_code();
|
2023-04-23 19:53:30 -07:00
|
|
|
result->Type = Enum_Body;
|
2023-04-10 18:33:06 -07:00
|
|
|
|
|
|
|
do
|
|
|
|
{
|
|
|
|
Code entry = *codes;
|
|
|
|
|
|
|
|
if ( ! entry )
|
|
|
|
{
|
2023-04-11 19:18:02 -07:00
|
|
|
log_failure("gen::def_enum_body: Provided a null entry");
|
2023-04-10 18:33:06 -07:00
|
|
|
return Code::Invalid;
|
|
|
|
}
|
|
|
|
|
2023-04-11 19:18:02 -07:00
|
|
|
if ( entry->Type != Untyped )
|
2023-04-10 18:33:06 -07:00
|
|
|
{
|
2023-04-11 19:18:02 -07:00
|
|
|
log_failure("gen::def_enum_body: Entry type is not allowed: %s", entry->debug_str() ); \
|
|
|
|
return Code::Invalid;
|
2023-04-10 18:33:06 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
result->add_entry( entry );
|
|
|
|
}
|
2023-04-11 19:18:02 -07:00
|
|
|
while ( codes++, num--, num > 0 );
|
2023-04-10 18:33:06 -07:00
|
|
|
|
|
|
|
result.lock();
|
2023-04-09 21:38:47 -07:00
|
|
|
return result;
|
2023-04-07 21:29:09 -07:00
|
|
|
}
|
|
|
|
|
2023-05-01 11:12:07 -07:00
|
|
|
Code def_export_body( s32 num, ... )
|
|
|
|
{
|
|
|
|
def_body_start( def_export_body );
|
|
|
|
|
|
|
|
Code
|
|
|
|
result = make_code();
|
|
|
|
result->Type = Export_Body;
|
|
|
|
|
|
|
|
va_list va;
|
|
|
|
va_start(va, num);
|
2023-05-07 12:46:28 -07:00
|
|
|
def_body_code_validation_start( def_export_body );
|
2023-05-01 11:12:07 -07:00
|
|
|
AST_BODY_EXPORT_UNALLOWED_TYPES
|
|
|
|
def_body_code_validation_end( def_export_body );
|
|
|
|
va_end(va);
|
|
|
|
|
|
|
|
result.lock();
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
Code def_export_body( s32 num, Code* codes )
|
|
|
|
{
|
|
|
|
def_body_code_array_start( def_export_body );
|
|
|
|
|
|
|
|
Code
|
|
|
|
result = make_code();
|
|
|
|
result->Type = Export_Body;
|
|
|
|
|
2023-05-07 12:46:28 -07:00
|
|
|
def_body_code_array_validation_start( def_export_body );
|
2023-05-01 11:12:07 -07:00
|
|
|
AST_BODY_EXPORT_UNALLOWED_TYPES
|
|
|
|
def_body_code_validation_end( def_export_body );
|
|
|
|
|
|
|
|
result.lock();
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
Code def_extern_link_body( s32 num, ... )
|
2023-04-23 16:04:43 -07:00
|
|
|
{
|
|
|
|
def_body_start( def_extern_linkage_body );
|
|
|
|
|
|
|
|
Code
|
|
|
|
result = make_code();
|
|
|
|
result->Type = Extern_Linkage_Body;
|
|
|
|
|
|
|
|
va_list va;
|
|
|
|
va_start(va, num);
|
2023-05-07 12:46:28 -07:00
|
|
|
def_body_code_validation_start( def_extern_linkage_body );
|
2023-04-23 16:04:43 -07:00
|
|
|
AST_BODY_EXTERN_LINKAGE_UNALLOWED_TYPES
|
|
|
|
def_body_code_validation_end( def_extern_linkage_body );
|
|
|
|
va_end(va);
|
|
|
|
|
|
|
|
result.lock();
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2023-05-01 11:12:07 -07:00
|
|
|
Code def_extern_link_body( s32 num, Code* codes )
|
2023-04-23 16:04:43 -07:00
|
|
|
{
|
|
|
|
def_body_code_array_start( def_extern_linkage_body );
|
|
|
|
|
|
|
|
Code
|
|
|
|
result = make_code();
|
|
|
|
result->Type = Extern_Linkage_Body;
|
|
|
|
|
2023-05-07 12:46:28 -07:00
|
|
|
def_body_code_array_validation_start( def_extern_linkage_body );
|
2023-04-23 16:04:43 -07:00
|
|
|
AST_BODY_EXTERN_LINKAGE_UNALLOWED_TYPES
|
|
|
|
def_body_code_validation_end( def_extern_linkage_body );
|
|
|
|
|
|
|
|
result.lock();
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2023-04-11 19:18:02 -07:00
|
|
|
Code def_function_body( s32 num, ... )
|
2023-04-07 21:29:09 -07:00
|
|
|
{
|
2023-04-11 19:18:02 -07:00
|
|
|
def_body_start( def_function_body );
|
2023-04-07 21:29:09 -07:00
|
|
|
|
|
|
|
Code
|
2023-04-11 19:18:02 -07:00
|
|
|
result = make_code();
|
|
|
|
result->Type = Function_Body;
|
2023-04-07 21:29:09 -07:00
|
|
|
|
|
|
|
va_list va;
|
|
|
|
va_start(va, num);
|
2023-05-07 12:46:28 -07:00
|
|
|
def_body_code_validation_start( def_function_body );
|
2023-04-22 19:24:55 -07:00
|
|
|
AST_BODY_FUNCTION_UNALLOWED_TYPES
|
2023-04-11 19:18:02 -07:00
|
|
|
def_body_code_validation_end( def_function_body );
|
|
|
|
va_end(va);
|
2023-04-07 21:29:09 -07:00
|
|
|
|
2023-04-11 19:18:02 -07:00
|
|
|
result.lock();
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
Code def_function_body( s32 num, Code* codes )
|
|
|
|
{
|
|
|
|
def_body_code_array_start( def_function_body );
|
|
|
|
|
|
|
|
Code
|
|
|
|
result = make_code();
|
|
|
|
result->Type = Function_Body;
|
|
|
|
|
2023-05-07 12:46:28 -07:00
|
|
|
def_body_code_array_validation_start( def_function_body );
|
2023-04-22 19:24:55 -07:00
|
|
|
AST_BODY_FUNCTION_UNALLOWED_TYPES
|
2023-04-11 19:18:02 -07:00
|
|
|
def_body_code_validation_end( def_function_body );
|
|
|
|
|
|
|
|
result.lock();
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
Code def_global_body( s32 num, ... )
|
|
|
|
{
|
|
|
|
def_body_start( def_global_body );
|
|
|
|
|
|
|
|
Code
|
|
|
|
result = make_code();
|
|
|
|
result->Type = Global_Body;
|
|
|
|
|
|
|
|
va_list va;
|
|
|
|
va_start(va, num);
|
2023-05-07 12:46:28 -07:00
|
|
|
def_body_code_validation_start( def_global_body );
|
2023-04-22 19:24:55 -07:00
|
|
|
AST_BODY_GLOBAL_UNALLOWED_TYPES
|
2023-04-11 19:18:02 -07:00
|
|
|
def_body_code_validation_end( def_global_body );
|
|
|
|
va_end(va);
|
|
|
|
|
|
|
|
result.lock();
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
Code def_global_body( s32 num, Code* codes )
|
|
|
|
{
|
|
|
|
def_body_code_array_start( def_global_body );
|
|
|
|
|
|
|
|
Code
|
|
|
|
result = make_code();
|
|
|
|
result->Type = Global_Body;
|
|
|
|
|
2023-05-07 12:46:28 -07:00
|
|
|
def_body_code_array_validation_start( def_global_body );
|
2023-04-22 19:24:55 -07:00
|
|
|
AST_BODY_GLOBAL_UNALLOWED_TYPES
|
2023-04-11 19:18:02 -07:00
|
|
|
def_body_code_validation_end( def_global_body );
|
2023-04-07 21:29:09 -07:00
|
|
|
|
2023-04-11 19:18:02 -07:00
|
|
|
result.lock();
|
|
|
|
return result;
|
|
|
|
}
|
2023-04-07 21:29:09 -07:00
|
|
|
|
2023-04-11 19:18:02 -07:00
|
|
|
Code def_namespace_body( s32 num, ... )
|
|
|
|
{
|
|
|
|
def_body_start( def_namespace_body );
|
|
|
|
|
|
|
|
Code
|
|
|
|
result = make_code();
|
|
|
|
result->Type = Namespace_Body;
|
|
|
|
|
|
|
|
va_list va;
|
|
|
|
va_start(va, num);
|
2023-05-07 12:46:28 -07:00
|
|
|
def_body_code_validation_start( def_namespace_body );
|
2023-04-22 19:24:55 -07:00
|
|
|
AST_BODY_NAMESPACE_UNALLOWED_TYPES
|
2023-04-11 19:18:02 -07:00
|
|
|
def_body_code_validation_end( def_namespace_body );
|
2023-04-07 21:29:09 -07:00
|
|
|
va_end(va);
|
|
|
|
|
2023-04-10 18:33:06 -07:00
|
|
|
result.lock();
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
Code def_namespace_body( s32 num, Code* codes )
|
|
|
|
{
|
2023-04-11 19:18:02 -07:00
|
|
|
def_body_code_array_start( def_namespace_body );
|
2023-04-10 18:33:06 -07:00
|
|
|
|
|
|
|
Code
|
|
|
|
result = make_code();
|
|
|
|
result->Type = Global_Body;
|
|
|
|
|
2023-05-07 12:46:28 -07:00
|
|
|
def_body_code_array_validation_start( def_namespace_body );
|
2023-04-22 19:24:55 -07:00
|
|
|
AST_BODY_NAMESPACE_UNALLOWED_TYPES
|
2023-04-11 19:18:02 -07:00
|
|
|
def_body_code_validation_end( def_namespace_body );
|
2023-04-10 18:33:06 -07:00
|
|
|
|
|
|
|
result.lock();
|
2023-04-07 21:29:09 -07:00
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
Code def_params( s32 num, ... )
|
2023-04-05 23:21:23 -07:00
|
|
|
{
|
2023-04-11 19:18:02 -07:00
|
|
|
def_body_start( def_params );
|
2023-04-05 23:21:23 -07:00
|
|
|
|
|
|
|
va_list va;
|
|
|
|
va_start(va, num);
|
|
|
|
|
2023-06-30 08:54:27 -07:00
|
|
|
Code_POD pod = va_arg(va, Code_POD);
|
|
|
|
Code param = pcast( Code, pod );
|
2023-04-05 23:21:23 -07:00
|
|
|
|
2023-06-30 08:54:27 -07:00
|
|
|
null_check( def_params, param );
|
2023-04-05 23:21:23 -07:00
|
|
|
|
2023-06-30 08:54:27 -07:00
|
|
|
if ( param->Type != Parameters )
|
2023-04-05 23:21:23 -07:00
|
|
|
{
|
2023-06-30 08:54:27 -07:00
|
|
|
log_failure( "gen::def_params: param %d is not a Parameters", num - num + 1 );
|
2023-04-05 23:21:23 -07:00
|
|
|
return Code::Invalid;
|
|
|
|
}
|
|
|
|
|
2023-06-30 08:54:27 -07:00
|
|
|
Code result = { param->duplicate() };
|
2023-04-05 23:21:23 -07:00
|
|
|
|
2023-06-30 08:54:27 -07:00
|
|
|
while ( -- num )
|
2023-04-05 23:21:23 -07:00
|
|
|
{
|
2023-06-30 08:54:27 -07:00
|
|
|
pod = va_arg(va, Code_POD);
|
|
|
|
param = pcast( Code, pod );
|
2023-04-05 23:21:23 -07:00
|
|
|
|
2023-06-30 08:54:27 -07:00
|
|
|
if ( param->Type != Parameters )
|
2023-04-05 23:21:23 -07:00
|
|
|
{
|
2023-06-30 08:54:27 -07:00
|
|
|
log_failure( "gen::def_params: param %d is not a Parameters", num - num + 1 );
|
2023-04-05 23:21:23 -07:00
|
|
|
return Code::Invalid;
|
|
|
|
}
|
|
|
|
|
2023-06-30 08:54:27 -07:00
|
|
|
result->add_entry( param );
|
2023-04-05 23:21:23 -07:00
|
|
|
}
|
|
|
|
va_end(va);
|
|
|
|
|
|
|
|
result.lock();
|
2023-04-03 23:04:19 -07:00
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2023-04-10 18:33:06 -07:00
|
|
|
Code def_params( s32 num, Code* codes )
|
|
|
|
{
|
2023-04-11 19:18:02 -07:00
|
|
|
def_body_code_array_start( def_params );
|
2023-04-10 18:33:06 -07:00
|
|
|
|
2023-04-23 11:33:37 -07:00
|
|
|
# define check_current() \
|
2023-04-10 18:33:06 -07:00
|
|
|
if ( current == nullptr ) \
|
|
|
|
{ \
|
|
|
|
log_failure("gen::def_params: Provide a null code in codes array"); \
|
|
|
|
return Code::Invalid; \
|
|
|
|
} \
|
|
|
|
\
|
|
|
|
if (current->Type != Parameters ) \
|
|
|
|
{ \
|
|
|
|
log_failure("gen::def_params: Code in coes array is not of paramter type - %s", current->debug_str() ); \
|
|
|
|
return Code::Invalid; \
|
|
|
|
}
|
|
|
|
|
|
|
|
Code current = * codes;
|
|
|
|
check_current();
|
|
|
|
|
|
|
|
Code
|
|
|
|
result = make_code();
|
|
|
|
result->Name = current->Name;
|
|
|
|
result->Type = current->Type;
|
|
|
|
|
2023-06-29 19:48:47 -07:00
|
|
|
result->add_entry( current->entry( 0 ) );
|
2023-06-28 21:20:23 -07:00
|
|
|
|
2023-04-10 18:33:06 -07:00
|
|
|
while( codes++, current = * codes, num--, num > 0 )
|
|
|
|
{
|
|
|
|
check_current();
|
|
|
|
result->add_entry( current );
|
|
|
|
}
|
2023-04-23 11:33:37 -07:00
|
|
|
# undef check_current
|
2023-04-10 18:33:06 -07:00
|
|
|
|
|
|
|
result.lock();
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2023-04-03 00:55:28 -07:00
|
|
|
Code def_specifiers( s32 num, ... )
|
2023-04-01 19:21:46 -07:00
|
|
|
{
|
|
|
|
if ( num <= 0 )
|
2023-04-11 19:18:02 -07:00
|
|
|
{
|
2023-04-10 18:33:06 -07:00
|
|
|
log_failure("gen::def_specifiers: num cannot be zero or less");
|
2023-04-11 19:18:02 -07:00
|
|
|
return Code::Invalid;
|
|
|
|
}
|
2023-04-01 19:21:46 -07:00
|
|
|
|
2023-04-11 19:18:02 -07:00
|
|
|
if ( num > AST::ArrSpecs_Cap )
|
|
|
|
{
|
|
|
|
log_failure("gen::def_specifiers: num of speciifers to define AST larger than AST specicifier capacity - %d", num);
|
|
|
|
return Code::Invalid;
|
|
|
|
}
|
2023-04-07 09:31:50 -07:00
|
|
|
|
2023-04-05 23:21:23 -07:00
|
|
|
Code
|
2023-04-09 21:38:47 -07:00
|
|
|
result = make_code();
|
|
|
|
result->Type = ECode::Specifiers;
|
2023-04-07 09:31:50 -07:00
|
|
|
|
2023-04-01 19:21:46 -07:00
|
|
|
va_list va;
|
|
|
|
va_start(va, num);
|
|
|
|
do
|
|
|
|
{
|
2023-04-03 23:04:19 -07:00
|
|
|
SpecifierT type = (SpecifierT)va_arg(va, int);
|
2023-04-01 19:21:46 -07:00
|
|
|
|
2023-04-11 19:18:02 -07:00
|
|
|
result->add_specifier( type );
|
2023-04-01 19:21:46 -07:00
|
|
|
}
|
2023-04-01 22:07:44 -07:00
|
|
|
while ( --num, num );
|
2023-04-01 19:21:46 -07:00
|
|
|
va_end(va);
|
|
|
|
|
2023-04-10 18:33:06 -07:00
|
|
|
result.lock();
|
2023-04-01 19:21:46 -07:00
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2023-04-03 00:55:28 -07:00
|
|
|
Code def_struct_body( s32 num, ... )
|
2023-04-02 09:35:14 -07:00
|
|
|
{
|
2023-04-11 19:18:02 -07:00
|
|
|
def_body_start( def_struct_body );
|
2023-04-03 00:55:28 -07:00
|
|
|
|
2023-04-07 21:29:09 -07:00
|
|
|
Code
|
2023-04-11 19:18:02 -07:00
|
|
|
result = make_code();
|
|
|
|
result->Type = Struct_Body;
|
2023-04-02 09:35:14 -07:00
|
|
|
|
2023-04-03 00:55:28 -07:00
|
|
|
va_list va;
|
|
|
|
va_start(va, num);
|
2023-05-07 12:46:28 -07:00
|
|
|
def_body_code_validation_start( def_struct_body );
|
2023-04-22 19:24:55 -07:00
|
|
|
AST_BODY_STRUCT_UNALLOWED_TYPES
|
2023-04-11 19:18:02 -07:00
|
|
|
def_body_code_validation_end( def_struct_body );
|
|
|
|
va_end(va);
|
2023-04-03 00:55:28 -07:00
|
|
|
|
2023-04-11 19:18:02 -07:00
|
|
|
result.lock();
|
|
|
|
return result;
|
|
|
|
}
|
2023-04-07 21:29:09 -07:00
|
|
|
|
2023-04-11 19:18:02 -07:00
|
|
|
Code def_struct_body( s32 num, Code* codes )
|
|
|
|
{
|
|
|
|
def_body_code_array_start( def_struct_body );
|
2023-04-10 18:33:06 -07:00
|
|
|
|
2023-04-11 19:18:02 -07:00
|
|
|
Code
|
|
|
|
result = make_code();
|
|
|
|
result->Type = Struct_Body;
|
2023-04-03 00:55:28 -07:00
|
|
|
|
2023-05-07 12:46:28 -07:00
|
|
|
def_body_code_array_validation_start( def_struct_body );
|
2023-04-22 19:24:55 -07:00
|
|
|
AST_BODY_STRUCT_UNALLOWED_TYPES
|
2023-04-11 19:18:02 -07:00
|
|
|
def_body_code_validation_end( def_struct_body );
|
2023-04-05 23:21:23 -07:00
|
|
|
|
2023-04-10 18:33:06 -07:00
|
|
|
result.lock();
|
2023-04-03 00:55:28 -07:00
|
|
|
return result;
|
2023-04-02 09:35:14 -07:00
|
|
|
}
|
2023-04-23 19:53:30 -07:00
|
|
|
|
|
|
|
Code def_union_body( s32 num, ... )
|
|
|
|
{
|
|
|
|
def_body_start( def_union_body );
|
|
|
|
|
|
|
|
Code
|
|
|
|
result = make_code();
|
|
|
|
result->Type = Union_Body;
|
|
|
|
|
|
|
|
va_list va;
|
|
|
|
va_start(va, num);
|
|
|
|
do
|
|
|
|
{
|
2023-05-07 12:46:28 -07:00
|
|
|
Code_POD pod = va_arg(va, Code_POD);
|
|
|
|
Code entry = pcast( Code, pod );
|
2023-04-23 19:53:30 -07:00
|
|
|
|
|
|
|
if ( ! entry )
|
|
|
|
{
|
|
|
|
log_failure("gen::def_union_body: Provided a null entry");
|
|
|
|
return Code::Invalid;
|
|
|
|
}
|
|
|
|
|
2023-06-28 21:20:23 -07:00
|
|
|
if ( entry->Type != Untyped && entry->Type != Comment )
|
2023-04-23 19:53:30 -07:00
|
|
|
{
|
|
|
|
log_failure("gen::def_union_body: Entry type is not allowed - %s. Must be of untyped type.", entry->debug_str() ); \
|
|
|
|
return Code::Invalid;
|
|
|
|
}
|
|
|
|
|
|
|
|
result->add_entry( entry );
|
|
|
|
}
|
|
|
|
while ( num--, num > 0 );
|
|
|
|
va_end(va);
|
|
|
|
|
|
|
|
result.lock();
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
Code def_union_body( s32 num, Code* codes )
|
|
|
|
{
|
|
|
|
def_body_code_array_start( def_union_body );
|
|
|
|
|
|
|
|
Code
|
|
|
|
result = make_code();
|
|
|
|
result->Type = Union_Body;
|
|
|
|
|
|
|
|
do
|
|
|
|
{
|
|
|
|
Code entry = *codes;
|
|
|
|
|
|
|
|
if ( ! entry )
|
|
|
|
{
|
|
|
|
log_failure("gen::def_union_body: Provided a null entry");
|
|
|
|
return Code::Invalid;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ( entry->Type != Untyped )
|
|
|
|
{
|
|
|
|
log_failure("gen::def_union_body: Entry type is not allowed: %s", entry->debug_str() ); \
|
|
|
|
return Code::Invalid;
|
|
|
|
}
|
|
|
|
|
|
|
|
result->add_entry( entry );
|
|
|
|
}
|
|
|
|
while ( codes++, num--, num > 0 );
|
|
|
|
|
|
|
|
result.lock();
|
|
|
|
return result;
|
|
|
|
}
|
2023-04-03 23:04:19 -07:00
|
|
|
|
2023-04-22 19:24:55 -07:00
|
|
|
# undef name_check
|
|
|
|
# undef null_check
|
|
|
|
# undef null_or_invalid_check
|
2023-06-29 19:48:47 -07:00
|
|
|
#pragma endregion Upfront Constructors
|
2023-04-22 19:24:55 -07:00
|
|
|
|
|
|
|
#pragma region Parsing Constructors
|
|
|
|
#ifdef GEN_FEATURE_PARSING
|
2023-04-18 19:47:59 -07:00
|
|
|
/*
|
|
|
|
These constructors are the most implementation intensive other than the edtior or scanner.
|
|
|
|
*/
|
|
|
|
|
2023-04-22 19:24:55 -07:00
|
|
|
namespace Parser
|
|
|
|
{
|
2023-04-11 22:24:43 -07:00
|
|
|
|
2023-04-22 19:24:55 -07:00
|
|
|
/*
|
|
|
|
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.
|
|
|
|
*/
|
2023-04-18 19:47:59 -07:00
|
|
|
|
2023-04-22 19:24:55 -07:00
|
|
|
// Angle brackets not supported as they are used for template arguments outside of expressions
|
|
|
|
// Any angle brackets found will be considered an operator token.
|
|
|
|
|
|
|
|
# define Define_TokType \
|
2023-05-05 15:10:03 -07:00
|
|
|
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( BraceCurly_Open, "{" ) \
|
|
|
|
Entry( BraceCurly_Close, "}" ) \
|
|
|
|
Entry( BraceSquare_Open, "[" ) \
|
|
|
|
Entry( BraceSquare_Close, "]" ) \
|
|
|
|
Entry( Capture_Start, "(" ) \
|
|
|
|
Entry( Capture_End, ")" ) \
|
|
|
|
Entry( Comment, "__comment__" ) \
|
|
|
|
Entry( Char, "__char__" ) \
|
|
|
|
Entry( Comma, "," ) \
|
|
|
|
Entry( Decl_Class, "class" ) \
|
|
|
|
Entry( Decl_Enum, "enum" ) \
|
|
|
|
Entry( Decl_Friend, "friend" ) \
|
|
|
|
Entry( Decl_Module, "module" ) \
|
|
|
|
Entry( Decl_Namespace, "namespace" ) \
|
|
|
|
Entry( Decl_Struct, "struct" ) \
|
|
|
|
Entry( Decl_Typedef, "typedef" ) \
|
|
|
|
Entry( Decl_Using, "using" ) \
|
|
|
|
Entry( Decl_Union, "union" ) \
|
|
|
|
Entry( Identifier, "__SymID__" ) \
|
|
|
|
Entry( Module_Import, "import" ) \
|
|
|
|
Entry( Module_Export, "export" ) \
|
|
|
|
Entry( Number, "number" ) \
|
|
|
|
Entry( Operator, "operator" ) \
|
|
|
|
Entry( Spec_Alignas, "alignas" ) \
|
|
|
|
Entry( Spec_CLinkage, "extern \"C\"" ) \
|
|
|
|
Entry( Spec_Const, "const" ) \
|
|
|
|
Entry( Spec_Consteval, "consteval" ) \
|
|
|
|
Entry( Spec_Constexpr, "constexpr" ) \
|
|
|
|
Entry( Spec_Constinit, "constinit" ) \
|
|
|
|
Entry( Spec_Extern, "extern" ) \
|
|
|
|
Entry( Spec_Inline, "inline" ) \
|
|
|
|
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" ) \
|
2023-04-22 19:24:55 -07:00
|
|
|
Entry( Type_Long, "long" )
|
|
|
|
|
|
|
|
enum class TokType : u32
|
|
|
|
{
|
|
|
|
# define Entry( Name_, Str_ ) Name_,
|
|
|
|
Define_TokType
|
|
|
|
# undef Entry
|
2023-05-05 15:10:03 -07:00
|
|
|
Attr_Keyword,
|
2023-04-18 19:47:59 -07:00
|
|
|
|
2023-04-22 19:24:55 -07:00
|
|
|
Num,
|
|
|
|
Invalid
|
|
|
|
};
|
2023-04-18 19:47:59 -07:00
|
|
|
|
2023-04-22 19:24:55 -07:00
|
|
|
struct Token
|
2023-04-18 19:47:59 -07:00
|
|
|
{
|
2023-04-22 19:24:55 -07:00
|
|
|
char const* Text;
|
|
|
|
sptr Length;
|
|
|
|
TokType Type;
|
2023-04-23 11:33:37 -07:00
|
|
|
bool IsAssign;
|
2023-04-23 16:04:43 -07:00
|
|
|
|
|
|
|
operator bool()
|
|
|
|
{
|
|
|
|
return Text && Length && Type != TokType::Invalid;
|
|
|
|
}
|
2023-05-06 13:15:49 -07:00
|
|
|
|
|
|
|
operator StrC()
|
|
|
|
{
|
|
|
|
return { Length, Text };
|
|
|
|
}
|
2023-04-18 19:47:59 -07:00
|
|
|
};
|
|
|
|
|
2023-05-05 15:10:03 -07:00
|
|
|
inline
|
2023-04-22 19:24:55 -07:00
|
|
|
TokType get_tok_type( char const* word, s32 length )
|
2023-04-18 19:47:59 -07:00
|
|
|
{
|
2023-04-22 19:24:55 -07:00
|
|
|
local_persist
|
|
|
|
char const* lookup[(u32)TokType::Num] =
|
|
|
|
{
|
|
|
|
# define Entry( Name_, Str_ ) Str_,
|
|
|
|
Define_TokType
|
|
|
|
# undef Entry
|
2023-05-05 15:10:03 -07:00
|
|
|
|
|
|
|
Attribute::Keyword,
|
2023-04-22 19:24:55 -07:00
|
|
|
};
|
2023-04-18 19:47:59 -07:00
|
|
|
|
2023-04-22 19:24:55 -07:00
|
|
|
for ( u32 index = 0; index < (u32)TokType::Num; index++ )
|
2023-04-18 19:47:59 -07:00
|
|
|
{
|
2023-04-22 19:24:55 -07:00
|
|
|
if ( str_compare( word, lookup[index], length ) == 0 )
|
|
|
|
return scast(TokType, index);
|
2023-04-18 19:47:59 -07:00
|
|
|
}
|
|
|
|
|
2023-04-22 19:24:55 -07:00
|
|
|
return TokType::Invalid;
|
|
|
|
}
|
2023-04-18 19:47:59 -07:00
|
|
|
|
2023-05-05 15:10:03 -07:00
|
|
|
inline
|
2023-04-23 11:33:37 -07:00
|
|
|
char const* str_tok_type( TokType type )
|
2023-04-22 19:24:55 -07:00
|
|
|
{
|
|
|
|
local_persist
|
|
|
|
char const* lookup[(u32)TokType::Num] =
|
|
|
|
{
|
|
|
|
# define Entry( Name_, Str_ ) Str_,
|
|
|
|
Define_TokType
|
|
|
|
# undef Entry
|
2023-05-05 15:10:03 -07:00
|
|
|
|
|
|
|
Attribute::Keyword,
|
2023-04-22 19:24:55 -07:00
|
|
|
};
|
2023-04-18 19:47:59 -07:00
|
|
|
|
2023-04-22 19:24:55 -07:00
|
|
|
return lookup[(u32)type];
|
|
|
|
}
|
2023-04-18 19:47:59 -07:00
|
|
|
|
2023-04-23 11:33:37 -07:00
|
|
|
# undef Define_TokType
|
|
|
|
|
2023-04-22 19:24:55 -07:00
|
|
|
inline
|
|
|
|
bool tok_is_specifier( Token const& tok )
|
2023-04-18 19:47:59 -07:00
|
|
|
{
|
2023-05-05 15:10:03 -07:00
|
|
|
return tok.Type <= TokType::Star
|
|
|
|
|| tok.Type >= TokType::Spec_Alignas
|
2023-04-22 21:43:31 -07:00
|
|
|
|| tok.Type == TokType::Ampersand
|
|
|
|
|| tok.Type == TokType::Ampersand_DBL
|
|
|
|
;
|
2023-04-18 19:47:59 -07:00
|
|
|
}
|
|
|
|
|
2023-05-01 11:12:07 -07:00
|
|
|
inline
|
|
|
|
bool tok_is_access_specifier( Token const& tok )
|
|
|
|
{
|
|
|
|
return tok.Type >= TokType::Access_Private && tok.Type <= TokType::Access_Public;
|
|
|
|
}
|
|
|
|
|
|
|
|
inline
|
|
|
|
AccessSpec tok_to_access_specifier( Token const& tok )
|
|
|
|
{
|
|
|
|
return scast(AccessSpec, tok.Type);
|
|
|
|
}
|
|
|
|
|
2023-04-22 19:24:55 -07:00
|
|
|
Arena LexAllocator;
|
2023-04-18 19:47:59 -07:00
|
|
|
|
2023-04-22 19:24:55 -07:00
|
|
|
struct TokArray
|
2023-04-18 19:47:59 -07:00
|
|
|
{
|
2023-04-22 19:24:55 -07:00
|
|
|
Array(Token) Arr;
|
2023-05-01 11:12:07 -07:00
|
|
|
s32 Idx;
|
2023-04-18 19:47:59 -07:00
|
|
|
|
2023-04-22 19:24:55 -07:00
|
|
|
inline
|
|
|
|
bool __eat( TokType type, char const* context )
|
2023-04-18 19:47:59 -07:00
|
|
|
{
|
2023-04-23 11:33:37 -07:00
|
|
|
if ( array_count(Arr) - Idx <= 0 )
|
|
|
|
{
|
|
|
|
log_failure( "gen::%s: No tokens left", context );
|
|
|
|
return Code::Invalid;
|
|
|
|
}
|
|
|
|
|
2023-04-22 19:24:55 -07:00
|
|
|
if ( Arr[0].Type != type )
|
|
|
|
{
|
2023-05-06 12:49:43 -07:00
|
|
|
String token_str = String::make( g_allocator, { Arr[Idx].Length, Arr[Idx].Text } );
|
2023-04-23 11:33:37 -07:00
|
|
|
|
|
|
|
log_failure( "gen::%s: expected %s, got %s", context, str_tok_type(type), str_tok_type(Arr[Idx].Type) );
|
|
|
|
|
2023-04-22 19:24:55 -07:00
|
|
|
return Code::Invalid;
|
|
|
|
}
|
2023-04-18 19:47:59 -07:00
|
|
|
|
2023-04-22 19:24:55 -07:00
|
|
|
Idx++;
|
|
|
|
}
|
|
|
|
};
|
2023-04-18 19:47:59 -07:00
|
|
|
|
2023-05-06 13:15:49 -07:00
|
|
|
TokArray lex( StrC content )
|
2023-04-22 19:24:55 -07:00
|
|
|
{
|
|
|
|
# define current ( * scanner )
|
2023-04-18 19:47:59 -07:00
|
|
|
|
2023-04-22 19:24:55 -07:00
|
|
|
# define move_forward() \
|
|
|
|
left--; \
|
|
|
|
scanner++
|
2023-04-18 19:47:59 -07:00
|
|
|
|
2023-04-22 19:24:55 -07:00
|
|
|
# define SkipWhitespace() \
|
|
|
|
while ( left && char_is_space( current ) ) \
|
|
|
|
{ \
|
|
|
|
move_forward(); \
|
|
|
|
}
|
2023-04-18 19:47:59 -07:00
|
|
|
|
2023-04-22 19:24:55 -07:00
|
|
|
# define SkipWhitespace_Checked( Context_, Msg_, ... ) \
|
|
|
|
while ( left && char_is_space( current ) ) \
|
|
|
|
{ \
|
|
|
|
move_forward(); \
|
|
|
|
} \
|
|
|
|
if ( left <= 0 ) \
|
|
|
|
{ \
|
|
|
|
log_failure( "gen::" txt(Context_) ": " Msg_, __VA_ARGS__ ); \
|
|
|
|
return { 0, nullptr }; \
|
|
|
|
}
|
2023-04-18 19:47:59 -07:00
|
|
|
|
2023-04-22 19:24:55 -07:00
|
|
|
do_once_start
|
|
|
|
arena_init_from_allocator( & LexAllocator, heap(), megabytes(10) );
|
2023-04-18 19:47:59 -07:00
|
|
|
|
2023-04-22 19:24:55 -07:00
|
|
|
if ( LexAllocator.physical_start == nullptr )
|
|
|
|
{
|
|
|
|
log_failure( "gen::lex: failed to allocate memory for parsing constructor's lexer");
|
2023-05-01 11:12:07 -07:00
|
|
|
return { nullptr, 0 };
|
2023-04-22 19:24:55 -07:00
|
|
|
}
|
|
|
|
do_once_end
|
2023-04-18 19:47:59 -07:00
|
|
|
|
2023-04-22 19:24:55 -07:00
|
|
|
local_persist thread_local
|
|
|
|
Array(Token) Tokens = nullptr;
|
2023-04-18 19:47:59 -07:00
|
|
|
|
2023-05-06 13:15:49 -07:00
|
|
|
s32 left = content.Len;
|
|
|
|
char const* scanner = content.Ptr;
|
2023-04-18 19:47:59 -07:00
|
|
|
|
2023-04-22 19:24:55 -07:00
|
|
|
char const* word = scanner;
|
|
|
|
s32 word_length = 0;
|
2023-04-18 19:47:59 -07:00
|
|
|
|
2023-04-22 19:24:55 -07:00
|
|
|
SkipWhitespace();
|
|
|
|
if ( left <= 0 )
|
|
|
|
{
|
|
|
|
log_failure( "gen::lex: no tokens found (only whitespace provided)" );
|
2023-05-01 11:12:07 -07:00
|
|
|
return { nullptr, 0 };
|
2023-04-22 19:24:55 -07:00
|
|
|
}
|
2023-04-18 19:47:59 -07:00
|
|
|
|
2023-04-22 19:24:55 -07:00
|
|
|
if ( Tokens )
|
|
|
|
array_clear( Tokens );
|
2023-04-18 19:47:59 -07:00
|
|
|
|
2023-05-06 13:15:49 -07:00
|
|
|
array_init_reserve( Tokens, arena_allocator( & LexAllocator), content.Len / 8 );
|
2023-04-18 19:47:59 -07:00
|
|
|
|
2023-04-22 19:24:55 -07:00
|
|
|
while (left )
|
|
|
|
{
|
2023-04-23 11:33:37 -07:00
|
|
|
Token token = { nullptr, 0, TokType::Invalid, false };
|
2023-04-18 19:47:59 -07:00
|
|
|
|
2023-04-22 19:24:55 -07:00
|
|
|
switch ( current )
|
|
|
|
{
|
|
|
|
case '.':
|
|
|
|
token.Text = scanner;
|
|
|
|
token.Length = 1;
|
|
|
|
token.Type = TokType::Access_MemberSymbol;
|
2023-04-18 19:47:59 -07:00
|
|
|
|
2023-04-22 19:24:55 -07:00
|
|
|
if (left)
|
2023-04-18 19:47:59 -07:00
|
|
|
move_forward();
|
2023-04-22 19:24:55 -07:00
|
|
|
goto FoundToken;
|
2023-04-18 19:47:59 -07:00
|
|
|
|
2023-04-22 19:24:55 -07:00
|
|
|
case '&' :
|
|
|
|
token.Text = scanner;
|
|
|
|
token.Length = 1;
|
|
|
|
token.Type = TokType::Ampersand;
|
2023-04-18 19:47:59 -07:00
|
|
|
|
2023-04-22 19:24:55 -07:00
|
|
|
if (left)
|
|
|
|
move_forward();
|
2023-04-18 19:47:59 -07:00
|
|
|
|
2023-04-22 19:24:55 -07:00
|
|
|
if ( current == '&' ) // &&
|
|
|
|
{
|
|
|
|
token.Length = 2;
|
|
|
|
token.Type = TokType::Ampersand_DBL;
|
2023-04-18 19:47:59 -07:00
|
|
|
|
2023-04-22 19:24:55 -07:00
|
|
|
if (left)
|
|
|
|
move_forward();
|
|
|
|
}
|
2023-04-18 19:47:59 -07:00
|
|
|
|
2023-04-22 19:24:55 -07:00
|
|
|
goto FoundToken;
|
2023-04-18 19:47:59 -07:00
|
|
|
|
2023-04-22 19:24:55 -07:00
|
|
|
case ':':
|
|
|
|
token.Text = scanner;
|
|
|
|
token.Length = 1;
|
|
|
|
token.Type = TokType::Assign_Classifer;
|
2023-04-18 19:47:59 -07:00
|
|
|
|
2023-04-22 19:24:55 -07:00
|
|
|
if (left)
|
|
|
|
move_forward();
|
2023-04-18 19:47:59 -07:00
|
|
|
|
2023-04-22 19:24:55 -07:00
|
|
|
if ( current == ':' )
|
2023-04-18 19:47:59 -07:00
|
|
|
{
|
|
|
|
move_forward();
|
2023-04-22 19:24:55 -07:00
|
|
|
token.Type = TokType::Access_StaticSymbol;
|
|
|
|
token.Length++;
|
2023-04-18 19:47:59 -07:00
|
|
|
}
|
2023-04-22 19:24:55 -07:00
|
|
|
goto FoundToken;
|
|
|
|
|
|
|
|
case '{':
|
|
|
|
token.Text = scanner;
|
|
|
|
token.Length = 1;
|
|
|
|
token.Type = TokType::BraceCurly_Open;
|
|
|
|
goto FoundToken;
|
|
|
|
|
|
|
|
case '}':
|
|
|
|
token.Text = scanner;
|
|
|
|
token.Length = 1;
|
|
|
|
token.Type = TokType::BraceCurly_Close;
|
|
|
|
goto FoundToken;
|
|
|
|
|
|
|
|
case '[':
|
|
|
|
token.Text = scanner;
|
|
|
|
token.Length = 1;
|
|
|
|
token.Type = TokType::BraceSquare_Open;
|
|
|
|
if ( left )
|
|
|
|
{
|
|
|
|
move_forward();
|
2023-04-18 19:47:59 -07:00
|
|
|
|
2023-04-22 19:24:55 -07:00
|
|
|
if ( current == ']' )
|
|
|
|
{
|
|
|
|
token.Length = 2;
|
|
|
|
token.Type = TokType::Operator;
|
|
|
|
move_forward();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
goto FoundToken;
|
|
|
|
|
|
|
|
case ']':
|
|
|
|
token.Text = scanner;
|
|
|
|
token.Length = 1;
|
|
|
|
token.Type = TokType::BraceSquare_Close;
|
|
|
|
goto FoundToken;
|
|
|
|
|
|
|
|
case '(':
|
|
|
|
token.Text = scanner;
|
|
|
|
token.Length = 1;
|
|
|
|
token.Type = TokType::Capture_Start;
|
|
|
|
if ( left )
|
2023-04-18 19:47:59 -07:00
|
|
|
{
|
|
|
|
move_forward();
|
|
|
|
|
2023-04-22 19:24:55 -07:00
|
|
|
if ( current == ')' )
|
2023-04-18 19:47:59 -07:00
|
|
|
{
|
2023-04-22 19:24:55 -07:00
|
|
|
token.Length = 2;
|
|
|
|
token.Type = TokType::Operator;
|
2023-04-18 19:47:59 -07:00
|
|
|
move_forward();
|
|
|
|
}
|
|
|
|
}
|
2023-04-22 19:24:55 -07:00
|
|
|
goto FoundToken;
|
2023-04-18 19:47:59 -07:00
|
|
|
|
2023-04-22 19:24:55 -07:00
|
|
|
case ')':
|
|
|
|
token.Text = scanner;
|
|
|
|
token.Length = 1;
|
|
|
|
token.Type = TokType::Capture_End;
|
|
|
|
goto FoundToken;
|
2023-04-18 19:47:59 -07:00
|
|
|
|
2023-04-22 19:24:55 -07:00
|
|
|
case '\'':
|
|
|
|
token.Text = scanner;
|
|
|
|
token.Length = 1;
|
|
|
|
token.Type = TokType::Char;
|
2023-04-18 19:47:59 -07:00
|
|
|
|
|
|
|
move_forward();
|
|
|
|
|
2023-04-22 19:24:55 -07:00
|
|
|
while ( left && current != '\'' )
|
|
|
|
{
|
2023-04-18 19:47:59 -07:00
|
|
|
move_forward();
|
2023-04-22 19:24:55 -07:00
|
|
|
token.Length++;
|
|
|
|
}
|
2023-04-18 19:47:59 -07:00
|
|
|
|
2023-04-22 19:24:55 -07:00
|
|
|
if ( left )
|
|
|
|
{
|
2023-04-18 19:47:59 -07:00
|
|
|
move_forward();
|
2023-04-22 19:24:55 -07:00
|
|
|
token.Length++;
|
|
|
|
}
|
|
|
|
goto FoundToken;
|
|
|
|
|
|
|
|
case ',':
|
|
|
|
token.Text = scanner;
|
|
|
|
token.Length = 1;
|
|
|
|
token.Type = TokType::Comma;
|
|
|
|
goto FoundToken;
|
|
|
|
|
|
|
|
case '*':
|
|
|
|
token.Text = scanner;
|
|
|
|
token.Length = 1;
|
|
|
|
token.Type = TokType::Star;
|
|
|
|
goto FoundToken;
|
|
|
|
|
|
|
|
case ';':
|
|
|
|
token.Text = scanner;
|
|
|
|
token.Length = 1;
|
|
|
|
token.Type = TokType::Statement_End;
|
|
|
|
goto FoundToken;
|
|
|
|
|
|
|
|
case '"':
|
|
|
|
token.Text = scanner;
|
|
|
|
token.Length = 1;
|
|
|
|
token.Type = TokType::String;
|
2023-04-18 19:47:59 -07:00
|
|
|
|
|
|
|
move_forward();
|
2023-04-22 19:24:55 -07:00
|
|
|
while ( left )
|
2023-04-18 19:47:59 -07:00
|
|
|
{
|
2023-04-22 19:24:55 -07:00
|
|
|
if ( current == '"' )
|
|
|
|
{
|
|
|
|
move_forward();
|
|
|
|
break;
|
|
|
|
}
|
2023-04-18 19:47:59 -07:00
|
|
|
|
2023-04-22 19:24:55 -07:00
|
|
|
if ( current == '\\' )
|
2023-04-18 19:47:59 -07:00
|
|
|
{
|
|
|
|
move_forward();
|
2023-04-22 19:24:55 -07:00
|
|
|
token.Length++;
|
|
|
|
|
|
|
|
if ( left )
|
|
|
|
{
|
|
|
|
move_forward();
|
|
|
|
token.Length++;
|
|
|
|
}
|
|
|
|
continue;
|
2023-04-18 19:47:59 -07:00
|
|
|
}
|
2023-04-22 19:24:55 -07:00
|
|
|
|
|
|
|
move_forward();
|
|
|
|
token.Length++;
|
2023-04-18 19:47:59 -07:00
|
|
|
}
|
2023-04-22 19:24:55 -07:00
|
|
|
goto FoundToken;
|
|
|
|
|
|
|
|
// All other operators we just label as an operator and move forward.
|
2023-04-23 11:33:37 -07:00
|
|
|
case '=':
|
|
|
|
token.Text = scanner;
|
|
|
|
token.Length = 1;
|
|
|
|
token.Type = TokType::Operator;
|
|
|
|
token.IsAssign = true;
|
|
|
|
|
|
|
|
if (left)
|
|
|
|
move_forward();
|
|
|
|
|
|
|
|
goto FoundToken;
|
|
|
|
|
2023-04-22 19:24:55 -07:00
|
|
|
case '+':
|
|
|
|
case '%':
|
|
|
|
case '^':
|
|
|
|
case '~':
|
|
|
|
case '!':
|
|
|
|
case '<':
|
|
|
|
case '>':
|
|
|
|
case '|':
|
|
|
|
token.Text = scanner;
|
|
|
|
token.Length = 1;
|
|
|
|
token.Type = TokType::Operator;
|
|
|
|
|
|
|
|
if (left)
|
|
|
|
move_forward();
|
|
|
|
|
|
|
|
if ( current == '=' )
|
2023-04-18 19:47:59 -07:00
|
|
|
{
|
|
|
|
token.Length++;
|
2023-04-23 11:33:37 -07:00
|
|
|
token.IsAssign = true;
|
2023-04-18 19:47:59 -07:00
|
|
|
|
|
|
|
if (left)
|
|
|
|
move_forward();
|
|
|
|
}
|
2023-05-06 13:15:49 -07:00
|
|
|
else while ( left && current == *(scanner - 1) && token.Length < 3 )
|
2023-04-18 19:47:59 -07:00
|
|
|
{
|
|
|
|
token.Length++;
|
|
|
|
|
|
|
|
if (left)
|
|
|
|
move_forward();
|
|
|
|
}
|
2023-04-22 19:24:55 -07:00
|
|
|
goto FoundToken;
|
|
|
|
|
|
|
|
// Dash is unfortunatlly a bit more complicated...
|
|
|
|
case '-':
|
|
|
|
token.Text = scanner;
|
|
|
|
token.Length = 1;
|
|
|
|
token.Type = TokType::Operator;
|
|
|
|
if ( left )
|
2023-04-18 19:47:59 -07:00
|
|
|
{
|
2023-04-22 19:24:55 -07:00
|
|
|
move_forward();
|
2023-04-18 19:47:59 -07:00
|
|
|
|
2023-04-22 19:24:55 -07:00
|
|
|
if ( current == '>' )
|
2023-04-18 19:47:59 -07:00
|
|
|
{
|
2023-04-22 19:24:55 -07:00
|
|
|
token.Length++;
|
2023-04-18 19:47:59 -07:00
|
|
|
move_forward();
|
2023-04-22 19:24:55 -07:00
|
|
|
|
|
|
|
if ( current == '*' )
|
|
|
|
{
|
|
|
|
token.Length++;
|
|
|
|
move_forward();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else if ( current == '=' )
|
|
|
|
{
|
2023-04-18 19:47:59 -07:00
|
|
|
token.Length++;
|
2023-04-23 11:33:37 -07:00
|
|
|
token.IsAssign = true;
|
2023-04-22 19:24:55 -07:00
|
|
|
|
|
|
|
if (left)
|
|
|
|
move_forward();
|
|
|
|
}
|
2023-05-06 13:15:49 -07:00
|
|
|
else while ( left && current == *(scanner - 1) && token.Length < 3 )
|
2023-04-22 19:24:55 -07:00
|
|
|
{
|
|
|
|
token.Length++;
|
|
|
|
|
|
|
|
if (left)
|
|
|
|
move_forward();
|
2023-04-18 19:47:59 -07:00
|
|
|
}
|
|
|
|
}
|
2023-04-22 19:24:55 -07:00
|
|
|
goto FoundToken;
|
|
|
|
|
|
|
|
case '/':
|
|
|
|
token.Text = scanner;
|
|
|
|
token.Length = 1;
|
|
|
|
token.Type = TokType::Operator;
|
|
|
|
|
|
|
|
if ( left )
|
2023-04-18 19:47:59 -07:00
|
|
|
{
|
2023-04-22 19:24:55 -07:00
|
|
|
move_forward();
|
2023-04-18 19:47:59 -07:00
|
|
|
|
2023-04-22 19:24:55 -07:00
|
|
|
if ( current == '/' )
|
2023-04-18 19:47:59 -07:00
|
|
|
{
|
2023-04-22 19:24:55 -07:00
|
|
|
token.Type = TokType::Comment;
|
|
|
|
|
2023-05-01 11:12:07 -07:00
|
|
|
move_forward();
|
|
|
|
token.Text = scanner;
|
|
|
|
token.Length = 0;
|
|
|
|
|
2023-04-22 19:24:55 -07:00
|
|
|
while ( left && current != '\n' )
|
|
|
|
{
|
|
|
|
move_forward();
|
|
|
|
token.Length++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else if ( current == '*' )
|
|
|
|
{
|
|
|
|
token.Type = TokType::Comment;
|
|
|
|
|
2023-05-01 11:12:07 -07:00
|
|
|
move_forward();
|
|
|
|
token.Text = scanner;
|
|
|
|
token.Length = 0;
|
|
|
|
|
|
|
|
while ( left && ( current != '*' && *(scanner + 1) != '/' ) )
|
2023-04-22 19:24:55 -07:00
|
|
|
{
|
|
|
|
move_forward();
|
|
|
|
token.Length++;
|
|
|
|
}
|
|
|
|
move_forward();
|
2023-04-18 19:47:59 -07:00
|
|
|
move_forward();
|
|
|
|
}
|
|
|
|
}
|
2023-04-22 19:24:55 -07:00
|
|
|
goto FoundToken;
|
|
|
|
}
|
2023-04-18 19:47:59 -07:00
|
|
|
|
2023-04-22 19:24:55 -07:00
|
|
|
SkipWhitespace();
|
|
|
|
if ( left <= 0 )
|
|
|
|
break;
|
2023-04-18 19:47:59 -07:00
|
|
|
|
2023-04-22 19:24:55 -07:00
|
|
|
if ( char_is_alpha( current ) || current == '_' )
|
2023-04-18 19:47:59 -07:00
|
|
|
{
|
2023-04-22 19:24:55 -07:00
|
|
|
token.Text = scanner;
|
|
|
|
token.Length = 1;
|
2023-04-18 19:47:59 -07:00
|
|
|
move_forward();
|
|
|
|
|
2023-04-22 19:24:55 -07:00
|
|
|
while ( left && ( char_is_alphanumeric(current) || current == '_' ) )
|
|
|
|
{
|
|
|
|
move_forward();
|
|
|
|
token.Length++;
|
|
|
|
}
|
2023-04-18 19:47:59 -07:00
|
|
|
|
2023-04-22 19:24:55 -07:00
|
|
|
goto FoundToken;
|
|
|
|
}
|
|
|
|
else
|
2023-04-18 19:47:59 -07:00
|
|
|
{
|
2023-05-06 12:49:43 -07:00
|
|
|
String context_str = String::fmt_buf( g_allocator, "%s", scanner, min( 100, left ) );
|
2023-04-22 19:24:55 -07:00
|
|
|
|
|
|
|
log_failure( "Failed to lex token %s", context_str );
|
|
|
|
|
|
|
|
// Skip to next whitespace since we can't know if anything else is valid until then.
|
|
|
|
while ( left && ! char_is_space( current ) )
|
|
|
|
{
|
|
|
|
move_forward();
|
|
|
|
}
|
2023-04-18 19:47:59 -07:00
|
|
|
}
|
|
|
|
|
2023-04-22 19:24:55 -07:00
|
|
|
FoundToken:
|
2023-04-18 19:47:59 -07:00
|
|
|
|
2023-04-22 19:24:55 -07:00
|
|
|
if ( token.Type != TokType::Invalid )
|
|
|
|
{
|
|
|
|
array_append( Tokens, token );
|
|
|
|
continue;
|
|
|
|
}
|
2023-04-18 19:47:59 -07:00
|
|
|
|
2023-04-22 19:24:55 -07:00
|
|
|
TokType type = get_tok_type( token.Text, token.Length );
|
2023-04-18 19:47:59 -07:00
|
|
|
|
2023-04-22 19:24:55 -07:00
|
|
|
if ( type != TokType::Invalid )
|
|
|
|
{
|
|
|
|
token.Type = type;
|
|
|
|
array_append( Tokens, token );
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
// Its most likely an identifier...
|
2023-04-18 19:47:59 -07:00
|
|
|
|
|
|
|
|
2023-05-06 12:49:43 -07:00
|
|
|
String tok_str = String::fmt_buf( g_allocator, "%s", token.Text, token.Length );
|
2023-04-18 19:47:59 -07:00
|
|
|
|
2023-04-22 19:24:55 -07:00
|
|
|
log_failure( "Failed to lex token %s", tok_str );
|
2023-04-18 19:47:59 -07:00
|
|
|
|
2023-04-22 19:24:55 -07:00
|
|
|
// Skip to next whitespace since we can't know if anything else is valid until then.
|
|
|
|
while ( left && ! char_is_space( current ) )
|
|
|
|
{
|
|
|
|
move_forward();
|
|
|
|
}
|
2023-04-18 19:47:59 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-04-23 11:33:37 -07:00
|
|
|
if ( array_count(Tokens) == 0 )
|
|
|
|
{
|
|
|
|
log_failure( "Failed to lex any tokens" );
|
2023-05-01 11:12:07 -07:00
|
|
|
return { nullptr, 0 };
|
2023-04-23 11:33:37 -07:00
|
|
|
}
|
|
|
|
|
2023-05-01 11:12:07 -07:00
|
|
|
return { Tokens, 0 };
|
2023-04-22 21:43:31 -07:00
|
|
|
# undef current
|
|
|
|
# undef move_forward
|
|
|
|
# undef SkipWhitespace
|
|
|
|
# undef SkipWhitespace_Checked
|
2023-04-22 19:24:55 -07:00
|
|
|
}
|
2023-04-18 19:47:59 -07:00
|
|
|
}
|
2023-04-22 19:24:55 -07:00
|
|
|
|
|
|
|
#pragma region Helper Macros
|
2023-05-06 13:15:49 -07:00
|
|
|
# define check_parse_args( func, def ) \
|
|
|
|
if ( def.Len <= 0 ) \
|
2023-04-22 19:24:55 -07:00
|
|
|
{ \
|
|
|
|
log_failure( "gen::" txt(func) ": length must greater than 0" ); \
|
|
|
|
return Code::Invalid; \
|
|
|
|
} \
|
2023-05-06 13:15:49 -07:00
|
|
|
if ( def.Ptr == nullptr ) \
|
2023-04-22 19:24:55 -07:00
|
|
|
{ \
|
|
|
|
log_failure( "gen::" txt(func) ": def was null" ); \
|
|
|
|
return Code::Invalid; \
|
|
|
|
}
|
|
|
|
|
|
|
|
# define currtok toks.Arr[toks.Idx]
|
2023-04-23 19:53:30 -07:00
|
|
|
# define eat( Type_ ) toks.__eat( Type_, context )
|
2023-04-22 19:24:55 -07:00
|
|
|
# define left array_count(toks.Arr) - toks.Idx
|
2023-04-23 11:33:37 -07:00
|
|
|
|
|
|
|
# define check( Type_ ) left && currtok.Type == Type_
|
2023-04-22 19:24:55 -07:00
|
|
|
#pragma endregion Helper Macros
|
2023-05-01 11:12:07 -07:00
|
|
|
|
2023-05-05 15:10:03 -07:00
|
|
|
Code parse_function_body( Parser::TokArray& toks, char const* context );
|
|
|
|
Code parse_global_nspace( Parser::TokArray& toks, char const* context );
|
|
|
|
|
|
|
|
Code parse_class ( Parser::TokArray& toks, char const* context );
|
|
|
|
Code parse_enum ( Parser::TokArray& toks, char const* context );
|
|
|
|
Code parse_export_body ( Parser::TokArray& toks, char const* context );
|
|
|
|
Code parse_exten_link ( Parser::TokArray& toks, char const* context );
|
|
|
|
Code parse_friend ( Parser::TokArray& toks, char const* context );
|
|
|
|
Code parse_function ( Parser::TokArray& toks, char const* context );
|
|
|
|
Code parse_struct ( Parser::TokArray& toks, char const* context );
|
|
|
|
Code parse_variable ( Parser::TokArray& toks, char const* context );
|
|
|
|
Code parse_type ( Parser::TokArray& toks, char const* context );
|
|
|
|
Code parse_typedef ( Parser::TokArray& toks, char const* context );
|
|
|
|
Code parse_union ( Parser::TokArray& toks, char const* context );
|
|
|
|
Code parse_using ( Parser::TokArray& toks, char const* context );
|
2023-04-23 16:04:43 -07:00
|
|
|
|
|
|
|
inline
|
2023-05-01 11:12:07 -07:00
|
|
|
Code parse_array_decl( Parser::TokArray& toks, char const* context )
|
2023-04-23 16:04:43 -07:00
|
|
|
{
|
|
|
|
using namespace Parser;
|
|
|
|
|
|
|
|
if ( check( TokType::BraceSquare_Open ) )
|
|
|
|
{
|
|
|
|
eat( TokType::BraceSquare_Open );
|
|
|
|
|
|
|
|
if ( left == 0 )
|
|
|
|
{
|
|
|
|
log_failure( "%s: Error, unexpected end of typedef definition ( '[]' scope started )", txt(parse_typedef) );
|
|
|
|
return Code::Invalid;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ( currtok.Type == TokType::BraceSquare_Close )
|
|
|
|
{
|
|
|
|
log_failure( "%s: Error, empty array expression in typedef definition", txt(parse_typedef) );
|
|
|
|
return Code::Invalid;
|
|
|
|
}
|
|
|
|
|
|
|
|
Token untyped_tok = currtok;
|
|
|
|
|
|
|
|
while ( left && currtok.Type != TokType::BraceSquare_Close )
|
|
|
|
{
|
|
|
|
untyped_tok.Length = ( (sptr)currtok.Text + currtok.Length ) - (sptr)untyped_tok.Text;
|
|
|
|
}
|
|
|
|
|
2023-05-06 13:15:49 -07:00
|
|
|
Code array_expr = untyped_str( untyped_tok );
|
2023-04-23 16:04:43 -07:00
|
|
|
|
|
|
|
if ( left == 0 )
|
|
|
|
{
|
|
|
|
log_failure( "%s: Error, unexpected end of type definition, expected ]", txt(parse_typedef) );
|
|
|
|
return Code::Invalid;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ( currtok.Type != TokType::BraceSquare_Close )
|
|
|
|
{
|
|
|
|
log_failure( "%s: Error, expected ] in type definition, not %s", txt(parse_typedef), str_tok_type( currtok.Type ) );
|
|
|
|
return Code::Invalid;
|
|
|
|
}
|
|
|
|
|
|
|
|
eat( TokType::BraceSquare_Close );
|
|
|
|
return array_expr;
|
|
|
|
}
|
|
|
|
|
|
|
|
return Code::Invalid;
|
|
|
|
}
|
|
|
|
|
|
|
|
inline
|
2023-05-01 11:12:07 -07:00
|
|
|
Parser::Token parse_identifier( Parser::TokArray& toks, char const* context )
|
2023-04-23 16:04:43 -07:00
|
|
|
{
|
|
|
|
using namespace Parser;
|
|
|
|
Token name = currtok;
|
|
|
|
|
|
|
|
eat( TokType::Identifier );
|
|
|
|
|
|
|
|
while ( left && currtok.Type == TokType::Access_StaticSymbol )
|
|
|
|
{
|
|
|
|
eat( TokType::Access_StaticSymbol );
|
|
|
|
|
|
|
|
if ( left == 0 )
|
|
|
|
{
|
2023-05-01 11:12:07 -07:00
|
|
|
log_failure( "%s: Error, unexpected end of type definition, expected identifier", context );
|
2023-04-23 16:04:43 -07:00
|
|
|
return { nullptr, 0, TokType::Invalid };
|
|
|
|
}
|
|
|
|
|
|
|
|
if ( currtok.Type != TokType::Identifier )
|
|
|
|
{
|
2023-05-01 11:12:07 -07:00
|
|
|
log_failure( "%s: Error, expected identifier in type definition, not %s", context, str_tok_type( currtok.Type ) );
|
2023-04-23 16:04:43 -07:00
|
|
|
return { nullptr, 0, TokType::Invalid };
|
|
|
|
}
|
|
|
|
|
|
|
|
name.Length = ( (sptr)currtok.Text + currtok.Length ) - (sptr)name.Text;
|
|
|
|
eat( TokType::Identifier );
|
|
|
|
}
|
|
|
|
|
|
|
|
return name;
|
|
|
|
}
|
|
|
|
|
|
|
|
Code parse_params( Parser::TokArray& toks, char const* context )
|
|
|
|
{
|
|
|
|
using namespace Parser;
|
|
|
|
using namespace ECode;
|
|
|
|
|
|
|
|
eat( TokType::Capture_Start );
|
|
|
|
|
|
|
|
if ( check(TokType::Capture_End) )
|
|
|
|
{
|
|
|
|
eat( TokType::Capture_End );
|
|
|
|
return { nullptr };
|
|
|
|
}
|
|
|
|
|
|
|
|
Code
|
|
|
|
result = make_code();
|
|
|
|
result->Type = Parameters;
|
|
|
|
|
|
|
|
while ( left && currtok.Type != TokType::Capture_End)
|
|
|
|
{
|
|
|
|
Code type = { nullptr };
|
|
|
|
Code value = { nullptr };
|
|
|
|
|
|
|
|
type = parse_type( toks, context );
|
|
|
|
if ( type == Code::Invalid )
|
|
|
|
return Code::Invalid;
|
|
|
|
|
|
|
|
Token name = currtok;
|
|
|
|
eat( TokType::Identifier );
|
|
|
|
|
|
|
|
if ( currtok.IsAssign )
|
|
|
|
{
|
|
|
|
eat( TokType::Operator );
|
|
|
|
|
|
|
|
Token value_tok = currtok;
|
|
|
|
|
|
|
|
if ( currtok.Type == TokType::Statement_End )
|
|
|
|
{
|
|
|
|
log_failure( "gen::%s: Expected value after assignment operator", context );
|
|
|
|
return Code::Invalid;
|
|
|
|
}
|
|
|
|
|
|
|
|
while ( left && currtok.Type != TokType::Statement_End )
|
|
|
|
{
|
|
|
|
value_tok.Length = ( (sptr)currtok.Text + currtok.Length ) - (sptr)value_tok.Text;
|
|
|
|
eat( currtok.Type );
|
|
|
|
}
|
|
|
|
|
|
|
|
value = parse_type( toks, context );
|
|
|
|
}
|
|
|
|
|
|
|
|
Code
|
|
|
|
param = make_code();
|
|
|
|
param->Type = Parameters;
|
2023-05-06 13:15:49 -07:00
|
|
|
param->Name = get_cached_string( name );
|
2023-04-23 16:04:43 -07:00
|
|
|
|
|
|
|
param->add_entry( type );
|
|
|
|
|
|
|
|
if ( value )
|
|
|
|
param->add_entry( value );
|
|
|
|
|
|
|
|
param.lock();
|
|
|
|
|
|
|
|
result->add_entry( param );
|
|
|
|
eat( TokType::Comma );
|
|
|
|
}
|
|
|
|
eat( TokType::Capture_End );
|
|
|
|
|
|
|
|
result.lock();
|
|
|
|
return result;
|
|
|
|
# undef context
|
|
|
|
}
|
|
|
|
|
2023-05-01 11:12:07 -07:00
|
|
|
Code parse_variable_assignment( Parser::TokArray& toks, char const* context )
|
|
|
|
{
|
|
|
|
using namespace Parser;
|
|
|
|
|
|
|
|
Code expr = Code::Invalid;
|
|
|
|
|
|
|
|
if ( currtok.IsAssign )
|
|
|
|
{
|
|
|
|
eat( TokType::Operator );
|
|
|
|
|
|
|
|
Token expr_tok = currtok;
|
|
|
|
|
|
|
|
if ( currtok.Type == TokType::Statement_End )
|
|
|
|
{
|
|
|
|
log_failure( "gen::parse_variable: expected expression after assignment operator" );
|
|
|
|
return Code::Invalid;
|
|
|
|
}
|
|
|
|
|
|
|
|
while ( left && currtok.Type != TokType::Statement_End )
|
|
|
|
{
|
|
|
|
expr_tok.Length = ( (sptr)currtok.Text + currtok.Length ) - (sptr)expr_tok.Text;
|
|
|
|
eat( currtok.Type );
|
|
|
|
}
|
|
|
|
|
2023-05-06 13:15:49 -07:00
|
|
|
expr = untyped_str( expr_tok );
|
2023-05-01 11:12:07 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
return expr;
|
|
|
|
}
|
|
|
|
|
|
|
|
Code parse_class_struct( Parser::TokType which, Parser::TokArray& toks, char const* context )
|
|
|
|
{
|
|
|
|
using namespace Parser;
|
|
|
|
|
|
|
|
return Code::Invalid;
|
|
|
|
}
|
|
|
|
|
2023-04-23 19:53:30 -07:00
|
|
|
Code parse_class_struct_body( Parser::TokArray& toks, char const* context )
|
2023-04-03 23:04:19 -07:00
|
|
|
{
|
2023-04-22 19:24:55 -07:00
|
|
|
using namespace Parser;
|
2023-04-23 19:53:30 -07:00
|
|
|
using namespace ECode;
|
2023-04-18 19:47:59 -07:00
|
|
|
|
2023-04-23 19:53:30 -07:00
|
|
|
eat( TokType::BraceCurly_Open );
|
2023-04-23 11:33:37 -07:00
|
|
|
|
2023-04-23 19:53:30 -07:00
|
|
|
Code
|
|
|
|
result = make_code();
|
|
|
|
result->Type = Class_Body;
|
|
|
|
|
|
|
|
while ( left && currtok.Type != TokType::BraceCurly_Close )
|
|
|
|
{
|
|
|
|
Code member = Code::Invalid;
|
|
|
|
|
|
|
|
switch ( currtok.Type )
|
|
|
|
{
|
|
|
|
case TokType::Comment:
|
2023-05-06 13:15:49 -07:00
|
|
|
member = def_comment( currtok );
|
2023-05-01 11:12:07 -07:00
|
|
|
eat( TokType::Comment );
|
2023-04-23 19:53:30 -07:00
|
|
|
break;
|
|
|
|
|
|
|
|
case TokType::Access_Public:
|
2023-05-01 11:12:07 -07:00
|
|
|
member = access_public;
|
|
|
|
eat( TokType::Access_Public );
|
|
|
|
break;
|
|
|
|
|
2023-04-23 19:53:30 -07:00
|
|
|
case TokType::Access_Protected:
|
2023-05-01 11:12:07 -07:00
|
|
|
member = access_protected;
|
|
|
|
eat( TokType::Access_Protected );
|
|
|
|
break;
|
|
|
|
|
2023-04-23 19:53:30 -07:00
|
|
|
case TokType::Access_Private:
|
2023-05-01 11:12:07 -07:00
|
|
|
member = access_private;
|
|
|
|
eat( TokType::Access_Private );
|
2023-04-23 19:53:30 -07:00
|
|
|
break;
|
|
|
|
|
|
|
|
case TokType::Decl_Class:
|
|
|
|
member = parse_class( toks, context );
|
|
|
|
break;
|
|
|
|
|
|
|
|
case TokType::Decl_Enum:
|
|
|
|
member = parse_enum( toks, context );
|
|
|
|
break;
|
|
|
|
|
|
|
|
case TokType::Decl_Friend:
|
|
|
|
member = parse_friend( toks, context );
|
|
|
|
break;
|
|
|
|
|
|
|
|
case TokType::Decl_Struct:
|
|
|
|
member = parse_struct( toks, context );
|
|
|
|
break;
|
|
|
|
|
|
|
|
case TokType::Decl_Typedef:
|
|
|
|
member = parse_typedef( toks, context );
|
|
|
|
break;
|
|
|
|
|
|
|
|
case TokType::Decl_Union:
|
|
|
|
member = parse_variable( toks, context );
|
|
|
|
break;
|
|
|
|
|
|
|
|
case TokType::Decl_Using:
|
|
|
|
member = parse_using( toks, context );
|
|
|
|
break;
|
|
|
|
|
|
|
|
case TokType::Spec_Consteval:
|
|
|
|
case TokType::Spec_Constexpr:
|
|
|
|
case TokType::Spec_Constinit:
|
|
|
|
case TokType::Spec_Inline:
|
|
|
|
case TokType::Spec_Static:
|
|
|
|
case TokType::Spec_ThreadLocal:
|
|
|
|
case TokType::Spec_Volatile:
|
2023-05-01 11:12:07 -07:00
|
|
|
Code specifiers = Code::Invalid;
|
|
|
|
|
|
|
|
SpecifierT specs_found[16] { ESpecifier::Num_Specifiers };
|
|
|
|
s32 num_specifiers = 0;
|
|
|
|
|
|
|
|
while ( left && tok_is_specifier( currtok ) )
|
|
|
|
{
|
2023-05-06 13:15:49 -07:00
|
|
|
SpecifierT spec = ESpecifier::to_type( currtok );
|
2023-05-01 11:12:07 -07:00
|
|
|
|
|
|
|
switch ( spec )
|
|
|
|
{
|
|
|
|
case ESpecifier::Constexpr:
|
|
|
|
case ESpecifier::Constinit:
|
|
|
|
case ESpecifier::External_Linkage:
|
|
|
|
case ESpecifier::Local_Persist:
|
|
|
|
case ESpecifier::Mutable:
|
|
|
|
case ESpecifier::Static_Member:
|
|
|
|
case ESpecifier::Thread_Local:
|
|
|
|
case ESpecifier::Volatile:
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
log_failure( "gen::parse_variable: invalid specifier " txt(spec) " for variable" );
|
|
|
|
return Code::Invalid;
|
|
|
|
}
|
|
|
|
|
|
|
|
specs_found[num_specifiers] = spec;
|
|
|
|
num_specifiers++;
|
|
|
|
eat( currtok.Type );
|
|
|
|
}
|
|
|
|
|
|
|
|
if ( num_specifiers )
|
|
|
|
{
|
|
|
|
specifiers = def_specifiers( num_specifiers, specs_found );
|
|
|
|
}
|
|
|
|
|
|
|
|
case TokType::Identifier:
|
|
|
|
case TokType::Spec_Const:
|
2023-04-23 19:53:30 -07:00
|
|
|
case TokType::Type_Unsigned:
|
|
|
|
case TokType::Type_Signed:
|
|
|
|
case TokType::Type_Short:
|
|
|
|
case TokType::Type_Long:
|
2023-05-01 11:12:07 -07:00
|
|
|
{
|
|
|
|
Code type = parse_type( toks, context );
|
|
|
|
if ( type == Code::Invalid )
|
|
|
|
return Code::Invalid;
|
2023-04-23 19:53:30 -07:00
|
|
|
|
2023-05-01 11:12:07 -07:00
|
|
|
Token name = currtok;
|
2023-04-23 19:53:30 -07:00
|
|
|
|
2023-05-01 11:12:07 -07:00
|
|
|
if ( check( TokType::Identifier ) )
|
|
|
|
{
|
|
|
|
name = currtok;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Parsing a member function
|
|
|
|
if ( check( TokType::Capture_Start ))
|
|
|
|
{
|
|
|
|
Code params = parse_params( toks, context );
|
|
|
|
if ( params == Code::Invalid )
|
|
|
|
return Code::Invalid;
|
|
|
|
|
|
|
|
if ( check( TokType::BraceCurly_Open ) )
|
|
|
|
{
|
|
|
|
Code body = parse_function_body( toks, context);
|
|
|
|
if ( body == Code::Invalid )
|
|
|
|
return Code::Invalid;
|
|
|
|
|
|
|
|
Code
|
|
|
|
member = make_code();
|
2023-05-06 13:15:49 -07:00
|
|
|
member->Name = get_cached_string( name );
|
2023-05-01 11:12:07 -07:00
|
|
|
|
|
|
|
if ( body )
|
|
|
|
{
|
|
|
|
switch ( body->Type )
|
|
|
|
{
|
|
|
|
case Function_Body:
|
|
|
|
case Untyped:
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
{
|
|
|
|
log_failure("gen::def_function: body must be either of Function_Body or Untyped type. %s", body->debug_str());
|
|
|
|
return Code::Invalid;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
member->Type = Function;
|
|
|
|
member->add_entry( body );
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
member->Type = Function_Fwd;
|
|
|
|
}
|
|
|
|
|
|
|
|
member->add_entry( type );
|
|
|
|
|
|
|
|
if ( params )
|
|
|
|
member->add_entry( params );
|
|
|
|
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Parsing a member variable
|
|
|
|
Code array_expr = parse_array_decl( toks, context );
|
|
|
|
Code expr = parse_variable_assignment( toks, context );
|
|
|
|
|
|
|
|
member = make_code();
|
|
|
|
member->Type = Variable;
|
2023-05-06 13:15:49 -07:00
|
|
|
member->Name = get_cached_string( name );
|
2023-05-01 11:12:07 -07:00
|
|
|
|
|
|
|
member->add_entry( type );
|
|
|
|
|
|
|
|
if (array_expr)
|
|
|
|
type->add_entry( array_expr );
|
|
|
|
|
|
|
|
if (specifiers)
|
|
|
|
member->add_entry( specifiers );
|
|
|
|
|
|
|
|
if ( expr )
|
|
|
|
member->add_entry( expr );
|
|
|
|
}
|
|
|
|
|
|
|
|
if ( member == Code::Invalid )
|
|
|
|
return Code::Invalid;
|
|
|
|
|
|
|
|
result->add_entry( member );
|
|
|
|
}
|
2023-04-23 19:53:30 -07:00
|
|
|
|
|
|
|
eat( currtok.Type );
|
|
|
|
}
|
|
|
|
|
|
|
|
eat( TokType::BraceCurly_Close );
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
Code parse_function_body( Parser::TokArray& toks, char const* context )
|
|
|
|
{
|
|
|
|
using namespace Parser;
|
|
|
|
using namespace ECode;
|
|
|
|
|
|
|
|
eat( TokType::BraceCurly_Open );
|
|
|
|
|
|
|
|
Code
|
|
|
|
result = make_code();
|
|
|
|
result->Type = Function_Body;
|
|
|
|
|
|
|
|
while ( left && currtok.Type != TokType::BraceCurly_Close )
|
|
|
|
{
|
|
|
|
Code member = Code::Invalid;
|
|
|
|
|
|
|
|
switch ( currtok.Type )
|
|
|
|
{
|
|
|
|
case TokType::Comment:
|
2023-05-06 13:15:49 -07:00
|
|
|
member = def_comment( currtok );
|
2023-05-06 12:49:43 -07:00
|
|
|
eat( TokType::Comment );
|
2023-04-23 19:53:30 -07:00
|
|
|
break;
|
|
|
|
|
|
|
|
case TokType::Decl_Class:
|
|
|
|
member = parse_class( toks, context );
|
|
|
|
break;
|
|
|
|
|
|
|
|
case TokType::Decl_Enum:
|
|
|
|
member = parse_enum( toks, context );
|
|
|
|
break;
|
|
|
|
|
|
|
|
case TokType::Decl_Struct:
|
|
|
|
member = parse_struct( toks, context );
|
|
|
|
break;
|
|
|
|
|
|
|
|
case TokType::Decl_Typedef:
|
|
|
|
member = parse_typedef( toks, context );
|
|
|
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
case TokType::Decl_Union:
|
|
|
|
member = parse_union( toks, context );
|
|
|
|
break;
|
|
|
|
|
|
|
|
case TokType::Decl_Using:
|
|
|
|
member = parse_using( toks, context );
|
|
|
|
break;
|
|
|
|
|
|
|
|
case TokType::Identifier:
|
|
|
|
break;
|
|
|
|
|
|
|
|
case TokType::Spec_Const:
|
|
|
|
case TokType::Spec_Constexpr:
|
|
|
|
case TokType::Spec_Constinit:
|
|
|
|
case TokType::Spec_Static:
|
|
|
|
case TokType::Spec_ThreadLocal:
|
|
|
|
case TokType::Spec_Volatile:
|
|
|
|
// Can be all kinds of crap
|
|
|
|
|
|
|
|
case TokType::Type_Unsigned:
|
|
|
|
case TokType::Type_Signed:
|
|
|
|
case TokType::Type_Short:
|
|
|
|
case TokType::Type_Long:
|
|
|
|
member = parse_variable( toks, context );
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
Token untyped_tok = currtok;
|
|
|
|
|
|
|
|
while ( left && currtok.Type != TokType::Statement_End )
|
|
|
|
{
|
|
|
|
untyped_tok.Length = ( (sptr)currtok.Text + currtok.Length ) - (sptr)untyped_tok.Text;
|
|
|
|
eat( currtok.Type );
|
|
|
|
}
|
|
|
|
|
2023-05-06 13:15:49 -07:00
|
|
|
Code member = untyped_str( untyped_tok );
|
2023-04-23 19:53:30 -07:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ( member == Code::Invalid )
|
|
|
|
return Code::Invalid;
|
|
|
|
|
|
|
|
result->add_entry( member );
|
|
|
|
|
|
|
|
eat( currtok.Type );
|
|
|
|
}
|
|
|
|
|
|
|
|
eat( TokType::BraceCurly_Close );
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
Code parse_class( Parser::TokArray& toks, char const* context )
|
|
|
|
{
|
|
|
|
using namespace Parser;
|
2023-04-18 19:47:59 -07:00
|
|
|
|
|
|
|
Token name { nullptr, 0, TokType::Invalid };
|
|
|
|
|
2023-05-01 11:12:07 -07:00
|
|
|
Code parent = { nullptr };
|
|
|
|
Code specifiers = { nullptr };
|
|
|
|
Code body = { nullptr };
|
|
|
|
Code lang_linkage = { nullptr };
|
2023-04-18 19:47:59 -07:00
|
|
|
|
2023-04-23 19:53:30 -07:00
|
|
|
Code result = Code::Invalid;
|
2023-04-18 19:47:59 -07:00
|
|
|
|
2023-05-01 11:12:07 -07:00
|
|
|
SpecifierT specs_found[16] { ESpecifier::Num_Specifiers };
|
|
|
|
s32 num_specifiers = 0;
|
|
|
|
|
|
|
|
// Parse module specifiers
|
2023-04-23 19:53:30 -07:00
|
|
|
eat( TokType::Decl_Class );
|
2023-04-22 19:24:55 -07:00
|
|
|
|
2023-05-01 11:12:07 -07:00
|
|
|
// Parse specifiers
|
|
|
|
while ( left && tok_is_specifier( currtok ) )
|
|
|
|
{
|
2023-05-06 13:15:49 -07:00
|
|
|
SpecifierT spec = ESpecifier::to_type( currtok );
|
2023-05-01 11:12:07 -07:00
|
|
|
|
|
|
|
switch ( spec )
|
|
|
|
{
|
|
|
|
case ESpecifier::External_Linkage:
|
|
|
|
case ESpecifier::Local_Persist:
|
|
|
|
case ESpecifier::Mutable:
|
|
|
|
case ESpecifier::Static_Member:
|
|
|
|
case ESpecifier::Thread_Local:
|
|
|
|
case ESpecifier::Volatile:
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
log_failure( "gen::parse_variable: invalid specifier " txt(spec) " for variable" );
|
|
|
|
return Code::Invalid;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ( spec == ESpecifier::External_Linkage )
|
|
|
|
{
|
|
|
|
specs_found[num_specifiers] = spec;
|
|
|
|
num_specifiers++;
|
|
|
|
eat( TokType::Spec_Extern );
|
|
|
|
|
|
|
|
if ( currtok.Type == TokType::String )
|
|
|
|
{
|
2023-05-06 13:15:49 -07:00
|
|
|
lang_linkage = untyped_str( currtok );
|
2023-05-01 11:12:07 -07:00
|
|
|
eat( TokType::String );
|
|
|
|
}
|
|
|
|
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
specs_found[num_specifiers] = spec;
|
|
|
|
num_specifiers++;
|
|
|
|
eat( currtok.Type );
|
|
|
|
}
|
|
|
|
|
|
|
|
if ( num_specifiers )
|
|
|
|
{
|
|
|
|
specifiers = def_specifiers( num_specifiers, specs_found );
|
|
|
|
}
|
|
|
|
|
|
|
|
// Parse alignment
|
|
|
|
|
|
|
|
Token name = parse_identifier( toks, context );
|
|
|
|
|
|
|
|
if ( check( TokType::Assign_Classifer ) )
|
|
|
|
{
|
|
|
|
eat( TokType::Assign_Classifer );
|
|
|
|
|
|
|
|
AccessSpec access = AccessSpec::Invalid;
|
|
|
|
|
|
|
|
if ( tok_is_access_specifier( currtok ) )
|
|
|
|
{
|
|
|
|
access = tok_to_access_specifier( currtok );
|
|
|
|
}
|
|
|
|
|
|
|
|
Token parent_tok = parse_identifier( toks, context );
|
|
|
|
}
|
|
|
|
|
2023-04-23 19:53:30 -07:00
|
|
|
not_implemented();
|
2023-04-07 21:29:09 -07:00
|
|
|
}
|
2023-04-03 23:04:19 -07:00
|
|
|
|
2023-05-06 13:15:49 -07:00
|
|
|
Code parse_class( StrC def )
|
2023-04-07 21:29:09 -07:00
|
|
|
{
|
2023-05-06 13:15:49 -07:00
|
|
|
check_parse_args( parse_class, def );
|
2023-04-22 19:24:55 -07:00
|
|
|
using namespace Parser;
|
2023-04-11 22:24:43 -07:00
|
|
|
|
2023-05-06 13:15:49 -07:00
|
|
|
TokArray toks = lex( def );
|
2023-04-23 11:33:37 -07:00
|
|
|
if ( toks.Arr == nullptr )
|
2023-04-11 22:24:43 -07:00
|
|
|
return Code::Invalid;
|
|
|
|
|
2023-05-01 11:12:07 -07:00
|
|
|
return parse_class( toks, txt(parse_class) );
|
2023-04-23 19:53:30 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
Code parse_enum( Parser::TokArray& toks, char const* context )
|
|
|
|
{
|
|
|
|
using namespace Parser;
|
|
|
|
using namespace ECode;
|
|
|
|
|
2023-04-18 21:56:43 -07:00
|
|
|
SpecifierT specs_found[16] { ESpecifier::Num_Specifiers };
|
|
|
|
s32 num_specifiers = 0;
|
2023-04-11 22:24:43 -07:00
|
|
|
|
2023-04-22 19:24:55 -07:00
|
|
|
Token name = { nullptr, 0, TokType::Invalid };
|
|
|
|
Code array_expr = { nullptr };
|
|
|
|
Code type = { nullptr };
|
|
|
|
Token body = { nullptr, 0, TokType::Invalid };
|
2023-04-11 22:24:43 -07:00
|
|
|
|
2023-04-18 21:56:43 -07:00
|
|
|
char entries_code[ kilobytes(128) ] { 0 };
|
|
|
|
s32 entries_length = 0;
|
2023-04-11 22:24:43 -07:00
|
|
|
|
2023-04-18 21:56:43 -07:00
|
|
|
bool is_enum_class = false;
|
2023-04-11 22:24:43 -07:00
|
|
|
|
2023-04-18 21:56:43 -07:00
|
|
|
eat( TokType::Decl_Enum );
|
2023-04-11 22:24:43 -07:00
|
|
|
|
2023-04-22 19:24:55 -07:00
|
|
|
if ( currtok.Type == TokType::Decl_Class )
|
2023-04-11 22:24:43 -07:00
|
|
|
{
|
2023-04-18 21:56:43 -07:00
|
|
|
eat( TokType::Decl_Class);
|
|
|
|
is_enum_class = true;
|
2023-04-11 22:24:43 -07:00
|
|
|
}
|
|
|
|
|
2023-05-01 11:12:07 -07:00
|
|
|
// TODO: Parse attributes
|
|
|
|
|
2023-04-22 19:24:55 -07:00
|
|
|
if ( currtok.Type != TokType::Identifier )
|
2023-04-11 22:24:43 -07:00
|
|
|
{
|
2023-04-18 21:56:43 -07:00
|
|
|
log_failure( "gen::parse_enum: expected identifier for enum name" );
|
2023-04-11 22:24:43 -07:00
|
|
|
return Code::Invalid;
|
|
|
|
}
|
|
|
|
|
2023-04-22 19:24:55 -07:00
|
|
|
name = currtok;
|
2023-04-18 21:56:43 -07:00
|
|
|
eat( TokType::Identifier );
|
2023-04-11 22:24:43 -07:00
|
|
|
|
2023-04-22 19:24:55 -07:00
|
|
|
if ( currtok.Type == TokType::Assign_Classifer )
|
2023-04-18 21:56:43 -07:00
|
|
|
{
|
|
|
|
eat( TokType::Assign_Classifer );
|
2023-04-11 22:24:43 -07:00
|
|
|
|
2023-04-22 19:24:55 -07:00
|
|
|
type = parse_type( toks, txt(parse_enum) );
|
2023-04-18 21:56:43 -07:00
|
|
|
if ( type == Code::Invalid )
|
2023-04-11 22:24:43 -07:00
|
|
|
return Code::Invalid;
|
2023-04-18 21:56:43 -07:00
|
|
|
}
|
|
|
|
|
2023-04-22 19:24:55 -07:00
|
|
|
if ( currtok.Type == TokType::BraceCurly_Open )
|
2023-04-18 21:56:43 -07:00
|
|
|
{
|
|
|
|
eat( TokType::BraceCurly_Open );
|
2023-04-11 22:24:43 -07:00
|
|
|
|
2023-04-22 19:24:55 -07:00
|
|
|
body = currtok;
|
2023-04-11 22:24:43 -07:00
|
|
|
|
2023-04-22 19:24:55 -07:00
|
|
|
while ( currtok.Type != TokType::BraceCurly_Close )
|
2023-04-11 22:24:43 -07:00
|
|
|
{
|
2023-04-22 19:24:55 -07:00
|
|
|
body.Length += currtok.Length;
|
2023-04-18 21:56:43 -07:00
|
|
|
|
2023-04-22 19:24:55 -07:00
|
|
|
eat( currtok.Type );
|
2023-04-11 22:24:43 -07:00
|
|
|
}
|
2023-04-18 21:56:43 -07:00
|
|
|
|
|
|
|
eat( TokType::BraceCurly_Close );
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
eat( TokType::Statement_End );
|
2023-04-11 22:24:43 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
using namespace ECode;
|
|
|
|
|
|
|
|
Code
|
|
|
|
result = make_code();
|
|
|
|
|
2023-04-18 21:56:43 -07:00
|
|
|
if ( body.Length )
|
2023-04-11 22:24:43 -07:00
|
|
|
{
|
2023-04-19 13:57:59 -07:00
|
|
|
mem_copy( entries_code, body.Text, body.Length );
|
2023-04-11 22:24:43 -07:00
|
|
|
|
2023-05-06 13:15:49 -07:00
|
|
|
Code untyped_body = untyped_str( { entries_length, entries_code } );
|
2023-04-11 22:24:43 -07:00
|
|
|
|
|
|
|
result->Type = is_enum_class ? Enum_Class : Enum;
|
2023-04-18 21:56:43 -07:00
|
|
|
result->add_entry( untyped_body );
|
2023-04-11 22:24:43 -07:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
result->Type = is_enum_class ? Enum_Class_Fwd : Enum_Fwd;
|
|
|
|
}
|
|
|
|
|
2023-05-06 13:15:49 -07:00
|
|
|
result->Name = get_cached_string( name );
|
2023-04-11 22:24:43 -07:00
|
|
|
|
|
|
|
if ( type )
|
|
|
|
result->add_entry( type );
|
|
|
|
|
|
|
|
result.lock();
|
|
|
|
return result;
|
2023-04-09 21:38:47 -07:00
|
|
|
}
|
2023-04-03 23:04:19 -07:00
|
|
|
|
2023-05-06 13:15:49 -07:00
|
|
|
Code parse_enum( StrC def )
|
2023-04-07 21:29:09 -07:00
|
|
|
{
|
2023-05-06 13:15:49 -07:00
|
|
|
check_parse_args( parse_enum, def );
|
2023-05-01 11:12:07 -07:00
|
|
|
using namespace Parser;
|
2023-04-18 19:47:59 -07:00
|
|
|
|
2023-05-06 13:15:49 -07:00
|
|
|
TokArray toks = lex( def );
|
2023-04-23 11:33:37 -07:00
|
|
|
if ( toks.Arr == nullptr )
|
2023-04-18 19:47:59 -07:00
|
|
|
return Code::Invalid;
|
|
|
|
|
2023-04-23 19:53:30 -07:00
|
|
|
return parse_enum( toks, txt(parse_enum) );
|
2023-05-01 11:12:07 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
Code parse_export_body( Parser::TokArray& toks, char const* context )
|
|
|
|
{
|
|
|
|
not_implemented();
|
|
|
|
return Code::Invalid;
|
|
|
|
}
|
|
|
|
|
2023-05-06 13:15:49 -07:00
|
|
|
Code parse_export_body( StrC def )
|
2023-05-01 11:12:07 -07:00
|
|
|
{
|
|
|
|
not_implemented();
|
|
|
|
return Code::Invalid;
|
|
|
|
}
|
|
|
|
|
|
|
|
Code parse_extern_link( Parser::TokArray& toks, char const* context )
|
|
|
|
{
|
|
|
|
not_implemented();
|
|
|
|
return Code::Invalid;
|
|
|
|
}
|
|
|
|
|
2023-05-06 13:15:49 -07:00
|
|
|
Code parse_extern_link( StrC def )
|
2023-05-01 11:12:07 -07:00
|
|
|
{
|
|
|
|
not_implemented();
|
|
|
|
return Code::Invalid;
|
2023-04-23 19:53:30 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
Code parse_friend( Parser::TokArray& toks, char const* context )
|
|
|
|
{
|
|
|
|
using namespace Parser;
|
|
|
|
using namespace ECode;
|
|
|
|
|
2023-04-18 19:47:59 -07:00
|
|
|
eat( TokType::Decl_Friend );
|
|
|
|
|
2023-04-23 16:04:43 -07:00
|
|
|
Code function = { nullptr };
|
|
|
|
|
|
|
|
// Type declaration or return type
|
|
|
|
Code type = parse_type( toks, txt(parse_friend) );
|
|
|
|
if ( type == Code::Invalid )
|
|
|
|
return Code::Invalid;
|
2023-04-18 19:47:59 -07:00
|
|
|
|
2023-04-23 16:04:43 -07:00
|
|
|
// Funciton declaration
|
|
|
|
if ( currtok.Type == TokType::Identifier )
|
|
|
|
{
|
|
|
|
// Name
|
|
|
|
Token name = parse_identifier( toks, txt(parse_friend) );
|
2023-04-19 12:40:23 -07:00
|
|
|
|
2023-04-23 16:04:43 -07:00
|
|
|
// Parameter list
|
|
|
|
Code params = parse_params( toks, txt(parse_friend) );
|
2023-04-19 12:40:23 -07:00
|
|
|
|
2023-04-23 16:04:43 -07:00
|
|
|
function = make_code();
|
|
|
|
function->Type = Function_Fwd;
|
2023-05-06 14:23:17 -07:00
|
|
|
function->Name = get_cached_string( name );
|
2023-04-23 16:04:43 -07:00
|
|
|
function->add_entry( type );
|
2023-04-19 12:40:23 -07:00
|
|
|
|
2023-04-23 16:04:43 -07:00
|
|
|
if ( params )
|
|
|
|
function->add_entry( params );
|
2023-04-19 12:40:23 -07:00
|
|
|
|
2023-04-23 16:04:43 -07:00
|
|
|
function.lock();
|
|
|
|
}
|
|
|
|
|
|
|
|
eat( TokType::Statement_End );
|
|
|
|
|
|
|
|
Code
|
|
|
|
result = make_code();
|
|
|
|
result->Type = Friend;
|
2023-04-19 12:40:23 -07:00
|
|
|
|
2023-04-23 16:04:43 -07:00
|
|
|
if ( function )
|
|
|
|
result->add_entry( function );
|
2023-04-19 12:40:23 -07:00
|
|
|
|
2023-04-23 16:04:43 -07:00
|
|
|
else
|
|
|
|
result->add_entry( type );
|
2023-04-19 12:40:23 -07:00
|
|
|
|
2023-04-23 16:04:43 -07:00
|
|
|
result.lock();
|
2023-04-19 12:40:23 -07:00
|
|
|
return result;
|
2023-04-07 21:29:09 -07:00
|
|
|
}
|
|
|
|
|
2023-05-06 13:15:49 -07:00
|
|
|
Code parse_friend( StrC def )
|
2023-04-23 19:53:30 -07:00
|
|
|
{
|
2023-05-06 13:15:49 -07:00
|
|
|
check_parse_args( parse_friend, def );
|
2023-05-01 11:12:07 -07:00
|
|
|
using namespace Parser;
|
2023-04-23 19:53:30 -07:00
|
|
|
|
2023-05-06 13:15:49 -07:00
|
|
|
TokArray toks = lex( def );
|
2023-04-23 19:53:30 -07:00
|
|
|
if ( toks.Arr == nullptr )
|
|
|
|
return Code::Invalid;
|
|
|
|
|
|
|
|
return parse_friend( toks, txt(parse_friend) );
|
|
|
|
}
|
|
|
|
|
|
|
|
Code parse_functon( Parser::TokArray& toks, char const* context )
|
2023-04-03 23:04:19 -07:00
|
|
|
{
|
2023-04-22 19:24:55 -07:00
|
|
|
using namespace Parser;
|
|
|
|
|
2023-04-23 19:53:30 -07:00
|
|
|
// TODO: Add attribute parsing support
|
2023-04-03 00:55:28 -07:00
|
|
|
|
2023-04-23 19:53:30 -07:00
|
|
|
SpecifierT specs_found[16] { ESpecifier::Num_Specifiers };
|
|
|
|
s32 num_specifiers = 0;
|
2023-04-03 23:04:19 -07:00
|
|
|
|
2023-04-23 19:53:30 -07:00
|
|
|
Code lang_linkage = Code::Invalid;
|
|
|
|
Code attributes = Code::Invalid;
|
|
|
|
Code array_expr = Code::Invalid;
|
|
|
|
Code specifiers = Code::Invalid;
|
2023-04-03 23:04:19 -07:00
|
|
|
|
2023-05-01 11:12:07 -07:00
|
|
|
// TODO: Parse module specifiers
|
|
|
|
|
|
|
|
// TODO: Parse attributes
|
|
|
|
|
2023-04-23 19:53:30 -07:00
|
|
|
while ( left && tok_is_specifier( currtok ) )
|
|
|
|
{
|
2023-05-06 14:23:17 -07:00
|
|
|
SpecifierT spec = ESpecifier::to_type( currtok );
|
2023-04-05 23:21:23 -07:00
|
|
|
|
2023-04-23 19:53:30 -07:00
|
|
|
switch ( spec )
|
|
|
|
{
|
|
|
|
case ESpecifier::Constexpr:
|
|
|
|
case ESpecifier::External_Linkage:
|
|
|
|
case ESpecifier::Local_Persist:
|
|
|
|
case ESpecifier::Mutable:
|
|
|
|
case ESpecifier::Static_Member:
|
|
|
|
case ESpecifier::Thread_Local:
|
|
|
|
case ESpecifier::Volatile:
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
log_failure( "gen::parse_variable: invalid specifier " txt(spec) " for variable" );
|
|
|
|
return Code::Invalid;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ( spec == ESpecifier::External_Linkage )
|
|
|
|
{
|
|
|
|
specs_found[num_specifiers] = spec;
|
|
|
|
num_specifiers++;
|
|
|
|
eat( TokType::Spec_Extern );
|
|
|
|
|
|
|
|
if ( currtok.Type == TokType::String )
|
|
|
|
{
|
2023-05-06 14:23:17 -07:00
|
|
|
lang_linkage = untyped_str( currtok );
|
2023-04-23 19:53:30 -07:00
|
|
|
eat( TokType::String );
|
|
|
|
}
|
|
|
|
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
specs_found[num_specifiers] = spec;
|
|
|
|
num_specifiers++;
|
|
|
|
eat( currtok.Type );
|
|
|
|
}
|
|
|
|
|
|
|
|
if ( num_specifiers )
|
|
|
|
{
|
|
|
|
specifiers = def_specifiers( num_specifiers, specs_found );
|
|
|
|
}
|
|
|
|
|
|
|
|
Code ret_type = parse_type( toks, txt(parse_function) );
|
|
|
|
if ( ret_type == Code::Invalid )
|
|
|
|
return Code::Invalid;
|
|
|
|
|
|
|
|
Token name = parse_identifier( toks, txt(parse_function) );
|
|
|
|
if ( ! name )
|
|
|
|
return Code::Invalid;
|
|
|
|
|
|
|
|
Code params = parse_params( toks, txt(parse_function) );
|
|
|
|
|
|
|
|
Code body = Code::Invalid;
|
|
|
|
if ( check( TokType::BraceCurly_Open ) )
|
|
|
|
{
|
|
|
|
body = parse_function_body( toks, txt(parse_function) );
|
|
|
|
if ( body == Code::Invalid )
|
|
|
|
return Code::Invalid;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
eat( TokType::Statement_End );
|
|
|
|
}
|
|
|
|
|
|
|
|
using namespace ECode;
|
2023-04-03 23:04:19 -07:00
|
|
|
|
2023-04-05 23:21:23 -07:00
|
|
|
Code
|
2023-04-23 19:53:30 -07:00
|
|
|
result = make_code();
|
2023-05-06 14:23:17 -07:00
|
|
|
result->Name = get_cached_string( name );
|
2023-04-23 19:53:30 -07:00
|
|
|
|
|
|
|
if ( body )
|
|
|
|
{
|
|
|
|
switch ( body->Type )
|
|
|
|
{
|
|
|
|
case Function_Body:
|
|
|
|
case Untyped:
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
{
|
|
|
|
log_failure("gen::def_function: body must be either of Function_Body or Untyped type. %s", body->debug_str());
|
|
|
|
return Code::Invalid;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
result->Type = Function;
|
|
|
|
result->add_entry( body );
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
result->Type = Function_Fwd;
|
|
|
|
}
|
2023-04-03 23:04:19 -07:00
|
|
|
|
2023-04-23 19:53:30 -07:00
|
|
|
if ( specifiers )
|
|
|
|
result->add_entry( specifiers );
|
|
|
|
|
|
|
|
result->add_entry( ret_type );
|
|
|
|
|
|
|
|
if ( params )
|
|
|
|
result->add_entry( params );
|
2023-04-03 23:04:19 -07:00
|
|
|
|
|
|
|
result.lock();
|
2023-04-02 09:35:14 -07:00
|
|
|
return result;
|
|
|
|
}
|
2023-04-03 23:04:19 -07:00
|
|
|
|
2023-05-06 13:15:49 -07:00
|
|
|
Code parse_function( StrC def )
|
2023-04-23 19:53:30 -07:00
|
|
|
{
|
|
|
|
using namespace Parser;
|
|
|
|
|
2023-05-06 13:15:49 -07:00
|
|
|
check_parse_args( parse_function, def );
|
2023-04-23 19:53:30 -07:00
|
|
|
|
|
|
|
Arena mem;
|
|
|
|
do_once_start
|
|
|
|
arena_init_from_allocator( & mem, heap(), kilobytes( 10 ) );
|
|
|
|
do_once_end
|
|
|
|
|
2023-05-06 13:15:49 -07:00
|
|
|
TokArray toks = lex( def );
|
2023-04-23 19:53:30 -07:00
|
|
|
if ( toks.Arr == nullptr )
|
|
|
|
return Code::Invalid;
|
|
|
|
|
|
|
|
return parse_functon( toks, txt(parse_function) );
|
|
|
|
}
|
|
|
|
|
2023-05-05 15:10:03 -07:00
|
|
|
Code parse_global_body( Parser::TokArray& toks, char const* context )
|
|
|
|
{
|
|
|
|
not_implemented();
|
|
|
|
}
|
|
|
|
|
2023-05-06 13:15:49 -07:00
|
|
|
Code parse_global_body( StrC def )
|
2023-05-05 15:10:03 -07:00
|
|
|
{
|
|
|
|
using namespace Parser;
|
|
|
|
using namespace ECode;
|
|
|
|
|
|
|
|
Code result = make_code();
|
|
|
|
result->Type = Global_Body;
|
|
|
|
|
|
|
|
// while ( left )
|
|
|
|
// {
|
|
|
|
|
|
|
|
// }
|
|
|
|
|
|
|
|
result.lock();
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2023-04-23 19:53:30 -07:00
|
|
|
Code parse_namespace( Parser::TokArray& toks, char const* context )
|
|
|
|
{
|
|
|
|
not_implemented();
|
|
|
|
}
|
|
|
|
|
2023-05-06 13:15:49 -07:00
|
|
|
Code parse_namespace( StrC def )
|
2023-04-07 21:29:09 -07:00
|
|
|
{
|
2023-04-09 21:38:47 -07:00
|
|
|
not_implemented( parse_namespace );
|
2023-04-07 21:29:09 -07:00
|
|
|
}
|
|
|
|
|
2023-04-23 19:53:30 -07:00
|
|
|
Code parse_operator( Parser::TokArray& toks, char const* context )
|
|
|
|
{
|
|
|
|
not_implemented();
|
|
|
|
}
|
|
|
|
|
2023-05-06 13:15:49 -07:00
|
|
|
Code parse_operator( StrC def )
|
2023-04-07 21:29:09 -07:00
|
|
|
{
|
2023-04-09 21:38:47 -07:00
|
|
|
not_implemented( parse_operator );
|
2023-04-07 21:29:09 -07:00
|
|
|
}
|
|
|
|
|
2023-04-23 19:53:30 -07:00
|
|
|
Code parse_struct( Parser::TokArray& toks, char const* context )
|
|
|
|
{
|
|
|
|
not_implemented();
|
|
|
|
}
|
|
|
|
|
2023-05-06 13:15:49 -07:00
|
|
|
Code parse_struct( StrC def )
|
2023-04-02 09:35:14 -07:00
|
|
|
{
|
2023-04-09 10:59:39 -07:00
|
|
|
Arena mem;
|
2023-04-03 23:04:19 -07:00
|
|
|
do_once_start
|
|
|
|
arena_init_from_allocator( & mem, heap(), kilobytes( 10 ) );
|
|
|
|
do_once_end
|
2023-04-03 00:55:28 -07:00
|
|
|
|
2023-04-03 23:04:19 -07:00
|
|
|
// Pretty sure its impossible to have more than this.
|
|
|
|
SpecifierT specs_found[16] { ESpecifier::Num_Specifiers };
|
|
|
|
|
|
|
|
u8 num_specifiers;
|
|
|
|
|
|
|
|
// Making all significant tokens have a max length of 128 for this parser.
|
2023-04-22 19:24:55 -07:00
|
|
|
constexpr sw LengthID = 128;
|
2023-04-03 23:04:19 -07:00
|
|
|
|
2023-04-09 21:38:47 -07:00
|
|
|
char const name [LengthID] { 0 };
|
2023-04-03 23:04:19 -07:00
|
|
|
char const parent[LengthID] { 0 };
|
2023-04-18 21:56:43 -07:00
|
|
|
|
|
|
|
return Code::Invalid;
|
2023-04-07 21:29:09 -07:00
|
|
|
}
|
2023-04-03 00:55:28 -07:00
|
|
|
|
2023-05-05 15:10:03 -07:00
|
|
|
Code parse_type( Parser::TokArray& toks, char const* context )
|
2023-04-07 21:29:09 -07:00
|
|
|
{
|
2023-04-23 11:33:37 -07:00
|
|
|
using namespace Parser;
|
|
|
|
|
|
|
|
SpecifierT specs_found[16] { ESpecifier::Num_Specifiers };
|
|
|
|
s32 num_specifiers = 0;
|
|
|
|
|
2023-05-05 15:10:03 -07:00
|
|
|
Token name = { nullptr, 0, TokType::Invalid };
|
2023-05-01 11:12:07 -07:00
|
|
|
|
2023-05-05 15:10:03 -07:00
|
|
|
while ( left && tok_is_specifier( currtok ) )
|
2023-04-23 11:33:37 -07:00
|
|
|
{
|
2023-05-06 14:23:17 -07:00
|
|
|
SpecifierT spec = ESpecifier::to_type( currtok );
|
2023-04-23 11:33:37 -07:00
|
|
|
|
2023-05-05 15:10:03 -07:00
|
|
|
if ( spec != ESpecifier::Const
|
|
|
|
&& spec < ESpecifier::Type_Signed )
|
2023-04-18 21:56:43 -07:00
|
|
|
{
|
2023-04-22 19:24:55 -07:00
|
|
|
log_failure( "gen::parse_type: Error, invalid specifier used in type definition: %s", currtok.Text );
|
|
|
|
return Code::Invalid;
|
2023-04-18 21:56:43 -07:00
|
|
|
}
|
2023-04-18 19:47:59 -07:00
|
|
|
|
2023-04-18 21:56:43 -07:00
|
|
|
specs_found[num_specifiers] = spec;
|
|
|
|
num_specifiers++;
|
2023-04-22 19:24:55 -07:00
|
|
|
eat( currtok.Type );
|
2023-04-18 21:56:43 -07:00
|
|
|
}
|
2023-04-18 19:47:59 -07:00
|
|
|
|
2023-04-18 21:56:43 -07:00
|
|
|
if ( left == 0 )
|
2023-04-18 19:47:59 -07:00
|
|
|
{
|
2023-05-01 11:12:07 -07:00
|
|
|
log_failure( "%s: Error, unexpected end of type definition", context );
|
2023-04-22 19:24:55 -07:00
|
|
|
return Code::Invalid;
|
2023-04-18 19:47:59 -07:00
|
|
|
}
|
|
|
|
|
2023-04-22 19:24:55 -07:00
|
|
|
if ( currtok.Type == TokType::Decl_Class
|
|
|
|
|| currtok.Type == TokType::Decl_Struct )
|
2023-04-19 12:40:23 -07:00
|
|
|
{
|
2023-04-22 19:24:55 -07:00
|
|
|
name = currtok;
|
|
|
|
eat( currtok.Type );
|
2023-04-19 12:40:23 -07:00
|
|
|
|
2023-04-23 16:04:43 -07:00
|
|
|
name.Length = ( (sptr)currtok.Text + currtok.Length ) - (sptr)name.Text;
|
2023-04-19 12:40:23 -07:00
|
|
|
eat( TokType::Identifier );
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2023-05-01 11:12:07 -07:00
|
|
|
name = parse_identifier( toks, context );
|
2023-04-23 16:04:43 -07:00
|
|
|
if ( ! name )
|
|
|
|
return Code::Invalid;
|
2023-04-19 12:40:23 -07:00
|
|
|
}
|
2023-04-11 22:24:43 -07:00
|
|
|
|
2023-04-22 19:24:55 -07:00
|
|
|
while ( left && tok_is_specifier( currtok ) )
|
2023-04-11 22:24:43 -07:00
|
|
|
{
|
2023-05-06 14:23:17 -07:00
|
|
|
SpecifierT spec = ESpecifier::to_type( currtok );
|
2023-04-18 21:56:43 -07:00
|
|
|
|
|
|
|
if ( spec != ESpecifier::Const
|
2023-04-22 21:43:31 -07:00
|
|
|
&& spec != ESpecifier::Ptr
|
2023-04-18 21:56:43 -07:00
|
|
|
&& spec != ESpecifier::Ref
|
|
|
|
&& spec != ESpecifier::RValue
|
|
|
|
&& spec < ESpecifier::Type_Signed )
|
2023-04-11 22:24:43 -07:00
|
|
|
{
|
2023-05-01 11:12:07 -07:00
|
|
|
log_failure( "%s: Error, invalid specifier used in type definition: %s", context, currtok.Text );
|
2023-04-22 19:24:55 -07:00
|
|
|
return Code::Invalid;
|
2023-04-11 22:24:43 -07:00
|
|
|
}
|
|
|
|
|
2023-04-18 21:56:43 -07:00
|
|
|
specs_found[num_specifiers] = spec;
|
|
|
|
num_specifiers++;
|
2023-04-22 19:24:55 -07:00
|
|
|
eat( currtok.Type );
|
2023-04-11 22:24:43 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
using namespace ECode;
|
|
|
|
|
2023-04-23 11:33:37 -07:00
|
|
|
// TODO: Need to figure out type code caching wiht the type table.
|
|
|
|
|
2023-04-11 22:24:43 -07:00
|
|
|
Code
|
|
|
|
result = make_code();
|
|
|
|
result->Type = Typename;
|
2023-05-06 14:23:17 -07:00
|
|
|
result->Name = get_cached_string( name );
|
2023-04-11 22:24:43 -07:00
|
|
|
|
2023-04-18 19:47:59 -07:00
|
|
|
if (num_specifiers)
|
|
|
|
{
|
|
|
|
Code specifiers = def_specifiers( num_specifiers, specs_found );
|
2023-04-11 22:24:43 -07:00
|
|
|
|
2023-04-18 19:47:59 -07:00
|
|
|
result->add_entry( specifiers );
|
|
|
|
}
|
2023-04-11 22:24:43 -07:00
|
|
|
|
|
|
|
return result;
|
2023-04-22 19:24:55 -07:00
|
|
|
}
|
|
|
|
|
2023-05-06 13:15:49 -07:00
|
|
|
Code parse_type( StrC def )
|
2023-04-22 19:24:55 -07:00
|
|
|
{
|
2023-05-06 13:15:49 -07:00
|
|
|
check_parse_args( parse_type, def );
|
2023-05-01 11:12:07 -07:00
|
|
|
using namespace Parser;
|
2023-04-22 19:24:55 -07:00
|
|
|
|
2023-05-06 13:15:49 -07:00
|
|
|
TokArray toks = lex( def );
|
2023-04-23 11:33:37 -07:00
|
|
|
if ( toks.Arr == nullptr )
|
2023-04-22 19:24:55 -07:00
|
|
|
return Code::Invalid;
|
|
|
|
|
|
|
|
Code result = parse_type( toks, txt(parse_type) );
|
2023-04-23 11:33:37 -07:00
|
|
|
|
|
|
|
result.lock();
|
2023-04-22 19:24:55 -07:00
|
|
|
return result;
|
2023-04-07 21:29:09 -07:00
|
|
|
}
|
|
|
|
|
2023-04-23 19:53:30 -07:00
|
|
|
Code parse_typedef( Parser::TokArray& toks, char const* context )
|
2023-04-07 21:29:09 -07:00
|
|
|
{
|
2023-04-23 11:33:37 -07:00
|
|
|
using namespace Parser;
|
|
|
|
|
2023-04-22 19:24:55 -07:00
|
|
|
Token name = { nullptr, 0, TokType::Invalid };
|
|
|
|
Code array_expr = { nullptr };
|
|
|
|
Code type = { nullptr };
|
2023-04-11 22:24:43 -07:00
|
|
|
|
2023-04-18 21:56:43 -07:00
|
|
|
eat( TokType::Decl_Typedef );
|
2023-04-11 22:24:43 -07:00
|
|
|
|
2023-04-22 19:24:55 -07:00
|
|
|
type = parse_type( toks, txt(parse_typedef) );
|
2023-04-11 22:24:43 -07:00
|
|
|
|
2023-04-23 11:33:37 -07:00
|
|
|
if ( check( TokType::Identifier ) )
|
2023-04-18 21:56:43 -07:00
|
|
|
{
|
|
|
|
log_failure( "gen::parse_typedef: Error, expected identifier for typedef" );
|
2023-04-11 22:24:43 -07:00
|
|
|
return Code::Invalid;
|
|
|
|
}
|
2023-04-18 21:56:43 -07:00
|
|
|
|
2023-04-22 19:24:55 -07:00
|
|
|
name = currtok;
|
2023-04-18 21:56:43 -07:00
|
|
|
eat( TokType::Identifier );
|
|
|
|
|
2023-04-23 16:04:43 -07:00
|
|
|
array_expr = parse_array_decl( toks, txt(parse_typedef) );
|
2023-04-23 11:33:37 -07:00
|
|
|
|
2023-04-18 21:56:43 -07:00
|
|
|
eat( TokType::Statement_End );
|
2023-04-11 22:24:43 -07:00
|
|
|
|
|
|
|
using namespace ECode;
|
|
|
|
|
|
|
|
Code
|
2023-04-23 11:33:37 -07:00
|
|
|
result = make_code();
|
2023-04-11 22:24:43 -07:00
|
|
|
result->Type = Typedef;
|
2023-05-06 14:23:17 -07:00
|
|
|
result->Name = get_cached_string( name );
|
2023-04-11 22:24:43 -07:00
|
|
|
|
|
|
|
result->add_entry( type );
|
|
|
|
|
2023-04-23 11:33:37 -07:00
|
|
|
if ( array_expr )
|
|
|
|
type->add_entry( array_expr );
|
|
|
|
|
2023-04-11 22:24:43 -07:00
|
|
|
result.lock();
|
|
|
|
return result;
|
2023-04-07 21:29:09 -07:00
|
|
|
}
|
|
|
|
|
2023-05-06 13:15:49 -07:00
|
|
|
Code parse_typedef( StrC def )
|
2023-04-07 21:29:09 -07:00
|
|
|
{
|
2023-05-06 13:15:49 -07:00
|
|
|
check_parse_args( parse_typedef, def );
|
2023-05-01 11:12:07 -07:00
|
|
|
using namespace Parser;
|
2023-04-18 19:47:59 -07:00
|
|
|
|
2023-05-06 13:15:49 -07:00
|
|
|
TokArray toks = lex( def );
|
2023-04-23 11:33:37 -07:00
|
|
|
if ( toks.Arr == nullptr )
|
2023-04-18 19:47:59 -07:00
|
|
|
return Code::Invalid;
|
|
|
|
|
2023-04-23 19:53:30 -07:00
|
|
|
return parse_typedef( toks, txt(parse_typedef) );
|
|
|
|
}
|
|
|
|
|
|
|
|
Code parse_union( Parser::TokArray& toks, char const* context )
|
|
|
|
{
|
|
|
|
using namespace Parser;
|
|
|
|
|
2023-05-05 15:10:03 -07:00
|
|
|
not_implemented();
|
|
|
|
|
2023-04-23 19:53:30 -07:00
|
|
|
return Code::Invalid;
|
|
|
|
}
|
|
|
|
|
|
|
|
Code parse_union( s32 length, char const* def )
|
|
|
|
{
|
2023-05-05 15:10:03 -07:00
|
|
|
not_implemented( parse_union );
|
2023-04-23 19:53:30 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
Code parse_using( Parser::TokArray& toks, char const* context )
|
|
|
|
{
|
|
|
|
using namespace Parser;
|
|
|
|
|
2023-04-18 21:56:43 -07:00
|
|
|
SpecifierT specs_found[16] { ESpecifier::Num_Specifiers };
|
|
|
|
s32 num_specifiers = 0;
|
2023-04-18 19:47:59 -07:00
|
|
|
|
2023-04-18 21:56:43 -07:00
|
|
|
Token* name = nullptr;
|
|
|
|
Code array_expr = { nullptr };
|
|
|
|
Code type = { nullptr };
|
2023-04-18 19:47:59 -07:00
|
|
|
|
2023-04-18 21:56:43 -07:00
|
|
|
bool is_namespace = false;
|
2023-04-18 19:47:59 -07:00
|
|
|
|
2023-04-18 21:56:43 -07:00
|
|
|
eat( TokType::Decl_Using );
|
2023-04-18 19:47:59 -07:00
|
|
|
|
2023-04-22 19:24:55 -07:00
|
|
|
if ( currtok.Type == TokType::Decl_Namespace )
|
2023-04-18 19:47:59 -07:00
|
|
|
{
|
2023-04-18 21:56:43 -07:00
|
|
|
is_namespace = true;
|
|
|
|
eat( TokType::Decl_Namespace );
|
2023-04-18 19:47:59 -07:00
|
|
|
}
|
|
|
|
|
2023-05-01 11:12:07 -07:00
|
|
|
// TODO: Parse Attributes
|
|
|
|
|
2023-04-18 21:56:43 -07:00
|
|
|
eat( TokType::Identifier );
|
2023-04-18 19:47:59 -07:00
|
|
|
|
2023-04-23 11:33:37 -07:00
|
|
|
if ( currtok.IsAssign )
|
2023-04-18 19:47:59 -07:00
|
|
|
{
|
2023-04-23 11:33:37 -07:00
|
|
|
eat( TokType::Operator );
|
|
|
|
|
|
|
|
type = parse_type( toks, txt(parse_typedef) );
|
|
|
|
}
|
|
|
|
|
2023-04-23 16:04:43 -07:00
|
|
|
array_expr = parse_array_decl( toks, txt(parse_typedef) );
|
2023-04-18 21:56:43 -07:00
|
|
|
|
|
|
|
eat( TokType::Statement_End );
|
|
|
|
|
|
|
|
using namespace ECode;
|
2023-04-18 19:47:59 -07:00
|
|
|
|
|
|
|
Code
|
2023-04-23 11:33:37 -07:00
|
|
|
result = make_code();
|
2023-04-18 19:47:59 -07:00
|
|
|
result->Type = is_namespace ? Using : Using_Namespace;
|
2023-05-06 14:23:17 -07:00
|
|
|
result->Name = get_cached_string( * name );
|
2023-04-18 19:47:59 -07:00
|
|
|
|
|
|
|
result->add_entry( type );
|
|
|
|
|
2023-04-23 11:33:37 -07:00
|
|
|
if ( array_expr )
|
|
|
|
type->add_entry( array_expr );
|
|
|
|
|
2023-04-18 19:47:59 -07:00
|
|
|
result.lock();
|
|
|
|
return result;
|
2023-04-23 19:53:30 -07:00
|
|
|
}
|
|
|
|
|
2023-05-06 13:15:49 -07:00
|
|
|
Code parse_using( StrC def )
|
2023-04-23 19:53:30 -07:00
|
|
|
{
|
2023-05-06 13:15:49 -07:00
|
|
|
check_parse_args( parse_using, def );
|
2023-05-01 11:12:07 -07:00
|
|
|
using namespace Parser;
|
2023-04-23 19:53:30 -07:00
|
|
|
|
2023-05-06 13:15:49 -07:00
|
|
|
TokArray toks = lex( def );
|
2023-04-23 19:53:30 -07:00
|
|
|
if ( toks.Arr == nullptr )
|
|
|
|
return Code::Invalid;
|
|
|
|
|
|
|
|
return parse_using( toks, txt(parse_using) );
|
2023-04-02 09:35:14 -07:00
|
|
|
}
|
2023-04-22 19:24:55 -07:00
|
|
|
|
2023-05-05 15:10:03 -07:00
|
|
|
Code parse_variable( Parser::TokArray& toks, char const* context )
|
|
|
|
{
|
|
|
|
using namespace Parser;
|
|
|
|
|
|
|
|
Token name = { nullptr, 0, TokType::Invalid };
|
|
|
|
|
|
|
|
SpecifierT specs_found[16] { ESpecifier::Num_Specifiers };
|
|
|
|
s32 num_specifiers = 0;
|
|
|
|
|
|
|
|
Code lang_linkage = Code::Invalid;
|
|
|
|
Code attributes = Code::Invalid;
|
|
|
|
Code array_expr = Code::Invalid;
|
|
|
|
Code specifiers = Code::Invalid;
|
|
|
|
|
|
|
|
// TODO: Parse module specifiers
|
|
|
|
|
|
|
|
// TODO: Parse attributes
|
|
|
|
if ( check( TokType::BraceSquare_Open ) )
|
|
|
|
{
|
|
|
|
eat( TokType::BraceSquare_Open );
|
|
|
|
|
|
|
|
// TODO: Need to have a parser for attributes, it gets complicated...
|
|
|
|
#if 0
|
|
|
|
Token attris_tok = currtok;
|
|
|
|
|
|
|
|
while ( left && currtok.Type != TokType::BraceSquare_Close )
|
|
|
|
{
|
|
|
|
|
|
|
|
eat( TokType::Identifier )
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2023-05-06 13:15:49 -07:00
|
|
|
attributes = untyped_str( currtok );
|
2023-05-05 15:10:03 -07:00
|
|
|
|
|
|
|
eat( TokType::BraceSquare_Close );
|
|
|
|
}
|
|
|
|
|
|
|
|
while ( left && tok_is_specifier( currtok ) )
|
|
|
|
{
|
2023-05-06 13:15:49 -07:00
|
|
|
SpecifierT spec = ESpecifier::to_type( currtok );
|
2023-05-05 15:10:03 -07:00
|
|
|
|
|
|
|
switch ( spec )
|
|
|
|
{
|
|
|
|
case ESpecifier::Constexpr:
|
|
|
|
case ESpecifier::Constinit:
|
|
|
|
case ESpecifier::External_Linkage:
|
|
|
|
case ESpecifier::Local_Persist:
|
|
|
|
case ESpecifier::Mutable:
|
|
|
|
case ESpecifier::Static_Member:
|
|
|
|
case ESpecifier::Thread_Local:
|
|
|
|
case ESpecifier::Volatile:
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
log_failure( "gen::parse_variable: invalid specifier " txt(spec) " for variable" );
|
|
|
|
return Code::Invalid;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ( spec == ESpecifier::External_Linkage )
|
|
|
|
{
|
|
|
|
specs_found[num_specifiers] = spec;
|
|
|
|
num_specifiers++;
|
|
|
|
eat( TokType::Spec_Extern );
|
|
|
|
|
|
|
|
if ( currtok.Type == TokType::String )
|
|
|
|
{
|
2023-05-06 13:15:49 -07:00
|
|
|
lang_linkage = untyped_str( currtok );
|
2023-05-05 15:10:03 -07:00
|
|
|
eat( TokType::String );
|
|
|
|
}
|
|
|
|
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
specs_found[num_specifiers] = spec;
|
|
|
|
num_specifiers++;
|
|
|
|
eat( currtok.Type );
|
|
|
|
}
|
|
|
|
|
|
|
|
if ( num_specifiers )
|
|
|
|
{
|
|
|
|
specifiers = def_specifiers( num_specifiers, specs_found );
|
|
|
|
}
|
|
|
|
|
|
|
|
Code type = parse_type( toks, txt(parse_variable) );
|
|
|
|
|
|
|
|
if ( type == Code::Invalid )
|
|
|
|
return Code::Invalid;
|
|
|
|
|
|
|
|
if ( currtok.Type != TokType::Identifier )
|
|
|
|
{
|
|
|
|
log_failure( "gen::parse_variable: expected identifier, recieved " txt(currtok.Type) );
|
|
|
|
return Code::Invalid;
|
|
|
|
}
|
|
|
|
|
|
|
|
name = currtok;
|
|
|
|
eat( TokType::Identifier );
|
|
|
|
|
|
|
|
array_expr = parse_array_decl( toks, txt(parse_variable) );
|
|
|
|
|
|
|
|
Code expr = Code::Invalid;
|
|
|
|
|
|
|
|
if ( currtok.IsAssign )
|
|
|
|
{
|
|
|
|
eat( TokType::Operator );
|
|
|
|
|
|
|
|
Token expr_tok = currtok;
|
|
|
|
|
|
|
|
if ( currtok.Type == TokType::Statement_End )
|
|
|
|
{
|
|
|
|
log_failure( "gen::parse_variable: expected expression after assignment operator" );
|
|
|
|
return Code::Invalid;
|
|
|
|
}
|
|
|
|
|
|
|
|
while ( left && currtok.Type != TokType::Statement_End )
|
|
|
|
{
|
|
|
|
expr_tok.Length = ( (sptr)currtok.Text + currtok.Length ) - (sptr)expr_tok.Text;
|
|
|
|
eat( currtok.Type );
|
|
|
|
}
|
|
|
|
|
2023-05-06 13:15:49 -07:00
|
|
|
expr = untyped_str( expr_tok );
|
2023-05-05 15:10:03 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
eat( TokType::Statement_End );
|
|
|
|
|
|
|
|
using namespace ECode;
|
|
|
|
|
|
|
|
Code result = make_code();
|
|
|
|
result->Type = Variable;
|
2023-05-06 13:15:49 -07:00
|
|
|
result->Name = get_cached_string( name );
|
2023-05-05 15:10:03 -07:00
|
|
|
|
|
|
|
result->add_entry( type );
|
|
|
|
|
|
|
|
if (array_expr)
|
|
|
|
type->add_entry( array_expr );
|
|
|
|
|
|
|
|
if ( attributes )
|
|
|
|
result->add_entry( attributes );
|
|
|
|
|
|
|
|
if (specifiers)
|
|
|
|
result->add_entry( specifiers );
|
|
|
|
|
|
|
|
if ( expr )
|
|
|
|
result->add_entry( expr );
|
|
|
|
|
|
|
|
result.lock();
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2023-05-06 12:49:43 -07:00
|
|
|
Code parse_variable( StrC def )
|
2023-05-05 15:10:03 -07:00
|
|
|
{
|
2023-05-06 12:49:43 -07:00
|
|
|
check_parse_args( parse_variable, def );
|
2023-05-05 15:10:03 -07:00
|
|
|
using namespace Parser;
|
|
|
|
|
2023-05-06 12:49:43 -07:00
|
|
|
TokArray toks = lex( def );
|
2023-05-05 15:10:03 -07:00
|
|
|
if ( toks.Arr == nullptr )
|
|
|
|
return Code::Invalid;
|
|
|
|
|
|
|
|
return parse_variable( toks, txt(parse_variable) );
|
|
|
|
}
|
|
|
|
|
2023-04-22 19:24:55 -07:00
|
|
|
// Undef helper macros
|
|
|
|
# undef check_parse_args
|
|
|
|
# undef curr_tok
|
|
|
|
# undef eat
|
|
|
|
# undef left
|
2023-04-18 19:47:59 -07:00
|
|
|
// End GEN_FEATURE_PARSING
|
|
|
|
# endif
|
2023-04-22 19:24:55 -07:00
|
|
|
#pragma endregion Parsing Constructors
|
|
|
|
|
|
|
|
#pragma region Untyped Constructors
|
|
|
|
struct TokEntry
|
|
|
|
{
|
|
|
|
char const* Str;
|
|
|
|
sw Length;
|
|
|
|
};
|
|
|
|
|
|
|
|
ZPL_TABLE( static, TokMap, tokmap_, TokEntry )
|
|
|
|
|
|
|
|
sw token_fmt_va( char* buf, uw buf_size, char const* fmt, s32 num_tokens, va_list va )
|
|
|
|
{
|
|
|
|
char const* buf_begin = buf;
|
|
|
|
sw remaining = buf_size;
|
|
|
|
|
|
|
|
TokMap tok_map;
|
|
|
|
{
|
|
|
|
tokmap_init( & tok_map, g_allocator );
|
|
|
|
|
|
|
|
s32 left = num_tokens;
|
|
|
|
|
|
|
|
while ( left-- )
|
|
|
|
{
|
|
|
|
char const* token = va_arg( va, char const* );
|
|
|
|
char const* value = va_arg( va, char const* );
|
|
|
|
|
|
|
|
TokEntry entry
|
|
|
|
{
|
|
|
|
value,
|
|
|
|
str_len(value, (sw)128)
|
|
|
|
};
|
|
|
|
|
|
|
|
u32 key = crc32( token, str_len(token, 32) );
|
|
|
|
|
|
|
|
tokmap_set( & tok_map, key, entry );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
char current = *fmt;
|
|
|
|
|
|
|
|
while ( current )
|
|
|
|
{
|
|
|
|
sw len = 0;
|
|
|
|
|
2023-06-30 00:13:41 -07:00
|
|
|
while ( current && current != '<' && remaining )
|
2023-04-22 19:24:55 -07:00
|
|
|
{
|
|
|
|
* buf = * fmt;
|
|
|
|
buf++;
|
|
|
|
fmt++;
|
2023-06-30 00:13:41 -07:00
|
|
|
remaining--;
|
2023-04-22 19:24:55 -07:00
|
|
|
|
|
|
|
current = * fmt;
|
|
|
|
}
|
|
|
|
|
2023-06-30 00:13:41 -07:00
|
|
|
if ( current == '<' )
|
2023-04-22 19:24:55 -07:00
|
|
|
{
|
2023-06-30 00:13:41 -07:00
|
|
|
char const* scanner = fmt + 1;
|
2023-04-22 19:24:55 -07:00
|
|
|
|
|
|
|
s32 tok_len = 0;
|
|
|
|
|
2023-06-30 00:13:41 -07:00
|
|
|
while ( *scanner != '>' )
|
2023-04-22 19:24:55 -07:00
|
|
|
{
|
|
|
|
tok_len++;
|
|
|
|
scanner++;
|
|
|
|
}
|
|
|
|
|
2023-06-30 00:13:41 -07:00
|
|
|
char const* token = fmt + 1;
|
2023-04-22 19:24:55 -07:00
|
|
|
|
2023-06-30 00:13:41 -07:00
|
|
|
u32 key = crc32( token, tok_len );
|
|
|
|
TokEntry* value = tokmap_get( & tok_map, key );
|
2023-04-22 19:24:55 -07:00
|
|
|
|
2023-06-30 00:13:41 -07:00
|
|
|
if ( value )
|
2023-04-22 19:24:55 -07:00
|
|
|
{
|
2023-06-30 00:13:41 -07:00
|
|
|
sw left = value->Length;
|
|
|
|
char const* str = value->Str;
|
|
|
|
|
|
|
|
while ( left-- )
|
|
|
|
{
|
|
|
|
* buf = * str;
|
|
|
|
buf++;
|
|
|
|
remaining--;
|
|
|
|
str++;
|
|
|
|
}
|
|
|
|
|
|
|
|
scanner++;
|
|
|
|
fmt = scanner;
|
|
|
|
current = * fmt;
|
|
|
|
continue;
|
2023-04-22 19:24:55 -07:00
|
|
|
}
|
|
|
|
|
2023-06-30 00:13:41 -07:00
|
|
|
* buf = * fmt;
|
|
|
|
buf++;
|
|
|
|
fmt++;
|
|
|
|
remaining--;
|
|
|
|
|
2023-04-22 19:24:55 -07:00
|
|
|
current = * fmt;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
tokmap_clear( & tok_map );
|
|
|
|
|
2023-06-30 00:13:41 -07:00
|
|
|
return buf_size - remaining;
|
2023-04-22 19:24:55 -07:00
|
|
|
}
|
2023-04-05 00:03:56 -07:00
|
|
|
|
2023-05-06 12:49:43 -07:00
|
|
|
Code untyped_str( StrC content )
|
2023-04-05 00:03:56 -07:00
|
|
|
{
|
2023-04-05 23:21:23 -07:00
|
|
|
Code
|
2023-04-05 00:03:56 -07:00
|
|
|
result = make_code();
|
2023-05-06 13:15:49 -07:00
|
|
|
result->Name = get_cached_string( content );
|
2023-04-05 00:03:56 -07:00
|
|
|
result->Type = ECode::Untyped;
|
|
|
|
result->Content = result->Name;
|
|
|
|
|
2023-04-18 21:56:43 -07:00
|
|
|
result.lock();
|
2023-04-05 00:03:56 -07:00
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2023-06-28 11:43:21 -07:00
|
|
|
Code untyped_fmt( char const* fmt, ...)
|
2023-04-05 00:03:56 -07:00
|
|
|
{
|
2023-04-05 23:21:23 -07:00
|
|
|
local_persist thread_local
|
2023-04-05 00:03:56 -07:00
|
|
|
char buf[ZPL_PRINTF_MAXLEN] = { 0 };
|
|
|
|
|
|
|
|
va_list va;
|
|
|
|
va_start(va, fmt);
|
2023-04-19 13:57:59 -07:00
|
|
|
sw length = str_fmt_va(buf, ZPL_PRINTF_MAXLEN, fmt, va);
|
2023-04-05 00:03:56 -07:00
|
|
|
va_end(va);
|
2023-04-05 23:21:23 -07:00
|
|
|
|
|
|
|
Code
|
2023-04-05 00:03:56 -07:00
|
|
|
result = make_code();
|
2023-05-06 13:15:49 -07:00
|
|
|
result->Name = get_cached_string( { str_len(fmt, MaxNameLength), fmt } );
|
2023-04-05 00:03:56 -07:00
|
|
|
result->Type = ECode::Untyped;
|
2023-05-06 13:15:49 -07:00
|
|
|
result->Content = get_cached_string( { length, buf } );
|
2023-04-05 00:03:56 -07:00
|
|
|
|
2023-04-18 21:56:43 -07:00
|
|
|
result.lock();
|
2023-04-05 00:03:56 -07:00
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
Code untyped_token_fmt( char const* fmt, s32 num_tokens, ... )
|
|
|
|
{
|
2023-04-05 23:21:23 -07:00
|
|
|
local_persist thread_local
|
2023-04-05 00:03:56 -07:00
|
|
|
char buf[ZPL_PRINTF_MAXLEN] = { 0 };
|
|
|
|
|
|
|
|
va_list va;
|
|
|
|
va_start(va, fmt);
|
2023-04-09 21:38:47 -07:00
|
|
|
sw length = token_fmt_va(buf, ZPL_PRINTF_MAXLEN, fmt, num_tokens, va);
|
2023-04-05 00:03:56 -07:00
|
|
|
va_end(va);
|
|
|
|
|
|
|
|
Code
|
2023-04-09 21:38:47 -07:00
|
|
|
result = make_code();
|
2023-05-06 13:15:49 -07:00
|
|
|
result->Name = get_cached_string( { str_len(fmt, MaxNameLength), fmt } );
|
2023-04-09 21:38:47 -07:00
|
|
|
result->Type = ECode::Untyped;
|
2023-05-06 13:15:49 -07:00
|
|
|
result->Content = get_cached_string( { length, buf } );
|
2023-04-05 00:03:56 -07:00
|
|
|
|
|
|
|
result.lock();
|
|
|
|
return result;
|
|
|
|
}
|
2023-04-22 19:24:55 -07:00
|
|
|
#pragma endregion Untyped Constructors
|
2023-04-06 16:19:11 -07:00
|
|
|
#pragma endregion Gen Interface
|
2023-04-05 23:21:23 -07:00
|
|
|
|
2023-04-07 21:29:09 -07:00
|
|
|
#pragma region Builder
|
2023-04-01 22:07:44 -07:00
|
|
|
void Builder::print( Code code )
|
|
|
|
{
|
2023-06-28 11:43:21 -07:00
|
|
|
Buffer.append_fmt( "%s\n", code.to_string() );
|
|
|
|
}
|
|
|
|
|
|
|
|
void Builder::print_fmt( char const* fmt, ... )
|
|
|
|
{
|
|
|
|
sw res;
|
|
|
|
char buf[ ZPL_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 );
|
2023-04-01 22:07:44 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
bool Builder::open( char const* path )
|
|
|
|
{
|
2023-04-09 10:59:39 -07:00
|
|
|
FileError error = file_open_mode( & File, EFileMode_WRITE, path );
|
2023-04-01 19:21:46 -07:00
|
|
|
|
2023-04-09 10:59:39 -07:00
|
|
|
if ( error != EFileError_NONE )
|
2023-04-01 22:07:44 -07:00
|
|
|
{
|
2023-04-09 10:59:39 -07:00
|
|
|
log_failure( "gen::File::open - Could not open file: %s", path);
|
2023-04-01 22:07:44 -07:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2023-05-06 13:15:49 -07:00
|
|
|
Buffer = String::make( g_allocator, "" );
|
2023-04-05 23:21:23 -07:00
|
|
|
|
2023-04-01 22:07:44 -07:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
void Builder::write()
|
|
|
|
{
|
2023-06-28 11:43:21 -07:00
|
|
|
bool result = file_write( & File, Buffer, Buffer.length() );
|
2023-04-01 22:07:44 -07:00
|
|
|
|
|
|
|
if ( result == false )
|
2023-04-09 10:59:39 -07:00
|
|
|
log_failure("gen::File::write - Failed to write to file: %s", file_name( & File ) );
|
2023-04-01 19:21:46 -07:00
|
|
|
|
2023-04-02 08:53:15 -07:00
|
|
|
// file_seek( & File, 0 );
|
2023-04-01 22:07:44 -07:00
|
|
|
file_close( & File );
|
|
|
|
}
|
2023-04-07 21:29:09 -07:00
|
|
|
#pragma endregion Builder
|
|
|
|
|
2023-04-18 19:47:59 -07:00
|
|
|
#pragma region File Lexer
|
|
|
|
// The Editor and Scanner require this lexer.
|
|
|
|
#if defined(GEN_FEATURE_EDITOR) || defined(GEN_FEATURE_SCANNER)
|
|
|
|
/*
|
|
|
|
This is a more robust lexer than the ones used for the lexer in the parse constructors interface.
|
|
|
|
Its needed to scan a C++ file and have awareness to skip content unsupported by the library.
|
|
|
|
*/
|
2023-04-22 19:24:55 -07:00
|
|
|
struct FileLexer
|
|
|
|
{
|
|
|
|
|
|
|
|
};
|
2023-04-18 19:47:59 -07:00
|
|
|
#endif
|
|
|
|
#pragma endregion File Lexer
|
|
|
|
|
2023-04-07 21:29:09 -07:00
|
|
|
#pragma region Editor
|
2023-04-18 19:47:59 -07:00
|
|
|
#ifdef GEN_FEATURE_EDITOR
|
|
|
|
#endif
|
2023-04-07 21:29:09 -07:00
|
|
|
#pragma endregion Editor
|
|
|
|
|
|
|
|
#pragma region Scanner
|
2023-04-18 19:47:59 -07:00
|
|
|
#ifdef GEN_FEATURE_SCANNER
|
|
|
|
#endif
|
2023-04-07 21:29:09 -07:00
|
|
|
#pragma endregion Scanner
|
2023-04-01 19:21:46 -07:00
|
|
|
}
|
2023-04-19 12:40:23 -07:00
|
|
|
// End: gen_time
|
2023-04-01 22:07:44 -07:00
|
|
|
#endif
|