2018-11-10 16:00:39 +00:00
|
|
|
using System.Collections.Generic;
|
2018-11-16 13:58:15 +00:00
|
|
|
using System.Collections.Immutable;
|
2018-11-14 12:09:01 +00:00
|
|
|
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; }
|
2018-11-10 16:00:39 +00:00
|
|
|
private Evaluator Evaluator { get; }
|
2018-11-12 16:45:50 +00:00
|
|
|
private readonly BlockStatementSyntax _parsed;
|
2018-11-16 13:46:12 +00:00
|
|
|
private BoundScript _bound;
|
2018-11-11 09:26:52 +00:00
|
|
|
public Diagnostics Diagnostics { get; }
|
2018-11-11 17:12:42 +00:00
|
|
|
private Binder.Binder Binder { get; }
|
2018-11-14 16:04:04 +00:00
|
|
|
public EvaluationScope Scope { get; }
|
2018-11-10 16:00:39 +00:00
|
|
|
|
2018-11-14 12:09:01 +00:00
|
|
|
public Script(string scriptString, Dictionary<VariableSymbol, LuaType> variables = null)
|
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);
|
2018-11-13 11:48:50 +00:00
|
|
|
if (variables == null)
|
2018-11-14 12:09:01 +00:00
|
|
|
variables = new Dictionary<VariableSymbol, LuaType>();
|
2018-11-16 12:55:31 +00:00
|
|
|
Binder = new Binder.Binder(Diagnostics, variables);
|
2018-11-15 19:13:53 +00:00
|
|
|
Evaluator = new Evaluator( Diagnostics, variables);
|
|
|
|
Scope = Evaluator.Scope;
|
2018-11-10 16:00:39 +00:00
|
|
|
}
|
|
|
|
|
2018-11-13 16:11:20 +00:00
|
|
|
public BoundScript Bind()
|
|
|
|
{
|
2018-11-16 13:46:12 +00:00
|
|
|
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
|
|
|
}
|
|
|
|
|
2018-11-16 13:58:15 +00:00
|
|
|
public object EvaluateFunction(string functionName, object[] parameters = null)
|
2018-11-16 13:46:12 +00:00
|
|
|
{
|
2018-11-16 13:58:15 +00:00
|
|
|
var luaParameters = ImmutableArray.CreateBuilder<LuaType>();
|
|
|
|
if (parameters != null)
|
|
|
|
{
|
|
|
|
foreach (var parameter in parameters)
|
|
|
|
{
|
|
|
|
luaParameters.Add(parameter.ToLuaType());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return Evaluator.Evaluate(Bind(), functionName, luaParameters.ToImmutable());
|
2018-11-16 13:46:12 +00:00
|
|
|
}
|
|
|
|
|
2018-11-14 12:09:01 +00:00
|
|
|
public T Evaluate<T>() where T : LuaType
|
2018-11-10 16:00:39 +00:00
|
|
|
{
|
2018-11-13 16:11:20 +00:00
|
|
|
return (T)Evaluator.Evaluate(Bind());
|
2018-11-10 16:00:39 +00:00
|
|
|
}
|
2018-11-15 14:51:05 +00:00
|
|
|
|
2018-11-16 13:58:15 +00:00
|
|
|
public T EvaluateFunction<T>(string functionName, object[] parameters = null) where T : LuaType
|
2018-11-16 13:46:12 +00:00
|
|
|
{
|
2018-11-16 13:58:15 +00:00
|
|
|
var luaParameters = ImmutableArray.CreateBuilder<LuaType>();
|
|
|
|
if (parameters != null)
|
|
|
|
{
|
|
|
|
foreach (var parameter in parameters)
|
|
|
|
{
|
|
|
|
luaParameters.Add(parameter.ToLuaType());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return (T)Evaluator.Evaluate(Bind(), functionName, luaParameters.ToImmutable());
|
2018-11-16 13:46:12 +00:00
|
|
|
}
|
|
|
|
|
2018-11-15 14:51:05 +00:00
|
|
|
public string PrettyPrintSyntaxTree()
|
|
|
|
{
|
|
|
|
return _parsed.Print();
|
|
|
|
}
|
2018-11-10 16:00:39 +00:00
|
|
|
}
|
|
|
|
}
|