override public void LeaveBlockExpression(BlockExpression node) { var closureEntity = GetEntity(node) as InternalMethod; if (closureEntity == null) return; var collector = new ForeignReferenceCollector(); { collector.CurrentMethod = closureEntity.Method; collector.CurrentType = (IType)closureEntity.DeclaringType; closureEntity.Method.Body.Accept(collector); if (collector.ContainsForeignLocalReferences) { BooClassBuilder closureClass = CreateClosureClass(collector, closureEntity); closureClass.ClassDefinition.LexicalInfo = node.LexicalInfo; collector.AdjustReferences(); ReplaceCurrentNode( CodeBuilder.CreateMemberReference( collector.CreateConstructorInvocationWithReferencedEntities( closureClass.Entity), closureEntity)); } else { Expression expression = CodeBuilder.CreateMemberReference(closureEntity); expression.LexicalInfo = node.LexicalInfo; TypeSystemServices.GetConcreteExpressionType(expression); ReplaceCurrentNode(expression); } } }
public static Expression evaluate(BlockExpression formula) { var body = new Block(formula.LexicalInfo); for (int i = 0; i < formula.Body.Statements.Count; ++i) { var statement = formula.Body.Statements[i]; if (statement is ExpressionStatement && i == formula.Body.Statements.Count - 1) { var last = (ExpressionStatement)statement; body.Statements.Add(new ReturnStatement(last.Expression)); } else body.Statements.Add(formula.Body.Statements[i]); } var result = new BlockExpression(body); result.Parameters.Add(new ParameterDeclaration("this", CompilerContext.Current.CodeBuilder .CreateTypeReference(EvaluationContext.CurrentContext.GetType()))); result.ReturnType = CompilerContext.Current.CodeBuilder .CreateTypeReference(typeof(bool)); return new MethodInvocationExpression( new ReferenceExpression("SetEvaluationFunction"), result); }
public object VisitAnonymousMethodExpression(AnonymousMethodExpression anonymousMethodExpression, object data) { B.BlockExpression cbe = new B.BlockExpression(GetLexicalInfo(anonymousMethodExpression)); cbe.EndSourceLocation = GetLocation(anonymousMethodExpression.EndLocation); cbe.Body = ConvertBlock(anonymousMethodExpression.Body); ConvertParameters(anonymousMethodExpression.Parameters, cbe.Parameters); return(cbe); }
public override void OnBlockExpression(BlockExpression node) { if (node.LexicalInfo.Line <= resolver.CaretLine && GetEndSourceLocation(node).Line >= resolver.CaretLine - 1) { foreach (ParameterDeclaration param in node.Parameters) { DeclarationFound(param.Name, param.Type ?? (resolver.IsDucky ? new SimpleTypeReference("duck") : new SimpleTypeReference("object")), null, param.LexicalInfo); } base.OnBlockExpression(node); } }
public static Expression when(Expression expression) { BlockExpression right = new BlockExpression(); right.Body.Add(new ReturnStatement(expression)); return new BinaryExpression( BinaryOperatorType.Assign, new ReferenceExpression("condition"), right ); }
public static Expression when(Expression expression, Expression action) { BlockExpression condition = new BlockExpression(); condition.Body.Add(new ReturnStatement(expression)); return new MethodInvocationExpression( new ReferenceExpression("When"), condition, action ); }
public static Statement when(Expression expression) { var body = new Block(expression.LexicalInfo); body.Add(new ReturnStatement(expression)); var result = new BlockExpression(body); result.Parameters.Add( new ParameterDeclaration("order", CurrentContext.CodeBuilder.CreateTypeReference(typeof(Order)))); result.Parameters.Add( new ParameterDeclaration("customer", CurrentContext.CodeBuilder.CreateTypeReference(typeof(Customer)))); return new ReturnStatement(result); }
public static Expression CreateCallableFromMacroBody(BooCodeBuilder builder, MacroStatement macro) { // create closure for macro's body or null Expression macroBody = new NullLiteralExpression(); if (macro.Block.Statements.Count > 0) { var callableExpr = new BlockExpression {Body = macro.Block}; callableExpr.Parameters.Add( new ParameterDeclaration("OutputStream", builder.CreateTypeReference(typeof(TextWriter)))); macroBody = callableExpr; } return macroBody; }
public object VisitLambdaExpression(LambdaExpression lambdaExpression, object data) { B.BlockExpression cbe = new B.BlockExpression(GetLexicalInfo(lambdaExpression)); cbe.EndSourceLocation = GetLocation(lambdaExpression.EndLocation); if (lambdaExpression.StatementBody.IsNull) { cbe.Body = new B.Block(); cbe.Body.Add(new B.ReturnStatement(ConvertExpression(lambdaExpression.ExpressionBody))); } else { cbe.Body = ConvertBlock(lambdaExpression.StatementBody); } ConvertParameters(lambdaExpression.Parameters, cbe.Parameters); return(cbe); }
public static Expression exclude(BlockExpression action) { var arrayExpression = new ArrayLiteralExpression(); foreach (Statement statement in action.Body.Statements) { var stringLiteral = new StringLiteralExpression( ((MethodInvocationExpression) (((ExpressionStatement) (statement)).Expression)).Arguments[0].ToString().Trim(new[]{'\''})); arrayExpression.Items.Add(stringLiteral); } return new MethodInvocationExpression( new ReferenceExpression("SetExcludeList"), arrayExpression ); }
public override void OnBlockExpression(BlockExpression node) { var dependencies = new ArrayLiteralExpression(); foreach (Statement statement in node.Body.Statements) { var expressionStatement = (ExpressionStatement)statement; var expression = (MethodInvocationExpression)expressionStatement.Expression; OnMethodInvocationExpression(dependencies, expression); } if (dependencies.Items.Count == 0) return; var referenceExpression = new ReferenceExpression("AddDependencies"); var replacementMethod = new MethodInvocationExpression(referenceExpression, dependencies); ReplaceCurrentNode(replacementMethod); }
public override void OnBlockExpression(BlockExpression node) { }
public static Expression prebuild(BlockExpression commands) { var cmdList = new ArrayLiteralExpression(); foreach (Statement statement in commands.Body.Statements) { var expression = (MethodInvocationExpression)((ExpressionStatement)statement).Expression; cmdList.Items.Add(new StringLiteralExpression(expression.Arguments[0].ToString().Trim(new char[] { '\'' }))); } return new MethodInvocationExpression(new ReferenceExpression("ParseCommands"), cmdList); }
public static Expression mode(ReferenceExpression expression, BlockExpression action) { var modename = new StringLiteralExpression(expression.Name); return new MethodInvocationExpression( new ReferenceExpression("ApplyModeSettings"), modename, action ); }
public static Expression install(ReferenceExpression expression, BlockExpression action) { var installName = new StringLiteralExpression(expression.Name); return new MethodInvocationExpression( new ReferenceExpression("GetInstallerMeta"), installName, action ); }
protected Expression closure_expression() //throws RecognitionException, TokenStreamException { Expression e; IToken anchorBegin = null; IToken anchorEnd = null; e = null; BlockExpression cbe = null; ParameterDeclarationCollection parameters = null; Block body = null; try { // for error handling anchorBegin = LT(1); match(LBRACE); if (0==inputState.guessing) { e = cbe = new BlockExpression(ToLexicalInfo(anchorBegin)); cbe.Annotate("inline"); parameters = cbe.Parameters; body = cbe.Body; } { bool synPredMatched349 = false; if (((tokenSet_102_.member(LA(1))) && (tokenSet_103_.member(LA(2))))) { int _m349 = mark(); synPredMatched349 = true; inputState.guessing++; try { { closure_parameters_test(); } } catch (RecognitionException) { synPredMatched349 = false; } rewind(_m349); inputState.guessing--; } if ( synPredMatched349 ) { { parameter_declaration_list(parameters); match(BITWISE_OR); } } else if ((tokenSet_104_.member(LA(1))) && (tokenSet_105_.member(LA(2)))) { } else { throw new NoViableAltException(LT(1), getFilename()); } } { internal_closure_stmt(body); { // ( ... )* for (;;) { if ((LA(1)==EOL||LA(1)==EOS)) { eos(); { switch ( LA(1) ) { case ESEPARATOR: case CAST: case CHAR: case FALSE: case NOT: case NULL: case RAISE: case RETURN: case SELF: case SUPER: case THEN: case TRUE: case TYPEOF: case YIELD: case TRIPLE_QUOTED_STRING: case LPAREN: case DOUBLE_QUOTED_STRING: case SINGLE_QUOTED_STRING: case ID: case MULTIPLY: case LBRACK: case COMMA: case SPLICE_BEGIN: case DOT: case LBRACE: case QQ_BEGIN: case SUBTRACT: case LONG: case INCREMENT: case DECREMENT: case ONES_COMPLEMENT: case INT: case BACKTICK_QUOTED_STRING: case RE_LITERAL: case DOUBLE: case FLOAT: case TIMESPAN: { internal_closure_stmt(body); break; } case EOL: case EOS: case RBRACE: { break; } default: { throw new NoViableAltException(LT(1), getFilename()); } } } } else { goto _loop354_breakloop; } } _loop354_breakloop: ; } // ( ... )* } anchorEnd = LT(1); match(RBRACE); if (0==inputState.guessing) { body.EndSourceLocation = SourceLocationFactory.ToEndSourceLocation(anchorEnd); } } catch (RecognitionException ex) { if (0 == inputState.guessing) { reportError(ex, "closure_expression"); recover(ex,tokenSet_44_); } else { throw ex; } } return e; }
void ProcessClosureBody(BlockExpression node) { MarkVisited(node); if (node.ContainsAnnotation("inline")) AddOptionalReturnStatement(node.Body); var explicitClosureName = node[BlockExpression.ClosureNameAnnotation] as string; Method closure = CodeBuilder.CreateMethod( ClosureName(explicitClosureName), node.ReturnType ?? CodeBuilder.CreateTypeReference(Unknown.Default), ClosureModifiers()); MarkVisited(closure); var closureEntity = (InternalMethod)closure.Entity; closure.LexicalInfo = node.LexicalInfo; closure.Parameters = node.Parameters; closure.Body = node.Body; CurrentMethod.DeclaringType.Members.Add(closure); CodeBuilder.BindParameterDeclarations(_currentMethod.IsStatic, closure); // check for invalid names and // resolve parameter types Visit(closure.Parameters); // Inside the closure, connect the closure method namespace with the current namespace var ns = new NamespaceDelegator(CurrentNamespace, closureEntity); // Allow closure body to reference itself using its explicit name (BOO-1085) if (explicitClosureName != null) ns.DelegateTo(new AliasedNamespace(explicitClosureName, closureEntity)); ProcessMethodBody(closureEntity, ns); if (closureEntity.ReturnType is Unknown) TryToResolveReturnType(closureEntity); node.ExpressionType = closureEntity.Type; node.Entity = closureEntity; }
private bool ShouldDeferClosureProcessing(BlockExpression node) { // Defer closure processing if it's an argument in a generic method invocation MethodInvocationExpression methodInvocationContext = node.ParentNode as MethodInvocationExpression; if (methodInvocationContext == null) return false; if (!methodInvocationContext.Arguments.Contains(node)) return false; if (methodInvocationContext.Target.Entity is Ambiguous) return ((Ambiguous) methodInvocationContext.Target.Entity).Any(GenericsServices.IsGenericMethod); IMethod target = methodInvocationContext.Target.Entity as IMethod; return (target != null && GenericsServices.IsGenericMethod(target)); }
protected Statement nested_function() //throws RecognitionException, TokenStreamException { Statement stmt; IToken def = null; IToken id = null; stmt = null; BlockExpression be = null; Block body = null; TypeReference rt = null; try { // for error handling def = LT(1); match(DEF); id = LT(1); match(ID); if (0==inputState.guessing) { be = new BlockExpression(ToLexicalInfo(id)); body = be.Body; } { switch ( LA(1) ) { case LPAREN: { match(LPAREN); parameter_declaration_list(be.Parameters); match(RPAREN); { switch ( LA(1) ) { case AS: { match(AS); rt=type_reference(); if (0==inputState.guessing) { be.ReturnType = rt; } break; } case COLON: { break; } default: { throw new NoViableAltException(LT(1), getFilename()); } } } break; } case COLON: { break; } default: { throw new NoViableAltException(LT(1), getFilename()); } } } compound_stmt(body); if (0==inputState.guessing) { string name = id.getText(); stmt = new DeclarationStatement( ToLexicalInfo(def), new Declaration( ToLexicalInfo(id), name), be); be[BlockExpression.ClosureNameAnnotation] = name; } } catch (RecognitionException ex) { if (0 == inputState.guessing) { reportError(ex, "nested_function"); recover(ex,tokenSet_84_); } else { throw ex; } } return stmt; }
private void AddInferredClosureParameterTypes(BlockExpression node, ICallableType callableType) { IParameter[] parameters = (callableType == null ? null : callableType.GetSignature().Parameters); for (int i = 0; i < node.Parameters.Count; i++) { ParameterDeclaration pd = node.Parameters[i]; if (pd.Type != null) continue; IType inferredType; if (parameters != null && i < parameters.Length) { inferredType = parameters[i].Type; } else if (pd.IsParamArray) { inferredType = TypeSystemServices.ObjectArrayType; } else { inferredType = TypeSystemServices.ObjectType; } pd.Type = CodeBuilder.CreateTypeReference(inferredType); } }
protected static AST.Expression multi_instance_split(AST.Expression expr) { AST.BlockExpression condition = new AST.BlockExpression(); condition.Body.Add(new AST.ReturnStatement(expr)); return(new AST.MethodInvocationExpression(new AST.ReferenceExpression("multi_instance_split_expression"), condition, new AST.StringLiteralExpression(expr.ToCodeString()))); }
public static AST.Expression when(AST.Expression expr) { AST.BlockExpression condition = new AST.BlockExpression(); condition.Body.Add(new AST.ReturnStatement(expr)); return(new AST.MethodInvocationExpression(new AST.ReferenceExpression("flow_condition"), condition, new AST.StringLiteralExpression(expr.ToCodeString()))); }
public override void OnBlockExpression(BlockExpression node) { // nothing to do }
protected static AST.Expression task_output_data(AST.Expression expr) { AST.BlockExpression condition = new AST.BlockExpression(); condition.Body.Add(new AST.ReturnStatement(expr)); return(new AST.MethodInvocationExpression(new AST.ReferenceExpression("output_bindings_code"), condition, new AST.StringLiteralExpression(expr.ToCodeString()))); }
public override void OnExtendedGeneratorExpression(ExtendedGeneratorExpression node) { BlockExpression block = new BlockExpression(node.LexicalInfo); Block body = block.Body; Expression e = node.Items[0].Expression; foreach (GeneratorExpression ge in node.Items) { ForStatement fs = new ForStatement(ge.LexicalInfo); fs.Iterator = ge.Iterator; fs.Declarations = ge.Declarations; body.Add(fs); if (null == ge.Filter) { body = fs.Block; } else { fs.Block.Add( NormalizeStatementModifiers.MapStatementModifier(ge.Filter, out body)); } } body.Add(new YieldStatement(e.LexicalInfo, e)); MethodInvocationExpression mie = new MethodInvocationExpression(node.LexicalInfo); mie.Target = block; Node parentNode = node.ParentNode; bool isGenerator = AstUtil.IsListMultiGenerator(parentNode); parentNode.Replace(node, mie); mie.Accept(this); if (isGenerator) { parentNode.ParentNode.Replace( parentNode, CodeBuilder.CreateConstructorInvocation( TypeSystemServices.Map(ProcessGenerators.List_IEnumerableConstructor), mie)); } }
public static AST.Expression default_value(AST.Expression expr) { AST.BlockExpression condition = new AST.BlockExpression(); condition.Body.Add(new AST.ReturnStatement(expr)); return(new AST.MethodInvocationExpression(new AST.ReferenceExpression("variable_default"), condition, new AST.StringLiteralExpression(expr.ToCodeString()))); }
protected static AST.Expression output_binding(AST.Expression vname, AST.Expression expr) { AST.BlockExpression condition = new AST.BlockExpression(); condition.Body.Add(new AST.ReturnStatement(expr)); return(new AST.MethodInvocationExpression(new AST.ReferenceExpression("add_output_binding"), vname, condition, new AST.StringLiteralExpression(expr.ToCodeString()))); }
protected Expression callable_expression() //throws RecognitionException, TokenStreamException { Expression e; IToken doAnchor = null; IToken defAnchor = null; e = null; Block body = null; BlockExpression cbe = null; TypeReference rt = null; IToken anchor = null; try { // for error handling switch ( LA(1) ) { case COLON: { { if (0==inputState.guessing) { body = new Block(); } compound_stmt(body); if (0==inputState.guessing) { e = new BlockExpression(body.LexicalInfo, body); } } break; } case DEF: case DO: { { { switch ( LA(1) ) { case DO: { { doAnchor = LT(1); match(DO); if (0==inputState.guessing) { anchor = doAnchor; } } break; } case DEF: { { defAnchor = LT(1); match(DEF); if (0==inputState.guessing) { anchor = defAnchor; } } break; } default: { throw new NoViableAltException(LT(1), getFilename()); } } } if (0==inputState.guessing) { e = cbe = new BlockExpression(ToLexicalInfo(anchor)); body = cbe.Body; } { switch ( LA(1) ) { case LPAREN: { match(LPAREN); parameter_declaration_list(cbe.Parameters); match(RPAREN); { switch ( LA(1) ) { case AS: { match(AS); rt=type_reference(); if (0==inputState.guessing) { cbe.ReturnType = rt; } break; } case COLON: { break; } default: { throw new NoViableAltException(LT(1), getFilename()); } } } break; } case COLON: { break; } default: { throw new NoViableAltException(LT(1), getFilename()); } } } compound_stmt(body); } break; } default: { throw new NoViableAltException(LT(1), getFilename()); } } } catch (RecognitionException ex) { if (0 == inputState.guessing) { reportError(ex, "callable_expression"); recover(ex,tokenSet_22_); } else { throw ex; } } return e; }
public override void OnBlockExpression(BlockExpression node) { _insideExpressionBlock = true; base.OnBlockExpression(node); _insideExpressionBlock = false; }
public ClosureSignatureInferrer(BlockExpression closure) { _closure = closure; InitializeInputTypes(); }
private bool CanResolveClosure(BlockExpression closure) { if (ResolveClosure == null) return false; // Only resolve closures that depend on generic parameters, where all the dependencies are fixed if (!_closureDependencies.ContainsKey(closure)) return false; foreach (InferredType dependency in _closureDependencies[closure]) { if (!dependency.Fixed) return false; } return true; }
private void InferClosureSignature(BlockExpression node) { ClosureSignatureInferrer inferrer = new ClosureSignatureInferrer(node); ICallableType inferredCallableType = inferrer.InferCallableType(); BindExpressionType(node, inferredCallableType); AddInferredClosureParameterTypes(node, inferredCallableType); }
/// <summary> /// Performs inference on explicitly typed arguments. /// </summary> /// <remarks> /// Corresponds to the first phase in generic parameter inference according to the C# 3.0 spec. /// </remarks> private void InferExplicits() { foreach (TypedArgument typedArgument in Arguments) { _currentClosure = typedArgument.Expression as BlockExpression; Infer( typedArgument.FormalType, typedArgument.Expression.ExpressionType, TypeInference.AllowCovariance); } }
private void ProcessClosureInMethodInvocation(GenericParameterInferrer inferrer, BlockExpression closure, ICallableType formalType) { var sig = formalType.GetSignature(); var replacer = new TypeReplacer(); var collector = new TypeCollector(delegate(IType t) { IGenericParameter gp = t as IGenericParameter; if (gp == null) return false; return gp.DeclaringEntity == inferrer.GenericMethod; }); collector.Visit(formalType); foreach (var typeParameter in collector.Matches) { var inferredType = inferrer.GetInferredType((IGenericParameter)typeParameter); if (inferredType != null) replacer.Replace(typeParameter, inferredType); } for (var i = 0; i < sig.Parameters.Length; i++) { var pd = closure.Parameters[i]; if (pd.Type != null) continue; pd.Type = CodeBuilder.CreateTypeReference(replacer.MapType(sig.Parameters[i].Type)); } ProcessClosureBody(closure); }
private void RecordClosureDependency(BlockExpression closure, IGenericParameter genericParameter) { if (!_closureDependencies.ContainsKey(closure)) { _closureDependencies.Add(closure, new List<InferredType>()); } _closureDependencies[closure].AddUnique(InferredTypes[genericParameter]); }
public override void OnBlockExpression(BlockExpression node) { if (WasVisited(node)) return; if (ShouldDeferClosureProcessing(node)) return; InferClosureSignature(node); ProcessClosureBody(node); }
public static Expression export(BlockExpression exportUrls) { var exportList = new ArrayLiteralExpression(); foreach (var statement in exportUrls.Body.Statements) { var expression = (MethodInvocationExpression)((ExpressionStatement)statement).Expression; var sourceType = expression.Target.ToString(); var remoteUrl = ((StringLiteralExpression)expression.Arguments[0]).Value; MethodInvocationExpression export; if (expression.Arguments.Count == 1) { export = new MethodInvocationExpression(new ReferenceExpression("ExportData"), new StringLiteralExpression(remoteUrl), new StringLiteralExpression(sourceType)); exportList.Items.Add(export); continue; } var to = ((StringLiteralExpression)((MethodInvocationExpression)expression.Arguments[1]).Arguments[0]).Value; export = new MethodInvocationExpression(new ReferenceExpression("ExportData"), new StringLiteralExpression(remoteUrl), new StringLiteralExpression(sourceType), new StringLiteralExpression(to)); exportList.Items.Add(export); } return new MethodInvocationExpression(new ReferenceExpression("ParseExportList"), exportList); }
public static Expression include(BlockExpression includes) { var includeList = new ArrayLiteralExpression(); foreach (var statement in includes.Body.Statements) { var expression = (MethodInvocationExpression)((ExpressionStatement)statement).Expression; var repositoryName = ((ReferenceExpression)expression.Arguments[0]).Name; var includePath = ((StringLiteralExpression)((MethodInvocationExpression)expression.Arguments[1]).Arguments[0]).Value; var exportPath = ((StringLiteralExpression)((MethodInvocationExpression)expression.Arguments[2]).Arguments[0]).Value; ; var repositoryInclude = new MethodInvocationExpression(new ReferenceExpression("RepositoryElement"), new StringLiteralExpression(repositoryName), new StringLiteralExpression(includePath), new StringLiteralExpression(exportPath)); includeList.Items.Add(repositoryInclude); } return new MethodInvocationExpression(new ReferenceExpression("ParseIncludes"), includeList); }
public AsyncBlockExpression(BlockExpression value) : base(value.LexicalInfo) { _block = value; _block.InitializeParent(this); }