229 lines
7.3 KiB
C#
229 lines
7.3 KiB
C#
using Common;
|
|
using Syntax.Parsing;
|
|
using Syntax.Parsing.Node;
|
|
using Syntax.Typing;
|
|
using Syntax.Typing.BoundNode;
|
|
|
|
namespace Syntax;
|
|
|
|
public class DefinitionTable
|
|
{
|
|
private readonly IEnumerable<SyntaxTree> _syntaxTrees;
|
|
|
|
public DefinitionTable(IEnumerable<SyntaxTree> syntaxTrees)
|
|
{
|
|
_syntaxTrees = syntaxTrees;
|
|
}
|
|
|
|
public Optional<FuncDefinition> LookupFunction(string @namespace, string name)
|
|
{
|
|
var definition = _syntaxTrees
|
|
.Where(c => c.Namespace == @namespace)
|
|
.SelectMany(c => c.Definitions)
|
|
.OfType<FuncDefinition>()
|
|
.SingleOrDefault(f => f.Name == name);
|
|
|
|
return Optional.OfNullable(definition);
|
|
}
|
|
|
|
public Optional<StructDefinitionNode> LookupStruct(string @namespace, string name)
|
|
{
|
|
var definition = _syntaxTrees
|
|
.Where(c => c.Namespace == @namespace)
|
|
.SelectMany(c => c.Definitions)
|
|
.OfType<StructDefinitionNode>()
|
|
.SingleOrDefault(f => f.Name == name);
|
|
|
|
return Optional.OfNullable(definition);
|
|
}
|
|
|
|
public Optional<TraitDefinitionNode> LookupTrait(string @namespace, string name)
|
|
{
|
|
var definition = _syntaxTrees
|
|
.Where(c => c.Namespace == @namespace)
|
|
.SelectMany(c => c.Definitions)
|
|
.OfType<TraitDefinitionNode>()
|
|
.SingleOrDefault(f => f.Name == name);
|
|
|
|
return Optional.OfNullable(definition);
|
|
}
|
|
|
|
public Optional<Tuple<TraitImplementationDefinitionNode, ImplementationFuncNode>> LookupTraitImplementationForType(NubType type, string funcName)
|
|
{
|
|
var implementations = _syntaxTrees
|
|
.SelectMany(c => c.Definitions)
|
|
.OfType<TraitImplementationDefinitionNode>()
|
|
.Where(c => c.ForType.Equals(type));
|
|
|
|
var implementation = implementations.SingleOrDefault(c => c.Functions.Any(x => x.Name == funcName));
|
|
|
|
var value = implementation == null ? null : new Tuple<TraitImplementationDefinitionNode, ImplementationFuncNode>(implementation, implementation.Functions.First(x => x.Name == funcName));
|
|
|
|
return Optional.OfNullable(value);
|
|
}
|
|
|
|
public Optional<TraitFunc> LookupFunctionOnTrait(string @namespace, string name, string funcName)
|
|
{
|
|
var traitDef = LookupTrait(@namespace, name);
|
|
if (traitDef.HasValue)
|
|
{
|
|
var function = traitDef.Value.Functions.SingleOrDefault(x => x.Name == funcName);
|
|
return Optional.OfNullable(function);
|
|
}
|
|
|
|
return Optional<TraitFunc>.Empty();
|
|
}
|
|
|
|
public IEnumerable<StructDefinitionNode> GetStructs()
|
|
{
|
|
return _syntaxTrees
|
|
.SelectMany(c => c.Definitions)
|
|
.OfType<StructDefinitionNode>();
|
|
}
|
|
|
|
public IEnumerable<FuncDefinition> GetFunctions()
|
|
{
|
|
return _syntaxTrees
|
|
.SelectMany(c => c.Definitions)
|
|
.OfType<FuncDefinition>();
|
|
}
|
|
|
|
public IEnumerable<LocalFuncDefinitionNode> GetLocalFunctions()
|
|
{
|
|
return _syntaxTrees
|
|
.SelectMany(c => c.Definitions)
|
|
.OfType<LocalFuncDefinitionNode>();
|
|
}
|
|
|
|
public IEnumerable<ExternFuncDefinitionNode> GetExternFunctions()
|
|
{
|
|
return _syntaxTrees
|
|
.SelectMany(c => c.Definitions)
|
|
.OfType<ExternFuncDefinitionNode>();
|
|
}
|
|
|
|
public IEnumerable<TraitDefinitionNode> GetTraits()
|
|
{
|
|
return _syntaxTrees
|
|
.SelectMany(c => c.Definitions)
|
|
.OfType<TraitDefinitionNode>();
|
|
}
|
|
|
|
public IEnumerable<TraitImplementationDefinitionNode> GetTraitImplementations()
|
|
{
|
|
return _syntaxTrees
|
|
.SelectMany(c => c.Definitions)
|
|
.OfType<TraitImplementationDefinitionNode>();
|
|
}
|
|
}
|
|
|
|
public class BoundDefinitionTable
|
|
{
|
|
private readonly IEnumerable<BoundSyntaxTree> _syntaxTrees;
|
|
|
|
public BoundDefinitionTable(IEnumerable<BoundSyntaxTree> syntaxTrees)
|
|
{
|
|
_syntaxTrees = syntaxTrees;
|
|
}
|
|
|
|
public Optional<BoundFuncDefinition> LookupFunc(string @namespace, string name)
|
|
{
|
|
var definition = _syntaxTrees
|
|
.Where(c => c.Namespace == @namespace)
|
|
.SelectMany(c => c.Definitions)
|
|
.OfType<BoundFuncDefinition>()
|
|
.SingleOrDefault(f => f.Name == name);
|
|
|
|
return Optional.OfNullable(definition);
|
|
}
|
|
|
|
public Optional<BoundStructDefinitionNode> LookupStruct(string @namespace, string name)
|
|
{
|
|
var definition = _syntaxTrees
|
|
.Where(c => c.Namespace == @namespace)
|
|
.SelectMany(c => c.Definitions)
|
|
.OfType<BoundStructDefinitionNode>()
|
|
.SingleOrDefault(f => f.Name == name);
|
|
|
|
return Optional.OfNullable(definition);
|
|
}
|
|
|
|
public Optional<BoundTraitDefinitionNode> LookupTrait(string @namespace, string name)
|
|
{
|
|
var definition = _syntaxTrees
|
|
.Where(c => c.Namespace == @namespace)
|
|
.SelectMany(c => c.Definitions)
|
|
.OfType<BoundTraitDefinitionNode>()
|
|
.SingleOrDefault(f => f.Name == name);
|
|
|
|
return Optional.OfNullable(definition);
|
|
}
|
|
|
|
public Optional<Tuple<BoundTraitImplementationDefinitionNode, BoundImplementationFuncNode>> LookupTraitImplementationForType(NubType type, string funcName)
|
|
{
|
|
var implementations = _syntaxTrees
|
|
.SelectMany(c => c.Definitions)
|
|
.OfType<BoundTraitImplementationDefinitionNode>()
|
|
.Where(c => c.ForType.Equals(type));
|
|
|
|
var implementation = implementations.SingleOrDefault(c => c.Functions.Any(x => x.Name == funcName));
|
|
|
|
var value = implementation == null ? null : new Tuple<BoundTraitImplementationDefinitionNode, BoundImplementationFuncNode>(implementation, implementation.Functions.First(x => x.Name == funcName));
|
|
|
|
return Optional.OfNullable(value);
|
|
}
|
|
|
|
public Optional<BountTraitFunc> LookupFunctionOnTrait(string @namespace, string name, string funcName)
|
|
{
|
|
var traitDef = LookupTrait(@namespace, name);
|
|
if (traitDef.HasValue)
|
|
{
|
|
var function = traitDef.Value.Functions.SingleOrDefault(x => x.Name == funcName);
|
|
return Optional.OfNullable(function);
|
|
}
|
|
|
|
return Optional<BountTraitFunc>.Empty();
|
|
}
|
|
|
|
public IEnumerable<BoundStructDefinitionNode> GetStructs()
|
|
{
|
|
return _syntaxTrees
|
|
.SelectMany(c => c.Definitions)
|
|
.OfType<BoundStructDefinitionNode>();
|
|
}
|
|
|
|
public IEnumerable<BoundFuncDefinition> GetFunctions()
|
|
{
|
|
return _syntaxTrees
|
|
.SelectMany(c => c.Definitions)
|
|
.OfType<BoundFuncDefinition>();
|
|
}
|
|
|
|
public IEnumerable<BoundLocalFuncDefinitionNode> GetLocalFunctions()
|
|
{
|
|
return _syntaxTrees
|
|
.SelectMany(c => c.Definitions)
|
|
.OfType<BoundLocalFuncDefinitionNode>();
|
|
}
|
|
|
|
public IEnumerable<BoundExternFuncDefinitionNode> GetExternFunctions()
|
|
{
|
|
return _syntaxTrees
|
|
.SelectMany(c => c.Definitions)
|
|
.OfType<BoundExternFuncDefinitionNode>();
|
|
}
|
|
|
|
public IEnumerable<BoundTraitDefinitionNode> GetTraits()
|
|
{
|
|
return _syntaxTrees
|
|
.SelectMany(c => c.Definitions)
|
|
.OfType<BoundTraitDefinitionNode>();
|
|
}
|
|
|
|
public IEnumerable<BoundTraitImplementationDefinitionNode> GetTraitImplementations()
|
|
{
|
|
return _syntaxTrees
|
|
.SelectMany(c => c.Definitions)
|
|
.OfType<BoundTraitImplementationDefinitionNode>();
|
|
}
|
|
} |