Move ScriptFunctions to own namespace

This commit is contained in:
2018-11-30 11:15:52 +01:00
parent 9455b753a0
commit 3a3ed071d2
7 changed files with 63 additions and 55 deletions

View File

@@ -0,0 +1,18 @@
namespace Upsilon.BaseTypes.ScriptFunction
{
internal abstract class ScriptFunction : ScriptType
{
public override Type Type => Type.Function;
public override object ToCSharpObject()
{
return this;
}
public override System.Type GetCSharpType()
{
return null;
}
public abstract ScriptType Run(Diagnostics diagnostics, ScriptType[] variables);
}
}

View File

@@ -0,0 +1,61 @@
using System;
using System.Linq;
using System.Reflection;
using Upsilon.BaseTypes.UserData;
namespace Upsilon.BaseTypes.ScriptFunction
{
internal class ScriptMethodInfoFunction : ScriptFunction
{
public ScriptMethodInfoFunction(UserDataMethod method, object o, bool directTypeManipulation)
{
_method = method;
_object = o;
_directTypeManipulation = directTypeManipulation;
ReturnType = _method.ReturnType;
}
private readonly UserDataMethod _method;
private readonly object _object;
private readonly bool _directTypeManipulation;
public System.Type ReturnType { get; }
public override ScriptType Run(Diagnostics diagnostics, ScriptType[] variables)
{
var types = _directTypeManipulation
? variables.Select(x => x.GetType()).ToArray()
: variables.Select(x => x.GetCSharpType()).ToArray();
var method = _method.GetMethod(types);
if (method == null)
{
throw new Exception(
$"No valid function found on type '{_object.GetType()}' with name '{_method.Name}' " +
$"and parameter types: {string.Join(", ", types.Select(x => $"'{x.Name}'"))}");
}
var objects = _directTypeManipulation
? variables.Select(x => (object) x).ToList()
: variables.Select(x => x.ToCSharpObject()).ToList();
var pars = method.GetParameters();
if (pars.Length != objects.Count)
{
for (var i = objects.Count; i < pars.Length; i++)
{
objects.Add(null);
}
}
try
{
var result = method.Invoke(_object, objects.ToArray());
if (_directTypeManipulation)
return (ScriptType)result;
return result.ToScriptType();
}
catch (TargetInvocationException e)
{
if (e.InnerException != null) throw e.InnerException;
throw;
}
}
}
}

View File

@@ -0,0 +1,34 @@
using System.Collections.Immutable;
using Upsilon.BaseTypes.ScriptTypeInterfaces;
using Upsilon.Binder;
using Upsilon.Evaluator;
namespace Upsilon.BaseTypes.ScriptFunction
{
internal class ScriptRuntimeFunction : ScriptFunction, IScopeOwner
{
public BoundBlockStatement Block { get; }
public ImmutableArray<BoundVariableSymbol> Parameters { get; }
public EvaluationScope EvaluationScope { get; }
public ScriptRuntimeFunction(ImmutableArray<BoundVariableSymbol> parameters, BoundBlockStatement block,
EvaluationScope evaluationScope)
{
Parameters = parameters;
Block = block;
EvaluationScope = evaluationScope;
}
public override ScriptType Run(Diagnostics diagnostics, ScriptType[] variables)
{
var innerEvaluator = new Evaluator.Evaluator(diagnostics, EvaluationScope);
for (var i = 0; i < Parameters.Length; i++)
{
var parameterVariable = Parameters[i];
var parameterValue = variables[i];
innerEvaluator.Scope.CreateLocal(parameterVariable.VariableSymbol, parameterValue);
}
return innerEvaluator.EvaluateNode(Block);
}
}
}