From 246aba3e95167254e434b96a94024200d0f5521e Mon Sep 17 00:00:00 2001 From: Deukhoofd Date: Sat, 19 Jan 2019 17:50:54 +0100 Subject: [PATCH] Fixed issue with userdata type on evaluation --- Upsilon/BaseTypes/Number/ScriptNumber.cs | 2 +- Upsilon/BaseTypes/ScriptBoolean.cs | 2 +- Upsilon/BaseTypes/ScriptFunction/ScriptFunction.cs | 2 +- Upsilon/BaseTypes/ScriptIterator.cs | 8 ++++---- Upsilon/BaseTypes/ScriptNull.cs | 2 +- Upsilon/BaseTypes/ScriptString.cs | 6 +++--- Upsilon/BaseTypes/ScriptTable/ScriptTable.cs | 4 ++-- Upsilon/BaseTypes/ScriptType.cs | 2 +- Upsilon/BaseTypes/SimpleScriptTable.cs | 8 ++++---- Upsilon/BaseTypes/UserData/DictionaryUserData.cs | 5 ++++- Upsilon/BaseTypes/UserData/GenericUserData.cs | 2 +- Upsilon/BaseTypes/UserData/ListUserData.cs | 13 ++++++++----- Upsilon/BaseTypes/UserData/UserDataType.cs | 3 +++ Upsilon/Evaluator/Evaluator.cs | 2 ++ 14 files changed, 36 insertions(+), 25 deletions(-) diff --git a/Upsilon/BaseTypes/Number/ScriptNumber.cs b/Upsilon/BaseTypes/Number/ScriptNumber.cs index 055c41a..c4200ea 100644 --- a/Upsilon/BaseTypes/Number/ScriptNumber.cs +++ b/Upsilon/BaseTypes/Number/ScriptNumber.cs @@ -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 diff --git a/Upsilon/BaseTypes/ScriptBoolean.cs b/Upsilon/BaseTypes/ScriptBoolean.cs index 36cee8c..8fabce4 100644 --- a/Upsilon/BaseTypes/ScriptBoolean.cs +++ b/Upsilon/BaseTypes/ScriptBoolean.cs @@ -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; diff --git a/Upsilon/BaseTypes/ScriptFunction/ScriptFunction.cs b/Upsilon/BaseTypes/ScriptFunction/ScriptFunction.cs index 0ffd3e6..ffec4a7 100644 --- a/Upsilon/BaseTypes/ScriptFunction/ScriptFunction.cs +++ b/Upsilon/BaseTypes/ScriptFunction/ScriptFunction.cs @@ -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; diff --git a/Upsilon/BaseTypes/ScriptIterator.cs b/Upsilon/BaseTypes/ScriptIterator.cs index df100cb..6f1aeac 100644 --- a/Upsilon/BaseTypes/ScriptIterator.cs +++ b/Upsilon/BaseTypes/ScriptIterator.cs @@ -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(){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(){key, value}); } } diff --git a/Upsilon/BaseTypes/ScriptNull.cs b/Upsilon/BaseTypes/ScriptNull.cs index 6ff4c43..68fc3d8 100644 --- a/Upsilon/BaseTypes/ScriptNull.cs +++ b/Upsilon/BaseTypes/ScriptNull.cs @@ -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; diff --git a/Upsilon/BaseTypes/ScriptString.cs b/Upsilon/BaseTypes/ScriptString.cs index 7a64777..1728def 100644 --- a/Upsilon/BaseTypes/ScriptString.cs +++ b/Upsilon/BaseTypes/ScriptString.cs @@ -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; diff --git a/Upsilon/BaseTypes/ScriptTable/ScriptTable.cs b/Upsilon/BaseTypes/ScriptTable/ScriptTable.cs index e3cea3a..476ac0d 100644 --- a/Upsilon/BaseTypes/ScriptTable/ScriptTable.cs +++ b/Upsilon/BaseTypes/ScriptTable/ScriptTable.cs @@ -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; diff --git a/Upsilon/BaseTypes/ScriptType.cs b/Upsilon/BaseTypes/ScriptType.cs index 13635ac..bc80864 100644 --- a/Upsilon/BaseTypes/ScriptType.cs +++ b/Upsilon/BaseTypes/ScriptType.cs @@ -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(); } diff --git a/Upsilon/BaseTypes/SimpleScriptTable.cs b/Upsilon/BaseTypes/SimpleScriptTable.cs index 14bef88..6621964 100644 --- a/Upsilon/BaseTypes/SimpleScriptTable.cs +++ b/Upsilon/BaseTypes/SimpleScriptTable.cs @@ -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; diff --git a/Upsilon/BaseTypes/UserData/DictionaryUserData.cs b/Upsilon/BaseTypes/UserData/DictionaryUserData.cs index 10c865a..8b960f1 100644 --- a/Upsilon/BaseTypes/UserData/DictionaryUserData.cs +++ b/Upsilon/BaseTypes/UserData/DictionaryUserData.cs @@ -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; diff --git a/Upsilon/BaseTypes/UserData/GenericUserData.cs b/Upsilon/BaseTypes/UserData/GenericUserData.cs index 3a1fb96..16399a0 100644 --- a/Upsilon/BaseTypes/UserData/GenericUserData.cs +++ b/Upsilon/BaseTypes/UserData/GenericUserData.cs @@ -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; diff --git a/Upsilon/BaseTypes/UserData/ListUserData.cs b/Upsilon/BaseTypes/UserData/ListUserData.cs index 6eb8a4c..6a44c74 100644 --- a/Upsilon/BaseTypes/UserData/ListUserData.cs +++ b/Upsilon/BaseTypes/UserData/ListUserData.cs @@ -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; diff --git a/Upsilon/BaseTypes/UserData/UserDataType.cs b/Upsilon/BaseTypes/UserData/UserDataType.cs index d4861f3..e494ffd 100644 --- a/Upsilon/BaseTypes/UserData/UserDataType.cs +++ b/Upsilon/BaseTypes/UserData/UserDataType.cs @@ -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(); @@ -44,6 +46,7 @@ namespace Upsilon.BaseTypes.UserData OperatorHandler = new UserDataTypeOperators(type); } + public string BoundTypeName { get; } private System.Type Type { get; } private Dictionary Variables { get; } private Dictionary Properties { get; } diff --git a/Upsilon/Evaluator/Evaluator.cs b/Upsilon/Evaluator/Evaluator.cs index f9913af..6b8eb54 100644 --- a/Upsilon/Evaluator/Evaluator.cs +++ b/Upsilon/Evaluator/Evaluator.cs @@ -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; }