⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 contextlist.java

📁 JAVA 数学程序库 提供常规的数值计算程序包
💻 JAVA
字号:
package jmathlib.core.interpreter;


/**Stores a list of contexts. A context is a set of variables pluss some code.
   When a function is called a new context is added to the list. When the function is removed, the context is destroyed. */
public class ContextList
{
    // reference to the currently executing context
    private Context currentContext;
    
    // reference to the global context
    private Context globalContext;
    
    /**Create an empty context to act as the global context
     * This constructor should only be called ONCE, because it also creates the
     * GLOBAL context
     */
    public ContextList()
    {
        globalContext  = new Context(); // global context 
        currentContext = new Context(); // local context
    }

    /**
     * 
     * @param context
     */
    public void pushContext(Context context)
    {
        context.setParent(currentContext);
        currentContext = context; 
    }

    /**Used to create a new context and put it on the stack when executing a function
     * @param _variables = the variable list of the function, null if it's a script file
     */
    public Context createContext(VariableList _variables)
    {
        //check if this is a script file
        if(_variables == null)
        {
            //use the calling functions variable list
            _variables = currentContext.getVariables();
        }
                   
        //create a new context and make it the current context
        //currentContext = new Context(_variables, null, currentContext);
        currentContext = new Context(_variables, currentContext);

        return currentContext;
    }
    
    /**
     * return to the calling context of the function
     */
    public Context popContext()
    {       
        Context context = currentContext;
        currentContext = currentContext.getParent();
        return context; 
    }

    /**
     * @return the variable list of the current context
     */
    public VariableList getVariables()
    {
        return currentContext.getVariables();
    }

    /**
     * @return the variable list of the global context
     */
    public VariableList getGlobalVariables()
    {
        return globalContext.getVariables();
    }

    /**
     * 
     * @param name
     * @return
     */
    public Variable getVariable(String name)
    {
        // check if variable is marked as global in current context
        Variable var = currentContext.getVariables().getVariable(name);
        if ((var!=null) && (var.isGlobal()))
        {
            // variable is marked global
            // return data from global context
            return globalContext.getVariables().getVariable(name);
        }
        else
        {
            // variable is local only
            // return data from current context
            return currentContext.getVariables().getVariable(name);
        }
    }

    /**
     * @return true if the current context is the global one
     */
    public boolean isGlobalContext()
    {
        return (currentContext.getParent() == null);
    }

    /**
     * 
     * @return
     */
    public Context getCurrentContext()
    {
        return currentContext;
    }
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -