Skip to content

Commit

Permalink
Implementing macro functions - Part 2
Browse files Browse the repository at this point in the history
  • Loading branch information
fedi-nabli committed Apr 2, 2024
1 parent d2336ea commit 29e376d
Show file tree
Hide file tree
Showing 2 changed files with 90 additions and 4 deletions.
89 changes: 87 additions & 2 deletions preprocessor/preprocessor.c
Original file line number Diff line number Diff line change
Expand Up @@ -672,6 +672,13 @@ struct preprocessor_function_arguments* preprocessor_function_arguments_create()
return args;
}

void preprocessor_function_argument_push(struct preprocessor_function_arguments* arguments, struct vector* value_vec)
{
struct preprocessor_function_argument arg;
arg.tokens = vector_clone(value_vec);
vector_push(arguments->arguments, &arg);
}

void preprocessor_number_push_to_function_arguments(struct preprocessor_function_arguments* arguments, int64_t number)
{
struct token token;
Expand Down Expand Up @@ -1280,6 +1287,82 @@ void preprocessor_handle_symbol(struct compile_process* compiler, struct token*
}
}

struct token* preprocessor_handle_identifier_macro_call_argument_parse_parentheses(struct compile_process* compiler, struct vector* src_vec, struct vector* value_vec, struct preprocessor_function_arguments* arguments, struct token* left_bracket_token)
{
// Push the left bracket token to the stack
vector_push(value_vec, left_bracket_token);

struct token* next_token = vector_peek(src_vec);
while (next_token && !token_is_symbol(next_token, ')'))
{
if (token_is_operator(next_token, "("))
{
next_token = preprocessor_handle_identifier_macro_call_argument_parse_parentheses(compiler, src_vec, value_vec, arguments, next_token);
}

vector_push(value_vec, next_token);
next_token = vector_peek(src_vec);
}

if (!next_token)
{
compiler_error(compiler, "You did not end your parentheses, expecting a ')'");
}

vector_push(value_vec, next_token);
return vector_peek(src_vec);
}

void preprocessor_handle_identifier_macro_call_argument(struct preprocessor_function_arguments* arguments, struct vector* token_vec)
{
preprocessor_function_argument_push(arguments, token_vec);
}

struct token* preprocessor_handle_identifier_macro_call_argument_parse(struct compile_process* compiler, struct vector* src_vec, struct vector* value_vec, struct preprocessor_function_arguments* arguments, struct token* token)
{
if (token_is_operator(token, "("))
{
return preprocessor_handle_identifier_macro_call_argument_parse_parentheses(compiler, src_vec, value_vec, arguments, token);
}

if (token_is_symbol(token, ')'))
{
// We are done handling the call argument
preprocessor_handle_identifier_macro_call_argument(arguments, value_vec);
return NULL;
}

if (token_is_operator(token, ","))
{
preprocessor_handle_identifier_macro_call_argument(arguments, value_vec);
// Ckear the value vectir ready for next argument
vector_clear(value_vec);
token = vector_peek(src_vec);
return token;
}

vector_push(value_vec, token);
token = vector_peek(src_vec);
return token;
}

struct preprocessor_function_arguments* preprocessor_handle_identifier_macro_call_arguments(struct compile_process* compiler, struct vector* src_vec)
{
// Skip the left bracket
vector_peek(src_vec);

struct preprocessor_function_arguments* arguments = preprocessor_function_arguments_create();
struct vector* value_vec = vector_create(sizeof(struct token));
struct token* token = vector_peek(src_vec);
while (token)
{
token = preprocessor_handle_identifier_macro_call_argument_parse(compiler, src_vec, value_vec, arguments, token);
}

vector_free(value_vec);
return arguments;
}

int preprocessor_handle_identifier_for_token_vector(struct compile_process* compiler, struct vector* src_vec, struct vector* dst_vec, struct token* token)
{
struct preprocessor_definition* definition = preprocessor_get_definition(compiler->preprocessor, token->sval);
Expand All @@ -1298,8 +1381,10 @@ int preprocessor_handle_identifier_for_token_vector(struct compile_process* comp

if (token_is_operator(vector_peek_no_increment(src_vec), "("))
{
#warning "Finish macro functions first"
// struct preprocessor_function_arguments* arguments = preprocessor_handle_identifier_macro_call_arguments(compiler, src_vec);
struct preprocessor_function_arguments* arguments = preprocessor_handle_identifier_macro_call_arguments(compiler, src_vec);
const char* funciton_name = token->sval;
preprocessor_macro_function_execute(compiler, funciton_name, arguments, 0);
return 0;
}

struct vector* definition_val = preprocessor_definition_value(definition);
Expand Down
5 changes: 3 additions & 2 deletions test.c
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
#define ABC 50
int x = ABC;
#define SUM(x, y) x+y

int a = SUM(5, 5);

0 comments on commit 29e376d

Please sign in to comment.