Esempio n. 1
0
 /// <summary>
 /// Initializes a new instance of <see cref="Dependency"/> AST node.
 /// </summary>
 /// <param name="variable">The name of the dependency definition.</param>
 /// <param name="functionBody">The body of the dependency definition.</param>
 /// <param name="codeText">The string representation of the dependency defintion.</param>
 /// <param name="variables">The variables associated with the dependency definition.</param>
 public Dependency(Identifier variable, Node functionBody, string codeText, Variables variables)
 {
     this.variable = variable;
     this.functionBody = functionBody;
     this.codeText = codeText;
     this.variables = variables;
 }
 /// <summary>
 /// Initializes a new instance of <see cref="UserDefFunction"/> AST node.
 /// </summary>
 /// <param name="name">The name of the user defined function.</param>
 /// <param name="parameters">The parameters for the user defined function.</param>
 /// <param name="codeblock">The codeblock for the user defined function.</param>
 /// <param name="code">The string representation of the user defined function.</param>
 public UserDefFunction(Identifier name, ExpressionList parameters, Node codeblock, string code)
 {
     this.name = name;
     this.parameters = parameters;
     this.codeblock = codeblock;
     this.code = code;
 }
        /// <summary>
        /// Builds a <b>dyadic</b> user defined operator with a <b>dyadic</b> derived function.
        /// </summary>
        /// <remarks>
        /// Builds a representation of a <see cref="UserDefOperator"/> of the following structure:
        /// <example>
        /// a (f OP h) b: { ... }
        /// </example>
        /// </remarks>
        /// <param name="name">The name of the user defined operator.</param>
        /// <param name="function">The name of the function in the user defined operator.</param>
        /// <param name="condition">The name of the condition in the user defined operator.</param>
        /// <param name="leftArgument">The name of left argument of the user defined operator.</param>
        /// <param name="rightArgument">The name of the right argument of the user defined operator.</param>
        /// <param name="codeblock">The codeblock of the user defined operator.</param>
        /// <param name="codeText">The string representation of the user defined operator.</param>
        /// <returns>Returns a dyadic case of <see cref="UserDefOperator"/> AST node.</returns>
        public static UserDefOperator DyadicUserDefOperator(
            Identifier name,
            Identifier function, Identifier condition,
            Identifier leftArgument, Identifier rightArgument,
            Node codeblock, string codeText = "")
        {
            UserDefOperator result = DyadicUserDefOperator(name, function, condition, rightArgument, codeblock, codeText);
            result.LeftArgument = leftArgument;

            return result;
        }
        /// <summary>
        /// Builds a <b>dyadic</b> user defined operator with a <b>monadic</b> derived function.
        /// </summary>
        /// <remarks>
        /// Builds a representation of a <see cref="UserDefOperator"/> of the following structure:
        /// <example>
        /// (f OP h) b: { ... }
        /// </example>
        /// </remarks>
        /// <param name="name">The name of the user defined operator.</param>
        /// <param name="function">The name of the function in the user defined operator.</param>
        /// <param name="condition">The name of the condition in the user defined operator.</param>
        /// <param name="argument">The name of the monadic argument of the user defined operator.</param>
        /// <param name="codeblock">The codeblock of the user defined operator.</param>
        /// <param name="codeText">The string representation of the user defined operator.</param>
        /// <returns>Returns a dyadic case of <see cref="UserDefOperator"/> AST node.</returns>
        public static UserDefOperator DyadicUserDefOperator(
            Identifier name,
            Identifier function, Identifier condition,
            Identifier argument,
            Node codeblock, string codeText = "")
        {
            UserDefOperator result = MonadicUserDefOperator(name, function, argument, codeblock, codeText);
            result.Condition = condition;

            return result;
        }
        /// <summary>
        /// Builds a <b>monadic</b> user defined operator with a <b>monadic</b> derived function.
        /// </summary>
        /// <remarks>
        /// Builds a representation of a <see cref="UserDefOperator"/> of the following structure:
        /// <example>
        /// (f OP) b: { ... }
        /// </example>
        /// </remarks>
        /// <param name="name">The name of the user defined operator.</param>
        /// <param name="function">The name of the function in the user defined operator.</param>
        /// <param name="argument">The name of the monadic argument of the user defined operator.</param>
        /// <param name="codeblock">The codeblock of the user defined operator.</param>
        /// <param name="codeText">The string representation of the user defined operator.</param>
        /// <returns>Returns a monadic case of <see cref="UserDefOperator"/> AST node.</returns>
        public static UserDefOperator MonadicUserDefOperator(
            Identifier name,
            Identifier function,
            Identifier argument,
            Node codeblock, string codeText = "")
        {

            UserDefOperator result = new UserDefOperator(name, function, codeblock, codeText)
            {
                RightArgument = argument
            };

            return result;
        }
        /// <summary>
        /// Adds the passed parameter's name to the scope, and the parameterexpression to the expression linkedlist.
        /// </summary>
        /// <param name="methodScope">The scope.</param>
        /// <param name="methodParameters">The LinkedList to add.</param>
        /// <param name="parameter">The parameter to add.</param>
        private static void BuildParameterExpression(
            AplusScope methodScope, LinkedList<DLR.ParameterExpression> methodParameters, Identifier parameter)
        {
            if (parameter != null)
            {
                string name = parameter.Name;
                DLR.ParameterExpression expression = DLR.Expression.Parameter(typeof(AType), name);
                methodScope.Variables[name] = expression;

                methodParameters.AddFirst(expression);
            }
        }
 /// <summary>
 /// Initializes a new instance of <see cref="UserDefOperator"/> AST node.
 /// </summary>
 /// <param name="name">The name of the user defined operator.</param>
 /// <param name="function">The function parameter of the user defined operator.</param>
 /// <param name="codeblock">The code block of the user defined operator.</param>
 /// <param name="codeText">The string representation of the user defined operator.</param>
 public UserDefOperator(Identifier name, Identifier function, Node codeblock, string codeText)
 {
     this.name = name;
     this.function = function;
     this.codeblock = codeblock;
     this.codeText = codeText;
 }
 public UserDefOperatorInvoke(Identifier name)
     : base(null)
 {
     this.name = name;
 }
