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:
parent
8b410241ee
commit
3bdbb51d5c
56
parse.c
56
parse.c
|
@ -677,37 +677,47 @@ 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);
|
||||||
if (Token == TokenVoidType && Identifier != StrEmpty)
|
do
|
||||||
ProgramFail(Parser, "can't define a void variable");
|
|
||||||
|
|
||||||
if ((Token != TokenVoidType && Token != TokenStructType && Token != TokenUnionType) && Identifier == StrEmpty)
|
|
||||||
ProgramFail(Parser, "identifier expected");
|
|
||||||
|
|
||||||
if (Identifier != StrEmpty)
|
|
||||||
{
|
{
|
||||||
/* handle function definitions */
|
TypeParseIdentPart(Parser, BasicType, &Typ, &Identifier);
|
||||||
if (LexGetToken(Parser, NULL, FALSE) == TokenOpenBracket)
|
if (Token == TokenVoidType && Identifier != StrEmpty)
|
||||||
ParseFunctionDefinition(Parser, Typ, Identifier, FALSE);
|
ProgramFail(Parser, "can't define a void variable");
|
||||||
else
|
|
||||||
|
if ((Token != TokenVoidType && Token != TokenStructType && Token != TokenUnionType) && Identifier == StrEmpty)
|
||||||
|
ProgramFail(Parser, "identifier expected");
|
||||||
|
|
||||||
|
if (Identifier != StrEmpty)
|
||||||
{
|
{
|
||||||
if (LexGetToken(Parser, NULL, FALSE) != TokenAssign)
|
/* handle function definitions */
|
||||||
VariableDefine(Parser, Identifier, VariableAllocValueFromType(Parser, Typ, TRUE, NULL));
|
if (LexGetToken(Parser, NULL, FALSE) == TokenOpenBracket)
|
||||||
|
ParseFunctionDefinition(Parser, Typ, Identifier, FALSE);
|
||||||
else
|
else
|
||||||
{ /* we're assigning an initial value */
|
{
|
||||||
LexGetToken(Parser, NULL, TRUE);
|
if (LexGetToken(Parser, NULL, FALSE) != TokenAssign)
|
||||||
if (!ParseExpression(Parser, &CValue))
|
VariableDefine(Parser, Identifier, VariableAllocValueFromType(Parser, Typ, TRUE, NULL));
|
||||||
ProgramFail(Parser, "expression expected");
|
else
|
||||||
|
{ /* we're assigning an initial value */
|
||||||
VariableDefine(Parser, Identifier, CValue);
|
LexGetToken(Parser, NULL, TRUE);
|
||||||
if (Parser->Mode == RunModeRun)
|
if (!ParseExpression(Parser, &CValue))
|
||||||
VariableStackPop(Parser, CValue);
|
ProgramFail(Parser, "expression expected");
|
||||||
|
|
||||||
|
VariableDefine(Parser, Identifier, CValue);
|
||||||
|
if (Parser->Mode == RunModeRun)
|
||||||
|
VariableStackPop(Parser, CValue);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
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 */
|
||||||
|
|
2
picoc.h
2
picoc.h
|
@ -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);
|
||||||
|
|
||||||
|
|
|
@ -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);
|
||||||
|
|
||||||
|
|
|
@ -1,2 +1,3 @@
|
||||||
42
|
42
|
||||||
64
|
64
|
||||||
|
12, 34
|
||||||
|
|
59
type.c
59
type.c
|
@ -175,14 +175,43 @@ 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 = *Parser;
|
||||||
|
enum LexToken Token = LexGetToken(Parser, NULL, TRUE);
|
||||||
|
*Typ = NULL;
|
||||||
|
|
||||||
|
switch (Token)
|
||||||
|
{
|
||||||
|
case TokenIntType: case TokenLongType: case TokenShortType: *Typ = &IntType; break;
|
||||||
|
case TokenCharType: *Typ = &CharType; break;
|
||||||
|
#ifndef NO_FP
|
||||||
|
case TokenFloatType: case TokenDoubleType: *Typ = &FPType; break;
|
||||||
|
#endif
|
||||||
|
case TokenVoidType: *Typ = &VoidType; break;
|
||||||
|
|
||||||
|
case TokenStructType: case TokenUnionType:
|
||||||
|
if (*Typ != NULL)
|
||||||
|
ProgramFail(Parser, "bad type declaration");
|
||||||
|
|
||||||
|
TypeParseStruct(Parser, Typ, Token == TokenStructType);
|
||||||
|
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;
|
struct ParseState Before;
|
||||||
enum LexToken Token;
|
enum LexToken Token;
|
||||||
struct Value *LexValue;
|
struct Value *LexValue;
|
||||||
int Done = FALSE;
|
int Done = FALSE;
|
||||||
*Typ = NULL;
|
*Typ = BasicTyp;
|
||||||
*Identifier = StrEmpty;
|
*Identifier = StrEmpty;
|
||||||
|
|
||||||
while (!Done)
|
while (!Done)
|
||||||
|
@ -191,20 +220,6 @@ void TypeParse(struct ParseState *Parser, struct ValueType **Typ, char **Identif
|
||||||
Token = LexGetToken(Parser, &LexValue, TRUE);
|
Token = LexGetToken(Parser, &LexValue, TRUE);
|
||||||
switch (Token)
|
switch (Token)
|
||||||
{
|
{
|
||||||
case TokenIntType: case TokenLongType: case TokenShortType: *Typ = &IntType; break;
|
|
||||||
case TokenCharType: *Typ = &CharType; break;
|
|
||||||
#ifndef NO_FP
|
|
||||||
case TokenFloatType: case TokenDoubleType: *Typ = &FPType; break;
|
|
||||||
#endif
|
|
||||||
case TokenVoidType: *Typ = &VoidType; break;
|
|
||||||
|
|
||||||
case TokenStructType: case TokenUnionType:
|
|
||||||
if (*Typ != NULL)
|
|
||||||
ProgramFail(Parser, "bad type declaration");
|
|
||||||
|
|
||||||
TypeParseStruct(Parser, Typ, Token == TokenStructType);
|
|
||||||
break;
|
|
||||||
|
|
||||||
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);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue