/// <summary> /// throw error; /// </summary> /// <returns></returns> public override Expr Parse() { // Validate _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; } var exp = new NewExpr(); exp.TypeName = typeName; _parser.State.FunctionCall++; _parser.ParseParameters(exp, true, false); _parser.State.FunctionCall--; return exp; }
/// <summary> /// Visits the function call expression tree /// </summary> /// <param name="exp"></param> public object VisitNew(NewExpr exp) { return null; }
/// <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 (!LDateType.CanCreateFrom(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; }
/// <summary> /// Creates new instance of the type. /// </summary> /// <returns></returns> public static object EvalNew(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); 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 = 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 = Ctx.Types.Create(expr.TypeName, hostLangArgs); return new LClass(instance); }
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; }