Example #1
0
        public override EvalResult Evaluate(MakeState s)
        {
            EvalResult elabel = label.Evaluate(s);

            if (elabel.Type == EvalResult.ResultType.Undefined)
            {
                throw new Statement.SyntaxException("attempt to access member of undefined object " + elabel.strval, this);
            }

            if (member is LabelExpression)
            {
                string m = ((LabelExpression)member).val;

                switch (elabel.Type)
                {
                case EvalResult.ResultType.String:
                    if (m == "length")
                    {
                        return(new EvalResult(elabel.strval.Length));
                    }
                    break;

                case EvalResult.ResultType.Object:
                    if (elabel.objval.ContainsKey(m))
                    {
                        return(elabel.objval[m]);
                    }
                    break;

                case EvalResult.ResultType.Array:
                    if (m == "length")
                    {
                        return(new EvalResult(elabel.arrval.Count));
                    }
                    break;
                }

                if (m == "type")
                {
                    return(new EvalResult(elabel.Type.ToString()));
                }
                throw new Statement.SyntaxException("object: " + label.ToString() + " does not contain member " + m.ToString(), this);
            }
            else if (member is FuncCall)
            {
                FuncCall f     = member as FuncCall;
                FuncCall fsite = new FuncCall {
                    target = f.target
                };
                fsite.args = new List <Expression>(f.args);
                fsite.args.Insert(0, label);
                string m = fsite.Mangle(s, out var ers);

                switch (elabel.Type)
                {
                case EvalResult.ResultType.String:
                    if (m == "5splitss")
                    {
                        string[]     split = elabel.strval.Split(new string[] { ers[1].strval }, StringSplitOptions.None);
                        EvalResult[] ret   = new EvalResult[split.Length];
                        for (int i = 0; i < split.Length; i++)
                        {
                            ret[i] = new EvalResult(split[i]);
                        }
                        return(new EvalResult(ret));
                    }
                    else if (m == "9substringsi")
                    {
                        return(new EvalResult(elabel.strval.Substring((int)ers[1].AsInt)));
                    }
                    else if (m == "9substringsii")
                    {
                        return(new EvalResult(elabel.strval.Substring((int)ers[1].AsInt, (int)ers[2].AsInt)));
                    }
                    else if (m == "8containsss")
                    {
                        return(new EvalResult(elabel.strval.Contains(ers[1].strval) ? -1 : 0));
                    }
                    break;

                case EvalResult.ResultType.Array:
                    if (m == "3addai")
                    {
                        elabel.arrval.Add(new EvalResult(ers[1].intval));
                        return(new EvalResult());
                    }
                    else if (m == "3addas")
                    {
                        elabel.arrval.Add(new EvalResult(ers[1].strval));
                        return(new EvalResult());
                    }
                    else if (m == "3addao")
                    {
                        elabel.arrval.Add(new EvalResult(ers[1].objval));
                        return(new EvalResult());
                    }
                    else if (m == "3addaa")
                    {
                        elabel.arrval.Add(new EvalResult(ers[1].arrval));
                        return(new EvalResult());
                    }
                    else if (m == "3addav")
                    {
                        elabel.arrval.Add(new EvalResult());
                        return(new EvalResult());
                    }
                    else if (m == "3addan")
                    {
                        elabel.arrval.Add(new EvalResult {
                            Type = EvalResult.ResultType.Null
                        });
                        return(new EvalResult());
                    }
                    else if (m == "8addrangeaa")
                    {
                        elabel.arrval.AddRange(ers[1].arrval);
                        return(new EvalResult());
                    }
                    else if (m == "6insertaii")
                    {
                        elabel.arrval.Insert((int)ers[1].intval, new EvalResult(ers[2].intval));
                        return(new EvalResult());
                    }
                    else if (m == "6insertais")
                    {
                        elabel.arrval.Insert((int)ers[1].intval, new EvalResult(ers[2].strval));
                        return(new EvalResult());
                    }
                    else if (m == "6insertaio")
                    {
                        elabel.arrval.Insert((int)ers[1].intval, new EvalResult(ers[2].objval));
                        return(new EvalResult());
                    }
                    else if (m == "6insertaia")
                    {
                        elabel.arrval.Insert((int)ers[1].intval, new EvalResult(ers[2].arrval));
                        return(new EvalResult());
                    }
                    else if (m == "6removeai")
                    {
                        elabel.arrval.RemoveAt((int)ers[1].intval);
                        return(new EvalResult());
                    }
                    else if (m == "8containsai" || m == "8containsas")
                    {
                        return(new EvalResult(elabel.arrval.Contains(ers[1]) ? 1 : 0));
                    }
                    break;

                case EvalResult.ResultType.Object:
                    if (m == "11containskeyos")
                    {
                        return(new EvalResult(elabel.objval.ContainsKey(ers[1].strval) ? 1 : 0));
                    }
                    else if (m == "3addoss")
                    {
                        elabel.objval[ers[1].strval] = new EvalResult(ers[2].strval);
                        return(new EvalResult());
                    }
                    else if (m == "3addoso")
                    {
                        elabel.objval[ers[1].strval] = new EvalResult(ers[2].objval);
                        return(new EvalResult());
                    }
                    else if (m == "3getos")
                    {
                        return(elabel.objval[ers[1].strval]);
                    }
                    if (elabel.objval.ContainsKey(m))
                    {
                        EvalResult feval = elabel.objval[m];
                        if (feval.Type == EvalResult.ResultType.Function)
                        {
                            return(feval.funcval.Run(s, ers));
                        }
                    }

                    break;
                }

                throw new Statement.SyntaxException("object: " + label.ToString() + " does not contain member " + m.ToString(), this);
            }
            else
            {
                throw new NotSupportedException();
            }
        }