From 82e13a85e20686fc11e2a4ee6acb8b1ce25bcd6f Mon Sep 17 00:00:00 2001 From: Deukhoofd Date: Wed, 14 Nov 2018 13:58:12 +0100 Subject: [PATCH] Change Binder variable type if value is assigned to null variable, or null is assigned to a variable type --- Upsilon/Binder/Binder.cs | 14 +++++++++++--- Upsilon/Binder/VariableSymbol.cs | 2 +- 2 files changed, 12 insertions(+), 4 deletions(-) diff --git a/Upsilon/Binder/Binder.cs b/Upsilon/Binder/Binder.cs index a216bbd..7ea651d 100644 --- a/Upsilon/Binder/Binder.cs +++ b/Upsilon/Binder/Binder.cs @@ -159,10 +159,18 @@ namespace Upsilon.Binder } else { - if (boundExpression.Type != variable.Type) + // don't allow assigning different typed variables to a variable, unless either of them is nil, allow assigning nil to all variables + if (boundExpression.Type != variable.Type && (variable.Type != Type.Nil || boundExpression.Type == Type.Nil)) { - _diagnostics.LogCannotConvert(boundExpression.Type, variable.Type, e.Span); - return new BoundExpressionStatement(boundExpression); + if (variable.Type == Type.Nil || boundExpression.Type == Type.Nil) + { + variable.Type = boundExpression.Type; + } + else + { + _diagnostics.LogCannotConvert(boundExpression.Type, variable.Type, e.Span); + return new BoundExpressionStatement(boundExpression); + } } } diff --git a/Upsilon/Binder/VariableSymbol.cs b/Upsilon/Binder/VariableSymbol.cs index 9ea1efd..900753a 100644 --- a/Upsilon/Binder/VariableSymbol.cs +++ b/Upsilon/Binder/VariableSymbol.cs @@ -10,7 +10,7 @@ namespace Upsilon.Binder Name = name; } - public Type Type { get; } + public Type Type { get; set; } public string Name { get; } } } \ No newline at end of file