Validate function call parameter types

This commit is contained in:
Deukhoofd 2018-11-28 16:47:52 +01:00
parent a64df2ff6b
commit ba82b28ee4
No known key found for this signature in database
GPG Key ID: B4C087AC81641654
1 changed files with 28 additions and 5 deletions

View File

@ -194,7 +194,6 @@ namespace Upsilon.Binder
var expression = BindExpression(e.Identifier);
if (expression.Type != Type.Function && expression.Type != Type.Unknown)
{
//TODO Better error
_diagnostics.LogError($"Unknown function called.", e.Span);
return new BoundLiteralExpression(new ScriptNull(), e.Span);
}
@ -232,9 +231,33 @@ namespace Upsilon.Binder
returnType = function.ResultType;
var pars = function.Parameters;
if (pars.Length != parameters.Count)
{
_diagnostics.LogError(
$"Invalid number of parameters for function '{function.Name}'. Expected {pars.Length}, got {parameters.Count}",
e.Span);
}
else
{
for (var i = 0; i < pars.Length; i++)
{
var functionParameter = pars[i];
var callingParameter = parameters[i];
if (functionParameter.Type != Type.Unknown &&
callingParameter.Type != Type.Unknown && callingParameter.Type != Type.Nil)
{
if (callingParameter.Type != functionParameter.Type)
{
_diagnostics.LogError(
$"Invalid type for function '{function.Name}' at parameter '{functionParameter.Name}'. " +
$"Expected type '{functionParameter.Type}', got '{callingParameter.Type}'",
callingParameter.Span);
}
}
}
}
}
//TODO: validate parameters
return new BoundFunctionCallExpression(expression, parameters.ToImmutable(), e.Span, returnType);
}
@ -470,7 +493,7 @@ namespace Upsilon.Binder
foreach (var parameter in func.Parameters)
{
var vari = new VariableSymbol(parameter.VariableSymbol.Name, Type.Unknown, true);
var vari = new VariableSymbol(parameter.VariableSymbol.Name, parameter.VariableSymbol.Type, true);
parameters.Add(vari);
innerScope.DefineLocalVariable(vari);
}
@ -499,7 +522,7 @@ namespace Upsilon.Binder
// don't allow assigning different typed variables to a variable, unless either of them is nil, allow assigning nil to all variables
if (variable.Type != Type.Function)
{
if (variable.Type == Type.Nil )
if (variable.Type == Type.Nil || variable.Type == Type.Unknown)
{
variable.Type = Type.Function;
}