From a784b77f75cb2deea152f1e60fdb90d1eda72ab2 Mon Sep 17 00:00:00 2001 From: "zik.saleeba" Date: Sat, 16 Mar 2013 06:09:32 +0000 Subject: [PATCH] Fix for macro bug sumbitted by broscutamaker git-svn-id: http://picoc.googlecode.com/svn/trunk@597 21eae674-98b7-11dd-bd71-f92a316d2d60 --- expression.c | 15 ++++++++++----- tests/57_macro_bug.c | 17 +++++++++++++++++ tests/57_macro_bug.expect | 1 + tests/Makefile | 1 + 4 files changed, 29 insertions(+), 5 deletions(-) create mode 100644 tests/57_macro_bug.c create mode 100644 tests/57_macro_bug.expect diff --git a/expression.c b/expression.c index 997ea35..d5b7835 100644 --- a/expression.c +++ b/expression.c @@ -8,6 +8,11 @@ #define BRACKET_PRECEDENCE 20 #define IS_TYPE_TOKEN(t) ((t) >= TokenIntType && (t) <= TokenUnsignedType) +/* If the destination is not float, we can't assign a floating value to it, we need to convert it to integer instead */ +#define ASSIGN_FP_OR_INT(value) \ + if (IS_FP(BottomValue)) { ResultFP = ExpressionAssignFP(Parser, BottomValue, value); } \ + else { ResultInt = ExpressionAssignInt(Parser, BottomValue, (long)(value), FALSE); ResultIsInt = TRUE; } \ + #define DEEP_PRECEDENCE (BRACKET_PRECEDENCE*1000) #ifdef DEBUG_EXPRESSIONS @@ -655,11 +660,11 @@ void ExpressionInfixOperator(struct ParseState *Parser, struct ExpressionStack * switch (Op) { - case TokenAssign: ResultFP = ExpressionAssignFP(Parser, BottomValue, TopFP); break; - case TokenAddAssign: ResultFP = ExpressionAssignFP(Parser, BottomValue, BottomFP + TopFP); break; - case TokenSubtractAssign: ResultFP = ExpressionAssignFP(Parser, BottomValue, BottomFP - TopFP); break; - case TokenMultiplyAssign: ResultFP = ExpressionAssignFP(Parser, BottomValue, BottomFP * TopFP); break; - case TokenDivideAssign: ResultFP = ExpressionAssignFP(Parser, BottomValue, BottomFP / TopFP); break; + case TokenAssign: ASSIGN_FP_OR_INT(TopFP); break; + case TokenAddAssign: ASSIGN_FP_OR_INT(BottomFP + TopFP); break; + case TokenSubtractAssign: ASSIGN_FP_OR_INT(BottomFP - TopFP); break; + case TokenMultiplyAssign: ASSIGN_FP_OR_INT(BottomFP * TopFP); break; + case TokenDivideAssign: ASSIGN_FP_OR_INT(BottomFP / TopFP); break; case TokenEqual: ResultInt = BottomFP == TopFP; ResultIsInt = TRUE; break; case TokenNotEqual: ResultInt = BottomFP != TopFP; ResultIsInt = TRUE; break; case TokenLessThan: ResultInt = BottomFP < TopFP; ResultIsInt = TRUE; break; diff --git a/tests/57_macro_bug.c b/tests/57_macro_bug.c new file mode 100644 index 0000000..68fb471 --- /dev/null +++ b/tests/57_macro_bug.c @@ -0,0 +1,17 @@ +#include "stdio.h" + +#define MIN(a,b) ((a) < (b) ? (a) : (b)) + +void main() +{ + float x = MIN(1,2); + int y = 14; + float z; + z = MIN(y, 13.5); + y = MIN(y, 13); + + float pi = 3.14; + int pi_int = pi; + + printf("Macro test: %d %d %f %d \n", x, y, z, pi_int); +} diff --git a/tests/57_macro_bug.expect b/tests/57_macro_bug.expect new file mode 100644 index 0000000..a29f45d --- /dev/null +++ b/tests/57_macro_bug.expect @@ -0,0 +1 @@ +Macro test: 1 13 13.500000 3 diff --git a/tests/Makefile b/tests/Makefile index a67b318..246fdd4 100644 --- a/tests/Makefile +++ b/tests/Makefile @@ -51,6 +51,7 @@ TESTS= 00_assignment.test \ 54_goto.test \ 55_array_initialiser.test \ 56_cross_structure.test \ + 57_macro_bug.test \ 59_break_before_loop.test %.test: %.expect %.c