Fixed issue with userdata type on evaluation
This commit is contained in:
parent
cf023af50d
commit
246aba3e95
|
@ -6,7 +6,7 @@ namespace Upsilon.BaseTypes.Number
|
|||
{
|
||||
protected internal abstract bool IsFloat { get; }
|
||||
|
||||
public override Type Type => Type.Number;
|
||||
public override TypeContainer Type => BaseTypes.Type.Number;
|
||||
|
||||
#region Binary Operators
|
||||
|
||||
|
|
|
@ -7,7 +7,7 @@ namespace Upsilon.BaseTypes
|
|||
Value = value;
|
||||
}
|
||||
|
||||
public override Type Type => Type.Boolean;
|
||||
public override TypeContainer Type => BaseTypes.Type.Boolean;
|
||||
public override object ToCSharpObject()
|
||||
{
|
||||
return Value;
|
||||
|
|
|
@ -5,7 +5,7 @@ namespace Upsilon.BaseTypes.ScriptFunction
|
|||
{
|
||||
public abstract class ScriptFunction : ScriptType
|
||||
{
|
||||
public override Type Type => Type.Function;
|
||||
public override TypeContainer Type => BaseTypes.Type.Function;
|
||||
public override object ToCSharpObject()
|
||||
{
|
||||
return this;
|
||||
|
|
|
@ -7,7 +7,7 @@ namespace Upsilon.BaseTypes
|
|||
internal abstract class ScriptIterator : ScriptType, IIterable
|
||||
{
|
||||
public IIterable BaseIterator { get; }
|
||||
public override Type Type => Type.Table;
|
||||
public override TypeContainer Type => BaseTypes.Type.Table;
|
||||
|
||||
public ScriptIterator(IIterable baseIterator)
|
||||
{
|
||||
|
@ -46,10 +46,10 @@ namespace Upsilon.BaseTypes
|
|||
while (baseEnumerator.MoveNext())
|
||||
{
|
||||
var key = baseEnumerator.Current;
|
||||
if (key == null || key.Type == Type.Nil)
|
||||
if (key == null || key.Type == BaseTypes.Type.Nil)
|
||||
break;
|
||||
var value = BaseIterator.GetValueFromIndex(key);
|
||||
if (value.Type == Type.Nil)
|
||||
if (value.Type == BaseTypes.Type.Nil)
|
||||
break;
|
||||
yield return new SimpleScriptTable(new List<ScriptType>(){key, value});
|
||||
}
|
||||
|
@ -72,7 +72,7 @@ namespace Upsilon.BaseTypes
|
|||
{
|
||||
var key = baseEnumerator.Current;
|
||||
var value = BaseIterator.GetValueFromIndex(key);
|
||||
if (value.Type != Type.Nil)
|
||||
if (value.Type != BaseTypes.Type.Nil)
|
||||
yield return new SimpleScriptTable(new List<ScriptType>(){key, value});
|
||||
}
|
||||
}
|
||||
|
|
|
@ -3,7 +3,7 @@ namespace Upsilon.BaseTypes
|
|||
internal class ScriptNull : ScriptType
|
||||
{
|
||||
|
||||
public override Type Type => Type.Nil;
|
||||
public override TypeContainer Type => BaseTypes.Type.Nil;
|
||||
public override object ToCSharpObject()
|
||||
{
|
||||
return null;
|
||||
|
|
|
@ -14,7 +14,7 @@ namespace Upsilon.BaseTypes
|
|||
}
|
||||
|
||||
public string Value { get; }
|
||||
public override Type Type => Type.String;
|
||||
public override TypeContainer Type => BaseTypes.Type.String;
|
||||
public override object ToCSharpObject()
|
||||
{
|
||||
return Value;
|
||||
|
@ -102,12 +102,12 @@ namespace Upsilon.BaseTypes
|
|||
public ScriptType Get(Diagnostics diagnostics, TextSpan span, ScriptType index, EvaluationScope scope)
|
||||
{
|
||||
int i;
|
||||
if (index.Type == Type.String)
|
||||
if (index.Type == BaseTypes.Type.String)
|
||||
{
|
||||
var str = (ScriptString)index;
|
||||
i = int.Parse(str.Value);
|
||||
}
|
||||
else if (index.Type == Type.Number)
|
||||
else if (index.Type == BaseTypes.Type.Number)
|
||||
{
|
||||
var ind = (Number.ScriptNumberLong) index;
|
||||
i = (int) ind.Value;
|
||||
|
|
|
@ -18,7 +18,7 @@ namespace Upsilon.BaseTypes.ScriptTable
|
|||
EvaluationScope = scope;
|
||||
}
|
||||
|
||||
public override Type Type => Type.Table;
|
||||
public override TypeContainer Type => BaseTypes.Type.Table;
|
||||
|
||||
public ScriptType Get(Diagnostics diagnostics, TextSpan span, ScriptType index, EvaluationScope scope)
|
||||
{
|
||||
|
@ -33,7 +33,7 @@ namespace Upsilon.BaseTypes.ScriptTable
|
|||
public void Set(Diagnostics diagnostics, TextSpan span, ScriptType index, ScriptType value)
|
||||
{
|
||||
var s = index.ToString();
|
||||
if (value.Type == Type.Nil)
|
||||
if (value.Type == BaseTypes.Type.Nil)
|
||||
{
|
||||
EvaluationScope.Delete(s);
|
||||
return;
|
||||
|
|
|
@ -2,7 +2,7 @@ namespace Upsilon.BaseTypes
|
|||
{
|
||||
public abstract class ScriptType
|
||||
{
|
||||
public abstract Type Type { get; }
|
||||
public abstract TypeContainer Type { get; }
|
||||
public abstract object ToCSharpObject();
|
||||
public abstract System.Type GetCSharpType();
|
||||
}
|
||||
|
|
|
@ -18,15 +18,15 @@ namespace Upsilon.BaseTypes
|
|||
public ScriptType Get(Diagnostics diagnostics, TextSpan span, ScriptType index, EvaluationScope scope)
|
||||
{
|
||||
int i;
|
||||
switch (index.Type)
|
||||
switch (index.Type.Type)
|
||||
{
|
||||
case Type.String:
|
||||
case BaseTypes.Type.String:
|
||||
{
|
||||
var str = (ScriptString)index;
|
||||
i = int.Parse(str.Value) - 1;
|
||||
break;
|
||||
}
|
||||
case Type.Number:
|
||||
case BaseTypes.Type.Number:
|
||||
{
|
||||
var ind = (Number.ScriptNumberLong) index;
|
||||
i = (int) ind.Value - 1;
|
||||
|
@ -46,7 +46,7 @@ namespace Upsilon.BaseTypes
|
|||
throw new System.NotImplementedException();
|
||||
}
|
||||
|
||||
public override Type Type => Type.Table;
|
||||
public override TypeContainer Type => BaseTypes.Type.Table;
|
||||
public override object ToCSharpObject()
|
||||
{
|
||||
return _objects;
|
||||
|
|
|
@ -3,6 +3,7 @@ using System.Collections.Generic;
|
|||
using System.Linq;
|
||||
using Upsilon.BaseTypes.Number;
|
||||
using Upsilon.BaseTypes.ScriptTypeInterfaces;
|
||||
using Upsilon.BoundTypes;
|
||||
using Upsilon.Evaluator;
|
||||
using Upsilon.Text;
|
||||
|
||||
|
@ -11,10 +12,12 @@ namespace Upsilon.BaseTypes.UserData
|
|||
internal class DictionaryUserData : ScriptType, IUserData, ILengthType, IIterable
|
||||
{
|
||||
public IDictionary Dictionary { get; }
|
||||
private string BoundTypeName { get; }
|
||||
|
||||
public DictionaryUserData(IDictionary dictionary)
|
||||
{
|
||||
Dictionary = dictionary;
|
||||
BoundTypeName = BoundTypeHandler.GetTypeName(dictionary.GetType());
|
||||
}
|
||||
|
||||
public ScriptType Get(Diagnostics diagnostics, TextSpan span, ScriptType index, EvaluationScope scope)
|
||||
|
@ -41,7 +44,7 @@ namespace Upsilon.BaseTypes.UserData
|
|||
}
|
||||
}
|
||||
|
||||
public override Type Type => Type.UserData;
|
||||
public override TypeContainer Type => new TypeContainer(BoundTypeName);
|
||||
public override object ToCSharpObject()
|
||||
{
|
||||
return Dictionary;
|
||||
|
|
|
@ -10,7 +10,7 @@ namespace Upsilon.BaseTypes.UserData
|
|||
Value = obj;
|
||||
_typeInfo = UserDataTypeHandler.GetTypeInfo(obj.GetType());
|
||||
}
|
||||
public override Type Type => Type.UserData;
|
||||
public override TypeContainer Type => new TypeContainer(_typeInfo.BoundTypeName);
|
||||
|
||||
private object Value { get; }
|
||||
private readonly UserDataType _typeInfo;
|
||||
|
|
|
@ -3,6 +3,7 @@ using System.Collections;
|
|||
using System.Collections.Generic;
|
||||
using Upsilon.BaseTypes.Number;
|
||||
using Upsilon.BaseTypes.ScriptTypeInterfaces;
|
||||
using Upsilon.BoundTypes;
|
||||
using Upsilon.Evaluator;
|
||||
using Upsilon.Exceptions;
|
||||
using Upsilon.Text;
|
||||
|
@ -12,21 +13,23 @@ namespace Upsilon.BaseTypes.UserData
|
|||
internal class ListUserData : ScriptType, IUserData, IIterable, ILengthType
|
||||
{
|
||||
public IList List { get; }
|
||||
public string TypeName { get; }
|
||||
|
||||
public ListUserData(IList list)
|
||||
{
|
||||
List = list;
|
||||
TypeName = BoundTypeHandler.GetTypeName(list.GetType());
|
||||
}
|
||||
|
||||
public ScriptType Get(Diagnostics diagnostics, TextSpan span, ScriptType index, EvaluationScope scope)
|
||||
{
|
||||
int i;
|
||||
if (index.Type == Type.String)
|
||||
if (index.Type == BaseTypes.Type.String)
|
||||
{
|
||||
var str = (ScriptString)index;
|
||||
i = int.Parse(str.Value) - 1;
|
||||
}
|
||||
else if (index.Type == Type.Number)
|
||||
else if (index.Type == BaseTypes.Type.Number)
|
||||
{
|
||||
var ind = (Number.ScriptNumberLong) index;
|
||||
i = (int) ind.Value - 1;
|
||||
|
@ -41,11 +44,11 @@ namespace Upsilon.BaseTypes.UserData
|
|||
public void Set(Diagnostics diagnostics, TextSpan span, ScriptType scriptIndex, ScriptType value)
|
||||
{
|
||||
var index = -1;
|
||||
if (scriptIndex.Type == Type.Number || scriptIndex.Type == Type.Unknown)
|
||||
if (scriptIndex.Type == BaseTypes.Type.Number || scriptIndex.Type == BaseTypes.Type.Unknown)
|
||||
{
|
||||
index = Convert.ToInt32(scriptIndex.ToCSharpObject());
|
||||
}
|
||||
else if (scriptIndex.Type == Type.String)
|
||||
else if (scriptIndex.Type == BaseTypes.Type.String)
|
||||
{
|
||||
index = int.Parse(((ScriptString) scriptIndex).Value);
|
||||
}
|
||||
|
@ -75,7 +78,7 @@ namespace Upsilon.BaseTypes.UserData
|
|||
}
|
||||
}
|
||||
|
||||
public override Type Type => Type.UserData;
|
||||
public override TypeContainer Type => new TypeContainer(TypeName);
|
||||
public override object ToCSharpObject()
|
||||
{
|
||||
return List;
|
||||
|
|
|
@ -3,6 +3,7 @@ using System.Globalization;
|
|||
using System.Linq;
|
||||
using System.Reflection;
|
||||
using Upsilon.BaseTypes.ScriptFunction;
|
||||
using Upsilon.BoundTypes;
|
||||
using Upsilon.StandardLibraries;
|
||||
|
||||
namespace Upsilon.BaseTypes.UserData
|
||||
|
@ -16,6 +17,7 @@ namespace Upsilon.BaseTypes.UserData
|
|||
type = type.GetGenericTypeDefinition();
|
||||
}
|
||||
Type = type;
|
||||
BoundTypeName = BoundTypeHandler.GetTypeName(type);
|
||||
Variables = type.GetFields().ToDictionary(x => x.Name.ToLowerInvariant(), x => x);
|
||||
Properties = type.GetProperties().ToDictionary(x => x.Name.ToLowerInvariant(), x => x);
|
||||
Methods = new Dictionary<string, UserDataMethod>();
|
||||
|
@ -44,6 +46,7 @@ namespace Upsilon.BaseTypes.UserData
|
|||
OperatorHandler = new UserDataTypeOperators(type);
|
||||
}
|
||||
|
||||
public string BoundTypeName { get; }
|
||||
private System.Type Type { get; }
|
||||
private Dictionary<string, FieldInfo> Variables { get; }
|
||||
private Dictionary<string, PropertyInfo> Properties { get; }
|
||||
|
|
|
@ -603,8 +603,10 @@ namespace Upsilon.Evaluator
|
|||
{
|
||||
innerEvaluator.EvaluateStatement(boundStatement);
|
||||
if (innerEvaluator._lastValue != null)
|
||||
{
|
||||
tableScope.CreateLocal(new VariableSymbol(currentPos.ToString(), innerEvaluator._lastValue.Type, false),
|
||||
innerEvaluator._lastValue);
|
||||
}
|
||||
innerEvaluator._lastValue = null;
|
||||
break;
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue