Skip to content

Commit

Permalink
Merge pull request #188 from Backlang-Org/codespace-furesoft-symmetri…
Browse files Browse the repository at this point in the history
…cal-memory-pwrqrv5j4q2r6px

Pending changes exported from your codespace
  • Loading branch information
furesoft authored Dec 1, 2022
2 parents bc10f05 + 62664c8 commit f71f1b6
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 20 deletions.
4 changes: 2 additions & 2 deletions Source/Backlang-Compiler/compilation.back
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@

func main() {
if true {
return;
return 1;
}
else {
print("else");
return 2;
}

print("after");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,13 +58,13 @@ public static MethodBody CompileBody(LNode function, CompilerContext context, IM

var afterBlock = AppendBlock(function.Args[3], block, context, method, modulename, scope, branchLabels);

if (afterBlock.Flow is NothingFlow)
SetReturnType((DescribedBodyMethod)method, function, context, scope, modulename.Value);

if (afterBlock.Flow is NothingFlow && method.ReturnParameter.Type.FullName.ToString() != "System.Void")
{
afterBlock.Flow = new ReturnFlow();
}

SetReturnType((DescribedBodyMethod)method, function, context, scope, modulename.Value);

return new MethodBody(
method.ReturnParameter,
method.IsStatic ? new Parameter() : Parameter.CreateThisParameter(method.ParentType),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,31 +7,26 @@ public class IfImplementor : IStatementImplementor
{
public BasicBlockBuilder Implement(LNode node, BasicBlockBuilder block, CompilerContext context, IMethod method, QualifiedName? modulename, Scope scope, BranchLabels branchLabels = null)
{
if (node is (_, (_, var condition, var body, var el)))
if (node is (_, (_, var condition, var body, var elseBody)))
{
TypeDeducer.ExpectType(condition, scope, context, modulename.Value,
context.Environment.Boolean);

if (el.Name != LNode.Missing.Name)
{
ImplementIfElse(block, context, method, modulename, scope, branchLabels, condition, body, el);
}
else
var if_after = ImplementIf(block, context, method, modulename, scope, branchLabels, condition, body);

if (elseBody.Name != LNode.Missing.Name)
{
ImplementIf(block, context, method, modulename, scope, branchLabels, condition, body);
if_after = ImplementIf(if_after, context, method, modulename, scope, branchLabels, condition, elseBody, true);
}

var if_after = block.Graph.AddBasicBlock(LabelGenerator.NewLabel("if_after"));
if_after.Flow = new NothingFlow();

return if_after;
}

return null;
}

private static void ImplementIf(BasicBlockBuilder block, CompilerContext context, IMethod method, QualifiedName? modulename,
Scope scope, BranchLabels branchLabels, LNode condition, LNode body)
private static BasicBlockBuilder ImplementIf(BasicBlockBuilder block, CompilerContext context, IMethod method, QualifiedName? modulename,
Scope scope, BranchLabels branchLabels, LNode condition, LNode body, bool negate = false)
{
var if_start = block.Graph.AddBasicBlock(LabelGenerator.NewLabel("if_start"));
var if_condition = block.Graph.AddBasicBlock(LabelGenerator.NewLabel("if_condition"));
Expand All @@ -41,20 +36,25 @@ private static void ImplementIf(BasicBlockBuilder block, CompilerContext context

AppendExpression(if_condition, condition, context.Environment.Boolean, context, scope, modulename);

if_condition.Flow = new JumpConditionalFlow(if_start, ConditionalJumpKind.True);
if_condition.Flow = new JumpConditionalFlow(if_start, negate ? ConditionalJumpKind.False : ConditionalJumpKind.True);

block.Flow = new JumpFlow(if_condition);

if_end.Flow = new NothingFlow();

var if_after = block.Graph.AddBasicBlock(LabelGenerator.NewLabel("if_after"));
if_after.Flow = new NothingFlow();

return if_after;
}

private static BasicBlockBuilder ImplementIfElse(BasicBlockBuilder block, CompilerContext context, IMethod method, QualifiedName? modulename,
Scope scope, BranchLabels branchLabels, LNode condition, LNode body, LNode elseBody)
{
var if_condition = block.Graph.AddBasicBlock(LabelGenerator.NewLabel("if_condition"));
var if_body = block.Graph.AddBasicBlock(LabelGenerator.NewLabel("if_start"));
var else_body = block.Graph.AddBasicBlock(LabelGenerator.NewLabel("else_start"));
var else_end = block.Graph.AddBasicBlock(LabelGenerator.NewLabel("else_end"));
var else_body = block.Graph.AddBasicBlock(LabelGenerator.NewLabel("else_start"));

else_end.Flow = new NothingFlow();

Expand All @@ -68,7 +68,11 @@ private static BasicBlockBuilder ImplementIfElse(BasicBlockBuilder block, Compil
{
if_body.Flow = new JumpFlow(else_end);
}

if (else_body.Flow is NothingFlow)
{
else_body.Flow = new JumpFlow(else_end);
}

return else_end;
}
}

0 comments on commit f71f1b6

Please sign in to comment.