Upsilon/Upsilon/Binder/BoundExpressions/BoundIndexExpression.cs

117 lines
4.3 KiB
C#
Raw Normal View History

using System.Collections.Generic;
2018-11-18 13:18:24 +00:00
using Upsilon.BaseTypes;
using Upsilon.BaseTypes.ScriptTypeInterfaces;
using Upsilon.Evaluator;
using Upsilon.Exceptions;
2018-11-18 13:18:24 +00:00
using Upsilon.Parser;
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;
ValueType = type;
2018-11-18 13:18:24 +00:00
}
public BoundExpression Identifier { get; }
2018-11-18 13:18:24 +00:00
public BoundExpression Index { get; }
public override BoundKind Kind => BoundKind.BoundIndexExpression;
2019-01-17 12:56:53 +00:00
public override IEnumerable<BoundNode> GetChildren()
{
yield return Identifier;
yield return Index;
}
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;
}
if (variable.Type == Type.Unknown)
{
throw new EvaluationException(state.Script.FileName,
$"Variable of type '{variable.Type}' is not a registered type, and can't be indexed.", Span);
}
if (variable.Type == Type.UserData && variable.Type is UndefinedUserDataTypeContainer userDataTypeContainer)
{
throw new EvaluationException(state.Script.FileName,
$"Variable of type '{userDataTypeContainer.UserData}' is not a registered type, and can't be indexed.", Span);
}
var indexer = Index.Evaluate(scope, diagnostics, ref state);
return indexable.Get(diagnostics, Span, indexer, innerScope);
}
2018-11-18 13:18:24 +00:00
}
public class BoundFullStopIndexExpression : BoundExpression
{
public BoundExpression Expression { get; }
public string Index { get; }
public BoundFullStopIndexExpression(BoundExpression expression, string index, TypeContainer type, TextSpan span) : base(span)
{
Expression = expression;
Index = index;
ValueType = type;
}
public override BoundKind Kind => BoundKind.BoundFullstopIndexExpression;
2019-01-17 12:56:53 +00:00
public override IEnumerable<BoundNode> GetChildren()
{
yield return Expression;
}
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);
}
if (variable.Type == Type.UserData && variable.Type is UndefinedUserDataTypeContainer userDataTypeContainer)
{
throw new EvaluationException(state.Script.FileName,
$"Variable of type '{userDataTypeContainer.UserData}' is not a registered type, and can't be indexed.", Span);
}
var innerScope = scope;
if (variable is IScopeOwner scopeOwner)
{
innerScope = scopeOwner.EvaluationScope;
}
return indexable.Get(diagnostics, Span, Index.ToScriptType(), innerScope);
}
}
2018-11-18 13:18:24 +00:00
}