Esempio n. 9
0
 /// <summary>
 /// Builds a <see cref="Dependency"/> node.
 /// </summary>
 /// <param name="variable">The name of the dependency definition.</param>
 /// <param name="functionBody">The body of the dependency definition.</param>
 /// <param name="codeText">The string representation of the dependency defintion.</param>
 /// <param name="variables">The variables associated with the dependency definition.</param>
 /// <returns>Returns a <see cref="Dependency"/> node.</returns>
 public static Dependency Dependency(Identifier variable, Node functionBody, string codeText, Variables variables)
 {
     return(new Dependency(variable, functionBody, codeText, variables));
 }
 public static UserDefOperatorInvoke UserDefOperatorInvoke(Identifier name)
 {
     return new UserDefOperatorInvoke(name);
 }
Esempio n. 11
0
 private static string ToDot(string parent, Identifier node)
 {
     string name = String.Format("ID{0}", counter++);
     text.AppendFormat("  {0} [label=\"{1}\"];\n", name, node.Name);
     return name;
 }
Esempio n. 12
0
 /// <summary>
 /// Initializes a new instance of <see cref="UserDefInvoke"/> AST node.
 /// </summary>
 /// <param name="method">The identifier of the user defined function to invoke.</param>
 /// <param name="arguments">The arguments for the user defined function.</param>
 public UserDefInvoke(Identifier method, ExpressionList arguments)
 {
     this.method = method;
     this.arguments = arguments;
 }
Esempio n. 13
0
 /// <summary>
 /// Initializes a new instance of <see cref="UserDefInvoke"/> AST node.
 /// </summary>
 /// <param name="method">The identifier of the user defined function to invoke.</param>
 /// <param name="arguments">The arguments for the user defined function.</param>
 public UserDefInvoke(Identifier method, ExpressionList arguments)
 {
     this.method    = method;
     this.arguments = arguments;
 }
