From 62ad100aeef31f3dd63698dbe37986fa3ab3b0bc Mon Sep 17 00:00:00 2001 From: Deukhoofd Date: Fri, 16 Nov 2018 14:11:27 +0100 Subject: [PATCH] More logical handling of return values --- Upsilon/Binder/Binder.cs | 2 +- Upsilon/Evaluator/Evaluator.cs | 24 ++++++++++++++---------- Ycicle/Program.cs | 2 ++ 3 files changed, 17 insertions(+), 11 deletions(-) diff --git a/Upsilon/Binder/Binder.cs b/Upsilon/Binder/Binder.cs index 6668987..8d7d7ac 100644 --- a/Upsilon/Binder/Binder.cs +++ b/Upsilon/Binder/Binder.cs @@ -181,7 +181,7 @@ namespace Upsilon.Binder } //TODO: validate parameters - return new BoundFunctionCallExpression((FunctionVariableSymbol) function, parameters.ToImmutable()); + return new BoundFunctionCallExpression(function, parameters.ToImmutable()); } private BoundExpression BindVariableExpression(VariableExpressionSyntax e) diff --git a/Upsilon/Evaluator/Evaluator.cs b/Upsilon/Evaluator/Evaluator.cs index 4b923f4..ad97b36 100644 --- a/Upsilon/Evaluator/Evaluator.cs +++ b/Upsilon/Evaluator/Evaluator.cs @@ -9,7 +9,8 @@ namespace Upsilon.Evaluator public class Evaluator { private readonly Diagnostics _diagnostics; - private LuaType _value; + private LuaType _lastValue; + private LuaType _returnValue; public EvaluationScope Scope { get; } private bool HasReturned { get; set; } @@ -28,7 +29,9 @@ namespace Upsilon.Evaluator public LuaType Evaluate(BoundScript e) { Evaluate(e.Statement); - return _value; + if (_returnValue == null) + return _lastValue; + return _returnValue; } private LuaType Evaluate(BoundNode b) @@ -43,7 +46,7 @@ namespace Upsilon.Evaluator case BoundKind.BoundUnaryExpression: case BoundKind.VariableExpression: case BoundKind.BoundFunctionCallExpression: - _value = EvaluateExpression((BoundExpression) b); + _lastValue = EvaluateExpression((BoundExpression) b); break; case BoundKind.BoundAssignmentStatement: case BoundKind.BoundExpressionStatement: @@ -57,7 +60,7 @@ namespace Upsilon.Evaluator default: throw new ArgumentOutOfRangeException(); } - return _value; + return _returnValue; } private void EvaluateStatement(BoundStatement e) @@ -93,7 +96,7 @@ namespace Upsilon.Evaluator private void EvaluateExpressionStatement(BoundExpressionStatement e) { - _value = EvaluateExpression(e.Expression); + _lastValue = EvaluateExpression(e.Expression); } private LuaType EvaluateExpression(BoundExpression e) @@ -165,7 +168,7 @@ namespace Upsilon.Evaluator { Scope.SetGlobal(e.Variable, val); } - _value = val; + _lastValue = val; } private LuaType EvaluateVariableExpression(BoundVariableExpression e) @@ -204,7 +207,7 @@ namespace Upsilon.Evaluator innerEvaluator.EvaluateStatement(boundBlockStatement.ElseStatement.Block); } HasReturned = innerEvaluator.HasReturned; - _value = innerEvaluator._value; + _lastValue = innerEvaluator._lastValue; } private void EvaluateBoundFunctionStatement(BoundFunctionStatement boundFunctionStatement) @@ -243,13 +246,14 @@ namespace Upsilon.Evaluator var parameterValue = innerEvaluator.EvaluateExpression(boundFunctionCallExpression.Parameters[i]); innerEvaluator.Scope.Set(parameterVariable, parameterValue); } - _value = innerEvaluator.Evaluate(function.Block); - return _value; + _lastValue = innerEvaluator.Evaluate(function.Block); + return _lastValue; } private void EvaluateReturnStatement(BoundReturnStatement b) { - _value = Evaluate(b.Expression); + _returnValue = EvaluateExpression(b.Expression); + _lastValue = _returnValue; HasReturned = true; } diff --git a/Ycicle/Program.cs b/Ycicle/Program.cs index 92c9704..b8e985b 100644 --- a/Ycicle/Program.cs +++ b/Ycicle/Program.cs @@ -51,6 +51,8 @@ namespace Ycicle else { var evaluate = parsed.Evaluate(); + if (evaluate == null) + continue; if (parsed.Diagnostics.Messages.Count > 0) { Console.ForegroundColor = ConsoleColor.Red;