From 6e960e38ffb546d333c66506ab0797241e42756d Mon Sep 17 00:00:00 2001 From: Deukhoofd Date: Sun, 9 Dec 2018 13:23:09 +0100 Subject: [PATCH] Split script tables in numerically keyed and generically keyed --- Upsilon/BaseTypes/Number/ScriptNumberLong.cs | 3 +-- .../ScriptTable/GenericKeyedScriptTable.cs | 12 +++++++++ .../ScriptTable/NumeratedScriptTable.cs | 26 +++++++++++++++++++ .../{ => ScriptTable}/ScriptTable.cs | 7 +++-- Upsilon/BaseTypes/TypeConversion.cs | 2 +- Upsilon/Evaluator/Evaluator.cs | 10 ++++++- Upsilon/StandardLibraries/StaticScope.cs | 1 + 7 files changed, 53 insertions(+), 8 deletions(-) create mode 100644 Upsilon/BaseTypes/ScriptTable/GenericKeyedScriptTable.cs create mode 100644 Upsilon/BaseTypes/ScriptTable/NumeratedScriptTable.cs rename Upsilon/BaseTypes/{ => ScriptTable}/ScriptTable.cs (91%) diff --git a/Upsilon/BaseTypes/Number/ScriptNumberLong.cs b/Upsilon/BaseTypes/Number/ScriptNumberLong.cs index b6bd156..bf17c0f 100644 --- a/Upsilon/BaseTypes/Number/ScriptNumberLong.cs +++ b/Upsilon/BaseTypes/Number/ScriptNumberLong.cs @@ -1,9 +1,8 @@ -using System; using System.Globalization; namespace Upsilon.BaseTypes.Number { - internal class ScriptNumberLong : ScriptNumber + public class ScriptNumberLong : ScriptNumber { public long Value { get; set; } protected internal override bool IsFloat { get; } = false; diff --git a/Upsilon/BaseTypes/ScriptTable/GenericKeyedScriptTable.cs b/Upsilon/BaseTypes/ScriptTable/GenericKeyedScriptTable.cs new file mode 100644 index 0000000..fdfa008 --- /dev/null +++ b/Upsilon/BaseTypes/ScriptTable/GenericKeyedScriptTable.cs @@ -0,0 +1,12 @@ +using Upsilon.Evaluator; + +namespace Upsilon.BaseTypes.ScriptTable +{ + public class GenericKeyedScriptTable : ScriptTable + { + public GenericKeyedScriptTable(EvaluationScope scope) : base(scope) + { + + } + } +} \ No newline at end of file diff --git a/Upsilon/BaseTypes/ScriptTable/NumeratedScriptTable.cs b/Upsilon/BaseTypes/ScriptTable/NumeratedScriptTable.cs new file mode 100644 index 0000000..5e99a99 --- /dev/null +++ b/Upsilon/BaseTypes/ScriptTable/NumeratedScriptTable.cs @@ -0,0 +1,26 @@ +using System.Collections; +using System.Collections.Generic; +using Upsilon.Evaluator; + +namespace Upsilon.BaseTypes.ScriptTable +{ + public class NumeratedScriptTable : ScriptTable, IEnumerable + { + public NumeratedScriptTable(EvaluationScope scope) : base(scope) + { + } + + public new IEnumerator GetEnumerator() + { + return Enumerator(); + } + + protected override IEnumerator Enumerator() + { + foreach (var variable in EvaluationScope.Variables) + { + yield return variable.Value; + } + } + } +} \ No newline at end of file diff --git a/Upsilon/BaseTypes/ScriptTable.cs b/Upsilon/BaseTypes/ScriptTable/ScriptTable.cs similarity index 91% rename from Upsilon/BaseTypes/ScriptTable.cs rename to Upsilon/BaseTypes/ScriptTable/ScriptTable.cs index 79c37ac..1f4836f 100644 --- a/Upsilon/BaseTypes/ScriptTable.cs +++ b/Upsilon/BaseTypes/ScriptTable/ScriptTable.cs @@ -3,14 +3,13 @@ using System.Collections.Generic; using System.Linq; using Upsilon.BaseTypes.Number; using Upsilon.BaseTypes.ScriptTypeInterfaces; -using Upsilon.Binder; using Upsilon.Binder.VariableSymbols; using Upsilon.Evaluator; using Upsilon.Text; -namespace Upsilon.BaseTypes +namespace Upsilon.BaseTypes.ScriptTable { - internal class ScriptTable : ScriptType, IIndexable, IScopeOwner, IIterable, ILengthType + public abstract class ScriptTable : ScriptType, IIndexable, IScopeOwner, IIterable, ILengthType { public EvaluationScope EvaluationScope { get; } @@ -61,7 +60,7 @@ namespace Upsilon.BaseTypes return Enumerator(); } - private IEnumerator Enumerator() + protected virtual IEnumerator Enumerator() { foreach (var variable in EvaluationScope.Variables) { diff --git a/Upsilon/BaseTypes/TypeConversion.cs b/Upsilon/BaseTypes/TypeConversion.cs index 9f4b8ff..cf2981a 100644 --- a/Upsilon/BaseTypes/TypeConversion.cs +++ b/Upsilon/BaseTypes/TypeConversion.cs @@ -125,7 +125,7 @@ namespace Upsilon.BaseTypes return Type.Number; if (typeof(ScriptFunction.ScriptFunction).IsAssignableFrom(t)) return Type.Function; - if (typeof(ScriptTable).IsAssignableFrom(t)) + if (typeof(ScriptTable.ScriptTable).IsAssignableFrom(t)) return Type.Table; if (t == typeof(IIterable)) return Type.Nil; diff --git a/Upsilon/Evaluator/Evaluator.cs b/Upsilon/Evaluator/Evaluator.cs index 0ae9d62..1540fe3 100644 --- a/Upsilon/Evaluator/Evaluator.cs +++ b/Upsilon/Evaluator/Evaluator.cs @@ -3,6 +3,7 @@ using System.Collections.Generic; using Upsilon.BaseTypes; using Upsilon.BaseTypes.Number; using Upsilon.BaseTypes.ScriptFunction; +using Upsilon.BaseTypes.ScriptTable; using Upsilon.BaseTypes.ScriptTypeInterfaces; using Upsilon.BaseTypes.UserData; using Upsilon.Binder; @@ -554,6 +555,7 @@ namespace Upsilon.Evaluator var tableScope = EvaluationScope.CreateWithGetOnlyParent(Scope); var innerEvaluator = new Evaluator(_diagnostics, tableScope, _script); var currentPos = 1; + var isNumerated = true; foreach (var boundStatement in e.Statements) { switch (boundStatement.Kind) @@ -562,6 +564,7 @@ namespace Upsilon.Evaluator case BoundKind.BoundFunctionExpression: case BoundKind.BoundFunctionAssignmentStatement: innerEvaluator.EvaluateNode(boundStatement); + isNumerated = false; break; default: { @@ -576,7 +579,12 @@ namespace Upsilon.Evaluator currentPos++; } - return new ScriptTable(tableScope); + + if (isNumerated) + { + return new NumeratedScriptTable(tableScope); + } + return new GenericKeyedScriptTable(tableScope); } private ScriptType EvaluateIndexExpression(BoundIndexExpression e) diff --git a/Upsilon/StandardLibraries/StaticScope.cs b/Upsilon/StandardLibraries/StaticScope.cs index e0e8b63..1841924 100644 --- a/Upsilon/StandardLibraries/StaticScope.cs +++ b/Upsilon/StandardLibraries/StaticScope.cs @@ -5,6 +5,7 @@ using System.Collections.Immutable; using System.Linq; using Upsilon.BaseTypes; using Upsilon.BaseTypes.Number; +using Upsilon.BaseTypes.ScriptTable; using Upsilon.BaseTypes.ScriptTypeInterfaces; using Upsilon.BaseTypes.UserData; using Upsilon.Binder;