示例#1
0
        /// <summary>
        /// Creates new instance of the type.
        /// </summary>
        /// <returns></returns>
        public object VisitNew(NewExpr expr)
        {
            object[] constructorArgs = null;
            var      paramListExprs  = expr.ParamListExpressions;

            if (paramListExprs != null && paramListExprs.Count > 0)
            {
                expr.ParamList = new List <object>();
                ParamHelper.ResolveNonNamedParameters(paramListExprs, expr.ParamList, this);
                constructorArgs = expr.ParamList.ToArray();
            }

            // CASE 1: Built in basic system types ( string, date, time, etc )
            if (LTypesLookup.IsBasicTypeShortName(expr.TypeName))
            {
                // TODO: Move this check to Semacts later
                var langType  = LTypesLookup.GetLType(expr.TypeName);
                var methods   = this.Ctx.Methods.Get(langType);
                var canCreate = methods.CanCreateFromArgs(constructorArgs);
                if (!canCreate)
                {
                    throw ExceptionHelper.BuildRunTimeException(expr, "Can not create " + expr.TypeName + " from parameters");
                }

                // Allow built in type methods to create it.
                var result = methods.CreateFromArgs(constructorArgs);
                return(result);
            }
            // CASE 2: Custom types e.g. custom classes.
            var hostLangArgs = LangTypeHelper.ConvertToArrayOfHostLangValues(constructorArgs);
            var instance     = this.Ctx.Types.Create(expr.TypeName, hostLangArgs);
            var obj          = LangTypeHelper.ConvertToLangClass(instance);

            return(obj);
        }
示例#2
0
        /// <summary>
        /// Creates new instance of the type.
        /// </summary>
        /// <returns></returns>
        public override object DoEvaluate()
        {
            object[] constructorArgs = null;
            if (ParamListExpressions != null && ParamListExpressions.Count > 0)
            {
                ParamList = new List <object>();
                ParamHelper.ResolveNonNamedParameters(ParamListExpressions, ParamList);
                constructorArgs = ParamList.ToArray();
            }

            // CASE 1: Built in basic system types ( string, date, time, etc )
            if (LTypesLookup.IsBasicTypeShortName(this.TypeName))
            {
                // TODO: Move this check to Semacts later
                var langType  = LTypesLookup.GetLType(this.TypeName);
                var methods   = this.Ctx.Methods.Get(langType);
                var canCreate = methods.CanCreateFromArgs(constructorArgs);
                if (!canCreate)
                {
                    throw BuildRunTimeException("Can not create " + this.TypeName + " from parameters");
                }

                // Allow built in type methods to create it.
                var result = methods.CreateFromArgs(constructorArgs);
                return(result);
            }
            // CASE 2: Custom types e.g. custom classes.
            var hostLangArgs = LangTypeHelper.ConvertToArrayOfHostLangValues(constructorArgs);
            var instance     = Ctx.Types.Create(this.TypeName, hostLangArgs);

            return(new LClass(instance));
        }