Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement Concat and Unary operations. #45

Merged
merged 1 commit into from
Apr 4, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/Unluau.CLI/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ internal class Program
{
static void Main(string[] args)
{
using var stream = File.OpenRead("./test/input.txt");
using var stream = File.OpenRead("./test/Unary.luau");
var chunk = LuauChunk.Create(stream);

Console.WriteLine(chunk.ToString());
Expand Down
29 changes: 29 additions & 0 deletions src/Unluau/Chunk/Luau/Function.cs
Original file line number Diff line number Diff line change
Expand Up @@ -526,6 +526,35 @@ OpCode.LOADKX or
block.Statements.Add(new Move(context, ra, rb));
break;
}
case OpCode.CONCAT:
{
var values = new Reference[instruction.C - instruction.B + 1];

for ( var i = instruction.B; i < instruction.C + 1; ++i )
{
values[i - instruction.B] = new Reference(context, stack.Get(i)!);
}

stack.Set(instruction.A, new Concat(context, values));

break;
}
case OpCode.LENGTH:
case OpCode.MINUS:
case OpCode.NOT:
{
var type = instruction.Code switch
{
OpCode.LENGTH => UnaryType.Length,
OpCode.MINUS => UnaryType.Minus,
OpCode.NOT => UnaryType.Not,
_ => throw new NotSupportedException()
};

stack.Set(instruction.A, new Unary(context, type, new Reference(context, stack.Get(instruction.B)!)));

break;
}
case OpCode.JUMPIFEQ:
case OpCode.JUMPIFLE:
case OpCode.JUMPIFLT:
Expand Down
43 changes: 43 additions & 0 deletions src/Unluau/IL/Values/Concat.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Unluau.Utils;

namespace Unluau.IL.Values
{
/// <summary>
/// Concatenates values in registers.
/// </summary>
/// <param name="context">Information about the location.</param>
/// <param name="registers">The registers that are concatenated together.</param>
public class Concat(Context context, BasicValue[] registers) : BasicValue(context)
{
/// <summary>
/// The registers that are concatenated together.
/// </summary>
public BasicValue[] Registers { get; set; } = registers;

/// <summary>
/// Implements the visitor.
/// </summary>
/// <param name="visitor">The visitor.</param>
public override void Visit(Visitor visitor)
{
if (visitor.Visit(this))
{
foreach (var reg in Registers)
{
reg.Visit(visitor);
}
}
}

/// <summary>
/// Converts the current <see cref="Concat"/> to a string.
/// </summary>
/// <returns>String representation.</returns>
public override string ToString() => $"Concat({TypeExtensions.ToString(Registers)})";
}
}
53 changes: 53 additions & 0 deletions src/Unluau/IL/Values/Unary.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Unluau.Utils;

namespace Unluau.IL.Values
{
public enum UnaryType
{
Not,
Minus,
Length
}

/// <summary>
/// Applies unary operation to a register.
/// </summary>
/// <param name="context">Information about the location.</param>
/// <param name="type">The type of unary operation.</param>
/// <param name="value">The register that the unary operation is applied to.</param>
public class Unary(Context context, UnaryType type, BasicValue value) : BasicValue(context)
{
/// <summary>
/// The type of unary operation.
/// </summary>
public UnaryType Type { get; set; } = type;

/// <summary>
/// The register that the unary operation is applied to.
/// </summary>
public BasicValue Value { get; set; } = value;

/// <summary>
/// Implements the visitor.
/// </summary>
/// <param name="visitor">The visitor.</param>
public override void Visit(Visitor visitor)
{
if (visitor.Visit(this))
{
Value.Visit(visitor);
}
}

/// <summary>
/// Converts the current <see cref="Unary"/> to a string.
/// </summary>
/// <returns>String representation.</returns>
public override string ToString() => $"{Enum.GetName(typeof(UnaryType), Type)}({TypeExtensions.ToString(Value)})";
}
}
6 changes: 6 additions & 0 deletions src/Unluau/IL/Visitors/ValueVisitor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -167,6 +167,12 @@ private static BasicValue ResolveValue(BasicValue value)
if (value is Table table)
ResolveTable(table);

if (value is Concat concat)
concat.Registers = ResolveValueList(concat.Registers);

if (value is Unary unary)
unary.Value = ResolveValue(unary.Value);

return value;
}

Expand Down
Loading