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:
return BindParenthesizedExpression((ParenthesizedExpressionSyntax) e);
case SyntaxKind.AssignmentExpression:
break;
return BindAssignmentExpression((AssignmentExpressionSyntax) e);
case SyntaxKind.VariableExpression:
break;
case SyntaxKind.BadExpression:
@ -101,9 +101,34 @@ namespace Upsilon.Binder
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,
BoundBinaryExpression,
BoundUnaryExpression
BoundUnaryExpression,
BoundAssignmentExpression
}
}

View File

@ -55,6 +55,10 @@ namespace Upsilon
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

View File

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