Adds basics for predefined function parameter types

This commit is contained in:
Deukhoofd 2018-11-28 16:18:56 +01:00
parent 062f0f84ad
commit 27a7465961
No known key found for this signature in database
GPG Key ID: B4C087AC81641654
6 changed files with 98 additions and 14 deletions

View File

@ -4,6 +4,7 @@ using System.Collections.Immutable;
using System.Linq; using System.Linq;
using Upsilon.BaseTypes; using Upsilon.BaseTypes;
using Upsilon.BaseTypes.Number; using Upsilon.BaseTypes.Number;
using Upsilon.BoundTypes;
using Upsilon.Parser; using Upsilon.Parser;
using Type = Upsilon.BaseTypes.Type; using Type = Upsilon.BaseTypes.Type;
@ -421,12 +422,19 @@ namespace Upsilon.Binder
var parameters = ImmutableArray.CreateBuilder<BoundVariableSymbol>(); var parameters = ImmutableArray.CreateBuilder<BoundVariableSymbol>();
foreach (var identifierToken in e.Parameters) foreach (var identifierToken in e.Parameters)
{ {
var vari = new VariableSymbol(identifierToken.Name, Type.Unknown, true); var variable = identifierToken;
var scriptType = Type.Unknown;
if (variable.TypeName != null)
{
var type = BoundTypeHandler.GetTypeDefinition(variable.TypeName.Name);
scriptType = type.ScriptType;
}
var vari = new VariableSymbol(variable.IdentifierName.Name, scriptType, true);
parameters.Add(new BoundVariableSymbol(vari, identifierToken.Span)); parameters.Add(new BoundVariableSymbol(vari, identifierToken.Span));
innerScope.DefineLocalVariable(vari); innerScope.DefineLocalVariable(vari);
} }
if (parameters.Count == 0) if (parameters.All(x => x.Type != Type.Unknown))
{ {
Scope = innerScope; Scope = innerScope;
var block = BindBlockStatement(e.Block); var block = BindBlockStatement(e.Block);

View File

@ -0,0 +1,42 @@
using System.Collections.Generic;
using Upsilon.BaseTypes;
namespace Upsilon.BoundTypes
{
public static class BoundTypeHandler
{
private static Dictionary<string, BoundTypeDefinition> _typeDefinitions = new Dictionary<string, BoundTypeDefinition>
{
{"string", new BoundTypeDefinition(Type.String, typeof(string))},
{
"number",
new BoundTypeDefinition(Type.Number,
new[] {typeof(int), typeof(long), typeof(float), typeof(double)})
},
{"bool", new BoundTypeDefinition(Type.Boolean, typeof(bool))}
};
public static BoundTypeDefinition GetTypeDefinition(string key)
{
return _typeDefinitions[key.ToLowerInvariant()];
}
}
public class BoundTypeDefinition
{
public BoundTypeDefinition(Type scriptType, System.Type[] validInternalTypes)
{
ScriptType = scriptType;
ValidInternalTypes = validInternalTypes;
}
public BoundTypeDefinition(Type scriptType, System.Type validInternalType)
{
ScriptType = scriptType;
ValidInternalTypes = new []{validInternalType};
}
public Type ScriptType { get; }
public System.Type[] ValidInternalTypes { get; }
}
}

View File

@ -8,13 +8,13 @@ namespace Upsilon.Parser
{ {
public SyntaxToken FunctionToken { get; } public SyntaxToken FunctionToken { get; }
public SyntaxToken OpenParenthesis { get; } public SyntaxToken OpenParenthesis { get; }
public ImmutableArray<IdentifierToken> Parameters { get; } public ImmutableArray<ParameterToken> Parameters { get; }
public SyntaxToken CloseParenthesis { get; } public SyntaxToken CloseParenthesis { get; }
public BlockStatementSyntax Block { get; } public BlockStatementSyntax Block { get; }
public SyntaxToken EndToken { get; } public SyntaxToken EndToken { get; }
public FunctionExpressionSyntax(SyntaxToken functionToken, public FunctionExpressionSyntax(SyntaxToken functionToken,
SyntaxToken openParenthesis, ImmutableArray<IdentifierToken> parameters, SyntaxToken closeParenthesis, SyntaxToken openParenthesis, ImmutableArray<ParameterToken> parameters, SyntaxToken closeParenthesis,
BlockStatementSyntax block, SyntaxToken endToken) BlockStatementSyntax block, SyntaxToken endToken)
{ {
FunctionToken = functionToken; FunctionToken = functionToken;

View File

@ -0,0 +1,23 @@
using System.Collections.Generic;
namespace Upsilon.Parser
{
public class ParameterToken : SyntaxNode
{
public ParameterToken(IdentifierToken typeName, IdentifierToken identifierName)
{
IdentifierName = identifierName;
TypeName = typeName;
}
public IdentifierToken IdentifierName { get; }
public IdentifierToken TypeName { get; }
public override SyntaxKind Kind => SyntaxKind.Parameter;
public override IEnumerable<SyntaxNode> ChildNodes()
{
yield return TypeName;
yield return IdentifierName;
}
}
}

View File

@ -204,7 +204,7 @@ namespace Upsilon.Parser
{ {
var functionToken = MatchToken(SyntaxKind.FunctionKeyword); var functionToken = MatchToken(SyntaxKind.FunctionKeyword);
var openParenthesis = MatchToken(SyntaxKind.OpenParenthesis); var openParenthesis = MatchToken(SyntaxKind.OpenParenthesis);
var variableBuilder = ImmutableArray.CreateBuilder<IdentifierToken>(); var variableBuilder = ImmutableArray.CreateBuilder<ParameterToken>();
SyntaxToken current = null; SyntaxToken current = null;
while (Current.Kind != SyntaxKind.CloseParenthesis) while (Current.Kind != SyntaxKind.CloseParenthesis)
{ {
@ -213,8 +213,16 @@ namespace Upsilon.Parser
break; break;
} }
current = Current; current = Current;
var variableIdentifier = (IdentifierToken)MatchToken(SyntaxKind.Identifier); var firstToken = MatchToken(SyntaxKind.Identifier);
variableBuilder.Add(variableIdentifier); if (Current.Kind == SyntaxKind.Identifier)
{
variableBuilder.Add(new ParameterToken((IdentifierToken) firstToken,
(IdentifierToken) NextToken()));
}
else
{
variableBuilder.Add(new ParameterToken(null, (IdentifierToken) firstToken));
}
if (Current.Kind == SyntaxKind.Comma) if (Current.Kind == SyntaxKind.Comma)
NextToken(); NextToken();
} }
@ -241,17 +249,19 @@ namespace Upsilon.Parser
} }
var identifier = MatchToken(SyntaxKind.Identifier); var identifier = MatchToken(SyntaxKind.Identifier);
var openParenthesis = MatchToken(SyntaxKind.OpenParenthesis); var openParenthesis = MatchToken(SyntaxKind.OpenParenthesis);
var variableBuilder = ImmutableArray.CreateBuilder<IdentifierToken>(); var variableBuilder = ImmutableArray.CreateBuilder<ParameterToken>();
IdentifierToken lastIdentifier = null;
while (Current.Kind != SyntaxKind.CloseParenthesis || Current.Kind == SyntaxKind.EndOfFile) while (Current.Kind != SyntaxKind.CloseParenthesis || Current.Kind == SyntaxKind.EndOfFile)
{ {
var variableIdentifier =(IdentifierToken) MatchToken(SyntaxKind.Identifier); var firstToken = MatchToken(SyntaxKind.Identifier);
if (lastIdentifier == variableIdentifier) if (Current.Kind == SyntaxKind.Identifier)
{ {
break; variableBuilder.Add(new ParameterToken((IdentifierToken) firstToken,
(IdentifierToken) NextToken()));
}
else
{
variableBuilder.Add(new ParameterToken(null, (IdentifierToken) firstToken));
} }
lastIdentifier = variableIdentifier;
variableBuilder.Add( variableIdentifier);
if (Current.Kind == SyntaxKind.Comma) if (Current.Kind == SyntaxKind.Comma)
NextToken(); NextToken();
} }

View File

@ -51,6 +51,7 @@ namespace Upsilon.Parser
BreakKeyword, BreakKeyword,
Identifier, Identifier,
Parameter,
// Expressions // Expressions
UnaryExpression, UnaryExpression,