internal PythonFunction(CodeContext/*!*/ context, Delegate target, FunctionInfo funcInfo, object modName, object[] defaults, Tuple closure) { Assert.NotNull(context, funcInfo); Assert.NotNull(context.Scope); _funcInfo = funcInfo; _context = context; _defaults = defaults ?? ArrayUtils.EmptyObjects; Target = target; Debug.Assert(_defaults.Length <= _funcInfo.ParameterCount); if (modName != Uninitialized.Instance) { _module = modName; } Closure = closure; _compat = CalculatedCachedCompat(); _code = new FunctionCode(this, funcInfo); }
internal static expr Convert(Compiler.Ast.Expression expr, expr_context ctx) { expr ast; if (expr is ConstantExpression) ast = Convert((ConstantExpression)expr); else if (expr is NameExpression) ast = new Name((NameExpression)expr, ctx); else if (expr is UnaryExpression) ast = new UnaryOp((UnaryExpression)expr); else if (expr is BinaryExpression) ast = Convert((BinaryExpression)expr); else if (expr is AndExpression) ast = new BoolOp((AndExpression)expr); else if (expr is OrExpression) ast = new BoolOp((OrExpression)expr); else if (expr is CallExpression) ast = new Call((CallExpression)expr); else if (expr is ParenthesisExpression) return Convert(((ParenthesisExpression)expr).Expression); else if (expr is LambdaExpression) ast = new Lambda((LambdaExpression)expr); else if (expr is ListExpression) ast = new List((ListExpression)expr, ctx); else if (expr is TupleExpression) ast = new Tuple((TupleExpression)expr, ctx); else if (expr is DictionaryExpression) ast = new Dict((DictionaryExpression)expr); else if (expr is ListComprehension) ast = new ListComp((ListComprehension)expr); else if (expr is GeneratorExpression) ast = new GeneratorExp((GeneratorExpression)expr); else if (expr is MemberExpression) ast = new Attribute((MemberExpression)expr, ctx); else if (expr is YieldExpression) ast = new Yield((YieldExpression)expr); else if (expr is ConditionalExpression) ast = new IfExp((ConditionalExpression)expr); else if (expr is IndexExpression) ast = new Subscript((IndexExpression)expr, ctx); else if (expr is SliceExpression) ast = new Slice((SliceExpression)expr); else if (expr is BackQuoteExpression) ast = new Repr((BackQuoteExpression)expr); else throw new ArgumentTypeException("Unexpected expression type: " + expr.GetType()); ast.GetSourceLocation(expr); return ast; }
/// <summary> /// Find all variable assignments in the source code and attempt to discover their type /// </summary> /// <param name="code">The code from whih to get the assignments</param> /// <returns>A dictionary matching the name of the variable to a tuple of typeName, character at which the assignment was found, and the CLR type</returns> public Dictionary<string, Tuple<string, int, Type> > FindAllVariables(string code) { // regex to collection var variables = new Dictionary<string, Tuple<string, int, Type>>(); var variableStatements = Regex.Matches(code, variableName + spacesOrNone + equals + spacesOrNone + @"(.*)", RegexOptions.Multiline); for (var i = 0; i < variableStatements.Count; i++) { var name = variableStatements[i].Groups[1].Value.Trim(); var typeString = variableStatements[i].Groups[6].Value.Trim(); // type var currentIndex = variableStatements[i].Index; // check if matches typename(blabla) - in this case its a type we need to look up var typeStringMatches = Regex.Matches(typeString, @"^(.*)\(.*\)$", RegexOptions.Singleline); if (typeStringMatches.Count > 0) { typeString = typeStringMatches[0].Groups[1].Value.Trim(); var typeInScope = this.LookupMember(typeString); if (typeInScope is PythonType) // dictionary, enum, etc { var type = ClrModule.GetClrType(typeInScope as PythonType); //// if we already saw this var if (variables.ContainsKey(name)) { var varInfo = variables[name]; if (currentIndex > varInfo.Item2) { variables[name] = new Tuple<string, int, Type>(typeString, currentIndex, type); } } else // we've never seen it, add the type { variables.Add(name, new Tuple<string, int, Type>(typeString, currentIndex, type)); } } } else // match default types (numbers, dicts, arrays, strings) { foreach (var pair in RegexToType) { var matches = Regex.Matches(typeString, "^" + pair.Key + "$", RegexOptions.Singleline); if (matches.Count > 0) { // if we already saw this var if (variables.ContainsKey(name)) { var varInfo = variables[name]; if (currentIndex > varInfo.Item2) { variables[name] = new Tuple<string, int, Type>(typeString, currentIndex, pair.Value); } } else // we've never seen it, add the type { variables.Add(name, new Tuple<string, int, Type>(typeString, currentIndex, pair.Value)); } break; } } } } return variables; }
public RuntimeVariablesDictionaryStorage(Tuple boxes, SymbolId[] args) { _boxes = boxes; _args = args; }
private static void GetTupleValues(Tuple tuple, List<object> args) { Type[] types = tuple.GetType().GetGenericArguments(); for (int i = 0; i < types.Length; i++) { if (typeof(Tuple).IsAssignableFrom(types[i])) { GetTupleValues((Tuple)tuple.GetValue(i), args); } else if (types[i] != typeof(DynamicNull)) { args.Add(tuple.GetValue(i)); } } }
/// <summary> /// Gets the values from a tuple including unpacking nested values. /// </summary> public static object[] GetTupleValues(Tuple tuple) { ContractUtils.RequiresNotNull(tuple, "tuple"); List<object> res = new List<object>(); GetTupleValues(tuple, res); return res.ToArray(); }
internal TotemConvertBinder GetConvertBinder(Type type, bool @explicit) { Tuple<Type, bool> key = new Tuple<Type, bool>(type, @explicit); TotemConvertBinder ret; if (!convertBinders.TryGetValue(key, out ret)) { ret = new TotemConvertBinder(this, type, @explicit); convertBinders[key] = ret; } return ret; }