2023-07-24 14:45:27 -07:00
|
|
|
/*
|
|
|
|
These constructors are the most implementation intensive other than the editor or scanner.
|
|
|
|
*/
|
|
|
|
|
|
|
|
namespace Parser
|
|
|
|
{
|
2023-07-27 14:12:58 -07:00
|
|
|
struct Token
|
|
|
|
{
|
|
|
|
char const* Text;
|
|
|
|
sptr Length;
|
|
|
|
TokType Type;
|
|
|
|
bool IsAssign;
|
2023-07-28 18:44:31 -07:00
|
|
|
s32 Line;
|
|
|
|
s32 Column;
|
2023-07-29 02:52:06 -07:00
|
|
|
// TokFlags Flags;
|
2023-07-27 14:12:58 -07:00
|
|
|
|
|
|
|
operator bool()
|
|
|
|
{
|
|
|
|
return Text && Length && Type != TokType::Invalid;
|
|
|
|
}
|
|
|
|
|
|
|
|
operator StrC()
|
|
|
|
{
|
|
|
|
return { Length, Text };
|
|
|
|
}
|
|
|
|
|
2023-07-28 18:44:31 -07:00
|
|
|
bool is_access_specifier()
|
|
|
|
{
|
|
|
|
return Type >= TokType::Access_Private && Type <= TokType::Access_Public;
|
|
|
|
}
|
2023-07-27 14:12:58 -07:00
|
|
|
|
2023-07-28 18:44:31 -07:00
|
|
|
bool is_attribute()
|
|
|
|
{
|
|
|
|
return Type > TokType::Attributes_Start;
|
|
|
|
}
|
2023-07-27 14:12:58 -07:00
|
|
|
|
2023-07-28 18:44:31 -07:00
|
|
|
bool is_preprocessor()
|
|
|
|
{
|
|
|
|
return Type >= TokType::Preprocess_Define && Type <= TokType::Preprocess_EndIf;
|
|
|
|
}
|
2023-07-27 14:12:58 -07:00
|
|
|
|
2023-07-28 18:44:31 -07:00
|
|
|
bool is_specifier()
|
|
|
|
{
|
|
|
|
return (Type <= TokType::Star && Type >= TokType::Spec_Alignas)
|
|
|
|
|| Type == TokType::Ampersand
|
|
|
|
|| Type == TokType::Ampersand_DBL
|
|
|
|
;
|
|
|
|
}
|
|
|
|
|
|
|
|
AccessSpec to_access_specifier()
|
|
|
|
{
|
|
|
|
return scast(AccessSpec, Type);
|
|
|
|
}
|
|
|
|
};
|
2023-07-27 14:12:58 -07:00
|
|
|
|
2023-07-29 02:52:06 -07:00
|
|
|
constexpr Token NullToken { nullptr, 0, TokType::Invalid, false, 0, 0 };
|
|
|
|
|
2023-07-24 14:45:27 -07:00
|
|
|
struct TokArray
|
|
|
|
{
|
|
|
|
Array<Token> Arr;
|
|
|
|
s32 Idx;
|
|
|
|
|
2023-07-28 18:44:31 -07:00
|
|
|
bool __eat( TokType type );
|
2023-07-24 14:45:27 -07:00
|
|
|
|
|
|
|
Token& current()
|
|
|
|
{
|
|
|
|
return Arr[Idx];
|
|
|
|
}
|
|
|
|
|
|
|
|
Token& previous()
|
|
|
|
{
|
|
|
|
return Arr[Idx - 1];
|
|
|
|
}
|
|
|
|
|
|
|
|
Token* next()
|
|
|
|
{
|
|
|
|
return Idx + 1 < Arr.num() ? &Arr[Idx + 1] : nullptr;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2023-07-28 18:44:31 -07:00
|
|
|
struct StackNode
|
|
|
|
{
|
|
|
|
StackNode* Prev;
|
|
|
|
|
|
|
|
Token Name; // The name of the AST node (if parsed)
|
|
|
|
StrC ProcName; // The name of the procedure
|
|
|
|
};
|
|
|
|
|
|
|
|
struct ParseContext
|
|
|
|
{
|
|
|
|
TokArray Tokens;
|
|
|
|
StackNode* Scope;
|
|
|
|
|
|
|
|
String to_string()
|
|
|
|
{
|
|
|
|
String result = String::make_reserve( GlobalAllocator, kilobytes(4) );
|
|
|
|
|
|
|
|
result.append_fmt("\tContext:\n");
|
|
|
|
|
2023-07-29 10:15:53 -07:00
|
|
|
Token last_valid = Tokens.Idx >= Tokens.Arr.num() ? Tokens.Arr[Tokens.Arr.num() -1] : Tokens.current();
|
|
|
|
|
|
|
|
char const* current = last_valid.Text;
|
|
|
|
sptr length = last_valid.Length;
|
|
|
|
while ( current <= Tokens.Arr.back().Text && *current != '\n' )
|
2023-07-29 02:52:06 -07:00
|
|
|
{
|
|
|
|
current++;
|
2023-07-29 10:15:53 -07:00
|
|
|
length++;
|
2023-07-29 02:52:06 -07:00
|
|
|
}
|
|
|
|
|
2023-07-29 10:15:53 -07:00
|
|
|
String line = String::make( GlobalAllocator, { length, last_valid.Text } );
|
|
|
|
result.append_fmt("\t(%d, %d): %s\n", last_valid.Line, last_valid.Column, line );
|
2023-07-29 02:52:06 -07:00
|
|
|
line.free();
|
|
|
|
|
2023-07-29 03:32:16 -07:00
|
|
|
StackNode* curr_scope = Scope;
|
2023-07-28 18:44:31 -07:00
|
|
|
do
|
|
|
|
{
|
2023-07-29 03:32:16 -07:00
|
|
|
if ( curr_scope->Name )
|
2023-07-29 02:52:06 -07:00
|
|
|
{
|
2023-07-29 10:15:53 -07:00
|
|
|
result.append_fmt("\tProcedure: %s, AST Name: %s\n", curr_scope->ProcName.Ptr, (StrC)curr_scope->Name );
|
2023-07-29 02:52:06 -07:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2023-07-29 10:15:53 -07:00
|
|
|
result.append_fmt("\tProcedure: %s\n", curr_scope->ProcName.Ptr );
|
2023-07-29 02:52:06 -07:00
|
|
|
}
|
2023-07-28 18:44:31 -07:00
|
|
|
|
2023-07-29 03:32:16 -07:00
|
|
|
curr_scope = curr_scope->Prev;
|
2023-07-28 18:44:31 -07:00
|
|
|
}
|
2023-07-29 10:15:53 -07:00
|
|
|
while ( curr_scope );
|
2023-07-28 18:44:31 -07:00
|
|
|
return result;
|
|
|
|
}
|
2023-07-29 02:52:06 -07:00
|
|
|
|
|
|
|
void push( StackNode* node )
|
|
|
|
{
|
|
|
|
node->Prev = Scope;
|
|
|
|
Scope = node;
|
|
|
|
}
|
|
|
|
|
|
|
|
void pop()
|
|
|
|
{
|
2023-07-29 03:32:16 -07:00
|
|
|
Scope = Scope->Prev;
|
2023-07-29 02:52:06 -07:00
|
|
|
}
|
2023-07-28 18:44:31 -07:00
|
|
|
};
|
|
|
|
|
|
|
|
global ParseContext Context;
|
|
|
|
|
|
|
|
bool TokArray::__eat( TokType type )
|
|
|
|
{
|
|
|
|
if ( Arr.num() - Idx <= 0 )
|
|
|
|
{
|
2023-07-29 02:52:06 -07:00
|
|
|
log_failure( "No tokens left.\n%s", Context.to_string() );
|
2023-07-28 18:44:31 -07:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ( Arr[Idx].Type != type )
|
|
|
|
{
|
|
|
|
String token_str = String::make( GlobalAllocator, { Arr[Idx].Length, Arr[Idx].Text } );
|
|
|
|
|
2023-07-29 02:52:06 -07:00
|
|
|
log_failure( "Parse Error, TokArray::eat, Expected: %s, not %s (%d, %d)`\n%s", ETokType::to_str(type), token_str, Context.to_string() );
|
2023-07-28 18:44:31 -07:00
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
Idx++;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
enum TokFlags : u32
|
|
|
|
{
|
|
|
|
IsAssign = bit(0),
|
|
|
|
};
|
|
|
|
|
|
|
|
TokArray lex( StrC content, bool keep_preprocess_directives = true )
|
2023-07-24 14:45:27 -07:00
|
|
|
{
|
|
|
|
# define current ( * scanner )
|
|
|
|
|
2023-07-28 18:44:31 -07:00
|
|
|
# define move_forward() \
|
|
|
|
{ \
|
|
|
|
if ( current == '\n' ) \
|
|
|
|
{ \
|
|
|
|
line++; \
|
|
|
|
column = 0; \
|
|
|
|
} \
|
|
|
|
else \
|
|
|
|
{ \
|
|
|
|
column++; \
|
|
|
|
} \
|
|
|
|
left--; \
|
|
|
|
scanner++; \
|
|
|
|
}
|
2023-07-24 14:45:27 -07:00
|
|
|
|
|
|
|
# define SkipWhitespace() \
|
|
|
|
while ( left && char_is_space( current ) ) \
|
|
|
|
{ \
|
|
|
|
move_forward(); \
|
|
|
|
}
|
|
|
|
|
|
|
|
# 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 }; \
|
|
|
|
}
|
|
|
|
|
|
|
|
local_persist thread_local
|
|
|
|
Array<Token> Tokens = { nullptr };
|
|
|
|
|
|
|
|
s32 left = content.Len;
|
|
|
|
char const* scanner = content.Ptr;
|
|
|
|
|
|
|
|
char const* word = scanner;
|
|
|
|
s32 word_length = 0;
|
|
|
|
|
2023-07-28 18:44:31 -07:00
|
|
|
s32 line = 0;
|
|
|
|
s32 column = 0;
|
|
|
|
|
2023-07-24 14:45:27 -07:00
|
|
|
SkipWhitespace();
|
|
|
|
if ( left <= 0 )
|
|
|
|
{
|
|
|
|
log_failure( "gen::lex: no tokens found (only whitespace provided)" );
|
|
|
|
return { { nullptr }, 0 };
|
|
|
|
}
|
|
|
|
|
|
|
|
if ( Tokens )
|
|
|
|
{
|
|
|
|
Tokens.free();
|
|
|
|
}
|
|
|
|
|
|
|
|
Tokens = Array<Token>::init_reserve( LexArena, content.Len / 6 );
|
|
|
|
|
|
|
|
while (left )
|
|
|
|
{
|
2023-07-28 18:44:31 -07:00
|
|
|
Token token = { nullptr, 0, TokType::Invalid, false, line, column };
|
2023-07-24 14:45:27 -07:00
|
|
|
|
|
|
|
SkipWhitespace();
|
|
|
|
if ( left <= 0 )
|
|
|
|
break;
|
|
|
|
|
|
|
|
switch ( current )
|
|
|
|
{
|
2023-07-29 02:52:06 -07:00
|
|
|
// TODO : Need to handle the preprocessor as a separate pass.
|
2023-07-24 14:45:27 -07:00
|
|
|
case '#':
|
|
|
|
token.Text = scanner;
|
|
|
|
token.Length = 1;
|
|
|
|
move_forward();
|
|
|
|
|
|
|
|
while (left && current != '\n' )
|
|
|
|
{
|
2023-07-28 18:44:31 -07:00
|
|
|
if ( token.Type == ETokType::Invalid && current == ' ' )
|
|
|
|
{
|
|
|
|
token.Type = ETokType::to_type( token );
|
|
|
|
}
|
|
|
|
|
2023-07-24 14:45:27 -07:00
|
|
|
if ( current == '\\' )
|
|
|
|
{
|
|
|
|
move_forward();
|
|
|
|
|
|
|
|
if ( current != '\n' && keep_preprocess_directives )
|
|
|
|
{
|
|
|
|
log_failure( "gen::lex: invalid preprocessor directive, will still grab but will not compile %s", token.Text );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
move_forward();
|
|
|
|
token.Length++;
|
|
|
|
}
|
|
|
|
goto FoundToken;
|
|
|
|
|
|
|
|
case '.':
|
|
|
|
token.Text = scanner;
|
|
|
|
token.Length = 1;
|
|
|
|
token.Type = TokType::Access_MemberSymbol;
|
|
|
|
|
2023-07-28 18:44:31 -07:00
|
|
|
if (left) {
|
2023-07-24 14:45:27 -07:00
|
|
|
move_forward();
|
2023-07-28 18:44:31 -07:00
|
|
|
}
|
2023-07-24 14:45:27 -07:00
|
|
|
|
|
|
|
if ( current == '.' )
|
|
|
|
{
|
|
|
|
move_forward();
|
|
|
|
if( current == '.' )
|
|
|
|
{
|
|
|
|
token.Length = 3;
|
|
|
|
token.Type = TokType::Varadic_Argument;
|
|
|
|
move_forward();
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
log_failure( "gen::lex: invalid varadic argument, expected '...' got '..%c'", current );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
goto FoundToken;
|
|
|
|
|
|
|
|
case '&' :
|
|
|
|
token.Text = scanner;
|
|
|
|
token.Length = 1;
|
|
|
|
token.Type = TokType::Ampersand;
|
|
|
|
|
|
|
|
if (left)
|
|
|
|
move_forward();
|
|
|
|
|
|
|
|
if ( current == '&' ) // &&
|
|
|
|
{
|
|
|
|
token.Length = 2;
|
|
|
|
token.Type = TokType::Ampersand_DBL;
|
|
|
|
|
|
|
|
if (left)
|
|
|
|
move_forward();
|
|
|
|
}
|
|
|
|
|
|
|
|
goto FoundToken;
|
|
|
|
|
|
|
|
case ':':
|
|
|
|
token.Text = scanner;
|
|
|
|
token.Length = 1;
|
|
|
|
token.Type = TokType::Assign_Classifer;
|
|
|
|
|
|
|
|
if (left)
|
|
|
|
move_forward();
|
|
|
|
|
|
|
|
if ( current == ':' )
|
|
|
|
{
|
|
|
|
move_forward();
|
|
|
|
token.Type = TokType::Access_StaticSymbol;
|
|
|
|
token.Length++;
|
|
|
|
}
|
|
|
|
goto FoundToken;
|
|
|
|
|
|
|
|
case '{':
|
|
|
|
token.Text = scanner;
|
|
|
|
token.Length = 1;
|
|
|
|
token.Type = TokType::BraceCurly_Open;
|
|
|
|
|
|
|
|
if (left)
|
|
|
|
move_forward();
|
|
|
|
goto FoundToken;
|
|
|
|
|
|
|
|
case '}':
|
|
|
|
token.Text = scanner;
|
|
|
|
token.Length = 1;
|
|
|
|
token.Type = TokType::BraceCurly_Close;
|
|
|
|
|
|
|
|
if (left)
|
|
|
|
move_forward();
|
|
|
|
goto FoundToken;
|
|
|
|
|
|
|
|
case '[':
|
|
|
|
token.Text = scanner;
|
|
|
|
token.Length = 1;
|
|
|
|
token.Type = TokType::BraceSquare_Open;
|
|
|
|
if ( left )
|
|
|
|
{
|
|
|
|
move_forward();
|
|
|
|
|
|
|
|
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;
|
|
|
|
|
|
|
|
if (left)
|
|
|
|
move_forward();
|
|
|
|
goto FoundToken;
|
|
|
|
|
|
|
|
case '(':
|
|
|
|
token.Text = scanner;
|
|
|
|
token.Length = 1;
|
|
|
|
token.Type = TokType::Capture_Start;
|
|
|
|
|
|
|
|
if (left)
|
|
|
|
move_forward();
|
|
|
|
goto FoundToken;
|
|
|
|
|
|
|
|
case ')':
|
|
|
|
token.Text = scanner;
|
|
|
|
token.Length = 1;
|
|
|
|
token.Type = TokType::Capture_End;
|
|
|
|
|
|
|
|
if (left)
|
|
|
|
move_forward();
|
|
|
|
goto FoundToken;
|
|
|
|
|
|
|
|
case '\'':
|
|
|
|
token.Text = scanner;
|
|
|
|
token.Length = 1;
|
|
|
|
token.Type = TokType::Char;
|
|
|
|
|
|
|
|
move_forward();
|
|
|
|
|
|
|
|
while ( left && current != '\'' )
|
|
|
|
{
|
|
|
|
move_forward();
|
|
|
|
token.Length++;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ( left )
|
|
|
|
{
|
|
|
|
move_forward();
|
|
|
|
token.Length++;
|
|
|
|
}
|
|
|
|
goto FoundToken;
|
|
|
|
|
|
|
|
case ',':
|
|
|
|
token.Text = scanner;
|
|
|
|
token.Length = 1;
|
|
|
|
token.Type = TokType::Comma;
|
|
|
|
|
|
|
|
if (left)
|
|
|
|
move_forward();
|
|
|
|
goto FoundToken;
|
|
|
|
|
|
|
|
case '*':
|
|
|
|
token.Text = scanner;
|
|
|
|
token.Length = 1;
|
|
|
|
token.Type = TokType::Star;
|
|
|
|
|
|
|
|
if (left)
|
|
|
|
move_forward();
|
|
|
|
goto FoundToken;
|
|
|
|
|
|
|
|
case ';':
|
|
|
|
token.Text = scanner;
|
|
|
|
token.Length = 1;
|
|
|
|
token.Type = TokType::Statement_End;
|
|
|
|
|
|
|
|
if (left)
|
|
|
|
move_forward();
|
|
|
|
goto FoundToken;
|
|
|
|
|
|
|
|
case '"':
|
|
|
|
token.Text = scanner;
|
|
|
|
token.Length = 1;
|
|
|
|
token.Type = TokType::String;
|
|
|
|
|
|
|
|
move_forward();
|
|
|
|
while ( left )
|
|
|
|
{
|
|
|
|
if ( current == '"' )
|
|
|
|
{
|
|
|
|
move_forward();
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ( current == '\\' )
|
|
|
|
{
|
|
|
|
move_forward();
|
|
|
|
token.Length++;
|
|
|
|
|
|
|
|
if ( left )
|
|
|
|
{
|
|
|
|
move_forward();
|
|
|
|
token.Length++;
|
|
|
|
}
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
move_forward();
|
|
|
|
token.Length++;
|
|
|
|
}
|
|
|
|
goto FoundToken;
|
|
|
|
|
|
|
|
// All other operators we just label as an operator and move forward.
|
|
|
|
case '=':
|
|
|
|
token.Text = scanner;
|
|
|
|
token.Length = 1;
|
|
|
|
token.Type = TokType::Operator;
|
|
|
|
token.IsAssign = true;
|
|
|
|
|
|
|
|
if (left)
|
|
|
|
move_forward();
|
|
|
|
|
|
|
|
goto FoundToken;
|
|
|
|
|
|
|
|
case '+':
|
|
|
|
case '%':
|
|
|
|
case '^':
|
|
|
|
case '~':
|
|
|
|
case '!':
|
|
|
|
case '<':
|
|
|
|
case '>':
|
|
|
|
case '|':
|
|
|
|
token.Text = scanner;
|
|
|
|
token.Length = 1;
|
|
|
|
token.Type = TokType::Operator;
|
|
|
|
|
|
|
|
if (left)
|
|
|
|
move_forward();
|
|
|
|
|
|
|
|
if ( current == '=' )
|
|
|
|
{
|
|
|
|
token.Length++;
|
|
|
|
token.IsAssign = true;
|
|
|
|
|
|
|
|
if (left)
|
|
|
|
move_forward();
|
|
|
|
}
|
|
|
|
else while ( left && current == *(scanner - 1) && token.Length < 3 )
|
|
|
|
{
|
|
|
|
token.Length++;
|
|
|
|
|
|
|
|
if (left)
|
|
|
|
move_forward();
|
|
|
|
}
|
|
|
|
goto FoundToken;
|
|
|
|
|
|
|
|
// Dash is unfortunatlly a bit more complicated...
|
|
|
|
case '-':
|
|
|
|
token.Text = scanner;
|
|
|
|
token.Length = 1;
|
|
|
|
token.Type = TokType::Operator;
|
|
|
|
if ( left )
|
|
|
|
{
|
|
|
|
move_forward();
|
|
|
|
|
|
|
|
if ( current == '>' )
|
|
|
|
{
|
|
|
|
token.Length++;
|
|
|
|
move_forward();
|
|
|
|
|
|
|
|
if ( current == '*' )
|
|
|
|
{
|
|
|
|
token.Length++;
|
|
|
|
move_forward();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else if ( current == '=' )
|
|
|
|
{
|
|
|
|
token.Length++;
|
|
|
|
token.IsAssign = true;
|
|
|
|
|
|
|
|
if (left)
|
|
|
|
move_forward();
|
|
|
|
}
|
|
|
|
else while ( left && current == *(scanner - 1) && token.Length < 3 )
|
|
|
|
{
|
|
|
|
token.Length++;
|
|
|
|
|
|
|
|
if (left)
|
|
|
|
move_forward();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
goto FoundToken;
|
|
|
|
|
|
|
|
case '/':
|
|
|
|
token.Text = scanner;
|
|
|
|
token.Length = 1;
|
|
|
|
token.Type = TokType::Operator;
|
|
|
|
|
|
|
|
if ( left )
|
|
|
|
{
|
|
|
|
move_forward();
|
|
|
|
|
|
|
|
if ( current == '/' )
|
|
|
|
{
|
|
|
|
token.Type = TokType::Comment;
|
|
|
|
|
|
|
|
move_forward();
|
|
|
|
token.Text = scanner;
|
|
|
|
token.Length = 0;
|
|
|
|
|
|
|
|
while ( left && current != '\n' )
|
|
|
|
{
|
|
|
|
move_forward();
|
|
|
|
token.Length++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else if ( current == '*' )
|
|
|
|
{
|
|
|
|
token.Type = TokType::Comment;
|
|
|
|
|
|
|
|
move_forward();
|
|
|
|
token.Text = scanner;
|
|
|
|
token.Length = 0;
|
|
|
|
|
|
|
|
while ( left && ( current != '*' && *(scanner + 1) != '/' ) )
|
|
|
|
{
|
|
|
|
move_forward();
|
|
|
|
token.Length++;
|
|
|
|
}
|
|
|
|
move_forward();
|
|
|
|
move_forward();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
goto FoundToken;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ( char_is_alpha( current ) || current == '_' )
|
|
|
|
{
|
|
|
|
token.Text = scanner;
|
|
|
|
token.Length = 1;
|
|
|
|
move_forward();
|
|
|
|
|
|
|
|
while ( left && ( char_is_alphanumeric(current) || current == '_' ) )
|
|
|
|
{
|
|
|
|
move_forward();
|
|
|
|
token.Length++;
|
|
|
|
}
|
|
|
|
|
|
|
|
goto FoundToken;
|
|
|
|
}
|
|
|
|
else if ( char_is_digit(current) )
|
|
|
|
{
|
|
|
|
// This is a very brute force lex, no checks are done for validity of literal.
|
|
|
|
|
|
|
|
token.Text = scanner;
|
|
|
|
token.Length = 1;
|
|
|
|
token.Type = TokType::Number;
|
|
|
|
move_forward();
|
|
|
|
|
|
|
|
if (left
|
|
|
|
&& ( current == 'x' || current == 'X'
|
|
|
|
|| current == 'b' || current == 'B'
|
|
|
|
|| current == 'o' || current == 'O' )
|
|
|
|
)
|
|
|
|
{
|
|
|
|
move_forward();
|
|
|
|
token.Length++;
|
|
|
|
|
|
|
|
while ( left && char_is_hex_digit(current) )
|
|
|
|
{
|
|
|
|
move_forward();
|
|
|
|
token.Length++;
|
|
|
|
}
|
|
|
|
|
|
|
|
goto FoundToken;
|
|
|
|
}
|
|
|
|
|
|
|
|
while ( left && char_is_digit(current) )
|
|
|
|
{
|
|
|
|
move_forward();
|
|
|
|
token.Length++;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ( left && current == '.' )
|
|
|
|
{
|
|
|
|
move_forward();
|
|
|
|
token.Length++;
|
|
|
|
|
|
|
|
while ( left && char_is_digit(current) )
|
|
|
|
{
|
|
|
|
move_forward();
|
|
|
|
token.Length++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
goto FoundToken;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
String context_str = String::fmt_buf( GlobalAllocator, "%s", scanner, min( 100, left ) );
|
|
|
|
|
|
|
|
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();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
FoundToken:
|
|
|
|
|
|
|
|
if ( token.Type != TokType::Invalid )
|
|
|
|
{
|
2023-07-28 18:44:31 -07:00
|
|
|
if ( token.is_preprocessor() && keep_preprocess_directives == false )
|
2023-07-24 14:45:27 -07:00
|
|
|
continue;
|
|
|
|
|
|
|
|
Tokens.append( token );
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2023-07-27 14:12:58 -07:00
|
|
|
TokType type = ETokType::to_type( token );
|
2023-07-24 14:45:27 -07:00
|
|
|
|
|
|
|
if ( type == TokType::Invalid)
|
|
|
|
type = TokType::Identifier;
|
|
|
|
|
|
|
|
token.Type = type;
|
|
|
|
Tokens.append( token );
|
|
|
|
}
|
|
|
|
|
|
|
|
if ( Tokens.num() == 0 )
|
|
|
|
{
|
|
|
|
log_failure( "Failed to lex any tokens" );
|
|
|
|
return { { nullptr }, 0 };
|
|
|
|
}
|
|
|
|
|
|
|
|
return { Tokens, 0 };
|
|
|
|
# undef current
|
|
|
|
# undef move_forward
|
|
|
|
# undef SkipWhitespace
|
|
|
|
# undef SkipWhitespace_Checked
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#pragma region Helper Macros
|
2023-07-28 18:44:31 -07:00
|
|
|
|
2023-07-29 02:52:06 -07:00
|
|
|
# define check_parse_args( def ) \
|
|
|
|
if ( def.Len <= 0 ) \
|
|
|
|
{ \
|
2023-07-29 03:32:16 -07:00
|
|
|
log_failure( "gen::" stringize(__func__) ": length must greater than 0" ); \
|
2023-07-29 02:52:06 -07:00
|
|
|
return CodeInvalid; \
|
|
|
|
} \
|
|
|
|
if ( def.Ptr == nullptr ) \
|
|
|
|
{ \
|
2023-07-29 03:32:16 -07:00
|
|
|
log_failure( "gen::" stringize(__func__) ": def was null" ); \
|
2023-07-29 02:52:06 -07:00
|
|
|
return CodeInvalid; \
|
2023-07-24 14:45:27 -07:00
|
|
|
}
|
|
|
|
|
2023-07-28 18:44:31 -07:00
|
|
|
# define nexttok Context.Tokens.next()
|
|
|
|
# define currtok Context.Tokens.current()
|
|
|
|
# define prevtok Context.Tokens.previous()
|
|
|
|
# define eat( Type_ ) Context.Tokens.__eat( Type_ )
|
|
|
|
# define left ( Context.Tokens.Arr.num() - Context.Tokens.Idx )
|
2023-07-24 14:45:27 -07:00
|
|
|
|
|
|
|
# define check( Type_ ) ( left && currtok.Type == Type_ )
|
2023-07-28 18:44:31 -07:00
|
|
|
|
2023-07-29 02:52:06 -07:00
|
|
|
# define push_scope() \
|
|
|
|
StackNode scope { nullptr, NullToken, txt_StrC( __func__ ) }; \
|
|
|
|
Context.push( & scope )
|
2023-07-28 18:44:31 -07:00
|
|
|
|
2023-07-24 14:45:27 -07:00
|
|
|
#pragma endregion Helper Macros
|
|
|
|
|
2023-07-28 18:44:31 -07:00
|
|
|
internal Code parse_function_body();
|
|
|
|
internal Code parse_global_nspace();
|
|
|
|
|
|
|
|
internal CodeClass parse_class ();
|
|
|
|
internal CodeEnum parse_enum ();
|
|
|
|
internal CodeBody parse_export_body ();
|
|
|
|
internal CodeBody parse_extern_link_body();
|
|
|
|
internal CodeExtern parse_exten_link ();
|
|
|
|
internal CodeFriend parse_friend ();
|
|
|
|
internal CodeFn parse_function ();
|
|
|
|
internal CodeNamespace parse_namespace ();
|
|
|
|
internal CodeOpCast parse_operator_cast ();
|
|
|
|
internal CodeStruct parse_struct ();
|
|
|
|
internal CodeVar parse_variable ();
|
|
|
|
internal CodeTemplate parse_template ();
|
|
|
|
internal CodeType parse_type ();
|
|
|
|
internal CodeTypedef parse_typedef ();
|
|
|
|
internal CodeUnion parse_union ();
|
|
|
|
internal CodeUsing parse_using ();
|
2023-07-24 14:45:27 -07:00
|
|
|
|
|
|
|
internal inline
|
2023-07-28 18:44:31 -07:00
|
|
|
Code parse_array_decl()
|
2023-07-24 14:45:27 -07:00
|
|
|
{
|
|
|
|
using namespace Parser;
|
2023-07-29 02:52:06 -07:00
|
|
|
push_scope();
|
2023-07-24 14:45:27 -07:00
|
|
|
|
|
|
|
if ( check( TokType::BraceSquare_Open ) )
|
|
|
|
{
|
|
|
|
eat( TokType::BraceSquare_Open );
|
|
|
|
|
|
|
|
if ( left == 0 )
|
|
|
|
{
|
2023-07-29 02:52:06 -07:00
|
|
|
log_failure( "Error, unexpected end of array declaration ( '[]' scope started )\n%s", Context.to_string() );
|
2023-07-24 14:45:27 -07:00
|
|
|
return Code::Invalid;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ( currtok.Type == TokType::BraceSquare_Close )
|
|
|
|
{
|
2023-07-29 02:52:06 -07:00
|
|
|
log_failure( "Error, empty array expression in typedef definition\n%s", Context.to_string() );
|
2023-07-24 14:45:27 -07:00
|
|
|
return Code::Invalid;
|
|
|
|
}
|
|
|
|
|
|
|
|
Token untyped_tok = currtok;
|
|
|
|
|
|
|
|
while ( left && currtok.Type != TokType::BraceSquare_Close )
|
|
|
|
{
|
|
|
|
eat( currtok.Type );
|
|
|
|
}
|
|
|
|
|
|
|
|
untyped_tok.Length = ( (sptr)prevtok.Text + prevtok.Length ) - (sptr)untyped_tok.Text;
|
|
|
|
|
|
|
|
Code array_expr = untyped_str( untyped_tok );
|
|
|
|
|
|
|
|
if ( left == 0 )
|
|
|
|
{
|
2023-07-29 02:52:06 -07:00
|
|
|
log_failure( "Error, unexpected end of array declaration, expected ]\n%s", Context.to_string() );
|
2023-07-24 14:45:27 -07:00
|
|
|
return Code::Invalid;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ( currtok.Type != TokType::BraceSquare_Close )
|
|
|
|
{
|
2023-07-29 02:52:06 -07:00
|
|
|
log_failure( "%s: Error, expected ] in array declaration, not %s\n%s", ETokType::to_str( currtok.Type ), Context.to_string() );
|
2023-07-24 14:45:27 -07:00
|
|
|
return Code::Invalid;
|
|
|
|
}
|
|
|
|
|
|
|
|
eat( TokType::BraceSquare_Close );
|
|
|
|
return array_expr;
|
|
|
|
}
|
|
|
|
|
2023-07-29 02:52:06 -07:00
|
|
|
Context.pop();
|
2023-07-24 14:45:27 -07:00
|
|
|
return { nullptr };
|
|
|
|
}
|
|
|
|
|
|
|
|
internal inline
|
2023-07-28 18:44:31 -07:00
|
|
|
CodeAttributes parse_attributes()
|
2023-07-24 14:45:27 -07:00
|
|
|
{
|
|
|
|
using namespace Parser;
|
2023-07-29 02:52:06 -07:00
|
|
|
push_scope();
|
2023-07-24 14:45:27 -07:00
|
|
|
|
|
|
|
Token start;
|
|
|
|
s32 len = 0;
|
|
|
|
|
|
|
|
if ( check(TokType::Attribute_Open) )
|
|
|
|
{
|
|
|
|
eat( TokType::Attribute_Open);
|
|
|
|
|
|
|
|
while ( left && currtok.Type != TokType::Attribute_Close )
|
|
|
|
{
|
|
|
|
eat( currtok.Type );
|
|
|
|
}
|
|
|
|
|
|
|
|
eat( TokType::Attribute_Close );
|
|
|
|
|
|
|
|
s32 len = ( (sptr)prevtok.Text + prevtok.Length ) - (sptr)start.Text;
|
|
|
|
}
|
|
|
|
|
|
|
|
else if ( check(TokType::Decl_GNU_Attribute) )
|
|
|
|
{
|
|
|
|
eat(TokType::BraceCurly_Open);
|
|
|
|
eat(TokType::BraceCurly_Open);
|
|
|
|
|
|
|
|
while ( left && currtok.Type != TokType::BraceCurly_Close )
|
|
|
|
{
|
|
|
|
eat(currtok.Type);
|
|
|
|
}
|
|
|
|
|
|
|
|
eat(TokType::BraceCurly_Close);
|
|
|
|
eat(TokType::BraceCurly_Close);
|
|
|
|
|
|
|
|
s32 len = ( (sptr)prevtok.Text + prevtok.Length ) - (sptr)start.Text;
|
|
|
|
}
|
|
|
|
|
|
|
|
else if ( check(TokType::Decl_MSVC_Attribute) )
|
|
|
|
{
|
|
|
|
eat( TokType::Decl_MSVC_Attribute );
|
|
|
|
eat( TokType::BraceCurly_Open);
|
|
|
|
|
|
|
|
while ( left && currtok.Type != TokType::BraceCurly_Close )
|
|
|
|
{
|
|
|
|
eat(currtok.Type);
|
|
|
|
}
|
|
|
|
|
|
|
|
eat(TokType::BraceCurly_Close);
|
|
|
|
|
|
|
|
s32 len = ( (sptr)prevtok.Text + prevtok.Length ) - (sptr)start.Text;
|
|
|
|
}
|
|
|
|
|
2023-07-28 18:44:31 -07:00
|
|
|
else if ( currtok.is_attribute() )
|
2023-07-24 14:45:27 -07:00
|
|
|
{
|
|
|
|
eat(currtok.Type);
|
|
|
|
s32 len = start.Length;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ( len > 0 )
|
|
|
|
{
|
|
|
|
StrC attribute_txt = { len, start.Text };
|
|
|
|
return def_attributes( attribute_txt );
|
|
|
|
}
|
|
|
|
|
2023-07-29 03:32:16 -07:00
|
|
|
Context.pop();
|
2023-07-24 14:45:27 -07:00
|
|
|
return { nullptr };
|
|
|
|
}
|
|
|
|
|
|
|
|
internal inline
|
2023-07-28 18:44:31 -07:00
|
|
|
Parser::Token parse_identifier()
|
2023-07-24 14:45:27 -07:00
|
|
|
{
|
|
|
|
using namespace Parser;
|
2023-07-29 02:52:06 -07:00
|
|
|
push_scope();
|
|
|
|
|
2023-07-24 14:45:27 -07:00
|
|
|
Token name = currtok;
|
|
|
|
|
|
|
|
eat( TokType::Identifier );
|
|
|
|
|
|
|
|
while ( check( TokType::Access_StaticSymbol ) )
|
|
|
|
{
|
|
|
|
eat( TokType::Access_StaticSymbol );
|
|
|
|
|
|
|
|
if ( left == 0 )
|
|
|
|
{
|
2023-07-29 02:52:06 -07:00
|
|
|
log_failure( "Error, unexpected end of static symbol identifier\n%s", Context.to_string() );
|
2023-07-24 14:45:27 -07:00
|
|
|
return { nullptr, 0, TokType::Invalid };
|
|
|
|
}
|
|
|
|
|
|
|
|
if ( currtok.Type != TokType::Identifier )
|
|
|
|
{
|
2023-07-29 02:52:06 -07:00
|
|
|
log_failure( "Error, expected static symbol identifier, not %s\n%s", ETokType::to_str( currtok.Type ), Context.to_string() );
|
2023-07-24 14:45:27 -07:00
|
|
|
return { nullptr, 0, TokType::Invalid };
|
|
|
|
}
|
|
|
|
|
|
|
|
name.Length = ( (sptr)currtok.Text + currtok.Length ) - (sptr)name.Text;
|
|
|
|
eat( TokType::Identifier );
|
2023-07-29 10:15:53 -07:00
|
|
|
|
|
|
|
if ( check( TokType::Operator ) && currtok.Text[0] == '<' )
|
|
|
|
{
|
|
|
|
eat( TokType::Operator );
|
|
|
|
|
|
|
|
// Template arguments can be complex so were not validating if they are correct.
|
|
|
|
s32 level = 0;
|
|
|
|
while ( left && (currtok.Text[0] != '>' || level > 0 ) )
|
|
|
|
{
|
|
|
|
if ( currtok.Text[0] == '<' )
|
|
|
|
level++;
|
|
|
|
|
|
|
|
else if ( currtok.Text[0] == '>' && level > 0 )
|
|
|
|
level--;
|
|
|
|
|
|
|
|
eat( currtok.Type );
|
|
|
|
}
|
|
|
|
|
|
|
|
if ( left == 0 )
|
|
|
|
{
|
|
|
|
log_failure( "Error, unexpected end of template arguments\n%s", Context.to_string() );
|
|
|
|
return { nullptr, 0, TokType::Invalid };
|
|
|
|
}
|
|
|
|
|
|
|
|
eat( TokType::Operator );
|
|
|
|
name.Length = ( (sptr)prevtok.Text + (sptr)prevtok.Length ) - (sptr)name.Text;
|
|
|
|
}
|
2023-07-24 14:45:27 -07:00
|
|
|
}
|
|
|
|
|
2023-07-29 02:52:06 -07:00
|
|
|
if ( check( TokType::Operator ) && currtok.Text[0] == '<' )
|
|
|
|
{
|
2023-07-29 10:15:53 -07:00
|
|
|
eat( TokType::Operator );
|
|
|
|
|
2023-07-29 02:52:06 -07:00
|
|
|
// Template arguments can be complex so were not validating if they are correct.
|
|
|
|
s32 level = 0;
|
|
|
|
while ( left && (currtok.Text[0] != '>' || level > 0 ) )
|
|
|
|
{
|
|
|
|
if ( currtok.Text[0] == '<' )
|
|
|
|
level++;
|
|
|
|
|
|
|
|
else if ( currtok.Text[0] == '>' && level > 0 )
|
|
|
|
level--;
|
|
|
|
|
|
|
|
eat( currtok.Type );
|
|
|
|
}
|
|
|
|
|
|
|
|
if ( left == 0 )
|
|
|
|
{
|
|
|
|
log_failure( "Error, unexpected end of template arguments\n%s", Context.to_string() );
|
|
|
|
return { nullptr, 0, TokType::Invalid };
|
|
|
|
}
|
|
|
|
|
|
|
|
eat( TokType::Operator );
|
|
|
|
|
|
|
|
name.Length = ( (sptr)prevtok.Text + (sptr)prevtok.Length ) - (sptr)name.Text;
|
|
|
|
}
|
|
|
|
|
2023-07-29 03:32:16 -07:00
|
|
|
Context.pop();
|
2023-07-24 14:45:27 -07:00
|
|
|
return name;
|
|
|
|
}
|
|
|
|
|
|
|
|
internal
|
2023-07-28 18:44:31 -07:00
|
|
|
CodeParam parse_params( bool use_template_capture = false )
|
2023-07-24 14:45:27 -07:00
|
|
|
{
|
|
|
|
using namespace Parser;
|
|
|
|
using namespace ECode;
|
2023-07-29 02:52:06 -07:00
|
|
|
push_scope();
|
2023-07-24 14:45:27 -07:00
|
|
|
|
|
|
|
if ( ! use_template_capture )
|
|
|
|
eat( TokType::Capture_Start );
|
|
|
|
|
|
|
|
else
|
|
|
|
{
|
|
|
|
if ( check ( TokType::Operator ) && currtok.Text[0] == '<' )
|
|
|
|
eat( TokType::Operator );
|
|
|
|
}
|
|
|
|
|
|
|
|
if ( ! use_template_capture && check(TokType::Capture_End) )
|
|
|
|
{
|
|
|
|
eat( TokType::Capture_End );
|
|
|
|
return { nullptr };
|
|
|
|
}
|
|
|
|
|
|
|
|
CodeType type = { nullptr };
|
|
|
|
Code value = { nullptr };
|
|
|
|
|
|
|
|
if ( check( TokType::Varadic_Argument) )
|
|
|
|
{
|
|
|
|
eat( TokType::Varadic_Argument );
|
|
|
|
|
|
|
|
return param_varadic;
|
|
|
|
}
|
|
|
|
|
2023-07-29 02:52:06 -07:00
|
|
|
type = parse_type();
|
2023-07-24 14:45:27 -07:00
|
|
|
if ( type == Code::Invalid )
|
|
|
|
return CodeInvalid;
|
|
|
|
|
|
|
|
Token name = { nullptr, 0, TokType::Invalid, false };
|
|
|
|
|
|
|
|
if ( check( TokType::Identifier ) )
|
|
|
|
{
|
|
|
|
name = currtok;
|
|
|
|
eat( TokType::Identifier );
|
|
|
|
|
|
|
|
if ( currtok.IsAssign )
|
|
|
|
{
|
|
|
|
eat( TokType::Operator );
|
|
|
|
|
|
|
|
Token value_tok = currtok;
|
|
|
|
|
|
|
|
if ( currtok.Type == TokType::Statement_End )
|
|
|
|
{
|
2023-07-29 02:52:06 -07:00
|
|
|
log_failure( "Expected value after assignment operator\n%s.", Context.to_string() );
|
2023-07-24 14:45:27 -07:00
|
|
|
return CodeInvalid;
|
|
|
|
}
|
|
|
|
|
|
|
|
while ( left && currtok.Type != TokType::Statement_End )
|
|
|
|
{
|
|
|
|
value_tok.Length = ( (sptr)currtok.Text + currtok.Length ) - (sptr)value_tok.Text;
|
|
|
|
eat( currtok.Type );
|
|
|
|
}
|
|
|
|
|
2023-07-29 03:32:16 -07:00
|
|
|
value = parse_type();
|
2023-07-24 14:45:27 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
CodeParam
|
|
|
|
result = (CodeParam) make_code();
|
|
|
|
result->Type = Parameters;
|
|
|
|
|
|
|
|
if ( name.Length > 0 )
|
|
|
|
result->Name = get_cached_string( name );
|
|
|
|
|
|
|
|
result->ValueType = type;
|
|
|
|
|
|
|
|
if ( value )
|
|
|
|
result->Value = value;
|
|
|
|
|
|
|
|
result->NumEntries++;
|
|
|
|
|
|
|
|
while ( left
|
|
|
|
&& use_template_capture ?
|
|
|
|
currtok.Type != TokType::Operator && currtok.Text[0] != '>'
|
|
|
|
: currtok.Type != TokType::Capture_End )
|
|
|
|
{
|
|
|
|
eat( TokType::Comma );
|
|
|
|
|
|
|
|
Code type = { nullptr };
|
|
|
|
Code value = { nullptr };
|
|
|
|
|
|
|
|
if ( check( TokType::Varadic_Argument) )
|
|
|
|
{
|
|
|
|
eat( TokType::Varadic_Argument );
|
|
|
|
result.append( param_varadic );
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2023-07-29 03:32:16 -07:00
|
|
|
type = parse_type();
|
2023-07-24 14:45:27 -07:00
|
|
|
if ( type == Code::Invalid )
|
|
|
|
return CodeInvalid;
|
|
|
|
|
|
|
|
name = { nullptr, 0, TokType::Invalid, false };
|
|
|
|
|
|
|
|
if ( check( TokType::Identifier ) )
|
|
|
|
{
|
|
|
|
name = currtok;
|
|
|
|
eat( TokType::Identifier );
|
|
|
|
|
|
|
|
if ( currtok.IsAssign )
|
|
|
|
{
|
|
|
|
eat( TokType::Operator );
|
|
|
|
|
|
|
|
Token value_tok = currtok;
|
|
|
|
|
|
|
|
if ( currtok.Type == TokType::Statement_End )
|
|
|
|
{
|
2023-07-29 02:52:06 -07:00
|
|
|
log_failure( "Expected value after assignment operator\n%s", Context.to_string() );
|
2023-07-24 14:45:27 -07:00
|
|
|
return CodeInvalid;
|
|
|
|
}
|
|
|
|
|
|
|
|
while ( left && currtok.Type != TokType::Statement_End )
|
|
|
|
{
|
|
|
|
value_tok.Length = ( (sptr)currtok.Text + currtok.Length ) - (sptr)value_tok.Text;
|
|
|
|
eat( currtok.Type );
|
|
|
|
}
|
|
|
|
|
2023-07-29 03:32:16 -07:00
|
|
|
value = parse_type();
|
2023-07-24 14:45:27 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
CodeParam
|
|
|
|
param = (CodeParam) make_code();
|
|
|
|
param->Type = Parameters;
|
|
|
|
|
|
|
|
if ( name.Length > 0 )
|
|
|
|
param->Name = get_cached_string( name );
|
|
|
|
|
|
|
|
param->ValueType = type;
|
|
|
|
|
|
|
|
if ( value )
|
|
|
|
param->Value = value;
|
|
|
|
|
|
|
|
result.append( param );
|
|
|
|
}
|
|
|
|
|
|
|
|
if ( ! use_template_capture )
|
|
|
|
eat( TokType::Capture_End );
|
|
|
|
|
|
|
|
else
|
|
|
|
{
|
|
|
|
if ( ! check( TokType::Operator) || currtok.Text[0] != '>' )
|
|
|
|
{
|
2023-07-29 02:52:06 -07:00
|
|
|
log_failure("Expected '<' after 'template' keyword\n%s", Context.to_string() );
|
2023-07-24 14:45:27 -07:00
|
|
|
return CodeInvalid;
|
|
|
|
}
|
|
|
|
eat( TokType::Operator );
|
|
|
|
}
|
|
|
|
|
2023-07-29 02:52:06 -07:00
|
|
|
Context.pop();
|
2023-07-24 14:45:27 -07:00
|
|
|
return result;
|
|
|
|
# undef context
|
|
|
|
}
|
|
|
|
|
|
|
|
// Function parsing is handled in multiple places because its initial signature is shared with variable parsing
|
|
|
|
internal inline
|
|
|
|
CodeFn parse_function_after_name(
|
|
|
|
ModuleFlag mflags
|
|
|
|
, CodeAttributes attributes
|
|
|
|
, CodeSpecifiers specifiers
|
|
|
|
, CodeType ret_type
|
|
|
|
, StrC name
|
|
|
|
)
|
|
|
|
{
|
|
|
|
using namespace Parser;
|
2023-07-29 02:52:06 -07:00
|
|
|
push_scope();
|
2023-07-24 14:45:27 -07:00
|
|
|
|
2023-07-29 03:32:16 -07:00
|
|
|
CodeParam params = parse_params();
|
2023-07-24 14:45:27 -07:00
|
|
|
|
2023-07-28 18:44:31 -07:00
|
|
|
while ( left && currtok.is_specifier() )
|
2023-07-24 14:45:27 -07:00
|
|
|
{
|
|
|
|
specifiers.append( ESpecifier::to_type(currtok) );
|
|
|
|
eat( currtok.Type );
|
|
|
|
}
|
|
|
|
|
|
|
|
CodeBody body = { nullptr };
|
|
|
|
if ( check( TokType::BraceCurly_Open ) )
|
|
|
|
{
|
2023-07-29 03:32:16 -07:00
|
|
|
body = parse_function_body();
|
2023-07-24 14:45:27 -07:00
|
|
|
if ( body == Code::Invalid )
|
|
|
|
return CodeInvalid;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
eat( TokType::Statement_End );
|
|
|
|
}
|
|
|
|
|
|
|
|
using namespace ECode;
|
|
|
|
|
|
|
|
CodeFn
|
|
|
|
result = (CodeFn) make_code();
|
|
|
|
result->Name = get_cached_string( name );
|
|
|
|
result->ModuleFlags = mflags;
|
|
|
|
|
|
|
|
if ( body )
|
|
|
|
{
|
|
|
|
switch ( body->Type )
|
|
|
|
{
|
|
|
|
case Function_Body:
|
|
|
|
case Untyped:
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
{
|
2023-07-29 02:52:06 -07:00
|
|
|
log_failure("Body must be either of Function_Body or Untyped type, %s\n%s", body.debug_str(), Context.to_string());
|
2023-07-24 14:45:27 -07:00
|
|
|
return CodeInvalid;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
result->Type = Function;
|
|
|
|
result->Body = body;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
result->Type = Function_Fwd;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ( specifiers )
|
|
|
|
result->Specs = specifiers;
|
|
|
|
|
|
|
|
result->ReturnType = ret_type;
|
|
|
|
|
|
|
|
if ( params )
|
|
|
|
result->Params = params;
|
|
|
|
|
2023-07-29 02:52:06 -07:00
|
|
|
Context.pop();
|
2023-07-24 14:45:27 -07:00
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
internal inline
|
2023-07-29 02:52:06 -07:00
|
|
|
CodeOperator parse_operator_after_ret_type(
|
|
|
|
ModuleFlag mflags
|
2023-07-24 14:45:27 -07:00
|
|
|
, CodeAttributes attributes
|
2023-07-29 02:52:06 -07:00
|
|
|
, CodeSpecifiers specifiers
|
2023-07-24 14:45:27 -07:00
|
|
|
, CodeType ret_type
|
2023-07-28 18:44:31 -07:00
|
|
|
)
|
2023-07-24 14:45:27 -07:00
|
|
|
{
|
|
|
|
using namespace Parser;
|
|
|
|
using namespace EOperator;
|
2023-07-29 02:52:06 -07:00
|
|
|
push_scope();
|
2023-07-24 14:45:27 -07:00
|
|
|
|
|
|
|
// Parse Operator
|
|
|
|
eat( TokType::Decl_Operator );
|
|
|
|
|
|
|
|
if ( ! check( TokType::Operator ) )
|
|
|
|
{
|
2023-07-29 02:52:06 -07:00
|
|
|
log_failure( "Expected operator after 'operator' keyword\n%s", Context.to_string() );
|
2023-07-24 14:45:27 -07:00
|
|
|
return CodeInvalid;
|
|
|
|
}
|
|
|
|
|
|
|
|
OperatorT op = Invalid;
|
|
|
|
switch ( currtok.Text[0] )
|
|
|
|
{
|
|
|
|
case '+':
|
|
|
|
{
|
|
|
|
if ( currtok.Text[1] == '=' )
|
|
|
|
op = Assign_Add;
|
|
|
|
|
|
|
|
else
|
|
|
|
op = Add;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case '-':
|
|
|
|
{
|
|
|
|
if ( currtok.Text[1] == '=' )
|
|
|
|
op = Assign_Subtract;
|
|
|
|
|
|
|
|
else
|
|
|
|
op = Subtract;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case '*':
|
|
|
|
{
|
|
|
|
if ( currtok.Text[1] == '=' )
|
|
|
|
op = Assign_Multiply;
|
|
|
|
|
|
|
|
else
|
|
|
|
{
|
|
|
|
Token& finder = prevtok;
|
|
|
|
while ( finder.Type != TokType::Decl_Operator )
|
|
|
|
{
|
|
|
|
if ( finder.Type == TokType::Identifier)
|
|
|
|
{
|
|
|
|
op = Indirection;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if ( op == Invalid)
|
|
|
|
op = Multiply;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case '/':
|
|
|
|
{
|
|
|
|
if ( currtok.Text[1] == '=' )
|
|
|
|
op = Assign_Divide;
|
|
|
|
|
|
|
|
else
|
|
|
|
op = Divide;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case '%':
|
|
|
|
{
|
|
|
|
if ( currtok.Text[1] == '=' )
|
|
|
|
op = Assign_Modulo;
|
|
|
|
|
|
|
|
else
|
|
|
|
op = Modulo;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case '&':
|
|
|
|
{
|
|
|
|
if ( currtok.Text[1] == '=' )
|
|
|
|
op = Assign_BAnd;
|
|
|
|
|
|
|
|
else if ( currtok.Text[1] == '&' )
|
|
|
|
op = LAnd;
|
|
|
|
|
|
|
|
else
|
|
|
|
{
|
|
|
|
|
|
|
|
|
|
|
|
if ( op == Invalid )
|
|
|
|
op = BAnd;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case '|':
|
|
|
|
{
|
|
|
|
if ( currtok.Text[1] == '=' )
|
|
|
|
op = Assign_BOr;
|
|
|
|
|
|
|
|
else if ( currtok.Text[1] == '|' )
|
|
|
|
op = LOr;
|
|
|
|
|
|
|
|
else
|
|
|
|
op = BOr;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case '^':
|
|
|
|
{
|
|
|
|
if ( currtok.Text[1] == '=' )
|
|
|
|
op = Assign_BXOr;
|
|
|
|
|
|
|
|
else
|
|
|
|
op = BXOr;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case '~':
|
|
|
|
{
|
|
|
|
op = BNot;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case '!':
|
|
|
|
{
|
|
|
|
if ( currtok.Text[1] == '=' )
|
|
|
|
op = LNot;
|
|
|
|
|
|
|
|
else
|
|
|
|
op = UnaryNot;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case '=':
|
|
|
|
{
|
|
|
|
if ( currtok.Text[1] == '=' )
|
|
|
|
op = LEqual;
|
|
|
|
|
|
|
|
else
|
|
|
|
op = Assign;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case '<':
|
|
|
|
{
|
|
|
|
if ( currtok.Text[1] == '=' )
|
|
|
|
op = LEqual;
|
|
|
|
|
|
|
|
else if ( currtok.Text[1] == '<' )
|
|
|
|
{
|
|
|
|
if ( currtok.Text[2] == '=' )
|
|
|
|
op = Assign_LShift;
|
|
|
|
|
|
|
|
else
|
|
|
|
op = LShift;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
op = Lesser;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case '>':
|
|
|
|
{
|
|
|
|
if ( currtok.Text[1] == '=' )
|
|
|
|
op = GreaterEqual;
|
|
|
|
|
|
|
|
else if ( currtok.Text[1] == '>' )
|
|
|
|
{
|
|
|
|
if ( currtok.Text[2] == '=' )
|
|
|
|
op = Assign_RShift;
|
|
|
|
|
|
|
|
else
|
|
|
|
op = RShift;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
op = Greater;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case '(':
|
|
|
|
{
|
|
|
|
if ( currtok.Text[1] == ')' )
|
|
|
|
op = FunctionCall;
|
|
|
|
|
|
|
|
else
|
|
|
|
op = Invalid;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case '[':
|
|
|
|
{
|
|
|
|
if ( currtok.Text[1] == ']' )
|
|
|
|
op = Subscript;
|
|
|
|
|
|
|
|
else
|
|
|
|
op = Invalid;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
{
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if ( op == Invalid )
|
|
|
|
{
|
2023-07-29 02:52:06 -07:00
|
|
|
log_failure( "Invalid operator '%s'\n%s", currtok.Text, Context.to_string() );
|
2023-07-24 14:45:27 -07:00
|
|
|
return CodeInvalid;
|
|
|
|
}
|
|
|
|
|
|
|
|
eat( TokType::Operator );
|
|
|
|
|
|
|
|
// Parse Params
|
2023-07-29 03:32:16 -07:00
|
|
|
CodeParam params = parse_params();
|
2023-07-24 14:45:27 -07:00
|
|
|
|
2023-07-28 18:44:31 -07:00
|
|
|
while ( left && currtok.is_specifier() )
|
2023-07-24 14:45:27 -07:00
|
|
|
{
|
|
|
|
specifiers.append( ESpecifier::to_type(currtok) );
|
|
|
|
eat( currtok.Type );
|
|
|
|
}
|
|
|
|
|
|
|
|
// Parse Body
|
|
|
|
CodeBody body = { nullptr };
|
|
|
|
if ( check( TokType::BraceCurly_Open ) )
|
|
|
|
{
|
2023-07-29 03:32:16 -07:00
|
|
|
body = parse_function_body();
|
2023-07-24 14:45:27 -07:00
|
|
|
if ( body == Code::Invalid )
|
|
|
|
return CodeInvalid;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
eat( TokType::Statement_End );
|
|
|
|
}
|
|
|
|
|
|
|
|
// OpValidateResult check_result = operator__validate( op, params, ret_type, specifiers );
|
|
|
|
CodeOperator result = def_operator( op, params, ret_type, body, specifiers, attributes, mflags );
|
2023-07-29 02:52:06 -07:00
|
|
|
Context.pop();
|
2023-07-24 14:45:27 -07:00
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Variable parsing is handled in multiple places because its initial signature is shared with function parsing
|
|
|
|
internal inline
|
|
|
|
CodeVar parse_variable_after_name(
|
|
|
|
ModuleFlag mflags
|
|
|
|
, CodeAttributes attributes
|
|
|
|
,CodeSpecifiers specifiers
|
|
|
|
, CodeType type
|
|
|
|
, StrC name
|
2023-07-29 02:52:06 -07:00
|
|
|
)
|
2023-07-24 14:45:27 -07:00
|
|
|
{
|
|
|
|
using namespace Parser;
|
2023-07-29 02:52:06 -07:00
|
|
|
push_scope();
|
2023-07-24 14:45:27 -07:00
|
|
|
|
2023-07-29 02:52:06 -07:00
|
|
|
Code array_expr = parse_array_decl();
|
|
|
|
Code expr = { nullptr };
|
2023-07-24 14:45:27 -07:00
|
|
|
|
|
|
|
if ( currtok.IsAssign )
|
|
|
|
{
|
|
|
|
eat( TokType::Operator );
|
|
|
|
|
|
|
|
Token expr_tok = currtok;
|
|
|
|
|
|
|
|
if ( currtok.Type == TokType::Statement_End )
|
|
|
|
{
|
2023-07-29 02:52:06 -07:00
|
|
|
log_failure( "Expected expression after assignment operator\n%s", Context.to_string() );
|
2023-07-24 14:45:27 -07:00
|
|
|
return CodeInvalid;
|
|
|
|
}
|
|
|
|
|
|
|
|
while ( left && currtok.Type != TokType::Statement_End )
|
|
|
|
{
|
|
|
|
eat( currtok.Type );
|
|
|
|
}
|
|
|
|
|
|
|
|
expr_tok.Length = ( (sptr)currtok.Text + currtok.Length ) - (sptr)expr_tok.Text;
|
|
|
|
expr = untyped_str( expr_tok );
|
|
|
|
}
|
|
|
|
|
|
|
|
eat( TokType::Statement_End );
|
|
|
|
|
|
|
|
using namespace ECode;
|
|
|
|
|
|
|
|
CodeVar
|
|
|
|
result = (CodeVar) make_code();
|
|
|
|
result->Type = Variable;
|
|
|
|
result->Name = get_cached_string( name );
|
|
|
|
result->ModuleFlags = mflags;
|
|
|
|
|
|
|
|
result->ValueType = type;
|
|
|
|
|
|
|
|
if (array_expr )
|
|
|
|
type->ArrExpr = array_expr;
|
|
|
|
|
|
|
|
if ( attributes )
|
|
|
|
result->Attributes = attributes;
|
|
|
|
|
|
|
|
if ( specifiers )
|
|
|
|
result->Specs = specifiers;
|
|
|
|
|
|
|
|
if ( expr )
|
|
|
|
result->Value = expr;
|
|
|
|
|
2023-07-29 02:52:06 -07:00
|
|
|
Context.pop();
|
2023-07-24 14:45:27 -07:00
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
internal inline
|
2023-07-28 18:44:31 -07:00
|
|
|
Code parse_variable_assignment()
|
2023-07-24 14:45:27 -07:00
|
|
|
{
|
|
|
|
using namespace Parser;
|
2023-07-29 02:52:06 -07:00
|
|
|
push_scope();
|
2023-07-24 14:45:27 -07:00
|
|
|
|
|
|
|
Code expr = Code::Invalid;
|
|
|
|
|
|
|
|
if ( currtok.IsAssign )
|
|
|
|
{
|
|
|
|
eat( TokType::Operator );
|
|
|
|
|
|
|
|
Token expr_tok = currtok;
|
|
|
|
|
|
|
|
if ( currtok.Type == TokType::Statement_End )
|
|
|
|
{
|
2023-07-29 02:52:06 -07:00
|
|
|
log_failure( "Expected expression after assignment operator\n%s", Context.to_string() );
|
2023-07-24 14:45:27 -07:00
|
|
|
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 );
|
|
|
|
}
|
|
|
|
|
|
|
|
expr = untyped_str( expr_tok );
|
|
|
|
}
|
|
|
|
|
2023-07-29 02:52:06 -07:00
|
|
|
Context.pop();
|
2023-07-24 14:45:27 -07:00
|
|
|
return expr;
|
|
|
|
}
|
|
|
|
|
|
|
|
internal inline
|
2023-07-28 18:44:31 -07:00
|
|
|
Code parse_operator_function_or_variable( bool expects_function, CodeAttributes attributes, CodeSpecifiers specifiers )
|
2023-07-24 14:45:27 -07:00
|
|
|
{
|
|
|
|
using namespace Parser;
|
2023-07-29 02:52:06 -07:00
|
|
|
push_scope();
|
2023-07-24 14:45:27 -07:00
|
|
|
|
|
|
|
Code result = Code::Invalid;
|
|
|
|
|
2023-07-29 03:32:16 -07:00
|
|
|
CodeType type = parse_type();
|
2023-07-24 14:45:27 -07:00
|
|
|
|
|
|
|
if ( type == Code::Invalid )
|
|
|
|
return CodeInvalid;
|
|
|
|
|
|
|
|
if ( check( TokType::Operator) )
|
|
|
|
{
|
|
|
|
// Dealing with an operator overload
|
2023-07-29 02:52:06 -07:00
|
|
|
result = parse_operator_after_ret_type( ModuleFlag::None, attributes, specifiers, type );
|
2023-07-24 14:45:27 -07:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
StrC name = currtok;
|
|
|
|
eat( TokType::Identifier );
|
|
|
|
|
|
|
|
if ( check( TokType::Capture_Start) )
|
|
|
|
{
|
|
|
|
// Dealing with a function
|
|
|
|
|
2023-07-29 02:52:06 -07:00
|
|
|
result = parse_function_after_name( ModuleFlag::None, attributes, specifiers, type, name );
|
2023-07-24 14:45:27 -07:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
if ( expects_function )
|
|
|
|
{
|
2023-07-29 02:52:06 -07:00
|
|
|
log_failure( "Expected function declaration (consteval was used)\n%s", Context.to_string() );
|
2023-07-24 14:45:27 -07:00
|
|
|
return Code::Invalid;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Dealing with a variable
|
2023-07-29 02:52:06 -07:00
|
|
|
result = parse_variable_after_name( ModuleFlag::None, attributes, specifiers, type, name );
|
2023-07-24 14:45:27 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-07-29 02:52:06 -07:00
|
|
|
Context.pop();
|
2023-07-24 14:45:27 -07:00
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
internal
|
2023-07-28 18:44:31 -07:00
|
|
|
CodeBody parse_class_struct_body( Parser::TokType which )
|
2023-07-24 14:45:27 -07:00
|
|
|
{
|
|
|
|
using namespace Parser;
|
|
|
|
using namespace ECode;
|
2023-07-29 02:52:06 -07:00
|
|
|
push_scope();
|
2023-07-24 14:45:27 -07:00
|
|
|
|
|
|
|
eat( TokType::BraceCurly_Open );
|
|
|
|
|
|
|
|
CodeBody
|
|
|
|
result = (CodeBody) make_code();
|
|
|
|
|
|
|
|
if ( which == TokType::Decl_Class )
|
|
|
|
result->Type = Class_Body;
|
|
|
|
|
|
|
|
else
|
|
|
|
result->Type = Struct_Body;
|
|
|
|
|
|
|
|
while ( left && currtok.Type != TokType::BraceCurly_Close )
|
|
|
|
{
|
|
|
|
Code member = Code::Invalid;
|
|
|
|
CodeAttributes attributes = { nullptr };
|
|
|
|
CodeSpecifiers specifiers = { nullptr };
|
|
|
|
|
|
|
|
bool expects_function = false;
|
|
|
|
|
|
|
|
switch ( currtok.Type )
|
|
|
|
{
|
|
|
|
case TokType::Comment:
|
|
|
|
member = def_comment( currtok );
|
|
|
|
eat( TokType::Comment );
|
|
|
|
break;
|
|
|
|
|
|
|
|
case TokType::Access_Public:
|
|
|
|
member = access_public;
|
|
|
|
eat( TokType::Access_Public );
|
|
|
|
eat( TokType::Assign_Classifer );
|
|
|
|
break;
|
|
|
|
|
|
|
|
case TokType::Access_Protected:
|
|
|
|
member = access_protected;
|
|
|
|
eat( TokType::Access_Protected );
|
|
|
|
eat( TokType::Assign_Classifer );
|
|
|
|
break;
|
|
|
|
|
|
|
|
case TokType::Access_Private:
|
|
|
|
member = access_private;
|
|
|
|
eat( TokType::Access_Private );
|
|
|
|
eat( TokType::Assign_Classifer );
|
|
|
|
break;
|
|
|
|
|
|
|
|
case TokType::Decl_Class:
|
2023-07-29 03:32:16 -07:00
|
|
|
member = parse_class();
|
2023-07-24 14:45:27 -07:00
|
|
|
break;
|
|
|
|
|
|
|
|
case TokType::Decl_Enum:
|
2023-07-29 03:32:16 -07:00
|
|
|
member = parse_enum();
|
2023-07-24 14:45:27 -07:00
|
|
|
break;
|
|
|
|
|
|
|
|
case TokType::Decl_Friend:
|
2023-07-29 03:32:16 -07:00
|
|
|
member = parse_friend();
|
2023-07-24 14:45:27 -07:00
|
|
|
break;
|
|
|
|
|
|
|
|
case TokType::Decl_Operator:
|
2023-07-29 03:32:16 -07:00
|
|
|
member = parse_operator_cast();
|
2023-07-24 14:45:27 -07:00
|
|
|
break;
|
|
|
|
|
|
|
|
case TokType::Decl_Struct:
|
2023-07-29 03:32:16 -07:00
|
|
|
member = parse_struct();
|
2023-07-24 14:45:27 -07:00
|
|
|
break;
|
|
|
|
|
|
|
|
case TokType::Decl_Template:
|
2023-07-29 03:32:16 -07:00
|
|
|
member = parse_template();
|
2023-07-24 14:45:27 -07:00
|
|
|
break;
|
|
|
|
|
|
|
|
case TokType::Decl_Typedef:
|
2023-07-29 03:32:16 -07:00
|
|
|
member = parse_typedef();
|
2023-07-24 14:45:27 -07:00
|
|
|
break;
|
|
|
|
|
|
|
|
case TokType::Decl_Union:
|
2023-07-29 03:32:16 -07:00
|
|
|
member = parse_variable();
|
2023-07-24 14:45:27 -07:00
|
|
|
break;
|
|
|
|
|
|
|
|
case TokType::Decl_Using:
|
2023-07-29 03:32:16 -07:00
|
|
|
member = parse_using();
|
2023-07-24 14:45:27 -07:00
|
|
|
break;
|
|
|
|
|
|
|
|
case TokType::Attribute_Open:
|
|
|
|
case TokType::Decl_GNU_Attribute:
|
|
|
|
case TokType::Decl_MSVC_Attribute:
|
|
|
|
#define Entry( attribute, str ) case TokType::attribute:
|
2023-07-29 02:52:06 -07:00
|
|
|
GEN_DEFINE_ATTRIBUTE_TOKENS
|
2023-07-24 14:45:27 -07:00
|
|
|
#undef Entry
|
|
|
|
{
|
2023-07-28 18:44:31 -07:00
|
|
|
attributes = parse_attributes();
|
2023-07-24 14:45:27 -07:00
|
|
|
}
|
|
|
|
//! Fallthrough intended
|
|
|
|
case TokType::Spec_Consteval:
|
|
|
|
case TokType::Spec_Constexpr:
|
|
|
|
case TokType::Spec_Constinit:
|
|
|
|
case TokType::Spec_Inline:
|
|
|
|
case TokType::Spec_Mutable:
|
|
|
|
case TokType::Spec_Static:
|
|
|
|
case TokType::Spec_Volatile:
|
|
|
|
{
|
2023-07-27 14:12:58 -07:00
|
|
|
SpecifierT specs_found[16] { ESpecifier::NumSpecifiers };
|
|
|
|
s32 NumSpecifiers = 0;
|
2023-07-24 14:45:27 -07:00
|
|
|
|
2023-07-28 18:44:31 -07:00
|
|
|
while ( left && currtok.is_specifier() )
|
2023-07-24 14:45:27 -07:00
|
|
|
{
|
|
|
|
SpecifierT spec = ESpecifier::to_type( currtok );
|
|
|
|
|
|
|
|
switch ( spec )
|
|
|
|
{
|
|
|
|
case ESpecifier::Constexpr:
|
|
|
|
case ESpecifier::Constinit:
|
|
|
|
case ESpecifier::Inline:
|
|
|
|
case ESpecifier::Mutable:
|
|
|
|
case ESpecifier::Static:
|
|
|
|
case ESpecifier::Volatile:
|
|
|
|
break;
|
|
|
|
|
|
|
|
case ESpecifier::Consteval:
|
|
|
|
expects_function = true;
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
2023-07-29 02:52:06 -07:00
|
|
|
log_failure( "Invalid specifier %s for variable\n%s", ESpecifier::to_str(spec), Context.to_string() );
|
2023-07-24 14:45:27 -07:00
|
|
|
return CodeInvalid;
|
|
|
|
}
|
|
|
|
|
2023-07-27 14:12:58 -07:00
|
|
|
specs_found[NumSpecifiers] = spec;
|
|
|
|
NumSpecifiers++;
|
2023-07-24 14:45:27 -07:00
|
|
|
eat( currtok.Type );
|
|
|
|
}
|
|
|
|
|
2023-07-27 14:12:58 -07:00
|
|
|
if ( NumSpecifiers )
|
2023-07-24 14:45:27 -07:00
|
|
|
{
|
2023-07-27 14:12:58 -07:00
|
|
|
specifiers = def_specifiers( NumSpecifiers, specs_found );
|
2023-07-24 14:45:27 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
//! Fallthrough intentional
|
|
|
|
case TokType::Identifier:
|
|
|
|
case TokType::Spec_Const:
|
|
|
|
case TokType::Type_Unsigned:
|
|
|
|
case TokType::Type_Signed:
|
|
|
|
case TokType::Type_Short:
|
|
|
|
case TokType::Type_Long:
|
2023-07-25 16:23:21 -07:00
|
|
|
case TokType::Type_char:
|
|
|
|
case TokType::Type_int:
|
|
|
|
case TokType::Type_double:
|
2023-07-24 14:45:27 -07:00
|
|
|
{
|
2023-07-29 02:52:06 -07:00
|
|
|
member = parse_operator_function_or_variable( expects_function, attributes, specifiers );
|
2023-07-24 14:45:27 -07:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
Token untyped_tok = currtok;
|
|
|
|
|
|
|
|
while ( left && currtok.Type != TokType::BraceCurly_Close )
|
|
|
|
{
|
|
|
|
untyped_tok.Length = ( (sptr)currtok.Text + currtok.Length ) - (sptr)untyped_tok.Text;
|
|
|
|
eat( currtok.Type );
|
|
|
|
}
|
|
|
|
|
|
|
|
member = untyped_str( untyped_tok );
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ( member == Code::Invalid )
|
|
|
|
{
|
2023-07-29 02:52:06 -07:00
|
|
|
log_failure( "Failed to parse member\n%s", Context.to_string() );
|
2023-07-24 14:45:27 -07:00
|
|
|
return CodeInvalid;
|
|
|
|
}
|
|
|
|
|
|
|
|
result.append( member );
|
|
|
|
}
|
|
|
|
|
|
|
|
eat( TokType::BraceCurly_Close );
|
2023-07-29 02:52:06 -07:00
|
|
|
Context.pop();
|
2023-07-24 14:45:27 -07:00
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
internal
|
2023-07-28 18:44:31 -07:00
|
|
|
Code parse_class_struct( Parser::TokType which )
|
2023-07-24 14:45:27 -07:00
|
|
|
{
|
|
|
|
using namespace Parser;
|
|
|
|
|
|
|
|
if ( which != TokType::Decl_Class && which != TokType::Decl_Struct )
|
|
|
|
{
|
2023-07-29 02:52:06 -07:00
|
|
|
log_failure( "Error, expected class or struct, not %s\n%s", ETokType::to_str( which ), Context.to_string() );
|
2023-07-24 14:45:27 -07:00
|
|
|
return CodeInvalid;
|
|
|
|
}
|
|
|
|
|
|
|
|
Token name { nullptr, 0, TokType::Invalid };
|
|
|
|
|
|
|
|
AccessSpec access = AccessSpec::Default;
|
|
|
|
CodeType parent = { nullptr };
|
|
|
|
CodeBody body = { nullptr };
|
|
|
|
CodeAttributes attributes = { nullptr };
|
|
|
|
ModuleFlag mflags = ModuleFlag::None;
|
|
|
|
|
|
|
|
CodeClass result = CodeInvalid;
|
|
|
|
|
|
|
|
if ( check(TokType::Module_Export) )
|
|
|
|
{
|
|
|
|
mflags = ModuleFlag::Export;
|
|
|
|
eat( TokType::Module_Export );
|
|
|
|
}
|
|
|
|
|
|
|
|
eat( which );
|
|
|
|
|
2023-07-28 18:44:31 -07:00
|
|
|
attributes = parse_attributes();
|
2023-07-24 14:45:27 -07:00
|
|
|
|
|
|
|
if ( check( TokType::Identifier ) )
|
2023-07-29 03:32:16 -07:00
|
|
|
name = parse_identifier();
|
2023-07-24 14:45:27 -07:00
|
|
|
|
|
|
|
local_persist
|
|
|
|
char interface_arr_mem[ kilobytes(4) ] {0};
|
|
|
|
Array<CodeType> interfaces = Array<CodeType>::init_reserve( Arena::init_from_memory(interface_arr_mem, kilobytes(4) ), 4 );
|
|
|
|
|
|
|
|
if ( check( TokType::Assign_Classifer ) )
|
|
|
|
{
|
|
|
|
eat( TokType::Assign_Classifer );
|
|
|
|
|
2023-07-28 18:44:31 -07:00
|
|
|
if ( currtok.is_access_specifier() )
|
2023-07-24 14:45:27 -07:00
|
|
|
{
|
2023-07-28 18:44:31 -07:00
|
|
|
access = currtok.to_access_specifier();
|
2023-07-24 14:45:27 -07:00
|
|
|
}
|
|
|
|
|
2023-07-29 03:32:16 -07:00
|
|
|
Token parent_tok = parse_identifier();
|
2023-07-24 14:45:27 -07:00
|
|
|
parent = def_type( parent_tok );
|
|
|
|
|
|
|
|
while ( check(TokType::Comma) )
|
|
|
|
{
|
|
|
|
eat(TokType::Access_Public);
|
|
|
|
|
2023-07-28 18:44:31 -07:00
|
|
|
if ( currtok.is_access_specifier() )
|
2023-07-24 14:45:27 -07:00
|
|
|
{
|
|
|
|
eat(currtok.Type);
|
|
|
|
}
|
|
|
|
|
2023-07-29 03:32:16 -07:00
|
|
|
Token interface_tok = parse_identifier();
|
2023-07-24 14:45:27 -07:00
|
|
|
|
|
|
|
interfaces.append( def_type( interface_tok ) );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if ( check( TokType::BraceCurly_Open ) )
|
|
|
|
{
|
2023-07-29 03:32:16 -07:00
|
|
|
body = parse_class_struct_body( which );
|
2023-07-24 14:45:27 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
eat( TokType::Statement_End );
|
|
|
|
|
|
|
|
if ( which == TokType::Decl_Class )
|
2023-07-29 03:32:16 -07:00
|
|
|
result = def_class( name, body, parent, access, attributes, mflags );
|
2023-07-24 14:45:27 -07:00
|
|
|
|
|
|
|
else
|
2023-07-29 03:32:16 -07:00
|
|
|
result = def_struct( name, body, (CodeType)parent, access, attributes, mflags );
|
2023-07-24 14:45:27 -07:00
|
|
|
|
|
|
|
interfaces.free();
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
internal
|
2023-07-28 18:44:31 -07:00
|
|
|
Code parse_function_body()
|
2023-07-24 14:45:27 -07:00
|
|
|
{
|
|
|
|
using namespace Parser;
|
|
|
|
using namespace ECode;
|
2023-07-29 02:52:06 -07:00
|
|
|
push_scope();
|
2023-07-24 14:45:27 -07:00
|
|
|
|
|
|
|
eat( TokType::BraceCurly_Open );
|
|
|
|
|
|
|
|
CodeBody
|
|
|
|
result = (CodeBody) make_code();
|
|
|
|
result->Type = Function_Body;
|
|
|
|
|
|
|
|
Token start = currtok;
|
|
|
|
|
|
|
|
s32 level = 0;
|
|
|
|
while ( left && ( currtok.Type != TokType::BraceCurly_Close || level > 0 ) )
|
|
|
|
{
|
|
|
|
if ( currtok.Type == TokType::BraceCurly_Open )
|
|
|
|
level++;
|
|
|
|
|
|
|
|
else if ( currtok.Type == TokType::BraceCurly_Close && level > 0 )
|
|
|
|
level--;
|
|
|
|
|
|
|
|
eat( currtok.Type );
|
|
|
|
}
|
|
|
|
|
|
|
|
Token previous = prevtok;
|
|
|
|
|
|
|
|
s32 len = ( (sptr)prevtok.Text + prevtok.Length ) - (sptr)start.Text;
|
|
|
|
|
|
|
|
if ( len > 0 )
|
|
|
|
{
|
|
|
|
result.append( def_execution( { len, start.Text } ) );
|
|
|
|
}
|
|
|
|
|
|
|
|
eat( TokType::BraceCurly_Close );
|
2023-07-29 02:52:06 -07:00
|
|
|
|
|
|
|
Context.pop();
|
2023-07-24 14:45:27 -07:00
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
internal
|
2023-07-28 18:44:31 -07:00
|
|
|
CodeBody parse_global_nspace( CodeT which )
|
2023-07-24 14:45:27 -07:00
|
|
|
{
|
|
|
|
using namespace Parser;
|
|
|
|
using namespace ECode;
|
|
|
|
|
|
|
|
if ( which != Namespace_Body && which != Global_Body && which != Export_Body && which != Extern_Linkage_Body )
|
|
|
|
return CodeInvalid;
|
|
|
|
|
|
|
|
if ( which != Global_Body )
|
|
|
|
eat( TokType::BraceCurly_Open );
|
|
|
|
|
|
|
|
CodeBody
|
|
|
|
result = (CodeBody) make_code();
|
|
|
|
result->Type = which;
|
|
|
|
|
|
|
|
while ( left && currtok.Type != TokType::BraceCurly_Close )
|
|
|
|
{
|
|
|
|
Code member = Code::Invalid;
|
|
|
|
CodeAttributes attributes = { nullptr };
|
|
|
|
CodeSpecifiers specifiers = { nullptr };
|
|
|
|
|
|
|
|
bool expects_function = false;
|
|
|
|
|
|
|
|
switch ( currtok.Type )
|
|
|
|
{
|
|
|
|
case TokType::Comment:
|
|
|
|
member = def_comment( currtok );
|
|
|
|
eat( TokType::Comment );
|
|
|
|
break;
|
|
|
|
|
|
|
|
case TokType::Decl_Enum:
|
2023-07-29 03:32:16 -07:00
|
|
|
member = parse_enum();
|
2023-07-24 14:45:27 -07:00
|
|
|
break;
|
|
|
|
|
|
|
|
case TokType::Decl_Class:
|
2023-07-29 03:32:16 -07:00
|
|
|
member = parse_class();
|
2023-07-24 14:45:27 -07:00
|
|
|
break;
|
|
|
|
|
|
|
|
case TokType::Decl_Extern_Linkage:
|
|
|
|
if ( which == Extern_Linkage_Body )
|
2023-07-29 02:52:06 -07:00
|
|
|
log_failure( "Nested extern linkage\n%s", Context.to_string() );
|
2023-07-24 14:45:27 -07:00
|
|
|
|
2023-07-29 03:32:16 -07:00
|
|
|
member = parse_extern_link_body();
|
2023-07-24 14:45:27 -07:00
|
|
|
break;
|
|
|
|
|
|
|
|
case TokType::Decl_Namespace:
|
2023-07-29 03:32:16 -07:00
|
|
|
member = parse_namespace();
|
2023-07-24 14:45:27 -07:00
|
|
|
break;
|
|
|
|
|
|
|
|
case TokType::Decl_Struct:
|
2023-07-29 03:32:16 -07:00
|
|
|
member = parse_struct();
|
2023-07-24 14:45:27 -07:00
|
|
|
break;
|
|
|
|
|
|
|
|
case TokType::Decl_Template:
|
2023-07-29 03:32:16 -07:00
|
|
|
member = parse_template();
|
2023-07-24 14:45:27 -07:00
|
|
|
break;
|
|
|
|
|
|
|
|
case TokType::Decl_Typedef:
|
2023-07-29 03:32:16 -07:00
|
|
|
member = parse_typedef();
|
2023-07-24 14:45:27 -07:00
|
|
|
break;
|
|
|
|
|
|
|
|
case TokType::Decl_Union:
|
2023-07-29 03:32:16 -07:00
|
|
|
member = parse_union();
|
2023-07-24 14:45:27 -07:00
|
|
|
break;
|
|
|
|
|
|
|
|
case TokType::Decl_Using:
|
2023-07-29 03:32:16 -07:00
|
|
|
member = parse_using();
|
2023-07-24 14:45:27 -07:00
|
|
|
break;
|
|
|
|
|
|
|
|
case TokType::Module_Export:
|
|
|
|
if ( which == Export_Body )
|
2023-07-29 02:52:06 -07:00
|
|
|
log_failure( "Nested export declaration\n%s", Context.to_string() );
|
2023-07-24 14:45:27 -07:00
|
|
|
|
2023-07-29 03:32:16 -07:00
|
|
|
member = parse_export_body();
|
2023-07-24 14:45:27 -07:00
|
|
|
break;
|
|
|
|
|
|
|
|
case TokType::Module_Import:
|
|
|
|
{
|
|
|
|
not_implemented( context );
|
|
|
|
}
|
|
|
|
//! Fallthrough intentional
|
|
|
|
case TokType::Decl_GNU_Attribute:
|
|
|
|
case TokType::Decl_MSVC_Attribute:
|
|
|
|
#define Entry( attribute, str ) case TokType::attribute:
|
2023-07-29 02:52:06 -07:00
|
|
|
GEN_DEFINE_ATTRIBUTE_TOKENS
|
2023-07-24 14:45:27 -07:00
|
|
|
#undef Entry
|
|
|
|
{
|
2023-07-28 18:44:31 -07:00
|
|
|
attributes = parse_attributes();
|
2023-07-24 14:45:27 -07:00
|
|
|
}
|
|
|
|
//! Fallthrough intentional
|
|
|
|
case TokType::Spec_Consteval:
|
|
|
|
case TokType::Spec_Constexpr:
|
|
|
|
case TokType::Spec_Constinit:
|
|
|
|
case TokType::Spec_Extern:
|
|
|
|
case TokType::Spec_Global:
|
|
|
|
case TokType::Spec_Inline:
|
|
|
|
case TokType::Spec_Internal_Linkage:
|
|
|
|
case TokType::Spec_Static:
|
|
|
|
{
|
2023-07-27 14:12:58 -07:00
|
|
|
SpecifierT specs_found[16] { ESpecifier::NumSpecifiers };
|
|
|
|
s32 NumSpecifiers = 0;
|
2023-07-24 14:45:27 -07:00
|
|
|
|
2023-07-28 18:44:31 -07:00
|
|
|
while ( left && currtok.is_specifier() )
|
2023-07-24 14:45:27 -07:00
|
|
|
{
|
|
|
|
SpecifierT spec = ESpecifier::to_type( currtok );
|
|
|
|
|
|
|
|
switch ( spec )
|
|
|
|
{
|
|
|
|
case ESpecifier::Constexpr:
|
|
|
|
case ESpecifier::Constinit:
|
|
|
|
case ESpecifier::Inline:
|
|
|
|
case ESpecifier::Mutable:
|
|
|
|
case ESpecifier::Static:
|
|
|
|
case ESpecifier::Volatile:
|
|
|
|
break;
|
|
|
|
|
|
|
|
case ESpecifier::Consteval:
|
|
|
|
expects_function = true;
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
2023-07-29 02:52:06 -07:00
|
|
|
log_failure( "Invalid specifier %s for variable\n%s", ESpecifier::to_str(spec), Context.to_string() );
|
2023-07-24 14:45:27 -07:00
|
|
|
return CodeInvalid;
|
|
|
|
}
|
|
|
|
|
2023-07-27 14:12:58 -07:00
|
|
|
specs_found[NumSpecifiers] = spec;
|
|
|
|
NumSpecifiers++;
|
2023-07-24 14:45:27 -07:00
|
|
|
eat( currtok.Type );
|
|
|
|
}
|
|
|
|
|
2023-07-27 14:12:58 -07:00
|
|
|
if ( NumSpecifiers )
|
2023-07-24 14:45:27 -07:00
|
|
|
{
|
2023-07-27 14:12:58 -07:00
|
|
|
specifiers = def_specifiers( NumSpecifiers, specs_found );
|
2023-07-24 14:45:27 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
//! Fallthrough intentional
|
|
|
|
case TokType::Identifier:
|
|
|
|
case TokType::Spec_Const:
|
|
|
|
case TokType::Type_Long:
|
|
|
|
case TokType::Type_Short:
|
|
|
|
case TokType::Type_Signed:
|
|
|
|
case TokType::Type_Unsigned:
|
2023-07-25 16:23:21 -07:00
|
|
|
case TokType::Type_char:
|
|
|
|
case TokType::Type_double:
|
|
|
|
case TokType::Type_int:
|
2023-07-24 14:45:27 -07:00
|
|
|
{
|
2023-07-29 03:32:16 -07:00
|
|
|
member = parse_operator_function_or_variable( expects_function, attributes, specifiers );
|
2023-07-24 14:45:27 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if ( member == Code::Invalid )
|
|
|
|
{
|
2023-07-29 02:52:06 -07:00
|
|
|
log_failure( "Failed to parse member\n%s", Context.to_string() );
|
2023-07-24 14:45:27 -07:00
|
|
|
return CodeInvalid;
|
|
|
|
}
|
|
|
|
|
|
|
|
result.append( member );
|
|
|
|
}
|
|
|
|
|
|
|
|
if ( which != Global_Body )
|
|
|
|
eat( TokType::BraceCurly_Close );
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
internal
|
2023-07-28 18:44:31 -07:00
|
|
|
CodeClass parse_class()
|
2023-07-24 14:45:27 -07:00
|
|
|
{
|
2023-07-29 03:32:16 -07:00
|
|
|
using namespace Parser;
|
2023-07-29 02:52:06 -07:00
|
|
|
push_scope();
|
2023-07-29 03:32:16 -07:00
|
|
|
CodeClass result = (CodeClass) parse_class_struct( Parser::TokType::Decl_Class );
|
2023-07-29 02:52:06 -07:00
|
|
|
Context.pop();
|
|
|
|
return result;
|
2023-07-24 14:45:27 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
CodeClass parse_class( StrC def )
|
|
|
|
{
|
2023-07-29 03:32:16 -07:00
|
|
|
check_parse_args( def );
|
2023-07-24 14:45:27 -07:00
|
|
|
using namespace Parser;
|
|
|
|
|
|
|
|
TokArray toks = lex( def );
|
|
|
|
if ( toks.Arr == nullptr )
|
|
|
|
return CodeInvalid;
|
|
|
|
|
2023-07-29 09:25:38 -07:00
|
|
|
Context.Tokens = toks;
|
2023-07-29 02:52:06 -07:00
|
|
|
push_scope();
|
|
|
|
CodeClass result = (CodeClass) parse_class_struct( TokType::Decl_Class );
|
|
|
|
Context.pop();
|
|
|
|
return result;
|
2023-07-24 14:45:27 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
internal
|
2023-07-28 18:44:31 -07:00
|
|
|
CodeEnum parse_enum()
|
2023-07-24 14:45:27 -07:00
|
|
|
{
|
|
|
|
using namespace Parser;
|
|
|
|
using namespace ECode;
|
2023-07-29 02:52:06 -07:00
|
|
|
push_scope();
|
2023-07-24 14:45:27 -07:00
|
|
|
|
2023-07-27 14:12:58 -07:00
|
|
|
SpecifierT specs_found[16] { ESpecifier::NumSpecifiers };
|
|
|
|
s32 NumSpecifiers = 0;
|
2023-07-24 14:45:27 -07:00
|
|
|
|
|
|
|
Token name = { nullptr, 0, TokType::Invalid };
|
|
|
|
Code array_expr = { nullptr };
|
|
|
|
CodeType type = { nullptr };
|
|
|
|
Token body = { nullptr, 0, TokType::Invalid };
|
|
|
|
|
|
|
|
char entries_code[ kilobytes(128) ] { 0 };
|
|
|
|
s32 entries_length = 0;
|
|
|
|
|
|
|
|
bool is_enum_class = false;
|
|
|
|
|
|
|
|
eat( TokType::Decl_Enum );
|
|
|
|
|
|
|
|
if ( currtok.Type == TokType::Decl_Class )
|
|
|
|
{
|
|
|
|
eat( TokType::Decl_Class);
|
|
|
|
is_enum_class = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
// TODO : Parse attributes
|
|
|
|
|
|
|
|
if ( currtok.Type != TokType::Identifier )
|
|
|
|
{
|
2023-07-29 02:52:06 -07:00
|
|
|
log_failure( "Expected identifier for enum name\n%s", Context.to_string() );
|
2023-07-24 14:45:27 -07:00
|
|
|
return CodeInvalid;
|
|
|
|
}
|
|
|
|
|
|
|
|
name = currtok;
|
|
|
|
eat( TokType::Identifier );
|
|
|
|
|
|
|
|
if ( currtok.Type == TokType::Assign_Classifer )
|
|
|
|
{
|
|
|
|
eat( TokType::Assign_Classifer );
|
|
|
|
|
2023-07-29 03:32:16 -07:00
|
|
|
type = parse_type();
|
2023-07-24 14:45:27 -07:00
|
|
|
if ( type == Code::Invalid )
|
|
|
|
return CodeInvalid;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ( currtok.Type == TokType::BraceCurly_Open )
|
|
|
|
{
|
|
|
|
eat( TokType::BraceCurly_Open );
|
|
|
|
|
|
|
|
body = currtok;
|
|
|
|
|
|
|
|
while ( currtok.Type != TokType::BraceCurly_Close )
|
|
|
|
{
|
|
|
|
eat( TokType::Identifier);
|
|
|
|
|
|
|
|
if ( currtok.Type == TokType::Operator && currtok.Text[0] == '=' )
|
|
|
|
{
|
|
|
|
eat( TokType::Operator );
|
|
|
|
|
|
|
|
while ( currtok.Type != TokType::Comma && currtok.Type != TokType::BraceCurly_Close )
|
|
|
|
{
|
|
|
|
eat( currtok.Type );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if ( currtok.Type == TokType::Comma )
|
|
|
|
{
|
|
|
|
eat( TokType::Comma );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
body.Length = ( (sptr)prevtok.Text + prevtok.Length ) - (sptr)body.Text;
|
|
|
|
|
|
|
|
eat( TokType::BraceCurly_Close );
|
|
|
|
}
|
|
|
|
|
|
|
|
eat( TokType::Statement_End );
|
|
|
|
|
|
|
|
using namespace ECode;
|
|
|
|
|
|
|
|
CodeEnum
|
|
|
|
result = (CodeEnum) make_code();
|
|
|
|
|
|
|
|
if ( body.Length )
|
|
|
|
{
|
|
|
|
// mem_copy( entries_code, body.Text, body.Length );
|
|
|
|
|
|
|
|
Code untyped_body = untyped_str( body );
|
|
|
|
|
|
|
|
result->Type = is_enum_class ? Enum_Class : Enum;
|
|
|
|
result->Body = untyped_body;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
result->Type = is_enum_class ? Enum_Class_Fwd : Enum_Fwd;
|
|
|
|
}
|
|
|
|
|
|
|
|
result->Name = get_cached_string( name );
|
|
|
|
|
|
|
|
if ( type )
|
|
|
|
result->UnderlyingType = type;
|
|
|
|
|
2023-07-29 02:52:06 -07:00
|
|
|
Context.pop();
|
2023-07-24 14:45:27 -07:00
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
CodeEnum parse_enum( StrC def )
|
|
|
|
{
|
2023-07-29 03:32:16 -07:00
|
|
|
check_parse_args( def );
|
2023-07-24 14:45:27 -07:00
|
|
|
using namespace Parser;
|
|
|
|
|
|
|
|
TokArray toks = lex( def );
|
|
|
|
if ( toks.Arr == nullptr )
|
|
|
|
return CodeInvalid;
|
|
|
|
|
2023-07-29 09:25:38 -07:00
|
|
|
Context.Tokens = toks;
|
2023-07-29 02:52:06 -07:00
|
|
|
return parse_enum();
|
2023-07-24 14:45:27 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
internal inline
|
2023-07-28 18:44:31 -07:00
|
|
|
CodeBody parse_export_body()
|
2023-07-24 14:45:27 -07:00
|
|
|
{
|
2023-07-29 03:32:16 -07:00
|
|
|
using namespace Parser;
|
2023-07-29 02:52:06 -07:00
|
|
|
push_scope();
|
|
|
|
CodeBody result = parse_global_nspace( ECode::Export_Body );
|
|
|
|
Context.pop();
|
|
|
|
return result;
|
2023-07-24 14:45:27 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
CodeBody parse_export_body( StrC def )
|
|
|
|
{
|
2023-07-29 03:32:16 -07:00
|
|
|
check_parse_args( def );
|
2023-07-24 14:45:27 -07:00
|
|
|
using namespace Parser;
|
|
|
|
|
|
|
|
TokArray toks = lex( def );
|
|
|
|
if ( toks.Arr == nullptr )
|
|
|
|
return CodeInvalid;
|
|
|
|
|
2023-07-29 09:25:38 -07:00
|
|
|
Context.Tokens = toks;
|
2023-07-29 02:52:06 -07:00
|
|
|
return parse_export_body();
|
2023-07-24 14:45:27 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
internal inline
|
2023-07-29 02:52:06 -07:00
|
|
|
CodeBody parse_extern_link_body()
|
2023-07-24 14:45:27 -07:00
|
|
|
{
|
2023-07-29 03:32:16 -07:00
|
|
|
using namespace Parser;
|
2023-07-29 02:52:06 -07:00
|
|
|
push_scope();
|
|
|
|
CodeBody result = parse_global_nspace( ECode::Extern_Linkage_Body );
|
|
|
|
Context.pop();
|
|
|
|
return result;
|
2023-07-24 14:45:27 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
internal
|
2023-07-29 02:52:06 -07:00
|
|
|
CodeExtern parse_extern_link()
|
2023-07-24 14:45:27 -07:00
|
|
|
{
|
|
|
|
using namespace Parser;
|
2023-07-29 02:52:06 -07:00
|
|
|
push_scope();
|
2023-07-24 14:45:27 -07:00
|
|
|
|
|
|
|
eat( TokType::Decl_Extern_Linkage );
|
|
|
|
|
|
|
|
Token name = currtok;
|
|
|
|
eat( TokType::String );
|
|
|
|
|
|
|
|
name.Text += 1;
|
|
|
|
name.Length -= 1;
|
|
|
|
|
|
|
|
CodeExtern
|
|
|
|
result = (CodeExtern) make_code();
|
|
|
|
result->Type = ECode::Extern_Linkage;
|
|
|
|
result->Name = get_cached_string( name );
|
|
|
|
|
2023-07-29 03:32:16 -07:00
|
|
|
Code entry = parse_extern_link_body();
|
2023-07-24 14:45:27 -07:00
|
|
|
if ( entry == Code::Invalid )
|
|
|
|
{
|
2023-07-29 02:52:06 -07:00
|
|
|
log_failure( "Failed to parse body\n%s", Context.to_string() );
|
2023-07-24 14:45:27 -07:00
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
result->Body = entry;
|
|
|
|
|
2023-07-29 02:52:06 -07:00
|
|
|
Context.pop();
|
2023-07-24 14:45:27 -07:00
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
CodeExtern parse_extern_link( StrC def )
|
|
|
|
{
|
2023-07-29 03:32:16 -07:00
|
|
|
check_parse_args( def );
|
2023-07-24 14:45:27 -07:00
|
|
|
using namespace Parser;
|
|
|
|
|
|
|
|
TokArray toks = lex( def );
|
|
|
|
if ( toks.Arr == nullptr )
|
|
|
|
return CodeInvalid;
|
|
|
|
|
2023-07-29 09:25:38 -07:00
|
|
|
Context.Tokens = toks;
|
2023-07-29 02:52:06 -07:00
|
|
|
return parse_extern_link();
|
2023-07-24 14:45:27 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
internal
|
2023-07-29 02:52:06 -07:00
|
|
|
CodeFriend parse_friend()
|
2023-07-24 14:45:27 -07:00
|
|
|
{
|
|
|
|
using namespace Parser;
|
|
|
|
using namespace ECode;
|
2023-07-29 02:52:06 -07:00
|
|
|
push_scope();
|
2023-07-24 14:45:27 -07:00
|
|
|
|
|
|
|
eat( TokType::Decl_Friend );
|
|
|
|
|
|
|
|
CodeFn function = { nullptr };
|
|
|
|
|
|
|
|
// Type declaration or return type
|
2023-07-29 03:32:16 -07:00
|
|
|
CodeType type = parse_type();
|
2023-07-24 14:45:27 -07:00
|
|
|
if ( type == Code::Invalid )
|
|
|
|
return CodeInvalid;
|
|
|
|
|
|
|
|
// Funciton declaration
|
|
|
|
if ( currtok.Type == TokType::Identifier )
|
|
|
|
{
|
|
|
|
// Name
|
2023-07-29 03:32:16 -07:00
|
|
|
Token name = parse_identifier();
|
2023-07-24 14:45:27 -07:00
|
|
|
|
|
|
|
// Parameter list
|
2023-07-29 03:32:16 -07:00
|
|
|
CodeParam params = parse_params();
|
2023-07-24 14:45:27 -07:00
|
|
|
|
|
|
|
function = make_code();
|
|
|
|
function->Type = Function_Fwd;
|
|
|
|
function->Name = get_cached_string( name );
|
|
|
|
function->ReturnType = type;
|
|
|
|
|
|
|
|
if ( params )
|
|
|
|
function->Params = params;
|
|
|
|
}
|
|
|
|
|
|
|
|
eat( TokType::Statement_End );
|
|
|
|
|
|
|
|
CodeFriend
|
|
|
|
result = (CodeFriend) make_code();
|
|
|
|
result->Type = Friend;
|
|
|
|
|
|
|
|
if ( function )
|
|
|
|
result->Declaration = function;
|
|
|
|
|
|
|
|
else
|
|
|
|
result->Declaration = type;
|
|
|
|
|
2023-07-29 02:52:06 -07:00
|
|
|
Context.pop();
|
2023-07-24 14:45:27 -07:00
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
CodeFriend parse_friend( StrC def )
|
|
|
|
{
|
2023-07-29 03:32:16 -07:00
|
|
|
check_parse_args( def );
|
2023-07-24 14:45:27 -07:00
|
|
|
using namespace Parser;
|
|
|
|
|
|
|
|
TokArray toks = lex( def );
|
|
|
|
if ( toks.Arr == nullptr )
|
|
|
|
return CodeInvalid;
|
|
|
|
|
2023-07-29 09:25:38 -07:00
|
|
|
Context.Tokens = toks;
|
2023-07-29 02:52:06 -07:00
|
|
|
return parse_friend();
|
2023-07-24 14:45:27 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
internal
|
2023-07-29 02:52:06 -07:00
|
|
|
CodeFn parse_functon()
|
2023-07-24 14:45:27 -07:00
|
|
|
{
|
|
|
|
using namespace Parser;
|
2023-07-29 02:52:06 -07:00
|
|
|
push_scope();
|
2023-07-24 14:45:27 -07:00
|
|
|
|
2023-07-27 14:12:58 -07:00
|
|
|
SpecifierT specs_found[16] { ESpecifier::NumSpecifiers };
|
|
|
|
s32 NumSpecifiers = 0;
|
2023-07-24 14:45:27 -07:00
|
|
|
|
|
|
|
CodeAttributes attributes = { nullptr };
|
|
|
|
CodeSpecifiers specifiers = { nullptr };
|
|
|
|
ModuleFlag mflags = ModuleFlag::None;
|
|
|
|
|
|
|
|
if ( check(TokType::Module_Export) )
|
|
|
|
{
|
|
|
|
mflags = ModuleFlag::Export;
|
|
|
|
eat( TokType::Module_Export );
|
|
|
|
}
|
|
|
|
|
2023-07-28 18:44:31 -07:00
|
|
|
attributes = parse_attributes();
|
2023-07-24 14:45:27 -07:00
|
|
|
|
2023-07-28 18:44:31 -07:00
|
|
|
while ( left && currtok.is_specifier() )
|
2023-07-24 14:45:27 -07:00
|
|
|
{
|
|
|
|
SpecifierT spec = ESpecifier::to_type( currtok );
|
|
|
|
|
|
|
|
switch ( spec )
|
|
|
|
{
|
|
|
|
case ESpecifier::Const:
|
|
|
|
case ESpecifier::Consteval:
|
|
|
|
case ESpecifier::Constexpr:
|
|
|
|
case ESpecifier::External_Linkage:
|
|
|
|
case ESpecifier::Inline:
|
|
|
|
case ESpecifier::Static:
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
2023-07-29 02:52:06 -07:00
|
|
|
log_failure( "Invalid specifier %s for functon\n%s", ESpecifier::to_str(spec), Context.to_string() );
|
2023-07-24 14:45:27 -07:00
|
|
|
return CodeInvalid;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ( spec == ESpecifier::Const )
|
|
|
|
continue;
|
|
|
|
|
2023-07-27 14:12:58 -07:00
|
|
|
specs_found[NumSpecifiers] = spec;
|
|
|
|
NumSpecifiers++;
|
2023-07-24 14:45:27 -07:00
|
|
|
eat( currtok.Type );
|
|
|
|
}
|
|
|
|
|
2023-07-27 14:12:58 -07:00
|
|
|
if ( NumSpecifiers )
|
2023-07-24 14:45:27 -07:00
|
|
|
{
|
2023-07-27 14:12:58 -07:00
|
|
|
specifiers = def_specifiers( NumSpecifiers, specs_found );
|
2023-07-24 14:45:27 -07:00
|
|
|
}
|
|
|
|
|
2023-07-29 03:32:16 -07:00
|
|
|
CodeType ret_type = parse_type();
|
2023-07-24 14:45:27 -07:00
|
|
|
if ( ret_type == Code::Invalid )
|
|
|
|
return CodeInvalid;
|
|
|
|
|
2023-07-29 03:32:16 -07:00
|
|
|
Token name = parse_identifier();
|
2023-07-24 14:45:27 -07:00
|
|
|
if ( ! name )
|
|
|
|
return CodeInvalid;
|
|
|
|
|
2023-07-29 02:52:06 -07:00
|
|
|
CodeFn result = parse_function_after_name( mflags, attributes, specifiers, ret_type, name );
|
2023-07-24 14:45:27 -07:00
|
|
|
|
2023-07-29 02:52:06 -07:00
|
|
|
Context.pop();
|
2023-07-24 14:45:27 -07:00
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
CodeFn parse_function( StrC def )
|
|
|
|
{
|
2023-07-29 03:32:16 -07:00
|
|
|
check_parse_args( def );
|
2023-07-29 02:52:06 -07:00
|
|
|
using namespace Parser;
|
2023-07-24 14:45:27 -07:00
|
|
|
|
|
|
|
TokArray toks = lex( def );
|
|
|
|
if ( toks.Arr == nullptr )
|
|
|
|
return CodeInvalid;
|
|
|
|
|
2023-07-29 09:25:38 -07:00
|
|
|
Context.Tokens = toks;
|
2023-07-29 02:52:06 -07:00
|
|
|
return (CodeFn) parse_functon();
|
2023-07-24 14:45:27 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
CodeBody parse_global_body( StrC def )
|
|
|
|
{
|
2023-07-29 03:32:16 -07:00
|
|
|
check_parse_args( def );
|
2023-07-24 14:45:27 -07:00
|
|
|
using namespace Parser;
|
|
|
|
|
|
|
|
TokArray toks = lex( def );
|
|
|
|
if ( toks.Arr == nullptr )
|
|
|
|
return CodeInvalid;
|
|
|
|
|
2023-07-29 09:25:38 -07:00
|
|
|
Context.Tokens = toks;
|
2023-07-29 02:52:06 -07:00
|
|
|
push_scope();
|
|
|
|
CodeBody result = parse_global_nspace( ECode::Global_Body );
|
|
|
|
Context.pop();
|
|
|
|
return result;
|
2023-07-24 14:45:27 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
internal
|
2023-07-29 03:32:16 -07:00
|
|
|
CodeNamespace parse_namespace()
|
2023-07-24 14:45:27 -07:00
|
|
|
{
|
|
|
|
using namespace Parser;
|
2023-07-29 02:52:06 -07:00
|
|
|
push_scope();
|
|
|
|
|
2023-07-24 14:45:27 -07:00
|
|
|
eat( TokType::Decl_Namespace );
|
|
|
|
|
2023-07-29 03:32:16 -07:00
|
|
|
Token name = parse_identifier();
|
2023-07-24 14:45:27 -07:00
|
|
|
|
2023-07-29 03:32:16 -07:00
|
|
|
CodeBody body = parse_global_nspace( ECode::Namespace_Body );
|
2023-07-24 14:45:27 -07:00
|
|
|
if ( body == Code::Invalid )
|
|
|
|
return CodeInvalid;
|
|
|
|
|
|
|
|
CodeNamespace
|
|
|
|
result = (CodeNamespace) make_code();
|
|
|
|
result->Type = ECode::Namespace;
|
|
|
|
result->Name = get_cached_string( name );
|
|
|
|
|
|
|
|
result->Body = body;
|
|
|
|
|
2023-07-29 02:52:06 -07:00
|
|
|
Context.pop();
|
2023-07-24 14:45:27 -07:00
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
CodeNamespace parse_namespace( StrC def )
|
|
|
|
{
|
2023-07-29 03:32:16 -07:00
|
|
|
check_parse_args( def );
|
2023-07-24 14:45:27 -07:00
|
|
|
using namespace Parser;
|
|
|
|
|
|
|
|
TokArray toks = lex( def );
|
|
|
|
if ( toks.Arr == nullptr )
|
|
|
|
return CodeInvalid;
|
|
|
|
|
2023-07-29 09:25:38 -07:00
|
|
|
Context.Tokens = toks;
|
2023-07-29 02:52:06 -07:00
|
|
|
return parse_namespace();
|
2023-07-24 14:45:27 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
internal
|
2023-07-29 02:52:06 -07:00
|
|
|
CodeOperator parse_operator()
|
2023-07-24 14:45:27 -07:00
|
|
|
{
|
|
|
|
using namespace Parser;
|
2023-07-29 02:52:06 -07:00
|
|
|
push_scope();
|
2023-07-24 14:45:27 -07:00
|
|
|
|
|
|
|
CodeAttributes attributes = { nullptr };
|
|
|
|
CodeSpecifiers specifiers = { nullptr };
|
|
|
|
ModuleFlag mflags = ModuleFlag::None;
|
|
|
|
|
2023-07-27 14:12:58 -07:00
|
|
|
SpecifierT specs_found[16] { ESpecifier::NumSpecifiers };
|
|
|
|
s32 NumSpecifiers = 0;
|
2023-07-24 14:45:27 -07:00
|
|
|
|
|
|
|
if ( check(TokType::Module_Export) )
|
|
|
|
{
|
|
|
|
mflags = ModuleFlag::Export;
|
|
|
|
eat( TokType::Module_Export );
|
|
|
|
}
|
|
|
|
|
2023-07-28 18:44:31 -07:00
|
|
|
attributes = parse_attributes();
|
2023-07-24 14:45:27 -07:00
|
|
|
|
2023-07-28 18:44:31 -07:00
|
|
|
while ( left && currtok.is_specifier() )
|
2023-07-24 14:45:27 -07:00
|
|
|
{
|
|
|
|
SpecifierT spec = ESpecifier::to_type( currtok );
|
|
|
|
|
|
|
|
switch ( spec )
|
|
|
|
{
|
|
|
|
case ESpecifier::Const:
|
|
|
|
case ESpecifier::Constexpr:
|
|
|
|
case ESpecifier::Inline:
|
|
|
|
case ESpecifier::Static:
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
2023-07-29 02:52:06 -07:00
|
|
|
log_failure( "Invalid specifier " "%s" " for operator\n%s", ESpecifier::to_str(spec), Context.to_string() );
|
2023-07-24 14:45:27 -07:00
|
|
|
return CodeInvalid;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ( spec == ESpecifier::Const )
|
|
|
|
continue;
|
|
|
|
|
2023-07-27 14:12:58 -07:00
|
|
|
specs_found[NumSpecifiers] = spec;
|
|
|
|
NumSpecifiers++;
|
2023-07-24 14:45:27 -07:00
|
|
|
eat( currtok.Type );
|
|
|
|
}
|
|
|
|
|
2023-07-27 14:12:58 -07:00
|
|
|
if ( NumSpecifiers )
|
2023-07-24 14:45:27 -07:00
|
|
|
{
|
2023-07-27 14:12:58 -07:00
|
|
|
specifiers = def_specifiers( NumSpecifiers, specs_found );
|
2023-07-24 14:45:27 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
// Parse Return Type
|
2023-07-29 03:32:16 -07:00
|
|
|
CodeType ret_type = parse_type();
|
2023-07-24 14:45:27 -07:00
|
|
|
|
2023-07-29 02:52:06 -07:00
|
|
|
CodeOperator result = parse_operator_after_ret_type( mflags, attributes, specifiers, ret_type );
|
|
|
|
|
|
|
|
Context.pop();
|
2023-07-24 14:45:27 -07:00
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
CodeOperator parse_operator( StrC def )
|
|
|
|
{
|
2023-07-29 03:32:16 -07:00
|
|
|
check_parse_args( def );
|
2023-07-24 14:45:27 -07:00
|
|
|
using namespace Parser;
|
|
|
|
|
|
|
|
TokArray toks = lex( def );
|
|
|
|
if ( toks.Arr == nullptr )
|
|
|
|
return CodeInvalid;
|
|
|
|
|
2023-07-29 09:25:38 -07:00
|
|
|
Context.Tokens = toks;
|
2023-07-29 02:52:06 -07:00
|
|
|
return (CodeOperator) parse_operator();
|
2023-07-24 14:45:27 -07:00
|
|
|
}
|
|
|
|
|
2023-07-29 02:52:06 -07:00
|
|
|
CodeOpCast parse_operator_cast()
|
2023-07-24 14:45:27 -07:00
|
|
|
{
|
|
|
|
using namespace Parser;
|
2023-07-29 02:52:06 -07:00
|
|
|
push_scope();
|
2023-07-24 14:45:27 -07:00
|
|
|
|
|
|
|
eat( TokType::Decl_Operator );
|
|
|
|
|
2023-07-29 03:32:16 -07:00
|
|
|
Code type = parse_type();
|
2023-07-24 14:45:27 -07:00
|
|
|
|
|
|
|
eat( TokType::Capture_Start );
|
|
|
|
eat( TokType::Capture_End );
|
|
|
|
|
|
|
|
CodeSpecifiers specifiers = { nullptr };
|
|
|
|
|
|
|
|
if ( check(TokType::Spec_Const))
|
|
|
|
specifiers = spec_const;
|
|
|
|
|
|
|
|
Code body = { nullptr };
|
|
|
|
|
|
|
|
if ( check( TokType::BraceCurly_Open) )
|
|
|
|
{
|
|
|
|
eat( TokType::BraceCurly_Open );
|
|
|
|
|
|
|
|
Token body_str = currtok;
|
|
|
|
|
|
|
|
s32 level = 0;
|
|
|
|
while ( left && ( currtok.Type != TokType::BraceCurly_Close || level > 0 ) )
|
|
|
|
{
|
|
|
|
if ( currtok.Type == TokType::BraceCurly_Open )
|
|
|
|
level++;
|
|
|
|
|
|
|
|
else if ( currtok.Type == TokType::BraceCurly_Close && level > 0 )
|
|
|
|
level--;
|
|
|
|
|
|
|
|
eat( currtok.Type );
|
|
|
|
}
|
|
|
|
|
|
|
|
body_str.Length = ( (sptr)prevtok.Text + prevtok.Length ) - (sptr)body_str.Text;
|
|
|
|
|
|
|
|
body = untyped_str( body_str );
|
|
|
|
|
|
|
|
eat( TokType::BraceCurly_Close );
|
|
|
|
}
|
|
|
|
|
|
|
|
CodeOpCast result = (CodeOpCast) make_code();
|
|
|
|
|
|
|
|
if (body)
|
|
|
|
{
|
|
|
|
result->Type = ECode::Operator_Cast;
|
|
|
|
result->Body = body;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
result->Type = ECode::Operator_Cast_Fwd;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ( specifiers )
|
|
|
|
result->Specs = specifiers;
|
|
|
|
|
|
|
|
result->ValueType = type;
|
|
|
|
|
2023-07-29 02:52:06 -07:00
|
|
|
Context.pop();
|
2023-07-24 14:45:27 -07:00
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
CodeOpCast parse_operator_cast( StrC def )
|
|
|
|
{
|
2023-07-29 03:32:16 -07:00
|
|
|
check_parse_args( def );
|
2023-07-24 14:45:27 -07:00
|
|
|
using namespace Parser;
|
|
|
|
|
|
|
|
TokArray toks = lex( def );
|
|
|
|
if ( toks.Arr == nullptr )
|
|
|
|
return CodeInvalid;
|
|
|
|
|
2023-07-29 09:25:38 -07:00
|
|
|
Context.Tokens = toks;
|
2023-07-29 02:52:06 -07:00
|
|
|
return parse_operator_cast();
|
2023-07-24 14:45:27 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
internal inline
|
2023-07-29 02:52:06 -07:00
|
|
|
CodeStruct parse_struct()
|
2023-07-24 14:45:27 -07:00
|
|
|
{
|
2023-07-29 03:32:16 -07:00
|
|
|
using namespace Parser;
|
2023-07-29 02:52:06 -07:00
|
|
|
push_scope();
|
2023-07-29 03:32:16 -07:00
|
|
|
CodeStruct result = (CodeStruct) parse_class_struct( TokType::Decl_Struct );
|
2023-07-29 02:52:06 -07:00
|
|
|
Context.pop();
|
|
|
|
return result;
|
2023-07-24 14:45:27 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
CodeStruct parse_struct( StrC def )
|
|
|
|
{
|
2023-07-29 03:32:16 -07:00
|
|
|
check_parse_args( def );
|
2023-07-24 14:45:27 -07:00
|
|
|
using namespace Parser;
|
|
|
|
|
|
|
|
TokArray toks = lex( def );
|
|
|
|
if ( toks.Arr == nullptr )
|
|
|
|
return CodeInvalid;
|
|
|
|
|
2023-07-29 09:25:38 -07:00
|
|
|
Context.Tokens = toks;
|
2023-07-29 02:52:06 -07:00
|
|
|
return (CodeStruct) parse_class_struct( TokType::Decl_Struct );
|
2023-07-24 14:45:27 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
internal
|
2023-07-29 02:52:06 -07:00
|
|
|
CodeTemplate parse_template()
|
2023-07-24 14:45:27 -07:00
|
|
|
{
|
|
|
|
# define UseTemplateCapture true
|
|
|
|
|
|
|
|
using namespace Parser;
|
2023-07-29 02:52:06 -07:00
|
|
|
push_scope();
|
2023-07-24 14:45:27 -07:00
|
|
|
|
|
|
|
ModuleFlag mflags = ModuleFlag::None;
|
|
|
|
|
|
|
|
if ( check(TokType::Module_Export) )
|
|
|
|
{
|
|
|
|
mflags = ModuleFlag::Export;
|
|
|
|
eat( TokType::Module_Export );
|
|
|
|
}
|
|
|
|
|
|
|
|
eat( TokType::Decl_Template );
|
|
|
|
|
2023-07-29 03:32:16 -07:00
|
|
|
Code params = parse_params( UseTemplateCapture );
|
2023-07-24 14:45:27 -07:00
|
|
|
if ( params == Code::Invalid )
|
|
|
|
return CodeInvalid;
|
|
|
|
|
|
|
|
Code definition = { nullptr };
|
|
|
|
|
|
|
|
while ( left )
|
|
|
|
{
|
|
|
|
if ( check( TokType::Decl_Class ) )
|
|
|
|
{
|
2023-07-29 03:32:16 -07:00
|
|
|
definition = parse_class();
|
2023-07-24 14:45:27 -07:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ( check( TokType::Decl_Struct ) )
|
|
|
|
{
|
2023-07-29 03:32:16 -07:00
|
|
|
definition = parse_enum();
|
2023-07-24 14:45:27 -07:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ( check( TokType::Decl_Using ))
|
|
|
|
{
|
2023-07-29 03:32:16 -07:00
|
|
|
definition = parse_using();
|
2023-07-24 14:45:27 -07:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Its either a function or a variable
|
|
|
|
Token name = { nullptr, 0, TokType::Invalid };
|
|
|
|
|
|
|
|
CodeAttributes attributes = { nullptr };
|
|
|
|
CodeSpecifiers specifiers = { nullptr };
|
|
|
|
|
|
|
|
bool expects_function = false;
|
|
|
|
|
2023-07-27 14:12:58 -07:00
|
|
|
SpecifierT specs_found[16] { ESpecifier::NumSpecifiers };
|
|
|
|
s32 NumSpecifiers = 0;
|
2023-07-24 14:45:27 -07:00
|
|
|
|
2023-07-28 18:44:31 -07:00
|
|
|
attributes = parse_attributes();
|
2023-07-24 14:45:27 -07:00
|
|
|
|
2023-07-28 18:44:31 -07:00
|
|
|
while ( left && currtok.is_specifier() )
|
2023-07-24 14:45:27 -07:00
|
|
|
{
|
|
|
|
SpecifierT spec = ESpecifier::to_type( currtok );
|
|
|
|
|
|
|
|
switch ( spec )
|
|
|
|
{
|
|
|
|
case ESpecifier::Const:
|
|
|
|
case ESpecifier::Constexpr:
|
|
|
|
case ESpecifier::Constinit:
|
|
|
|
case ESpecifier::External_Linkage:
|
|
|
|
case ESpecifier::Global:
|
|
|
|
case ESpecifier::Inline:
|
|
|
|
case ESpecifier::Local_Persist:
|
|
|
|
case ESpecifier::Mutable:
|
|
|
|
case ESpecifier::Static:
|
|
|
|
case ESpecifier::Thread_Local:
|
|
|
|
case ESpecifier::Volatile:
|
|
|
|
break;
|
|
|
|
|
|
|
|
case ESpecifier::Consteval:
|
|
|
|
expects_function = true;
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
2023-07-29 02:52:06 -07:00
|
|
|
log_failure( "Invalid specifier %s for variable or function\n%s", ESpecifier::to_str( spec ), Context.to_string() );
|
2023-07-24 14:45:27 -07:00
|
|
|
return CodeInvalid;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Ignore const it will be handled by the type
|
|
|
|
if ( spec == ESpecifier::Const )
|
|
|
|
continue;
|
|
|
|
|
2023-07-27 14:12:58 -07:00
|
|
|
specs_found[NumSpecifiers] = spec;
|
|
|
|
NumSpecifiers++;
|
2023-07-24 14:45:27 -07:00
|
|
|
eat( currtok.Type );
|
|
|
|
}
|
|
|
|
|
2023-07-27 14:12:58 -07:00
|
|
|
if ( NumSpecifiers )
|
2023-07-24 14:45:27 -07:00
|
|
|
{
|
2023-07-27 14:12:58 -07:00
|
|
|
specifiers = def_specifiers( NumSpecifiers, specs_found );
|
2023-07-24 14:45:27 -07:00
|
|
|
}
|
|
|
|
|
2023-07-29 03:32:16 -07:00
|
|
|
definition = parse_operator_function_or_variable( expects_function, attributes, specifiers );
|
2023-07-24 14:45:27 -07:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
CodeTemplate
|
|
|
|
result = (CodeTemplate) make_code();
|
|
|
|
result->Type = ECode::Template;
|
|
|
|
result->Params = params;
|
|
|
|
result->Declaration = definition;
|
|
|
|
result->ModuleFlags = mflags;
|
|
|
|
|
2023-07-29 02:52:06 -07:00
|
|
|
Context.pop();
|
2023-07-24 14:45:27 -07:00
|
|
|
return result;
|
|
|
|
# undef UseTemplateCapture
|
|
|
|
}
|
|
|
|
|
|
|
|
CodeTemplate parse_template( StrC def )
|
|
|
|
{
|
2023-07-29 03:32:16 -07:00
|
|
|
check_parse_args( def );
|
2023-07-24 14:45:27 -07:00
|
|
|
using namespace Parser;
|
|
|
|
|
|
|
|
TokArray toks = lex( def );
|
|
|
|
if ( toks.Arr == nullptr )
|
|
|
|
return CodeInvalid;
|
|
|
|
|
2023-07-29 09:25:38 -07:00
|
|
|
Context.Tokens = toks;
|
2023-07-29 02:52:06 -07:00
|
|
|
return parse_template();
|
2023-07-24 14:45:27 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
internal
|
2023-07-29 02:52:06 -07:00
|
|
|
CodeType parse_type()
|
2023-07-24 14:45:27 -07:00
|
|
|
{
|
|
|
|
using namespace Parser;
|
2023-07-29 02:52:06 -07:00
|
|
|
push_scope();
|
2023-07-24 14:45:27 -07:00
|
|
|
|
|
|
|
Token context_tok = prevtok;
|
|
|
|
|
2023-07-27 14:12:58 -07:00
|
|
|
SpecifierT specs_found[16] { ESpecifier::NumSpecifiers };
|
|
|
|
s32 NumSpecifiers = 0;
|
2023-07-24 14:45:27 -07:00
|
|
|
|
|
|
|
Token name = { nullptr, 0, TokType::Invalid };
|
|
|
|
Token brute_sig = { currtok.Text, 0, TokType::Invalid };
|
|
|
|
|
2023-07-28 18:44:31 -07:00
|
|
|
CodeAttributes attributes = parse_attributes();
|
2023-07-24 14:45:27 -07:00
|
|
|
|
2023-07-28 18:44:31 -07:00
|
|
|
while ( left && currtok.is_specifier() )
|
2023-07-24 14:45:27 -07:00
|
|
|
{
|
|
|
|
SpecifierT spec = ESpecifier::to_type( currtok );
|
|
|
|
|
|
|
|
if ( spec != ESpecifier::Const )
|
|
|
|
{
|
2023-07-29 02:52:06 -07:00
|
|
|
log_failure( "Error, invalid specifier used in type definition: %s\n%s", currtok.Text, Context.to_string() );
|
2023-07-24 14:45:27 -07:00
|
|
|
return CodeInvalid;
|
|
|
|
}
|
|
|
|
|
2023-07-27 14:12:58 -07:00
|
|
|
specs_found[NumSpecifiers] = spec;
|
|
|
|
NumSpecifiers++;
|
2023-07-24 14:45:27 -07:00
|
|
|
eat( currtok.Type );
|
|
|
|
}
|
|
|
|
|
|
|
|
if ( left == 0 )
|
|
|
|
{
|
2023-07-29 02:52:06 -07:00
|
|
|
log_failure( "Error, unexpected end of type definition\n%s", Context.to_string() );
|
2023-07-24 14:45:27 -07:00
|
|
|
return CodeInvalid;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ( currtok.Type == TokType::Decl_Class
|
|
|
|
|| currtok.Type == TokType::Decl_Enum
|
|
|
|
|| currtok.Type == TokType::Decl_Struct )
|
|
|
|
{
|
|
|
|
name = currtok;
|
|
|
|
eat( currtok.Type );
|
|
|
|
|
|
|
|
name.Length = ( (sptr)currtok.Text + currtok.Length ) - (sptr)name.Text;
|
|
|
|
eat( TokType::Identifier );
|
|
|
|
}
|
|
|
|
else if ( currtok.Type >= TokType::Type_Unsigned )
|
|
|
|
{
|
|
|
|
name = currtok;
|
|
|
|
eat( currtok.Type );
|
|
|
|
|
|
|
|
while (currtok.Type >= TokType::Type_Unsigned)
|
|
|
|
{
|
|
|
|
eat( currtok.Type );
|
|
|
|
}
|
|
|
|
|
|
|
|
name.Length = ( (sptr)prevtok.Text + prevtok.Length ) - (sptr)name.Text;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2023-07-29 03:32:16 -07:00
|
|
|
name = parse_identifier();
|
2023-07-24 14:45:27 -07:00
|
|
|
if ( ! name )
|
|
|
|
return CodeInvalid;
|
|
|
|
|
|
|
|
// Problably dealing with a templated symbol
|
|
|
|
if ( currtok.Type == TokType::Operator && currtok.Text[0] == '<' && currtok.Length == 1 )
|
|
|
|
{
|
|
|
|
eat( TokType::Operator );
|
|
|
|
|
|
|
|
s32 level = 0;
|
|
|
|
while ( left && ( currtok.Text[0] != '>' || level > 0 ))
|
|
|
|
{
|
|
|
|
if ( currtok.Text[0] == '<' )
|
|
|
|
level++;
|
|
|
|
|
|
|
|
if ( currtok.Text[0] == '>' )
|
|
|
|
level--;
|
|
|
|
|
|
|
|
eat( currtok.Type );
|
|
|
|
}
|
|
|
|
|
|
|
|
eat( TokType::Operator );
|
|
|
|
|
|
|
|
// Extend length of name to last token
|
|
|
|
name.Length = ( (sptr)prevtok.Text + prevtok.Length ) - (sptr)name.Text;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-07-28 18:44:31 -07:00
|
|
|
while ( left && currtok.is_specifier() )
|
2023-07-24 14:45:27 -07:00
|
|
|
{
|
|
|
|
SpecifierT spec = ESpecifier::to_type( currtok );
|
|
|
|
|
|
|
|
if ( spec != ESpecifier::Const
|
|
|
|
&& spec != ESpecifier::Ptr
|
|
|
|
&& spec != ESpecifier::Ref
|
|
|
|
&& spec != ESpecifier::RValue )
|
|
|
|
{
|
2023-07-29 02:52:06 -07:00
|
|
|
log_failure( "Error, invalid specifier used in type definition: %s\n%s", currtok.Text, Context.to_string() );
|
2023-07-24 14:45:27 -07:00
|
|
|
return CodeInvalid;
|
|
|
|
}
|
|
|
|
|
2023-07-27 14:12:58 -07:00
|
|
|
specs_found[NumSpecifiers] = spec;
|
|
|
|
NumSpecifiers++;
|
2023-07-24 14:45:27 -07:00
|
|
|
eat( currtok.Type );
|
|
|
|
}
|
|
|
|
|
|
|
|
// Not sure if its technically possible to cast ot a function pointer user defined operator cast...
|
|
|
|
// Supporting it is not worth the effort.
|
|
|
|
if ( check( TokType::Capture_Start ) && context_tok.Type != TokType::Decl_Operator )
|
|
|
|
{
|
|
|
|
// Its a function type
|
|
|
|
eat( TokType::Capture_Start );
|
|
|
|
|
|
|
|
while ( check( TokType::Star ) || currtok.Type == TokType::Spec_Const )
|
|
|
|
{
|
|
|
|
eat( currtok.Type );
|
|
|
|
}
|
|
|
|
|
|
|
|
// if its a using statement there will not be an ID.
|
|
|
|
if ( check( TokType::Identifier ) )
|
|
|
|
eat(TokType::Identifier);
|
|
|
|
|
|
|
|
eat( TokType::Capture_End );
|
|
|
|
|
|
|
|
// Parameters
|
|
|
|
|
|
|
|
eat( TokType::Capture_Start );
|
|
|
|
|
|
|
|
// TODO : Change this to validate the parameters...
|
|
|
|
// Bruteforce lex the parameters, no validation.
|
|
|
|
s32 level = 0;
|
|
|
|
while ( ! check( TokType::Capture_End ) || level > 0 )
|
|
|
|
{
|
|
|
|
if ( check( TokType::Capture_Start ) )
|
|
|
|
level++;
|
|
|
|
|
|
|
|
if ( check( TokType::Capture_End ) )
|
|
|
|
level--;
|
|
|
|
|
|
|
|
eat( currtok.Type );
|
|
|
|
}
|
|
|
|
|
|
|
|
eat(TokType::Capture_End);
|
|
|
|
|
|
|
|
brute_sig.Length = ( (sptr)prevtok.Text + prevtok.Length ) - (sptr)brute_sig.Text;
|
|
|
|
}
|
|
|
|
|
|
|
|
using namespace ECode;
|
|
|
|
|
|
|
|
CodeType
|
|
|
|
result = (CodeType) make_code();
|
|
|
|
result->Type = Typename;
|
|
|
|
|
|
|
|
if ( brute_sig.Length > 0 )
|
|
|
|
{
|
|
|
|
// Bruteforce all tokens together.
|
|
|
|
name = brute_sig;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2023-07-27 14:12:58 -07:00
|
|
|
if (NumSpecifiers)
|
2023-07-24 14:45:27 -07:00
|
|
|
{
|
2023-07-27 14:12:58 -07:00
|
|
|
Code specifiers = def_specifiers( NumSpecifiers, (SpecifierT*)specs_found );
|
2023-07-24 14:45:27 -07:00
|
|
|
result->Specs = specifiers;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
result->Name = get_cached_string( name );
|
|
|
|
|
|
|
|
if ( attributes )
|
|
|
|
result->Attributes = attributes;
|
|
|
|
|
2023-07-29 02:52:06 -07:00
|
|
|
Context.pop();
|
2023-07-24 14:45:27 -07:00
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
CodeType parse_type( StrC def )
|
|
|
|
{
|
2023-07-29 03:32:16 -07:00
|
|
|
check_parse_args( def );
|
2023-07-24 14:45:27 -07:00
|
|
|
using namespace Parser;
|
|
|
|
|
|
|
|
TokArray toks = lex( def );
|
|
|
|
if ( toks.Arr == nullptr )
|
|
|
|
return CodeInvalid;
|
|
|
|
|
2023-07-29 09:25:38 -07:00
|
|
|
Context.Tokens = toks;
|
2023-07-29 02:52:06 -07:00
|
|
|
return parse_type();
|
2023-07-24 14:45:27 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
internal
|
2023-07-29 02:52:06 -07:00
|
|
|
CodeTypedef parse_typedef()
|
2023-07-24 14:45:27 -07:00
|
|
|
{
|
|
|
|
using namespace Parser;
|
2023-07-29 02:52:06 -07:00
|
|
|
push_scope();
|
2023-07-24 14:45:27 -07:00
|
|
|
|
|
|
|
Token name = { nullptr, 0, TokType::Invalid };
|
|
|
|
Code array_expr = { nullptr };
|
|
|
|
Code type = { nullptr };
|
|
|
|
|
|
|
|
ModuleFlag mflags = ModuleFlag::None;
|
|
|
|
|
|
|
|
if ( check(TokType::Module_Export) )
|
|
|
|
{
|
|
|
|
mflags = ModuleFlag::Export;
|
|
|
|
eat( TokType::Module_Export );
|
|
|
|
}
|
|
|
|
|
|
|
|
eat( TokType::Decl_Typedef );
|
|
|
|
|
|
|
|
if ( check( TokType::Decl_Enum ) )
|
2023-07-29 03:32:16 -07:00
|
|
|
type = parse_enum();
|
2023-07-24 14:45:27 -07:00
|
|
|
|
|
|
|
else if ( check(TokType::Decl_Class ) )
|
2023-07-29 03:32:16 -07:00
|
|
|
type = parse_class();
|
2023-07-24 14:45:27 -07:00
|
|
|
|
|
|
|
else if ( check(TokType::Decl_Struct ) )
|
2023-07-29 03:32:16 -07:00
|
|
|
type = parse_struct();
|
2023-07-24 14:45:27 -07:00
|
|
|
|
|
|
|
else if ( check(TokType::Decl_Union) )
|
2023-07-29 03:32:16 -07:00
|
|
|
type = parse_union();
|
2023-07-24 14:45:27 -07:00
|
|
|
|
|
|
|
else
|
2023-07-29 03:32:16 -07:00
|
|
|
type = parse_type();
|
2023-07-24 14:45:27 -07:00
|
|
|
|
|
|
|
if ( ! check( TokType::Identifier ) )
|
|
|
|
{
|
2023-07-29 02:52:06 -07:00
|
|
|
log_failure( "Error, expected identifier for typedef\n%s", Context.to_string() );
|
2023-07-24 14:45:27 -07:00
|
|
|
return CodeInvalid;
|
|
|
|
}
|
|
|
|
|
|
|
|
name = currtok;
|
|
|
|
eat( TokType::Identifier );
|
|
|
|
|
2023-07-29 03:32:16 -07:00
|
|
|
array_expr = parse_array_decl();
|
2023-07-24 14:45:27 -07:00
|
|
|
|
|
|
|
eat( TokType::Statement_End );
|
|
|
|
|
|
|
|
using namespace ECode;
|
|
|
|
|
|
|
|
CodeTypedef
|
|
|
|
result = (CodeTypedef) make_code();
|
|
|
|
result->Type = Typedef;
|
|
|
|
result->Name = get_cached_string( name );
|
|
|
|
result->ModuleFlags = mflags;
|
|
|
|
|
|
|
|
result->UnderlyingType = type;
|
|
|
|
|
|
|
|
if ( type->Type == Typename && array_expr && array_expr->Type != Invalid )
|
|
|
|
type.cast<CodeType>()->ArrExpr = array_expr;
|
|
|
|
|
2023-07-29 02:52:06 -07:00
|
|
|
Context.pop();
|
2023-07-24 14:45:27 -07:00
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
CodeTypedef parse_typedef( StrC def )
|
|
|
|
{
|
2023-07-29 03:32:16 -07:00
|
|
|
check_parse_args( def );
|
2023-07-24 14:45:27 -07:00
|
|
|
using namespace Parser;
|
|
|
|
|
|
|
|
TokArray toks = lex( def );
|
|
|
|
if ( toks.Arr == nullptr )
|
|
|
|
return CodeInvalid;
|
|
|
|
|
2023-07-29 09:25:38 -07:00
|
|
|
Context.Tokens = toks;
|
2023-07-29 02:52:06 -07:00
|
|
|
return parse_typedef();
|
2023-07-24 14:45:27 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
internal
|
2023-07-29 02:52:06 -07:00
|
|
|
CodeUnion parse_union()
|
2023-07-24 14:45:27 -07:00
|
|
|
{
|
|
|
|
using namespace Parser;
|
2023-07-29 02:52:06 -07:00
|
|
|
push_scope();
|
2023-07-24 14:45:27 -07:00
|
|
|
|
|
|
|
ModuleFlag mflags = ModuleFlag::None;
|
|
|
|
|
|
|
|
if ( check(TokType::Module_Export) )
|
|
|
|
{
|
|
|
|
mflags = ModuleFlag::Export;
|
|
|
|
eat( TokType::Module_Export );
|
|
|
|
}
|
|
|
|
|
|
|
|
eat( TokType::Decl_Union );
|
|
|
|
|
2023-07-28 18:44:31 -07:00
|
|
|
CodeAttributes attributes = parse_attributes();
|
2023-07-24 14:45:27 -07:00
|
|
|
|
|
|
|
StrC name = { 0, nullptr };
|
|
|
|
|
|
|
|
if ( check( TokType::Identifier ) )
|
|
|
|
{
|
|
|
|
name = currtok;
|
|
|
|
eat( TokType::Identifier );
|
|
|
|
}
|
|
|
|
|
|
|
|
CodeBody body = { nullptr };
|
|
|
|
|
|
|
|
eat( TokType::BraceCurly_Open );
|
|
|
|
|
|
|
|
body = make_code();
|
|
|
|
body->Type = ECode::Union_Body;
|
|
|
|
|
|
|
|
while ( ! check( TokType::BraceCurly_Close ) )
|
|
|
|
{
|
2023-07-29 03:32:16 -07:00
|
|
|
Code entry = parse_variable();
|
2023-07-24 14:45:27 -07:00
|
|
|
|
|
|
|
if ( entry )
|
|
|
|
body.append( entry );
|
|
|
|
}
|
|
|
|
|
|
|
|
eat( TokType::BraceCurly_Close );
|
|
|
|
eat( TokType::Statement_End );
|
|
|
|
|
|
|
|
CodeUnion
|
|
|
|
result = (CodeUnion) make_code();
|
|
|
|
result->Type = ECode::Union;
|
|
|
|
result->ModuleFlags = mflags;
|
|
|
|
|
|
|
|
if ( name )
|
|
|
|
result->Name = get_cached_string( name );
|
|
|
|
|
|
|
|
if ( body )
|
|
|
|
result->Body = body;
|
|
|
|
|
|
|
|
if ( attributes )
|
|
|
|
result->Attributes = attributes;
|
|
|
|
|
2023-07-29 02:52:06 -07:00
|
|
|
Context.pop();
|
2023-07-24 14:45:27 -07:00
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
CodeUnion parse_union( StrC def )
|
|
|
|
{
|
2023-07-29 03:32:16 -07:00
|
|
|
check_parse_args( def );
|
2023-07-24 14:45:27 -07:00
|
|
|
using namespace Parser;
|
|
|
|
|
|
|
|
TokArray toks = lex( def );
|
|
|
|
if ( toks.Arr == nullptr )
|
|
|
|
return CodeInvalid;
|
|
|
|
|
2023-07-29 09:25:38 -07:00
|
|
|
Context.Tokens = toks;
|
2023-07-29 02:52:06 -07:00
|
|
|
return parse_union();
|
2023-07-24 14:45:27 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
internal
|
2023-07-29 02:52:06 -07:00
|
|
|
CodeUsing parse_using()
|
2023-07-24 14:45:27 -07:00
|
|
|
{
|
|
|
|
using namespace Parser;
|
2023-07-29 02:52:06 -07:00
|
|
|
push_scope();
|
2023-07-24 14:45:27 -07:00
|
|
|
|
|
|
|
SpecifierT specs_found[16] { ESpecifier::Invalid };
|
2023-07-27 14:12:58 -07:00
|
|
|
s32 NumSpecifiers = 0;
|
2023-07-24 14:45:27 -07:00
|
|
|
|
|
|
|
Token name = { nullptr, 0, TokType::Invalid };
|
|
|
|
Code array_expr = { nullptr };
|
|
|
|
CodeType type = { nullptr };
|
|
|
|
|
|
|
|
bool is_namespace = false;
|
|
|
|
|
|
|
|
ModuleFlag mflags = ModuleFlag::None;
|
|
|
|
CodeAttributes attributes = { nullptr };
|
|
|
|
|
|
|
|
if ( check(TokType::Module_Export) )
|
|
|
|
{
|
|
|
|
mflags = ModuleFlag::Export;
|
|
|
|
eat( TokType::Module_Export );
|
|
|
|
}
|
|
|
|
|
|
|
|
eat( TokType::Decl_Using );
|
|
|
|
|
|
|
|
if ( currtok.Type == TokType::Decl_Namespace )
|
|
|
|
{
|
|
|
|
is_namespace = true;
|
|
|
|
eat( TokType::Decl_Namespace );
|
|
|
|
}
|
|
|
|
|
|
|
|
name = currtok;
|
|
|
|
eat( TokType::Identifier );
|
|
|
|
|
|
|
|
if ( currtok.IsAssign )
|
|
|
|
{
|
2023-07-29 03:32:16 -07:00
|
|
|
attributes = parse_attributes();
|
2023-07-24 14:45:27 -07:00
|
|
|
|
|
|
|
eat( TokType::Operator );
|
|
|
|
|
2023-07-29 03:32:16 -07:00
|
|
|
type = parse_type();
|
2023-07-24 14:45:27 -07:00
|
|
|
}
|
|
|
|
|
2023-07-29 03:32:16 -07:00
|
|
|
array_expr = parse_array_decl();
|
2023-07-24 14:45:27 -07:00
|
|
|
|
|
|
|
eat( TokType::Statement_End );
|
|
|
|
|
|
|
|
using namespace ECode;
|
|
|
|
|
|
|
|
CodeUsing
|
|
|
|
result = (CodeUsing) make_code();
|
|
|
|
result->Name = get_cached_string( name );
|
|
|
|
result->ModuleFlags = mflags;
|
|
|
|
|
|
|
|
if ( is_namespace)
|
|
|
|
{
|
|
|
|
result->Type = Using_Namespace;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
result->Type = Using;
|
|
|
|
|
|
|
|
if ( type )
|
|
|
|
result->UnderlyingType = type;
|
|
|
|
|
|
|
|
if ( array_expr )
|
|
|
|
type->ArrExpr = array_expr;
|
|
|
|
|
|
|
|
if ( attributes )
|
|
|
|
result->Attributes = attributes;
|
|
|
|
}
|
2023-07-29 02:52:06 -07:00
|
|
|
|
|
|
|
Context.pop();
|
2023-07-24 14:45:27 -07:00
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
CodeUsing parse_using( StrC def )
|
|
|
|
{
|
2023-07-29 03:32:16 -07:00
|
|
|
check_parse_args( def );
|
2023-07-24 14:45:27 -07:00
|
|
|
using namespace Parser;
|
|
|
|
|
|
|
|
TokArray toks = lex( def );
|
|
|
|
if ( toks.Arr == nullptr )
|
|
|
|
return CodeInvalid;
|
|
|
|
|
2023-07-29 09:25:38 -07:00
|
|
|
Context.Tokens = toks;
|
2023-07-29 02:52:06 -07:00
|
|
|
return parse_using();
|
2023-07-24 14:45:27 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
internal
|
2023-07-28 18:44:31 -07:00
|
|
|
CodeVar parse_variable()
|
2023-07-24 14:45:27 -07:00
|
|
|
{
|
|
|
|
using namespace Parser;
|
2023-07-29 02:52:06 -07:00
|
|
|
push_scope();
|
2023-07-24 14:45:27 -07:00
|
|
|
|
2023-07-27 14:12:58 -07:00
|
|
|
SpecifierT specs_found[16] { ESpecifier::NumSpecifiers };
|
|
|
|
s32 NumSpecifiers = 0;
|
2023-07-24 14:45:27 -07:00
|
|
|
|
|
|
|
ModuleFlag mflags = ModuleFlag::None;
|
|
|
|
CodeAttributes attributes = { nullptr };
|
|
|
|
CodeSpecifiers specifiers = { nullptr };
|
|
|
|
|
|
|
|
if ( check(TokType::Module_Export) )
|
|
|
|
{
|
|
|
|
mflags = ModuleFlag::Export;
|
|
|
|
eat( TokType::Module_Export );
|
|
|
|
}
|
|
|
|
|
2023-07-28 18:44:31 -07:00
|
|
|
attributes = parse_attributes();
|
2023-07-24 14:45:27 -07:00
|
|
|
|
2023-07-29 03:32:16 -07:00
|
|
|
while ( left && currtok.is_specifier() )
|
2023-07-24 14:45:27 -07:00
|
|
|
{
|
|
|
|
SpecifierT spec = ESpecifier::to_type( currtok );
|
|
|
|
|
|
|
|
switch ( spec )
|
|
|
|
{
|
|
|
|
case ESpecifier::Const:
|
|
|
|
case ESpecifier::Constexpr:
|
|
|
|
case ESpecifier::Constinit:
|
|
|
|
case ESpecifier::External_Linkage:
|
|
|
|
case ESpecifier::Global:
|
|
|
|
case ESpecifier::Inline:
|
|
|
|
case ESpecifier::Local_Persist:
|
|
|
|
case ESpecifier::Mutable:
|
|
|
|
case ESpecifier::Static:
|
|
|
|
case ESpecifier::Thread_Local:
|
|
|
|
case ESpecifier::Volatile:
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
2023-07-29 02:52:06 -07:00
|
|
|
log_failure( "Invalid specifier %s for variable\n%s", ESpecifier::to_str( spec ), Context.to_string() );
|
2023-07-24 14:45:27 -07:00
|
|
|
return CodeInvalid;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Ignore const specifiers, they're handled by the type
|
|
|
|
if ( spec == ESpecifier::Const )
|
|
|
|
continue;
|
|
|
|
|
2023-07-27 14:12:58 -07:00
|
|
|
specs_found[NumSpecifiers] = spec;
|
|
|
|
NumSpecifiers++;
|
2023-07-24 14:45:27 -07:00
|
|
|
eat( currtok.Type );
|
|
|
|
}
|
|
|
|
|
2023-07-27 14:12:58 -07:00
|
|
|
if ( NumSpecifiers )
|
2023-07-24 14:45:27 -07:00
|
|
|
{
|
2023-07-27 14:12:58 -07:00
|
|
|
specifiers = def_specifiers( NumSpecifiers, specs_found );
|
2023-07-24 14:45:27 -07:00
|
|
|
}
|
|
|
|
|
2023-07-29 03:32:16 -07:00
|
|
|
CodeType type = parse_type();
|
2023-07-24 14:45:27 -07:00
|
|
|
|
|
|
|
if ( type == Code::Invalid )
|
|
|
|
return CodeInvalid;
|
|
|
|
|
2023-07-29 03:32:16 -07:00
|
|
|
Context.Scope->Name = currtok;
|
2023-07-24 14:45:27 -07:00
|
|
|
eat( TokType::Identifier );
|
|
|
|
|
2023-07-29 02:52:06 -07:00
|
|
|
CodeVar result = parse_variable_after_name( mflags, attributes, specifiers, type, Context.Scope->Name );
|
2023-07-24 14:45:27 -07:00
|
|
|
|
2023-07-29 02:52:06 -07:00
|
|
|
Context.pop();
|
2023-07-24 14:45:27 -07:00
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
CodeVar parse_variable( StrC def )
|
|
|
|
{
|
2023-07-29 03:32:16 -07:00
|
|
|
check_parse_args( def );
|
2023-07-29 02:52:06 -07:00
|
|
|
using namespace Parser;
|
2023-07-24 14:45:27 -07:00
|
|
|
|
|
|
|
TokArray toks = lex( def );
|
|
|
|
if ( toks.Arr == nullptr )
|
|
|
|
return CodeInvalid;
|
|
|
|
|
2023-07-29 09:25:38 -07:00
|
|
|
Context.Tokens = toks;
|
2023-07-28 18:44:31 -07:00
|
|
|
return parse_variable();
|
2023-07-24 14:45:27 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
// Undef helper macros
|
|
|
|
# undef check_parse_args
|
2023-07-29 02:52:06 -07:00
|
|
|
# undef nexttok
|
|
|
|
# undef currtok
|
|
|
|
# undef prevtok
|
2023-07-24 14:45:27 -07:00
|
|
|
# undef eat
|
|
|
|
# undef left
|
2023-07-29 02:52:06 -07:00
|
|
|
# undef check
|
|
|
|
# undef push_scope
|