Exemple #1
0
        public static ConditionalStaNode Parse(TokenString tStr, ref int index)
        {
            int startIndex = index;

            if (!tStr.Match(index, TokenType.ifStatement, TokenType.rBraceOpen))
            {
                return(null);
            }

            index++;

            ExpressionNode condition = Exp.Parse(tStr.GetRangeInBrackets(ref index));
            StatementNode  ifTrue    = Sta.Parse(tStr, ref index);
            StatementNode  ifFalse   = null;

            if (tStr.Match(index, TokenType.elseStatement))
            {
                index++;
                ifFalse = Sta.Parse(tStr, ref index);
            }

            ConditionalStaNode node = new ConditionalStaNode(condition, ifTrue, ifFalse);

            return(node);
        }
Exemple #2
0
        public static DoLoopStaNode Parse(TokenString tStr, ref int index)
        {
            int startIndex = index;

            if (!tStr.Match(index, TokenType.doStatement, TokenType.cBraceOpen))
            {
                return(null);
            }

            index++;


            StatementNode body = Sta.Parse(tStr, ref index);

            if (!tStr.Match(index, TokenType.whileStatement, TokenType.rBraceOpen))
            {
                index = startIndex;
                return(null);
            }

            index++;
            ExpressionNode condition = Exp.Parse(tStr.GetRangeInBrackets(ref index));

            if (!tStr.Match(index, TokenType.lineEnd))
            {
                index = startIndex;
                return(null);
            }

            index++;
            DoLoopStaNode node = new DoLoopStaNode(condition, body);

            return(node);
        }
Exemple #3
0
        public static CompoundStaNode Parse(TokenString tStr, ref int index)
        {
            if (!tStr.Match(index, TokenType.cBraceOpen))
            {
                return(null);
            }

            index++;

            CompoundStaNode node = new CompoundStaNode();

            while (!tStr.Match(index, TokenType.cBraceClose))
            {
                var statementNode = Sta.Parse(tStr, ref index);

                if (statementNode != null)
                {
                    node.AddStatement(statementNode);
                }
                else
                {
                    index++;
                }
            }

            index++;
            return(node);
        }
Exemple #4
0
        public static CtorNode Parse(string className, TokenString tStr, ref int index)
        {
            int startIndex = index;

            var accessors = new List <string>();

            while (tStr[index].Type == TokenType.accessor)
            {
                accessors.Add(tStr[index].Value);
                index++;
            }

            if (!(tStr.Match(index, TokenType.identifier, TokenType.rBraceOpen) && tStr[index].Value == className))
            {
                index = startIndex;
                return(null);
            }

            string type = tStr[index].Value;
            string name = "ctor";

            index++;

            var args = new List <ArgumentNode>();

            var argTokens = tStr.GetRangeInBrackets(ref index);

            if (argTokens != null && argTokens.Count > 0)
            {
                var argTokensSep = argTokens.Split(false, TokenType.comma);

                foreach (var argTs in argTokensSep)
                {
                    args.Add(new ArgumentNode(argTs[0].Value, argTs[1].Value));
                }
            }

            index = tStr.FindNextIndex(index, TokenType.cBraceOpen);

            var sta = CompoundStaNode.Parse(tStr, ref index);

            if (sta == null)
            {
                index = startIndex;
                return(null);
            }

            CtorNode node = new CtorNode(type, name, accessors, args, sta);

            return(node);
        }
Exemple #5
0
        public static NopStaNode Parse(TokenString tStr, ref int index)
        {
            if (!tStr.Match(index, TokenType.lineEnd))
            {
                return(null);
            }


            NopStaNode node = new NopStaNode(tStr[index].Value);

            index++;

            return(node);
        }
Exemple #6
0
        public static FieldNode Parse(TokenString tStr, ref int index)
        {
            var accessors = AccessibleNode.GetAccessors(tStr, ref index);

            if (!tStr.Match(index, TokenType.typeSpecifier, TokenType.identifier, TokenType.lineEnd))
            {
                return(null);
            }

            string type = tStr[index].Value;

            index++;
            string name = tStr[index].Value;

            index++;

            FieldNode node = new FieldNode(type, name, accessors);

            return(node);
        }
