'->' operator implemented

git-svn-id: http://picoc.googlecode.com/svn/trunk@181 21eae674-98b7-11dd-bd71-f92a316d2d60
This commit is contained in:
zik.saleeba 2009-03-09 23:18:04 +00:00
parent 23bce9d38a
commit 456a83da75
4 changed files with 41 additions and 3 deletions

2
TODO
View file

@ -13,10 +13,8 @@ Bugs:
Implement:
* operator precedence
* '->'
* pointer arithmetic
* casts
* enum
* fix #include
* char access/char array access/char * access

View file

@ -192,6 +192,7 @@ int ExpressionParse(struct ParseState *Parser, struct Value **Result)
if (Parser->Mode == RunModeRun)
{
void *TotalValueData = (void *)TotalValue->Val;
struct Value *TotalLValueFrom = TotalValue->LValueFrom;
if (TotalValue->Typ->Base != TypeStruct && TotalValue->Typ->Base != TypeUnion)
ProgramFail(Parser, "can't use '.' on something that's not a struct or union");
@ -200,7 +201,37 @@ int ExpressionParse(struct ParseState *Parser, struct Value **Result)
ProgramFail(Parser, "structure doesn't have a member called '%s'", Ident->Val->Identifier);
VariableStackPop(Parser, TotalValue);
TotalValue = VariableAllocValueFromExistingData(Parser, CurrentValue->Typ, TotalValueData + CurrentValue->Val->Integer, TRUE, TotalValue->LValueFrom);
TotalValue = VariableAllocValueFromExistingData(Parser, CurrentValue->Typ, TotalValueData + CurrentValue->Val->Integer, TRUE, TotalLValueFrom);
}
continue;
}
case TokenArrow:
{
struct Value *Ident;
LexGetToken(Parser, NULL, TRUE);
if (LexGetToken(Parser, &Ident, TRUE) != TokenIdentifier)
ProgramFail(Parser, "need an structure or union member after '->'");
if (Parser->Mode == RunModeRun)
{
void *TotalValueData;
struct Value *DerefValue;
if (TotalValue->Typ->Base != TypePointer)
ProgramFail(Parser, "can't dereference this non-pointer");
DerefValue = TotalValue->Val->Pointer.Segment;
TotalValueData = (void *)DerefValue->Val;
if (DerefValue->Typ->Base != TypeStruct && DerefValue->Typ->Base != TypeUnion)
ProgramFail(Parser, "can't use '->' on something that's not a struct or union");
if (!TableGet(DerefValue->Typ->Members, Ident->Val->Identifier, &CurrentValue))
ProgramFail(Parser, "structure doesn't have a member called '%s'", Ident->Val->Identifier);
VariableStackPop(Parser, TotalValue);
TotalValue = VariableAllocValueFromExistingData(Parser, CurrentValue->Typ, TotalValueData + CurrentValue->Val->Integer, TRUE, DerefValue);
}
continue;
}

View file

@ -21,6 +21,12 @@ printf("bolshevic.a = %d\n", bolshevic.a);
printf("bolshevic.b = %d\n", bolshevic.b);
printf("bolshevic.c = %d\n", bolshevic.c);
struct ziggy *tsar = &bolshevic;
printf("tsar->a = %d\n", tsar->a);
printf("tsar->b = %d\n", tsar->b);
printf("tsar->c = %d\n", tsar->c);
/*
b = &(bolshevic.b);
printf("bolshevic.b = %d\n", *b);

View file

@ -2,3 +2,6 @@ a = 42
bolshevic.a = 12
bolshevic.b = 34
bolshevic.c = 56
tsar->a = 12
tsar->b = 34
tsar->c = 56