From 3d811ff80193950f24928822ee831a7ab0ea2129 Mon Sep 17 00:00:00 2001 From: Deukhoofd Date: Sun, 11 Nov 2018 21:03:50 +0100 Subject: [PATCH] More work on variables --- Upsilon/Binder/BoundScope.cs | 8 +++++++- Upsilon/Evaluator/Evaluator.cs | 22 +++++++++++++--------- Upsilon/Evaluator/Script.cs | 6 +++--- Yc/Program.cs | 5 +++-- 4 files changed, 26 insertions(+), 15 deletions(-) diff --git a/Upsilon/Binder/BoundScope.cs b/Upsilon/Binder/BoundScope.cs index 348cfbd..c47ce65 100644 --- a/Upsilon/Binder/BoundScope.cs +++ b/Upsilon/Binder/BoundScope.cs @@ -1,4 +1,5 @@ using System.Collections.Generic; +using System.Linq; namespace Upsilon.Binder { @@ -10,7 +11,12 @@ namespace Upsilon.Binder public BoundScope(BoundScope parentScope) { _parentScope = parentScope; - _variables = new Dictionary(); + _variables = new Dictionary(); + } + public BoundScope(Dictionary variables, BoundScope parentScope) + { + _parentScope = parentScope; + _variables = variables.ToDictionary(x => x.Key.Name, x => x.Key); } public void SetVariable(VariableSymbol var) diff --git a/Upsilon/Evaluator/Evaluator.cs b/Upsilon/Evaluator/Evaluator.cs index ea9abcb..82063d4 100644 --- a/Upsilon/Evaluator/Evaluator.cs +++ b/Upsilon/Evaluator/Evaluator.cs @@ -2,7 +2,6 @@ using System; using System.Collections.Generic; using Upsilon.BaseTypes.Number; using Upsilon.Binder; -using Upsilon.Parser; namespace Upsilon.Evaluator { @@ -10,12 +9,18 @@ namespace Upsilon.Evaluator { private readonly Diagnostics _diagnostics; public Script Script { get; } - private Dictionary _variables = new Dictionary(); + private readonly Dictionary _variables = new Dictionary(); public Evaluator(Script script, Diagnostics diagnostics) { _diagnostics = diagnostics; - Script = script; + Script = script; + } + public Evaluator(Script script, Diagnostics diagnostics, Dictionary vars) + { + _diagnostics = diagnostics; + Script = script; + _variables = vars; } public object Evaluate(BoundScript e) @@ -35,6 +40,8 @@ namespace Upsilon.Evaluator return EvaluateUnaryExpression((BoundUnaryExpression) e); case BoundKind.BoundAssignmentExpression: return EvaluateAssignmentExpression((BoundAssignmenExpression) e); + case BoundKind.VariableExpression: + return EvaluateVariableExpression((BoundVariableExpression) e); default: throw new NotImplementedException(); } @@ -81,17 +88,14 @@ namespace Upsilon.Evaluator private object EvaluateAssignmentExpression(BoundAssignmenExpression e) { - var variableName = e.Variable.Name; var val = EvaluateExpression(e.Expression); - _variables[variableName] = val; + _variables[e.Variable] = val; return val; } - private object EvaluateVariableExpression(VariableExpressionSyntax e) + private object EvaluateVariableExpression(BoundVariableExpression e) { - var varName = e.Identifier.Name; - _diagnostics.LogUnknownVariable(e.Span, varName); - return null; + return _variables[e.Variable]; } } diff --git a/Upsilon/Evaluator/Script.cs b/Upsilon/Evaluator/Script.cs index 08a537e..9b00a95 100644 --- a/Upsilon/Evaluator/Script.cs +++ b/Upsilon/Evaluator/Script.cs @@ -13,13 +13,13 @@ namespace Upsilon.Evaluator public Diagnostics Diagnostics { get; } private Binder.Binder Binder { get; } - public Script(string scriptString) + public Script(string scriptString, Dictionary variables = null) { ScriptString = new SourceText(scriptString); Diagnostics = new Diagnostics(ScriptString); _parsed = Parser.Parser.Parse(scriptString, Diagnostics); - Binder = new Binder.Binder(new BoundScope(null), Diagnostics); - Evaluator = new Evaluator(this, Diagnostics); + Binder = new Binder.Binder(new BoundScope(variables, null), Diagnostics); + Evaluator = new Evaluator(this, Diagnostics, variables); } public object Evaluate() diff --git a/Yc/Program.cs b/Yc/Program.cs index 0c0004a..4917efc 100644 --- a/Yc/Program.cs +++ b/Yc/Program.cs @@ -1,6 +1,7 @@ using System; using System.Collections.Generic; using Upsilon; +using Upsilon.Binder; using Upsilon.Evaluator; namespace Yc @@ -10,7 +11,7 @@ namespace Yc static void Main(string[] args) { Console.WriteLine("Upsilon REPL"); - Dictionary variables = new Dictionary(); + Dictionary variables = new Dictionary(); while (true) { Console.Write("» "); @@ -20,7 +21,7 @@ namespace Yc return; } - var parsed = new Script(input); + var parsed = new Script(input, variables); if (parsed.Diagnostics.Messages.Count > 0) { Console.ForegroundColor = ConsoleColor.Red;