From e780f4322231e2681349a03fd302a501fbc19ea1 Mon Sep 17 00:00:00 2001 From: Joseph Poirier Date: Sun, 14 Jun 2015 17:54:48 -0500 Subject: [PATCH] fix the unary not (logical not) handling for issue #5, add tests --- expression.c | 12 +++++++----- tests/08_while.c | 19 +++++++++++++++++++ tests/08_while.expect | 16 ++++++++++++++++ 3 files changed, 42 insertions(+), 5 deletions(-) diff --git a/expression.c b/expression.c index 851b544..b2052b5 100644 --- a/expression.c +++ b/expression.c @@ -761,23 +761,25 @@ void ExpressionPrefixOperator(struct ParseState *Parser, ProgramFail(Parser, "can't assign to this"); switch (Op) { case TokenIncrement: - TopValue->Val->Pointer = + ResultPtr = TopValue->Val->Pointer = (void*)((char*)TopValue->Val->Pointer+Size); break; case TokenDecrement: - TopValue->Val->Pointer = + ResultPtr = TopValue->Val->Pointer = (void*)((char*)TopValue->Val->Pointer-Size); break; case TokenUnaryNot: - /* conditionally checking a pointer's value */ + /* conditionally checking a pointer's value, we only want + to change the stack value (ResultPtr) and not the pointer's + actual value */ TopValue->Val->Pointer = - (void*)((TopValue->Val->Pointer) ? NULL : (void*)1); + (void*)((char*)TopValue->Val->Pointer); + ResultPtr = TopValue->Val->Pointer ? NULL : (void*)0x01; break; default: ProgramFail(Parser, "invalid operation"); break; } - ResultPtr = TopValue->Val->Pointer; StackValue = ExpressionStackPushValueByType(Parser, StackTop, TopValue->Typ); StackValue->Val->Pointer = ResultPtr; diff --git a/tests/08_while.c b/tests/08_while.c index aee79ba..994309b 100644 --- a/tests/08_while.c +++ b/tests/08_while.c @@ -3,6 +3,7 @@ int a; int p; int t; +int *pp; a = 1; p = 0; @@ -16,4 +17,22 @@ while (a < 100) p = t; } +a = 0; +pp = NULL; +while (!pp) { + printf("%d\n", a); + a += 1; + if (a == 8) + pp = &a; +} + +p = 10; +pp = &p; +while (pp) { + printf("%d\n", p); + p -= 1; + if (p == 2) + pp = NULL; +} + void main() {} diff --git a/tests/08_while.expect b/tests/08_while.expect index 702d4c0..e659c65 100644 --- a/tests/08_while.expect +++ b/tests/08_while.expect @@ -9,3 +9,19 @@ 34 55 89 +0 +1 +2 +3 +4 +5 +6 +7 +10 +9 +8 +7 +6 +5 +4 +3