Exemple #7
0
        public static MethodNode Parse(TokenString tStr, ref int index)
        {
            int startIndex = index;

            var accessors = AccessibleNode.GetAccessors(tStr, ref index);

            if (!tStr.Match(index, TokenType.typeSpecifier, TokenType.identifier, TokenType.rBraceOpen))
            {
                return(null);
            }

            string type = tStr[index].Value;

            index++;
            string name = tStr[index].Value;

            index++;

            var args = new List <ArgumentNode>();

            var argTokens = tStr.GetRangeInBrackets(ref index).Split(false, TokenType.comma);

            foreach (var argTs in argTokens)
            {
                args.Add(new ArgumentNode(argTs[0].Value, argTs[1].Value));
            }

            index = tStr.FindNextIndex(index, TokenType.cBraceOpen);

            var sta = CompoundStaNode.Parse(tStr, ref index);

            if (sta == null)
            {
                index = startIndex;
                return(null);
            }

            MethodNode node = new MethodNode(type, name, accessors, args, sta);

            return(node);
        }
Exemple #8
0
        public static ForLoopStaNode Parse(TokenString tStr, ref int index)
        {
            int startIndex = index;

            if (!tStr.Match(index, TokenType.forStatement, TokenType.rBraceOpen))
            {
                return(null);
            }

            index++;

            var forDef = tStr.GetRangeInBrackets(ref index).Split(false, TokenType.lineEnd);

            if (forDef.Count != 3)
            {
                index = startIndex;
                return(null);
            }

            VariableDefExpNode declarations = Exp.Parse(forDef[0]) as VariableDefExpNode;
            ExpressionNode     condition    = Exp.Parse(forDef[1]);

            if (condition == null)
            {
                index = startIndex;
                return(null);
            }

            StatementNode body = Sta.Parse(tStr, ref index);

            ForLoopStaNode node = new ForLoopStaNode(declarations, condition, body);

            var exps = forDef[2].Split(false, TokenType.comma);

            foreach (var exp in exps)
            {
                node.AddExpression(Exp.Parse(exp));
            }

            return(node);
        }
Exemple #9
0
        public static ReturnStaNode Parse(TokenString tStr, ref int index)
        {
            int startIndex = index;

            if (!tStr.Match(index, TokenType.returnStatement))
            {
                return(null);
            }

            index++;

            ExpressionNode expression = Exp.Parse(tStr.GetRangeUntil(ref index, TokenType.lineEnd));

            if (expression == null)
            {
                index = startIndex;
                return(null);
            }

            ReturnStaNode node = new ReturnStaNode(expression);

            index++;
            return(node);
        }
Exemple #10
0
        public static ClassNode Parse(TokenString tStr, ref int index)
        {
            var accessors = AccessibleNode.GetAccessors(tStr, ref index);

            if (!tStr.Match(index, TokenType.@class, TokenType.identifier, TokenType.cBraceOpen))
            {
                return(null);
            }

            int startIndex = index;

            index++;

            ClassNode node = new ClassNode(tStr[index].Value, accessors);

            index++;

            if (tStr[index].Type != TokenType.cBraceOpen)
            {
                index = startIndex;
                return(null);
            }

            // Get internals
            var internalRange = tStr.GetRangeInBrackets(ref index);
            int iRangeIndex   = 0;

            while (iRangeIndex < internalRange.Count)
            {
                Node tempNode;


                if ((tempNode = CtorNode.Parse(node.Name, internalRange, ref iRangeIndex)) != null)
                {
                    node.AddCtor(tempNode as CtorNode);
                }
                else if ((tempNode = MethodNode.Parse(internalRange, ref iRangeIndex)) != null)
                {
                    node.AddMethod(tempNode as MethodNode);
                }
                else if ((tempNode = FieldNode.Parse(internalRange, ref iRangeIndex)) != null)
                {
                    node.AddField(tempNode as FieldNode);
                }
                else
                {
                    iRangeIndex++;
                }

                //if (internalRange.Match(iRangeIndex, TokenType.typeSpecifier, TokenType.identifier))
                //{
                //    if (internalRange.Match(iRangeIndex + 2, TokenType.rBraceOpen))
                //        node.AddMethod(MethodNode.Parse(internalRange, ref iRangeIndex));
                //    else
                //        node.AddField(FieldNode.Parse(internalRange, ref iRangeIndex));
                //}
                //else
                //    iRangeIndex++;
            }

            //index += iRangeIndex;
            return(node);
        }
