From 5e9111a08529a5ce71fc62b5c34a721766230df2 Mon Sep 17 00:00:00 2001 From: Alexander Luzgarev Date: Thu, 16 Jul 2020 11:50:19 +0200 Subject: [PATCH] Fix REPL context handling --- Parser/Compilation.cs | 4 ++-- Parser/Evaluator.cs | 40 ++++++++++++---------------------------- Repl/MRepl.cs | 2 +- cmi/Program.cs | 2 +- 4 files changed, 16 insertions(+), 32 deletions(-) diff --git a/Parser/Compilation.cs b/Parser/Compilation.cs index 8744a58..d566e7e 100644 --- a/Parser/Compilation.cs +++ b/Parser/Compilation.cs @@ -21,7 +21,7 @@ namespace Parser return Binder.BindProgram(_syntaxTree); } - public EvaluationResult Evaluate(CompilationContext context) + public EvaluationResult Evaluate(CompilationContext context, bool inRepl) { var program = GetBoundProgram(); if (program.Diagnostics.Length > 0) @@ -29,7 +29,7 @@ namespace Parser return new EvaluationResult(null, program.Diagnostics); } - var evaluator = new Evaluator(program, context); + var evaluator = new Evaluator(program, context, inRepl); return evaluator.Evaluate(); } } diff --git a/Parser/Evaluator.cs b/Parser/Evaluator.cs index bb06ca0..efa86c8 100644 --- a/Parser/Evaluator.cs +++ b/Parser/Evaluator.cs @@ -13,15 +13,16 @@ namespace Parser private readonly BoundProgram _program; private readonly CompilationContext _context; private readonly DiagnosticsBag _diagnostics = new DiagnosticsBag(); - private bool _insideFunction = false; + private bool _inRepl = false; private readonly Stack _scopeStack = new Stack(); - public Evaluator(BoundProgram program, CompilationContext context) + public Evaluator(BoundProgram program, CompilationContext context, bool inRepl) { _program = program; _context = context; var outerScope = new EvaluationScope(); _scopeStack.Push(outerScope); + _inRepl = inRepl; } internal EvaluationResult Evaluate() @@ -33,7 +34,7 @@ namespace Parser _diagnostics.ReportNotEnoughInputs( new TextSpan(mainFunction.Declaration.Position, mainFunction.Declaration.Position + mainFunction.Declaration.FullWidth), mainFunction.Name); - return new EvaluationResult(null, _diagnostics.ToImmutableArray()); + return new EvaluationResult(null, _diagnostics.ToImmutableArray()); } else { @@ -458,44 +459,27 @@ namespace Parser private MObject? GetVariableValue(string name) { - if (_insideFunction) + if (_inRepl) { - if (_context.Variables.TryGetValue(name, out var globalValue)) - { - return globalValue; - } - - var currentScope = _scopeStack.Peek(); - return currentScope.Variables.TryGetValue(name, out var localValue) ? globalValue : null; + return _context.Variables.TryGetValue(name, out var globalValue) ? globalValue : null; } else { - if (_context.Variables.TryGetValue(name, out var globalValue)) - { - return globalValue; - } - - return null; + var currentScope = _scopeStack.Peek(); + return currentScope.Variables.TryGetValue(name, out var localValue) ? localValue : null; } } private void SetVariableValue(string name, MObject value) { - if (_insideFunction) + if (_inRepl) { - if (_context.Variables.ContainsKey(name)) - { - _context.Variables[name] = value; - } - else - { - var currentScope = _scopeStack.Peek(); - currentScope.Variables[name] = value; - } + _context.Variables[name] = value; } else { - _context.Variables[name] = value; + var currentScope = _scopeStack.Peek(); + currentScope.Variables[name] = value; } } diff --git a/Repl/MRepl.cs b/Repl/MRepl.cs index ed902d0..c8b1b81 100644 --- a/Repl/MRepl.cs +++ b/Repl/MRepl.cs @@ -65,7 +65,7 @@ namespace Repl TreeRenderer.RenderTree(tree); var compilation = Compilation.Create(tree); - var evaluationResult = compilation.Evaluate(_context); + var evaluationResult = compilation.Evaluate(_context, inRepl: true); foreach (var diagnostic in evaluationResult.Diagnostics) { diff --git a/cmi/Program.cs b/cmi/Program.cs index ec5f7fe..f1f0042 100644 --- a/cmi/Program.cs +++ b/cmi/Program.cs @@ -36,7 +36,7 @@ namespace cmi } var context = new CompilationContext(); - var evaluationResult = compilation.Evaluate(context); + var evaluationResult = compilation.Evaluate(context, inRepl: false); foreach (var diagnostic in evaluationResult.Diagnostics) {