private static void AddDeclarationsFromAssignments(Block block, Scope scope) { var variables = block.Expressions.OfType<Assign>().Select(assign => assign.Variable as Value).Where(v => !v.HasProperties); foreach (var name in CompileToNames(variables)) scope.GetOrCreate(VariableType.Variable, name); }
public static Expression CompileCode(Code code, Scope scope) { var block = code.Body as Block; scope = new Scope(scope, block, code, scope.Runtime); var parameters = new List<ParameterExpression>(); var funcArgs = new List<Type> { typeof(object) }; foreach (var name in CompileToNames(code.Parameters)) { parameters.Add(scope.GetOrCreate(VariableType.Parameter, name)); funcArgs.Add(typeof(object)); } AddDeclarationsFromAssignments(block, scope); var body = Compile(code.Body, scope); Helper.IsTrue(string.IsNullOrEmpty(code.Name)); return Expression.Lambda(Expression.GetFuncType(funcArgs.ToArray()), body, code.Name, parameters); }