public override DLR.Expression GenerateExpressionTree(GenScope scope) { var args = new DLR.Expression[Parameters.Count + 1]; args[0] = Identifier.GenerateExpressionTree(scope); for (var i = 1; i <= Parameters.Count; i++) { args[i] = Parameters.ElementAt(i - 1).GenerateExpressionTree(scope); } if (IsSet) //Last param is the value from the rhs expression { return(DLR.Expression.Dynamic(new RilaSetIndexBinder(new CallInfo(Parameters.Count)), typeof(object), args)); } return(DLR.Expression.Dynamic(new RilaGetIndexBinder(new CallInfo(Parameters.Count)), typeof(object), args)); }
public DLR.Expression <System.Func <Runtime.Aplus, AType> > ParseToLambda(string code) { AplusScope scope = new AplusScope(null, "__top__", this.aplus, DLR.Expression.Parameter(typeof(Aplus), "__aplusRuntime__"), DLR.Expression.Parameter(typeof(DYN.IDynamicMetaObjectProvider), "__module__") ); FunctionInformation funcionInfo = new FunctionInformation(this.aplus.CurrentContext); if (this.aplus.Context != null) { funcionInfo.LoadInfo((this.aplus.Context as Scope).Storage as ScopeStorage); } DLR.Expression codebody = null; AST.Node tree = Compiler.Parse.String(code, this.aplus.LexerMode, funcionInfo); if (tree == null) { codebody = DLR.Expression.Constant(null); } else { codebody = DLR.Expression.Block( new DLR.ParameterExpression[] { scope.ModuleExpression }, DLR.Expression.Assign( scope.ModuleExpression, DLR.Expression.PropertyOrField(scope.RuntimeExpression, "Context") ), tree.Generate(scope) ); } DLR.Expression <System.Func <Aplus, AType> > method = DLR.Expression.Lambda <Func <Aplus, AType> >( codebody, scope.RuntimeExpression ); return(method); }
public DLR.Expression <Func <Rila, dynamic> > ConstructProgram(Rila runtime) { var scope = new GenScopeRoot(runtime); var stmts = new DLR.Expression[Statements.Count]; for (int i = 0; i < Statements.Count; i++) { stmts[i] = Statements.ElementAt(i).GenerateExpressionTree(scope); } //Set the variables defined at the top level var globals = scope.Definitions.Select(x => x.Value); var block = DLR.Expression.Block(globals, stmts); if (block.Type == typeof(void)) //Program should always return a value { block = DLR.Expression.Block(block, DLR.Expression.Default(typeof(object))); } return(DLR.Expression.Lambda <Func <Rila, dynamic> >(block, scope.Root.RuntimeParameter)); }
public override DLR.Expression GenerateExpressionTree(GenScope scope) { DLR.Expression arg = null; if (Expression is IdentifierExpression identifier) { arg = DLR.Expression.Constant(new UnresolvedType(identifier.Name)); } else { arg = Expression.GenerateExpressionTree(scope); // Alias } var getTypeProvider = DLR.Expression.Dynamic( scope.Runtime.GetGetMemberBinder(nameof(Rila.TypeProvider)), typeof(object), scope.Root.RuntimeParameter); return(DLR.Expression.Dynamic( scope.Runtime.GetInvokeMemberBinder(new Tuple <string, CallInfo>(nameof(TypeProvider.GetType), new CallInfo(1))), typeof(object), getTypeProvider, arg)); }
public override DLR.Expression GenerateExpressionTree(GenScope scope) { DLR.Expression result = null; var first = Expressions.First(); if (first is IdentifierExpression ident) { if (scope.TryGetVariable(ident.Name, out ParameterExpression variable)) //expression performed on a variable { result = variable; } else { result = DLR.Expression.Constant(new UnresolvedType(ident.Name)); //static or alias access } } else { result = first.GenerateExpressionTree(scope); } for (var element = 1; element < Expressions.Count; element++) { switch (Expressions.ElementAt(element)) { case IdentifierExpression identifier: { if (IsSetMember && element == Expressions.Count - 2) { result = DLR.Expression.Dynamic( new RilaSetMemberBinder(identifier.Name), typeof(object), result, Expressions.Last().GenerateExpressionTree(scope)); //The last expression is the new value element = Expressions.Count; } else { result = DLR.Expression.Dynamic(scope.Runtime.GetGetMemberBinder(identifier.Name), typeof(object), result); } } break; case CallExpression call: { var name = call.Function as IdentifierExpression; var args = new DLR.Expression[call.Arguments.Count + 1]; args[0] = result; for (var i = 1; i <= call.Arguments.Count; i++) { args[i] = call.Arguments.ElementAt(i - 1).GenerateExpressionTree(scope); } result = DLR.Expression.Dynamic( scope.Runtime.GetInvokeMemberBinder(new Tuple <string, CallInfo>(name.Name, new CallInfo(call.Arguments.Count))), typeof(object), args); } break; case IndexerExpression indexer: result = indexer.GenerateExpressionTree(scope); break; default: throw new ArgumentException("Expecting member access, call or indexer expression!"); } } return(result); }