Skip to content

Commit

Permalink
impl macro function arguments
Browse files Browse the repository at this point in the history
  • Loading branch information
omdxp committed Sep 4, 2024
1 parent f541d7d commit a51bf40
Show file tree
Hide file tree
Showing 2 changed files with 68 additions and 4 deletions.
68 changes: 67 additions & 1 deletion preprocessor/preprocessor.c
Original file line number Diff line number Diff line change
Expand Up @@ -225,10 +225,31 @@ struct preprocessor *preprocessor_create(struct compile_process *compiler) {
return preprocessor;
}

struct token *preprocessor_prev_token(struct compile_process *compiler) {
return vector_peek_at(compiler->token_vec_original,
compiler->token_vec_original->pindex - 1);
}

struct token *preprocessor_next_token(struct compile_process *compiler) {
return vector_peek(compiler->token_vec_original);
}

struct token *
preprocessor_next_token_no_increment(struct compile_process *compiler) {
return vector_peek_no_increment(compiler->token_vec_original);
}

struct token *
preprocessor_peek_next_token_skip_nl(struct compile_process *compiler) {
struct token *token = preprocessor_next_token_no_increment(compiler);
while (token && token->type == TOKEN_TYPE_NEWLINE) {
token = preprocessor_next_token(compiler);
}

token = preprocessor_next_token_no_increment(compiler);
return token;
}

void *preprocessor_handle_number_token(struct expressionable *expressionable) {
struct token *token = expressionable_token_next(expressionable);
return preprocessor_node_create(&(struct preprocessor_node){
Expand Down Expand Up @@ -507,10 +528,55 @@ preprocessor_definition_create(const char *name, struct vector *value,
return def;
}

bool preprocessor_is_next_macro_arguments(struct compile_process *compiler) {
bool res = false;
vector_save(compiler->token_vec_original);
struct token *last_token = preprocessor_prev_token(compiler);
struct token *cur_token = preprocessor_next_token(compiler);
if (token_is_operator(cur_token, "(") &&
(!last_token || !last_token->whitespace)) {
res = true;
}

vector_restore(compiler->token_vec_original);
return res;
}

void preprocessor_parse_macro_argument_declaration(
struct compile_process *compiler, struct vector *args) {
if (token_is_operator(preprocessor_next_token_no_increment(compiler), "(")) {
// skip (
preprocessor_next_token(compiler);
struct token *next_token = preprocessor_next_token(compiler);
while (!token_is_symbol(next_token, ')')) {
if (next_token->type != TOKEN_TYPE_IDENTIFIER) {
compiler_error(compiler, "expected identifier");
}

vector_push(args, (void *)next_token->sval);
next_token = preprocessor_next_token(compiler);
if (!token_is_operator(next_token, ",") &&
!token_is_symbol(next_token, ')')) {
compiler_error(compiler, "expected , or )");
}

if (token_is_symbol(next_token, ')')) {
break;
}

// skip comma
next_token = preprocessor_next_token(compiler);
}
}
}

void preprocessor_handle_definition_token(struct compile_process *compiler) {
struct token *name_token = preprocessor_next_token(compiler);
struct vector *args = vector_create(sizeof(const char *));
#warning "handle macro function arguments"

if (preprocessor_is_next_macro_arguments(compiler)) {
preprocessor_parse_macro_argument_declaration(compiler, args);
}

struct vector *value_token_vec = vector_create(sizeof(struct token));
preprocessor_multi_value_insert_to_vector(compiler, value_token_vec);
Expand Down
4 changes: 1 addition & 3 deletions test.c
Original file line number Diff line number Diff line change
@@ -1,3 +1 @@
#define ABC 50

int main() { return 0; }
#define ABC(x, y)

0 comments on commit a51bf40

Please sign in to comment.