Skip to content

Commit

Permalink
fix nil-coalescing assignment
Browse files Browse the repository at this point in the history
  • Loading branch information
vddCore committed Nov 13, 2023
1 parent 8356eb9 commit ca8685c
Show file tree
Hide file tree
Showing 6 changed files with 25 additions and 8 deletions.
2 changes: 2 additions & 0 deletions Core/EVIL.Grammar/AST/Statements/ExpressionStatement.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ public sealed class ExpressionStatement : Statement
public ExpressionStatement(Expression expression)
{
Expression = expression;
Reparent(Expression);

Line = expression.Line;
}
}
Expand Down
2 changes: 1 addition & 1 deletion FrontEnd/EVIL.evil/EVIL.evil.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
<TargetFramework>net7.0</TargetFramework>
<Nullable>enable</Nullable>
<AssemblyName>evil</AssemblyName>
<Version>1.6.0</Version>
<Version>1.7.0</Version>
</PropertyGroup>

<ItemGroup>
Expand Down
16 changes: 14 additions & 2 deletions FrontEnd/EVIL.evil/EvmFrontEnd.cs
Original file line number Diff line number Diff line change
Expand Up @@ -30,12 +30,14 @@ public partial class EvmFrontEnd
{ "h|help", "display this message and quit.", (h) => _displayHelpAndQuit = h != null },
{ "v|version", "display compiler and VM version information.", (v) => _displayVersionAndQuit = v != null },
{ "I|include-dir=", "add include directory to the list of search paths.", (I) => _includeHandler.AddIncludeSearchPath(I) },
{ "g|gen-docs", "generate documentation for all detected native modules.", (g) => _generateModuleDocsAndQuit = g != null }
{ "g|gen-docs", "generate documentation for all detected native modules.", (g) => _generateModuleDocsAndQuit = g != null },
{ "d|disasm", "disassemble the compiled script.", (d) => _disassembleCompiledScript = d != null }
};

private static bool _displayHelpAndQuit;
private static bool _displayVersionAndQuit;
private static bool _generateModuleDocsAndQuit;
private static bool _generateModuleDocsAndQuit;
private static bool _disassembleCompiledScript;

public async Task Run(string[] args)
{
Expand Down Expand Up @@ -122,6 +124,16 @@ public async Task Run(string[] args)
Terminate(msg, exitCode: ExitCode.CompilerError);
}

if (_disassembleCompiledScript)
{
Console.WriteLine($"Disassembly of '{scriptPath}'\n-------------------");

foreach (var chunk in script.Chunks)
{
Disassembler.Disassemble(chunk, Console.Out);
}
}

if (!script.TryFindChunkByName("main", out var mainChunk))
{
Terminate(
Expand Down
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.0</Version>
<Version>1.3.1</Version>
<AssemblyName>Ceres</AssemblyName>
<AssemblyTitle>Ceres</AssemblyTitle>
</PropertyGroup>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
using Ceres.TranslationEngine.Diagnostics;
using EVIL.Grammar;
using EVIL.Grammar.AST.Expressions;
using EVIL.Grammar.AST.Statements;

namespace Ceres.TranslationEngine
{
Expand All @@ -22,14 +23,15 @@ public override void Visit(AssignmentExpression assignmentExpression)
Chunk.CodeGenerator.Emit(OpCode.LDNIL);
Chunk.CodeGenerator.Emit(OpCode.CNE);
Chunk.CodeGenerator.Emit(OpCode.TJMP, valueNotNilLabel);
Chunk.CodeGenerator.Emit(OpCode.POP);
Visit(assignmentExpression.Right);
Chunk.CodeGenerator.Emit(OpCode.DUP);
EmitVarSet(symRef.Identifier);
Chunk.UpdateLabel(valueNotNilLabel, Chunk.CodeGenerator.IP);

return;
}

if (assignmentExpression.OperationType != AssignmentOperationType.Direct)
{
EmitVarGet(symRef.Identifier);
Expand Down Expand Up @@ -77,6 +79,7 @@ public override void Visit(AssignmentExpression assignmentExpression)
Chunk.CodeGenerator.Emit(OpCode.LDNIL);
Chunk.CodeGenerator.Emit(OpCode.CNE);
Chunk.CodeGenerator.Emit(OpCode.TJMP, valueNotNilLabel);
Chunk.CodeGenerator.Emit(OpCode.POP);
Visit(assignmentExpression.Right);
Chunk.CodeGenerator.Emit(OpCode.DUP);
Visit(ie.Indexable);
Expand All @@ -86,7 +89,7 @@ public override void Visit(AssignmentExpression assignmentExpression)

return;
}

if (assignmentExpression.OperationType != AssignmentOperationType.Direct)
{
Visit(ie);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -194,10 +194,10 @@ fn coalescing_assignment_table_entry() {
#[test]
fn chained_coalescing_assignment_table_entry_when_first_is_nil() {
val t = { };
val a = "";
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 ca8685c

Please sign in to comment.