Skip to content

Commit

Permalink
Merge aafa429 into ebefcd5
Browse files Browse the repository at this point in the history
  • Loading branch information
deavmi committed May 19, 2024
2 parents ebefcd5 + aafa429 commit 39aa9a3
Show file tree
Hide file tree
Showing 8 changed files with 83 additions and 38 deletions.
19 changes: 15 additions & 4 deletions .github/workflows/d.yml
Original file line number Diff line number Diff line change
Expand Up @@ -451,10 +451,21 @@ jobs:
run: |
./tlang compile source/tlang/testing/simple_while.t
./tlang.out
#- name: Simple do-while
# run: |
# ./tlang compile source/tlang/testing/simple_do_while.t
# ./tlang.out
- name: Simple do-while
run: |
./tlang compile source/tlang/testing/simple_do_while.t
set +e
./tlang.out
if [ $? = 6 ]
then
set -e
exit 0
else
set -e
exit 1
fi
- name: Simple for-loops
run: |
./tlang compile source/tlang/testing/simple_for_loops.t
Expand Down
39 changes: 30 additions & 9 deletions source/tlang/compiler/codegen/emit/dgen.d
Original file line number Diff line number Diff line change
Expand Up @@ -462,18 +462,39 @@ public final class DCodeEmitter : CodeEmitter

string emit;

/* Generate the `while(<expr>)` and opening curly brace */
emit = "while("~transform(conditionInstr)~")\n";
emit~=genTabs(transformDepth)~"{\n";

/* Transform each body statement */
foreach(Instruction curBodyInstr; bodyInstructions)
/* While-loop */
if(!whileLoopInstr.isDoWhileLoop())
{
emit~=genTabs(transformDepth)~"\t"~transform(curBodyInstr)~"\n";
/* Generate the `while(<expr>)` and opening curly brace */
emit = "while("~transform(conditionInstr)~")\n";
emit~=genTabs(transformDepth)~"{\n";

/* Transform each body statement */
foreach(Instruction curBodyInstr; bodyInstructions)
{
emit~=genTabs(transformDepth)~"\t"~transform(curBodyInstr)~"\n";
}

/* Closing curly brace */
emit~=genTabs(transformDepth)~"}";
}
/* Do-while loop */
else
{
/* Generate `do {` */
emit = "do\n";
emit~=genTabs(transformDepth)~"{\n";

/* Closing curly brace */
emit~=genTabs(transformDepth)~"}";
/* Transform each body statement */
foreach(Instruction curBodyInstr; bodyInstructions)
{
emit~=genTabs(transformDepth)~"\t"~transform(curBodyInstr)~"\n";
}

/* Closing curly brace and `while(<expr>)` */
emit~=genTabs(transformDepth)~"}\n";
emit~=genTabs(transformDepth)~"while("~transform(conditionInstr)~");";
}

emmmmit = emit;
}
Expand Down
10 changes: 9 additions & 1 deletion source/tlang/compiler/codegen/instruction.d
Original file line number Diff line number Diff line change
Expand Up @@ -405,10 +405,12 @@ public final class IfStatementInstruction : Instruction
public final class WhileLoopInstruction : Instruction
{
private BranchInstruction branchInstruction;
private bool isDoWhile;

this(BranchInstruction branchInstruction)
this(BranchInstruction branchInstruction, bool isDoWhile = false)
{
this.branchInstruction = branchInstruction;
this.isDoWhile = isDoWhile;

addInfo = "Branch: "~to!(string)(branchInstruction);
}
Expand All @@ -417,6 +419,12 @@ public final class WhileLoopInstruction : Instruction
{
return branchInstruction;
}

public bool isDoWhileLoop()
{
return this.isDoWhile;
}

}

public final class ForLoopInstruction : Instruction
Expand Down
1 change: 1 addition & 0 deletions source/tlang/compiler/core.d
Original file line number Diff line number Diff line change
Expand Up @@ -398,6 +398,7 @@ unittest
"source/tlang/testing/simple_variables_only_decs.t",
"source/tlang/testing/simple_variables_decls_ass.t",
"source/tlang/testing/simple_while.t",
"source/tlang/testing/simple_do_while.t",

