Unnamed enums, structs and unions implemented.

git-svn-id: http://picoc.googlecode.com/svn/trunk@553 21eae674-98b7-11dd-bd71-f92a316d2d60
This commit is contained in:
zik.saleeba 2011-02-18 03:30:34 +00:00
parent 33985ad161
commit 6b31113511
3 changed files with 37 additions and 16 deletions

View file

@ -24,7 +24,7 @@ clean:
count:
@echo "Core:"
@cat picoc.h picoc.c table.c lex.c parse.c expression.c platform.c heap.c type.c variable.c include.c | grep -v '^[ ]*/\*' | grep -v '^[ ]*$$' | wc
@cat picoc.h interpreter.h picoc.c table.c lex.c parse.c expression.c platform.c heap.c type.c variable.c include.c | grep -v '^[ ]*/\*' | grep -v '^[ ]*$$' | wc
@echo ""
@echo "Everything:"
@cat $(SRCS) *.h */*.h | wc

View file

@ -223,14 +223,14 @@ char *PlatformMakeTempName(char *TempNameBuffer)
if (TempNameBuffer[CPos] < '9')
{
TempNameBuffer[CPos]++;
return TempNameBuffer;
return TableStrRegister(TempNameBuffer);
}
else
{
TempNameBuffer[CPos] = 0;
TempNameBuffer[CPos] = '0';
CPos--;
}
}
return TempNameBuffer;
return TableStrRegister(TempNameBuffer);
}

45
type.c
View file

@ -45,7 +45,8 @@ struct ValueType *TypeAdd(struct ParseState *Parser, struct ValueType *ParentTyp
return NewType;
}
/* given a parent type, get a matching derived type and make one if necessary */
/* given a parent type, get a matching derived type and make one if necessary.
* Identifier should be registered with the shared string table. */
struct ValueType *TypeGetMatching(struct ParseState *Parser, struct ValueType *ParentType, enum BaseType Base, int ArraySize, const char *Identifier, int AllowDuplicates)
{
int Sizeof;
@ -195,14 +196,25 @@ void TypeParseStruct(struct ParseState *Parser, struct ValueType **Typ, int IsSt
struct Value *LexValue;
struct ValueType *MemberType;
char *MemberIdentifier;
char *StructIdentifier;
struct Value *MemberValue;
enum LexToken Token;
int AlignBoundary;
if (LexGetToken(Parser, &LexValue, TRUE) != TokenIdentifier)
ProgramFail(Parser, "struct/union name required");
*Typ = TypeGetMatching(Parser, &UberType, IsStruct ? TypeStruct : TypeUnion, 0, LexValue->Val->Identifier);
Token = LexGetToken(Parser, &LexValue, FALSE);
if (Token == TokenIdentifier)
{
LexGetToken(Parser, &LexValue, TRUE);
StructIdentifier = LexValue->Val->Identifier;
Token = LexGetToken(Parser, NULL, FALSE);
}
else
{
static char TempNameBuf[7] = "^s0000";
StructIdentifier = PlatformMakeTempName(TempNameBuf);
}
*Typ = TypeGetMatching(Parser, &UberType, IsStruct ? TypeStruct : TypeUnion, 0, StructIdentifier, Token != TokenLeftBrace);
Token = LexGetToken(Parser, NULL, FALSE);
if (Token != TokenLeftBrace)
@ -271,7 +283,7 @@ void TypeParseStruct(struct ParseState *Parser, struct ValueType **Typ, int IsSt
/* create a system struct which has no user-visible members */
struct ValueType *TypeCreateOpaqueStruct(struct ParseState *Parser, const char *StructName, int Size)
{
struct ValueType *Typ = TypeGetMatching(Parser, &UberType, TypeStruct, 0, StructName);
struct ValueType *Typ = TypeGetMatching(Parser, &UberType, TypeStruct, 0, StructName, FALSE);
/* create the (empty) table */
Typ->Members = VariableAlloc(Parser, sizeof(struct Table) + STRUCT_TABLE_SIZE * sizeof(struct TableEntry), TRUE);
@ -292,17 +304,26 @@ void TypeParseEnum(struct ParseState *Parser, struct ValueType **Typ)
int EnumValue = 0;
char *EnumIdentifier;
if (LexGetToken(Parser, &LexValue, TRUE) != TokenIdentifier)
ProgramFail(Parser, "enum name required");
Token = LexGetToken(Parser, &LexValue, FALSE);
if (Token == TokenIdentifier)
{
LexGetToken(Parser, &LexValue, TRUE);
EnumIdentifier = LexValue->Val->Identifier;
Token = LexGetToken(Parser, NULL, FALSE);
}
else
{
static char TempNameBuf[7] = "^e0000";
EnumIdentifier = PlatformMakeTempName(TempNameBuf);
}
EnumType = TypeGetMatching(Parser, &UberType, TypeEnum, 0, EnumIdentifier, Token != TokenLeftBrace);
*Typ = &IntType;
EnumType = TypeGetMatching(Parser, &UberType, TypeEnum, 0, LexValue->Val->Identifier);
Token = LexGetToken(Parser, NULL, FALSE);
if (Token != TokenLeftBrace)
{
/* use the already defined enum */
if ((*Typ)->Members == NULL)
ProgramFail(Parser, "enum '%s' isn't defined", LexValue->Val->Identifier);
ProgramFail(Parser, "enum '%s' isn't defined", EnumIdentifier);
return;
}