Esempio n. 14
0
 /// <summary>
 /// Builds a <see cref="Dependency"/> node.
 /// </summary>
 /// <param name="variable">The name of the dependency definition.</param>
 /// <param name="functionBody">The body of the dependency definition.</param>
 /// <param name="codeText">The string representation of the dependency defintion.</param>
 /// <param name="variables">The variables associated with the dependency definition.</param>
 /// <returns>Returns a <see cref="Dependency"/> node.</returns>
 public static Dependency Dependency(Identifier variable, Node functionBody, string codeText, Variables variables)
 {
     return new Dependency(variable, functionBody, codeText, variables);
 }
Esempio n. 15
0
        internal static DLR.Expression GenerateIdentifierAssign(
            AplusScope scope, Identifier target, DLR.Expression value, bool isStrand = false, bool needCallback = true)
        {
            Aplus runtime = scope.GetRuntime();
            DLR.Expression result = null;

            if (scope.IsMethod)
            {
                string qualifiedName = target.BuildQualifiedName(runtime.CurrentContext);

                // If the generation is inside of a method
                // check if it has the variablename as function parameter
                DLR.Expression functionParameter = scope.FindIdentifier(target.Name);
                if (functionParameter != null)
                {
                    // Found variable as a function parameter, do a simple assign
                    // the variable is already defined in AST.UserDefFunction
                    return DLR.Expression.Assign(functionParameter, value);
                }

                DLR.Expression functionScopeParam = scope.GetModuleExpression();
                DLR.Expression globalScopeParam = scope.Parent.GetModuleExpression();

                switch (target.Type)
                {
                    case IdentifierType.UnQualifiedName:
                        // Need to check if we are inside an eval block:
                        if (scope.IsEval)
                        {
                            // Code is inside an eval block, we behave slightly differently!

                            #region description about what are we doing in this case

                            //
                            // Check if the variable exists in the function's scope
                            //  |-> Exists: set that variable's value
                            //  |-> otherwise: set the global variable's value
                            //
                            // if(((IDictionary<String, Object>)($FunctionScope).ContainsKey($VARIABLE))
                            // {
                            //      $FunctionScope.$VARIABLE = $VALUE;
                            // }
                            // else
                            // {
                            //      $GlobalScope.$VARIABLE = $VALUE;
                            // }
                            //
                            #endregion

                            result = DLR.Expression.Condition(
                                // Check if the varaible exists in the function scope
                                DLR.Expression.Call(
                                    DLR.Expression.Convert(functionScopeParam, typeof(IDictionary<string, object>)),
                                    typeof(IDictionary<string, object>).GetMethod("ContainsKey"),
                                    DLR.Expression.Constant(target.Name)
                                ),
                                // found the variable in function scope, so assign a value to it
                                DLR.Expression.Block(
                                    needCallback
                                    ? DLR.Expression.Assign(scope.CallbackInfo.QualifiedName, DLR.Expression.Constant(qualifiedName))
                                    : (DLR.Expression)DLR.Expression.Empty(),
                                    DLR.Expression.Dynamic(
                                        runtime.SetMemberBinder(target.Name),
                                        typeof(object),
                                        functionScopeParam,
                                        value
                                    ),
                                    value
                                ),
                                // did NOT found the variable in the function scope
                                // perform the assignment in the global scope
                                BuildGlobalAssignment(scope, runtime, globalScopeParam, target, value, isStrand, needCallback)
                            );

                        }
                        else if (target.IsEnclosed)
                        {
                            result = BuildGlobalAssignment(scope, runtime, globalScopeParam, target, value, isStrand, needCallback);
                        }
                        else
                        {
                            // Simple case, we are inside a user defined function, but not inside an eval block
                            result =
                                DLR.Expression.Block(
                                    needCallback
                                    ? DLR.Expression.Assign(scope.CallbackInfo.QualifiedName, DLR.Expression.Constant(qualifiedName))
                                    : (DLR.Expression)DLR.Expression.Empty(),
                                    DLR.Expression.Dynamic(
                                    runtime.SetMemberBinder(target.Name),
                                    typeof(object),
                                    functionScopeParam,
                                    value
                                    )
                                );
                        }
                        break;

                    case IdentifierType.QualifiedName:
                    case IdentifierType.SystemName:
                    default:
                        // Do an assignment on the global scope
                        result = BuildGlobalAssignment(scope, runtime, globalScopeParam, target, value, isStrand, needCallback);
                        break;
                }
            }
            else
            {
                result = BuildGlobalAssignment(scope, runtime, scope.GetModuleExpression(), target, value, isStrand, needCallback);
            }


            return DLR.Expression.Convert(result, typeof(AType));
        }
