diff --git a/Upsilon/BaseTypes/LuaString.cs b/Upsilon/BaseTypes/LuaString.cs index 32da32d..b373da1 100644 --- a/Upsilon/BaseTypes/LuaString.cs +++ b/Upsilon/BaseTypes/LuaString.cs @@ -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()); + } } } \ No newline at end of file diff --git a/Upsilon/BaseTypes/LuaTable.cs b/Upsilon/BaseTypes/LuaTable.cs index a1fe201..518be13 100644 --- a/Upsilon/BaseTypes/LuaTable.cs +++ b/Upsilon/BaseTypes/LuaTable.cs @@ -1,7 +1,4 @@ -using System.Collections; -using System.Collections.Generic; using System.Linq; -using Upsilon.Binder; using Upsilon.Evaluator; namespace Upsilon.BaseTypes diff --git a/Upsilon/Binder/Binder.cs b/Upsilon/Binder/Binder.cs index 1053bde..b67b34f 100644 --- a/Upsilon/Binder/Binder.cs +++ b/Upsilon/Binder/Binder.cs @@ -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()); + } } } } \ No newline at end of file diff --git a/UpsilonTests/StringTests.cs b/UpsilonTests/StringTests.cs new file mode 100644 index 0000000..e2fe05a --- /dev/null +++ b/UpsilonTests/StringTests.cs @@ -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(); + 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(); + 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(); + Assert.Empty(script.Diagnostics.Messages); + Assert.Equal("test123", evaluated); + } + + } +} \ No newline at end of file