Skip to content

Commit

Permalink
added condition testing ;P
Browse files Browse the repository at this point in the history
  • Loading branch information
atrexus committed Feb 8, 2024
1 parent e9320a9 commit 39ab9aa
Show file tree
Hide file tree
Showing 6 changed files with 65 additions and 16 deletions.
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/Conditions.luau");
using var stream = File.OpenRead("./test/RegisterTest.luau");
var chunk = LuauChunk.Create(stream);

Console.WriteLine(chunk.ToString());
Expand Down
23 changes: 19 additions & 4 deletions src/Unluau/Chunk/Luau/Function.cs
Original file line number Diff line number Diff line change
Expand Up @@ -486,21 +486,36 @@ OpCode.LOADKX or
case OpCode.JUMPIFNOTEQ:
case OpCode.JUMPIFNOTLE:
case OpCode.JUMPIFNOTLT:
case OpCode.JUMPIFNOT:
{
// Save the original pc value so that we can conserve the jump offset.
var ogPc = pc;

var left = new Reference(context, stack.Get(instruction.A)!);
var right = new Reference(context, stack.Get(Instructions[++pc].Value)!);

var right = instruction.Code switch
{
// These operations just test a value and have one operand.
OpCode.JUMPIF or OpCode.JUMPIFNOT => null,

_ => new Reference(context, stack.Get(Instructions[++pc].Value)!)
};

// Build the condition based on the current operation code.
var condition = instruction.Code switch
BasicCondition condition = instruction.Code switch
{
OpCode.JUMPIFNOTEQ => new Equals(context, left, right),
// Performs a jump if values are not equal to each other.
OpCode.JUMPIFNOTEQ => new Equals(context, left, right!),

// Jumps if the register is nil or false.
OpCode.JUMPIFNOT => new Test(context, left),

// This should never happen, but just in case.
_ => throw new NotImplementedException()
};

// Now we lift the body of the block.
var body = LiftBasicBlock(stack, ++pc, pc + instruction.D - 1);
var body = LiftBasicBlock(stack, ++pc, pc + instruction.D - (pc - ogPc - 1));

block.Statements.Add(new IfBlock(body.Context, condition, body.Statements));
break;
Expand Down
33 changes: 33 additions & 0 deletions src/Unluau/IL/Values/Conditions/Test.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
namespace Unluau.IL.Values.Conditions
{
/// <summary>
/// Condition that checks if the basic value is valid (not null or true boolean).
/// </summary>
/// <param name="context"></param>
/// <param name="basicValue"></param>
public class Test(Context context, BasicValue basicValue) : BasicCondition(context)
{
/// <summary>
/// The value to check.
/// </summary>
public BasicValue Value { get; set; } = basicValue;

/// <summary>
/// Returns a string representation of the current value.
/// </summary>
/// <returns>String representation.</returns>
public override string? ToString() => $"Test({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);
}
}
}
}
1 change: 1 addition & 0 deletions src/Unluau/IL/Visitor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -41,5 +41,6 @@ public class Visitor

// Conditions
public virtual bool Visit(Equals node) => Visit(node as BasicCondition);
public virtual bool Visit(Test node) => Visit(node as BasicCondition);
}
}
15 changes: 4 additions & 11 deletions src/Unluau/IL/Visitors/OutputVisitor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -93,16 +93,9 @@ public override bool Visit(BasicBlock node)
return false;
}

public override bool Visit(Equals node)
{
Writer.Write($"{node.Left} == {node.Right}");
return false;
}

public override bool Visit(IfBlock node)
{
Writer.Write(Format(node.Context, "IfBlock "));
node.Condition.Visit(this);
Writer.Write(Format(node.Context, $"IfBlock {node.Condition}"));
Writer.WriteLine(" {");

Indent++;
Expand All @@ -113,7 +106,7 @@ public override bool Visit(IfBlock node)
return false;
}

private string Format(Context context, string op, string? a, string? b, string? c)
private static string Format(Context context, string op, string? a, string? b, string? c)
{
StringBuilder stringBuilder = new();

Expand All @@ -122,10 +115,10 @@ private string Format(Context context, string op, string? a, string? b, string?
return stringBuilder.ToString();
}

private string Format(Context context, string op, string? a, string? b)
private static string Format(Context context, string op, string? a, string? b)
=> Format(context, op, a, b, " ");

private string Format(Context context, string op)
private static string Format(Context context, string op)
{
StringBuilder stringBuilder = new();

Expand Down
7 changes: 7 additions & 0 deletions src/Unluau/IL/Visitors/ValueVisitor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,13 @@ public override bool Visit(Equals node)
return true;
}

public override bool Visit(Test node)
{
node.Value = ResolveValue(node.Value);

return true;
}

public override bool Visit(GetIndexSelf node)
{
if (!TryDelete(node, node.Slot))
Expand Down

0 comments on commit 39ab9aa

Please sign in to comment.