Skip to content

Commit

Permalink
added break
Browse files Browse the repository at this point in the history
  • Loading branch information
EthanSK committed Mar 26, 2019
1 parent 8c2bd65 commit e192fab
Show file tree
Hide file tree
Showing 5 changed files with 20 additions and 65 deletions.
24 changes: 6 additions & 18 deletions src/ast/conditions/switch/switchStatement.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ void SwitchStatement::generateIL(std::vector<Instr> &instrs, ILContext &context,
ScopeBlockPtr switchBlock = Utils::tryCast<ScopeBlock>(getSwitchBlock(), "switch block must be a scope block"); //just to get access to branches
std::vector<StatementPtr> switchBlockBranches = switchBlock->getBranches();
std::string switchEnd_lb = context.makeName("switchEnd"); //the a in switch (a)
context.pushLoopLabels("NULL", switchEnd_lb);

//we could set up labels and scope blocks first, the branch past all that, then beq back...nah would mess up at the end.

Expand All @@ -44,7 +45,7 @@ void SwitchStatement::generateIL(std::vector<Instr> &instrs, ILContext &context,

try
{ //is default
SwitchDefaultPtr switchDefault = Utils::tryCast<SwitchDefault>(switchBlockBranches[i], "node is not a default class");
Utils::tryCast<SwitchDefault>(switchBlockBranches[i], "node is not a default class");
hasDefault = true;
branchToDefaultInstr = Instr("b", case_lb);
}
Expand All @@ -57,7 +58,7 @@ void SwitchStatement::generateIL(std::vector<Instr> &instrs, ILContext &context,
labels.push_back(case_lb); //even if it's from default, it should stay in place as if there is no break it can continue executing the other lines
}
catch (std::string)
{ //not a switch case
{ //not a switch case or default
}
}
if (hasDefault)
Expand All @@ -72,31 +73,18 @@ void SwitchStatement::generateIL(std::vector<Instr> &instrs, ILContext &context,
{
//if there is corresponding label for node, make label
try
{ //is a switch case
{ //is a switch case or default
SwitchCasePtr switchCase = Utils::tryCast<SwitchCase>(switchBlockBranches[i], "node is not a switch case");
std::string label = labels[labelIndex++];
instrs.push_back(Instr::makeLabel(label));
switchCase->getScopeBlock()->generateIL(instrs, context, destReg);
//need to somehow store the label for later us
}
catch (std::string)
{ //not a switch case
{ //not a switch case or default
switchBlockBranches[i]->generateIL(instrs, context, destReg);
}
}
instrs.push_back(Instr::makeLabel(switchEnd_lb));

//then after we have set all the labels with their blocks, we need to do an eq to jump to the correct block
//we can do a beq as it is in our IL spec.
//we need to beq to the label if the switch case constant value is eq to the getCase constant value .that should actually be done i nthe for loop as we need to check it for each case.

// context.compileInput(getCase(), instrs, "$t0"); //case we wanna match against is stored in t0. we wanna do an eq
// instrs.push_back(Instr("eq", "$t1", "$t0"));

//wont work
// context.pushSwitchCase(getCase());

// getSwitchBlock()->generateIL(instrs, context, destReg); //will be switchBlock implementeation of genIL, which loops through all VALID cases and genIL on those

// context.popSwitchCase();
context.popLoopLabels();
}
4 changes: 2 additions & 2 deletions src/ast/keywords/continueKeyword.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,6 @@ void ContinueKeyword::printC(std::ostream &os) const
void ContinueKeyword::generateIL(std::vector<Instr> &instrs, ILContext &context, std::string destReg) const
{
//need to jump to label of begining of function, which can be found in context
std::string endLoopLabel = std::get<0>(context.getLastLoopLabel());
instrs.push_back(Instr("b", endLoopLabel, "", "", {"#continue"}));
std::string startLoopLabel = std::get<0>(context.getLastLoopLabel());
instrs.push_back(Instr("b", startLoopLabel, "", "", {"#continue"}));
}
4 changes: 2 additions & 2 deletions test/compiler/tests/conditionals/switch_break_driver.c
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
int switch_break_driver();
int switch_break();

int main()
{
return !(switch_break_driver() == 70);
return !(switch_break() == 70);
}
8 changes: 2 additions & 6 deletions test/parser/testProgram.c
Original file line number Diff line number Diff line change
@@ -1,15 +1,11 @@
int main()
{
int a = 234;
int a = 69;
switch (a)
{
default:

case 69:
return a;
case 79:
return a;
return 70;
return 70;
}
return 80;
}
45 changes: 8 additions & 37 deletions test/parser/testProgram2.c
Original file line number Diff line number Diff line change
@@ -1,42 +1,13 @@
int x = 5;
int a1, a2, a3;
int* yy;
auto int a12;
extern auto int a15;
extern a20;
register a50;

double a = 5.5f;

int getNum();
int factorial(int num);
getBigNum();
dumbFactoria(int num);

int main()
{
if (2 > 1)
int a = 69;
switch (a)
{

case 69:
a++;
break;
case 90:
a += 100;
}

if (3 > 2)
{
int xxx = 10;
}
return 0;
return a;
}

int getNum() { return 10; }

int factorial(int num)
{
if (num == 0) { return 1; }
else { return num * factorial(num - 1); }
}

getBigNum() { return 100; }
dumbFactorial(int num)
{
return num == 0 ? 1 : num * dumbFactorial(num - 1);
}

0 comments on commit e192fab

Please sign in to comment.