Skip to content

Commit

Permalink
refactor: Move swap operator implementation to own lowerer
Browse files Browse the repository at this point in the history
  • Loading branch information
furesoft committed Feb 4, 2025
1 parent 913dabc commit 14104b0
Show file tree
Hide file tree
Showing 6 changed files with 46 additions and 53 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,6 @@ protected override void ListenToNode(BodyCompilation context, BinaryOperatorExpr
{
EmitAssignment(context, node);
}
else if (node.Operator is "<->")
{
EmitSwap(context, node);
}
}

private void EmitAssignment(BodyCompilation context, BinaryOperatorExpression node)
Expand All @@ -37,38 +33,5 @@ private void EmitAssignment(BodyCompilation context, BinaryOperatorExpression no
}
}

private void EmitSwap(BodyCompilation context, BinaryOperatorExpression node)
{
//ToDo: test swap operator
var lvalue = context.Scope.GetFromNode(node.Left);
var rvalue = context.Scope.GetFromNode(node.Right);

if (lvalue is ScopeItem { IsMutable: false } si)
{
node.Left.AddError("Variable '" + si.Name + "' is not mutable");
return;
}

if (rvalue is ScopeItem { IsMutable: false } si2)
{
node.Right.AddError("Variable '" + si2.Name + "' is not mutable");
return;
}

//ToDo: add check if types are compatible

//todo: generalize to allow swapping of fields and parameters too
if (lvalue is VariableScopeItem vsi && rvalue is VariableScopeItem vsi2)
{
var temp = context.Builder.Method.CreateVar(vsi.Type);
context.Builder.CreateStore(temp, vsi.Slot);
context.Builder.CreateStore(vsi.Slot, vsi2.Slot);
context.Builder.CreateStore(vsi2.Slot, temp);
}
}

protected override bool ShouldListenToChildren(BodyCompilation context, BinaryOperatorExpression node)
{
return false;
}
protected override bool ShouldListenToChildren(BodyCompilation context, BinaryOperatorExpression node) => false;
}
Original file line number Diff line number Diff line change
Expand Up @@ -23,4 +23,4 @@ public class AssignmentsLowerer : Replacer<AstNode, BinaryOperatorExpression>
new BinaryOperatorExpression(newOperator, node.Left, node.Right)
);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,9 @@ public static class Lowerer
public static readonly Pipeline<AstNode> Pipeline =
Pipeline<AstNode>
.Build(
builder => { builder.AddStage<AssignmentsLowerer>(); });
builder =>
{
builder.AddStage<AssignmentsLowerer>();
builder.AddStage<SwapLowerer>();
});
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
using MrKWatkins.Ast.Processing;
using Socordia.CodeAnalysis.AST;
using Socordia.CodeAnalysis.AST.Expressions;

namespace SocordiaC.Compilation.Listeners.Body.Lowering;

public class SwapLowerer : Replacer<AstNode, BinaryOperatorExpression>
{
protected override AstNode? ReplaceNode(BinaryOperatorExpression node)
{
if (node.Operator != "<->") return node;

//todo: implement a swap by introducting a new temp variable and test it
var tmpName = Utils.GenerateIdentifier();
var tmp = new VariableStatement(tmpName, node.Left.Type, node.Left, false);

var left = new BinaryOperatorExpression("=", node.Left, node.Right);
var right = new BinaryOperatorExpression("=", node.Right, new Identifier(tmpName));

return new Block([tmp, left, right]);
}
}
16 changes: 16 additions & 0 deletions NewSource/SocordiaC/Compilation/Utils.cs
Original file line number Diff line number Diff line change
Expand Up @@ -163,4 +163,20 @@ public static void EmitAnnotations(Declaration declaration, ModuleEntity entity)
{
foreach (var annotation in declaration.Annotations) EmitAnnotation(annotation, entity);
}

public static string GenerateIdentifier()
{
var sb = new StringBuilder();
sb.Append("<tmp>_");

const string ALPHABET = "abcdefhijklmnopqrstABCDEFGHIJKLMNOPQRSTUVWXYZ&%$";
var random = new Random();

for (var i = 0; i < random.Next(5, 9); i++)
{
sb.Append(ALPHABET[random.Next(ALPHABET.Length)]);
}

return sb.ToString();
}
}
14 changes: 1 addition & 13 deletions Source/Backlang.Driver/Utils.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,19 +6,7 @@ namespace Backlang.Driver;

public static class Utils
{
public static string GenerateIdentifier()
{
var sb = new StringBuilder();
const string ALPHABET = "abcdefhijklmnopqrstABCDEFGHIJKLMNOPQRSTUVWXYZ&%$";
var random = new Random();

for (var i = 0; i < random.Next(5, 9); i++)
{
sb.Append(ALPHABET[random.Next(ALPHABET.Length)]);
}

return sb.ToString();
}


public static void AddCompilerGeneratedAttribute(TypeResolver binder, DescribedType type)
{
Expand Down

0 comments on commit 14104b0

Please sign in to comment.