Skip to content

Commit

Permalink
Unnamed enums, structs and unions implemented.
Browse files Browse the repository at this point in the history
git-svn-id: http://picoc.googlecode.com/svn/trunk@553 21eae674-98b7-11dd-bd71-f92a316d2d60
  • Loading branch information
zik.saleeba committed Feb 18, 2011
1 parent 33985ad commit 6b31113
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 16 deletions.
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
6 changes: 3 additions & 3 deletions platform.c
Original file line number Diff line number Diff line change
Expand Up @@ -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 changes: 33 additions & 12 deletions type.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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);
Expand All @@ -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;
}
Expand Down

0 comments on commit 6b31113

Please sign in to comment.