From a648f2cf27d298fc9ebbc90320217c9d828c83a2 Mon Sep 17 00:00:00 2001 From: "zik.saleeba" Date: Sun, 8 Nov 2009 16:33:31 +0000 Subject: [PATCH] Array initialisers are now implemented - enhancement issue #42 git-svn-id: http://picoc.googlecode.com/svn/trunk@396 21eae674-98b7-11dd-bd71-f92a316d2d60 --- parse.c | 65 ++++++++++++++++++++++++++++++++++++++------ tests/25_quicksort.c | 2 +- tests/Makefile | 3 +- 3 files changed, 59 insertions(+), 11 deletions(-) diff --git a/parse.c b/parse.c index a8332f2..4302d16 100644 --- a/parse.c +++ b/parse.c @@ -109,13 +109,67 @@ struct Value *ParseFunctionDefinition(struct ParseState *Parser, struct ValueTyp return FuncValue; } +/* assign an initial value to a variable */ +void ParseDeclarationAssignment(struct ParseState *Parser, struct Value *NewVariable) +{ + struct Value *CValue; + int ArrayIndex; + enum LexToken Token = TokenComma; + + if (LexGetToken(Parser, NULL, FALSE) == TokenLeftBrace) + { + /* this is an array initialiser */ + LexGetToken(Parser, NULL, TRUE); + + for (ArrayIndex = 0; ArrayIndex < NewVariable->Typ->ArraySize; ArrayIndex++) + { + struct Value *ArrayElement; + + if (Token != TokenComma) + ProgramFail(Parser, "comma expected"); + + if (Parser->Mode == RunModeRun) + ArrayElement = VariableAllocValueFromExistingData(Parser, NewVariable->Typ->FromType, (union AnyValue *)(&NewVariable->Val->ArrayMem[0] + TypeSize(NewVariable->Typ->FromType, 0, TRUE) * ArrayIndex), TRUE, NewVariable); + + if (!ExpressionParse(Parser, &CValue)) + ProgramFail(Parser, "expression expected"); + + if (Parser->Mode == RunModeRun) + { + ExpressionAssign(Parser, ArrayElement, CValue, FALSE, NULL, 0, FALSE); + VariableStackPop(Parser, CValue); + VariableStackPop(Parser, ArrayElement); + } + + Token = LexGetToken(Parser, NULL, TRUE); + } + + if (Token == TokenComma) + Token = LexGetToken(Parser, NULL, TRUE); + + if (Token != TokenRightBrace) + ProgramFail(Parser, "'}' expected"); + } + else + { + /* this is a normal expression initialiser */ + if (!ExpressionParse(Parser, &CValue)) + ProgramFail(Parser, "expression expected"); + + if (Parser->Mode == RunModeRun) + { + ExpressionAssign(Parser, NewVariable, CValue, FALSE, NULL, 0, FALSE); + VariableStackPop(Parser, CValue); + } + } +} + /* declare a variable or function */ int ParseDeclaration(struct ParseState *Parser, enum LexToken Token) { char *Identifier; struct ValueType *BasicType; struct ValueType *Typ; - struct Value *CValue; struct Value *NewVariable; TypeParseFront(Parser, &BasicType); @@ -145,14 +199,7 @@ int ParseDeclaration(struct ParseState *Parser, enum LexToken Token) { /* we're assigning an initial value */ LexGetToken(Parser, NULL, TRUE); - if (!ExpressionParse(Parser, &CValue)) - ProgramFail(Parser, "expression expected"); - - if (Parser->Mode == RunModeRun) - { - ExpressionAssign(Parser, NewVariable, CValue, FALSE, NULL, 0, FALSE); - VariableStackPop(Parser, CValue); - } + ParseDeclarationAssignment(Parser, NewVariable); } } } diff --git a/tests/25_quicksort.c b/tests/25_quicksort.c index c023ade..f54881b 100644 --- a/tests/25_quicksort.c +++ b/tests/25_quicksort.c @@ -63,7 +63,7 @@ void main() array[14] = 74; array[15] = 55; - for (i = 0; i < 16; i++) + for (i = 0; i < 16; i++) printf("%d ", array[i]); printf("\n"); diff --git a/tests/Makefile b/tests/Makefile index 02dfa14..31f42c9 100644 --- a/tests/Makefile +++ b/tests/Makefile @@ -28,7 +28,8 @@ TESTS= 00_assignment.test \ 28_strings.test \ 29_array_address.test \ 34_array_assignment.test \ - 35_sizeof.test + 35_sizeof.test \ + 36_array_initialisers.test %.test: %.expect %.c @echo Test: $*...