public static Notion TryIdentifyDefinition(NameInstance expression) { CompositeNameInstance compositeExpression = expression as CompositeNameInstance; if (compositeExpression == null) return null; return TryIdentifyDefinition(compositeExpression); }
/// <summary> Tries to find an expression of the form ?∈set, and returns the involved name instance and set. </summary> /// <param name="expression"> The expression that may be of the form ?∈set. </param> /// <param name="toBeDefined"> If the specified expression was of said form, this is the left hand operand. </param> /// <param name="toBeDefinedIn"> If the specified expression was of said form, this is the right hand operand's set. </param> public static bool TryIdentifyDefinition(CompositeNameInstance expression, out NameInstance toBeDefined, out Set toBeDefinedIn) { Contract.Requires(expression != null); if (expression.Elements.Count == 3 && expression.Elements[0].Name == null //first expression element is still unknown && expression.Elements[1].Name?.Notion == DefaultNotions.InDefining && expression.Elements[2].Name?.Notion is Set) { toBeDefined = expression.Elements[0]; toBeDefinedIn = (Set)expression.Elements[2].Name.Notion; return true; } else { toBeDefined = null; toBeDefinedIn = null; return false; } }
/// <summary> Defines the specified name instance to be a representation of a new element in the specified set. </summary> public void Add(NameInstance toBeDefined, Set toBeDefinedIn) { var newNotion = new Notion(toBeDefinedIn, "No description available", false); //newNotion.Add(toBeDefined); base.Add(newNotion); }
private static NameInstance GetMutatedState(NameInstance original, Dictionary<NameInstance, MutationCollection> allMutations) { MutationCollection applicableMutations; if (allMutations.TryGetValue(original, out applicableMutations)) return applicableMutations.Modify(original, allMutations); return original; }
private List<NameInstance> GetElementsOf(NameInstance original, Dictionary<NameInstance, MutationCollection> allMutations) { if (original is CompositeNameInstance) { return ((CompositeNameInstance)original).Elements.Select(ni => GetMutatedState(ni, allMutations)).ToList(); } else { //assumes the placeholder glyph is removed in NameInstance representation as though it is any other character return new List<NameInstance> { GetMutatedState(original, allMutations) }; } }
public NameInstance Modify(NameInstance original, Dictionary<NameInstance, MutationCollection> allMutations) { Contract.Requires(mutations != null); List<NameInstance> lazyNameInstances = GetElementsOf(original, allMutations); foreach (var mutation in mutations.GetInvocationList().Cast<Func<List<NameInstance>, List<NameInstance>>>()) lazyNameInstances = mutation(lazyNameInstances); mutations = null; if (lazyNameInstances.Count == 1 && lazyNameInstances[0].Position is LinearPosition && !RootPosition.Instance.Contains(original.Position)) return lazyNameInstances[0]; return new CompositeNameInstance(lazyNameInstances, original.Position); }
/// <summary> Infers the names in the specified expression, and gives the result to an ASTBuilder. Both results are yielded. </summary> /// <param name="expression"> The expression to bind to notions and to parse. </param> /// <param name="booleanNames"> The names bindable to booleans. </param> /// <param name="nameInstanceBindings"> The result of the name inference. </param> /// <param name="parsedResults"> The result of the AST builder per inferred names set. </param> /// <param name="expectedNumberOfInstanceBindingSets"> The number of expected full binding sets. An optional intermediate check after inferring names but before parsing into an AST. </param> private static void InferNamesAndGetAST(NameInstance expression, string booleanNames, out List<ReadOnlyCollection<Binding>> nameInstanceBindings, out List<List<AbstractSyntaxTreeNodeBase<TDomain>>> parsedResults, int? expectedNumberOfInstanceBindingSets = null) { Contract.Requires(expression.ParentIsSet); NameInstanceNameInferrer binder; var astBuilder = CreateASTBuilderWithOrNotation(out binder, booleanNames.ToArray()); nameInstanceBindings = binder.Match(expression).ToList(); if (expectedNumberOfInstanceBindingSets != null) Contract.Assert(nameInstanceBindings.Count == expectedNumberOfInstanceBindingSets.Value); parsedResults = new List<List<AbstractSyntaxTreeNodeBase<TDomain>>>(); foreach (ReadOnlyCollection<Binding> fullSetOfBindings in nameInstanceBindings) { fullSetOfBindings.BindAll(); var ast = astBuilder.GetAbstractSyntaxTrees(fullSetOfBindings).ToList(); parsedResults.Add(ast); } }
public NameBinding(Name name, LeafIndex indexOfMatched, NameInstance matchedNameInstance) { this.Index = indexOfMatched; this.Name = name; this.Instance = matchedNameInstance; }