Fix continuing with previous script scope

This commit is contained in:
Deukhoofd 2018-11-17 13:05:08 +01:00
parent a631d3b377
commit c013ed38c6
No known key found for this signature in database
GPG Key ID: B4C087AC81641654
5 changed files with 48 additions and 54 deletions

View File

@ -16,7 +16,7 @@ namespace Upsilon.Binder
private Dictionary<FunctionVariableSymbol, UnboundFunctionStatement> _unboundFunctions =
new Dictionary<FunctionVariableSymbol, UnboundFunctionStatement>();
public Binder(Diagnostics diagnostics, Dictionary<VariableSymbol, LuaType> variables)
public Binder(Diagnostics diagnostics, Dictionary<string, VariableSymbol> variables)
{
_diagnostics = diagnostics;
Scope = new BoundScope(variables, null);

View File

@ -1,3 +1,4 @@
using System;
using System.Collections.Generic;
using System.Linq;
using Upsilon.BaseTypes;
@ -7,25 +8,25 @@ namespace Upsilon.Binder
internal class BoundScope
{
public readonly BoundScope ParentScope;
private readonly Dictionary<string, VariableSymbol> _variables;
internal readonly Dictionary<string, VariableSymbol> Variables;
public BoundScope(BoundScope parentScope)
{
ParentScope = parentScope;
_variables = new Dictionary<string, VariableSymbol>();
Variables = new Dictionary<string, VariableSymbol>();
}
public BoundScope(Dictionary<VariableSymbol, LuaType> variables, BoundScope parentScope)
public BoundScope(Dictionary<string, VariableSymbol> variables, BoundScope parentScope)
{
ParentScope = parentScope;
_variables = variables.ToDictionary(x => x.Key.Name, x => x.Key);
Variables = variables;
}
public void SetVariable(VariableSymbol var)
{
if (_variables.ContainsKey(var.Name))
_variables[var.Name] = var;
if (Variables.ContainsKey(var.Name))
Variables[var.Name] = var;
else
_variables.Add(var.Name, var);
Variables.Add(var.Name, var);
}
public void SetGlobalVariable(VariableSymbol var)
@ -43,7 +44,7 @@ namespace Upsilon.Binder
public bool TryGetVariable(string key, bool allowUpperScopes, out VariableSymbol result)
{
if (_variables.TryGetValue(key, out result))
if (Variables.TryGetValue(key, out result))
{
return true;
}

View File

@ -7,27 +7,27 @@ namespace Upsilon.Evaluator
internal class EvaluationScope
{
private readonly EvaluationScope _parentScope;
private readonly Dictionary<VariableSymbol, LuaType> _variables;
internal readonly Dictionary<VariableSymbol, LuaType> Variables;
internal EvaluationScope(EvaluationScope parentScope)
{
_parentScope = parentScope;
_variables = new Dictionary<VariableSymbol, LuaType>();
Variables = new Dictionary<VariableSymbol, LuaType>();
}
internal EvaluationScope(Dictionary<VariableSymbol, LuaType> vars)
{
_variables = vars;
Variables = vars;
}
public void Set(VariableSymbol symbol, LuaType obj)
{
if (_variables.ContainsKey(symbol))
if (Variables.ContainsKey(symbol))
{
_variables[symbol] = obj;
Variables[symbol] = obj;
}
else
{
_variables.Add(symbol, obj);
Variables.Add(symbol, obj);
}
}
@ -43,7 +43,7 @@ namespace Upsilon.Evaluator
public bool TryGet(VariableSymbol symbol, out LuaType obj)
{
if (_variables.TryGetValue(symbol, out obj))
if (Variables.TryGetValue(symbol, out obj))
return true;
if (_parentScope != null)
if (_parentScope.TryGet(symbol, out obj))
@ -53,7 +53,7 @@ namespace Upsilon.Evaluator
public bool TryGet(string variable, out LuaType obj)
{
foreach (var luaType in _variables)
foreach (var luaType in Variables)
{
if (luaType.Key.Name == variable)
{

View File

@ -15,23 +15,33 @@ namespace Upsilon.Evaluator
private Evaluator Evaluator { get; set; }
private readonly BlockStatementSyntax _parsed;
private BoundScript _bound;
public Diagnostics Diagnostics { get; }
public Diagnostics Diagnostics { get; private set; }
private Binder.Binder Binder { get; set; }
internal EvaluationScope Scope { get; }
private 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>());
Binder = new Binder.Binder(Diagnostics, new Dictionary<string, VariableSymbol>());
Evaluator = new Evaluator( Diagnostics, new Dictionary<VariableSymbol, LuaType>());
Scope = Evaluator.Scope;
}
private Script(string scriptString, Binder.Binder binder, Evaluator evaluator)
{
ScriptString = new SourceText(scriptString);
Diagnostics = new Diagnostics(ScriptString);
_parsed = Parser.Parser.Parse(scriptString, Diagnostics);
Binder = new Binder.Binder(Diagnostics, binder.Scope.Variables);
Evaluator = new Evaluator( Diagnostics, evaluator.Scope.Variables);
Scope = Evaluator.Scope;
}
public static Script ContinueWith(Script previousScript, string scriptString)
{
var s = new Script(scriptString) {Binder = previousScript.Binder, Evaluator = previousScript.Evaluator};
var s = new Script(scriptString, previousScript.Binder, previousScript.Evaluator);
return s;
}
@ -42,7 +52,7 @@ namespace Upsilon.Evaluator
public object Evaluate()
{
return Evaluator.Evaluate(Bind());
return Convert(Evaluator.Evaluate(Bind()));
}
public object EvaluateFunction(string functionName, object[] parameters = null)
@ -58,7 +68,7 @@ namespace Upsilon.Evaluator
return Convert(Evaluator.Evaluate(Bind(), functionName, luaParameters.ToImmutable()));
}
private T Convert<T>(LuaType t)
private static T Convert<T>(LuaType t)
{
if (typeof(T) == typeof(double))
return (T)(object)System.Convert.ToDouble(t.ToCSharpObject());
@ -67,9 +77,9 @@ namespace Upsilon.Evaluator
return (T) t.ToCSharpObject();
}
private object Convert(LuaType t)
private static object Convert(LuaType t)
{
return t.ToCSharpObject();
return t?.ToCSharpObject();
}
public T Evaluate<T>()

View File

@ -1,10 +1,6 @@
using System;
using System.Collections.Generic;
using Upsilon;
using Upsilon.BaseTypes;
using Upsilon.Binder;
using Upsilon.Evaluator;
using UpsilonCompiler;
namespace Ycicle
{
@ -13,7 +9,6 @@ namespace Ycicle
private static void Main()
{
Console.WriteLine("Upsilon REPL");
var showTranspiledOutput = false;
Script script = null;
while (true)
{
@ -23,9 +18,6 @@ namespace Ycicle
{
case "exit":
return;
case "compiled":
showTranspiledOutput = !showTranspiledOutput;
break;
}
if (script == null)
@ -45,17 +37,7 @@ namespace Ycicle
continue;
}
if (showTranspiledOutput)
{
var transpiler = new Transpiler();
var transpiled = transpiler.TranspilerToCSharp(script.Bind());
Console.WriteLine(transpiled);
}
else
{
var evaluate = script.Evaluate();
if (evaluate == null)
continue;
if (script.Diagnostics.Messages.Count > 0)
{
Console.ForegroundColor = ConsoleColor.Red;
@ -68,13 +50,14 @@ namespace Ycicle
}
else
{
if (evaluate == null)
continue;
Console.ForegroundColor = ConsoleColor.Cyan;
Console.WriteLine(evaluate);
Console.ResetColor();
}
}
}
}
private static void LogError(DiagnosticsMessage message)
{