Reworked script function attribute, added initial math library

This commit is contained in:
2018-12-07 16:11:52 +01:00
parent 9bd82174f2
commit ac05647d71
15 changed files with 95 additions and 29 deletions

View File

@@ -3,7 +3,7 @@ using Upsilon.Text;
namespace Upsilon.BaseTypes.UserData
{
internal class GenericUserData : ScriptType, IUserData
internal sealed class GenericUserData : ScriptType, IUserData
{
public GenericUserData(object dictionary)
{
@@ -12,7 +12,7 @@ namespace Upsilon.BaseTypes.UserData
}
public override Type Type => Type.UserData;
protected virtual object Value { get; }
private object Value { get; }
private readonly UserDataType _typeInfo;
public override object ToCSharpObject()
@@ -25,7 +25,7 @@ namespace Upsilon.BaseTypes.UserData
return Value.GetType();
}
public virtual ScriptType Get(Diagnostics diagnostics, TextSpan span, ScriptType index, EvaluationScope scope)
public ScriptType Get(Diagnostics diagnostics, TextSpan span, ScriptType index, EvaluationScope scope)
{
var s = index.ToCSharpObject().ToString();
var (type, failed, error) = _typeInfo.Get(Value, s);
@@ -36,7 +36,7 @@ namespace Upsilon.BaseTypes.UserData
return type;
}
public virtual void Set(Diagnostics diagnostics, TextSpan span, ScriptType index, ScriptType value)
public void Set(Diagnostics diagnostics, TextSpan span, ScriptType index, ScriptType value)
{
var s = index.ToCSharpObject().ToString();
var (failed, error) = _typeInfo.Set(Value, s, value);

View File

@@ -2,6 +2,7 @@ using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using Upsilon.Evaluator;
using Upsilon.StandardLibraries;
namespace Upsilon.BaseTypes.UserData
{
@@ -13,6 +14,8 @@ namespace Upsilon.BaseTypes.UserData
{
Method = method;
var pars = method.GetParameters();
Attribute = (ScriptFunctionAttribute) method.GetCustomAttribute(typeof(ScriptFunctionAttribute));
var ls = new List<UserDataMethodParameter>();
foreach (var parameter in pars)
{
@@ -26,6 +29,7 @@ namespace Upsilon.BaseTypes.UserData
}
public MethodInfo Method { get; }
public ScriptFunctionAttribute Attribute { get; }
public UserDataMethodParameter[] Parameters { get; }
}

View File

@@ -2,6 +2,7 @@ using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using Upsilon.BaseTypes.ScriptFunction;
using Upsilon.StandardLibraries;
namespace Upsilon.BaseTypes.UserData
{
@@ -16,6 +17,12 @@ namespace Upsilon.BaseTypes.UserData
foreach (var methodInfo in type.GetMethods())
{
var commonName = methodInfo.Name.ToLowerInvariant();
var attribute = methodInfo.GetCustomAttribute(typeof(ScriptFunctionAttribute));
if (attribute is ScriptFunctionAttribute sfa )
{
commonName = sfa.Name.ToLowerInvariant();
}
if (Methods.TryGetValue(commonName, out var methodData))
{
methodData.LoadMethodPart(methodInfo);