73 lines
2.8 KiB
C#
73 lines
2.8 KiB
C#
using Nub.Lang.Frontend.Lexing;
|
|
using Nub.Lang.Frontend.Typing;
|
|
|
|
namespace Nub.Lang.Frontend.Parsing;
|
|
|
|
public class FuncParameter(string name, NubType type, bool variadic)
|
|
{
|
|
public string Name { get; } = name;
|
|
public NubType Type { get; } = type;
|
|
public bool Variadic { get; } = variadic;
|
|
|
|
public override string ToString() => $"{Name}: {Type}";
|
|
}
|
|
|
|
public interface IFuncSignature
|
|
{
|
|
public string Name { get; }
|
|
public List<FuncParameter> Parameters { get; }
|
|
public Optional<NubType> ReturnType { get; }
|
|
|
|
public bool SignatureMatches(string name, List<NubType> parameters)
|
|
{
|
|
if (Name != name) return false;
|
|
if (Parameters.Count == 0 && parameters.Count == 0) return true;
|
|
if (Parameters.Count > parameters.Count) return false;
|
|
|
|
for (var i = 0; i < parameters.Count; i++)
|
|
{
|
|
if (i > Parameters.Count)
|
|
{
|
|
if (Parameters[^1].Variadic)
|
|
{
|
|
if (!NubType.IsCompatibleWith(parameters[i], Parameters[^1].Type))
|
|
{
|
|
return false;
|
|
}
|
|
}
|
|
else
|
|
{
|
|
return false;
|
|
}
|
|
}
|
|
else if (!NubType.IsCompatibleWith(parameters[i], Parameters[i].Type))
|
|
{
|
|
return false;
|
|
}
|
|
}
|
|
|
|
return true;
|
|
}
|
|
|
|
public string ToString() => $"{Name}({string.Join(", ", Parameters.Select(p => p.ToString()))}){(ReturnType.HasValue ? ": " + ReturnType.Value : "")}";
|
|
}
|
|
|
|
public class LocalFuncDefinitionNode(IReadOnlyList<Token> tokens, Optional<string> documentation, string name, List<FuncParameter> parameters, BlockNode body, Optional<NubType> returnType, bool global) : DefinitionNode(tokens, documentation), IFuncSignature
|
|
{
|
|
public string Name { get; } = name;
|
|
public List<FuncParameter> Parameters { get; } = parameters;
|
|
public BlockNode Body { get; } = body;
|
|
public Optional<NubType> ReturnType { get; } = returnType;
|
|
public bool Global { get; } = global;
|
|
|
|
public override string ToString() => $"{Name}({string.Join(", ", Parameters.Select(p => p.ToString()))}){(ReturnType.HasValue ? ": " + ReturnType.Value : "")}";
|
|
}
|
|
|
|
public class ExternFuncDefinitionNode(IReadOnlyList<Token> tokens, Optional<string> documentation, string name, List<FuncParameter> parameters, Optional<NubType> returnType) : DefinitionNode(tokens, documentation), IFuncSignature
|
|
{
|
|
public string Name { get; } = name;
|
|
public List<FuncParameter> Parameters { get; } = parameters;
|
|
public Optional<NubType> ReturnType { get; } = returnType;
|
|
|
|
public override string ToString() => $"{Name}({string.Join(", ", Parameters.Select(p => p.ToString()))}){(ReturnType.HasValue ? ": " + ReturnType.Value : "")}";
|
|
} |