Exemplo n.º 1
0
        private Object evalNewExpression(Object context, NewExpression expression)
        {
            CallExpression ctorCallExp;
            Prototype      prototype;

            if (expression.ctorCall is CallExpression ctorCall)
            {
                string typeName     = ctorCall.Function.Literal;
                Object prototypeObj = ExecutionContext.Peek()[typeName];
                if (prototypeObj is not Prototype)
                {
                    throw new System.Exception("The object named \"" +
                                               typeName + "\" is not a type");
                }
                ctorCallExp = ctorCall;
                prototype   = prototypeObj as Prototype;
            }
            else
            {
                // is PathExpression
                PathExpression    typePath = expression.ctorCall as PathExpression;
                List <Expression> path     = typePath.Path;
                Expression        pathCall = path[path.Count - 1];
                if (pathCall is not CallExpression)
                {
                    throw new System.Exception("Need a type");
                }
                // get the parent of prototype
                PathExpression parentPath = typePath.SubPath(0, -1);
                Object         parent     = Eval(ExecutionContext.Peek(), parentPath);
                ctorCallExp = pathCall as CallExpression;
                if (parent[ctorCallExp.Function.Literal] is Prototype prototypeTarget)
                {
                    prototype = prototypeTarget;
                }
                else
                {
                    throw new System.Exception("Need a type");
                }
            }
            Prototype     type       = prototype;
            List <Object> callParams = new List <Object>();

            foreach (Expression param in ctorCallExp.Parameters)
            {
                callParams.Add(Eval(ExecutionContext.Peek(), param));
            }
            return(type.Create(callParams));
        }