This instance will be upgraded to Heptapod 0.17.2 on 2020-12-03 at 14:00 UTC+1 (a few minutes of down time)

Commit 5fec5a34 authored by Jesse McGrew's avatar Jesse McGrew

Changed the primitive representation of OBLIST to be compatible with MDL.

[ZILF-204]
parent ed8715724fa9
......@@ -18,6 +18,7 @@
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using Zilf.Interpreter.Values;
using Zilf.Language;
using Zilf.Diagnostics;
......@@ -54,16 +55,31 @@ namespace Zilf.Interpreter
{
switch (first)
{
case ZilList pair when pair.Matches(out ZilString key, out ZilAtom value):
result[key.Text] = value;
break;
case ZilListoidBase bucket:
{
foreach (var elem in bucket)
{
switch (elem)
{
case ZilAtom atom:
result[atom.Text] = atom;
break;
default:
throw new InterpreterError(
InterpreterMessages._0_In_1_Must_Be_2,
"elements",
"OBLIST bucket",
"atoms");
}
}
case ZilList pair when !pair.HasLength(2):
throw new InterpreterError(InterpreterMessages._0_In_1_Must_Have_2_Element2s, "elements", "OBLIST", 2);
break;
}
case ZilList _:
throw new InterpreterError(InterpreterMessages._0_In_1_Must_Be_2, "elements", "OBLIST",
"string-atom pairs");
default:
throw new InterpreterError(
InterpreterMessages._0_In_1_Must_Be_2, "buckets", "OBLIST", "lists");
}
list = rest;
......@@ -74,27 +90,21 @@ namespace Zilf.Interpreter
protected override TiedLayout GetLayout()
{
return TiedLayout.Create<ObList>().WithCatchAll<ObList>(x => x.PairsList);
return TiedLayout.Create<ObList>().WithCatchAll<ObList>(x => x.BucketList);
}
[NotNull]
public ZilList PairsList
public ZilList BucketList
{
get
{
var result = new List<ZilObject>(dict.Count);
var empty = new ZilList(null, null);
foreach (var pair in dict)
{
Debug.Assert(pair.Key != null, "pair.Key != null");
Debug.Assert(pair.Value != null, "pair.Value != null");
result.Add(
new ZilList(ZilString.FromString(pair.Key),
new ZilList(pair.Value,
new ZilList(null, null))));
}
if (dict.Count == 0)
return empty;
return new ZilList(result);
var bucket = new ZilList(dict.Values);
return new ZilList(bucket, empty);
}
}
......
......@@ -22,6 +22,7 @@ using System.Diagnostics.CodeAnalysis;
using System.Linq;
using JetBrains.Annotations;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Zilf.Common;
using Zilf.Interpreter;
using Zilf.Interpreter.Values;
using Zilf.Language;
......@@ -1413,5 +1414,27 @@ namespace Zilf.Tests.Interpreter
{
TestHelpers.EvalAndAssert(ctx, "<REST '<1> 0>", new ZilList(new ZilFix(1), new ZilList(null, null)));
}
[TestMethod]
public void OBLIST_Primitive_Structure_Should_Resemble_MDL()
{
if (!(ctx.RootObList.GetPrimitive(ctx) is ZilList prim))
{
Assert.Fail("expected primitive to be a list");
throw new UnreachableCodeException();
}
var items = prim.ToArray();
CollectionAssert.AllItemsAreInstancesOfType(items, typeof(ZilList));
// ReSharper disable once PossibleInvalidCastExceptionInForeachLoop
foreach (ZilList i in items)
{
var subItems = i.ToArray();
foreach (var j in subItems)
Assert.IsInstanceOfType(j, typeof(ZilAtom), "expected bucket item to be atom, but found '{0}'", j);
}
}
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment