Array initialisers are now implemented - enhancement issue #42
git-svn-id: http://picoc.googlecode.com/svn/trunk@396 21eae674-98b7-11dd-bd71-f92a316d2d60
This commit is contained in:
parent
d5e7a536f1
commit
a648f2cf27
65
parse.c
65
parse.c
|
@ -109,13 +109,67 @@ struct Value *ParseFunctionDefinition(struct ParseState *Parser, struct ValueTyp
|
||||||
return FuncValue;
|
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 */
|
/* declare a variable or function */
|
||||||
int ParseDeclaration(struct ParseState *Parser, enum LexToken Token)
|
int ParseDeclaration(struct ParseState *Parser, enum LexToken Token)
|
||||||
{
|
{
|
||||||
char *Identifier;
|
char *Identifier;
|
||||||
struct ValueType *BasicType;
|
struct ValueType *BasicType;
|
||||||
struct ValueType *Typ;
|
struct ValueType *Typ;
|
||||||
struct Value *CValue;
|
|
||||||
struct Value *NewVariable;
|
struct Value *NewVariable;
|
||||||
|
|
||||||
TypeParseFront(Parser, &BasicType);
|
TypeParseFront(Parser, &BasicType);
|
||||||
|
@ -145,14 +199,7 @@ int ParseDeclaration(struct ParseState *Parser, enum LexToken Token)
|
||||||
{
|
{
|
||||||
/* we're assigning an initial value */
|
/* we're assigning an initial value */
|
||||||
LexGetToken(Parser, NULL, TRUE);
|
LexGetToken(Parser, NULL, TRUE);
|
||||||
if (!ExpressionParse(Parser, &CValue))
|
ParseDeclarationAssignment(Parser, NewVariable);
|
||||||
ProgramFail(Parser, "expression expected");
|
|
||||||
|
|
||||||
if (Parser->Mode == RunModeRun)
|
|
||||||
{
|
|
||||||
ExpressionAssign(Parser, NewVariable, CValue, FALSE, NULL, 0, FALSE);
|
|
||||||
VariableStackPop(Parser, CValue);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -63,7 +63,7 @@ void main()
|
||||||
array[14] = 74;
|
array[14] = 74;
|
||||||
array[15] = 55;
|
array[15] = 55;
|
||||||
|
|
||||||
for (i = 0; i < 16; i++)
|
for (i = 0; i < 16; i++)
|
||||||
printf("%d ", array[i]);
|
printf("%d ", array[i]);
|
||||||
|
|
||||||
printf("\n");
|
printf("\n");
|
||||||
|
|
|
@ -28,7 +28,8 @@ TESTS= 00_assignment.test \
|
||||||
28_strings.test \
|
28_strings.test \
|
||||||
29_array_address.test \
|
29_array_address.test \
|
||||||
34_array_assignment.test \
|
34_array_assignment.test \
|
||||||
35_sizeof.test
|
35_sizeof.test \
|
||||||
|
36_array_initialisers.test
|
||||||
|
|
||||||
%.test: %.expect %.c
|
%.test: %.expect %.c
|
||||||
@echo Test: $*...
|
@echo Test: $*...
|
||||||
|
|
Loading…
Reference in a new issue