Skip to content

Commit

Permalink
Range for loop with reference iteration
Browse files Browse the repository at this point in the history
  • Loading branch information
drmortalwombat committed Sep 13, 2023
1 parent eccb278 commit 532bf51
Show file tree
Hide file tree
Showing 5 changed files with 106 additions and 23 deletions.
24 changes: 24 additions & 0 deletions oscar64/Declaration.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -964,6 +964,30 @@ Declaration* Declaration::NonRefBase(void)
return this;
}

Declaration* Declaration::DeduceAuto(Declaration * dec)
{
if (mType == DT_TYPE_AUTO || IsReference() && mBase->mType == DT_TYPE_AUTO)
{
dec = dec->NonRefBase();

if (dec->mType == DT_TYPE_ARRAY)
dec = dec->mBase->BuildPointer(mLocation);

if (mFlags & DTF_CONST)
dec = dec->ToConstType();
else
dec = dec->ToMutableType();

if (IsReference())
dec = dec->BuildReference(mLocation);

return dec;
}
else
return this;
}


Declaration* Declaration::BuildConstRValueRef(const Location& loc)
{
Declaration* pdec = new Declaration(loc, DT_TYPE_RVALUEREF);
Expand Down
1 change: 1 addition & 0 deletions oscar64/Declaration.h
Original file line number Diff line number Diff line change
Expand Up @@ -310,6 +310,7 @@ class Declaration
Declaration* BuildRValueRef(const Location& loc);
Declaration* BuildConstRValueRef(const Location& loc);
Declaration* NonRefBase(void);
Declaration* DeduceAuto(Declaration* dec);

DecType ValueType(void) const;

Expand Down
2 changes: 1 addition & 1 deletion oscar64/InterCode.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -17102,7 +17102,7 @@ void InterCodeProcedure::Close(void)
{
GrowingTypeArray tstack(IT_NONE);

CheckFunc = !strcmp(mIdent->mString, "main");
CheckFunc = !strcmp(mIdent->mString, "test");

mEntryBlock = mBlocks[0];

Expand Down
12 changes: 9 additions & 3 deletions oscar64/InterCodeGenerator.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2034,13 +2034,19 @@ InterCodeGenerator::ExValue InterCodeGenerator::TranslateExpression(Declaration*

if (vl.mType->IsReference())
{
vr = Dereference(proc, exp, block, vr, 1);
if (vr.mType->IsReference())
vr = Dereference(proc, exp, block, vr, 0);
else
{
vr = Dereference(proc, exp, block, vr, 1);
if (vr.mReference != 1)
mErrors->Error(exp->mLeft->mLocation, EERR_NOT_AN_LVALUE, "Not an addressable expression");
}

vl = Dereference(proc, exp, block, vl, 2);

if (vl.mReference != 2)
mErrors->Error(exp->mLeft->mLocation, EERR_NOT_AN_LVALUE, "Not a left hand reference expression");
if (vr.mReference != 1)
mErrors->Error(exp->mLeft->mLocation, EERR_NOT_AN_LVALUE, "Not an addressable expression");

if (vr.mTemp != vl.mTemp)
{
Expand Down
90 changes: 71 additions & 19 deletions oscar64/Parser.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4168,20 +4168,7 @@ Declaration* Parser::ParseDeclaration(Declaration * pdec, bool variable, bool ex
if (ConsumeTokenIf(TK_ASSIGN))
{
ndec->mValue = ParseInitExpression(ndec->mBase);
if (ndec->mBase->mType == DT_TYPE_AUTO)
{
if (ndec->mBase->mFlags & DTF_CONST)
ndec->mBase = ndec->mValue->mDecType->ToConstType();
else
ndec->mBase = ndec->mValue->mDecType;

if (ndec->mBase->mType == DT_TYPE_ARRAY)
{
ndec->mBase = ndec->mBase->Clone();
ndec->mBase->mType = DT_TYPE_POINTER;
ndec->mBase->mSize = 2;
}
}
ndec->mBase = ndec->mBase->DeduceAuto(ndec->mValue->mDecType);

if (ndec->mFlags & DTF_GLOBAL)
{
Expand Down Expand Up @@ -7055,6 +7042,71 @@ Expression* Parser::CheckOperatorOverload(Expression* exp)
}
}
}
else if (exp->mType == EX_INITIALIZATION)
{
Declaration* tdec = exp->mLeft->mDecType;
if (tdec->mType == DT_TYPE_STRUCT && exp->mToken == TK_ASSIGN)
{
Declaration* fcons = tdec->mScope ? tdec->mScope->Lookup(tdec->mIdent->PreMangle("+"), SLEVEL_CLASS) : nullptr;
if (fcons)
{
Declaration* mtype = tdec->ToMutableType();

Expression* vexp = exp->mLeft;

Expression* cexp = new Expression(mScanner->mLocation, EX_CONSTANT);
cexp->mDecValue = fcons;
cexp->mDecType = cexp->mDecValue->mBase;

Expression* fexp = new Expression(mScanner->mLocation, EX_CALL);
fexp->mLeft = cexp;
fexp->mRight = exp->mRight;

Expression* texp = new Expression(mScanner->mLocation, EX_PREFIX);
texp->mToken = TK_BINARY_AND;
texp->mLeft = vexp;
texp->mDecType = new Declaration(mScanner->mLocation, DT_TYPE_POINTER);
texp->mDecType->mFlags |= DTF_CONST | DTF_DEFINED;
texp->mDecType->mBase = mtype;
texp->mDecType->mSize = 2;

if (fexp->mRight)
{
Expression* lexp = new Expression(mScanner->mLocation, EX_LIST);
lexp->mLeft = texp;
lexp->mRight = fexp->mRight;
fexp->mRight = lexp;
}
else
fexp->mRight = texp;

fexp = ResolveOverloadCall(fexp);

Expression* dexp = nullptr;
if (tdec->mDestructor)
{
Expression* cexp = new Expression(mScanner->mLocation, EX_CONSTANT);
cexp->mDecValue = tdec->mDestructor;
cexp->mDecType = cexp->mDecValue->mBase;

dexp = new Expression(mScanner->mLocation, EX_CALL);
dexp->mLeft = cexp;
dexp->mRight = texp;
}

Expression* nexp = new Expression(mScanner->mLocation, EX_CONSTRUCT);

nexp->mLeft = new Expression(mScanner->mLocation, EX_LIST);
nexp->mLeft->mLeft = fexp;
nexp->mLeft->mRight = dexp;

nexp->mRight = vexp;
nexp->mDecType = vexp->mDecType;

exp = nexp;
}
}
}
else if (exp->mType == EX_INDEX)
{
Declaration* tdec = exp->mLeft->mDecType;
Expand Down Expand Up @@ -7690,8 +7742,8 @@ Expression* Parser::ParseStatement(void)
endVarDec->mFlags |= DTF_DEFINED;

Declaration* valueVarDec = initExp->mDecValue;
if (valueVarDec->mBase->mType == DT_TYPE_AUTO)
valueVarDec->mBase = containerExp->mDecType->mBase;
valueVarDec->mBase = valueVarDec->mBase->DeduceAuto(containerExp->mDecType->mBase);
valueVarDec->mSize = valueVarDec->mBase->mSize;

initExp = new Expression(mScanner->mLocation, EX_LIST);
initExp->mLeft = new Expression(mScanner->mLocation, EX_INITIALIZATION);
Expand Down Expand Up @@ -7814,7 +7866,7 @@ Expression* Parser::ParseStatement(void)
conditionExp->mRight->mDecValue = endVarDec;
conditionExp = CheckOperatorOverload(conditionExp);

bodyExp = new Expression(mScanner->mLocation, EX_ASSIGNMENT);
bodyExp = new Expression(mScanner->mLocation, EX_INITIALIZATION);
bodyExp->mToken = TK_ASSIGN;
bodyExp->mLeft = new Expression(mScanner->mLocation, EX_VARIABLE);

Expand All @@ -7826,8 +7878,8 @@ Expression* Parser::ParseStatement(void)
bodyExp->mRight->mLeft->mDecValue = iterVarDec;
bodyExp->mRight = CheckOperatorOverload(bodyExp->mRight);

if (valueVarDec->mBase->mType == DT_TYPE_AUTO)
valueVarDec->mBase = bodyExp->mRight->mDecType->NonRefBase();
valueVarDec->mBase = valueVarDec->mBase->DeduceAuto(bodyExp->mRight->mDecType);
valueVarDec->mSize = valueVarDec->mBase->mSize;

bodyExp->mLeft->mDecType = valueVarDec->mBase;
bodyExp->mLeft->mDecValue = valueVarDec;
Expand Down

0 comments on commit 532bf51

Please sign in to comment.