Package org.onemind.commons.java.datastructure

Examples of org.onemind.commons.java.datastructure.NametableStack


    public JxpProcessingContext(Writer writer, Map env)
    {
        _writer = writer;
        env.put(KEY_WRITER, _writer);
        env.put(KEY_CONTEXT, this);
        _nametableStack = new NametableStack(env);
    };
View Full Code Here


     * @param value the value
     * @return the value assigned
     */
    protected Object assignVariable(String name, Object value)
    {
        NametableStack ntStack = getCurrentContext().getNametableStack();
        ntStack.assign(name, value);
        return value;
    }
View Full Code Here

            JxpProcessingContext context = getCurrentContext();
            String functionName = methodName;
            if (context.getUserDefinedFunctions().containsKey(functionName))
            {
                JxpUserDefinedFunction function = (JxpUserDefinedFunction) context.getUserDefinedFunctions().get(functionName);
                NametableStack nt = context.getNametableStack();
                int scope = nt.newLocalScope();
                declareFunctionLocals(functionName, function.getParameterSpec(), args);
                try
                {
                    Object obj = function.getBlock().jjtAccept(this, null);
                    if (obj == Control.EXIT)
                    {
                        return obj;
                    } else if (obj instanceof Control) //return value
                    {
                        return ((Control) obj).getObject();
                    } else
                    {
                        return obj;
                    }
                } finally
                {
                    nt.closeLocalScope(scope);
                }
            } else
            {
                //try static import
                List staticImports = context.getStaticImports();
View Full Code Here

     * @param params the parameters
     * @param args the arguments
     */
    public void declareFunctionLocals(String functionName, SimpleNode params, Object[] args) throws Exception
    {
        NametableStack ntStack = getCurrentContext().getNametableStack();
        int n = params.jjtGetNumChildren();
        Map defaults = new HashMap();
        for (int i = 0; i < args.length; i++)
        {
            if (args[i] instanceof AstNamedArgument)
            {
                SimpleNode arg = (SimpleNode) args[i];
                Object value = arg.jjtGetChild(0).jjtAccept(this, null);
                defaults.put(arg.getData(), value);
            }
        }
        if (params instanceof AstFormalParameters)
        {
            for (int i = 0; i < n; i++)
            {
                AstFormalParameter param = (AstFormalParameter) params.jjtGetChild(i);
                AstVariableDeclaratorId id = (AstVariableDeclaratorId) param.jjtGetChild(1);
                AstType type = (AstType) param.jjtGetChild(0);
                Class paramType = (Class) type.jjtAccept(this, null);
               
                if (param.jjtGetNumChildren() == 2)
                {//no defaults
                    if (args.length <= i)
                    {
                        throw new IllegalArgumentException("Mismatch number of arguments");
                    } else if (args[i] instanceof AstNamedArgument)
                    {
                        throw new IllegalArgumentException("Expecting required paramater "
                                + id.getData() + " for function " + functionName);
                    } else
                    {
                        if (_checkFunctionArgumentType)
                        {//do type checking                  
                            if (paramType.isPrimitive())
                            {
                                if (args[i] == null)
                                {
                                    throw new IllegalArgumentException("Primitive argument " + id.getData() + " cannot be null");
                                } else if (!ReflectUtils.isPrimitiveInstance(paramType, args[i]))
                                {
                                    throw new IllegalArgumentException("Mismatch argument type for " + id.getData() + ". Expecting "
                                                + paramType.getName());
                                }
                            } else if (args[i] != null)
                            {
                                if (!paramType.isInstance(args[i]))
                                {
                                    throw new IllegalArgumentException("Mismatch argument type for " + id.getData()
                                            + ". Expecting " + paramType.getName());
                                }
                            }
                           
                        }
                        ntStack.declare((String) id.getData(), args[i]);
                    }
                } else
                {//has default                   
                    if (defaults.containsKey(id.getData()))
                    {
                        Object value = defaults.remove(id.getData());
                        ntStack.declare((String) id.getData(), value);
                    } else
                    {
                        if (i >= args.length || args[i] instanceof AstNamedArgument)
                        {
                            Object value = param.jjtGetChild(2).jjtAccept(this, null);
                            ntStack.declare((String) id.getData(), value);
                        } else
                        {
                            ntStack.declare((String) id.getData(), args[i]);
                        }
                    }
                }
            }
            if (defaults.size() > 0)
            {
                throw new IllegalArgumentException("Unknown default " + defaults + " for function " + functionName);
            }
        } else
        {//assume variable parameters (varargs)
            ntStack.declare((String) params.getData(), args);
        }
    }
View Full Code Here

     * @return the value of the variable
     * @throws NoSuchFieldException the the variable cannot be found
     */
    protected Object lookupVariable(String variableName) throws NoSuchFieldException
    {
        NametableStack ntStack = getCurrentContext().getNametableStack();
        if (!ntStack.containsName(variableName))
        {
            throw new NoSuchFieldException("Variable/Function " + variableName + " is not declared before");
        }
        Object v = ntStack.access(variableName);
        if (_logger.isLoggable(Level.FINEST))
        {
            _logger.finest("Looking up variable " + variableName + " found " + v);
        }
        return v;
View Full Code Here

     */
    protected Object resolveName(List l) throws Exception
    {
        Object current = null;
        JxpProcessingContext context = getCurrentContext();
        NametableStack ntStack = context.getNametableStack();
        for (int i = 0; i < l.size(); i++)
        {
            String s = (String) l.get(i);
            if (i == 0)
            {
                //first case, variable
                if (ntStack.containsName(s))
                {
                    current = ntStack.access(s); //lookup variable
                } else
                {//second case, page static
                    JxpPage page = context.getCurrentPage();
                    if (page.hasStaticVariable(s))
                    {
View Full Code Here

            throw generateProcessingException(new IllegalArgumentException("Cannot resolve class " + className), node);
        }
        StaticImport si = StaticImportUtils.getStaticImport(c);
        context.addStaticImport(si);
        Iterator it = si.getStaticFields().entrySet().iterator();
        NametableStack ntStack = context.getNametableStack();
        while (it.hasNext())
        {
            Map.Entry entry = (Map.Entry) it.next();
            ntStack.declare((String) entry.getKey(), entry.getValue());
        }
        return null;
    }
View Full Code Here

TOP

Related Classes of org.onemind.commons.java.datastructure.NametableStack

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.