WIP: It can parse to around ~2k lines. Need to improve its ability to detect when a forward declare of a class/enum/struct/union..

This language truly is a mess.
This commit is contained in:
Edward R. Gonzalez 2023-08-01 00:42:08 -04:00
parent 2b63fc27cd
commit 21a8f3bb39
5 changed files with 500 additions and 122 deletions

View File

@ -107,13 +107,16 @@ String AST::to_string()
} }
else else
{ {
result.append_fmt( "%s \n{\n%s\n};", Name, Body->to_string() ); result.append_fmt( "%s \n{\n%s\n}", Name, Body->to_string() );
} }
} }
else else
{ {
result.append_fmt( "class %s\n{\n%s\n};", Name, Body->to_string() ); result.append_fmt( "class %s\n{\n%s\n}", Name, Body->to_string() );
} }
if ( Parent && Parent->Type != ECode::Typedef )
result.append(";");
} }
break; break;
@ -123,9 +126,12 @@ String AST::to_string()
result.append( "export " ); result.append( "export " );
if ( Attributes ) if ( Attributes )
result.append_fmt( "class %s %s;", Attributes->to_string(), Name ); result.append_fmt( "class %s %s", Attributes->to_string(), Name );
else result.append_fmt( "class %s;", Name ); else result.append_fmt( "class %s", Name );
if ( Parent && Parent->Type != ECode::Typedef )
result.append(";");
} }
break; break;
@ -142,21 +148,24 @@ String AST::to_string()
result.append_fmt( "%s ", Attributes->to_string() ); result.append_fmt( "%s ", Attributes->to_string() );
if ( UnderlyingType ) if ( UnderlyingType )
result.append_fmt( "%s : %s\n{\n%s\n};" result.append_fmt( "%s : %s\n{\n%s\n}"
, Name , Name
, UnderlyingType->to_string() , UnderlyingType->to_string()
, Body->to_string() , Body->to_string()
); );
else result.append_fmt( "%s\n{\n%s\n};" else result.append_fmt( "%s\n{\n%s\n}"
, Name , Name
, Body->to_string() , Body->to_string()
); );
} }
else result.append_fmt( "enum %s\n{\n%s\n};" else result.append_fmt( "enum %s\n{\n%s\n}"
, Name , Name
, Body->to_string() , Body->to_string()
); );
if ( Parent && Parent->Type != ECode::Typedef )
result.append(";");
} }
break; break;
@ -168,7 +177,10 @@ String AST::to_string()
if ( Attributes ) if ( Attributes )
result.append_fmt( "%s ", Attributes->to_string() ); result.append_fmt( "%s ", Attributes->to_string() );
result.append_fmt( "enum %s : %s;", Name, UnderlyingType->to_string() ); result.append_fmt( "enum %s : %s", Name, UnderlyingType->to_string() );
if ( Parent && Parent->Type != ECode::Typedef )
result.append(";");
} }
break; break;
@ -188,7 +200,7 @@ String AST::to_string()
if ( UnderlyingType ) if ( UnderlyingType )
{ {
result.append_fmt( "%s : %s\n{\n%s\n};" result.append_fmt( "%s : %s\n{\n%s\n}"
, Name , Name
, UnderlyingType->to_string() , UnderlyingType->to_string()
, Body->to_string() , Body->to_string()
@ -196,7 +208,7 @@ String AST::to_string()
} }
else else
{ {
result.append_fmt( "%s\n{\n%s\n};" result.append_fmt( "%s\n{\n%s\n}"
, Name , Name
, Body->to_string() , Body->to_string()
); );
@ -204,10 +216,13 @@ String AST::to_string()
} }
else else
{ {
result.append_fmt( "enum class %s\n{\n%s\n};" result.append_fmt( "enum class %s\n{\n%s\n}"
, Body->to_string() , Body->to_string()
); );
} }
if ( Parent && Parent->Type != ECode::Typedef )
result.append(";");
} }
break; break;
@ -221,7 +236,10 @@ String AST::to_string()
if ( Attributes ) if ( Attributes )
result.append_fmt( "%s ", Attributes->to_string() ); result.append_fmt( "%s ", Attributes->to_string() );
result.append_fmt( "%s : %s;", Name, UnderlyingType->to_string() ); result.append_fmt( "%s : %s", Name, UnderlyingType->to_string() );
if ( Parent && Parent->Type != ECode::Typedef )
result.append(";");
} }
break; break;
@ -587,13 +605,16 @@ String AST::to_string()
{ {
if ( Name ) if ( Name )
result.append_fmt( "%s \n{\n%s\n};", Name, Body->to_string() ); result.append_fmt( "%s \n{\n%s\n}", Name, Body->to_string() );
} }
} }
else else
{ {
result.append_fmt( "struct %s\n{\n%s\n};", Name, Body->to_string() ); result.append_fmt( "struct %s\n{\n%s\n}", Name, Body->to_string() );
} }
if ( Parent && Parent->Type != ECode::Typedef )
result.append(";");
} }
break; break;
@ -603,9 +624,12 @@ String AST::to_string()
result.append( "export " ); result.append( "export " );
if ( Attributes ) if ( Attributes )
result.append_fmt( "struct %s %s;", Attributes->to_string(), Name ); result.append_fmt( "struct %s %s", Attributes->to_string(), Name );
else result.append_fmt( "struct %s;", Name ); else result.append_fmt( "struct %s", Name );
if ( Parent && Parent->Type != ECode::Typedef )
result.append(";");
} }
break; break;
@ -670,7 +694,7 @@ String AST::to_string()
if ( Name ) if ( Name )
{ {
result.append_fmt( "%s\n{\n%s\n};" result.append_fmt( "%s\n{\n%s\n}"
, Name , Name
, Body->to_string() , Body->to_string()
); );
@ -678,10 +702,13 @@ String AST::to_string()
else else
{ {
// Anonymous union // Anonymous union
result.append_fmt( "\n{\n%s\n};" result.append_fmt( "\n{\n%s\n}"
, Body->to_string() , Body->to_string()
); );
} }
if ( Parent && Parent->Type != ECode::Typedef )
result.append(";");
} }
break; break;

