Skip to content

Commit

Permalink
refactor: Cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
furesoft committed Feb 5, 2025
1 parent 6b3a1a2 commit b361b77
Show file tree
Hide file tree
Showing 7 changed files with 31 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
using MrKWatkins.Ast.Listening;
using Socordia.CodeAnalysis.AST;
using Socordia.CodeAnalysis.AST.Statements;
using Socordia.Compilation;

namespace SocordiaC.Compilation.Listeners.Body;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,14 @@ public class AssignmentsLowerer : Replacer<AstNode, BinaryOperatorExpression>
{
if (!ShortAssignmentOperators.Contains(node.Operator)) return node;

var left = node.Left;
var right = node.Right;
node.Left.RemoveFromParent();
node.Right.RemoveFromParent();

var newOperator = node.Operator.Replace("=", "");
return new BinaryOperatorExpression("=", node.Left,
new BinaryOperatorExpression(newOperator, node.Left, node.Right)
return new BinaryOperatorExpression("=", left,
new BinaryOperatorExpression(newOperator, left, right)
);
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
using MrKWatkins.Ast.Processing;
using Socordia.CodeAnalysis.AST;
using Socordia.CodeAnalysis.AST.Expressions;
using Socordia.CodeAnalysis.AST.Statements;
using Socordia.CodeAnalysis.AST.TypeNames;

namespace SocordiaC.Compilation.Listeners.Body.Lowering;

Expand All @@ -10,12 +12,15 @@ public class SwapLowerer : Replacer<AstNode, BinaryOperatorExpression>
{
if (node.Operator != "<->") return node;

var leftId = (Identifier)node.Left;
var rightId = (Identifier)node.Right;

//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 tmp = new VariableStatement(tmpName, new NoTypeName(), new Identifier(leftId.Name), false);

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

return new Block([tmp, left, right]);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ public class VariableDeclarationListener : Listener<BodyCompilation, AstNode, Va
protected override void ListenToNode(BodyCompilation context, VariableStatement node)
{
var value = Utils.CreateValue(node.Initializer, context);
if (value is Instruction instr) context.Builder.Emit(instr);
if (value is Instruction { Block: null } instr) context.Builder.Emit(instr);

TypeDesc type;
if (node.Type is not NoTypeName)
Expand All @@ -24,6 +24,11 @@ protected override void ListenToNode(BodyCompilation context, VariableStatement
else
{
type = value.ResultType;

if (value is LocalSlot s)
{
// type = s.Type;
}
}

var slot = context.Method.Body!.CreateVar(type, node.Name);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
using MrKWatkins.Ast.Listening;
using Socordia.CodeAnalysis.AST;
using Socordia.CodeAnalysis.AST.Statements.Loops;
using Socordia.Compilation;

namespace SocordiaC.Compilation.Listeners.Body;

Expand Down
6 changes: 3 additions & 3 deletions NewSource/SocordiaC/SocordiaC.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
<PackAsTool>true</PackAsTool>
<ToolCommandName>scc</ToolCommandName>
<CopyLocalLockFileAssemblies>true</CopyLocalLockFileAssemblies>
<ApplicationIcon>logo.png</ApplicationIcon>
<ApplicationIcon>icon.ico</ApplicationIcon>
</PropertyGroup>

<ItemGroup>
Expand All @@ -19,10 +19,10 @@
</ItemGroup>

<ItemGroup>
<None Update="compilation.sc">
<None Update="compilation.scs">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</None>
<None Update="hello.sc">
<None Update="hello.scs">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</None>
</ItemGroup>
Expand Down
7 changes: 5 additions & 2 deletions NewSource/SocordiaC/hello.scs
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@

func main()
{
let x = 12;
let y = 5;
let mut x = 12;
let mut y = 5;

x <-> y;

print(x);
print(y);
}

0 comments on commit b361b77

Please sign in to comment.