Example #1
0
 public abstract Template Visit(ExprCurryLambda expr);
Example #2
0
        public override Template Visit(ExprCurryLambda expr)
        {
            if (expr.Args.Count() <= 1)
            {
                return (new ExprLambda(expr.Block, expr.Args, expr.IsRef, expr.Type)).Accept(this);
            }

            Stack<ExprAlloc> stack = new Stack<ExprAlloc>();
            foreach (var x in expr.Args) stack.Push(x);

            Stack<string> capture_args = new Stack<string>();
            foreach (var x in expr.Args) capture_args.Push(x.Name.First());

            Template template = null;
            foreach (var x in stack)
            {
                Template node = null;
                if (template == null)
                {
                    node = new Template("([<ref><capture>](<arg>)<type> {\n    <block>\n})");
                    if (expr.Type == null)
                    {
                        node.Add("type", "");
                    }
                    else
                    {
                        Template type = new Template(" -> <type>");
                        type.Add("type", expr.Type.Accept(this));
                        node.Add("type", type);
                    }
                    node.Add("block", expr.Block.Accept(this));
                }
                else
                {
                    node = new Template("([<ref><capture>](<arg>) {\n    return <expr>;\n})");
                    node.Add("expr", template);
                }
                node.Add("ref", expr.IsRef ? "&" : "=");
                node.Add("arg", x.Accept(this));

                capture_args.Pop();
                string capture_by_value = "";
                foreach (var name in capture_args)
                {
                    capture_by_value = capture_by_value + ", " + name;
                }
                if (!expr.IsRef) capture_by_value = "";
                node.Add("capture", capture_by_value);
                template = node;
            }
            return template;
        }
