Ejemplo n.º 1
0
        public Node Instantiate(string guid)
        {
            var parserNode = ParserNodes.FirstOrDefault(p => p.Guid == guid);

            if (parserNode == null)
            {
                var error = new BuildError {
                    Message = $"Could not find a node with GUID: {guid}."
                };
                Errors.Add(error);

                return(null);
            }

            // Uses a depth first loop instead of recursion to avoid potential stack overflows.
            var root = parserNode.CreateInstance(TypeDefMap, Errors);
            var n    = new InstantiationNode {
                Instance = root, Parser = parserNode
            };
            var open = new Stack <InstantiationNode>();

            open.Push(n);

            while (open.Count > 0)
            {
                var next = open.Pop();

                var children = next.Parser.IsDerivedFromTypeDef
                    ? TypeDefMap[next.Parser.Tag].Children
                    : next.Parser.Children;

                foreach (var parserChild in children)
                {
                    var instance = parserChild.CreateInstance(TypeDefMap, Errors);
                    if (instance == null)
                    {
                        continue;
                    }

                    var c = new InstantiationNode {
                        Instance = instance, Parser = parserChild
                    };

                    if (next.Instance.Children == null)
                    {
                        next.Instance.Children = new List <Node>();
                    }

                    next.Instance.Children.Add(instance);
                    open.Push(c);
                }
            }

            foreach (var node in root.DepthFirstIterate())
            {
                node?.Initialize();
            }

            return(root);
        }
Ejemplo n.º 2
0
        private ExpressionNode Instantiation()
        {
            ExpressionNode     itsAST = null; // in case there is a syntactic error
            SourceCodePosition itsPos = _currentToken.SourcePosition;

            Accept(Token.TokenType.New);
            if (Array.Exists(_typeFirstSet, e => e == _currentToken.Type && e != Token.TokenType.Identifier))
            {
                TypeNode       itsType  = ValueType();
                ExpressionNode itsSize  = ArrayExpression();
                ArrayTypeNode  itsArray = new ArrayTypeNode(itsType, itsSize, itsPos);
                itsAST = new InstantiationNode(itsArray, itsSize, itsPos);
            }
            else
            {
                IdentifierNode itsName = new IdentifierNode(_currentToken);
                Accept(Token.TokenType.Identifier);
                ClassTypeNode itsType = new ClassTypeNode(itsName, _currentToken.SourcePosition);
                if (_currentToken.Type == Token.TokenType.LeftBracket)
                {
                    ExpressionNode itsSize  = ArrayExpression();
                    ArrayTypeNode  itsArray = new ArrayTypeNode(itsType, itsSize, itsPos);
                    itsAST = new InstantiationNode(itsArray, itsSize, itsPos);
                }
                else if (_currentToken.Type == Token.TokenType.LeftParen)
                {
                    itsAST = new InstantiationNode(itsType, Call(itsName), itsPos);
                }
                else
                {
                    Error("Expected leftBracket or leftParen");
                }
            }
            return(itsAST);
        }
Ejemplo n.º 3
0
 public object Visit(InstantiationNode n, object o)
 {
     Append($"new ");
     if (n.Type is ArrayTypeNode)
     {
         // if the type is an array, visit the type of that array to enclose the expression in brackets
         ((ArrayTypeNode)n.Type).Type.Accept(this, null);
         Append("[");
         n.Expression.Accept(this, null);
         Append("]");
     }
     else
     {
         n.Type.Accept(this, null);
         n.Expression.Accept(this, null);
     }
     return(null);
 }
Ejemplo n.º 4
0
 public object Visit(InstantiationNode n, object o)
 {
     return(null);
 }