Esempio n. 16
0
        private static DLR.Expression BuildGlobalAssignment(
            AplusScope scope,
            Aplus runtime,
            DLR.Expression variableContainer,
            Identifier target,
            DLR.Expression value,
            bool isStrand = false,
            bool needCallback = true
            )
        {
            // Build the ET for updating the dependency
            DLR.Expression dependencyCall;
            string qualifiedName = target.BuildQualifiedName(runtime.CurrentContext);

            if (isStrand)
            {
                // We are inside a starnd assignment, do nothing.
                // Dependency update will be handled later.
                dependencyCall = DLR.Expression.Empty();
            }
            else
            {
                // Otherwise build the dependency invalidation call.
                DLR.Expression dependencyManager = DLR.Expression.Property(scope.GetRuntimeExpression(), "DependencyManager");
                dependencyCall = DLR.Expression.Call(
                    dependencyManager,
                    typeof(DependencyManager).GetMethod("InvalidateDependencies", new Type[] { typeof(string) }),
                    DLR.Expression.Constant(qualifiedName)
                );
            }

            DLR.ParameterExpression errorParam = DLR.Expression.Parameter(typeof(Error.Signal), "__ERRORPARAM__");
            DLR.ParameterExpression valueParam = DLR.Expression.Parameter(typeof(AType), "__VALUEPARAMETER__");
            scope.AssignDone = (scope.AssignDone == null) ? DLR.Expression.Parameter(typeof(bool), "__ASSIGNDONE__") : scope.AssignDone;

            DLR.Expression presetCallback = BuildPresetCallbackCall(scope, valueParam);
            DLR.Expression callback = BuildCallbackCall(scope, valueParam);

            DLR.Expression variableSet =
                DLR.Expression.Block(
                        VariableHelper.SetVariable(
                            runtime,
                            variableContainer,
                            target.CreateContextNames(runtime.CurrentContext),
                            valueParam
                        ),
                        dependencyCall
                    );

            DLR.Expression codebody;

            if (needCallback)
            {
                codebody =
                    DLR.Expression.Block(
                        typeof(void),
                        DLR.Expression.Assign(scope.CallbackInfo.QualifiedName, DLR.Expression.Constant(qualifiedName)),
                        DLR.Expression.Assign(scope.AssignDone, DLR.Expression.Constant(false)),
                        DLR.Expression.TryCatch(
                            DLR.Expression.Block(
                                typeof(void),
                                DLR.Expression.Assign(valueParam, presetCallback),
                                DLR.Expression.Assign(scope.AssignDone, DLR.Expression.Constant(true))
                            ),
                            DLR.Expression.Catch(
                                errorParam,
                                DLR.Expression.Empty()
                            )
                        ),
                        DLR.Expression.IfThen(
                            scope.AssignDone,
                            DLR.Expression.Block(
                                variableSet,
                                callback
                            )
                        )
                    );
            }
            else
            {
                codebody = variableSet;
            }

            // value assignment
            DLR.Expression result = DLR.Expression.Block(
                new DLR.ParameterExpression[] { valueParam },
                DLR.Expression.Assign(valueParam, value),
                codebody,
                valueParam
            );

            return result;
        }
 public static UserDefOperatorInvoke UserDefOperatorInvoke(Identifier name, Node condition)
 {
     UserDefOperatorInvoke userOp = new UserDefOperatorInvoke(name);
     userOp.Condition = condition;
     return userOp;
 }