Don't allow using indexing to access local variables

This commit is contained in:
Deukhoofd 2019-01-19 14:12:16 +01:00
parent bee6fad947
commit e44116b2cd
No known key found for this signature in database
GPG Key ID: B4C087AC81641654
1 changed files with 18 additions and 4 deletions

View File

@ -384,6 +384,20 @@ namespace Upsilon.Binder
else if (indexerVariable.TypeContainer == Type.Table)
{
var tableVariable = (TableVariableSymbol)indexerVariable;
if (indexExpression.Index is BoundLiteralExpression literal)
{
if (literal.Value.Type == Type.Number || literal.Value.Type == Type.String)
{
var toString = literal.Value.ToString();
if (!tableVariable.Variables.TryGetValue(toString, out var variable) || variable.Local)
{
diagnostics?.LogError($"No variable '{toString}' found in table.", expression.Span);
return new VariableSymbol("", Type.Nil, true);
}
return new VariableSymbol("", variable.TypeContainer, true);
}
}
if (tableVariable.TypeContainer is CompositeTypeContainer compositeTypeContainer)
{
return new VariableSymbol("", compositeTypeContainer.Types[1], true);
@ -755,7 +769,7 @@ namespace Upsilon.Binder
var table = (BoundTableExpression)expression;
var realIndex = (BoundLiteralExpression) index;
var variableDic = table.Expressions;
if (variableDic.TryGetValue(realIndex.Value.ToString(), out var variable))
if (variableDic.TryGetValue(realIndex.Value.ToString(), out var variable) && !variable.Local)
{
return new BoundIndexExpression(expression, index, variable.TypeContainer, e.Span);
}
@ -775,7 +789,7 @@ namespace Upsilon.Binder
return new BoundIndexExpression(expression, index, Type.Unknown, e.Span);
}
var variableDic = tableSymbol.Variables;
if (variableDic.TryGetValue(realIndex.Value.ToString(), out var variable))
if (variableDic.TryGetValue(realIndex.Value.ToString(), out var variable) && !variable.Local)
{
return new BoundIndexExpression(expression, index, variable.TypeContainer, e.Span);
}
@ -809,7 +823,7 @@ namespace Upsilon.Binder
if (expression.Kind == BoundKind.BoundTableExpression)
{
var table = (BoundTableExpression)expression;
if (table.Expressions.TryGetValue(index, out var variable))
if (table.Expressions.TryGetValue(index, out var variable) && !variable.Local)
{
return new BoundFullStopIndexExpression(expression, index, variable.TypeContainer, e.Span);
}
@ -820,7 +834,7 @@ namespace Upsilon.Binder
var table = (BoundVariableExpression)expression;
var realTable = table.Variable;
var variableDic = ((TableVariableSymbol) realTable.VariableSymbol).Variables;
if (variableDic.TryGetValue(index, out var variable))
if (variableDic.TryGetValue(index, out var variable) && !variable.Local)
{
return new BoundFullStopIndexExpression(expression, index, variable.TypeContainer, e.Span);
}