struct are now working
git-svn-id: http://picoc.googlecode.com/svn/trunk@69 21eae674-98b7-11dd-bd71-f92a316d2d60
This commit is contained in:
parent
3a2c0552d6
commit
51afc538be
2
Makefile
2
Makefile
|
@ -1,5 +1,5 @@
|
|||
CC=gcc
|
||||
CFLAGS=-Wall -g
|
||||
CFLAGS=-Wall -g #-DDEBUG_HEAP
|
||||
LIBS=-lm
|
||||
|
||||
TARGET = picoc
|
||||
|
|
31
parse.c
31
parse.c
|
@ -149,10 +149,10 @@ int ParseValue(struct ParseState *Parser, struct Value **Result, int ResultOnHea
|
|||
if (!ParseExpression(&MacroLexer, Result, ResultOnHeap, TRUE))
|
||||
ProgramFail(&MacroLexer, "expression expected");
|
||||
}
|
||||
else if (!ISVALUETYPE(LocalLValue->Typ))
|
||||
ProgramFail(Parser, "bad variable type");
|
||||
else if (LocalLValue->Typ == TypeVoid)
|
||||
ProgramFail(Parser, "a void value isn't much use here");
|
||||
else
|
||||
*Result = VariableAllocValueAndCopy(Parser, LocalLValue, ResultOnHeap);
|
||||
*Result = VariableAllocValueFromExistingData(Parser, LocalLValue->Typ, LocalLValue->Val, ResultOnHeap);
|
||||
}
|
||||
}
|
||||
break;
|
||||
|
@ -218,7 +218,7 @@ int ParseExpression(struct ParseState *Parser, struct Value **Result, int Result
|
|||
{
|
||||
void *TotalValueData = (void *)TotalValue->Val;
|
||||
|
||||
if (TotalValue->Typ->Base != TypeStruct || TotalValue->Typ->Base != TypeUnion)
|
||||
if (TotalValue->Typ->Base != TypeStruct && TotalValue->Typ->Base != TypeUnion)
|
||||
ProgramFail(Parser, "can't use '.' on something that's not a struct or union");
|
||||
|
||||
if (!TableGet(TotalValue->Typ->Members, Ident->Val->String, &CurrentValue))
|
||||
|
@ -226,8 +226,9 @@ int ParseExpression(struct ParseState *Parser, struct Value **Result, int Result
|
|||
|
||||
VariableStackPop(Parser, TotalValue);
|
||||
TotalValue = VariableAllocValueFromExistingData(Parser, CurrentValue->Typ, TotalValueData + CurrentValue->Val->Integer, ResultOnHeap);
|
||||
LValue = TotalValue;
|
||||
}
|
||||
break;
|
||||
continue;
|
||||
}
|
||||
case TokenAssign: case TokenAddAssign: case TokenSubtractAssign:
|
||||
LexGetToken(Parser, NULL, TRUE);
|
||||
|
@ -563,16 +564,24 @@ int ParseStatement(struct ParseState *Parser, int RunIt)
|
|||
case TokenFloatType:
|
||||
case TokenDoubleType:
|
||||
case TokenVoidType:
|
||||
case TokenStructType:
|
||||
case TokenUnionType:
|
||||
*Parser = PreState;
|
||||
TypeParse(Parser, &Typ, &Identifier);
|
||||
if (Identifier == StrEmpty)
|
||||
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");
|
||||
|
||||
/* handle function definitions */
|
||||
if (LexGetToken(Parser, NULL, FALSE) == TokenOpenBracket)
|
||||
ParseFunctionDefinition(Parser, Typ, Identifier, FALSE);
|
||||
else
|
||||
VariableDefine(Parser, Identifier, VariableAllocValueFromType(Parser, Typ, FALSE));
|
||||
if (Identifier != StrEmpty)
|
||||
{
|
||||
/* handle function definitions */
|
||||
if (LexGetToken(Parser, NULL, FALSE) == TokenOpenBracket)
|
||||
ParseFunctionDefinition(Parser, Typ, Identifier, FALSE);
|
||||
else
|
||||
VariableDefine(Parser, Identifier, VariableAllocValueFromType(Parser, Typ, FALSE));
|
||||
}
|
||||
break;
|
||||
|
||||
case TokenHashDefine:
|
||||
|
|
|
@ -1,2 +1,5 @@
|
|||
/* this is a comment */
|
||||
// this is also a comment
|
||||
sayhello();
|
||||
sayhello(); /* this is a comment */ sayhello();
|
||||
sayhello();
|
||||
// this is also a comment sayhello();
|
||||
sayhello();
|
||||
|
|
|
@ -0,0 +1,5 @@
|
|||
Hello
|
||||
Hello
|
||||
Hello
|
||||
Hello
|
||||
Hello
|
27
type.c
27
type.c
|
@ -87,22 +87,34 @@ void TypeParseStruct(struct ParseState *Parser, struct ValueType **Typ, int IsSt
|
|||
struct ValueType *MemberType;
|
||||
const char *MemberIdentifier;
|
||||
struct Value *MemberValue;
|
||||
|
||||
if (LexGetToken(Parser, &LexValue, TRUE) != TokenIdentifier)
|
||||
ProgramFail(Parser, "struct/union name required");
|
||||
|
||||
if (LexGetToken(Parser, NULL, TRUE) != TokenLeftBrace)
|
||||
ProgramFail(Parser, "'{' expected");
|
||||
enum LexToken Token;
|
||||
|
||||
if (TopStackFrame != NULL)
|
||||
ProgramFail(Parser, "struct/union definitions can only be globals");
|
||||
|
||||
if (LexGetToken(Parser, &LexValue, TRUE) != TokenIdentifier)
|
||||
ProgramFail(Parser, "struct/union name required");
|
||||
|
||||
*Typ = TypeGetMatching(Parser, &UberType, IsStruct ? TypeStruct : TypeUnion, 0, LexValue->Val->String);
|
||||
|
||||
Token = LexGetToken(Parser, NULL, FALSE);
|
||||
if (Token != TokenLeftBrace)
|
||||
{ /* use the already defined structure */
|
||||
if ((*Typ)->Members == NULL)
|
||||
ProgramFail(Parser, "structure '%s' isn't defined", LexValue->Val->String);
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
LexGetToken(Parser, NULL, TRUE);
|
||||
(*Typ)->Members = VariableAlloc(Parser, sizeof(struct Table) + STRUCT_TABLE_SIZE * sizeof(struct TableEntry), TRUE);
|
||||
(*Typ)->Members->HashTable = (void *)(*Typ)->Members + sizeof(struct Table);
|
||||
TableInit((*Typ)->Members, (void *)(*Typ)->Members + sizeof(struct Table), STRUCT_TABLE_SIZE, TRUE);
|
||||
|
||||
do {
|
||||
TypeParse(Parser, &MemberType, &MemberIdentifier);
|
||||
if (MemberType == NULL || MemberIdentifier == NULL)
|
||||
ProgramFail(Parser, "invalid type in struct");
|
||||
|
||||
MemberValue = VariableAllocValueAndData(Parser, sizeof(int), TRUE);
|
||||
MemberValue->Typ = MemberType;
|
||||
|
@ -121,6 +133,9 @@ void TypeParseStruct(struct ParseState *Parser, struct ValueType **Typ, int IsSt
|
|||
if (!TableSet((*Typ)->Members, MemberIdentifier, MemberValue))
|
||||
ProgramFail(Parser, "member '%s' already defined", &MemberIdentifier);
|
||||
|
||||
if (LexGetToken(Parser, NULL, TRUE) != TokenSemicolon)
|
||||
ProgramFail(Parser, "semicolon expected");
|
||||
|
||||
} while (LexGetToken(Parser, NULL, FALSE) != TokenRightBrace);
|
||||
|
||||
LexGetToken(Parser, NULL, TRUE);
|
||||
|
|
|
@ -1,3 +1,6 @@
|
|||
#ifdef DEBUG_HEAP
|
||||
#include <stdio.h>
|
||||
#endif
|
||||
#include <string.h>
|
||||
|
||||
#include "picoc.h"
|
||||
|
|
Loading…
Reference in a new issue