Now copying function tokens so when we're in interactive mode we can free the original tokens without

losing the box and dice.


git-svn-id: http://picoc.googlecode.com/svn/trunk@161 21eae674-98b7-11dd-bd71-f92a316d2d60
This commit is contained in:
zik.saleeba 2009-03-07 06:17:11 +00:00
parent 4e0f734572
commit 8a5ed05fd2
3 changed files with 38 additions and 16 deletions

42
lex.c
View file

@ -474,21 +474,37 @@ enum LexToken LexGetToken(struct ParseState *Parser, struct Value **Value, int I
return Token; return Token;
} }
/* substitute an end of line token for another in the token stream (once only) */ /* find the end of the line */
void LexSubstituteEndOfLine(struct ParseState *Parser, enum LexToken ToToken) void LexToEndOfLine(struct ParseState *Parser)
{ {
enum LexToken Token; while (TRUE)
do
{ {
Token = (enum LexToken)*(unsigned char *)Parser->Pos; enum LexToken Token = (enum LexToken)*(unsigned char *)Parser->Pos;
if (Token == TokenEndOfLine) if (Token == TokenEndOfLine || Token == TokenEOF)
{ return;
*(unsigned char *)Parser->Pos = (unsigned char )ToToken;
Parser->Pos++;
Parser->Line++;
}
else else
LexGetToken(Parser, NULL, TRUE); LexGetToken(Parser, NULL, TRUE);
}
} while (Token != TokenEndOfLine && Token != TokenEOF); }
/* copy the tokens from StartParser to EndParser into new memory and terminate with a TokenEOF */
void *LexCopyTokens(struct ParseState *StartParser, struct ParseState *EndParser)
{
int MemSize = 0;
unsigned char *Pos = (unsigned char *)StartParser->Pos;
unsigned char *NewTokens;
enum LexToken Token;
while ( (Token = (enum LexToken)*Pos) != TokenEOF && (void *)Pos != EndParser->Pos)
{ /* count up how much memory we need */
int ValueSize = LexTokenSize(Token);
Pos += ValueSize + 1;
MemSize += ValueSize + 1;
}
assert((void *)Pos == EndParser->Pos);
NewTokens = VariableAlloc(StartParser, MemSize + 1, TRUE);
memcpy(NewTokens, StartParser->Pos, MemSize);
NewTokens[MemSize] = (unsigned char)TokenEOF;
return NewTokens;
} }

View file

@ -612,6 +612,7 @@ struct Value *ParseFunctionDefinition(struct ParseState *Parser, struct ValueTyp
enum LexToken Token; enum LexToken Token;
struct Value *FuncValue; struct Value *FuncValue;
struct ParseState ParamParser; struct ParseState ParamParser;
struct ParseState FuncBody;
int ParamCount = 0; int ParamCount = 0;
LexGetToken(Parser, NULL, TRUE); /* open bracket */ LexGetToken(Parser, NULL, TRUE); /* open bracket */
@ -636,7 +637,6 @@ struct Value *ParseFunctionDefinition(struct ParseState *Parser, struct ValueTyp
FuncValue->Val->FuncDef.VarArgs = FALSE; FuncValue->Val->FuncDef.VarArgs = FALSE;
FuncValue->Val->FuncDef.ParamType = (void *)FuncValue->Val + sizeof(struct FuncDef); FuncValue->Val->FuncDef.ParamType = (void *)FuncValue->Val + sizeof(struct FuncDef);
FuncValue->Val->FuncDef.ParamName = (void *)FuncValue->Val->FuncDef.ParamType + sizeof(struct ValueType *) * ParamCount; FuncValue->Val->FuncDef.ParamName = (void *)FuncValue->Val->FuncDef.ParamType + sizeof(struct ValueType *) * ParamCount;
FuncValue->Val->FuncDef.Body = *Parser;
for (ParamCount = 0; ParamCount < FuncValue->Val->FuncDef.NumParams; ParamCount++) for (ParamCount = 0; ParamCount < FuncValue->Val->FuncDef.NumParams; ParamCount++)
{ /* harvest the parameters into the function definition */ { /* harvest the parameters into the function definition */
@ -663,8 +663,12 @@ struct Value *ParseFunctionDefinition(struct ParseState *Parser, struct ValueTyp
if (LexGetToken(Parser, NULL, FALSE) != TokenLeftBrace) if (LexGetToken(Parser, NULL, FALSE) != TokenLeftBrace)
ProgramFail(Parser, "bad function definition"); ProgramFail(Parser, "bad function definition");
FuncBody = *Parser;
if (!ParseStatementMaybeRun(Parser, FALSE)) if (!ParseStatementMaybeRun(Parser, FALSE))
ProgramFail(Parser, "function definition expected"); ProgramFail(Parser, "function definition expected");
FuncValue->Val->FuncDef.Body = FuncBody;
FuncValue->Val->FuncDef.Body.Pos = LexCopyTokens(&FuncBody, Parser);
} }
if (!TableSet(&GlobalTable, Identifier, FuncValue)) if (!TableSet(&GlobalTable, Identifier, FuncValue))
@ -731,7 +735,8 @@ void ParseMacroDefinition(struct ParseState *Parser)
MacroValue->Val->Parser = *Parser; MacroValue->Val->Parser = *Parser;
MacroValue->Typ = &MacroType; MacroValue->Typ = &MacroType;
LexSubstituteEndOfLine(Parser, TokenEOF); LexToEndOfLine(Parser);
MacroValue->Val->Parser.Pos = LexCopyTokens(&MacroValue->Val->Parser, Parser);
if (!TableSet(&GlobalTable, MacroName->Val->Identifier, MacroValue)) if (!TableSet(&GlobalTable, MacroName->Val->Identifier, MacroValue))
ProgramFail(Parser, "'%s' is already defined", &MacroName->Val->Identifier); ProgramFail(Parser, "'%s' is already defined", &MacroName->Val->Identifier);

View file

@ -257,7 +257,8 @@ void LexInit(void);
void *LexAnalyse(const char *FileName, const char *Source, int SourceLen); void *LexAnalyse(const char *FileName, const char *Source, int SourceLen);
void LexInitParser(struct ParseState *Parser, void *TokenSource, const char *FileName, int Line, int RunIt); void LexInitParser(struct ParseState *Parser, void *TokenSource, const char *FileName, int Line, int RunIt);
enum LexToken LexGetToken(struct ParseState *Parser, struct Value **Value, int IncPos); enum LexToken LexGetToken(struct ParseState *Parser, struct Value **Value, int IncPos);
void LexSubstituteEndOfLine(struct ParseState *Parser, enum LexToken ToToken); void LexToEndOfLine(struct ParseState *Parser);
void *LexCopyTokens(struct ParseState *StartParser, struct ParseState *EndParser);
/* parse.c */ /* parse.c */
int ParseExpression(struct ParseState *Parser, struct Value **Result); int ParseExpression(struct ParseState *Parser, struct Value **Result);