WIP: prepped for dealing with the compiler...

This commit is contained in:
Edward R. Gonzalez 2024-12-14 20:33:14 -05:00
parent c8cf55403b
commit 1b4f9a2e77
5 changed files with 332 additions and 401 deletions

View File

@ -427,30 +427,24 @@ s32 lex_preprocessor_directive( LexContext* ctx )
forceinline forceinline
void lex_found_token( LexContext* ctx ) void lex_found_token( LexContext* ctx )
{ {
if ( ctx->token.Type != Tok_Invalid ) if ( ctx->token.Type != Tok_Invalid ) {
{
array_append( _ctx->Lexer_Tokens, ctx->token ); array_append( _ctx->Lexer_Tokens, ctx->token );
return; return;
} }
TokType type = str_to_toktype( tok_to_str(ctx->token) ); TokType type = str_to_toktype( tok_to_str(ctx->token) );
if (type <= Tok_Access_Public && type >= Tok_Access_Private ) if (type <= Tok_Access_Public && type >= Tok_Access_Private ) {
{
ctx->token.Flags |= TF_AccessSpecifier; ctx->token.Flags |= TF_AccessSpecifier;
} }
if ( type > Tok___Attributes_Start ) {
if ( type > Tok___Attributes_Start )
{
ctx->token.Flags |= TF_Attribute; ctx->token.Flags |= TF_Attribute;
} }
if ( type == Tok_Decl_Extern_Linkage ) if ( type == Tok_Decl_Extern_Linkage )
{ {
skip_whitespace(); skip_whitespace();
if ( (* ctx->scanner) != '"' ) if ( (* ctx->scanner) != '"' ) {
{
type = Tok_Spec_Extern; type = Tok_Spec_Extern;
ctx->token.Flags |= TF_Specifier; ctx->token.Flags |= TF_Specifier;
} }
@ -459,7 +453,6 @@ void lex_found_token( LexContext* ctx )
array_append( _ctx->Lexer_Tokens, ctx->token ); array_append( _ctx->Lexer_Tokens, ctx->token );
return; return;
} }
if ( ( type <= Tok_Star && type >= Tok_Spec_Alignas) if ( ( type <= Tok_Star && type >= Tok_Spec_Alignas)
|| type == Tok_Ampersand || type == Tok_Ampersand
|| type == Tok_Ampersand_DBL ) || type == Tok_Ampersand_DBL )
@ -469,8 +462,6 @@ void lex_found_token( LexContext* ctx )
array_append( _ctx->Lexer_Tokens, ctx->token ); array_append( _ctx->Lexer_Tokens, ctx->token );
return; return;
} }
if ( type != Tok_Invalid ) if ( type != Tok_Invalid )
{ {
ctx->token.Type = type; ctx->token.Type = type;
@ -478,21 +469,17 @@ void lex_found_token( LexContext* ctx )
return; return;
} }
u64 key = 0; PreprocessorMacro* macro = lookup_preprocess_macro( ctx->token.Text );
// if ( (* ctx->scanner) == '(') if ( macro )
// key = crc32( ctx->token.Text.Ptr, ctx->token.Text.Len + 1 );
// else
key = crc32( ctx->token.Text.Ptr, ctx->token.Text.Len );
Str* define = hashtable_get(_ctx->PreprocessMacros, key );
if ( define )
{ {
// TODO(Ed): Needs updating (Macros) ctx->token.Type = macrotype_to_toktype(macro->Type);
ctx->token.Type = Tok_Preprocess_Macro;
// TODO(Ed): When we introduce a macro AST (and expression support), we'll properly lex this section.
// Want to ignore any arguments the define may have as they can be execution expressions. // Want to ignore any arguments the define may have as they can be execution expressions.
if ( ctx->left && (* ctx->scanner) == '(' ) if ( ctx->left && (* ctx->scanner) == '(' )
{ {
ctx->token.Flags |= TF_Macro_Functional;
move_forward(); move_forward();
ctx->token.Text.Len++; ctx->token.Text.Len++;
@ -512,7 +499,6 @@ void lex_found_token( LexContext* ctx )
move_forward(); move_forward();
ctx->token.Text.Len++; ctx->token.Text.Len++;
} }
//if ( (* ctx->scanner) == '\r' && ctx->scanner[1] == '\n' ) //if ( (* ctx->scanner) == '\r' && ctx->scanner[1] == '\n' )
//{ //{
// move_forward(); // move_forward();
@ -1002,7 +988,7 @@ TokArray lex( Str content )
goto FoundToken; goto FoundToken;
} }
// Dash is unfortunatlly a bit more complicated... // Dash is unfortunately a bit more complicated...
case '-': case '-':
{ {
Str text = { c.scanner, 1 }; Str text = { c.scanner, 1 };
@ -1139,8 +1125,7 @@ TokArray lex( Str content )
c.token.Text = text; c.token.Text = text;
move_forward(); move_forward();
while ( c.left && ( char_is_alphanumeric((* ctx->scanner)) || (* ctx->scanner) == '_' ) ) while ( c.left && ( char_is_alphanumeric((* ctx->scanner)) || (* ctx->scanner) == '_' ) ) {
{
move_forward(); move_forward();
c.token.Text.Len++; c.token.Text.Len++;
} }
@ -1166,8 +1151,7 @@ TokArray lex( Str content )
move_forward(); move_forward();
c.token.Text.Len++; c.token.Text.Len++;
while ( c.left && char_is_hex_digit((* ctx->scanner)) ) while ( c.left && char_is_hex_digit((* ctx->scanner)) ) {
{
move_forward(); move_forward();
c.token.Text.Len++; c.token.Text.Len++;
} }
@ -1175,8 +1159,7 @@ TokArray lex( Str content )
goto FoundToken; goto FoundToken;
} }
while ( c.left && char_is_digit((* ctx->scanner)) ) while ( c.left && char_is_digit((* ctx->scanner)) ) {
{
move_forward(); move_forward();
c.token.Text.Len++; c.token.Text.Len++;
} }
@ -1186,8 +1169,7 @@ TokArray lex( Str content )
move_forward(); move_forward();
c.token.Text.Len++; c.token.Text.Len++;
while ( c.left && char_is_digit((* ctx->scanner)) ) while ( c.left && char_is_digit((* ctx->scanner)) ) {
{
move_forward(); move_forward();
c.token.Text.Len++; c.token.Text.Len++;
} }
@ -1205,8 +1187,7 @@ TokArray lex( Str content )
c.token.Text.Len++; c.token.Text.Len++;
// Handle 'll'/'LL' as a special case when we just processed an 'l'/'L' // Handle 'll'/'LL' as a special case when we just processed an 'l'/'L'
if (c.left && (prev == 'l' || prev == 'L') && ((* ctx->scanner) == 'l' || (* ctx->scanner) == 'L')) if (c.left && (prev == 'l' || prev == 'L') && ((* ctx->scanner) == 'l' || (* ctx->scanner) == 'L')) {
{
move_forward(); move_forward();
c.token.Text.Len++; c.token.Text.Len++;
} }
@ -1232,8 +1213,7 @@ TokArray lex( Str content )
log_failure( "Failed to lex token '%c' (%d, %d)\n%s", (* ctx->scanner), c.line, c.column, context_str ); log_failure( "Failed to lex token '%c' (%d, %d)\n%s", (* ctx->scanner), c.line, c.column, context_str );
// Skip to next whitespace since we can't know if anything else is valid until then. // Skip to next whitespace since we can't know if anything else is valid until then.
while ( c.left && ! char_is_space( (* ctx->scanner) ) ) while ( c.left && ! char_is_space( (* ctx->scanner) ) ) {
{
move_forward(); move_forward();
} }
} }
@ -1242,17 +1222,14 @@ TokArray lex( Str content )
{ {
lex_found_token( ctx ); lex_found_token( ctx );
TokType last_type = array_back(_ctx->Lexer_Tokens)->Type; TokType last_type = array_back(_ctx->Lexer_Tokens)->Type;
// TODO(Ed): Change this to just detect if its a MACRO THAT SHOULD ACCEPT NEWLINES if ( last_type == Tok_Preprocess_Macro_Stmt )
if ( last_type == Tok_Preprocess_Macro )
{ {
Token thanks_c = { { c.scanner, 0 }, Tok_Invalid, c.line, c.column, TF_Null }; Token thanks_c = { { c.scanner, 0 }, Tok_Invalid, c.line, c.column, TF_Null };
c.token = thanks_c; c.token = thanks_c;
if ( (* ctx->scanner) == '\r') if ( (* ctx->scanner) == '\r') {
{
move_forward(); move_forward();
c.token.Text.Len = 1; c.token.Text.Len = 1;
} }
if ( (* ctx->scanner) == '\n' ) if ( (* ctx->scanner) == '\n' )
{ {
c.token.Type = Tok_NewLine; c.token.Type = Tok_NewLine;
@ -1266,14 +1243,11 @@ TokArray lex( Str content )
} }
} }
if ( array_num(_ctx->Lexer_Tokens) == 0 ) if ( array_num(_ctx->Lexer_Tokens) == 0 ) {
{
log_failure( "Failed to lex any tokens" ); log_failure( "Failed to lex any tokens" );
{
TokArray tok_array = {}; TokArray tok_array = {};
return tok_array; return tok_array;
} }
}
TokArray result = { _ctx->Lexer_Tokens, 0 }; TokArray result = { _ctx->Lexer_Tokens, 0 };
return result; return result;

File diff suppressed because it is too large Load Diff

View File

@ -138,7 +138,18 @@ enum MacroType : u16
MF_UnderlyingType = GEN_U16_Max, MF_UnderlyingType = GEN_U16_Max,
}; };
Str macro_type_to_str( MacroType type ) forceinline
TokenType macrotype_to_toktype( MacroType type ) {
switch ( type ) {
case MT_Statement : return Tok_Preprocess_Macro_Stmt;
case MT_Expression : return Tok_Preprocess_Macro_Expr;
case MT_Typename : return Tok_Preprocess_Macro_Typename;
}
// All others unsupported for now.
return Tok_Invalid;
}
Str macrotype_to_str( MacroType type )
{ {
local_persist local_persist
Str lookup[ (u32)Num_ModuleFlags ] = { Str lookup[ (u32)Num_ModuleFlags ] = {

View File

@ -506,6 +506,7 @@ CodeBody gen_ast_inlines()
CodeBody impl_code_constr = parse_global_body( token_fmt( "typename", Str name(CodeConstructor), code_impl_tmpl )); CodeBody impl_code_constr = parse_global_body( token_fmt( "typename", Str name(CodeConstructor), code_impl_tmpl ));
CodeBody impl_code_class = parse_global_body( token_fmt( "typename", Str name(CodeClass), code_impl_tmpl )); CodeBody impl_code_class = parse_global_body( token_fmt( "typename", Str name(CodeClass), code_impl_tmpl ));
CodeBody impl_code_define = parse_global_body( token_fmt( "typename", Str name(CodeDefine), code_impl_tmpl )); CodeBody impl_code_define = parse_global_body( token_fmt( "typename", Str name(CodeDefine), code_impl_tmpl ));
CodeBody impl_code_define_params = parse_global_body( token_fmt( "typename", Str name(CodeDefineParams), code_impl_tmpl ));
CodeBody impl_code_destruct = parse_global_body( token_fmt( "typename", Str name(CodeDestructor), code_impl_tmpl )); CodeBody impl_code_destruct = parse_global_body( token_fmt( "typename", Str name(CodeDestructor), code_impl_tmpl ));
CodeBody impl_code_enum = parse_global_body( token_fmt( "typename", Str name(CodeEnum), code_impl_tmpl )); CodeBody impl_code_enum = parse_global_body( token_fmt( "typename", Str name(CodeEnum), code_impl_tmpl ));
CodeBody impl_code_exec = parse_global_body( token_fmt( "typename", Str name(CodeExec), code_impl_tmpl )); CodeBody impl_code_exec = parse_global_body( token_fmt( "typename", Str name(CodeExec), code_impl_tmpl ));
@ -569,6 +570,7 @@ CodeBody gen_ast_inlines()
CodeBody impl_cast_constr = parse_global_body( token_fmt( "typename", Str name(Constructor), cast_tmpl )); CodeBody impl_cast_constr = parse_global_body( token_fmt( "typename", Str name(Constructor), cast_tmpl ));
CodeBody impl_cast_class = parse_global_body( token_fmt( "typename", Str name(Class), cast_tmpl )); CodeBody impl_cast_class = parse_global_body( token_fmt( "typename", Str name(Class), cast_tmpl ));
CodeBody impl_cast_define = parse_global_body( token_fmt( "typename", Str name(Define), cast_tmpl )); CodeBody impl_cast_define = parse_global_body( token_fmt( "typename", Str name(Define), cast_tmpl ));
CodeBody impl_cast_define_params = parse_global_body( token_fmt( "typename", Str name(DefineParams), cast_tmpl ));
CodeBody impl_cast_destruct = parse_global_body( token_fmt( "typename", Str name(Destructor), cast_tmpl )); CodeBody impl_cast_destruct = parse_global_body( token_fmt( "typename", Str name(Destructor), cast_tmpl ));
CodeBody impl_cast_enum = parse_global_body( token_fmt( "typename", Str name(Enum), cast_tmpl )); CodeBody impl_cast_enum = parse_global_body( token_fmt( "typename", Str name(Enum), cast_tmpl ));
CodeBody impl_cast_exec = parse_global_body( token_fmt( "typename", Str name(Exec), cast_tmpl )); CodeBody impl_cast_exec = parse_global_body( token_fmt( "typename", Str name(Exec), cast_tmpl ));
@ -602,6 +604,7 @@ CodeBody gen_ast_inlines()
impl_code_constr, impl_code_constr,
impl_code_class, impl_code_class,
impl_code_define, impl_code_define,
impl_code_define_params,
impl_code_destruct, impl_code_destruct,
impl_code_enum, impl_code_enum,
impl_code_exec, impl_code_exec,
@ -636,6 +639,7 @@ CodeBody gen_ast_inlines()
impl_cast_constr, impl_cast_constr,
impl_cast_class, impl_cast_class,
impl_cast_define, impl_cast_define,
impl_cast_define_params,
impl_cast_destruct, impl_cast_destruct,
impl_cast_enum, impl_cast_enum,
impl_cast_exec, impl_cast_exec,