Skip to content

Commit

Permalink
fix subchunks not being finalized
Browse files Browse the repository at this point in the history
  • Loading branch information
vddCore committed Nov 14, 2023
1 parent ca8685c commit 02f3219
Show file tree
Hide file tree
Showing 8 changed files with 45 additions and 24 deletions.
2 changes: 1 addition & 1 deletion VirtualMachine/Ceres.Runtime/Ceres.Runtime.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
<Nullable>enable</Nullable>
<LangVersion>11.0</LangVersion>

<Version>0.8.0</Version>
<Version>0.9.0</Version>
<AssemblyName>Ceres.Runtime</AssemblyName>
<AssemblyTitle>Ceres.Runtime</AssemblyTitle>
<AllowUnsafeBlocks>true</AllowUnsafeBlocks>
Expand Down
26 changes: 21 additions & 5 deletions VirtualMachine/Ceres.Runtime/Modules/StringModule.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using System;
using System.Globalization;
using System.Linq;
using System.Security.Cryptography;
using System.Text;
using System.Text.RegularExpressions;
using Ceres.ExecutionEngine.Collections;
Expand Down Expand Up @@ -412,7 +413,7 @@ private static DynamicValue RegexMatch(Fiber _, params DynamicValue[] args)
{ "value", match.Value },
{ "starts_at", match.Index },
{ "length", match.Length },
{ "groups", new Table() }
{ "groups", new Array(0) }
};

foreach (Group group in match.Groups)
Expand All @@ -425,16 +426,31 @@ private static DynamicValue RegexMatch(Fiber _, params DynamicValue[] args)
{ "length", group.Length }
};

matchTable["groups"].Table!.Add(
matchTable["groups"].Table!.Length,
groupTable
);
matchTable["groups"].Array!.Push(groupTable);
}

array[i] = matchTable;
}

return array;
}

[RuntimeModuleFunction("md5")]
private static DynamicValue Md5(Fiber _, params DynamicValue[] args)
{
args.ExpectStringAt(0, out var text)
.OptionalStringAt(1, "utf-8", out var encoding);

var bytes = Encoding.GetEncoding(encoding).GetBytes(text);
var hash = MD5.HashData(bytes);

var sb = new StringBuilder();
for (var i = 0; i < hash.Length; i++)
{
sb.Append(hash[i].ToString("x2"));
}

return sb.ToString();
}
}
}
2 changes: 1 addition & 1 deletion VirtualMachine/Ceres/Ceres.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
<Nullable>enable</Nullable>
<LangVersion>11.0</LangVersion>

<Version>1.3.1</Version>
<Version>1.3.2</Version>
<AssemblyName>Ceres</AssemblyName>
<AssemblyTitle>Ceres</AssemblyTitle>
</PropertyGroup>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ public override void Visit(FnExpression fnExpression)
Visit(fnExpression.ParameterList);
Visit(fnExpression.Statement);
FinalizeChunk();
});
});

Expand Down
18 changes: 2 additions & 16 deletions VirtualMachine/Ceres/TranslationEngine/Compiler.Statement.Fn.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
using Ceres.ExecutionEngine.Diagnostics;
using Ceres.TranslationEngine.Diagnostics;
using EVIL.Grammar.AST.Statements.TopLevel;

Expand All @@ -20,21 +19,8 @@ public override void Visit(FnStatement fnStatement)
Visit(fnStatement.Statement);
});
if (Chunk.CodeGenerator.TryPeekOpCode(out var opCode))
{
if (opCode == OpCode.RET || opCode == OpCode.TAILINVOKE)
{
foreach (var attr in fnStatement.Attributes)
Visit(attr);
return;
}
}
/* Either we have no instructions in chunk, or it's not a RET. */
Chunk.CodeGenerator.Emit(OpCode.LDNIL);
Chunk.CodeGenerator.Emit(OpCode.RET);
FinalizeChunk();
foreach (var attr in fnStatement.Attributes)
Visit(attr);
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@ public override void Visit(OverrideStatement overrideStatement)
Visit(overrideStatement.ParameterList);
Visit(overrideStatement.Statement);
FinalizeChunk();
});
});

Expand Down
15 changes: 15 additions & 0 deletions VirtualMachine/Ceres/TranslationEngine/Compiler.Utilities.Emit.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,21 @@ namespace Ceres.TranslationEngine
{
public partial class Compiler
{
private void FinalizeChunk()
{
if (Chunk.CodeGenerator.TryPeekOpCode(out var opCode))
{
if (opCode == OpCode.RET || opCode == OpCode.TAILINVOKE)
{
return;
}
}

/* Either we have no instructions in chunk, or it's not a RET. */
Chunk.CodeGenerator.Emit(OpCode.LDNIL);
Chunk.CodeGenerator.Emit(OpCode.RET);
}

private void EmitVarSet(string identifier)
{
var symbolInfo = FindSymbolInClosedScopes(identifier);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -197,7 +197,7 @@ fn chained_coalescing_assignment_table_entry_when_first_is_nil() {
val a = "string";
rw val b = nil;

t.entry ??= (b ??= a);
t.entry ??= b ??= a;

assert.equal(b, a);
assert.equal(t.entry, a);
Expand Down

0 comments on commit 02f3219

Please sign in to comment.