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: count:
@echo "Core:" @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 ""
@echo "Everything:" @echo "Everything:"
@cat $(SRCS) *.h */*.h | wc @cat $(SRCS) *.h */*.h | wc

View file

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

43
type.c
View file

@ -45,7 +45,8 @@ struct ValueType *TypeAdd(struct ParseState *Parser, struct ValueType *ParentTyp
return NewType; 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) struct ValueType *TypeGetMatching(struct ParseState *Parser, struct ValueType *ParentType, enum BaseType Base, int ArraySize, const char *Identifier, int AllowDuplicates)
{ {
int Sizeof; int Sizeof;
@ -195,14 +196,25 @@ void TypeParseStruct(struct ParseState *Parser, struct ValueType **Typ, int IsSt
struct Value *LexValue; struct Value *LexValue;
struct ValueType *MemberType; struct ValueType *MemberType;
char *MemberIdentifier; char *MemberIdentifier;
char *StructIdentifier;
struct Value *MemberValue; struct Value *MemberValue;
enum LexToken Token; enum LexToken Token;
int AlignBoundary; int AlignBoundary;
if (LexGetToken(Parser, &LexValue, TRUE) != TokenIdentifier) Token = LexGetToken(Parser, &LexValue, FALSE);
ProgramFail(Parser, "struct/union name required"); 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, LexValue->Val->Identifier); *Typ = TypeGetMatching(Parser, &UberType, IsStruct ? TypeStruct : TypeUnion, 0, StructIdentifier, Token != TokenLeftBrace);
Token = LexGetToken(Parser, NULL, FALSE); Token = LexGetToken(Parser, NULL, FALSE);
if (Token != TokenLeftBrace) 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 */ /* create a system struct which has no user-visible members */
struct ValueType *TypeCreateOpaqueStruct(struct ParseState *Parser, const char *StructName, int Size) 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 */ /* create the (empty) table */
Typ->Members = VariableAlloc(Parser, sizeof(struct Table) + STRUCT_TABLE_SIZE * sizeof(struct TableEntry), TRUE); 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; int EnumValue = 0;
char *EnumIdentifier; char *EnumIdentifier;
if (LexGetToken(Parser, &LexValue, TRUE) != TokenIdentifier) Token = LexGetToken(Parser, &LexValue, FALSE);
ProgramFail(Parser, "enum name required"); if (Token == TokenIdentifier)
{
*Typ = &IntType; LexGetToken(Parser, &LexValue, TRUE);
EnumType = TypeGetMatching(Parser, &UberType, TypeEnum, 0, LexValue->Val->Identifier); EnumIdentifier = LexValue->Val->Identifier;
Token = LexGetToken(Parser, NULL, FALSE); 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;
if (Token != TokenLeftBrace) if (Token != TokenLeftBrace)
{ {
/* use the already defined enum */ /* use the already defined enum */
if ((*Typ)->Members == NULL) if ((*Typ)->Members == NULL)
ProgramFail(Parser, "enum '%s' isn't defined", LexValue->Val->Identifier); ProgramFail(Parser, "enum '%s' isn't defined", EnumIdentifier);
return; return;
} }