diff --git a/TODO b/TODO index 54096ca..cfe1f2e 100644 --- a/TODO +++ b/TODO @@ -1,24 +1,13 @@ TODO -* struct * pointers * switch -* arrays * operator precedence * enum * interactive mode -New type system: -* basic types: picoc int, picoc char, native int, native char -* structs -* arrays of picoc types or structs -* pointers to: - Also: -* Change function store system to use dynamic allocation -* Check for Alloc returns checking NULL * Do we really need OnHeap? Isn't every value on the stack anyway? * Remove Var parameter from HeapPopStack() once we're certain it all works -* Allocating a variable on the heap isn't correct since we need the struct Value to be on the stack anyway diff --git a/parse.c b/parse.c index 5aa6d63..0f474e9 100644 --- a/parse.c +++ b/parse.c @@ -193,6 +193,25 @@ int ParseValue(struct ParseState *Parser, struct Value **Result, int ResultOnHea (*Result)->Val->Integer--; } } + else if (Token == TokenLeftSquareBracket) + { /* array access */ + LexGetToken(Parser, &LexValue, TRUE); + IntValue = ParseIntExpression(Parser, RunIt); + if (LexGetToken(Parser, &LexValue, TRUE) != TokenRightSquareBracket) + ProgramFail(Parser, "expected ']'"); + + if (RunIt) + { /* look up the array element */ + if ((*Result)->Typ->Base != TypeArray) + ProgramFail(Parser, "not an array"); + + if (IntValue < 0 || IntValue >= (*Result)->Typ->ArraySize) + ProgramFail(Parser, "illegal array index"); + + VariableStackPop(Parser, *Result); + LocalLValue = VariableAllocValueFromExistingData(Parser, (*Result)->Typ->FromType, (union AnyValue *)((void *)(&(*Result)->Val) + (*Result)->Typ->FromType->Sizeof * IntValue), TRUE, ResultOnHeap); + } + } } break; @@ -296,7 +315,7 @@ int ParseExpression(struct ParseState *Parser, struct Value **Result, int Result if (RunIt) { if (CurrentValue->Typ->Base == TypeFP || TotalValue->Typ->Base == TypeFP) - { + { /* floating point expression */ double FPTotal, FPCurrent, FPResult; if (CurrentValue->Typ->Base != TypeFP || TotalValue->Typ->Base != TypeFP) @@ -336,7 +355,7 @@ int ParseExpression(struct ParseState *Parser, struct Value **Result, int Result TotalValue = ParsePushFP(Parser, ResultOnHeap, FPResult); } else - { + { /* integer expression */ int IntX, IntY, IntResult; if (CurrentValue->Typ->Base != TypeInt || TotalValue->Typ->Base != TypeInt) diff --git a/tests/05_array.example b/tests/05_array.expect similarity index 100% rename from tests/05_array.example rename to tests/05_array.expect