/// <summary> /// Rewrite visited node. /// </summary> /// <param name="node">Current node</param> /// <param name="children">Rewritten chilren node</param> /// <returns>Rewritten node</returns> /// <exception cref="CannotResolveIdentifierException">Throw if there is unresolved identifier</exception> public ISyntacticNode Visit(ISyntacticNode node, IEnumerable<ISyntacticNode> children) { var termNode = (TermSyntacticNode) node; var term = (IDeclarationTerm) termNode.Term; var linkedTerm = Resolvers .Select(resolver => resolver.Resolve(term, children, LinkedLibrary)) .FirstOrDefault(lt => lt != null); if (linkedTerm == null) { throw new CannotResolveIdentifierException(term.Identifier); } return new TermSyntacticNode(linkedTerm, children); }
public ISyntacticNode Visit(ISyntacticNode node, IEnumerable<ISyntacticNode> children) => Visitor(node, children);
public bool Filter(ISyntacticNode node) => FilterPredicate(node);
public ISyntacticNode Rewrite(ISyntacticNode node) => Rules.Aggregate(node, (current, rewriter) => current.Rewrite(rewriter));
/// <summary> /// Initializes a new instance of the <see cref="SyntacticParseResult"/> class. /// </summary> /// <param name="node">Syntactic node</param> /// <param name="tokenNodes">Non-read token sequence</param> public SyntacticParseResult(ISyntacticNode node, IEnumerable<ITokenSyntacticNode> tokenNodes) { Node = node; TokenNodes = tokenNodes; }
/// <summary> /// Compile translated tree and create expression tree /// </summary> /// <param name="node"></param> /// <returns></returns> public Expression<Func<double>> CreateExpression(ISyntacticNode node) => Expression.Lambda<Func<double>>(node.CreateExpression());
/// <summary> /// Check correctness of resolve identifier. /// Throw exception if result isn't term tree. /// </summary> /// <param name="node">Root node of result</param> /// <exception cref="CannotResolveIdentifierException">Throw if trnslation tree contains non-term nodes</exception> public void CheckResult(ISyntacticNode node) => node.Rewrite(new SyntaxRewriter( nd => !((node as TermSyntacticNode)?.Term is IResolvedTerm), (nd, ch) => { throw new TranslateException(); }));
/// <summary> /// Resolve constant and function in translated sysntactic tree /// </summary> /// <param name="node">Root of translated tree</param> /// <returns>Resolving tree</returns> public ISyntacticNode Resolve(ISyntacticNode node) => node.Rewrite(Rules);
/// <summary> /// Check correctness of translation result. /// Throw exception if result isn't term tree. /// </summary> /// <param name="node">Root node of result</param> public void CheckResult(ISyntacticNode node) => node.Rewrite(new SyntaxRewriter( nd => !(nd is TermSyntacticNode), (nd, ch) => { throw new TranslateException(); }));
/// <summary> /// Translate syntax tree using rules /// </summary> /// <param name="node">Root node</param> /// <returns>Rewritten root node</returns> public ISyntacticNode Translate(ISyntacticNode node) => node.Rewrite(Rules);
/// <summary> /// Filter node for visit. /// </summary> /// <param name="node">Current node</param> /// <returns>True if visitor must visit this node</returns> public bool Filter(ISyntacticNode node) { var termNode = node as TermSyntacticNode; return termNode != null && ResolverType.Resolve(termNode.Term); }