More logical handling of return values

This commit is contained in:
Deukhoofd 2018-11-16 14:11:27 +01:00
parent 62f31ef0d3
commit 62ad100aee
No known key found for this signature in database
GPG Key ID: B4C087AC81641654
3 changed files with 17 additions and 11 deletions

View File

@ -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)

View File

@ -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;
}

View File

@ -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;