113 lines
3.5 KiB
C#
113 lines
3.5 KiB
C#
using System;
|
|
using System.Collections.Generic;
|
|
using System.Collections.Immutable;
|
|
using Upsilon.BaseTypes;
|
|
using Upsilon.Binder;
|
|
using Upsilon.Parser;
|
|
using Upsilon.Text;
|
|
using Upsilon.Utilities;
|
|
|
|
namespace Upsilon.Evaluator
|
|
{
|
|
public class Script
|
|
{
|
|
private SourceText ScriptString { get; }
|
|
private Evaluator Evaluator { get; set; }
|
|
private readonly BlockStatementSyntax _parsed;
|
|
private BoundScript _bound;
|
|
public Diagnostics Diagnostics { get; }
|
|
private Binder.Binder Binder { get; set; }
|
|
internal EvaluationScope Scope { get; }
|
|
|
|
public Script(string scriptString)
|
|
{
|
|
ScriptString = new SourceText(scriptString);
|
|
Diagnostics = new Diagnostics(ScriptString);
|
|
_parsed = Parser.Parser.Parse(scriptString, Diagnostics);
|
|
Binder = new Binder.Binder(Diagnostics, new Dictionary<VariableSymbol, LuaType>());
|
|
Evaluator = new Evaluator( Diagnostics, new Dictionary<VariableSymbol, LuaType>());
|
|
Scope = Evaluator.Scope;
|
|
}
|
|
|
|
public static Script ContinueWith(Script previousScript, string scriptString)
|
|
{
|
|
var s = new Script(scriptString) {Binder = previousScript.Binder, Evaluator = previousScript.Evaluator};
|
|
return s;
|
|
}
|
|
|
|
public BoundScript Bind()
|
|
{
|
|
return _bound ?? (_bound = Binder.BindScript(_parsed));
|
|
}
|
|
|
|
public object Evaluate()
|
|
{
|
|
return Evaluator.Evaluate(Bind());
|
|
}
|
|
|
|
public object EvaluateFunction(string functionName, object[] parameters = null)
|
|
{
|
|
var luaParameters = ImmutableArray.CreateBuilder<LuaType>();
|
|
if (parameters != null)
|
|
{
|
|
foreach (var parameter in parameters)
|
|
{
|
|
luaParameters.Add(parameter.ToLuaType());
|
|
}
|
|
}
|
|
return Convert(Evaluator.Evaluate(Bind(), functionName, luaParameters.ToImmutable()));
|
|
}
|
|
|
|
private T Convert<T>(LuaType t)
|
|
{
|
|
if (typeof(T) == typeof(double))
|
|
return (T)(object)System.Convert.ToDouble(t.ToCSharpObject());
|
|
if (typeof(T) == typeof(long))
|
|
return (T)(object)System.Convert.ToInt64(t.ToCSharpObject());
|
|
return (T) t.ToCSharpObject();
|
|
}
|
|
|
|
private object Convert(LuaType t)
|
|
{
|
|
return t.ToCSharpObject();
|
|
}
|
|
|
|
public T Evaluate<T>()
|
|
{
|
|
return Convert<T>(Evaluator.Evaluate(Bind()));
|
|
}
|
|
|
|
public T EvaluateFunction<T>(string functionName, object[] parameters = null)
|
|
{
|
|
var luaParameters = ImmutableArray.CreateBuilder<LuaType>();
|
|
if (parameters != null)
|
|
{
|
|
foreach (var parameter in parameters)
|
|
{
|
|
luaParameters.Add(parameter.ToLuaType());
|
|
}
|
|
}
|
|
return Convert<T>(Evaluator.Evaluate(Bind(), functionName, luaParameters.ToImmutable()));
|
|
}
|
|
|
|
public T GetVariable<T>(string variableName)
|
|
{
|
|
if (!Scope.TryGet(variableName, out var variable))
|
|
{
|
|
throw new NullReferenceException();
|
|
}
|
|
|
|
return Convert<T>(variable);
|
|
}
|
|
|
|
public bool HasVariable(string variableName)
|
|
{
|
|
return Scope.TryGet(variableName, out _);
|
|
}
|
|
|
|
public string PrettyPrintSyntaxTree()
|
|
{
|
|
return _parsed.Print();
|
|
}
|
|
}
|
|
} |