Skip to content

Commit

Permalink
impl if macro with definition values
Browse files Browse the repository at this point in the history
  • Loading branch information
omdxp committed Sep 4, 2024
1 parent 542f76e commit 8c7e344
Show file tree
Hide file tree
Showing 2 changed files with 98 additions and 3 deletions.
96 changes: 95 additions & 1 deletion preprocessor/preprocessor.c
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,10 @@ void preprocessor_exec_warning(struct compile_process *compiler,

void preprocessor_handle_token(struct compile_process *compiler,
struct token *token);
int preprocessor_evaluate(struct compile_process *compiler,
struct preprocessor_node *root_node);
int preprocessor_parse_evaluate(struct compile_process *compiler,
struct vector *token_vec);

struct preprocessor_included_file *
preprocessor_add_included_file(struct preprocessor *preprocessor,
Expand Down Expand Up @@ -602,7 +606,7 @@ preprocessor_definition_create(const char *name, struct vector *value,
return def;
}

struct preprocessor_definiton *
struct preprocessor_definition *
preprocessor_get_definition(struct preprocessor *preprocessor,
const char *name) {
vector_set_peek_pointer(preprocessor->defs, 0);
Expand All @@ -618,6 +622,65 @@ preprocessor_get_definition(struct preprocessor *preprocessor,
return NULL;
}

struct vector *preprocessor_definition_value_for_standard(
struct preprocessor_definition *def) {
return def->standard.value;
}

struct vector *preprocessor_definition_value_with_arguments(
struct preprocessor_definition *def,
struct preprocessor_function_args *args) {
if (def->type == PREPROCESSOR_DEFINITION_NATIVE_CALLBACK) {
#warning "impl definition value for native"
return NULL;
} else if (def->type == PREPROCESSOR_DEFINITION_TYPEDEF) {
#warning "impl definition value for typdef"
return NULL;
}

return preprocessor_definition_value_for_standard(def);
}

struct vector *
preprocessor_definition_value(struct preprocessor_definition *def) {
return preprocessor_definition_value_with_arguments(def, NULL);
}

int preprocessor_parse_evaluate_token(struct compile_process *compiler,
struct token *token) {
struct vector *token_vec = vector_create(sizeof(struct token));
vector_push(token_vec, token);
return preprocessor_parse_evaluate(compiler, token_vec);
}

int preprocessor_definition_evaluated_value_for_standard(
struct preprocessor_definition *def) {
struct token *token = vector_back(def->standard.value);
if (token->type == TOKEN_TYPE_IDENTIFIER) {
return preprocessor_parse_evaluate_token(def->preprocessor->compiler,
token);
}

if (token->type != TOKEN_TYPE_NUMBER) {
compiler_error(def->preprocessor->compiler,
"Definition must hold number value");
}

return token->llnum;
}

int preprocessor_definition_evaluated_value(
struct preprocessor_definition *def,
struct preprocessor_function_args *args) {
if (def->type == PREPROCESSOR_DEFINITION_STANDARD) {
return preprocessor_definition_evaluated_value_for_standard(def);
} else if (def->type == PREPROCESSOR_DEFINITION_NATIVE_CALLBACK) {
#warning "impl native callback"
}

compiler_error(def->preprocessor->compiler, "Cannot evaluate to number");
}

bool preprocessor_is_next_macro_arguments(struct compile_process *compiler) {
bool res = false;
vector_save(compiler->token_vec_original);
Expand Down Expand Up @@ -761,6 +824,33 @@ int preprocessor_evaluate_number(struct preprocessor_node *node) {
return node->const_val.llnum;
}

int preprocessor_evaluate_identifier(struct compile_process *compiler,
struct preprocessor_node *node) {
struct preprocessor *preprocessor = compiler->preprocessor;
struct preprocessor_definition *def =
preprocessor_get_definition(preprocessor, node->sval);
if (!def) {
return true;
}

if (vector_count(preprocessor_definition_value(def)) > 1) {
struct vector *node_vec = vector_create(sizeof(struct preprocessor_node *));
struct expressionable *expressionable = expressionable_create(
&preprocessor_expressionable_config, preprocessor_definition_value(def),
node_vec, EXPRESSIONABLE_FLAG_IS_PREPROCESSER_EXPRESSION);
expressionable_parse(expressionable);
struct preprocessor_node *node = expressionable_node_pop(expressionable);
int val = preprocessor_evaluate(compiler, node);
return val;
}

if (vector_count(preprocessor_definition_value(def)) == 0) {
return false;
}

return preprocessor_definition_evaluated_value(def, NULL);
}

int preprocessor_evaluate(struct compile_process *compiler,
struct preprocessor_node *root_node) {
struct preprocessor_node *current = root_node;
Expand All @@ -769,6 +859,10 @@ int preprocessor_evaluate(struct compile_process *compiler,
case PREPROCESSOR_NUMBER_NODE:
res = preprocessor_evaluate_number(current);
break;

case PREPROCESSOR_IDENTIFIER_NODE:
res = preprocessor_evaluate_identifier(compiler, current);
break;
}

return res;
Expand Down
5 changes: 3 additions & 2 deletions test.c
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
#if 0
#error testing
#define ABC 0
#if ABC
#error "this is an error"
#endif

0 comments on commit 8c7e344

Please sign in to comment.