01: /*
02: * $Id: ArrayNode.java,v 1.6 2002/09/16 08:05:04 jkl Exp $
03: *
04: * Copyright (c) 2002 Njet Communications Ltd. All Rights Reserved.
05: *
06: * Use is subject to license terms, as defined in
07: * Anvil Sofware License, Version 1.1. See LICENSE
08: * file, or http://njet.org/license-1.1.txt
09: */
10: package anvil.script.expression;
11:
12: import anvil.core.Any;
13: import anvil.core.Array;
14: import anvil.codec.Code;
15: import anvil.codec.ConstantPool;
16: import anvil.script.compiler.ByteCompiler;
17: import anvil.script.Context;
18: import java.io.IOException;
19:
20: /**
21: * class ArrayNode
22: *
23: * @author: Jani Lehtimäki
24: */
25: public class ArrayNode extends MultiParent {
26:
27: public ArrayNode(Parent elements) {
28: super (elements);
29: }
30:
31: public int typeOf() {
32: return Node.EXPR_ARRAY;
33: }
34:
35: public Node optimize() {
36: optimizeChilds();
37: return this ;
38: }
39:
40: public Any eval() {
41: Array array = new Array();
42: int n = childs();
43: for (int i = 0; i < n; i++) {
44: Node node = getChild(i);
45: if (node.typeOf() == Node.EXPR_MAPPING) {
46: MappingNode mapnode = (MappingNode) node;
47: array.append(mapnode.getKey(), mapnode.getValue());
48: } else {
49: array.append(node.eval());
50: }
51: }
52: return array;
53: }
54:
55: public void compile(ByteCompiler context, int operation) {
56: Code code = context.getCode();
57: ConstantPool pool = code.getPool();
58: int clazz = pool.addClass("anvil/core/Array");
59: code.anew(clazz);
60: code.dup();
61: code.invokespecial(pool.addMethodRef(clazz, "<init>", "()V"));
62: int append1 = pool.addMethodRef(clazz, "append",
63: "(Lanvil/core/Any;)Lanvil/core/Array;");
64: int append2 = pool.addMethodRef(clazz, "append",
65: "(Lanvil/core/Any;Lanvil/core/Any;)Lanvil/core/Array;");
66: int n = childs();
67: for (int i = 0; i < n; i++) {
68: Node node = getChild(i);
69: if (node.typeOf() == Node.EXPR_MAPPING) {
70: MappingNode mapnode = (MappingNode) node;
71: mapnode.getLeft().compile(context, GET);
72: mapnode.getRight().compile(context, GET);
73: code.invokevirtual(append2);
74: } else {
75: node.compile(context, GET);
76: code.invokevirtual(append1);
77: }
78: }
79: if (operation == GET_BOOLEAN) {
80: context.any2boolean();
81: }
82: }
83:
84: }
|