2018-11-27 17:38:28 +00:00
|
|
|
using System.Collections.Generic;
|
2018-11-18 13:18:24 +00:00
|
|
|
using Upsilon.BaseTypes;
|
2019-02-13 15:29:58 +00:00
|
|
|
using Upsilon.BaseTypes.ScriptTypeInterfaces;
|
|
|
|
using Upsilon.Evaluator;
|
|
|
|
using Upsilon.Exceptions;
|
2018-11-18 13:18:24 +00:00
|
|
|
using Upsilon.Parser;
|
2018-11-25 18:30:18 +00:00
|
|
|
using Upsilon.Text;
|
2018-11-18 13:18:24 +00:00
|
|
|
|
|
|
|
namespace Upsilon.Binder
|
|
|
|
{
|
|
|
|
public class BoundIndexExpression : BoundExpression
|
|
|
|
{
|
2019-01-26 12:23:12 +00:00
|
|
|
public BoundIndexExpression(BoundExpression identifier, BoundExpression index, TypeContainer type, TextSpan span) : base(span)
|
2018-11-18 13:18:24 +00:00
|
|
|
{
|
|
|
|
Identifier = identifier;
|
|
|
|
Index = index;
|
2019-02-13 15:29:58 +00:00
|
|
|
ValueType = type;
|
2018-11-18 13:18:24 +00:00
|
|
|
}
|
|
|
|
|
2018-11-18 15:08:20 +00:00
|
|
|
public BoundExpression Identifier { get; }
|
2018-11-18 13:18:24 +00:00
|
|
|
public BoundExpression Index { get; }
|
|
|
|
|
|
|
|
public override BoundKind Kind => BoundKind.BoundIndexExpression;
|
2018-11-25 18:30:18 +00:00
|
|
|
|
2019-01-17 12:56:53 +00:00
|
|
|
public override IEnumerable<BoundNode> GetChildren()
|
|
|
|
{
|
|
|
|
yield return Identifier;
|
|
|
|
yield return Index;
|
|
|
|
}
|
|
|
|
|
2019-02-13 15:29:58 +00:00
|
|
|
public override TypeContainer ValueType { get; }
|
|
|
|
internal override ScriptType Evaluate(EvaluationScope scope, Diagnostics diagnostics, ref EvaluationState state)
|
|
|
|
{
|
|
|
|
var variable = Identifier.Evaluate(scope, diagnostics, ref state);
|
|
|
|
if (variable.Type == Type.Nil)
|
|
|
|
{
|
|
|
|
throw new EvaluationException(state.Script.FileName, $"Nil variable can't be indexed", Span);
|
|
|
|
}
|
|
|
|
if (!(variable is IIndexable indexable))
|
|
|
|
{
|
|
|
|
throw new EvaluationException(state.Script.FileName,
|
|
|
|
$"Variable of type '{variable.Type}' is not indexable.", Span);
|
|
|
|
}
|
|
|
|
|
|
|
|
var innerScope = scope;
|
|
|
|
if (variable is IScopeOwner scopeOwner)
|
|
|
|
{
|
|
|
|
innerScope = scopeOwner.EvaluationScope;
|
|
|
|
}
|
|
|
|
|
|
|
|
var indexer = Index.Evaluate(scope, diagnostics, ref state);
|
|
|
|
return indexable.Get(diagnostics, Span, indexer, innerScope);
|
|
|
|
|
|
|
|
}
|
2018-11-18 13:18:24 +00:00
|
|
|
}
|
2018-11-20 13:05:34 +00:00
|
|
|
|
|
|
|
public class BoundFullStopIndexExpression : BoundExpression
|
|
|
|
{
|
|
|
|
public BoundExpression Expression { get; }
|
|
|
|
public string Index { get; }
|
|
|
|
|
2019-01-19 15:38:33 +00:00
|
|
|
public BoundFullStopIndexExpression(BoundExpression expression, string index, TypeContainer type, TextSpan span) : base(span)
|
2018-11-20 13:05:34 +00:00
|
|
|
{
|
|
|
|
Expression = expression;
|
|
|
|
Index = index;
|
2019-02-13 15:29:58 +00:00
|
|
|
ValueType = type;
|
2018-11-20 13:05:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public override BoundKind Kind => BoundKind.BoundFullstopIndexExpression;
|
2018-11-25 18:30:18 +00:00
|
|
|
|
2019-01-17 12:56:53 +00:00
|
|
|
public override IEnumerable<BoundNode> GetChildren()
|
|
|
|
{
|
|
|
|
yield return Expression;
|
|
|
|
}
|
|
|
|
|
2019-02-13 15:29:58 +00:00
|
|
|
public override TypeContainer ValueType { get; }
|
|
|
|
internal override ScriptType Evaluate(EvaluationScope scope, Diagnostics diagnostics, ref EvaluationState state)
|
|
|
|
{
|
|
|
|
var variable = Expression.Evaluate(scope, diagnostics, ref state);
|
|
|
|
if (variable.Type == Type.Nil)
|
|
|
|
{
|
|
|
|
throw new EvaluationException(state.Script.FileName, $"Nil variable can't be indexed", Span);
|
|
|
|
}
|
|
|
|
if (!(variable is IIndexable indexable))
|
|
|
|
{
|
|
|
|
throw new EvaluationException(state.Script.FileName,
|
|
|
|
$"Variable of type '{variable.Type}' is not indexable.", Span);
|
|
|
|
}
|
|
|
|
|
|
|
|
var innerScope = scope;
|
|
|
|
if (variable is IScopeOwner scopeOwner)
|
|
|
|
{
|
|
|
|
innerScope = scopeOwner.EvaluationScope;
|
|
|
|
}
|
|
|
|
|
|
|
|
return indexable.Get(diagnostics, Span, Index.ToScriptType(), innerScope);
|
|
|
|
|
|
|
|
}
|
2018-11-20 13:05:34 +00:00
|
|
|
}
|
2018-11-18 13:18:24 +00:00
|
|
|
}
|