Skip to content

Commit

Permalink
Merge aa5533e into ebefcd5
Browse files Browse the repository at this point in the history
  • Loading branch information
deavmi authored May 18, 2024
2 parents ebefcd5 + aa5533e commit 2a8f954
Show file tree
Hide file tree
Showing 8 changed files with 71 additions and 28 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
2 changes: 1 addition & 1 deletion source/tlang/compiler/typecheck/dependency/core.d
Original file line number Diff line number Diff line change
Expand Up @@ -1326,7 +1326,7 @@ public class DNodeGenerator
DEBUG("Branch: "~to!(string)(whileBranch));

// If this is a while-loop
if(!whileLoopStmt.isDoWhile)
if(!whileLoopStmt.isDoWhileLoop())
{
// Extract the condition
Expression branchCondition = whileBranch.getCondition();
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 2a8f954

Please sign in to comment.