public FunctionDefinition(string name, Parameter[] parameters, Stmt body) { ContractUtils.RequiresNotNullItems(parameters, "parameters"); if (name == null) { _name = "$lambda-" + Interlocked.Increment(ref _lambdaId); _lambda = true; } else { _name = name; } _parameters = parameters; _body = body; }
public FunctionDefinition(string name, Parameter[] parameters) : this(name, parameters, (Stmt)null) { }
public LambdaExpr(Parameter[] parameters, Stmt body) { _function = new FunctionDefinition(null, parameters, body); }
private static Type GetDelegateType(Parameter[] parameters, bool wrapper, out Delegate originalTarget) { return TotemCallTargets.GetTotemTargetType(wrapper, parameters.Length, out originalTarget); }
public virtual void PostWalk(Parameter node) { }
public override bool Walk(Parameter node) { node.Parent = _binder._currentScope; node.TotemVariable = _binder.DefineParameter(node.Name); return false; }
public override void PostWalk(Parameter node) { }
// Parameter public virtual bool Walk(Parameter node) { return true; }
private Parameter ParseParameterName(HashSet<string> names, ParameterKind kind) { string name = ReadName(); int start = GetStart(), end = GetEnd(); if (name != null) CheckUniqueParameter(names, name); else return null; Parameter parameter = new Parameter(name, kind); parameter.SetLoc(_globalParent, GetStart(), GetEnd()); if (_sink != null) _sink.StartName( new SourceSpan( _tokenizer.IndexToLocation(start), _tokenizer.IndexToLocation(end) ), name ); return parameter; }
// Parameter public override bool Walk(Parameter node) { return false; }
private Parameter ParseParameter(int position, HashSet<string> names) { Parameter ret; Expr def = null; string name = ReadName(); int start = GetStart(), end = GetEnd(); if (MaybeEat(TokenType.Assign)) def = ParseExpr(); ret = new Parameter(name, def); ret.SetLoc(_globalParent, start, GetEnd()); CheckUniqueParameter(names, name); if (_sink != null) _sink.StartName( new SourceSpan( _tokenizer.IndexToLocation(start), _tokenizer.IndexToLocation(end) ), name ); return ret; }
private LambdaExpr ParseLambdaBody(int startPosition, Parameter[] parameters) { Expr exp; LambdaExpr ret; Stmt st; if (PeekToken(TokenType.LeftBrace)) { var inFinally = _inFinally; var inLoop = _inLoop; var return_ = _return; var isGenerator = _isGenerator; var isAsync = _isAsync; _inLoop = false; _inFinally = false; _return = false; _isGenerator = false; _isAsync = false; FunctionDefinition fnDef = new FunctionDefinition(null, parameters); PushFunction(fnDef); st = ParseBlock(); FunctionDefinition fnDef2 = PopFunction(); Debug.Assert(fnDef == fnDef2); fnDef.Body = st; ret = new LambdaExpr(fnDef); _inLoop = inLoop; _inFinally = inFinally; _return = return_; _isGenerator = isGenerator; _isAsync = isAsync; } else { exp = ParseExpr(); st = new ReturnStmt(exp); st.SetLoc(_globalParent, exp.IndexSpan); ret = new LambdaExpr(parameters, st); } ret.SetLoc(_globalParent, startPosition, GetEnd()); return ret; }
private void ParseParameterList(IList<Parameter> parameters) { while (MaybeEat(TokenType.Comma)) { // TODO: Add type if (!PeekToken(TokenType.Name)) throw new SyntaxErrorException(); NameToken nt = (NameToken)NextToken(); int start = GetStart(); Expr defaultVal = MaybeEat(TokenType.Assign) ? ParseExpr() : null; var param = new Parameter(nt.Name, defaultVal); param.SetLoc(_globalParent, start, GetEnd()); parameters.Add(param); } }
private Expr ParsePrimaryExpr() { int start, end, globalStart; Expr ret; // lambda handling List<Parameter> parameters = new List<Parameter>(); Parameter param; bool startBody = false; switch (PeekToken().Kind) { case TokenType.Name: NameToken nameToken = (NameToken)NextToken(); globalStart = GetStart(); if (_sink != null) _sink.StartName(GetSourceSpan(), nameToken.Name); ret = new IdentifierExpr(nameToken.Name); ret.SetLoc(_globalParent, GetStart(), GetEnd()); if (MaybeEat(TokenType.Arrow)) { param = new Parameter(nameToken.Name); param.SetLoc(_globalParent, ret.IndexSpan); parameters.Add(param); startBody = true; goto lambda; } return ret; case TokenType.Constant: ConstantValueToken constantToken = (ConstantValueToken)NextToken(); start = GetStart(); object cv = constantToken.Value; if (cv == null) { ret = new ConstantExpr(null); } else if (cv is bool) { ret = new ConstantExpr((bool)cv); } else if (cv is string) { ret = new ConstantExpr((string)cv); } else if (cv is int) { ret = new ConstantExpr((int)cv); } else if (cv is Uninitialized) { ret = new ConstantExpr(Uninitialized.Instance); } else if (cv is double) { ret = new ConstantExpr((double)cv); } else if (cv is BigInteger) { ret = new ConstantExpr((BigInteger)cv); } else { throw Assert.Unreachable; } ret.SetLoc(_globalParent, GetStart(), GetEnd()); return ret; case TokenType.LeftParenthesis: NextToken(); start = GetStart(); globalStart = GetStart(); if (PeekToken().Kind == TokenType.Name) // may be lambda: (a = 2) => a; { NameToken nt = (NameToken)NextToken(); if (PeekToken().Kind == TokenType.RightParenthesis) { end = GetEnd(); start = GetStart(); NextToken(); if (MaybeEat(TokenType.Arrow)) { param = new Parameter(nt.Name); param.SetLoc(_globalParent, GetStart(), end); parameters.Add(param); startBody = true; goto lambda; } else { ret = new IdentifierExpr(nt.Name); ret.SetLoc(_globalParent, start, end); ret = new ParenthesisExpr(ret); ret.SetLoc(_globalParent, globalStart, GetEnd()); return ret; } } else if (PeekToken().Kind == TokenType.Assign) { end = GetEnd(); start = GetStart(); NextToken(); var left = new IdentifierExpr(nt.Name); left.SetLoc(_globalParent, start, end); var right = ParseExpr(); if (PeekToken().Kind == TokenType.RightParenthesis) { end = GetEnd(); NextToken(); if (MaybeEat(TokenType.Arrow)) { param = new Parameter(nt.Name, right); param.SetLoc(_globalParent, left.StartIndex, right.EndIndex); parameters.Add(param); startBody = true; goto lambda; } else { ret = new AssignExpr(TokenType.Assign, left, right); ret.SetLoc(_globalParent, start, end); ret = new ParenthesisExpr(ret); ret.SetLoc(_globalParent, globalStart, GetEnd()); return ret; } } else if (PeekToken().Kind == TokenType.Comma) { param = new Parameter(nt.Name, right); param.SetLoc(_globalParent, start, GetEnd()); parameters.Add(param); goto lambda; } else { throw Assert.Unreachable; } } else if (PeekToken().Kind == TokenType.Comma) { param = new Parameter(nt.Name); param.SetLoc(_globalParent, GetStart(), GetEnd()); parameters.Add(param); goto lambda; } else { throw Assert.Unreachable; } } else if (PeekToken(TokenType.RightParenthesis)) { goto lambda; } else // not NameToken nor RightParen { ret = new ParenthesisExpr(ParseExpr()); Eat(TokenType.RightParenthesis); ret.SetLoc(_globalParent, globalStart, GetEnd()); return ret; } lambda: // parse the rest of the lambda if (!startBody) { while (MaybeEat(TokenType.Comma)) { if (PeekToken(TokenType.Name)) throw new SyntaxErrorException(); NameToken nt = (NameToken)NextToken(); start = GetStart(); Expr defaultVal = MaybeEat(TokenType.Assign) ? ParseExpr() : null; param = new Parameter(nt.Name, defaultVal); param.SetLoc(_globalParent, start, GetEnd()); parameters.Add(param); } Eat(TokenType.RightParenthesis); Eat(TokenType.Arrow); } return ParseLambdaBody(globalStart, parameters.ToArray()); case TokenType.LeftBracket: return ParseArrayLiteral(); default: return null; // syntax error } }
// Parameter public override bool Walk(Parameter node) { node.Parent = _currentScope; return base.Walk(node); }