Exemplo n.º 1
0
        public Expr OnParseNew()
        {
            var tokenIt = this._parser.TokenIt;
            var initiatorToken = tokenIt.NextToken;
            var expr = new NewExpr();
            // <codeNew>

            tokenIt.Expect(Tokens.New);
            var typeName = tokenIt.ExpectId();

            // Keep parsing to capture full name.
            // e.g new App.Core.User()
            while (tokenIt.NextToken.Token == Tokens.Dot)
            {
                tokenIt.Advance();
                var name = tokenIt.ExpectId();
                typeName += "." + name;
                if (tokenIt.IsEndOfStmtOrBlock())
                    break;
            }
            expr.TypeName = typeName;
            expr.ParamListExpressions = new List<Expr>();
            expr.ParamList = new List<object>();
            this._parser.State.FunctionCall++;
            this._parser.ParseParameters(expr, true, false);
            this._parser.State.FunctionCall--;

            // </codeNew>
            this._parser.SetupContext(expr, initiatorToken);
            return expr;
        }
Exemplo n.º 2
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;
        }
Exemplo n.º 3
0
        /// <summary>
        /// Check new expressions.
        /// </summary>
        /// <param name="semActs">The semantic analyser</param>
        /// <param name="exp">The newexpression</param>
        private SemanticCheckResult CheckNewExpression(SemActs semActs, NewExpr exp)
        {
            var typeName = exp.TypeName;

            // 1. Check # params to Date 
            if (string.Compare(typeName, "Date", StringComparison.InvariantCultureIgnoreCase) == 0)
            {
                if (!DateTimeTypeHelper.CanCreateDateFrom(exp.ParamListExpressions.Count))
                    return AddError("Unexpected number of inputs when creating date", exp);
            }
            // 2. Check # params to Time
            else if (string.Compare(typeName, "Time", StringComparison.InvariantCultureIgnoreCase) == 0)
            {
                if (!DateTimeTypeHelper.CanCreateTimeFrom(exp.ParamListExpressions.Count))
                    return AddError("Unexpected number of inputs when creating time", exp);
            }
            return SemanticCheckResult.Valid;
        }
Exemplo n.º 4
0
 /// <summary>
 /// Visits the function call expression tree
 /// </summary>
 /// <param name="exp"></param>
 public object VisitNew(NewExpr exp)
 {
     return null;
 }