From a4889edcf141bbdac56b939c5c25ef6b8fe89c48 Mon Sep 17 00:00:00 2001 From: "zik.saleeba" Date: Tue, 2 Jun 2009 07:01:43 +0000 Subject: [PATCH] Fixes in handling void values. Making quicksort test more real. Better error reporting for invalid parameters. git-svn-id: http://picoc.googlecode.com/svn/trunk@318 21eae674-98b7-11dd-bd71-f92a316d2d60 --- expression.c | 16 ++++++++-------- parse.c | 4 +++- tests/25_quicksort.c | 4 ++-- variable.c | 4 +++- 4 files changed, 16 insertions(+), 12 deletions(-) diff --git a/expression.c b/expression.c index d6b3043..4b61108 100644 --- a/expression.c +++ b/expression.c @@ -270,34 +270,34 @@ void ExpressionAssignToPointer(struct ParseState *Parser, struct Value *ToValue, #endif } else - ProgramFail(Parser, "can't assign from a %t to a %t", FromValue->Typ, ToValue->Typ); + ProgramFail(Parser, "can't set a %t from a %t", ToValue->Typ, FromValue->Typ); } /* assign any kind of value */ void ExpressionAssign(struct ParseState *Parser, struct Value *DestValue, struct Value *SourceValue, int Force) { if (!DestValue->IsLValue && !Force) - ProgramFail(Parser, "can't assign to this"); + ProgramFail(Parser, "can't set this"); switch (DestValue->Typ->Base) { case TypeInt: if (!IS_NUMERIC_COERCIBLE(SourceValue)) - ProgramFail(Parser, "can't assign to this"); + ProgramFail(Parser, "can't set a %t from a %t", DestValue->Typ, SourceValue->Typ); DestValue->Val->Integer = COERCE_INTEGER(SourceValue); break; case TypeChar: if (!IS_NUMERIC_COERCIBLE(SourceValue)) - ProgramFail(Parser, "can't assign to this"); + ProgramFail(Parser, "can't set a %t from a %t", DestValue->Typ, SourceValue->Typ); DestValue->Val->Character = COERCE_INTEGER(SourceValue); break; #ifndef NO_FP case TypeFP: if (!IS_NUMERIC_COERCIBLE(SourceValue)) - ProgramFail(Parser, "can't assign to this"); + ProgramFail(Parser, "can't set a %t from a %t", DestValue->Typ, SourceValue->Typ); DestValue->Val->FP = COERCE_FP(SourceValue); break; @@ -308,7 +308,7 @@ void ExpressionAssign(struct ParseState *Parser, struct Value *DestValue, struct case TypeArray: if (DestValue->Typ != SourceValue->Typ) - ProgramFail(Parser, "can't assign from a %t to a %t", SourceValue->Typ, DestValue->Typ); + ProgramFail(Parser, "can't set a %t from a %t", DestValue->Typ, SourceValue->Typ); if (DestValue->Val->Array.Size != SourceValue->Val->Array.Size) ProgramFail(Parser, "can't assign from an array of size %d to one of size %d", SourceValue->Val->Array.Size, DestValue->Val->Array.Size); @@ -319,13 +319,13 @@ void ExpressionAssign(struct ParseState *Parser, struct Value *DestValue, struct case TypeStruct: case TypeUnion: if (DestValue->Typ != SourceValue->Typ) - ProgramFail(Parser, "can't assign from a %t to a %t", SourceValue->Typ, DestValue->Typ); + ProgramFail(Parser, "can't set a %t from a %t", DestValue->Typ, SourceValue->Typ); memcpy((void *)DestValue->Val, (void *)SourceValue->Val, TypeSizeValue(SourceValue)); break; default: - ProgramFail(Parser, "can't assign to a %t", DestValue->Typ); + ProgramFail(Parser, "can't set a %t", DestValue->Typ); break; } } diff --git a/parse.c b/parse.c index e1d56a1..a37f9e8 100644 --- a/parse.c +++ b/parse.c @@ -474,7 +474,9 @@ int ParseStatement(struct ParseState *Parser) if (!ExpressionParse(Parser, &CValue) && TopStackFrame->ReturnValue->Typ->Base != TypeVoid) ProgramFail(Parser, "value required in return"); - ExpressionAssign(Parser, TopStackFrame->ReturnValue, CValue, TRUE); + if (TopStackFrame->ReturnValue->Typ->Base != TypeVoid) + ExpressionAssign(Parser, TopStackFrame->ReturnValue, CValue, TRUE); + VariableStackPop(Parser, CValue); Parser->Mode = RunModeReturn; } diff --git a/tests/25_quicksort.c b/tests/25_quicksort.c index 578aa14..8fa05ba 100644 --- a/tests/25_quicksort.c +++ b/tests/25_quicksort.c @@ -39,7 +39,7 @@ int partition(int left, int right) { if(array[i] < pivotValue) { - swap(array, i, index); + swap(i, index); index += 1; } } @@ -66,7 +66,7 @@ for (i = 0; i < 16; i++) printf("\n"); -//quicksort(0, 15); +quicksort(0, 15); for (i = 0; i < 16; i++) printf("%d ", array[i]); diff --git a/variable.c b/variable.c index f6bc03b..30846ae 100644 --- a/variable.c +++ b/variable.c @@ -216,7 +216,9 @@ void VariableStackPop(struct ParseState *Parser, struct Value *Var) if (Var->ValOnHeap) { - HeapFree(Var->Val); + if (Var->Val != NULL) + HeapFree(Var->Val); + Success = HeapPopStack(Var, sizeof(struct Value)); /* free from heap */ } else if (Var->ValOnStack)