コード例 #1
0
        public static ConstNode GetConstNode <TSymbol>(IParseTree <TSymbol> parseTree) where TSymbol : ISymbol <TSymbol>
        {
            // one of: DecimalNumberLiteral | CharacterLiteral | StringLiteral | BooleanLiteral
            TypeNode type;

            switch (ASTBuilder.GetName(parseTree.Symbol))
            {
            case "DecimalNumberLiteral":
                type = NamedTypeNode.IntType(true);
                break;

            case "CharacterLiteral":
                type = NamedTypeNode.CharType(true);
                break;

            case "StringLiteral":
                throw new NotImplementedException();     // TODO: strings implementation

            case "BooleanLiteral":
                type = NamedTypeNode.BoolType(true);
                break;

            default:
                throw new System.ArgumentException();
            }
            return(new AtomNode(type));
        }
コード例 #2
0
        /// <summary>
        /// Resolve Expression from ParseTree. Main method.
        /// </summary>
        public static ExpressionNode GetExpressionNode <TSymbol>(IParseTree <TSymbol> parseTree) where TSymbol : ISymbol <TSymbol>
        {
            var node = ASTBuilder.FirstChild(parseTree);             // Expression -> RecordVariableDefinitionExpression | RecordFieldAccessExpression | OperatorExpression

            if (ASTBuilder.GetName(node.Symbol) == "RecordVariableDefinitionExpression")
            {
                var nod = new RecordVariableDefNode();
                nod.BuildNode(node);
                return(nod);
            }

            if (ASTBuilder.GetName(node.Symbol) == "RecordFieldAccessExpression")
            {
                var nod = new RecordVariableFieldUseNode();
                nod.BuildNode(node);
                return(nod);
            }

            node = ASTBuilder.FirstChild(node);                                  // OperatorExpression -> Operator19Expression
            node = ASTBuilder.FirstChild(node);                                  // Operator19Expression -> ObjectDefinitionExpression | Operator18Expression
            if (ASTBuilder.GetName(node.Symbol) == "ObjectDefinitionExpression") // ObjectDefinition
            {
                var defNode = new VariableDefNode();
                defNode.BuildNode(node);
                return(defNode);
            }
            // Operator18Expression
            node = ASTBuilder.FirstChild(node); // IfExpression | WhileExpression | Operator17Expression
            switch (ASTBuilder.GetName(node.Symbol))
            {
            case "IfExpression":
                var ifNode = new IfNode();
                ifNode.BuildNode(node);
                return(ifNode);

            case "WhileExpression":
                var whileNode = new WhileNode();
                whileNode.BuildNode(node);
                return(whileNode);

            default:     // default - Operator17Expression
                break;
            }
            node = ASTBuilder.FirstChild(node);          // Operator17Expression -> Operator16Expression
            node = ASTBuilder.FirstChild(node);          // Operator16Expression -> Operator15Expression
            return(ResolveOperatorExpression(node, 15)); // node is operator 15
        }
コード例 #3
0
        public static ExpressionNode ResolveAtomicExpression <TSymbol>(IParseTree <TSymbol> parseTree) where TSymbol : ISymbol <TSymbol>
        {
            // AtomicExpression -> BlockExpression | ObjectDefinitionExpression | ArrayLiteralExpression | ObjectUseExpression |
            //                     IfExpression | WhileExpression | LoopControlExpression
            var            node = ASTBuilder.FirstChild(parseTree); // AtomicExpression -> one of available symbols
            ExpressionNode atomic;

            switch (ASTBuilder.GetName(node.Symbol))
            {
            case "BlockExpression":
                atomic = new BlockExpressionNode();
                break;

            case "ArrayLiteralExpression":
                throw new System.NotImplementedException();     // TODO: arrays implementation

            case "ObjectUseExpression":
                // ObjectUseExpression -> ObjectName | Literals
                node = ASTBuilder.FirstChild(node);
                if (node.Symbol.IsTerminal)       // ObjectName
                {
                    atomic = new VariableUseNode();
                }
                else                                      // Literals
                {
                    node   = ASTBuilder.FirstChild(node); // Literals -> one of available literals
                    atomic = ConstNode.GetConstNode(node);
                }
                break;

            case "LoopControlExpression":
                atomic = new LoopControlNode();
                break;

            default:
                throw new System.ArgumentException();
            }
            atomic.BuildNode(node);
            return(atomic);
        }
コード例 #4
0
        // ----- Methods -----

        #region implemented abstract members of Node

        // Program -> (Function|Record)* Eof
        public override void BuildNode <TSymbol>(IParseTree <TSymbol> parseTree)
        {
            var childs = ASTBuilder.ChildrenArray(parseTree);

            for (int i = 0; i < childs.Length - 1; i++) // skip last child - it is EOF
            {
                switch (ASTBuilder.GetName(childs [i].Symbol))
                {
                case "RecordTypeDeclaration":
                    var recordNode = new RecordTypeDeclarationNode();
                    recordNode.BuildNode(childs [i]);
                    records.AddLast(recordNode);
                    break;

                default:
                    var funNode = new FunctionDefinitionNode();
                    funNode.BuildNode(childs [i]);
                    functions.AddLast(funNode);
                    break;
                }
            }
        }