Пример #1
0
        public ContextVariable FreeVariable(string name)
        {
            ContextVariable var = GetVariable(name);

            if (var == null)
            {
                return(null);
            }

            Variables.Remove(var);
            return(var);
        }
Пример #2
0
        public ContextVariable AllocVariable(string name)
        {
            ContextVariable var = GetVariable(name);

            if (var != null)
            {
                return(var);
            }

            var = new ContextVariable(name);
            Variables.Add(var);
            return(var);
        }
Пример #3
0
        /// <summary>
        /// Evaluate one instruction. Change the state in the (parent) context depending on its current state.
        /// Names are resolved for each instruction. Alternatively, we could first resolve all names in all instructions in a separate pass and then execute without resolution.
        /// </summary>
        public virtual object Execute()
        {
            // Context stores the current state which is read and then written as a result of the operation execution
            ScriptContext ctx = GetContext();

            Schema.Schema top = ctx.Schemas[0];
            string        name, type;

            switch (OpType)
            {
            case ScriptOpType.NOP:
                break;

            case ScriptOpType.CONTEXT:     // Not possible: it is overloaded and processed by a subclass
                break;

            case ScriptOpType.ALLOC:
                ctx.AllocVariable(Name);
                // TODO: process the type of the variable
                break;

            case ScriptOpType.FREE:
                ctx.FreeVariable(Name);
                break;

            case ScriptOpType.VALUE:
                if (Children.Count != 0)     // Expression (not value/literal), e.g., a variable
                {
                    foreach (ScriptOp op in Children)
                    {
                        op.Execute();
                    }
                    Result.Value = GetChild(Children.Count - 1).Result.Value;
                }
                break;

            case ScriptOpType.DOT:
            case ScriptOpType.CALL:
                // Check if it is a variable
                if (OpType == ScriptOpType.CALL && Children.Count == 0)
                {
                    ContextVariable var = ctx.GetVariable(Name);
                    if (var != null)
                    {
                        Result.Value = var.Value;
                        break;
                    }
                }

                // Process all parameters so that they have values that can be used in this run-time call
                foreach (ScriptOp op in Children)
                {
                    op.Execute();
                }

                // Try to find a run-time procedure/method from our API corresponding to this instruction and its parameters (resolution, binding)
                if (Name == "OpenOledb")
                {
                    // Find parameter 'connection' among children
                    ScriptOp child      = GetChild("connection");
                    string   connection = null;
                    if (child != null)
                    {
                        connection = (string)child.Result.Value;
                    }
                    else
                    {
                        break;
                    }                   // ERROR: parameter wrong or not found

                    //SchemaOledb dbTop = new SchemaOledb("");
                    //dbTop.ConnectionString = connection;
                    //dbTop.Open();
                    //dbTop.ImportSchema();
                    //Result.Value = dbTop;
                }
                else if (Name == "Load")
                {
                    ScriptOp      child = GetChild("this");
                    Schema.Schema db    = null;
                    if (child != null)
                    {
                        db = (Schema.Schema)child.Result.Value;
                    }
                    else
                    {
                        break;
                    }                   // ERROR: parameter wrong or not found

                    // Find parameter 'table' among children
                    child = GetChild("table");
                    string table = null;
                    if (child != null)
                    {
                        table = (string)child.Result.Value;
                    }
                    else
                    {
                        break;
                    }                   // ERROR: parameter wrong or not found

                    Table set = null;   // Mapper.ImportSet(db.FindSubset(table), top);
                    Result.Value = set;
                }
                else if (Name == "AddFunction")
                {
                    ScriptOp child = GetChild("this");
                    Table    set   = null;
                    if (child != null)
                    {
                        set = (Table)child.Result.Value;
                    }
                    else
                    {
                        break;
                    }                   // ERROR: parameter wrong or not found

                    // Find parameter 'name' among children
                    child = GetChild("name");
                    name  = null;
                    if (child != null)
                    {
                        name = (string)child.Result.Value;
                    }
                    else
                    {
                        break;
                    }                   // ERROR: parameter wrong or not found

                    // Find parameter 'type' among children
                    child = GetChild("type");
                    type  = null;
                    if (child != null)
                    {
                        type = (string)child.Result.Value;
                    }
                    else
                    {
                        break;
                    }                   // ERROR: parameter wrong or not found

                    // Find parameter 'formula' among children
                    child = GetChild("formula");
                    AstNode formula = null;
                    if (child != null)
                    {
                        formula = (AstNode)child.Result.Value;
                    }
                    else
                    {
                        break;
                    }                   // ERROR: parameter wrong or not found

                    // TOD: Really add a new function with this definition
                    Table  typeSet = (Table)top.GetSubTable(type);
                    Column dim     = (Column)((DcSchema)top).Space.CreateColumn(name, set, typeSet, false);
                    //dim.FormulaAst = formula;

                    Result.Value = dim;
                }

                break;

            case ScriptOpType.WRITE:     // ASSIGN
            {
                ContextVariable var = ctx.GetVariable(Name);
                GetChild(0).Execute();
                var.Value = GetChild(0).Result.Value;
                break;
            }
            }

            return(null); // TODO: Return content of the 'return' variable
        }
Пример #4
0
 public ScriptOp()
 {
     OpType = ScriptOpType.NOP;
     Result = new ContextVariable("return");
 }