Now allowing multiple declarations in one statement

git-svn-id: http://picoc.googlecode.com/svn/trunk@157 21eae674-98b7-11dd-bd71-f92a316d2d60
This commit is contained in:
zik.saleeba 2009-03-06 06:00:51 +00:00
parent 8b410241ee
commit 3bdbb51d5c
5 changed files with 81 additions and 40 deletions

12
parse.c
View file

@ -677,10 +677,14 @@ struct Value *ParseFunctionDefinition(struct ParseState *Parser, struct ValueTyp
void ParseDeclaration(struct ParseState *Parser, enum LexToken Token) void ParseDeclaration(struct ParseState *Parser, enum LexToken Token)
{ {
char *Identifier; char *Identifier;
struct ValueType *BasicType;
struct ValueType *Typ; struct ValueType *Typ;
struct Value *CValue; struct Value *CValue;
TypeParse(Parser, &Typ, &Identifier); TypeParseFront(Parser, &BasicType);
do
{
TypeParseIdentPart(Parser, BasicType, &Typ, &Identifier);
if (Token == TokenVoidType && Identifier != StrEmpty) if (Token == TokenVoidType && Identifier != StrEmpty)
ProgramFail(Parser, "can't define a void variable"); ProgramFail(Parser, "can't define a void variable");
@ -708,6 +712,12 @@ void ParseDeclaration(struct ParseState *Parser, enum LexToken Token)
} }
} }
} }
Token = LexGetToken(Parser, NULL, FALSE);
if (Token == TokenComma)
LexGetToken(Parser, NULL, TRUE);
} while (Token == TokenComma);
} }
/* parse a #define macro definition and store it for later */ /* parse a #define macro definition and store it for later */

View file

@ -270,6 +270,8 @@ void TypeInit();
int TypeSize(struct ValueType *Typ, int ArraySize); int TypeSize(struct ValueType *Typ, int ArraySize);
int TypeSizeValue(struct Value *Val); int TypeSizeValue(struct Value *Val);
int TypeStackSizeValue(struct Value *Val); int TypeStackSizeValue(struct Value *Val);
int TypeParseFront(struct ParseState *Parser, struct ValueType **Typ);
void TypeParseIdentPart(struct ParseState *Parser, struct ValueType *BasicTyp, struct ValueType **Typ, char **Identifier);
void TypeParse(struct ParseState *Parser, struct ValueType **Typ, char **Identifier); void TypeParse(struct ParseState *Parser, struct ValueType **Typ, char **Identifier);
struct ValueType *TypeGetMatching(struct ParseState *Parser, struct ValueType *ParentType, enum BaseType Base, int ArraySize, const char *Identifier); struct ValueType *TypeGetMatching(struct ParseState *Parser, struct ValueType *ParentType, enum BaseType Base, int ArraySize, const char *Identifier);

View file

@ -5,3 +5,6 @@ printf("%d\n", a);
int b = 64; int b = 64;
printf("%d\n", b); printf("%d\n", b);
int c = 12, d = 34;
printf("%d, %d\n", c, d);

View file

@ -1,2 +1,3 @@
42 42
64 64
12, 34

47
type.c
View file

@ -175,20 +175,13 @@ void TypeParseStruct(struct ParseState *Parser, struct ValueType **Typ, int IsSt
LexGetToken(Parser, NULL, TRUE); LexGetToken(Parser, NULL, TRUE);
} }
/* parse a type */ /* parse a type - just the basic type */
void TypeParse(struct ParseState *Parser, struct ValueType **Typ, char **Identifier) int TypeParseFront(struct ParseState *Parser, struct ValueType **Typ)
{ {
struct ParseState Before; struct ParseState Before = *Parser;
enum LexToken Token; enum LexToken Token = LexGetToken(Parser, NULL, TRUE);
struct Value *LexValue;
int Done = FALSE;
*Typ = NULL; *Typ = NULL;
*Identifier = StrEmpty;
while (!Done)
{
Before = *Parser;
Token = LexGetToken(Parser, &LexValue, TRUE);
switch (Token) switch (Token)
{ {
case TokenIntType: case TokenLongType: case TokenShortType: *Typ = &IntType; break; case TokenIntType: case TokenLongType: case TokenShortType: *Typ = &IntType; break;
@ -205,6 +198,28 @@ void TypeParse(struct ParseState *Parser, struct ValueType **Typ, char **Identif
TypeParseStruct(Parser, Typ, Token == TokenStructType); TypeParseStruct(Parser, Typ, Token == TokenStructType);
break; break;
default: *Parser = Before; return FALSE;
}
return TRUE;
}
/* parse a type - the part which is repeated with each identifier in a declaration list */
void TypeParseIdentPart(struct ParseState *Parser, struct ValueType *BasicTyp, struct ValueType **Typ, char **Identifier)
{
struct ParseState Before;
enum LexToken Token;
struct Value *LexValue;
int Done = FALSE;
*Typ = BasicTyp;
*Identifier = StrEmpty;
while (!Done)
{
Before = *Parser;
Token = LexGetToken(Parser, &LexValue, TRUE);
switch (Token)
{
case TokenOpenBracket: case TokenOpenBracket:
if (*Typ != NULL) if (*Typ != NULL)
ProgramFail(Parser, "bad type declaration"); ProgramFail(Parser, "bad type declaration");
@ -266,3 +281,13 @@ void TypeParse(struct ParseState *Parser, struct ValueType **Typ, char **Identif
} }
} }
} }
/* parse a type - a complete declaration including identifier */
void TypeParse(struct ParseState *Parser, struct ValueType **Typ, char **Identifier)
{
struct ValueType *BasicType;
TypeParseFront(Parser, &BasicType);
TypeParseIdentPart(Parser, BasicType, Typ, Identifier);
}