예제 #1
0
        static CArg BuildElemToArg(SBuildElem elem, ILogger inLogger)
        {
            CArg left_arg;

            if (elem.Token != null)
            {
                if (elem.Token.TokenType == ETokenType.Float ||
                    elem.Token.TokenType == ETokenType.Int ||
                    elem.Token.TokenType == ETokenType.UInt)
                {
                    left_arg = new CArg_Num(elem.Token.GetFloatValue(), elem.Token.StartLineIndex, elem.Token.EndLineIndex);
                }
                else if (elem.Token.TokenType == ETokenType.Word)
                {
                    left_arg = new CArg_Var(elem.Token.Text, elem.Token.StartLineIndex, elem.Token.EndLineIndex);
                }
                else
                {
                    LogError(inLogger, EErrorCode.InvalidArgument, elem.Token);
                    return(null);
                }
            }
            else if (elem.Op != null)
            {
                left_arg = new CArg_BinOp(elem.Op);
            }
            else
            {
                LogError(inLogger, EErrorCode.InvalidArgument, "Empty SBuildElem");
                return(null);
            }
            return(left_arg);
        }
예제 #2
0
        private static KeyValuePair <bool, int> FindCloseBrace(int inStartPos, List <SBuildElem> inList, ILogger inLogger)
        {
            int bdeep = 0;

            for (int i = inStartPos; i < inList.Count; i++)
            {
                SBuildElem el = inList[i];
                if (el.Token != null && el.Token.TokenType == ETokenType.OpenBrace)
                {
                    bdeep++;
                }

                if (el.Token != null && el.Token.TokenType == ETokenType.CloseBrace)
                {
                    if (bdeep == 0)
                    {
                        return(new KeyValuePair <bool, int>(true, i));
                    }
                    else
                    {
                        bdeep--;
                    }
                }
            }

            SBuildElem fel = inList[inStartPos];

            inLogger.LogError(string.Format("Can't find close bracer from {0} position!", fel.StartLineIndex));
            return(new KeyValuePair <bool, int>(false, -1));
        }
예제 #3
0
        static List <SBuildElem> ChangeBracers(int inStartPos, int inEndPos, List <SBuildElem> inList, ILogger inLogger)
        {
            List <SBuildElem> lst = new List <SBuildElem>();

            for (int i = inStartPos; i <= inEndPos;)
            {
                SBuildElem el = inList[i];
                if (el.Token != null && el.Token.TokenType == ETokenType.CloseBrace)
                {
                    LogError(inLogger, EErrorCode.InvalidCloseBracer, el.Token);
                    return(null);
                }
                else if (el.Token != null && el.Token.TokenType == ETokenType.OpenBrace)
                {
                    KeyValuePair <bool, int> res_close_brace_pos = FindCloseBrace(i + 1, inList, inLogger);
                    if (!res_close_brace_pos.Key)
                    {
                        return(null);
                    }

                    KeyValuePair <bool, IMathFunc> res_op;
                    if (i > inStartPos && inList[i - 1].Token != null && inList[i - 1].Token.TokenType == ETokenType.Word)
                    {                                //func
                        lst.RemoveAt(lst.Count - 1); //func name
                        res_op = BuildFunc(inList[i - 1].Token.Text, inList[i - 1].Token.StartLineIndex, i + 1, res_close_brace_pos.Value - 1, inList, inLogger);
                    }
                    else
                    {//simple ( )
                        res_op = CheckOneOrMany(i + 1, res_close_brace_pos.Value - 1, inList, inLogger);
                    }

                    if (!res_op.Key)
                    {
                        return(null);
                    }

                    if (res_op.Value != null)
                    {
                        lst.Add(new SBuildElem(res_op.Value));
                    }

                    i = res_close_brace_pos.Value + 1;
                }
                else
                {
                    lst.Add(inList[i]);
                    i++;
                }
            }

            return(lst);
        }
예제 #4
0
        private static int FindComma(int inStartPos, int inEndPos, List <SBuildElem> inList, ILogger inLogger)
        {
            for (int i = inStartPos; i <= inEndPos; i++)
            {
                SBuildElem el = inList[i];
                if (el.Token != null && el.Token.TokenType == ETokenType.Comma)
                {
                    return(i);
                }
            }

            return(-1);
        }
예제 #5
0
        static KeyValuePair <bool, IMathFunc> BuildTriplet(int inPos, List <SBuildElem> inList, ILogger inLogger)
        {
            if (inPos <= 0 || inPos >= inList.Count - 1)
            {
                LogError(inLogger, EErrorCode.InvalidTripletPosition, $"{inList[inPos].StartLineIndex}");
                return(new KeyValuePair <bool, IMathFunc>(false, null));
            }

            EBinOp     op_type = EBinOp.Undefined;
            SBuildElem el      = inList[inPos];

            if (el.Token != null)
            {
                op_type = el.Token.GetBinOp();
            }

            if (op_type == EBinOp.Undefined)
            {
                if (el.Token != null)
                {
                    LogError(inLogger, EErrorCode.BinOperationUndefined, el.Token);
                }
                else
                {
                    LogError(inLogger, EErrorCode.BinOperationUndefined, string.Format("el.Op {0}", el.Op));
                }
                return(new KeyValuePair <bool, IMathFunc>(false, null));
            }

            //left
            el = inList[inPos - 1];
            CArg left_arg = BuildElemToArg(el, inLogger);

            //right
            el = inList[inPos + 1];
            CArg right_arg = BuildElemToArg(el, inLogger);

            if (left_arg == null || right_arg == null)
            {
                return(new KeyValuePair <bool, IMathFunc>(false, null));
            }

            return(new KeyValuePair <bool, IMathFunc>(true, new CBinOp(op_type, left_arg, right_arg)));
        }