Split script tables in numerically keyed and generically keyed

This commit is contained in:
Deukhoofd 2018-12-09 13:23:09 +01:00
parent 2d6e3335aa
commit 6e960e38ff
No known key found for this signature in database
GPG Key ID: B4C087AC81641654
7 changed files with 53 additions and 8 deletions

View File

@ -1,9 +1,8 @@
using System;
using System.Globalization; using System.Globalization;
namespace Upsilon.BaseTypes.Number namespace Upsilon.BaseTypes.Number
{ {
internal class ScriptNumberLong : ScriptNumber public class ScriptNumberLong : ScriptNumber
{ {
public long Value { get; set; } public long Value { get; set; }
protected internal override bool IsFloat { get; } = false; protected internal override bool IsFloat { get; } = false;

View File

@ -0,0 +1,12 @@
using Upsilon.Evaluator;
namespace Upsilon.BaseTypes.ScriptTable
{
public class GenericKeyedScriptTable : ScriptTable
{
public GenericKeyedScriptTable(EvaluationScope scope) : base(scope)
{
}
}
}

View File

@ -0,0 +1,26 @@
using System.Collections;
using System.Collections.Generic;
using Upsilon.Evaluator;
namespace Upsilon.BaseTypes.ScriptTable
{
public class NumeratedScriptTable : ScriptTable, IEnumerable<ScriptType>
{
public NumeratedScriptTable(EvaluationScope scope) : base(scope)
{
}
public new IEnumerator GetEnumerator()
{
return Enumerator();
}
protected override IEnumerator<ScriptType> Enumerator()
{
foreach (var variable in EvaluationScope.Variables)
{
yield return variable.Value;
}
}
}
}

View File

@ -3,14 +3,13 @@ using System.Collections.Generic;
using System.Linq; using System.Linq;
using Upsilon.BaseTypes.Number; using Upsilon.BaseTypes.Number;
using Upsilon.BaseTypes.ScriptTypeInterfaces; using Upsilon.BaseTypes.ScriptTypeInterfaces;
using Upsilon.Binder;
using Upsilon.Binder.VariableSymbols; using Upsilon.Binder.VariableSymbols;
using Upsilon.Evaluator; using Upsilon.Evaluator;
using Upsilon.Text; 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; } public EvaluationScope EvaluationScope { get; }
@ -61,7 +60,7 @@ namespace Upsilon.BaseTypes
return Enumerator(); return Enumerator();
} }
private IEnumerator<ScriptType> Enumerator() protected virtual IEnumerator<ScriptType> Enumerator()
{ {
foreach (var variable in EvaluationScope.Variables) foreach (var variable in EvaluationScope.Variables)
{ {

View File

@ -125,7 +125,7 @@ namespace Upsilon.BaseTypes
return Type.Number; return Type.Number;
if (typeof(ScriptFunction.ScriptFunction).IsAssignableFrom(t)) if (typeof(ScriptFunction.ScriptFunction).IsAssignableFrom(t))
return Type.Function; return Type.Function;
if (typeof(ScriptTable).IsAssignableFrom(t)) if (typeof(ScriptTable.ScriptTable).IsAssignableFrom(t))
return Type.Table; return Type.Table;
if (t == typeof(IIterable)) if (t == typeof(IIterable))
return Type.Nil; return Type.Nil;

View File

@ -3,6 +3,7 @@ using System.Collections.Generic;
using Upsilon.BaseTypes; using Upsilon.BaseTypes;
using Upsilon.BaseTypes.Number; using Upsilon.BaseTypes.Number;
using Upsilon.BaseTypes.ScriptFunction; using Upsilon.BaseTypes.ScriptFunction;
using Upsilon.BaseTypes.ScriptTable;
using Upsilon.BaseTypes.ScriptTypeInterfaces; using Upsilon.BaseTypes.ScriptTypeInterfaces;
using Upsilon.BaseTypes.UserData; using Upsilon.BaseTypes.UserData;
using Upsilon.Binder; using Upsilon.Binder;
@ -554,6 +555,7 @@ namespace Upsilon.Evaluator
var tableScope = EvaluationScope.CreateWithGetOnlyParent(Scope); var tableScope = EvaluationScope.CreateWithGetOnlyParent(Scope);
var innerEvaluator = new Evaluator(_diagnostics, tableScope, _script); var innerEvaluator = new Evaluator(_diagnostics, tableScope, _script);
var currentPos = 1; var currentPos = 1;
var isNumerated = true;
foreach (var boundStatement in e.Statements) foreach (var boundStatement in e.Statements)
{ {
switch (boundStatement.Kind) switch (boundStatement.Kind)
@ -562,6 +564,7 @@ namespace Upsilon.Evaluator
case BoundKind.BoundFunctionExpression: case BoundKind.BoundFunctionExpression:
case BoundKind.BoundFunctionAssignmentStatement: case BoundKind.BoundFunctionAssignmentStatement:
innerEvaluator.EvaluateNode(boundStatement); innerEvaluator.EvaluateNode(boundStatement);
isNumerated = false;
break; break;
default: default:
{ {
@ -576,7 +579,12 @@ namespace Upsilon.Evaluator
currentPos++; currentPos++;
} }
return new ScriptTable(tableScope);
if (isNumerated)
{
return new NumeratedScriptTable(tableScope);
}
return new GenericKeyedScriptTable(tableScope);
} }
private ScriptType EvaluateIndexExpression(BoundIndexExpression e) private ScriptType EvaluateIndexExpression(BoundIndexExpression e)

View File

@ -5,6 +5,7 @@ using System.Collections.Immutable;
using System.Linq; using System.Linq;
using Upsilon.BaseTypes; using Upsilon.BaseTypes;
using Upsilon.BaseTypes.Number; using Upsilon.BaseTypes.Number;
using Upsilon.BaseTypes.ScriptTable;
using Upsilon.BaseTypes.ScriptTypeInterfaces; using Upsilon.BaseTypes.ScriptTypeInterfaces;
using Upsilon.BaseTypes.UserData; using Upsilon.BaseTypes.UserData;
using Upsilon.Binder; using Upsilon.Binder;