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 = private Dictionary<FunctionVariableSymbol, UnboundFunctionStatement> _unboundFunctions =
new Dictionary<FunctionVariableSymbol, UnboundFunctionStatement>(); new Dictionary<FunctionVariableSymbol, UnboundFunctionStatement>();
public Binder(Diagnostics diagnostics, Dictionary<VariableSymbol, LuaType> variables) public Binder(Diagnostics diagnostics, Dictionary<string, VariableSymbol> variables)
{ {
_diagnostics = diagnostics; _diagnostics = diagnostics;
Scope = new BoundScope(variables, null); Scope = new BoundScope(variables, null);

View File

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

View File

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

View File

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

View File

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