Changed function variable declarations to BoundVariableSymbols

This commit is contained in:
Deukhoofd 2018-11-25 21:44:03 +01:00
parent d4fc24b389
commit 55206f3807
No known key found for this signature in database
GPG Key ID: B4C087AC81641654
7 changed files with 53 additions and 32 deletions

View File

@ -28,10 +28,10 @@ namespace Upsilon.BaseTypes
internal class ScriptInternalFunction : ScriptFunction, IScopeOwner
{
public BoundBlockStatement Block { get; }
public ImmutableArray<VariableSymbol> Parameters { get; }
public ImmutableArray<BoundVariableSymbol> Parameters { get; }
public EvaluationScope EvaluationScope { get; }
public ScriptInternalFunction(ImmutableArray<VariableSymbol> parameters, BoundBlockStatement block,
public ScriptInternalFunction(ImmutableArray<BoundVariableSymbol> parameters, BoundBlockStatement block,
EvaluationScope evaluationScope)
{
Parameters = parameters;
@ -46,7 +46,7 @@ namespace Upsilon.BaseTypes
{
var parameterVariable = Parameters[i];
var parameterValue = variables[i];
innerEvaluator.Scope.CreateLocal(parameterVariable, parameterValue);
innerEvaluator.Scope.CreateLocal(parameterVariable.VariableSymbol, parameterValue);
}
return innerEvaluator.EvaluateNode(Block);
}

View File

@ -44,7 +44,7 @@ namespace Upsilon.Binder
Scope = new BoundScope(Scope);
foreach (var valueParameter in unboundFunctionStatement.Value.Parameters)
{
Scope.AssignToNearest(valueParameter);
Scope.AssignToNearest(valueParameter.VariableSymbol);
}
unboundFunctionStatement.Value.Block =
(BoundBlockStatement) BindBlockStatement(unboundFunctionStatement.Value.UnboundBlock);
@ -377,11 +377,11 @@ namespace Upsilon.Binder
private BoundExpression BindFunctionExpression(FunctionExpressionSyntax e, string variableSymbol = null)
{
var innerScope = new BoundScope(Scope);
var parameters = ImmutableArray.CreateBuilder<VariableSymbol>();
var parameters = ImmutableArray.CreateBuilder<BoundVariableSymbol>();
foreach (var identifierToken in e.Parameters)
{
var vari = new VariableSymbol(identifierToken.Name, Type.Unknown, true);
parameters.Add(vari);
parameters.Add(new BoundVariableSymbol(vari, identifierToken.Span));
innerScope.DefineLocalVariable(vari);
}
@ -417,9 +417,9 @@ namespace Upsilon.Binder
var func = (BoundFunctionExpression)BindFunctionExpression(e.FunctionExpression, name);
var parameters = ImmutableArray.CreateBuilder<VariableSymbol>();
foreach (var identifierToken in func.Parameters)
foreach (var parameter in func.Parameters)
{
var vari = new VariableSymbol(identifierToken.Name, Type.Unknown, true);
var vari = new VariableSymbol(parameter.VariableSymbol.Name, Type.Unknown, true);
parameters.Add(vari);
innerScope.DefineLocalVariable(vari);
}

View File

@ -6,10 +6,10 @@ namespace Upsilon.Binder
{
public class BoundFunctionExpression : BoundExpression
{
public ImmutableArray<VariableSymbol> Parameters { get; }
public ImmutableArray<BoundVariableSymbol> Parameters { get; }
public BoundBlockStatement Block { get; set; }
public BoundFunctionExpression(ImmutableArray<VariableSymbol> parameters, BoundBlockStatement block, TextSpan span) : base(span)
public BoundFunctionExpression(ImmutableArray<BoundVariableSymbol> parameters, BoundBlockStatement block, TextSpan span) : base(span)
{
Parameters = parameters;
Block = block;
@ -18,6 +18,11 @@ namespace Upsilon.Binder
public override BoundKind Kind => BoundKind.BoundFunctionExpression;
public override BoundNode GetNodeAtPosition(int characterPosition)
{
foreach (var parameter in Parameters)
{
if (characterPosition >= parameter.Span.Start && characterPosition <= parameter.Span.End)
return parameter.GetNodeAtPosition(characterPosition);
}
if (characterPosition >= Block.Span.Start && characterPosition <= Block.Span.End)
return Block.GetNodeAtPosition(characterPosition);
return this;

View File

@ -1,19 +0,0 @@
using System.Collections.Immutable;
using Upsilon.Parser;
using Upsilon.Text;
namespace Upsilon.Binder
{
public class UnboundFunctionExpression : BoundFunctionExpression
{
public UnboundFunctionExpression(ImmutableArray<VariableSymbol> parameters,
BlockStatementSyntax unboundBlock, TextSpan span) : base(parameters, null, span)
{
UnboundBlock = unboundBlock;
}
public override BoundKind Kind => BoundKind.BoundPromise;
public BlockStatementSyntax UnboundBlock { get; }
}
}

View File

@ -0,0 +1,36 @@
using System.Collections.Immutable;
using Upsilon.Parser;
using Upsilon.Text;
namespace Upsilon.Binder
{
public class UnboundFunctionExpression : BoundFunctionExpression
{
public UnboundFunctionExpression(ImmutableArray<BoundVariableSymbol> parameters,
BlockStatementSyntax unboundBlock, TextSpan span) : base(parameters, null, span)
{
UnboundBlock = unboundBlock;
}
public override BoundKind Kind => BoundKind.BoundPromise;
public BlockStatementSyntax UnboundBlock { get; }
public override BoundNode GetNodeAtPosition(int characterPosition)
{
foreach (var parameter in Parameters)
{
if (characterPosition >= parameter.Span.Start && characterPosition <= parameter.Span.End)
return parameter.GetNodeAtPosition(characterPosition);
}
if (Block != null)
{
if (characterPosition >= Block.Span.Start && characterPosition <= Block.Span.End)
return Block.GetNodeAtPosition(characterPosition);
}
return this;
}
}
}

View File

@ -8,7 +8,6 @@ namespace Upsilon.Binder
public BoundVariableSymbol(VariableSymbol variableSymbol, TextSpan span) : base(span)
{
VariableSymbol = variableSymbol;
Type = variableSymbol.Type;
}
public VariableSymbol VariableSymbol { get; }
@ -18,6 +17,6 @@ namespace Upsilon.Binder
return this;
}
public override Type Type { get; }
public override Type Type => VariableSymbol.Type;
}
}

View File

@ -66,7 +66,7 @@ namespace Upsilon.Evaluator
{
var parameter = parameters[index];
var parameterName = function.Parameters[index];
innerEvaluator.Scope.CreateLocal(parameterName, parameter);
innerEvaluator.Scope.CreateLocal(parameterName.VariableSymbol, parameter);
}
var result = innerEvaluator.EvaluateNode(function.Block);