/*
* $Id: ArrayNode.java,v 1.6 2002/09/16 08:05:04 jkl Exp $
*
* Copyright (c) 2002 Njet Communications Ltd. All Rights Reserved.
*
* Use is subject to license terms, as defined in
* Anvil Sofware License, Version 1.1. See LICENSE
* file, or http://njet.org/license-1.1.txt
*/
package anvil.script.expression;
import anvil.core.Any;
import anvil.core.Array;
import anvil.codec.Code;
import anvil.codec.ConstantPool;
import anvil.script.compiler.ByteCompiler;
import anvil.script.Context;
import java.io.IOException;
/**
* class ArrayNode
*
* @author: Jani Lehtim�ki
*/
public class ArrayNode extends MultiParent
{
public ArrayNode(Parent elements)
{
super(elements);
}
public int typeOf()
{
return Node.EXPR_ARRAY;
}
public Node optimize()
{
optimizeChilds();
return this;
}
public Any eval()
{
Array array = new Array();
int n = childs();
for(int i=0; i<n; i++) {
Node node = getChild(i);
if (node.typeOf() == Node.EXPR_MAPPING) {
MappingNode mapnode = (MappingNode)node;
array.append(mapnode.getKey(), mapnode.getValue());
} else {
array.append(node.eval());
}
}
return array;
}
public void compile(ByteCompiler context, int operation)
{
Code code = context.getCode();
ConstantPool pool = code.getPool();
int clazz = pool.addClass("anvil/core/Array");
code.anew(clazz);
code.dup();
code.invokespecial(pool.addMethodRef(clazz, "<init>", "()V"));
int append1 = pool.addMethodRef(clazz, "append",
"(Lanvil/core/Any;)Lanvil/core/Array;");
int append2 = pool.addMethodRef(clazz, "append",
"(Lanvil/core/Any;Lanvil/core/Any;)Lanvil/core/Array;");
int n = childs();
for(int i=0; i<n; i++) {
Node node = getChild(i);
if (node.typeOf() == Node.EXPR_MAPPING) {
MappingNode mapnode = (MappingNode)node;
mapnode.getLeft().compile(context, GET);
mapnode.getRight().compile(context, GET);
code.invokevirtual(append2);
} else {
node.compile(context, GET);
code.invokevirtual(append1);
}
}
if (operation == GET_BOOLEAN) {
context.any2boolean();
}
}
}