Made strings indexable
This commit is contained in:
@@ -1,9 +1,10 @@
|
||||
using System;
|
||||
using Upsilon.BaseTypes.Number;
|
||||
using Upsilon.Evaluator;
|
||||
|
||||
namespace Upsilon.BaseTypes
|
||||
{
|
||||
internal class LuaString : LuaType
|
||||
internal class LuaString : LuaType, IIndexable
|
||||
{
|
||||
public LuaString(string value)
|
||||
{
|
||||
@@ -76,5 +77,11 @@ namespace Upsilon.BaseTypes
|
||||
{
|
||||
return Value;
|
||||
}
|
||||
|
||||
public LuaType Get(string s, EvaluationScope scope)
|
||||
{
|
||||
var i = int.Parse(s);
|
||||
return new LuaString(Value[i - 1].ToString());
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,7 +1,4 @@
|
||||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using Upsilon.Binder;
|
||||
using Upsilon.Evaluator;
|
||||
|
||||
namespace Upsilon.BaseTypes
|
||||
|
||||
@@ -416,15 +416,21 @@ namespace Upsilon.Binder
|
||||
private BoundExpression BindIndexExpression(IndexExpressionSyntax e)
|
||||
{
|
||||
var expression = BindExpression(e.Expression);
|
||||
if (expression.Type != Type.Table && expression.Type != Type.Unknown)
|
||||
{
|
||||
//TODO: wrong type diagnostic
|
||||
throw new Exception(expression.Type.ToString());
|
||||
return new BoundLiteralExpression(new LuaNull());
|
||||
}
|
||||
|
||||
var index = BindExpression(e.Index);
|
||||
return new BoundIndexExpression(expression, index, Type.Unknown);
|
||||
switch (expression.Type)
|
||||
{
|
||||
case Type.Table:
|
||||
case Type.UserData:
|
||||
case Type.Unknown:
|
||||
return new BoundIndexExpression(expression, index, Type.Unknown);
|
||||
case Type.String when index.Type == Type.Number:
|
||||
return new BoundIndexExpression(expression, index, Type.String);
|
||||
default:
|
||||
//TODO: wrong type diagnostic
|
||||
throw new Exception(expression.Type.ToString());
|
||||
return new BoundLiteralExpression(new LuaNull());
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user