Declarations can now assign an initial value
git-svn-id: http://picoc.googlecode.com/svn/trunk@156 21eae674-98b7-11dd-bd71-f92a316d2d60
This commit is contained in:
parent
886526f462
commit
8b410241ee
3
TODO
3
TODO
|
@ -1,6 +1,6 @@
|
|||
TODO
|
||||
|
||||
* change character constants to have a leading array length
|
||||
* assignment on declaration
|
||||
* pointers to array elements and struct elements
|
||||
* operator precedence
|
||||
* test character array pointers and dereferencing
|
||||
|
@ -17,7 +17,6 @@ TODO
|
|||
* periodic heap cleanup
|
||||
* octal/hex character constants
|
||||
* fix #include
|
||||
* assignment on declaration
|
||||
|
||||
Need test/debug:
|
||||
* all break/continue variations
|
||||
|
|
55
parse.c
55
parse.c
|
@ -673,6 +673,43 @@ struct Value *ParseFunctionDefinition(struct ParseState *Parser, struct ValueTyp
|
|||
return FuncValue;
|
||||
}
|
||||
|
||||
/* declare a variable or function */
|
||||
void ParseDeclaration(struct ParseState *Parser, enum LexToken Token)
|
||||
{
|
||||
char *Identifier;
|
||||
struct ValueType *Typ;
|
||||
struct Value *CValue;
|
||||
|
||||
TypeParse(Parser, &Typ, &Identifier);
|
||||
if (Token == TokenVoidType && Identifier != StrEmpty)
|
||||
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 */
|
||||
if (LexGetToken(Parser, NULL, FALSE) == TokenOpenBracket)
|
||||
ParseFunctionDefinition(Parser, Typ, Identifier, FALSE);
|
||||
else
|
||||
{
|
||||
if (LexGetToken(Parser, NULL, FALSE) != TokenAssign)
|
||||
VariableDefine(Parser, Identifier, VariableAllocValueFromType(Parser, Typ, TRUE, NULL));
|
||||
else
|
||||
{ /* we're assigning an initial value */
|
||||
LexGetToken(Parser, NULL, TRUE);
|
||||
if (!ParseExpression(Parser, &CValue))
|
||||
ProgramFail(Parser, "expression expected");
|
||||
|
||||
VariableDefine(Parser, Identifier, CValue);
|
||||
if (Parser->Mode == RunModeRun)
|
||||
VariableStackPop(Parser, CValue);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/* parse a #define macro definition and store it for later */
|
||||
void ParseMacroDefinition(struct ParseState *Parser)
|
||||
{
|
||||
|
@ -806,8 +843,6 @@ int ParseStatement(struct ParseState *Parser)
|
|||
struct Value *CValue;
|
||||
int Condition;
|
||||
struct ParseState PreState = *Parser;
|
||||
char *Identifier;
|
||||
struct ValueType *Typ;
|
||||
enum LexToken Token = LexGetToken(Parser, NULL, TRUE);
|
||||
|
||||
switch (Token)
|
||||
|
@ -902,21 +937,7 @@ int ParseStatement(struct ParseState *Parser)
|
|||
case TokenStructType:
|
||||
case TokenUnionType:
|
||||
*Parser = PreState;
|
||||
TypeParse(Parser, &Typ, &Identifier);
|
||||
if (Token == TokenVoidType && Identifier != StrEmpty)
|
||||
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 */
|
||||
if (LexGetToken(Parser, NULL, FALSE) == TokenOpenBracket)
|
||||
ParseFunctionDefinition(Parser, Typ, Identifier, FALSE);
|
||||
else
|
||||
VariableDefine(Parser, Identifier, VariableAllocValueFromType(Parser, Typ, TRUE, NULL));
|
||||
}
|
||||
ParseDeclaration(Parser, Token);
|
||||
break;
|
||||
|
||||
case TokenHashDefine:
|
||||
|
|
|
@ -1,3 +1,7 @@
|
|||
int a;
|
||||
a = 42;
|
||||
printint(a);
|
||||
printf("%d\n", a);
|
||||
|
||||
int b = 64;
|
||||
printf("%d\n", b);
|
||||
|
||||
|
|
|
@ -1 +1,2 @@
|
|||
42
|
||||
64
|
||||
|
|
Loading…
Reference in a new issue