Upsilon/Upsilon/Evaluator/Script.cs

113 lines
3.5 KiB
C#
Raw Normal View History

using System;
2018-11-10 16:00:39 +00:00
using System.Collections.Generic;
using System.Collections.Immutable;
using Upsilon.BaseTypes;
2018-11-11 17:12:42 +00:00
using Upsilon.Binder;
2018-11-10 16:00:39 +00:00
using Upsilon.Parser;
2018-11-11 09:26:52 +00:00
using Upsilon.Text;
2018-11-15 14:51:05 +00:00
using Upsilon.Utilities;
2018-11-10 16:00:39 +00:00
namespace Upsilon.Evaluator
{
2018-11-11 17:12:42 +00:00
public class Script
2018-11-10 16:00:39 +00:00
{
2018-11-11 09:26:52 +00:00
private SourceText ScriptString { get; }
private Evaluator Evaluator { get; set; }
private readonly BlockStatementSyntax _parsed;
private BoundScript _bound;
2018-11-11 09:26:52 +00:00
public Diagnostics Diagnostics { get; }
private Binder.Binder Binder { get; set; }
internal EvaluationScope Scope { get; }
2018-11-10 16:00:39 +00:00
public Script(string scriptString)
2018-11-10 16:00:39 +00:00
{
2018-11-11 09:26:52 +00:00
ScriptString = new SourceText(scriptString);
Diagnostics = new Diagnostics(ScriptString);
2018-11-11 17:12:42 +00:00
_parsed = Parser.Parser.Parse(scriptString, Diagnostics);
Binder = new Binder.Binder(Diagnostics, new Dictionary<VariableSymbol, LuaType>());
Evaluator = new Evaluator( Diagnostics, new Dictionary<VariableSymbol, LuaType>());
2018-11-15 19:13:53 +00:00
Scope = Evaluator.Scope;
2018-11-10 16:00:39 +00:00
}
public static Script ContinueWith(Script previousScript, string scriptString)
{
var s = new Script(scriptString) {Binder = previousScript.Binder, Evaluator = previousScript.Evaluator};
return s;
}
2018-11-13 16:11:20 +00:00
public BoundScript Bind()
{
return _bound ?? (_bound = Binder.BindScript(_parsed));
2018-11-13 16:11:20 +00:00
}
2018-11-10 16:00:39 +00:00
public object Evaluate()
{
2018-11-13 16:11:20 +00:00
return Evaluator.Evaluate(Bind());
2018-11-10 16:00:39 +00:00
}
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>()
2018-11-10 16:00:39 +00:00
{
return Convert<T>(Evaluator.Evaluate(Bind()));
2018-11-10 16:00:39 +00:00
}
2018-11-15 14:51:05 +00:00
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 _);
}
2018-11-15 14:51:05 +00:00
public string PrettyPrintSyntaxTree()
{
return _parsed.Print();
}
2018-11-10 16:00:39 +00:00
}
}