/// <summary> /// Evaluates an array type declaration. /// </summary> /// <returns></returns> public object VisitArray(ArrayExpr expr) { var arrayExprs = expr.Exprs; // Case 1: array type if (arrayExprs != null) { var items = new List<object>(); foreach (var exp in arrayExprs) { object result = exp == null ? null : exp.Evaluate(this); items.Add(result); } var array = new LArray(items); return array; } return LObjects.Null; }
private static object ConvertToHostLangArray(Type type, LArray array) { var length = array.Value.Count; var fsarray = array.Value; Array items = null; var hostType = typeof (int); if ( type == typeof(string[])) { items = new string[length]; hostType = typeof (string); } else if (type == typeof(bool[])) { items = new bool[length]; hostType = typeof (bool); } else if (type == typeof(DateTime[])) { items = new DateTime[length]; hostType = typeof (DateTime); } else if (type == typeof(TimeSpan[])) { items = new TimeSpan[length]; hostType = typeof (TimeSpan); } else if (type == typeof(double[])) { items = new double[length]; hostType = typeof (double); } else if (type == typeof(float[])) { items = new float[length]; hostType = typeof (float); } else if (type == typeof(long[])) { items = new long[length]; hostType = typeof (long); } else if (type == typeof(int[])) { items = new int[length]; hostType = typeof (int); } for(var ndx = 0; ndx < fsarray.Count; ndx++) { var val = fsarray[ndx] as LObject; var hostval = val.GetValue(); // This converts double to long as fluentscript only supports double right now. var converted = ConvertToCorrectHostLangValue(hostType, hostval); items.SetValue(converted, ndx); } return items; }
private void PushParametersInScope() { if (this.ArgumentValues == null || this.ArgumentValues.Count == 0) return; if (this.Meta.Arguments == null || this.Meta.Arguments.Count == 0) return; //if (this.ArgumentValues.Count > this.Meta.Arguments.Count) // throw new ArgumentException("Invalid function call, more arguments passed than arguments in function: line " + Caller.Ref.Line + ", pos: " + Caller.Ref.CharPos); // Check if there is an parameter named "arguments" var hasParameterNamedArguments = false; if (this.Meta.Arguments != null && this.Meta.Arguments.Count > 0) if (this.Meta.ArgumentsLookup.ContainsKey("arguments")) hasParameterNamedArguments = true; // Add function arguments to scope. for (int ndx = 0; ndx < this.Meta.Arguments.Count; ndx++) { var val = this.ArgumentValues[ndx] as LObject; if(val.Type.IsPrimitiveType()) { var copied = val.Clone(); this.ArgumentValues[ndx] = copied; } this.Ctx.Memory.SetValue(this.Meta.Arguments[ndx].Name, this.ArgumentValues[ndx]); } // Finally add the arguments. // NOTE: Any extra arguments will be part of the implicit "arguments" array. if(!hasParameterNamedArguments) { var argArray = new LArray(this.ArgumentValues); this.Ctx.Memory.SetValue("arguments", argArray); } }
/// <summary> /// Evaluates an array type declaration. /// </summary> /// <returns></returns> public static object EvalArrayType(List<Expr> arrayExprs) { // Case 1: array type if (arrayExprs != null) { var items = new List<object>(); foreach (var exp in arrayExprs) { object result = exp == null ? null : exp.Evaluate(); items.Add(result); } var array = new LArray(items); return array; } return LObjects.Null; }
private void PushParametersInScope(FunctionExpr expr) { // 1. Validate : any arguments. if (expr.ArgumentValues == null || expr.ArgumentValues.Count == 0) return; if (expr.Meta.Arguments == null || expr.Meta.Arguments.Count == 0) return; // 2. Check if there is an parameter named "arguments" var hasParameterNamedArguments = false; if (expr.Meta.Arguments != null && expr.Meta.Arguments.Count > 0) if (expr.Meta.ArgumentsLookup.ContainsKey("arguments")) hasParameterNamedArguments = true; // 3. Get the symbolscope of the inside of the function and see if any statements. ISymbols symscope = null; var hasStatements = expr.Statements != null && expr.Statements.Count > 0; if (hasStatements) symscope = expr.Statements[0].SymScope; // 3. Add function arguments to scope for (var ndx = 0; ndx < expr.Meta.Arguments.Count; ndx++) { var val = expr.ArgumentValues[ndx] as LObject; var arg = expr.Meta.Arguments[ndx]; // 4. Clone primitive datatypes. if (val.Type.IsPrimitiveType()) { var copied = val.Clone(); expr.ArgumentValues[ndx] = copied; } // 5. Now, set the memory value of the parameter. this.Ctx.Memory.SetValue(arg.Name, val); // 6. Finally, update the symbol type if (hasStatements) { var sym = symscope.GetSymbol(arg.Name); if (sym != null && val.Type.TypeVal == TypeConstants.Function && sym.Category != SymbolCategory.Func) { SymbolHelper.ResetSymbolAsFunction(symscope, arg.Name, val); } } } // Finally add the arguments. // NOTE: Any extra arguments will be part of the implicit "arguments" array. if (!hasParameterNamedArguments) { var argArray = new LArray(expr.ArgumentValues); expr.Ctx.Memory.SetValue("arguments", argArray); } }