/* define a variable. Ident must be registered */ struct Value *VariableDefine(struct ParseState *Parser, char *Ident, struct Value *InitValue, struct ValueType *Typ, int MakeWritable) { struct Value *AssignValue; if (InitValue != NULL) AssignValue = VariableAllocValueAndCopy(Parser, InitValue, TopStackFrame == NULL); else AssignValue = VariableAllocValueFromType(Parser, Typ, MakeWritable, NULL, TopStackFrame == NULL); AssignValue->IsLValue = MakeWritable; if (!TableSet((TopStackFrame == NULL) ? &GlobalTable : &TopStackFrame->LocalTable, Ident, AssignValue, Parser ? ((char *)Parser->FileName) : NULL, Parser ? Parser->Line : 0, Parser ? Parser->CharacterPos : 0)) ProgramFail(Parser, "'%s' is already defined", Ident); return AssignValue; }
/* define a variable. Ident must be registered */ struct Value *VariableDefine(struct ParseState *Parser, char *Ident, struct Value *InitValue, struct ValueType *Typ, int MakeWritable) { struct Value *AssignValue; short int ScopeLevel = Parser ? Parser->ScopeLevel : -1; //~ fprintf(stderr, "def %s %d\n", Ident, ScopeLevel); if (InitValue != NULL) AssignValue = VariableAllocValueAndCopy(Parser, InitValue, TopStackFrame == NULL); else AssignValue = VariableAllocValueFromType(Parser, Typ, MakeWritable, NULL, TopStackFrame == NULL); AssignValue->IsLValue = MakeWritable; AssignValue->ScopeLevel = ScopeLevel; AssignValue->OutOfScope = 0; struct Table * currentTable = (TopStackFrame == NULL) ? &GlobalTable : &TopStackFrame->LocalTable; struct Value * ExistingValue; if (TableGet(currentTable, Ident, &ExistingValue, NULL, NULL, NULL)) { if (ExistingValue->OutOfScope) { //~ fprintf(stderr, "reusing %s\n", Ident); // not sure how to delete properly, so... // throw the old copy away and hope it's cleaned up at VariableStackFramePop TableDeleteStack(currentTable, Ident); } } if (!TableSet(currentTable, Ident, AssignValue, Parser ? ((char *)Parser->FileName) : NULL, Parser ? Parser->Line : 0, Parser ? Parser->CharacterPos : 0)) ProgramFail(Parser, "'%s' is already defined", Ident); return AssignValue; }