Made resolvevariable in binder public static so VS Code extension can use it

This commit is contained in:
Deukhoofd 2018-12-12 16:34:47 +01:00
parent a4cdaa5b05
commit e741e9c355
No known key found for this signature in database
GPG Key ID: B4C087AC81641654
1 changed files with 27 additions and 13 deletions

View File

@ -12,7 +12,7 @@ using Type = Upsilon.BaseTypes.Type;
namespace Upsilon.Binder namespace Upsilon.Binder
{ {
internal class Binder : IDisposable public class Binder : IDisposable
{ {
private Diagnostics _diagnostics; private Diagnostics _diagnostics;
public BoundScope Scope { get; private set; } public BoundScope Scope { get; private set; }
@ -216,7 +216,11 @@ namespace Upsilon.Binder
} }
var returnType = Type.Unknown; var returnType = Type.Unknown;
var resolved = ResolveVariable(expression); var resolved = ResolveVariable(expression, _diagnostics);
if (resolved == null)
{
_diagnostics.LogError("Can't resolve variable", expression.Span);
}
if (resolved is FunctionVariableSymbol function) if (resolved is FunctionVariableSymbol function)
{ {
if (function is ScriptFunctionVariableSymbol scriptFunction) if (function is ScriptFunctionVariableSymbol scriptFunction)
@ -294,7 +298,7 @@ namespace Upsilon.Binder
return new BoundFunctionCallExpression(expression, parameters.ToImmutable(), e.Span, returnType); return new BoundFunctionCallExpression(expression, parameters.ToImmutable(), e.Span, returnType);
} }
private VariableSymbol ResolveVariable(BoundExpression expression) public static VariableSymbol ResolveVariable(BoundExpression expression, Diagnostics diagnostics)
{ {
if (expression.Kind == BoundKind.VariableExpression) if (expression.Kind == BoundKind.VariableExpression)
{ {
@ -305,20 +309,27 @@ namespace Upsilon.Binder
{ {
var fullStopIndexExpression = (BoundFullStopIndexExpression) expression; var fullStopIndexExpression = (BoundFullStopIndexExpression) expression;
var indexerExpression = fullStopIndexExpression.Expression; var indexerExpression = fullStopIndexExpression.Expression;
var indexerVariable = ResolveVariable(indexerExpression); var indexerVariable = ResolveVariable(indexerExpression, diagnostics);
if (indexerVariable.Type == Type.Table) if (indexerVariable == null)
{
diagnostics?.LogError("Can't resolve variable", expression.Span);
}
else if (indexerVariable.Type == Type.Table)
{ {
return ((TableVariableSymbol)indexerVariable).Variables[fullStopIndexExpression.Index]; return ((TableVariableSymbol)indexerVariable).Variables[fullStopIndexExpression.Index];
} }
if (indexerVariable.Type == Type.UserData) else if (indexerVariable.Type == Type.UserData)
{ {
var parent = var parent =
(UserDataBoundTypeDefinition) ((UserDataVariableSymbol) indexerVariable).BoundTypeDefinition; (UserDataBoundTypeDefinition) ((UserDataVariableSymbol) indexerVariable).BoundTypeDefinition;
var bDefProperty = parent.Properties[fullStopIndexExpression.Index.ToLowerInvariant()]; if (parent.Properties.TryGetValue(fullStopIndexExpression.Index.ToLowerInvariant(),
var boundDef = BoundTypeHandler.GetTypeDefinition(bDefProperty.ActualType); out var bDefProperty))
if (boundDef != null)
{ {
return new UserDataVariableSymbol(fullStopIndexExpression.Index, boundDef, parent); var boundDef = BoundTypeHandler.GetTypeDefinition(bDefProperty.ActualType);
if (boundDef != null)
{
return new UserDataVariableSymbol(fullStopIndexExpression.Index, boundDef, parent);
}
} }
} }
else if (indexerVariable.Type == Type.Unknown) else if (indexerVariable.Type == Type.Unknown)
@ -341,7 +352,6 @@ namespace Upsilon.Binder
{ {
return new VariableSymbol("", expression.Type, true); return new VariableSymbol("", expression.Type, true);
} }
_diagnostics.LogError("Can't resolve variable", expression.Span);
return null; return null;
} }
@ -773,8 +783,12 @@ namespace Upsilon.Binder
return new BoundFullStopIndexExpression(expression, index, Type.Unknown, e.Span); return new BoundFullStopIndexExpression(expression, index, Type.Unknown, e.Span);
} }
var variableSymbol = ResolveVariable(expression); var variableSymbol = ResolveVariable(expression, _diagnostics);
if (variableSymbol != null) if (variableSymbol == null)
{
_diagnostics.LogError("Can't resolve variable", expression.Span);
}
else
{ {
var functionParameter = (UserDataVariableSymbol) variableSymbol; var functionParameter = (UserDataVariableSymbol) variableSymbol;
var udBoundDef = (UserDataBoundTypeDefinition)functionParameter.BoundTypeDefinition; var udBoundDef = (UserDataBoundTypeDefinition)functionParameter.BoundTypeDefinition;