Bind Variables

This commit is contained in:
Deukhoofd 2018-11-11 20:41:24 +01:00
parent ac83dba77f
commit 05d0de31a0
No known key found for this signature in database
GPG Key ID: B4C087AC81641654
3 changed files with 32 additions and 3 deletions

View File

@ -1,5 +1,4 @@
using System;
using System.Runtime.InteropServices;
using Upsilon.BaseTypes.Number;
using Upsilon.Parser;
using Type = Upsilon.BaseTypes.Type;
@ -38,7 +37,7 @@ namespace Upsilon.Binder
case SyntaxKind.AssignmentExpression:
return BindAssignmentExpression((AssignmentExpressionSyntax) e);
case SyntaxKind.VariableExpression:
break;
return BindVariableExpression((VariableExpressionSyntax) e);
case SyntaxKind.BadExpression:
break;
case SyntaxKind.ScriptUnit:
@ -130,5 +129,17 @@ namespace Upsilon.Binder
return new BoundAssignmenExpression(variable, boundExpression);
}
private BoundExpression BindVariableExpression(VariableExpressionSyntax e)
{
var name = e.Identifier.Name;
if (!_scope.TryGetVariable(name, out var variable))
{
_diagnostics.LogUnknownVariable(e.Identifier.Span, name);
return new BoundLiteralExpression(null, Type.Nil);
}
return new BoundVariableExpression(variable);
}
}
}

View File

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

View File

@ -0,0 +1,17 @@
using System;
using Type = Upsilon.BaseTypes.Type;
namespace Upsilon.Binder
{
public class BoundVariableExpression : BoundExpression
{
public BoundVariableExpression(VariableSymbol variable)
{
Variable = variable;
}
public VariableSymbol Variable { get; }
public override BoundKind Kind => BoundKind.VariableExpression;
public override Type Type => Variable.Type;
}
}