View File

@ -489,6 +489,12 @@ struct CodeSpecifiers
bool append( SpecifierT spec ) bool append( SpecifierT spec )
{ {
if ( ast == nullptr )
{
log_failure("CodeSpecifiers: Attempted to append to a null specifiers AST!");
return false;
}
if ( raw()->NumEntries == AST::ArrSpecs_Cap ) if ( raw()->NumEntries == AST::ArrSpecs_Cap )
{ {
log_failure("CodeSpecifiers: Attempted to append over %d specifiers to a specifiers AST!", AST::ArrSpecs_Cap ); log_failure("CodeSpecifiers: Attempted to append over %d specifiers to a specifiers AST!", AST::ArrSpecs_Cap );

View File

@ -6,7 +6,7 @@ namespace Parser
For the sake of scanning files, it can scan preprocessor directives For the sake of scanning files, it can scan preprocessor directives
Attributes_Start is only used to indicate the start of the user_defined attribute list. __Attributes_Start is only used to indicate the start of the user_defined attribute list.
*/ */
#ifndef GEN_DEFINE_ATTRIBUTE_TOKENS #ifndef GEN_DEFINE_ATTRIBUTE_TOKENS
@ -33,8 +33,8 @@ namespace Parser
Entry( BraceSquare_Close, "]" ) \ Entry( BraceSquare_Close, "]" ) \
Entry( Capture_Start, "(" ) \ Entry( Capture_Start, "(" ) \
Entry( Capture_End, ")" ) \ Entry( Capture_End, ")" ) \
Entry( Comment, "comment" ) \ Entry( Comment, "__comment__" ) \
Entry( Char, "character" ) \ Entry( Char, "__character__" ) \
Entry( Comma, "," ) \ Entry( Comma, "," ) \
Entry( Decl_Class, "class" ) \ Entry( Decl_Class, "class" ) \
Entry( Decl_GNU_Attribute, "__attribute__" ) \ Entry( Decl_GNU_Attribute, "__attribute__" ) \
@ -50,11 +50,11 @@ namespace Parser
Entry( Decl_Typedef, "typedef" ) \ Entry( Decl_Typedef, "typedef" ) \
Entry( Decl_Using, "using" ) \ Entry( Decl_Using, "using" ) \
Entry( Decl_Union, "union" ) \ Entry( Decl_Union, "union" ) \
Entry( Identifier, "identifier" ) \ Entry( Identifier, "__identifier__" ) \
Entry( Module_Import, "import" ) \ Entry( Module_Import, "import" ) \
Entry( Module_Export, "export" ) \ Entry( Module_Export, "export" ) \
Entry( Number, "number" ) \ Entry( Number, "__number__" ) \
Entry( Operator, "operator" ) \ Entry( Operator, "__operator__" ) \
Entry( Preprocess_Define, "define") \ Entry( Preprocess_Define, "define") \
Entry( Preprocess_If, "if") \ Entry( Preprocess_If, "if") \
Entry( Preprocess_IfDef, "ifdef") \ Entry( Preprocess_IfDef, "ifdef") \
@ -64,8 +64,9 @@ namespace Parser
Entry( Preprocess_EndIf, "endif") \ Entry( Preprocess_EndIf, "endif") \
Entry( Preprocess_Include, "include" ) \ Entry( Preprocess_Include, "include" ) \
Entry( Preprocess_Pragma, "pragma") \ Entry( Preprocess_Pragma, "pragma") \
Entry( Preprocess_Content, "macro content") \ Entry( Preprocess_Content, "__macro_content__") \
Entry( Preprocess_Macro, "macro") \ Entry( Preprocess_Macro, "__macro__") \
Entry( Preprocess_Unsupported, "__unsupported__" ) \
Entry( Spec_Alignas, "alignas" ) \ Entry( Spec_Alignas, "alignas" ) \
Entry( Spec_Const, "const" ) \ Entry( Spec_Const, "const" ) \
Entry( Spec_Consteval, "consteval" ) \ Entry( Spec_Consteval, "consteval" ) \
@ -85,7 +86,8 @@ namespace Parser
Entry( Spec_Volatile, "volatile") \ Entry( Spec_Volatile, "volatile") \
Entry( Star, "*" ) \ Entry( Star, "*" ) \
Entry( Statement_End, ";" ) \ Entry( Statement_End, ";" ) \
Entry( String, "string" ) \ Entry( StaticAssert, "static_assert" ) \
Entry( String, "__string__" ) \
Entry( Type_Unsigned, "unsigned" ) \ Entry( Type_Unsigned, "unsigned" ) \
Entry( Type_Signed, "signed" ) \ Entry( Type_Signed, "signed" ) \
Entry( Type_Short, "short" ) \ Entry( Type_Short, "short" ) \
@ -93,8 +95,13 @@ namespace Parser
Entry( Type_char, "char" ) \ Entry( Type_char, "char" ) \
Entry( Type_int, "int" ) \ Entry( Type_int, "int" ) \
Entry( Type_double, "double" ) \ Entry( Type_double, "double" ) \
Entry( Type_MS_int8, "__int8" ) \
Entry( Type_MS_int16, "__int16" ) \
Entry( Type_MS_int32, "__int32" ) \
Entry( Type_MS_int64, "__int64" ) \
Entry( Type_MS_W64, "_W64" ) \
Entry( Varadic_Argument, "..." ) \ Entry( Varadic_Argument, "..." ) \
Entry( Attributes_Start, "__attrib_start__" ) Entry( __Attributes_Start, "__attrib_start__" )
namespace ETokType namespace ETokType
{ {

File diff suppressed because it is too large Load Diff

View File

@ -15,8 +15,8 @@ BraceSquare_Open, "["
BraceSquare_Close, "]" BraceSquare_Close, "]"
Capture_Start, "(" Capture_Start, "("
Capture_End, ")" Capture_End, ")"
Comment, "comemnt" Comment, "__comemnt__"
Char, "character" Char, "__character__"
Comma, "," Comma, ","
Decl_Class, "class" Decl_Class, "class"
Decl_GNU_Attribute, "__attribute__" Decl_GNU_Attribute, "__attribute__"
@ -26,18 +26,18 @@ Decl_Extern_Linkage, "extern"
Decl_Friend, "friend" Decl_Friend, "friend"
Decl_Module, "module" Decl_Module, "module"
Decl_Namespace, "namespace" Decl_Namespace, "namespace"
Decl_Operator, "operator" Decl_Operator, "__operator__"
Decl_Struct, "struct" Decl_Struct, "struct"
Decl_Template, "template" Decl_Template, "template"
Decl_Typedef, "typedef" Decl_Typedef, "typedef"
Decl_Using, "using" Decl_Using, "using"
Decl_Union, "union" Decl_Union, "union"
Identifier, "identifier" Identifier, "__identifier__"
Module_Import, "import" Module_Import, "import"
Module_Export, "export" Module_Export, "export"
Number, "number" Number, "__number__"
Operator, "operator" Operator, "__operator__"
Preprocess_Define, "#define" Preprocess_Define, "define"
Preprocess_If, "if" Preprocess_If, "if"
Preprocess_IfDef, "ifdef" Preprocess_IfDef, "ifdef"
Preprocess_IfNotDef, "ifndef" Preprocess_IfNotDef, "ifndef"
@ -46,7 +46,8 @@ Preprocess_Else, "else"
Preprocess_EndIf, "endif" Preprocess_EndIf, "endif"
Preprocess_Include, "include" Preprocess_Include, "include"
Preprocess_Pragma, "pragma" Preprocess_Pragma, "pragma"
Preprocess_Macro, "macro" Preprocess_Macro, "__macro__"
Preprocess_Unsupported, "__unsupported__"
Spec_Alignas, "alignas" Spec_Alignas, "alignas"
Spec_Const, "const" Spec_Const, "const"
Spec_Consteval, "consteval" Spec_Consteval, "consteval"
@ -66,7 +67,8 @@ Spec_ThreadLocal, "thread_local"
Spec_Volatile, "volatile" Spec_Volatile, "volatile"
Star, "*" Star, "*"
Statement_End, ";" Statement_End, ";"
String, "string" StaticAssert, "static_assert"
String, "__string__"
Type_Unsigned, "unsigned" Type_Unsigned, "unsigned"
Type_Signed, "signed" Type_Signed, "signed"
Type_Short, "short" Type_Short, "short"
@ -74,5 +76,10 @@ Type_Long, "long"
Type_char, "char" Type_char, "char"
Type_int, "int" Type_int, "int"
Type_double, "double" Type_double, "double"
Type_MS_int8, "__int8"
Type_MS_int16, "__int16"
Type_MS_int32, "__int32"
Type_MS_int64, "__int64"
Type_MS_W64, "_W64"
Varadic_Argument, "..." Varadic_Argument, "..."
Attributes_Start, "__attrib_start__" __Attributes_Start, "__attrib_start__"

1 Invalid __invalid__
15 BraceSquare_Close ]
16 Capture_Start (
17 Capture_End )
18 Comment comemnt __comemnt__
19 Char character __character__
20 Comma ,
21 Decl_Class class
22 Decl_GNU_Attribute __attribute__
26 Decl_Friend friend
27 Decl_Module module
28 Decl_Namespace namespace
29 Decl_Operator operator __operator__
30 Decl_Struct struct
31 Decl_Template template
32 Decl_Typedef typedef
33 Decl_Using using
34 Decl_Union union
35 Identifier identifier __identifier__
36 Module_Import import
37 Module_Export export
38 Number number __number__
39 Operator operator __operator__
40 Preprocess_Define #define define
41 Preprocess_If if
42 Preprocess_IfDef ifdef
43 Preprocess_IfNotDef ifndef
46 Preprocess_EndIf endif
47 Preprocess_Include include
48 Preprocess_Pragma pragma
49 Preprocess_Macro macro __macro__
50 Preprocess_Unsupported __unsupported__
51 Spec_Alignas alignas
52 Spec_Const const
53 Spec_Consteval consteval
67 Spec_Volatile volatile
68 Star *
69 Statement_End ;
70 String StaticAssert string static_assert
71 String __string__
72 Type_Unsigned unsigned
73 Type_Signed signed
74 Type_Short short
76 Type_char char
77 Type_int int
78 Type_double double
79 Type_MS_int8 __int8
80 Type_MS_int16 __int16
81 Type_MS_int32 __int32
82 Type_MS_int64 __int64
83 Type_MS_W64 _W64
84 Varadic_Argument ...
85 Attributes_Start __Attributes_Start __attrib_start__