Skip to content

Commit

Permalink
evaluate expressions in preprocessor
Browse files Browse the repository at this point in the history
  • Loading branch information
omdxp committed Sep 4, 2024
1 parent 8c7e344 commit 74d94bd
Show file tree
Hide file tree
Showing 4 changed files with 93 additions and 3 deletions.
2 changes: 2 additions & 0 deletions compiler.h
Original file line number Diff line number Diff line change
Expand Up @@ -1164,6 +1164,8 @@ bool is_parentheses(const char *op);
bool is_left_operanded_unary_operator(const char *op);
bool unary_operand_compatible(struct token *token);
void datatype_decrement_pointer(struct datatype *dtype);
long arithmetic(struct compile_process *compiler, long left, long right,
const char *op, bool *success);

size_t datatype_size_for_array_access(struct datatype *dtype);
size_t datatype_element_size(struct datatype *dtype);
Expand Down
57 changes: 57 additions & 0 deletions helper.c
Original file line number Diff line number Diff line change
Expand Up @@ -250,3 +250,60 @@ bool unary_operand_compatible(struct token *token) {
bool is_left_operanded_unary_operator(const char *op) {
return S_EQ(op, "++") || S_EQ(op, "--");
}

long arithmetic(struct compile_process *compiler, long left, long right,
const char *op, bool *success) {
*success = true;
int res = 0;
if (S_EQ(op, "+")) {
res = left + right;
} else if (S_EQ(op, "-")) {
res = left - right;
} else if (S_EQ(op, "*")) {
res = left * right;
} else if (S_EQ(op, "/")) {
if (right == 0) {
*success = false;
return 0;
}

res = left / right;
} else if (S_EQ(op, "%")) {
if (right == 0) {
*success = false;
return 0;
}

res = left % right;
} else if (S_EQ(op, "<<")) {
res = left << right;
} else if (S_EQ(op, ">>")) {
res = left >> right;
} else if (S_EQ(op, "&")) {
res = left & right;
} else if (S_EQ(op, "|")) {
res = left | right;
} else if (S_EQ(op, "^")) {
res = left ^ right;
} else if (S_EQ(op, "&&")) {
res = left && right;
} else if (S_EQ(op, "||")) {
res = left || right;
} else if (S_EQ(op, "==")) {
res = left == right;
} else if (S_EQ(op, "!=")) {
res = left != right;
} else if (S_EQ(op, "<")) {
res = left < right;
} else if (S_EQ(op, ">")) {
res = left > right;
} else if (S_EQ(op, "<=")) {
res = left <= right;
} else if (S_EQ(op, ">=")) {
res = left >= right;
} else {
*success = false;
}

return res;
}
31 changes: 31 additions & 0 deletions preprocessor/preprocessor.c
Original file line number Diff line number Diff line change
Expand Up @@ -851,6 +851,33 @@ int preprocessor_evaluate_identifier(struct compile_process *compiler,
return preprocessor_definition_evaluated_value(def, NULL);
}

int preprocessor_arithmetic(struct compile_process *compiler, long left,
long right, const char *op) {
bool success = false;
long res = arithmetic(compiler, left, right, op, &success);
if (!success) {
compiler_error(compiler, "arithmetic error");
}

return res;
}

int preprocessor_evaluate_exp(struct compile_process *compiler,
struct preprocessor_node *node) {
// if (preprocessor_exp_is_macro_function_call(node)) {
#warning "handle macro function call"
// }

long left_operand = preprocessor_evaluate(compiler, node->exp.left);
if (node->exp.right->type == PREPROCESSOR_TENARY_NODE) {
#warning "handle ternary"
}

long right_operand = preprocessor_evaluate(compiler, node->exp.right);
return preprocessor_arithmetic(compiler, left_operand, right_operand,
node->exp.op);
}

int preprocessor_evaluate(struct compile_process *compiler,
struct preprocessor_node *root_node) {
struct preprocessor_node *current = root_node;
Expand All @@ -863,6 +890,10 @@ int preprocessor_evaluate(struct compile_process *compiler,
case PREPROCESSOR_IDENTIFIER_NODE:
res = preprocessor_evaluate_identifier(compiler, current);
break;

case PREPROCESSOR_EXPRESSION_NODE:
res = preprocessor_evaluate_exp(compiler, current);
break;
}

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

0 comments on commit 74d94bd

Please sign in to comment.