From ba82b28ee49bb621b11385949767014846fb3767 Mon Sep 17 00:00:00 2001 From: Deukhoofd Date: Wed, 28 Nov 2018 16:47:52 +0100 Subject: [PATCH] Validate function call parameter types --- Upsilon/Binder/Binder.cs | 33 ++++++++++++++++++++++++++++----- 1 file changed, 28 insertions(+), 5 deletions(-) diff --git a/Upsilon/Binder/Binder.cs b/Upsilon/Binder/Binder.cs index 4a31e11..d5cffc0 100644 --- a/Upsilon/Binder/Binder.cs +++ b/Upsilon/Binder/Binder.cs @@ -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; }