"source/tlang/testing/simple_for_loops.t",
"source/tlang/testing/simple_cast.t",
Expand Down
7 changes: 6 additions & 1 deletion source/tlang/compiler/symbols/data.d
Original file line number Diff line number Diff line change
Expand Up @@ -1277,7 +1277,7 @@ public final class WhileLoop : Entity, Container
{
private Branch branch;
private static ulong whileStmtContainerRollingNameCounter = 0;
public const bool isDoWhile;
private bool isDoWhile;

/**
* Creates a new While Loop parser node, optionally specifying
Expand Down Expand Up @@ -1378,6 +1378,11 @@ public final class WhileLoop : Entity, Container
return branch.replace(thiz, that);
}
}

public bool isDoWhileLoop()
{
return this.isDoWhile;
}
}

public final class ForLoop : Entity, Container
Expand Down
10 changes: 1 addition & 9 deletions source/tlang/compiler/typecheck/core.d
Original file line number Diff line number Diff line change
Expand Up @@ -2623,14 +2623,6 @@ public final class TypeChecker
else if(cast(WhileLoop)statement)
{
WhileLoop whileLoop = cast(WhileLoop)statement;

// FIXME: Do-while loops are still being considered in terms of dependency construction
if(whileLoop.isDoWhile)
{
DEBUG("Still looking at dependency construction in this thing (do while loops )");
assert(false);
}

Branch branch = whileLoop.getBranch();

/* The condition `Value` instruction should be on the stack */
Expand Down Expand Up @@ -2664,7 +2656,7 @@ public final class TypeChecker
* 2. Set the context
* 3. Add the instruction
*/
WhileLoopInstruction whileLoopInstruction = new WhileLoopInstruction(branchInstr);
WhileLoopInstruction whileLoopInstruction = new WhileLoopInstruction(branchInstr, whileLoop.isDoWhileLoop());
whileLoopInstruction.setContext(whileLoop.getContext());
addInstrB(whileLoopInstruction);
}
Expand Down
24 changes: 13 additions & 11 deletions source/tlang/compiler/typecheck/dependency/core.d
Original file line number Diff line number Diff line change
Expand Up @@ -1325,8 +1325,10 @@ public class DNodeGenerator
DNode branchDNode = pool(whileBranch);
DEBUG("Branch: "~to!(string)(whileBranch));

// If this is a while-loop
if(!whileLoopStmt.isDoWhile)
/**
* while-loop
*/
if(!whileLoopStmt.isDoWhileLoop())
{
// Extract the condition
Expression branchCondition = whileBranch.getCondition();
Expand All @@ -1345,13 +1347,15 @@ public class DNodeGenerator
// Finally make the branchDNode depend on the body dnode (above)
branchDNode.needs(branchBodyDNode);
}
// If this is a do-while loop
// TODO: I don't think we really need to reverse this?
// Logically we should, but the typechecker will add this things in the correct order anyways?
// We need to look into this!
// Our nodes at the back will always be placed at the back, and the expression will end ip upfront
// i think it is a problem oif maybe other expressions are left on the stack but is that ever a problem
//now with the statement <-> instruction mapping (like will that ever even occur?)
/**
* do-while loop
*
* The order of dependencies is basically
* switched from [expr, stmts...] to
* [stmts..., expr] but at the end of the
* day the way it is processed by the
* typechecker won't change
*/
else
{
// Pass over the statements in the branch's body
Expand All @@ -1375,8 +1379,6 @@ public class DNodeGenerator
/* Make the while-loop/do-while loop depend on the branchDNode */
whileLoopDNode.needs(branchDNode);

/* Make the node of this generalPass we are in depend on the whileLoop's DNode */
// node.needs(whileLoopDNode);
return whileLoopDNode;
}
/**
Expand Down
11 changes: 8 additions & 3 deletions source/tlang/testing/simple_do_while.t
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,18 @@ module simple_do_while;

int function(int i)
{
int test = 2;
int test = 0;
do
{
i = i - 1;
test = test + i;
}
while(i);
while(i > 1);

return test;
}
}

int main()
{
return function(4);
}

0 comments on commit 39aa9a3

Please sign in to comment.