Add simple range for loop

This commit is contained in:
drmortalwombat 2023-09-12 21:33:59 +02:00
parent d94d52b852
commit eccb2787dc
5 changed files with 306 additions and 41 deletions

View File

@ -956,6 +956,14 @@ Declaration* Declaration::BuildRValueRef(const Location& loc)
return pdec; return pdec;
} }
Declaration* Declaration::NonRefBase(void)
{
if (IsReference())
return mBase;
else
return this;
}
Declaration* Declaration::BuildConstRValueRef(const Location& loc) Declaration* Declaration::BuildConstRValueRef(const Location& loc)
{ {
Declaration* pdec = new Declaration(loc, DT_TYPE_RVALUEREF); Declaration* pdec = new Declaration(loc, DT_TYPE_RVALUEREF);

View File

@ -309,6 +309,7 @@ public:
Declaration* BuildConstReference(const Location& loc); Declaration* BuildConstReference(const Location& loc);
Declaration* BuildRValueRef(const Location& loc); Declaration* BuildRValueRef(const Location& loc);
Declaration* BuildConstRValueRef(const Location& loc); Declaration* BuildConstRValueRef(const Location& loc);
Declaration* NonRefBase(void);
DecType ValueType(void) const; DecType ValueType(void) const;

View File

@ -155,12 +155,14 @@ void GlobalAnalyzer::AutoInline(void)
Declaration* dec = f->mBase->mParams; Declaration* dec = f->mBase->mParams;
while (dec) while (dec)
{ {
nparams++; nparams += dec->mSize;
dec = dec->mNext; dec = dec->mNext;
} }
int cost = (f->mComplexity - 20 * nparams); int cost = (f->mComplexity - 20 * nparams);
// printf("CHEK INLINING %s %d * (%d - 1)\n", f->mIdent->mString, cost, f->mCallers.Size());
bool doinline = false; bool doinline = false;
if ((f->mCompilerOptions & COPT_OPTIMIZE_INLINE) && (f->mFlags & DTF_REQUEST_INLINE)) if ((f->mCompilerOptions & COPT_OPTIMIZE_INLINE) && (f->mFlags & DTF_REQUEST_INLINE))
doinline = true; doinline = true;
@ -171,9 +173,8 @@ void GlobalAnalyzer::AutoInline(void)
if (doinline) if (doinline)
{ {
#if 0 // printf("INLINING %s %d * (%d - 1)\n", f->mIdent->mString, cost, f->mCallers.Size());
printf("INLINING %s %d * (%d - 1)\n", f->mIdent->mString, cost, f->mCallers.Size());
#endif
f->mFlags |= DTF_INLINE; f->mFlags |= DTF_INLINE;
for (int j = 0; j < f->mCallers.Size(); j++) for (int j = 0; j < f->mCallers.Size(); j++)
{ {
@ -655,8 +656,6 @@ Declaration * GlobalAnalyzer::Analyze(Expression* exp, Declaration* procDec, boo
{ {
Declaration* ldec, * rdec; Declaration* ldec, * rdec;
procDec->mComplexity += 10;
switch (exp->mType) switch (exp->mType)
{ {
case EX_ERROR: case EX_ERROR:
@ -715,6 +714,8 @@ Declaration * GlobalAnalyzer::Analyze(Expression* exp, Declaration* procDec, boo
return exp->mDecValue; return exp->mDecValue;
case EX_INITIALIZATION: case EX_INITIALIZATION:
case EX_ASSIGNMENT: case EX_ASSIGNMENT:
procDec->mComplexity += 10 * exp->mLeft->mDecType->mSize;
ldec = Analyze(exp->mLeft, procDec, true); ldec = Analyze(exp->mLeft, procDec, true);
rdec = Analyze(exp->mRight, procDec, false); rdec = Analyze(exp->mRight, procDec, false);
if (exp->mLeft->mType == EX_VARIABLE && exp->mRight->mType == EX_CALL && exp->mLeft->mDecType->mType == DT_TYPE_STRUCT) if (exp->mLeft->mType == EX_VARIABLE && exp->mRight->mType == EX_CALL && exp->mLeft->mDecType->mType == DT_TYPE_STRUCT)
@ -723,16 +724,22 @@ Declaration * GlobalAnalyzer::Analyze(Expression* exp, Declaration* procDec, boo
return ldec; return ldec;
case EX_BINARY: case EX_BINARY:
procDec->mComplexity += 10 * exp->mDecType->mSize;
ldec = Analyze(exp->mLeft, procDec, lhs); ldec = Analyze(exp->mLeft, procDec, lhs);
rdec = Analyze(exp->mRight, procDec, lhs); rdec = Analyze(exp->mRight, procDec, lhs);
return ldec; return ldec;
case EX_RELATIONAL: case EX_RELATIONAL:
procDec->mComplexity += 10 * exp->mLeft->mDecType->mSize;
ldec = Analyze(exp->mLeft, procDec, false); ldec = Analyze(exp->mLeft, procDec, false);
rdec = Analyze(exp->mRight, procDec, false); rdec = Analyze(exp->mRight, procDec, false);
return TheBoolTypeDeclaration; return TheBoolTypeDeclaration;
case EX_PREINCDEC: case EX_PREINCDEC:
procDec->mComplexity += 10 * exp->mLeft->mDecType->mSize;
return Analyze(exp->mLeft, procDec, true); return Analyze(exp->mLeft, procDec, true);
case EX_PREFIX: case EX_PREFIX:
if (exp->mToken == TK_BINARY_AND) if (exp->mToken == TK_BINARY_AND)
@ -751,13 +758,21 @@ Declaration * GlobalAnalyzer::Analyze(Expression* exp, Declaration* procDec, boo
return exp->mDecType; return exp->mDecType;
} }
else else
{
procDec->mComplexity += 10 * exp->mLeft->mDecType->mSize;
return Analyze(exp->mLeft, procDec, false); return Analyze(exp->mLeft, procDec, false);
}
break; break;
case EX_POSTFIX: case EX_POSTFIX:
procDec->mComplexity += 10 * exp->mLeft->mDecType->mSize;
break; break;
case EX_POSTINCDEC: case EX_POSTINCDEC:
procDec->mComplexity += 10 * exp->mLeft->mDecType->mSize;
return Analyze(exp->mLeft, procDec, true); return Analyze(exp->mLeft, procDec, true);
case EX_INDEX: case EX_INDEX:
procDec->mComplexity += 10 * exp->mLeft->mDecType->mSize;
ldec = Analyze(exp->mLeft, procDec, lhs); ldec = Analyze(exp->mLeft, procDec, lhs);
if (ldec->mType == DT_VARIABLE || ldec->mType == DT_ARGUMENT) if (ldec->mType == DT_VARIABLE || ldec->mType == DT_ARGUMENT)
{ {
@ -786,6 +801,8 @@ Declaration * GlobalAnalyzer::Analyze(Expression* exp, Declaration* procDec, boo
// intentional fall through // intentional fall through
case EX_CALL: case EX_CALL:
case EX_INLINE: case EX_INLINE:
procDec->mComplexity += 10;
ldec = Analyze(exp->mLeft, procDec, false); ldec = Analyze(exp->mLeft, procDec, false);
if ((ldec->mFlags & DTF_INTRINSIC) && !ldec->mValue) if ((ldec->mFlags & DTF_INTRINSIC) && !ldec->mValue)
{ {
@ -813,6 +830,8 @@ Declaration * GlobalAnalyzer::Analyze(Expression* exp, Declaration* procDec, boo
{ {
Expression* pex = rex->mType == EX_LIST ? rex->mLeft : rex; Expression* pex = rex->mType == EX_LIST ? rex->mLeft : rex;
procDec->mComplexity += 5 * pex->mDecType->mSize;
if (pdec && !(ldec->mBase->mFlags & DTF_VARIADIC) && !(ldec->mFlags & (DTF_INTRINSIC | DTF_FUNC_ASSEMBLER))) if (pdec && !(ldec->mBase->mFlags & DTF_VARIADIC) && !(ldec->mFlags & (DTF_INTRINSIC | DTF_FUNC_ASSEMBLER)))
{ {
#if 1 #if 1
@ -930,10 +949,14 @@ Declaration * GlobalAnalyzer::Analyze(Expression* exp, Declaration* procDec, boo
case EX_WHILE: case EX_WHILE:
procDec->mFlags &= ~DTF_FUNC_CONSTEXPR; procDec->mFlags &= ~DTF_FUNC_CONSTEXPR;
procDec->mComplexity += 20;
ldec = Analyze(exp->mLeft, procDec, false); ldec = Analyze(exp->mLeft, procDec, false);
rdec = Analyze(exp->mRight, procDec, false); rdec = Analyze(exp->mRight, procDec, false);
break; break;
case EX_IF: case EX_IF:
procDec->mComplexity += 20;
ldec = Analyze(exp->mLeft, procDec, false); ldec = Analyze(exp->mLeft, procDec, false);
rdec = Analyze(exp->mRight->mLeft, procDec, false); rdec = Analyze(exp->mRight->mLeft, procDec, false);
if (exp->mRight->mRight) if (exp->mRight->mRight)
@ -944,6 +967,8 @@ Declaration * GlobalAnalyzer::Analyze(Expression* exp, Declaration* procDec, boo
case EX_FOR: case EX_FOR:
procDec->mFlags &= ~DTF_FUNC_CONSTEXPR; procDec->mFlags &= ~DTF_FUNC_CONSTEXPR;
procDec->mComplexity += 30;
if (exp->mLeft->mRight) if (exp->mLeft->mRight)
ldec = Analyze(exp->mLeft->mRight, procDec, false); ldec = Analyze(exp->mLeft->mRight, procDec, false);
if (exp->mLeft->mLeft->mLeft) if (exp->mLeft->mLeft->mLeft)
@ -953,6 +978,8 @@ Declaration * GlobalAnalyzer::Analyze(Expression* exp, Declaration* procDec, boo
ldec = Analyze(exp->mLeft->mLeft->mRight, procDec, false); ldec = Analyze(exp->mLeft->mLeft->mRight, procDec, false);
break; break;
case EX_DO: case EX_DO:
procDec->mComplexity += 20;
ldec = Analyze(exp->mLeft, procDec, false); ldec = Analyze(exp->mLeft, procDec, false);
rdec = Analyze(exp->mRight, procDec, false); rdec = Analyze(exp->mRight, procDec, false);
break; break;
@ -989,6 +1016,8 @@ Declaration * GlobalAnalyzer::Analyze(Expression* exp, Declaration* procDec, boo
exp = exp->mRight; exp = exp->mRight;
while (exp) while (exp)
{ {
procDec->mComplexity += 10;
if (exp->mLeft->mRight) if (exp->mLeft->mRight)
rdec = Analyze(exp->mLeft->mRight, procDec, false); rdec = Analyze(exp->mLeft->mRight, procDec, false);
exp = exp->mRight; exp = exp->mRight;
@ -999,6 +1028,8 @@ Declaration * GlobalAnalyzer::Analyze(Expression* exp, Declaration* procDec, boo
case EX_DEFAULT: case EX_DEFAULT:
break; break;
case EX_CONDITIONAL: case EX_CONDITIONAL:
procDec->mComplexity += exp->mDecType->mSize * 10;
ldec = Analyze(exp->mLeft, procDec, false); ldec = Analyze(exp->mLeft, procDec, false);
RegisterProc(Analyze(exp->mRight->mLeft, procDec, false)); RegisterProc(Analyze(exp->mRight->mLeft, procDec, false));
RegisterProc(Analyze(exp->mRight->mRight, procDec, false)); RegisterProc(Analyze(exp->mRight->mRight, procDec, false));

View File

@ -3425,11 +3425,32 @@ bool InterInstruction::PropagateConstTemps(const GrowingInstructionPtrArray& cte
} }
} }
if (!changed)
{
if (mSrc[0].mType == IT_POINTER && mSrc[1].mType == IT_POINTER &&
mSrc[0].mTemp >= 0 && mSrc[1].mTemp >= 0 &&
ctemps[mSrc[0].mTemp] && ctemps[mSrc[1].mTemp])
{
InterInstruction* si0 = ctemps[mSrc[0].mTemp];
InterInstruction* si1 = ctemps[mSrc[1].mTemp];
if (si0->mConst.mMemory != IM_INDIRECT && si1->mConst.mMemory != IM_INDIRECT)
{
mCode = IC_CONSTANT;
mConst.mIntConst = ::ConstantRelationalPointerFolding(mOperator, si1->mConst, si0->mConst);
mConst.mType = IT_BOOL;
mNumOperands = 0;
return true;
}
}
}
if (changed) if (changed)
{ {
this->ConstantFolding(); this->ConstantFolding();
return true; return true;
} }
} break; } break;
case IC_FREE: case IC_FREE:

View File

@ -2588,6 +2588,7 @@ void Parser::AddDefaultConstructors(Declaration* pthis)
Expression* iexp = new Expression(mScanner->mLocation, EX_POSTINCDEC); Expression* iexp = new Expression(mScanner->mLocation, EX_POSTINCDEC);
iexp->mToken = TK_INC; iexp->mToken = TK_INC;
iexp->mLeft = pexp; iexp->mLeft = pexp;
iexp->mDecType = pexp->mDecType;
Expression* fexp = new Expression(mScanner->mLocation, EX_CONSTANT); Expression* fexp = new Expression(mScanner->mLocation, EX_CONSTANT);
fexp->mDecValue = pthis->mBase->mDefaultConstructor; fexp->mDecValue = pthis->mBase->mDefaultConstructor;
@ -2664,6 +2665,7 @@ void Parser::AddDefaultConstructors(Declaration* pthis)
Expression* iexp = new Expression(mScanner->mLocation, EX_POSTINCDEC); Expression* iexp = new Expression(mScanner->mLocation, EX_POSTINCDEC);
iexp->mToken = TK_INC; iexp->mToken = TK_INC;
iexp->mLeft = pexp; iexp->mLeft = pexp;
iexp->mDecType = pexp->mDecType;
Expression* fexp = new Expression(mScanner->mLocation, EX_CONSTANT); Expression* fexp = new Expression(mScanner->mLocation, EX_CONSTANT);
fexp->mDecValue = pthis->mBase->mDestructor; fexp->mDecValue = pthis->mBase->mDestructor;
@ -2753,10 +2755,12 @@ void Parser::AddDefaultConstructors(Declaration* pthis)
Expression* iexp = new Expression(mScanner->mLocation, EX_POSTINCDEC); Expression* iexp = new Expression(mScanner->mLocation, EX_POSTINCDEC);
iexp->mToken = TK_INC; iexp->mToken = TK_INC;
iexp->mLeft = pexp; iexp->mLeft = pexp;
iexp->mDecType = pexp->mDecType;
Expression* isexp = new Expression(mScanner->mLocation, EX_POSTINCDEC); Expression* isexp = new Expression(mScanner->mLocation, EX_POSTINCDEC);
isexp->mToken = TK_INC; isexp->mToken = TK_INC;
isexp->mLeft = psexp; isexp->mLeft = psexp;
isexp->mDecType = psexp->mDecType;
Expression* disexp = new Expression(mScanner->mLocation, EX_PREFIX); Expression* disexp = new Expression(mScanner->mLocation, EX_PREFIX);
disexp->mToken = TK_MUL; disexp->mToken = TK_MUL;
@ -2766,6 +2770,7 @@ void Parser::AddDefaultConstructors(Declaration* pthis)
Expression* dexp = new Expression(mScanner->mLocation, EX_POSTINCDEC); Expression* dexp = new Expression(mScanner->mLocation, EX_POSTINCDEC);
dexp->mToken = TK_DEC; dexp->mToken = TK_DEC;
dexp->mLeft = aexp; dexp->mLeft = aexp;
dexp->mDecType = aexp->mDecType;
Expression* fexp = new Expression(mScanner->mLocation, EX_CONSTANT); Expression* fexp = new Expression(mScanner->mLocation, EX_CONSTANT);
fexp->mDecValue = pthis->mBase->mCopyConstructor; fexp->mDecValue = pthis->mBase->mCopyConstructor;
@ -2854,10 +2859,12 @@ void Parser::AddDefaultConstructors(Declaration* pthis)
Expression* iexp = new Expression(mScanner->mLocation, EX_POSTINCDEC); Expression* iexp = new Expression(mScanner->mLocation, EX_POSTINCDEC);
iexp->mToken = TK_INC; iexp->mToken = TK_INC;
iexp->mLeft = pexp; iexp->mLeft = pexp;
iexp->mDecType = pexp->mDecType;
Expression* isexp = new Expression(mScanner->mLocation, EX_POSTINCDEC); Expression* isexp = new Expression(mScanner->mLocation, EX_POSTINCDEC);
isexp->mToken = TK_INC; isexp->mToken = TK_INC;
isexp->mLeft = psexp; isexp->mLeft = psexp;
isexp->mDecType = psexp->mDecType;
Expression* disexp = new Expression(mScanner->mLocation, EX_PREFIX); Expression* disexp = new Expression(mScanner->mLocation, EX_PREFIX);
disexp->mToken = TK_MUL; disexp->mToken = TK_MUL;
@ -2867,6 +2874,7 @@ void Parser::AddDefaultConstructors(Declaration* pthis)
Expression* dexp = new Expression(mScanner->mLocation, EX_POSTINCDEC); Expression* dexp = new Expression(mScanner->mLocation, EX_POSTINCDEC);
dexp->mToken = TK_DEC; dexp->mToken = TK_DEC;
dexp->mLeft = aexp; dexp->mLeft = aexp;
dexp->mDecValue = aexp->mDecType;
Expression* fexp = new Expression(mScanner->mLocation, EX_CONSTANT); Expression* fexp = new Expression(mScanner->mLocation, EX_CONSTANT);
fexp->mDecValue = pthis->mBase->mCopyAssignment; fexp->mDecValue = pthis->mBase->mCopyAssignment;
@ -4146,7 +4154,7 @@ Declaration* Parser::ParseDeclaration(Declaration * pdec, bool variable, bool ex
ldec = ndec; ldec = ndec;
// ndec->mNext = nullptr; // ndec->mNext = nullptr;
if (ConsumeTokenIf(TK_COLON)) if (!mFunctionType && ConsumeTokenIf(TK_COLON))
{ {
Expression* exp = ParseRExpression(); Expression* exp = ParseRExpression();
if (!ndec->mBase->IsIntegerType()) if (!ndec->mBase->IsIntegerType())
@ -4360,11 +4368,11 @@ Expression* Parser::ParseDeclarationExpression(Declaration * pdec)
else else
{ {
while (dec) while (dec)
{
if (dec->mValue && !(dec->mFlags & DTF_GLOBAL))
{ {
Expression* nexp; Expression* nexp;
if (dec->mValue && !(dec->mFlags & DTF_GLOBAL))
{
if ((mCompilerOptions & COPT_CPLUSPLUS) && dec->mValue->mType == EX_CONSTRUCT) if ((mCompilerOptions & COPT_CPLUSPLUS) && dec->mValue->mType == EX_CONSTRUCT)
{ {
nexp = dec->mValue; nexp = dec->mValue;
@ -4446,6 +4454,13 @@ Expression* Parser::ParseDeclarationExpression(Declaration * pdec)
nexp->mRight = dec->mValue; nexp->mRight = dec->mValue;
} }
}
else
{
nexp = new Expression(dec->mLocation, EX_VARIABLE);
nexp->mDecValue = dec;
nexp->mDecType = dec->mBase;
}
if (!exp) if (!exp)
exp = nexp; exp = nexp;
@ -4461,7 +4476,6 @@ Expression* Parser::ParseDeclarationExpression(Declaration * pdec)
rexp = rexp->mRight; rexp = rexp->mRight;
rexp->mLeft = nexp; rexp->mLeft = nexp;
} }
}
dec = dec->mNext; dec = dec->mNext;
} }
@ -5213,7 +5227,13 @@ Expression* Parser::ParseSimpleExpression(bool lhs)
} }
break; break;
case TK_OPEN_BRACKET: case TK_OPEN_BRACKET:
if (mCompilerOptions & COPT_CPLUSPLUS)
exp = ParseLambdaExpression(); exp = ParseLambdaExpression();
else
{
mErrors->Error(mScanner->mLocation, EERR_SYNTAX, "Term starts with invalid token", TokenNames[mScanner->mToken]);
mScanner->NextToken();
}
break; break;
case TK_ASM: case TK_ASM:
mScanner->NextToken(); mScanner->NextToken();
@ -7646,10 +7666,184 @@ Expression* Parser::ParseStatement(void)
Expression* initExp = nullptr, * iterateExp = nullptr, * conditionExp = nullptr, * bodyExp = nullptr, * finalExp = nullptr; Expression* initExp = nullptr, * iterateExp = nullptr, * conditionExp = nullptr, * bodyExp = nullptr, * finalExp = nullptr;
// Assignment // Assignment
if (mScanner->mToken != TK_SEMICOLON) if (mScanner->mToken != TK_SEMICOLON)
initExp = CleanupExpression(ParseExpression(true)); initExp = CleanupExpression(ParseExpression(true));
if ((mCompilerOptions & COPT_CPLUSPLUS) && ConsumeTokenIf(TK_COLON))
{
Expression* containerExp = ParseExpression(false);
if (initExp->mType == EX_VARIABLE)
{
if (containerExp->mDecType->mType == DT_TYPE_ARRAY)
{
Declaration* iterVarDec = new Declaration(mScanner->mLocation, DT_VARIABLE);
iterVarDec->mBase = containerExp->mDecType->mBase->BuildPointer(mScanner->mLocation);
iterVarDec->mVarIndex = mLocalIndex++;
iterVarDec->mSize = iterVarDec->mBase->mSize;
iterVarDec->mFlags |= DTF_DEFINED;
Declaration* endVarDec = new Declaration(mScanner->mLocation, DT_VARIABLE);
endVarDec->mBase = iterVarDec->mBase;
endVarDec->mVarIndex = mLocalIndex++;
endVarDec->mSize = endVarDec->mBase->mSize;
endVarDec->mFlags |= DTF_DEFINED;
Declaration* valueVarDec = initExp->mDecValue;
if (valueVarDec->mBase->mType == DT_TYPE_AUTO)
valueVarDec->mBase = containerExp->mDecType->mBase;
initExp = new Expression(mScanner->mLocation, EX_LIST);
initExp->mLeft = new Expression(mScanner->mLocation, EX_INITIALIZATION);
initExp->mLeft->mToken = TK_ASSIGN;
initExp->mLeft->mLeft = new Expression(mScanner->mLocation, EX_VARIABLE);
initExp->mLeft->mLeft->mDecType = iterVarDec->mBase;
initExp->mLeft->mLeft->mDecValue = iterVarDec;
initExp->mLeft->mRight = containerExp;
initExp->mRight = new Expression(mScanner->mLocation, EX_INITIALIZATION);
initExp->mRight->mToken = TK_ASSIGN;
initExp->mRight->mLeft = new Expression(mScanner->mLocation, EX_VARIABLE);
initExp->mRight->mLeft->mDecType = endVarDec->mBase;
initExp->mRight->mLeft->mDecValue = endVarDec;
initExp->mRight->mRight = new Expression(mScanner->mLocation, EX_BINARY);
initExp->mRight->mRight->mToken = TK_ADD;
initExp->mRight->mRight->mDecType = iterVarDec->mBase;
initExp->mRight->mRight->mLeft = new Expression(mScanner->mLocation, EX_VARIABLE);
initExp->mRight->mRight->mLeft->mDecType = iterVarDec->mBase;
initExp->mRight->mRight->mLeft->mDecValue = iterVarDec;
initExp->mRight->mRight->mRight = new Expression(mScanner->mLocation, EX_CONSTANT);
initExp->mRight->mRight->mRight->mDecType = TheSignedIntTypeDeclaration;
initExp->mRight->mRight->mRight->mDecValue = new Declaration(mScanner->mLocation, DT_CONST_INTEGER);
initExp->mRight->mRight->mRight->mDecValue->mBase = TheSignedIntTypeDeclaration;
initExp->mRight->mRight->mRight->mDecValue->mSize = 2;
initExp->mRight->mRight->mRight->mDecValue->mInteger = containerExp->mDecType->mSize;
iterateExp = new Expression(mScanner->mLocation, EX_PREINCDEC);
iterateExp->mToken = TK_INC;
iterateExp->mLeft = new Expression(mScanner->mLocation, EX_VARIABLE);
iterateExp->mLeft->mDecType = iterVarDec->mBase;
iterateExp->mLeft->mDecValue = iterVarDec;
conditionExp = new Expression(mScanner->mLocation, EX_RELATIONAL);
conditionExp->mToken = TK_NOT_EQUAL;
conditionExp->mLeft = new Expression(mScanner->mLocation, EX_VARIABLE);
conditionExp->mLeft->mDecType = iterVarDec->mBase;
conditionExp->mLeft->mDecValue = iterVarDec;
conditionExp->mRight = new Expression(mScanner->mLocation, EX_VARIABLE);
conditionExp->mRight->mDecType = endVarDec->mBase;
conditionExp->mRight->mDecValue = endVarDec;
bodyExp = new Expression(mScanner->mLocation, EX_ASSIGNMENT);
bodyExp->mToken = TK_ASSIGN;
bodyExp->mLeft = new Expression(mScanner->mLocation, EX_VARIABLE);
bodyExp->mLeft->mDecType = valueVarDec->mBase;
bodyExp->mLeft->mDecValue = valueVarDec;
bodyExp->mRight = new Expression(mScanner->mLocation, EX_PREFIX);
bodyExp->mRight->mToken = TK_MUL;
bodyExp->mRight->mDecType = containerExp->mDecType->mBase;
bodyExp->mRight->mLeft = new Expression(mScanner->mLocation, EX_VARIABLE);
bodyExp->mRight->mLeft->mDecType = iterVarDec->mBase;
bodyExp->mRight->mLeft->mDecValue = iterVarDec;
}
else if (containerExp->mDecType->mType == DT_TYPE_STRUCT)
{
Declaration* fbegin = containerExp->mDecType->mScope->Lookup(Ident::Unique("begin"));
Declaration* fend = containerExp->mDecType->mScope->Lookup(Ident::Unique("end"));
if (fbegin && fend)
{
Expression* cexp = new Expression(mScanner->mLocation, EX_PREFIX);
cexp->mToken = TK_BINARY_AND;
cexp->mDecType = containerExp->mDecType->BuildPointer(mScanner->mLocation);
cexp->mLeft = containerExp;
Declaration* iterVarDec = new Declaration(mScanner->mLocation, DT_VARIABLE);
iterVarDec->mBase = fbegin->mBase->mBase->NonRefBase();
iterVarDec->mVarIndex = mLocalIndex++;
iterVarDec->mSize = iterVarDec->mBase->mSize;
iterVarDec->mFlags |= DTF_DEFINED;
Declaration* endVarDec = new Declaration(mScanner->mLocation, DT_VARIABLE);
endVarDec->mBase = fend->mBase->mBase->NonRefBase();
endVarDec->mVarIndex = mLocalIndex++;
endVarDec->mSize = endVarDec->mBase->mSize;
endVarDec->mFlags |= DTF_DEFINED;
Declaration* valueVarDec = initExp->mDecValue;
initExp = new Expression(mScanner->mLocation, EX_LIST);
initExp->mLeft = new Expression(mScanner->mLocation, EX_INITIALIZATION);
initExp->mLeft->mToken = TK_ASSIGN;
initExp->mLeft->mLeft = new Expression(mScanner->mLocation, EX_VARIABLE);
initExp->mLeft->mLeft->mDecType = iterVarDec->mBase;
initExp->mLeft->mLeft->mDecValue = iterVarDec;
initExp->mLeft->mRight = new Expression(mScanner->mLocation, EX_CALL);
initExp->mLeft->mRight->mDecType = fbegin->mBase->mBase;
initExp->mLeft->mRight->mLeft = new Expression(mScanner->mLocation, EX_CONSTANT);
initExp->mLeft->mRight->mLeft->mDecType = fbegin->mBase;
initExp->mLeft->mRight->mLeft->mDecValue = fbegin;
initExp->mLeft->mRight->mRight = cexp;
initExp->mRight = new Expression(mScanner->mLocation, EX_INITIALIZATION);
initExp->mRight->mToken = TK_ASSIGN;
initExp->mRight->mLeft = new Expression(mScanner->mLocation, EX_VARIABLE);
initExp->mRight->mLeft->mDecType = endVarDec->mBase;
initExp->mRight->mLeft->mDecValue = endVarDec;
initExp->mRight->mRight = new Expression(mScanner->mLocation, EX_CALL);
initExp->mRight->mRight->mDecType = fend->mBase->mBase;
initExp->mRight->mRight->mLeft = new Expression(mScanner->mLocation, EX_CONSTANT);
initExp->mRight->mRight->mLeft->mDecType = fend->mBase;
initExp->mRight->mRight->mLeft->mDecValue = fend;
initExp->mRight->mRight->mRight = cexp;
iterateExp = new Expression(mScanner->mLocation, EX_PREINCDEC);
iterateExp->mToken = TK_INC;
iterateExp->mLeft = new Expression(mScanner->mLocation, EX_VARIABLE);
iterateExp->mLeft->mDecType = iterVarDec->mBase;
iterateExp->mLeft->mDecValue = iterVarDec;
iterateExp = CheckOperatorOverload(iterateExp);
conditionExp = new Expression(mScanner->mLocation, EX_RELATIONAL);
conditionExp->mToken = TK_NOT_EQUAL;
conditionExp->mLeft = new Expression(mScanner->mLocation, EX_VARIABLE);
conditionExp->mLeft->mDecType = iterVarDec->mBase;
conditionExp->mLeft->mDecValue = iterVarDec;
conditionExp->mRight = new Expression(mScanner->mLocation, EX_VARIABLE);
conditionExp->mRight->mDecType = endVarDec->mBase;
conditionExp->mRight->mDecValue = endVarDec;
conditionExp = CheckOperatorOverload(conditionExp);
bodyExp = new Expression(mScanner->mLocation, EX_ASSIGNMENT);
bodyExp->mToken = TK_ASSIGN;
bodyExp->mLeft = new Expression(mScanner->mLocation, EX_VARIABLE);
bodyExp->mRight = new Expression(mScanner->mLocation, EX_PREFIX);
bodyExp->mRight->mToken = TK_MUL;
bodyExp->mRight->mDecType = valueVarDec->mBase;
bodyExp->mRight->mLeft = new Expression(mScanner->mLocation, EX_VARIABLE);
bodyExp->mRight->mLeft->mDecType = iterVarDec->mBase;
bodyExp->mRight->mLeft->mDecValue = iterVarDec;
bodyExp->mRight = CheckOperatorOverload(bodyExp->mRight);
if (valueVarDec->mBase->mType == DT_TYPE_AUTO)
valueVarDec->mBase = bodyExp->mRight->mDecType->NonRefBase();
bodyExp->mLeft->mDecType = valueVarDec->mBase;
bodyExp->mLeft->mDecValue = valueVarDec;
bodyExp = CheckOperatorOverload(bodyExp);
}
else
mErrors->Error(containerExp->mLocation, EERR_INCOMPATIBLE_TYPES, "Array or container expected");
}
else
mErrors->Error(containerExp->mLocation, EERR_INCOMPATIBLE_TYPES, "Array or container expected");
}
else
mErrors->Error(initExp->mLocation, EERR_INCOMPATIBLE_TYPES, "Variable expected");
}
else
{
if (mScanner->mToken == TK_SEMICOLON) if (mScanner->mToken == TK_SEMICOLON)
mScanner->NextToken(); mScanner->NextToken();
else else
@ -7667,11 +7861,21 @@ Expression* Parser::ParseStatement(void)
// Iteration // Iteration
if (mScanner->mToken != TK_CLOSE_PARENTHESIS) if (mScanner->mToken != TK_CLOSE_PARENTHESIS)
iterateExp = CleanupExpression(ParseExpression(false)); iterateExp = CleanupExpression(ParseExpression(false));
if (mScanner->mToken == TK_CLOSE_PARENTHESIS) }
mScanner->NextToken();
ConsumeToken(TK_CLOSE_PARENTHESIS);
Expression * innerExp = ParseStatement();
if (bodyExp)
{
Expression* exp = new Expression(bodyExp->mLocation, EX_SEQUENCE);
exp->mLeft = bodyExp;
exp->mRight = innerExp;
bodyExp = exp;
}
else else
mErrors->Error(mScanner->mLocation, EERR_SYNTAX, "')' expected"); bodyExp = innerExp;
bodyExp = ParseStatement();
mScope->End(mScanner->mLocation); mScope->End(mScanner->mLocation);
exp->mLeft = new Expression(mScanner->mLocation, EX_SEQUENCE); exp->mLeft = new Expression(mScanner->mLocation, EX_SEQUENCE);