Example #3
0
	private Expr lambda_expr()
	{
		EnterRule_lambda_expr();
		EnterRule("lambda_expr", 50);
		TraceIn("lambda_expr", 50);
		Expr value = default(Expr);


		List<ExprAlloc> b = default(List<ExprAlloc>);
		SugarType t = default(SugarType);
		StmtBlock a = default(StmtBlock);

		try { DebugEnterRule(GrammarFileName, "lambda_expr");
		DebugLocation(640, 1);
		try
		{
			// SugarWalker.g:641:2: ( ^( Expr_Lambda '->' (b= func_args )? (t= type_name )? a= stmt_block ) | ^( Expr_Lambda '=>' (b= func_args )? (t= type_name )? a= stmt_block ) | ^( Expr_Lambda '-->' (b= func_args )? (t= type_name )? a= stmt_block ) | ^( Expr_Lambda '==>' (b= func_args )? (t= type_name )? a= stmt_block ) )
			int alt82=4;
			try { DebugEnterDecision(82, false);
			int LA82_0 = input.LA(1);

			if ((LA82_0==Expr_Lambda))
			{
				int LA82_1 = input.LA(2);

				if ((LA82_1==DOWN))
				{
					switch (input.LA(3))
					{
					case 105:
						{
						alt82 = 1;
						}
						break;
					case 124:
						{
						alt82 = 2;
						}
						break;
					case 103:
						{
						alt82 = 3;
						}
						break;
					case 123:
						{
						alt82 = 4;
						}
						break;
					default:
						{
							NoViableAltException nvae = new NoViableAltException("", 82, 2, input);
							DebugRecognitionException(nvae);
							throw nvae;
						}
					}

				}
				else
				{
					NoViableAltException nvae = new NoViableAltException("", 82, 1, input);
					DebugRecognitionException(nvae);
					throw nvae;
				}
			}
			else
			{
				NoViableAltException nvae = new NoViableAltException("", 82, 0, input);
				DebugRecognitionException(nvae);
				throw nvae;
			}
			} finally { DebugExitDecision(82); }
			switch (alt82)
			{
			case 1:
				DebugEnterAlt(1);
				// SugarWalker.g:641:4: ^( Expr_Lambda '->' (b= func_args )? (t= type_name )? a= stmt_block )
				{
				DebugLocation(641, 4);
				DebugLocation(641, 6);
				Match(input,Expr_Lambda,Follow._Expr_Lambda_in_lambda_expr2444); 

				Match(input, TokenTypes.Down, null); 
				DebugLocation(641, 18);
				Match(input,105,Follow._105_in_lambda_expr2446); 
				DebugLocation(641, 23);
				// SugarWalker.g:641:23: (b= func_args )?
				int alt74=2;
				try { DebugEnterSubRule(74);
				try { DebugEnterDecision(74, false);
				int LA74_0 = input.LA(1);

				if ((LA74_0==Func_Args))
				{
					alt74 = 1;
				}
				} finally { DebugExitDecision(74); }
				switch (alt74)
				{
				case 1:
					DebugEnterAlt(1);
					// SugarWalker.g:641:24: b= func_args
					{
					DebugLocation(641, 25);
					PushFollow(Follow._func_args_in_lambda_expr2451);
					b=func_args();
					PopFollow();


					}
					break;

				}
				} finally { DebugExitSubRule(74); }

				DebugLocation(641, 38);
				// SugarWalker.g:641:38: (t= type_name )?
				int alt75=2;
				try { DebugEnterSubRule(75);
				try { DebugEnterDecision(75, false);
				int LA75_0 = input.LA(1);

				if (((LA75_0>=Type_Array && LA75_0<=Type_Ident)||(LA75_0>=Type_Ref && LA75_0<=Type_Template)))
				{
					alt75 = 1;
				}
				} finally { DebugExitDecision(75); }
				switch (alt75)
				{
				case 1:
					DebugEnterAlt(1);
					// SugarWalker.g:641:39: t= type_name
					{
					DebugLocation(641, 40);
					PushFollow(Follow._type_name_in_lambda_expr2458);
					t=type_name();
					PopFollow();


					}
					break;

				}
				} finally { DebugExitSubRule(75); }

				DebugLocation(641, 54);
				PushFollow(Follow._stmt_block_in_lambda_expr2464);
				a=stmt_block();
				PopFollow();


				Match(input, TokenTypes.Up, null); 

				DebugLocation(642, 2);

						value = new ExprLambda(a, b, true, t);
					

				}
				break;
			case 2:
				DebugEnterAlt(2);
				// SugarWalker.g:645:4: ^( Expr_Lambda '=>' (b= func_args )? (t= type_name )? a= stmt_block )
				{
				DebugLocation(645, 4);
				DebugLocation(645, 6);
				Match(input,Expr_Lambda,Follow._Expr_Lambda_in_lambda_expr2474); 

				Match(input, TokenTypes.Down, null); 
				DebugLocation(645, 18);
				Match(input,124,Follow._124_in_lambda_expr2476); 
				DebugLocation(645, 23);
				// SugarWalker.g:645:23: (b= func_args )?
				int alt76=2;
				try { DebugEnterSubRule(76);
				try { DebugEnterDecision(76, false);
				int LA76_0 = input.LA(1);

				if ((LA76_0==Func_Args))
				{
					alt76 = 1;
				}
				} finally { DebugExitDecision(76); }
				switch (alt76)
				{
				case 1:
					DebugEnterAlt(1);
					// SugarWalker.g:645:24: b= func_args
					{
					DebugLocation(645, 25);
					PushFollow(Follow._func_args_in_lambda_expr2481);
					b=func_args();
					PopFollow();


					}
					break;

				}
				} finally { DebugExitSubRule(76); }

				DebugLocation(645, 38);
				// SugarWalker.g:645:38: (t= type_name )?
				int alt77=2;
				try { DebugEnterSubRule(77);
				try { DebugEnterDecision(77, false);
				int LA77_0 = input.LA(1);

				if (((LA77_0>=Type_Array && LA77_0<=Type_Ident)||(LA77_0>=Type_Ref && LA77_0<=Type_Template)))
				{
					alt77 = 1;
				}
				} finally { DebugExitDecision(77); }
				switch (alt77)
				{
				case 1:
					DebugEnterAlt(1);
					// SugarWalker.g:645:39: t= type_name
					{
					DebugLocation(645, 40);
					PushFollow(Follow._type_name_in_lambda_expr2488);
					t=type_name();
					PopFollow();


					}
					break;

				}
				} finally { DebugExitSubRule(77); }

				DebugLocation(645, 54);
				PushFollow(Follow._stmt_block_in_lambda_expr2494);
				a=stmt_block();
				PopFollow();


				Match(input, TokenTypes.Up, null); 

				DebugLocation(646, 2);

						value = new ExprLambda(a, b, false, t);
					

				}
				break;
			case 3:
				DebugEnterAlt(3);
				// SugarWalker.g:649:4: ^( Expr_Lambda '-->' (b= func_args )? (t= type_name )? a= stmt_block )
				{
				DebugLocation(649, 4);
				DebugLocation(649, 6);
				Match(input,Expr_Lambda,Follow._Expr_Lambda_in_lambda_expr2504); 

				Match(input, TokenTypes.Down, null); 
				DebugLocation(649, 18);
				Match(input,103,Follow._103_in_lambda_expr2506); 
				DebugLocation(649, 24);
				// SugarWalker.g:649:24: (b= func_args )?
				int alt78=2;
				try { DebugEnterSubRule(78);
				try { DebugEnterDecision(78, false);
				int LA78_0 = input.LA(1);

				if ((LA78_0==Func_Args))
				{
					alt78 = 1;
				}
				} finally { DebugExitDecision(78); }
				switch (alt78)
				{
				case 1:
					DebugEnterAlt(1);
					// SugarWalker.g:649:25: b= func_args
					{
					DebugLocation(649, 26);
					PushFollow(Follow._func_args_in_lambda_expr2511);
					b=func_args();
					PopFollow();


					}
					break;

				}
				} finally { DebugExitSubRule(78); }

				DebugLocation(649, 39);
				// SugarWalker.g:649:39: (t= type_name )?
				int alt79=2;
				try { DebugEnterSubRule(79);
				try { DebugEnterDecision(79, false);
				int LA79_0 = input.LA(1);

				if (((LA79_0>=Type_Array && LA79_0<=Type_Ident)||(LA79_0>=Type_Ref && LA79_0<=Type_Template)))
				{
					alt79 = 1;
				}
				} finally { DebugExitDecision(79); }
				switch (alt79)
				{
				case 1:
					DebugEnterAlt(1);
					// SugarWalker.g:649:40: t= type_name
					{
					DebugLocation(649, 41);
					PushFollow(Follow._type_name_in_lambda_expr2518);
					t=type_name();
					PopFollow();


					}
					break;

				}
				} finally { DebugExitSubRule(79); }

				DebugLocation(649, 55);
				PushFollow(Follow._stmt_block_in_lambda_expr2524);
				a=stmt_block();
				PopFollow();


				Match(input, TokenTypes.Up, null); 

				DebugLocation(650, 2);

						value = new ExprCurryLambda(a, b, true, t);
					

				}
				break;
			case 4:
				DebugEnterAlt(4);
				// SugarWalker.g:653:4: ^( Expr_Lambda '==>' (b= func_args )? (t= type_name )? a= stmt_block )
				{
				DebugLocation(653, 4);
				DebugLocation(653, 6);
				Match(input,Expr_Lambda,Follow._Expr_Lambda_in_lambda_expr2534); 

				Match(input, TokenTypes.Down, null); 
				DebugLocation(653, 18);
				Match(input,123,Follow._123_in_lambda_expr2536); 
				DebugLocation(653, 24);
				// SugarWalker.g:653:24: (b= func_args )?
				int alt80=2;
				try { DebugEnterSubRule(80);
				try { DebugEnterDecision(80, false);
				int LA80_0 = input.LA(1);

				if ((LA80_0==Func_Args))
				{
					alt80 = 1;
				}
				} finally { DebugExitDecision(80); }
				switch (alt80)
				{
				case 1:
					DebugEnterAlt(1);
					// SugarWalker.g:653:25: b= func_args
					{
					DebugLocation(653, 26);
					PushFollow(Follow._func_args_in_lambda_expr2541);
					b=func_args();
					PopFollow();


					}
					break;

				}
				} finally { DebugExitSubRule(80); }

				DebugLocation(653, 39);
				// SugarWalker.g:653:39: (t= type_name )?
				int alt81=2;
				try { DebugEnterSubRule(81);
				try { DebugEnterDecision(81, false);
				int LA81_0 = input.LA(1);

				if (((LA81_0>=Type_Array && LA81_0<=Type_Ident)||(LA81_0>=Type_Ref && LA81_0<=Type_Template)))
				{
					alt81 = 1;
				}
				} finally { DebugExitDecision(81); }
				switch (alt81)
				{
				case 1:
					DebugEnterAlt(1);
					// SugarWalker.g:653:40: t= type_name
					{
					DebugLocation(653, 41);
					PushFollow(Follow._type_name_in_lambda_expr2548);
					t=type_name();
					PopFollow();


					}
					break;

				}
				} finally { DebugExitSubRule(81); }

				DebugLocation(653, 55);
				PushFollow(Follow._stmt_block_in_lambda_expr2554);
				a=stmt_block();
				PopFollow();


				Match(input, TokenTypes.Up, null); 

				DebugLocation(654, 2);

						value = new ExprCurryLambda(a, b, false, t);
					

				}
				break;

			}
		}
		catch (RecognitionException re)
		{
			ReportError(re);
			Recover(input,re);
		}
		finally
		{
			TraceOut("lambda_expr", 50);
			LeaveRule("lambda_expr", 50);
			LeaveRule_lambda_expr();
		}
		DebugLocation(657, 1);
		} finally { DebugExitRule(GrammarFileName, "lambda_expr"); }
		return value;

	}