From 9769bd432b2aef20903cc3c815f8e3cfa0309163 Mon Sep 17 00:00:00 2001 From: "zik.saleeba" Date: Tue, 10 Mar 2009 02:18:17 +0000 Subject: [PATCH] Fixed problem with 'return' parsing which was causing the factorial example to fail git-svn-id: http://picoc.googlecode.com/svn/trunk@185 21eae674-98b7-11dd-bd71-f92a316d2d60 --- parse.c | 3 +++ tests/15_recursion.c | 16 +++++++++------- tests/15_recursion.expect | 8 ++++++++ tests/Makefile | 1 + 4 files changed, 21 insertions(+), 7 deletions(-) diff --git a/parse.c b/parse.c index 65f002e..88097be 100644 --- a/parse.c +++ b/parse.c @@ -269,6 +269,7 @@ int ParseStatement(struct ParseState *Parser) case TokenLeftBrace: ParseBlock(Parser, FALSE, TRUE); + CheckTrailingSemicolon = FALSE; break; case TokenIf: @@ -451,6 +452,8 @@ int ParseStatement(struct ParseState *Parser) memcpy(TopStackFrame->ReturnValue->Val, CValue->Val, TypeSizeValue(CValue)); Parser->Mode = RunModeReturn; } + else + ExpressionParse(Parser, &CValue); break; default: diff --git a/tests/15_recursion.c b/tests/15_recursion.c index 03a7845..c7d7afb 100644 --- a/tests/15_recursion.c +++ b/tests/15_recursion.c @@ -1,10 +1,12 @@ -int fact(int i) { - if (i < 2) { - return i; - } - return (i * fact(i - 1)); +int factorial(int i) +{ + if (i < 2) + return i; + else + return (i * factorial(i - 1)); } -fact(5) -fact(6) +int Count; +for (Count = 1; Count <= 10; Count++) + printf("%d\n", factorial(Count)); diff --git a/tests/15_recursion.expect b/tests/15_recursion.expect index 7401823..db47b28 100644 --- a/tests/15_recursion.expect +++ b/tests/15_recursion.expect @@ -1,2 +1,10 @@ +1 +2 +6 +24 120 720 +5040 +40320 +362880 +3628800 diff --git a/tests/Makefile b/tests/Makefile index f73b176..3d70135 100644 --- a/tests/Makefile +++ b/tests/Makefile @@ -12,6 +12,7 @@ TESTS= 00_assignment.test \ 12_hashdefine.test \ 13_integer_literals.test \ 14_if.test \ + 15_recursion.test \ 16_nesting.test \ 17_enum.test \ 18_include.test