C language interpreter-18 syntax analysis (3)

Source: Internet
Author: User

In expression parsing, it is important to insert operators in the Expression Tree. Priority needs to be compared:

private bool AddOperatorNode(ref Expression.ExpressionNode expTree, Expression.Operator.Operator op)        {            Expression.ExpressionNode node = new Expression.ExpressionNode()            {                Token = op            };            if (expTree != null && expTree.Token is Expression.Operand.Operand)            {                node.LeftNode = expTree;                expTree = node;            }            else            {                if (expTree == null)                {                    expTree = node;                }                else                {                    if (node.Token <= expTree.Token)                    {                        node.LeftNode = expTree;                        expTree = node;                    }                    else                    {                        Expression.ExpressionNode parent = null;                        Expression.ExpressionNode root = expTree;                        do                        {                            // Operand ?                            if (root.Token is Expression.Operand.Operand)                            {                                if (parent != null)                                {                                    parent.RightNode = node;                                    node.LeftNode = root;                                    break;                                }                                else                                {                                    node.LeftNode = root;                                    root = node;                                    break;                                }                            }                            // Less priority, new node will be add to right child node                            if (root.Token <= node.Token)                            {                                if (root.RightNode == null)                                {                                    root.RightNode = node;                                    break;                                }                                parent = root;                                root = root.RightNode;                            }                            else                            {                                // Higher priority                                if (parent == null)                                {                                    node.LeftNode = root;                                    expTree = node;                                }                                else                                {                                    parent.RightNode = node;                                    node.LeftNode = root;                                }                                break;                            }                        } while (true);                    }                }            }            m_lastExpNode = node;            return true;        } // func

The next step is to add the operand and verify the type Matching:

private bool AddOperandNode(Context ctx, SourceCode src, ref Expression.ExpressionNode expTree, Expression.Operand.Operand op)        {            Expression.ExpressionNode node = new Expression.ExpressionNode()            {                Token = op            };            if (expTree == null)            {                expTree = node;                m_lastExpNode = node;                return true;            }            // Root should be operator.            if (expTree.Token is Expression.Operand.Operand)                if (!NotifyError(ctx, src.Location, ParsingErrorType.SyntaxError, "Syntax error."))                     return false;            Expression.ExpressionNode root = expTree;            // Add to end of right side            while (root.RightNode != null)                root = root.RightNode;            // Check type            if (root.LeftNode != null)            {                DataTypeInfo leftDti = root.LeftNode.ResultType(ctx);                DataTypeInfo rightDti = op.GetTypeInfo(ctx);                if (leftDti != rightDti)                {                    // void type has no meaning                    if (leftDti.BaseType == PrimitiveDataType.VoidType && !leftDti.IsPointer)                        if (!NotifyError(ctx, src.Location, ParsingErrorType.TypeError, "Incomplete type void"))                             return false;                    if ((leftDti.IsPointer && (!rightDti.IsPointer && rightDti.BaseType == PrimitiveDataType.VoidType || rightDti.BaseType == PrimitiveDataType.FloatType)) ||                        ((!leftDti.IsPointer && leftDti.BaseType == PrimitiveDataType.VoidType || leftDti.BaseType == PrimitiveDataType.FloatType) && rightDti.IsPointer))                    {                        // A warning should be cast.                        NotifyWarning(ctx, src.Location, rightDti.ToString() + " will be cast to " + leftDti.ToString());                     }                }            }            root.RightNode = node;            m_lastExpNode = node;            return true;        }

 

 

 

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.