public override DLR.Expression GenerateExpressionTree(GenScope scope) { if (!scope.IsInLambda()) { throw new InvalidOperationException("No function to return from!"); } if (Expression is null) { return(DLR.Expression.Goto(scope.GetFirstLambdaScope().CreateReturnTarget())); } var expression = Expression.GenerateExpressionTree(scope); return(DLR.Expression.Goto(scope.GetFirstLambdaScope().CreateReturnTarget(false), expression, expression.Type)); }
public override DLR.Expression GenerateExpressionTree(GenScope scope) { if (scope.IsInLoop() || scope.IsInLambda()) { throw new InvalidOperationException("Cannot declare a function inside the body of another function or inside a loop!"); } if (scope.Root.FunctionDefinitions.ContainsKey(Name)) { throw new InvalidOperationException($"Function \"{Name}\" is already defined!"); } var functionScope = scope.CreateLambda(); var parameters = new DLR.ParameterExpression[Parameters.Count]; for (var i = 0; i < Parameters.Count; i++) { var name = Parameters.ElementAt(i).Name; var param = DLR.Expression.Parameter(typeof(object), name); parameters[i] = param; functionScope.Definitions[name] = param; } var body = Body.GenerateExpressionTree(functionScope); DLR.LambdaExpression function = null; if (functionScope.ReturnTarget is null) //void returning function { function = DLR.Expression.Lambda(body, Name, parameters); } else { var wrapper = DLR.Expression.Block(body, DLR.Expression.Label(functionScope.ReturnTarget, body)); function = DLR.Expression.Lambda(wrapper, Name, parameters); } scope.Root.FunctionDefinitions[Name] = function.Compile(); return(function); }