Upsilon/Upsilon/Evaluator/Script.cs

39 lines
1.2 KiB
C#
Raw Normal View History

2018-11-10 16:00:39 +00:00
using System.Collections.Generic;
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-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; }
private readonly BlockStatementSyntax _parsed;
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-10 16:00:39 +00:00
2018-11-11 20:03:50 +00:00
public Script(string scriptString, Dictionary<VariableSymbol, object> 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)
variables = new Dictionary<VariableSymbol, object>();
2018-11-11 20:03:50 +00:00
Binder = new Binder.Binder(new BoundScope(variables, null), Diagnostics);
Evaluator = new Evaluator(this, Diagnostics, variables);
2018-11-10 16:00:39 +00:00
}
public object Evaluate()
{
2018-11-11 17:12:42 +00:00
var bound = Binder.BindScript(_parsed);
return Evaluator.Evaluate(bound);
2018-11-10 16:00:39 +00:00
}
public T Evaluate<T>()
{
2018-11-11 17:12:42 +00:00
var bound = Binder.BindScript(_parsed);
return (T)Evaluator.Evaluate(bound);
2018-11-10 16:00:39 +00:00
}
}
}