Fiddling with pointers

git-svn-id: http://picoc.googlecode.com/svn/trunk@115 21eae674-98b7-11dd-bd71-f92a316d2d60
This commit is contained in:
zik.saleeba 2009-02-25 11:14:46 +00:00
parent 901da5409a
commit 3558506261
5 changed files with 63 additions and 21 deletions

13
TODO
View file

@ -1,13 +1,15 @@
TODO TODO
* move stdio functions into an implementation-specific file
* make sure everything compiles without much library support
* make printf() honour memory constraints
* pointers * pointers
* test character array pointers and dereferencing
* operator precedence * operator precedence
* enum * '->'
* make printf() honour memory constraints
* interactive mode * interactive mode
* casts * casts
* enum
* '+=' / '-=' for pointers
* pointer arithmetic
* change heap to use a single consistent freelist node struct * change heap to use a single consistent freelist node struct
* periodic heap cleanup * periodic heap cleanup
* fix type comparison to take into account array size * fix type comparison to take into account array size
@ -15,8 +17,7 @@ TODO
* expression and auto-cast support for all types * expression and auto-cast support for all types
* octal/hex character constants * octal/hex character constants
* fix #include * fix #include
* eliminate host puts use * assignment on declaration
* move all system specific includes to a single file
Need test/debug: Need test/debug:
* all break/continue variations * all break/continue variations

70
parse.c
View file

@ -5,11 +5,6 @@ int ParseArguments(struct ParseState *Parser, int RunIt);
int ParseStatementMaybeRun(struct ParseState *Parser, int Condition); int ParseStatementMaybeRun(struct ParseState *Parser, int Condition);
/* initialise the parser */
void ParseInit()
{
}
/* do a function call */ /* do a function call */
void ParseFunctionCall(struct ParseState *Parser, struct Value **Result, const char *FuncName) void ParseFunctionCall(struct ParseState *Parser, struct Value **Result, const char *FuncName)
{ {
@ -157,6 +152,7 @@ int ParseValue(struct ParseState *Parser, struct Value **Result)
VType = (*Result)->Typ; VType = (*Result)->Typ;
VariableStackPop(Parser, *Result); VariableStackPop(Parser, *Result);
*Result = VariableAllocValueFromType(Parser, TypeGetMatching(Parser, VType, TypePointer, 0, StrEmpty), FALSE); *Result = VariableAllocValueFromType(Parser, TypeGetMatching(Parser, VType, TypePointer, 0, StrEmpty), FALSE);
// XXX - need to rethink how to deal with lvalues - I need the original lvalue, not a copy of it for the segment
(*Result)->Val->Pointer.Segment = LocalLValue; (*Result)->Val->Pointer.Segment = LocalLValue;
(*Result)->Val->Pointer.Data.Offset = 0; (*Result)->Val->Pointer.Data.Offset = 0;
break; break;
@ -292,23 +288,69 @@ int ParseExpression(struct ParseState *Parser, struct Value **Result)
} }
continue; continue;
} }
case TokenAssign: case TokenAddAssign: case TokenSubtractAssign: case TokenAssign:
LexGetToken(Parser, NULL, TRUE); LexGetToken(Parser, NULL, TRUE);
if (!ParseExpression(Parser, &CurrentValue)) if (!ParseExpression(Parser, &CurrentValue))
ProgramFail(Parser, "expression expected"); ProgramFail(Parser, "expression expected");
if (Parser->Mode == RunModeRun) if (Parser->Mode == RunModeRun)
{ { /* do the assignment */
if (CurrentValue->Typ->Base != TypeInt || !TotalValue->IsLValue || TotalValue->Typ->Base != TypeInt) if (!TotalValue->IsLValue)
ProgramFail(Parser, "can't assign"); ProgramFail(Parser, "can't assign to this");
switch (Token) if (CurrentValue->Typ != TotalValue->Typ)
{ ProgramFail(Parser, "can't assign incompatible types");
case TokenAddAssign: TotalValue->Val->Integer += CurrentValue->Val->Integer; break;
case TokenSubtractAssign: TotalValue->Val->Integer -= CurrentValue->Val->Integer; break; if (TotalValue->Typ->Base != TypeArray)
default: TotalValue->Val->Integer = CurrentValue->Val->Integer; break; memcpy(TotalValue->Val, CurrentValue->Val, TotalValue->Typ->Sizeof);
else
{ /* array assignment */
if (TotalValue->Val->Array.Size != CurrentValue->Val->Array.Size)
ProgramFail(Parser, "incompatible array sizes in assignment");
//memcpy(TotalValue->Val->Array.Data, CurrentValue->Val->Array.Data, CurrentValue->Typ->Sizeof * CurrentValue->Val->Array.Size);
} }
VariableStackPop(Parser, CurrentValue); VariableStackPop(Parser, CurrentValue);
*Result = TotalValue;
}
return TRUE;
case TokenAddAssign: case TokenSubtractAssign:
LexGetToken(Parser, NULL, TRUE);
if (!ParseExpression(Parser, &CurrentValue))
ProgramFail(Parser, "expression expected");
if (Parser->Mode == RunModeRun)
{ /* do the assignment */
if (!TotalValue->IsLValue)
ProgramFail(Parser, "can't assign");
if (CurrentValue->Typ->Base == TypeInt && TotalValue->Typ->Base == TypeInt)
{
switch (Token)
{
case TokenAddAssign: TotalValue->Val->Integer += CurrentValue->Val->Integer; break;
case TokenSubtractAssign: TotalValue->Val->Integer -= CurrentValue->Val->Integer; break;
default: break;
}
VariableStackPop(Parser, CurrentValue);
}
#ifndef NO_FP
else if (CurrentValue->Typ->Base == TypeFP && TotalValue->Typ->Base == TypeFP)
{
switch (Token)
{
case TokenAddAssign: TotalValue->Val->FP += CurrentValue->Val->FP; break;
case TokenSubtractAssign: TotalValue->Val->FP -= CurrentValue->Val->FP; break;
default: break;
}
VariableStackPop(Parser, CurrentValue);
}
#endif
else
ProgramFail(Parser, "can't operate and assign these types");
} }
// fallthrough // fallthrough

View file

@ -11,7 +11,6 @@ void Initialise()
TypeInit(); TypeInit();
LibraryInit(&GlobalTable, "c library", &CLibrary); LibraryInit(&GlobalTable, "c library", &CLibrary);
LibraryInit(&GlobalTable, "platform library", &PlatformLibrary); LibraryInit(&GlobalTable, "platform library", &PlatformLibrary);
ParseInit();
} }
/* platform-dependent code for running programs is in this file */ /* platform-dependent code for running programs is in this file */

View file

@ -247,7 +247,6 @@ void LexInitParser(struct ParseState *Parser, void *TokenSource, const char *Fil
enum LexToken LexGetToken(struct ParseState *Parser, struct Value **Value, int IncPos); enum LexToken LexGetToken(struct ParseState *Parser, struct Value **Value, int IncPos);
/* parse.c */ /* parse.c */
void ParseInit(void);
int ParseExpression(struct ParseState *Parser, struct Value **Result); int ParseExpression(struct ParseState *Parser, struct Value **Result);
int ParseIntExpression(struct ParseState *Parser); int ParseIntExpression(struct ParseState *Parser);
int ParseStatement(struct ParseState *Parser); int ParseStatement(struct ParseState *Parser);

View file

@ -1,5 +1,6 @@
int a; int a;
int *b; int *b;
int c;
a = 42; a = 42;
b = &a; b = &a;