Upsilon/Upsilon/Evaluator/Evaluator.cs

774 lines
32 KiB
C#
Raw Normal View History

2018-11-10 12:11:36 +00:00
using System;
2018-11-15 19:13:53 +00:00
using System.Collections.Generic;
2018-12-11 17:31:54 +00:00
using System.Threading;
using Upsilon.BaseTypes;
2018-11-11 17:12:42 +00:00
using Upsilon.BaseTypes.Number;
2018-11-30 10:15:52 +00:00
using Upsilon.BaseTypes.ScriptFunction;
using Upsilon.BaseTypes.ScriptTable;
2018-11-23 17:18:07 +00:00
using Upsilon.BaseTypes.ScriptTypeInterfaces;
using Upsilon.BaseTypes.UserData;
2018-11-11 17:12:42 +00:00
using Upsilon.Binder;
using Upsilon.Binder.VariableSymbols;
2018-12-11 17:31:54 +00:00
using Upsilon.Evaluator.Debugging;
2018-12-03 15:05:14 +00:00
using Upsilon.Exceptions;
using Upsilon.Text;
2018-12-11 17:31:54 +00:00
using Upsilon.Utilities;
using Type = Upsilon.BaseTypes.Type;
2018-11-10 12:11:36 +00:00
namespace Upsilon.Evaluator
{
internal class Evaluator : IDisposable
2018-11-10 12:11:36 +00:00
{
private readonly Script _script;
private Diagnostics _diagnostics;
2018-11-23 17:18:07 +00:00
private ScriptType _lastValue;
private ScriptType _returnValue;
internal EvaluationScope Scope { get; private set; }
2018-11-16 12:45:03 +00:00
private bool HasReturned { get; set; }
2018-11-23 14:27:48 +00:00
private bool HasBroken { get; set; }
2018-11-10 16:00:39 +00:00
internal Evaluator(Diagnostics diagnostics, Dictionary<string, ScriptType> variables, Script script)
2018-11-11 20:03:50 +00:00
{
_diagnostics = diagnostics;
_script = script;
2018-11-15 19:13:53 +00:00
Scope = new EvaluationScope(variables);
2018-11-10 16:00:39 +00:00
}
internal Evaluator(Diagnostics diagnostics, EvaluationScope parentScope, Script script)
2018-11-16 12:45:03 +00:00
{
_diagnostics = diagnostics;
_script = script;
2018-11-16 12:45:03 +00:00
Scope = new EvaluationScope(parentScope);
}
private Evaluator(Script script)
{
_script = script;
}
internal static Evaluator CreateWithSetScope(Diagnostics diagnostics, EvaluationScope scope, Script script)
{
return new Evaluator(script) {_diagnostics = diagnostics, Scope = scope};
}
public void Dispose()
{
Scope.Dispose();
_lastValue = null;
_returnValue = null;
}
2018-11-23 17:18:07 +00:00
public ScriptType Evaluate(BoundScript e)
2018-11-10 12:11:36 +00:00
{
2018-12-11 17:31:54 +00:00
if (DebugSession.DebuggerAttached && !string.IsNullOrWhiteSpace(e.FileName))
{
var file = DebugSession.GetFileInfo(e.FileName);
if (file != null)
{
foreach (var breakpoint in file.Breakpoints)
{
var debugStatement = e.GetBottomStatementAtPosition(breakpoint.Line, breakpoint.Position);
2018-12-11 17:31:54 +00:00
debugStatement.HasBreakpoint = true;
}
}
}
EvaluateNode(e.Statement);
2018-11-16 13:11:27 +00:00
if (_returnValue == null)
return _lastValue;
return _returnValue;
2018-11-16 12:45:03 +00:00
}
public ScriptType Evaluate(BoundScript e, string functionName, object[] parameters)
{
if (!Scope.TryGet(functionName, out var statement) || statement.Type != Type.Function)
{
throw new ArgumentException(($"Function '{functionName}' could not be found"));
}
var function = (ScriptRuntimeFunction) statement;
var innerEvaluator = new Evaluator(_diagnostics, Scope, _script);
if (parameters != null)
{
for (var index = 0; index < parameters.Length; index++)
{
object parameter;
if (index < parameters.Length)
{
parameter = parameters[index];
}
else
{
parameter = null;
}
UserDataVariableSymbol parameterSymbol;
if (index < function.Parameters.Length)
{
parameterSymbol = (UserDataVariableSymbol)function.Parameters[index].VariableSymbol;
}
else
{
continue;
}
if (parameterSymbol.BoundTypeDefinition != null && parameter != null)
{
bool isCompatible = false;
var parameterType = parameter.GetType();
foreach (var validType in parameterSymbol.BoundTypeDefinition.ValidInternalTypes)
{
if (validType.IsAssignableFrom(parameterType))
{
isCompatible = true;
break;
}
}
if (!isCompatible)
{
throw new EvaluationException(
$"Parameter '{parameterSymbol.Name}' of function '{functionName}' can't handle the given object with type '{parameterType}'");
}
}
var parameterConverted = parameter == null ? new ScriptNull() : parameter.ToScriptType();
innerEvaluator.Scope.CreateLocal(parameterSymbol, parameterConverted);
}
}
var result = innerEvaluator.EvaluateNode(function.Block);
return result;
}
2018-11-23 17:18:07 +00:00
internal ScriptType EvaluateNode(BoundNode b)
2018-11-16 12:45:03 +00:00
{
switch (b.Kind)
{
case BoundKind.BoundScript:
EvaluateStatement(((BoundScript)b).Statement);
break;
case BoundKind.BoundLiteralExpression:
case BoundKind.BoundBinaryExpression:
case BoundKind.BoundUnaryExpression:
case BoundKind.VariableExpression:
case BoundKind.BoundFunctionCallExpression:
case BoundKind.BoundFunctionExpression:
2018-11-17 18:13:05 +00:00
case BoundKind.BoundTableExpression:
case BoundKind.BoundIndexExpression:
case BoundKind.BoundFullstopIndexExpression:
2018-11-16 13:11:27 +00:00
_lastValue = EvaluateExpression((BoundExpression) b);
2018-11-16 12:45:03 +00:00
break;
case BoundKind.BoundAssignmentStatement:
case BoundKind.BoundExpressionStatement:
case BoundKind.BoundBlockStatement:
case BoundKind.BoundIfStatement:
case BoundKind.BoundElseStatement:
case BoundKind.BoundFunctionAssignmentStatement:
2018-11-16 12:45:03 +00:00
case BoundKind.BoundPromise:
case BoundKind.BoundReturnStatement:
2018-11-19 15:22:13 +00:00
case BoundKind.BoundTableAssigmentStatement:
case BoundKind.BoundMultiAssignmentStatement:
2018-11-23 13:38:45 +00:00
case BoundKind.BoundNumericForStatement:
2018-11-23 17:18:07 +00:00
case BoundKind.BoundGenericForStatement:
case BoundKind.BoundBreakStatement:
2018-12-07 18:17:49 +00:00
case BoundKind.BoundWhileStatement:
2018-11-16 12:45:03 +00:00
EvaluateStatement((BoundStatement) b);
break;
default:
throw new ArgumentOutOfRangeException();
}
2018-11-16 13:11:27 +00:00
return _returnValue;
}
private void EvaluateStatement(BoundStatement e)
{
2018-11-16 12:45:03 +00:00
if (HasReturned)
return;
switch (e.Kind)
{
case BoundKind.BoundAssignmentStatement:
EvaluateAssignmentStatement((BoundVariableAssignment) e);
break;
case BoundKind.BoundBlockStatement:
EvaluateBoundBlockStatement((BoundBlockStatement) e);
break;
2018-11-13 11:48:50 +00:00
case BoundKind.BoundIfStatement:
EvaluateBoundIfStatement((BoundIfStatement) e);
break;
2018-11-16 12:45:03 +00:00
case BoundKind.BoundReturnStatement:
EvaluateReturnStatement((BoundReturnStatement) e);
break;
case BoundKind.BoundFunctionAssignmentStatement:
EvaluateBoundFunctionAssigmentStatement((BoundFunctionAssignmentStatement) e);
break;
2018-11-19 15:22:13 +00:00
case BoundKind.BoundTableAssigmentStatement:
EvaluateTableAssignmentStatement((BoundTableAssigmentStatement) e);
break;
case BoundKind.BoundMultiAssignmentStatement:
EvaluateMultiAssignmentStatement((BoundMultiAssignmentStatement) e);
break;
2018-11-23 13:38:45 +00:00
case BoundKind.BoundNumericForStatement:
EvaluateNumericForStatement((BoundNumericForStatement) e);
break;
2018-11-23 14:27:48 +00:00
case BoundKind.BoundBreakStatement:
HasBroken = true;
return;
2018-11-23 17:18:07 +00:00
case BoundKind.BoundGenericForStatement:
EvaluateGenericForStatement((BoundGenericForStatement) e);
break;
2018-12-07 18:17:49 +00:00
case BoundKind.BoundWhileStatement:
EvaluateWhileStatement((BoundWhileStatement) e);
break;
default:
EvaluateExpressionStatement((BoundExpressionStatement) e);
break;
}
}
private void EvaluateExpressionStatement(BoundExpressionStatement e)
{
2018-11-16 13:11:27 +00:00
_lastValue = EvaluateExpression(e.Expression);
2018-11-10 12:11:36 +00:00
}
2018-11-23 17:18:07 +00:00
internal ScriptType EvaluateExpression(BoundExpression e)
2018-11-10 12:11:36 +00:00
{
switch (e.Kind)
{
2018-11-11 17:12:42 +00:00
case BoundKind.BoundLiteralExpression:
return ((BoundLiteralExpression) e).Value;
case BoundKind.BoundBinaryExpression:
return EvaluateBinaryExpression((BoundBinaryExpression) e);
2018-11-11 18:56:53 +00:00
case BoundKind.BoundUnaryExpression:
return EvaluateUnaryExpression((BoundUnaryExpression) e);
2018-11-11 20:03:50 +00:00
case BoundKind.VariableExpression:
return EvaluateVariableExpression((BoundVariableExpression) e);
2018-11-15 19:13:53 +00:00
case BoundKind.BoundFunctionCallExpression:
return EvaluateBoundFunctionCallExpression((BoundFunctionCallExpression) e);
2018-11-17 18:13:05 +00:00
case BoundKind.BoundTableExpression:
return EvaluateTableExpression((BoundTableExpression) e);
2018-11-18 13:18:24 +00:00
case BoundKind.BoundIndexExpression:
return EvaluateIndexExpression((BoundIndexExpression) e);
case BoundKind.BoundFunctionExpression:
return EvaluateBoundFunctionStatement((BoundFunctionExpression) e);
case BoundKind.BoundPromise:
return EvaluateUnboundFunctionStatement((UnboundFunctionExpression) e);
case BoundKind.BoundFullstopIndexExpression:
return EvaluateFullStopIndexExpression((BoundFullStopIndexExpression) e);
2018-11-10 12:11:36 +00:00
default:
2019-01-17 16:48:10 +00:00
throw new NotImplementedException(e.Kind.ToString());
2018-11-10 12:11:36 +00:00
}
}
2018-11-23 17:18:07 +00:00
private ScriptType EvaluateUnaryExpression(BoundUnaryExpression e)
2018-11-10 12:11:36 +00:00
{
2018-11-11 18:56:53 +00:00
var operand = EvaluateExpression(e.InExpression);
2018-11-10 12:11:36 +00:00
switch (e.Operator.Kind)
{
2018-11-11 18:56:53 +00:00
case BoundUnaryOperator.OperatorKind.Identity:
2018-11-10 12:11:36 +00:00
return operand;
2018-11-11 18:56:53 +00:00
case BoundUnaryOperator.OperatorKind.Negation:
if (operand.Type == Type.Number)
2018-11-23 17:18:07 +00:00
return -((ScriptNumber)operand);
else if (operand.Type == Type.UserData)
{
var ud = (GenericUserData) operand;
var (type, failed) = ud.UnaryOperator(operand, OperatorType.UnaryNegation);
if (failed) goto default;
return type;
}
goto default;
2018-11-11 18:56:53 +00:00
case BoundUnaryOperator.OperatorKind.LogicalNegation:
if (operand.Type == Type.Boolean)
2018-11-23 17:18:07 +00:00
return !(ScriptBoolean) operand;
else if (operand.Type == Type.UserData)
{
var ud = (GenericUserData) operand;
var (type, failed) = ud.UnaryOperator(operand, OperatorType.LogicalNot);
if (failed) goto default;
return type;
}
goto default;
2018-11-24 14:11:33 +00:00
case BoundUnaryOperator.OperatorKind.Length:
if (operand is ILengthType length)
{
return length.Length();
}
goto default;
2018-11-10 12:11:36 +00:00
default:
throw new Exception("Invalid Unary Operator: " + e.Operator.Kind);
}
2018-11-11 18:56:53 +00:00
}
2018-11-10 12:11:36 +00:00
2018-11-23 17:18:07 +00:00
private ScriptType EvaluateBinaryExpression(BoundBinaryExpression e)
2018-11-10 12:11:36 +00:00
{
2018-12-09 10:29:11 +00:00
if (e.Operator.Kind == BoundBinaryOperator.OperatorKind.Or)
{
var l = EvaluateExpression(e.LeftExpression);
if (l.Type == Type.Boolean && ((ScriptBoolean)l).Value)
return new ScriptBoolean(true);
var r = EvaluateExpression(e.RightExpression);
if (r.Type == Type.Boolean && ((ScriptBoolean)r).Value)
return new ScriptBoolean(true);
return new ScriptBoolean(false);
}
if (e.Operator.Kind == BoundBinaryOperator.OperatorKind.And)
{
var l = EvaluateExpression(e.LeftExpression);
if (l.Type != Type.Boolean || !((ScriptBoolean)l).Value)
return new ScriptBoolean(false);
var r = EvaluateExpression(e.RightExpression);
if (r.Type != Type.Boolean || !((ScriptBoolean)r).Value)
return new ScriptBoolean(false);
return new ScriptBoolean(true);
}
2018-11-11 17:12:42 +00:00
var left = EvaluateExpression(e.LeftExpression);
var right = EvaluateExpression(e.RightExpression);
switch (e.Operator.Kind)
2018-11-11 09:26:52 +00:00
{
2018-11-11 17:12:42 +00:00
case BoundBinaryOperator.OperatorKind.Addition:
2018-11-17 14:57:26 +00:00
if (left.Type == Type.Number)
2018-11-23 17:18:07 +00:00
{
if (right.Type == Type.Number)
{
return ((ScriptNumber)left) + ((ScriptNumber)right);
}
}
2018-11-17 14:57:26 +00:00
else if (left.Type == Type.String)
{
2018-11-23 17:18:07 +00:00
return ((ScriptString) left) + right;
2018-11-17 14:57:26 +00:00
}
else if (left.Type == Type.UserData)
{
var ud = (GenericUserData) left;
var (type, failed) = ud.BinaryOperator(left, OperatorType.Addition, right);
if (failed) goto default;
return type;
}
2018-11-17 14:57:26 +00:00
goto default;
2018-11-11 17:12:42 +00:00
case BoundBinaryOperator.OperatorKind.Subtraction:
if (left.Type == Type.Number && right.Type == Type.Number)
{
2018-11-23 17:18:07 +00:00
return ((ScriptNumber)left) - ((ScriptNumber)right);
}
else if (left.Type == Type.UserData)
{
var ud = (GenericUserData) left;
var (type, failed) = ud.BinaryOperator(left, OperatorType.Subtraction, right);
if (failed) goto default;
return type;
}
goto default;
2018-11-11 17:12:42 +00:00
case BoundBinaryOperator.OperatorKind.Multiplication:
if (left.Type == Type.Number && right.Type == Type.Number)
{
2018-11-23 17:18:07 +00:00
return ((ScriptNumber)left) * ((ScriptNumber)right);
}
else if (left.Type == Type.UserData)
{
var ud = (GenericUserData) left;
var (type, failed) = ud.BinaryOperator(left, OperatorType.Multiplication, right);
if (failed) goto default;
return type;
}
goto default;
2018-11-11 17:12:42 +00:00
case BoundBinaryOperator.OperatorKind.Division:
if (left.Type == Type.Number && right.Type == Type.Number)
{
2018-11-23 17:18:07 +00:00
return ((ScriptNumber)left) / ((ScriptNumber)right);
}
else if (left.Type == Type.UserData)
{
var ud = (GenericUserData) left;
var (type, failed) = ud.BinaryOperator(left, OperatorType.Division, right);
if (failed) goto default;
return type;
}
goto default;
case BoundBinaryOperator.OperatorKind.Exponent:
if (left.Type == Type.Number && right.Type == Type.Number)
{
return ScriptNumber.Exponent((ScriptNumber) left, (ScriptNumber) right);
}
goto default;
case BoundBinaryOperator.OperatorKind.Remainder:
if (left.Type == Type.Number && right.Type == Type.Number)
{
return ((ScriptNumber)left) % ((ScriptNumber)right);
}
goto default;
2018-11-11 17:12:42 +00:00
case BoundBinaryOperator.OperatorKind.Equality:
2018-12-09 10:45:38 +00:00
return new ScriptBoolean(left.Equals(right));
2018-11-11 17:12:42 +00:00
case BoundBinaryOperator.OperatorKind.Inequality:
2018-12-09 10:45:38 +00:00
return new ScriptBoolean(!left.Equals(right));
2018-12-03 15:05:14 +00:00
case BoundBinaryOperator.OperatorKind.Less:
if (left.Type == Type.Number && right.Type == Type.Number)
{
return ((ScriptNumber)left) < ((ScriptNumber)right);
}
ThrowException($"Can't find operator for types '{left.Type}' and '{right.Type}'", e.Span);
return new ScriptNull();
case BoundBinaryOperator.OperatorKind.LessEquals:
if (left.Type == Type.Number && right.Type == Type.Number)
{
return ((ScriptNumber)left) <= ((ScriptNumber)right);
}
ThrowException($"Can't find operator for types '{left.Type}' and '{right.Type}'", e.Span);
return new ScriptNull();
case BoundBinaryOperator.OperatorKind.Greater:
if (left.Type == Type.Number && right.Type == Type.Number)
{
return ((ScriptNumber)left) > ((ScriptNumber)right);
}
ThrowException($"Can't find operator for types '{left.Type}' and '{right.Type}'", e.Span);
return new ScriptNull();
case BoundBinaryOperator.OperatorKind.GreaterEquals:
if (left.Type == Type.Number && right.Type == Type.Number)
{
return ((ScriptNumber)left) >= ((ScriptNumber)right);
}
ThrowException($"Can't find operator for types '{left.Type}' and '{right.Type}'", e.Span);
return new ScriptNull();
2018-11-11 17:12:42 +00:00
default:
throw new Exception("Invalid Binary Operator: " + e.Operator.Kind);
2018-11-10 12:11:36 +00:00
}
}
2018-12-03 15:05:14 +00:00
private void ThrowException(string message, TextSpan location)
{
throw new ScriptRuntimeException(message, location, _diagnostics.ScriptString.GetSpan(location));
2018-12-03 15:05:14 +00:00
}
private void EvaluateAssignmentStatement(BoundVariableAssignment e)
2018-11-10 16:00:39 +00:00
{
var val = EvaluateExpression(e.BoundExpression);
2018-11-24 11:42:54 +00:00
if (e.IsLocalDefinition)
2018-11-14 15:39:52 +00:00
{
Scope.CreateLocal(e.Variable.VariableSymbol, val);
2018-11-14 15:39:52 +00:00
}
else
{
Scope.AssignToNearest(e.Variable.VariableSymbol, val);
2018-11-14 15:39:52 +00:00
}
2018-11-16 13:11:27 +00:00
_lastValue = val;
2018-11-11 19:31:55 +00:00
}
2018-11-10 16:00:39 +00:00
private void EvaluateMultiAssignmentStatement(BoundMultiAssignmentStatement e)
{
var val = EvaluateExpression(e.Assignment);
if (val is IIndexable table)
{
for (var i = 0; i < e.Variables.Length; i++)
{
var variableSymbol = e.Variables[i];
if (variableSymbol == null)
continue;
if (variableSymbol.Name == "_")
continue;
2018-11-23 17:18:07 +00:00
var value = table.Get(_diagnostics, e.Span, new ScriptNumberLong(i + 1), Scope);
if (variableSymbol.Local)
2018-11-24 11:42:54 +00:00
Scope.CreateLocal(variableSymbol, value);
else
2018-11-24 11:42:54 +00:00
Scope.AssignToNearest(variableSymbol, value);
}
}
else
{
_diagnostics.LogError($"Can't assign type '{val.Type}' to multiple variables.", e.Span);
}
_lastValue = val;
}
2018-11-23 17:18:07 +00:00
private ScriptType EvaluateVariableExpression(BoundVariableExpression e)
2018-11-10 16:00:39 +00:00
{
if (Scope.TryGet(e.Variable.VariableSymbol, out var val))
2018-11-14 15:39:52 +00:00
{
return val;
}
throw new Exception($"Cannot find variable: '{e.Variable.VariableSymbol.Name}'");
2018-11-10 16:00:39 +00:00
}
private readonly Stack<BoundStatement> _todoStatements = new Stack<BoundStatement>();
2018-12-11 17:31:54 +00:00
private void EvaluateBoundBlockStatement(BoundBlockStatement boundBlockStatement)
{
for (var index = boundBlockStatement.Statements.Length - 1; index >= 0; index--)
{
var s = boundBlockStatement.Statements[index];
_todoStatements.Push(s);
}
2018-12-11 17:31:54 +00:00
while (true)
{
2018-12-11 17:31:54 +00:00
if (DebugSession.Debugging)
{
Thread.Sleep(10);
continue;
}
2018-11-16 12:45:03 +00:00
if (HasReturned)
return;
2018-11-23 14:27:48 +00:00
if (HasBroken)
return;
2018-12-11 17:31:54 +00:00
if (_todoStatements.Count == 0)
return;
var boundStatement = _todoStatements.Pop();
2018-12-11 17:31:54 +00:00
if (DebugSession.DebuggerAttached && boundStatement.HasBreakpoint)
{
DebugSession.TriggerBreakpoint(Scope);
continue;
}
2018-11-15 19:13:53 +00:00
EvaluateStatement(boundStatement);
}
}
2018-11-13 11:48:50 +00:00
private void EvaluateBoundIfStatement(BoundIfStatement boundBlockStatement)
{
2018-11-23 14:27:48 +00:00
var condition = EvaluateExpression(boundBlockStatement.Condition.Expression);
2018-11-23 17:18:07 +00:00
if ((ScriptBoolean) condition)
2018-11-13 11:48:50 +00:00
{
2018-11-23 14:27:48 +00:00
EvaluateStatement(boundBlockStatement.Block);
2018-11-13 11:48:50 +00:00
}
2018-11-13 14:15:44 +00:00
else if (boundBlockStatement.NextElseIf != null)
{
2018-11-23 14:27:48 +00:00
EvaluateStatement(boundBlockStatement.NextElseIf);
2018-11-13 14:15:44 +00:00
}
2018-11-13 12:54:51 +00:00
else if (boundBlockStatement.ElseStatement != null)
{
2018-11-23 14:27:48 +00:00
EvaluateStatement(boundBlockStatement.ElseStatement.Block);
2018-11-13 12:54:51 +00:00
}
2018-11-13 11:48:50 +00:00
}
2018-11-15 14:51:05 +00:00
private void EvaluateBoundFunctionAssigmentStatement(BoundFunctionAssignmentStatement e)
2018-11-15 14:51:05 +00:00
{
var func = EvaluateBoundFunctionStatement(e.Func);
if (e.Variable.Local)
2018-11-24 11:42:54 +00:00
Scope.CreateLocal(e.Variable, func);
2018-11-15 14:51:05 +00:00
else
2018-11-24 11:42:54 +00:00
Scope.AssignToNearest(e.Variable, func);
2018-11-15 19:13:53 +00:00
}
2018-11-23 17:18:07 +00:00
private ScriptType EvaluateBoundFunctionStatement(BoundFunctionExpression boundFunctionExpression)
{
var func = new ScriptRuntimeFunction(boundFunctionExpression.Parameters, boundFunctionExpression.Block, Scope);
return func;
}
2018-11-23 17:18:07 +00:00
private ScriptType EvaluateUnboundFunctionStatement(UnboundFunctionExpression unboundFunctionExpression)
{
var func = new ScriptRuntimeFunction(unboundFunctionExpression.Parameters, unboundFunctionExpression.Block, Scope);
return func;
}
2018-11-23 17:18:07 +00:00
private ScriptType EvaluateBoundFunctionCallExpression(BoundFunctionCallExpression boundFunctionCallExpression)
2018-11-15 19:13:53 +00:00
{
2018-11-18 19:20:03 +00:00
var variable = EvaluateExpression(boundFunctionCallExpression.Identifier);
2018-11-23 17:18:07 +00:00
if (!(variable is ScriptFunction function))
2018-11-15 19:13:53 +00:00
{
throw new Exception($"Variable is not a function.");
2018-11-15 19:13:53 +00:00
}
2018-11-23 17:18:07 +00:00
var ls = new List<ScriptType>();
foreach (var t in boundFunctionCallExpression.Parameters)
2018-11-15 19:13:53 +00:00
{
var evaluate = EvaluateExpression(t);
ls.Add(evaluate);
2018-11-15 19:13:53 +00:00
}
2018-12-11 14:50:24 +00:00
var val = function.Run(_diagnostics, ls.ToArray(), _script, Scope, boundFunctionCallExpression.Span);
return val;
2018-11-15 14:51:05 +00:00
}
2018-11-16 12:45:03 +00:00
private void EvaluateReturnStatement(BoundReturnStatement b)
{
2018-11-28 13:13:39 +00:00
_returnValue = b.Expression == null ? null : EvaluateExpression(b.Expression);
2018-11-16 13:11:27 +00:00
_lastValue = _returnValue;
2018-11-16 12:45:03 +00:00
HasReturned = true;
}
2018-11-23 17:18:07 +00:00
private ScriptType EvaluateTableExpression(BoundTableExpression e)
2018-11-17 18:13:05 +00:00
{
2018-11-19 11:17:21 +00:00
var tableScope = EvaluationScope.CreateWithGetOnlyParent(Scope);
var innerEvaluator = new Evaluator(_diagnostics, tableScope, _script);
var currentPos = 1;
var isNumerated = true;
foreach (var boundStatement in e.Statements)
2018-11-17 18:13:05 +00:00
{
2018-11-19 11:17:21 +00:00
switch (boundStatement.Kind)
{
2018-11-19 11:17:21 +00:00
case BoundKind.BoundAssignmentStatement:
case BoundKind.BoundFunctionExpression:
case BoundKind.BoundFunctionAssignmentStatement:
innerEvaluator.EvaluateNode(boundStatement);
isNumerated = false;
2018-11-19 11:17:21 +00:00
break;
default:
{
innerEvaluator.EvaluateStatement(boundStatement);
if (innerEvaluator._lastValue != null)
2018-11-24 11:42:54 +00:00
tableScope.CreateLocal(new VariableSymbol(currentPos.ToString(), innerEvaluator._lastValue.Type, false),
innerEvaluator._lastValue);
2018-11-19 11:17:21 +00:00
innerEvaluator._lastValue = null;
break;
}
}
2018-11-19 11:17:21 +00:00
currentPos++;
2018-11-17 18:13:05 +00:00
}
if (isNumerated)
{
return new NumeratedScriptTable(tableScope);
}
return new GenericKeyedScriptTable(tableScope);
2018-11-18 13:18:24 +00:00
}
2018-11-23 17:18:07 +00:00
private ScriptType EvaluateIndexExpression(BoundIndexExpression e)
2018-11-18 13:18:24 +00:00
{
2018-11-18 19:20:03 +00:00
var variable = EvaluateExpression(e.Identifier);
if (!(variable is IIndexable indexable))
2018-11-18 13:18:24 +00:00
{
2018-11-18 19:20:03 +00:00
throw new Exception("Variable is not indexable.");
2018-11-18 13:18:24 +00:00
}
2018-11-19 11:17:21 +00:00
var scope = Scope;
if (variable is IScopeOwner scopeOwner)
{
scope = scopeOwner.EvaluationScope;
}
var indexer = EvaluateExpression(e.Index);
return indexable.Get(_diagnostics, e.Span, indexer, scope);
2018-11-17 18:13:05 +00:00
}
2018-11-23 17:18:07 +00:00
private ScriptType EvaluateFullStopIndexExpression(BoundFullStopIndexExpression e)
{
var variable = EvaluateExpression(e.Expression);
if (!(variable is IIndexable indexable))
{
throw new Exception("Variable is not indexable.");
}
var scope = Scope;
if (variable is IScopeOwner scopeOwner)
{
scope = scopeOwner.EvaluationScope;
}
return indexable.Get(_diagnostics, e.Span, e.Index.ToScriptType(), scope);
}
2018-11-19 15:22:13 +00:00
private void EvaluateTableAssignmentStatement(BoundTableAssigmentStatement e)
{
2018-11-23 17:18:07 +00:00
ScriptType table;
ScriptType index;
if (e.TableIndexExpression.Kind == BoundKind.BoundIndexExpression)
{
table = EvaluateExpression(((BoundIndexExpression)e.TableIndexExpression).Identifier);
index = EvaluateExpression(((BoundIndexExpression)e.TableIndexExpression).Index);
}
else
{
table = EvaluateExpression(((BoundFullStopIndexExpression)e.TableIndexExpression).Expression);
index = ((BoundFullStopIndexExpression) e.TableIndexExpression).Index.ToScriptType();
}
2018-11-19 15:22:13 +00:00
var value = EvaluateExpression(e.Value);
2018-11-19 16:22:25 +00:00
if (!(table is IIndexable indexable))
2018-11-19 15:22:13 +00:00
{
2018-11-19 16:22:25 +00:00
throw new Exception("Cant assign to that");
2018-11-19 15:22:13 +00:00
}
indexable.Set(_diagnostics, e.Span, index.ToString().ToScriptType(), value);
2018-11-19 15:22:13 +00:00
}
2018-11-23 13:38:45 +00:00
private void EvaluateNumericForStatement(BoundNumericForStatement e)
{
var innerEvaluator = new Evaluator(_diagnostics, Scope, _script);
2018-11-23 17:18:07 +00:00
var startVal = (ScriptNumberLong)innerEvaluator.EvaluateExpression(e.BoundStart);
2018-11-24 11:42:54 +00:00
innerEvaluator.Scope.CreateLocal(e.Variable, startVal);
2018-11-23 17:18:07 +00:00
var stopVal = (ScriptNumberLong)innerEvaluator.EvaluateExpression(e.BoundStop);
2018-11-23 13:38:45 +00:00
long step = 1;
if (e.BoundStep != null)
{
2018-11-23 17:18:07 +00:00
var stepVal = (ScriptNumberLong)innerEvaluator.EvaluateExpression(e.BoundStep);
2018-11-23 13:38:45 +00:00
step = stepVal.Value;
}
if (step > 0)
{
for (; startVal.Value <= stopVal.Value; startVal.Value = startVal.Value + step)
{
innerEvaluator.EvaluateBoundBlockStatement(e.Block);
2018-11-23 14:27:48 +00:00
if (innerEvaluator.HasBroken)
break;
2018-11-23 13:38:45 +00:00
}
}
else if (step < 0)
{
for (; startVal.Value >= stopVal.Value; startVal.Value = startVal.Value + step)
{
innerEvaluator.EvaluateBoundBlockStatement(e.Block);
2018-11-23 14:27:48 +00:00
if (innerEvaluator.HasBroken)
break;
2018-11-23 13:38:45 +00:00
}
}
}
2018-11-23 17:18:07 +00:00
private void EvaluateGenericForStatement(BoundGenericForStatement e)
{
var innerEvaluator = new Evaluator(_diagnostics, Scope, _script);
2018-11-23 17:18:07 +00:00
var enumeratorObject = EvaluateExpression(e.BoundEnumerableExpression);
if (!(enumeratorObject is IIterable iterable))
{
throw new Exception($"Can't iterate over object with type '{enumeratorObject.Type}'");
}
using (var enumerator = iterable.GetScriptEnumerator())
2018-11-23 17:18:07 +00:00
{
while (enumerator.MoveNext())
{
var current = enumerator.Current;
2018-12-07 14:17:24 +00:00
if (current == null)
{
throw new Exception($"Can't assign result value of nothing to multiple values");
}
if (current.Type != Type.Table)
{
throw new Exception($"Can't assign result value with type '{current.Type}' to multiple values");
}
var table = (SimpleScriptTable)current;
2018-11-23 17:18:07 +00:00
if (e.Variables[0].Name != "_")
innerEvaluator.Scope.CreateLocal(e.Variables[0], table[0].ToScriptType());
2018-11-23 17:18:07 +00:00
if (e.Variables[1].Name != "_")
innerEvaluator.Scope.CreateLocal(e.Variables[1], table[1]);
2018-11-23 17:18:07 +00:00
innerEvaluator.EvaluateBoundBlockStatement((BoundBlockStatement) e.Block);
2018-12-09 13:33:04 +00:00
if (innerEvaluator.HasBroken || innerEvaluator.HasReturned)
{
if (innerEvaluator.HasReturned)
{
HasReturned = innerEvaluator.HasReturned;
_returnValue = innerEvaluator._returnValue;
}
2018-11-23 17:18:07 +00:00
break;
}
2018-11-23 17:18:07 +00:00
}
}
}
2018-12-07 18:17:49 +00:00
private void EvaluateWhileStatement(BoundWhileStatement e)
{
var innerEvaluator = new Evaluator(_diagnostics, Scope, _script);
var block = (BoundBlockStatement) e.Block;
2018-12-09 13:33:04 +00:00
while ((ScriptBoolean)innerEvaluator.EvaluateExpression(e.Condition))
2018-12-07 18:17:49 +00:00
{
innerEvaluator.EvaluateBoundBlockStatement(block);
2018-12-09 13:33:04 +00:00
if (innerEvaluator.HasBroken || innerEvaluator.HasReturned)
{
if (innerEvaluator.HasReturned)
{
HasReturned = innerEvaluator.HasReturned;
_returnValue = innerEvaluator._returnValue;
}
2018-12-09 13:33:04 +00:00
break;
}
2018-12-07 18:17:49 +00:00
}
}
2018-11-10 12:11:36 +00:00
}
}