Made strings indexable
This commit is contained in:
parent
b3b26964cc
commit
86447d0a36
|
@ -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());
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
|
@ -0,0 +1,51 @@
|
|||
using Upsilon.Evaluator;
|
||||
using Xunit;
|
||||
|
||||
namespace UpsilonTests
|
||||
{
|
||||
public class StringTests
|
||||
{
|
||||
[Fact]
|
||||
public void BasicStringVariable()
|
||||
{
|
||||
const string input = @"
|
||||
string = ""test""
|
||||
return string
|
||||
";
|
||||
var script = new Script(input);
|
||||
Assert.Empty(script.Diagnostics.Messages);
|
||||
var evaluated = script.Evaluate<string>();
|
||||
Assert.Empty(script.Diagnostics.Messages);
|
||||
Assert.Equal("test", evaluated);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void StringIndexable()
|
||||
{
|
||||
const string input = @"
|
||||
string = ""test""
|
||||
return string[3]
|
||||
";
|
||||
var script = new Script(input);
|
||||
Assert.Empty(script.Diagnostics.Messages);
|
||||
var evaluated = script.Evaluate<string>();
|
||||
Assert.Empty(script.Diagnostics.Messages);
|
||||
Assert.Equal("s", evaluated);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void StringAddition()
|
||||
{
|
||||
const string input = @"
|
||||
string = ""test"" + ""123""
|
||||
return string
|
||||
";
|
||||
var script = new Script(input);
|
||||
Assert.Empty(script.Diagnostics.Messages);
|
||||
var evaluated = script.Evaluate<string>();
|
||||
Assert.Empty(script.Diagnostics.Messages);
|
||||
Assert.Equal("test123", evaluated);
|
||||
}
|
||||
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue