Binds Variable Assignment

This commit is contained in:
Deukhoofd 2018-11-11 20:31:55 +01:00
parent ce92348199
commit ac83dba77f
No known key found for this signature in database
GPG Key ID: B4C087AC81641654
5 changed files with 61 additions and 8 deletions

View File

@ -36,7 +36,7 @@ namespace Upsilon.Binder
case SyntaxKind.ParenthesizedExpression: case SyntaxKind.ParenthesizedExpression:
return BindParenthesizedExpression((ParenthesizedExpressionSyntax) e); return BindParenthesizedExpression((ParenthesizedExpressionSyntax) e);
case SyntaxKind.AssignmentExpression: case SyntaxKind.AssignmentExpression:
break; return BindAssignmentExpression((AssignmentExpressionSyntax) e);
case SyntaxKind.VariableExpression: case SyntaxKind.VariableExpression:
break; break;
case SyntaxKind.BadExpression: case SyntaxKind.BadExpression:
@ -101,9 +101,34 @@ namespace Upsilon.Binder
return new BoundLiteralExpression(outValue, type); return new BoundLiteralExpression(outValue, type);
} }
private BoundExpression BindParenthesizedExpression(ParenthesizedExpressionSyntax syntax) private BoundExpression BindParenthesizedExpression(ParenthesizedExpressionSyntax e)
{ {
return BindExpression(syntax.Expression); return BindExpression(e.Expression);
}
private BoundExpression BindAssignmentExpression(AssignmentExpressionSyntax e)
{
var name = e.Identifier.Name;
var boundExpression = BindExpression(e.Expression);
if (!_scope.TryGetVariable(name, out var variable))
{
variable = new VariableSymbol(name, boundExpression.Type);
if (e.LocalToken != null)
_scope.SetVariable(variable);
else
_scope.SetGlobalVariable(variable);
}
else
{
if (boundExpression.Type != variable.Type)
{
_diagnostics.LogCannotConvert(boundExpression.Type, variable.Type, e.Span);
return boundExpression;
}
}
return new BoundAssignmenExpression(variable, boundExpression);
} }
} }
} }

View File

@ -0,0 +1,19 @@
using Upsilon.BaseTypes;
namespace Upsilon.Binder
{
public class BoundAssignmenExpression : BoundExpression
{
public BoundAssignmenExpression(VariableSymbol variable, BoundExpression expression)
{
Variable = variable;
Expression = expression;
}
public VariableSymbol Variable { get; }
public BoundExpression Expression { get; }
public override BoundKind Kind => BoundKind.BoundAssignmentExpression;
public override Type Type { get; }
}
}

View File

@ -6,6 +6,7 @@ namespace Upsilon.Binder
BoundLiteralExpression, BoundLiteralExpression,
BoundBinaryExpression, BoundBinaryExpression,
BoundUnaryExpression BoundUnaryExpression,
BoundAssignmentExpression
} }
} }

View File

@ -55,6 +55,10 @@ namespace Upsilon
LogError($"No unary operator {text} found for type '{inType}'", eSpan); LogError($"No unary operator {text} found for type '{inType}'", eSpan);
} }
public void LogCannotConvert(Type actualType, Type expectedType, TextSpan span)
{
LogError($"Cannot convert type '{actualType}' to '{expectedType}'", span);
}
} }
public class DiagnosticsMessage public class DiagnosticsMessage

View File

@ -1,4 +1,5 @@
using System; using System;
using System.Collections.Generic;
using Upsilon.BaseTypes.Number; using Upsilon.BaseTypes.Number;
using Upsilon.Binder; using Upsilon.Binder;
using Upsilon.Parser; using Upsilon.Parser;
@ -9,6 +10,7 @@ namespace Upsilon.Evaluator
{ {
private readonly Diagnostics _diagnostics; private readonly Diagnostics _diagnostics;
public Script Script { get; } public Script Script { get; }
private Dictionary<string, object> _variables = new Dictionary<string, object>();
public Evaluator(Script script, Diagnostics diagnostics) public Evaluator(Script script, Diagnostics diagnostics)
{ {
@ -31,6 +33,8 @@ namespace Upsilon.Evaluator
return EvaluateBinaryExpression((BoundBinaryExpression) e); return EvaluateBinaryExpression((BoundBinaryExpression) e);
case BoundKind.BoundUnaryExpression: case BoundKind.BoundUnaryExpression:
return EvaluateUnaryExpression((BoundUnaryExpression) e); return EvaluateUnaryExpression((BoundUnaryExpression) e);
case BoundKind.BoundAssignmentExpression:
return EvaluateAssignmentExpression((BoundAssignmenExpression) e);
default: default:
throw new NotImplementedException(); throw new NotImplementedException();
} }
@ -75,13 +79,13 @@ namespace Upsilon.Evaluator
} }
} }
/* private object EvaluateAssignmentExpression(BoundAssignmenExpression e)
private object EvaluateAssignmentExpression(AssignmentExpressionSyntax e)
{ {
var variableName = e.Identifier.Name; var variableName = e.Variable.Name;
var val = EvaluateExpression(e.Expression); var val = EvaluateExpression(e.Expression);
_variables[variableName] = val;
return val; return val;
}*/ }
private object EvaluateVariableExpression(VariableExpressionSyntax e) private object EvaluateVariableExpression(VariableExpressionSyntax e)
{ {