Made strings indexable

This commit is contained in:
Deukhoofd 2018-11-19 13:53:44 +01:00
parent b3b26964cc
commit 86447d0a36
No known key found for this signature in database
GPG Key ID: B4C087AC81641654
4 changed files with 72 additions and 11 deletions

View File

@ -1,9 +1,10 @@
using System; using System;
using Upsilon.BaseTypes.Number; using Upsilon.BaseTypes.Number;
using Upsilon.Evaluator;
namespace Upsilon.BaseTypes namespace Upsilon.BaseTypes
{ {
internal class LuaString : LuaType internal class LuaString : LuaType, IIndexable
{ {
public LuaString(string value) public LuaString(string value)
{ {
@ -76,5 +77,11 @@ namespace Upsilon.BaseTypes
{ {
return Value; return Value;
} }
public LuaType Get(string s, EvaluationScope scope)
{
var i = int.Parse(s);
return new LuaString(Value[i - 1].ToString());
}
} }
} }

View File

@ -1,7 +1,4 @@
using System.Collections;
using System.Collections.Generic;
using System.Linq; using System.Linq;
using Upsilon.Binder;
using Upsilon.Evaluator; using Upsilon.Evaluator;
namespace Upsilon.BaseTypes namespace Upsilon.BaseTypes

View File

@ -416,15 +416,21 @@ namespace Upsilon.Binder
private BoundExpression BindIndexExpression(IndexExpressionSyntax e) private BoundExpression BindIndexExpression(IndexExpressionSyntax e)
{ {
var expression = BindExpression(e.Expression); 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); 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());
}
} }
} }
} }

View File

@ -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);
}
}
}