Skip to content

Commit

Permalink
Fix for macro bug sumbitted by broscutamaker
Browse files Browse the repository at this point in the history
  • Loading branch information
zik.saleeba committed Mar 16, 2013
1 parent 6cb57ee commit 64373d9
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 5 deletions.
15 changes: 10 additions & 5 deletions expression.c
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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;
Expand Down
17 changes: 17 additions & 0 deletions tests/57_macro_bug.c
Original file line number Diff line number Diff line change
@@ -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);
}
1 change: 1 addition & 0 deletions tests/57_macro_bug.expect
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Macro test: 1 13 13.500000 3
1 change: 1 addition & 0 deletions tests/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 64373d9

Please sign in to comment.