Exemple #11
0
        public static ExpressionNode Parse(TokenString tStr)
        {
            if (tStr[0].Type == TokenType.rBraceOpen && tStr.Last().Type == TokenType.rBraceClose)
            {
                var trimmedLine = tStr.GetRangeInBrackets(0);

                if (trimmedLine != null)
                {
                    tStr = trimmedLine;
                }
            }

            if (tStr.Last().Type == TokenType.lineEnd)
            {
                tStr.RemoveAt(tStr.Count - 1);
            }

            if (tStr.Count == 1)
            {
                if (tStr[0].Type == TokenType.identifier)
                {
                    return(new IdentifierExpNode(tStr[0].Value));
                }
                else if (tStr[0].Type == TokenType.constant)
                {
                    return(new LiteralExpNode("INTEGER", tStr[0].Value));
                }
                else if (tStr[0].Type == TokenType.@string)
                {
                    return(new LiteralExpNode("STRING", tStr[0].Value));
                }
            }


            if (tStr[0].Type == TokenType.typeSpecifier || tStr[0].Type == TokenType.identifier)
            {
                if (tStr[1].Type == TokenType.identifier)
                {
                    VariableDefExpNode vdNode = new VariableDefExpNode(tStr[0].Value);

                    var defs = tStr.GetRangeUntil(1, TokenType.lineEnd).Split(false, TokenType.comma);

                    foreach (var defTStr in defs)
                    {
                        string varName = defTStr[0].Value;
                        var    varDef  = Exp.Parse(defTStr);

                        vdNode.AddVariable(varName, varDef);
                    }

                    return(vdNode);
                }
            }

            if ((tStr[0].Type == TokenType.prepostOperator || tStr[0].Type == TokenType.preOperator) && tStr[1].Type == TokenType.identifier)
            {
                return(new PreUnaryExpNode(tStr[0].Value, new TokenString(tStr[1])));
            }
            if (tStr[0].Type == TokenType.identifier && (tStr[1].Type == TokenType.prepostOperator || tStr[1].Type == TokenType.postOperator))
            {
                return(new PostUnaryExpNode(tStr[1].Value, new TokenString(tStr[0])));
            }

            int bracNum = 0;

            Token opToken    = null;
            int   opPriority = -1;

            // Get all operators
            for (int i = tStr.Count - 1; i >= 0; i--)
            {
                if (tStr[i].Type == TokenType.rBraceOpen)
                {
                    bracNum++;
                }
                else if (tStr[i].Type == TokenType.rBraceClose)
                {
                    bracNum--;
                }
                else if (bracNum == 0 && (tStr[i].Type == TokenType.binaryOperator || tStr[i].Type == TokenType.accessOperator))
                {
                    int tempOPPriority = Parser.GetOperatorPriority(tStr[i], opPriority);

                    if (tempOPPriority > opPriority)
                    {
                        opToken    = tStr[i];
                        opPriority = tempOPPriority;
                    }
                }
            }


            if (opToken == null)
            {
                if (tStr.Match(0, TokenType.@new, TokenType.identifier, TokenType.rBraceOpen))
                {
                    ConstructionExpNode ceNode = new ConstructionExpNode(tStr[1].Value);

                    var argRangesUnsplit = tStr.GetRangeInBrackets(2);

                    if (argRangesUnsplit.Count > 0)
                    {
                        var argRanges = argRangesUnsplit.Split(false, TokenType.comma);

                        foreach (var argRange in argRanges)
                        {
                            ceNode.AddArgument(Exp.Parse(argRange));
                        }
                    }

                    return(ceNode);
                }
                else if (tStr.Match(0, TokenType.identifier, TokenType.rBraceOpen))
                {
                    MethodCallExpNode mceNode = new MethodCallExpNode(tStr[0].Value);

                    var argRangesUnsplit = tStr.GetRangeInBrackets(1);

                    if (argRangesUnsplit.Count > 0)
                    {
                        var argRanges = argRangesUnsplit.Split(false, TokenType.comma);

                        foreach (var argRange in argRanges)
                        {
                            mceNode.AddArgument(Exp.Parse(argRange));
                        }
                    }

                    return(mceNode);
                }

                return(null);
            }

            int tIndex = tStr.IndexOf(opToken);

            var left  = tStr.GetRangeTStr(0, tIndex);
            var right = tStr.GetRangeTStr(tIndex + 1);

            ExpressionNode node = null;

            switch (opToken.Type)
            {
            case TokenType.binaryOperator:
                node = new BinaryExpNode(opToken.Value, left, right);
                break;

            case TokenType.accessOperator:
                node = new MemberExpNode(opToken.Value, left, right);
                break;

            //case TokenType.preOperator:
            //    node = new PreUnaryExpNode(opToken.Value, right);
            //    break;
            //case TokenType.postOperator:
            //    node = new PostUnaryExpNode(opToken.Value, left);
            //    break;
            //case TokenType.prepostOperator:
            //    if (left.Count == 0)
            //        node = new PreUnaryExpNode(opToken.Value, right);
            //    else
            //        node = new PostUnaryExpNode(opToken.Value, left);
            //    break;

            default:
                break;
            }

            return(node);
        }