diff --git a/Upsilon/Binder/Binder.cs b/Upsilon/Binder/Binder.cs index 0621b1d..8bf73d5 100644 --- a/Upsilon/Binder/Binder.cs +++ b/Upsilon/Binder/Binder.cs @@ -16,7 +16,7 @@ namespace Upsilon.Binder private Dictionary _unboundFunctions = new Dictionary(); - public Binder(Diagnostics diagnostics, Dictionary variables) + public Binder(Diagnostics diagnostics, Dictionary variables) { _diagnostics = diagnostics; Scope = new BoundScope(variables, null); diff --git a/Upsilon/Binder/BoundScope.cs b/Upsilon/Binder/BoundScope.cs index ce572e1..3dc4185 100644 --- a/Upsilon/Binder/BoundScope.cs +++ b/Upsilon/Binder/BoundScope.cs @@ -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 _variables; + internal readonly Dictionary Variables; public BoundScope(BoundScope parentScope) { ParentScope = parentScope; - _variables = new Dictionary(); + Variables = new Dictionary(); } - public BoundScope(Dictionary variables, BoundScope parentScope) + public BoundScope(Dictionary 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; } diff --git a/Upsilon/Evaluator/EvaluationScope.cs b/Upsilon/Evaluator/EvaluationScope.cs index ea4b682..100f6fe 100644 --- a/Upsilon/Evaluator/EvaluationScope.cs +++ b/Upsilon/Evaluator/EvaluationScope.cs @@ -7,27 +7,27 @@ namespace Upsilon.Evaluator internal class EvaluationScope { private readonly EvaluationScope _parentScope; - private readonly Dictionary _variables; + internal readonly Dictionary Variables; internal EvaluationScope(EvaluationScope parentScope) { _parentScope = parentScope; - _variables = new Dictionary(); + Variables = new Dictionary(); } internal EvaluationScope(Dictionary 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) { diff --git a/Upsilon/Evaluator/Script.cs b/Upsilon/Evaluator/Script.cs index d2af372..0e8e502 100644 --- a/Upsilon/Evaluator/Script.cs +++ b/Upsilon/Evaluator/Script.cs @@ -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()); + Binder = new Binder.Binder(Diagnostics, new Dictionary()); Evaluator = new Evaluator( Diagnostics, new Dictionary()); 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(LuaType t) + private static T Convert(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() diff --git a/Ycicle/Program.cs b/Ycicle/Program.cs index 625a622..9564f81 100644 --- a/Ycicle/Program.cs +++ b/Ycicle/Program.cs @@ -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,33 +37,24 @@ namespace Ycicle continue; } - if (showTranspiledOutput) + var evaluate = script.Evaluate(); + if (script.Diagnostics.Messages.Count > 0) { - var transpiler = new Transpiler(); - var transpiled = transpiler.TranspilerToCSharp(script.Bind()); - Console.WriteLine(transpiled); + Console.ForegroundColor = ConsoleColor.Red; + Console.WriteLine("Errors were found during evaluating"); + foreach (var diagnosticsMessage in script.Diagnostics.Messages) + { + LogError(diagnosticsMessage); + } + Console.ResetColor(); } else { - var evaluate = script.Evaluate(); if (evaluate == null) continue; - if (script.Diagnostics.Messages.Count > 0) - { - Console.ForegroundColor = ConsoleColor.Red; - Console.WriteLine("Errors were found during evaluating"); - foreach (var diagnosticsMessage in script.Diagnostics.Messages) - { - LogError(diagnosticsMessage); - } - Console.ResetColor(); - } - else - { - Console.ForegroundColor = ConsoleColor.Cyan; - Console.WriteLine(evaluate); - Console.ResetColor(); - } + Console.ForegroundColor = ConsoleColor.Cyan; + Console.WriteLine(evaluate); + Console.ResetColor(); } } }