001: /*
002: * $Id: Parent.java,v 1.8 2002/09/16 08:05:05 jkl Exp $
003: *
004: * Copyright (c) 2002 Njet Communications Ltd. All Rights Reserved.
005: *
006: * Use is subject to license terms, as defined in
007: * Anvil Sofware License, Version 1.1. See LICENSE
008: * file, or http://njet.org/license-1.1.txt
009: */
010: package anvil.script.expression;
011:
012: import anvil.core.Any;
013: import anvil.script.compiler.ByteCompiler;
014: import anvil.ErrorListener;
015: import anvil.script.Context;
016:
017: /**
018: * class Parent
019: *
020: * @author: Jani Lehtimäki
021: */
022: public abstract class Parent extends Node {
023:
024: public Parent() {
025: super ();
026: }
027:
028: public boolean isConstant() {
029: int n = childs();
030: for (int i = 0; i < n; i++) {
031: if (!getChild(i).isConstant()) {
032: return false;
033: }
034: }
035: return true;
036: }
037:
038: public boolean hasSplices() {
039: int n = childs();
040: for (int i = 0; i < n; i++) {
041: if (getChild(i).typeOf() == EXPR_SPLICE) {
042: return true;
043: }
044: }
045: return false;
046: }
047:
048: public boolean hasNamedParameters() {
049: int n = childs();
050: for (int i = 0; i < n; i++) {
051: if (getChild(i).typeOf() == EXPR_NAMED) {
052: return true;
053: }
054: }
055: return false;
056: }
057:
058: public NamedNode findNamed(String name) {
059: int n = childs();
060: for (int i = 0; i < n; i++) {
061: Node child = getChild(i);
062: if (child.typeOf() == EXPR_NAMED) {
063: NamedNode named = (NamedNode) child;
064: if (named.getName().equals(name)) {
065: return named;
066: }
067: }
068: }
069: return null;
070: }
071:
072: public int findNamedIndex(String name) {
073: int n = childs();
074: for (int i = 0; i < n; i++) {
075: Node child = getChild(i);
076: if (child.typeOf() == EXPR_NAMED) {
077: NamedNode named = (NamedNode) child;
078: if (named.getName().equals(name)) {
079: return i;
080: }
081: }
082: }
083: return -1;
084: }
085:
086: public boolean needLineNumbers() {
087: int n = childs();
088: for (int i = 0; i < n; i++) {
089: if (getChild(i).needLineNumbers()) {
090: return true;
091: }
092: }
093: return false;
094: }
095:
096: public void optimizeChilds() {
097: int n = childs();
098: for (int i = 0; i < n; i++) {
099: Node child = getChild(i);
100: Node optimizedChild = child.optimize();
101: if (optimizedChild != child) {
102: setChild(i, optimizedChild);
103: }
104: }
105: }
106:
107: public Node optimize() {
108: optimizeChilds();
109: if (isConstant()) {
110: return new ConstantNode(eval());
111: } else {
112: return this ;
113: }
114: }
115:
116: public abstract int childs();
117:
118: public abstract Node getChild(int i);
119:
120: public abstract void setChild(int i, Node child);
121:
122: public Any eval() {
123: Any any = null;
124: int n = childs();
125: for (int i = 0; i < n; i++) {
126: any = getChild(i).eval();
127: }
128: return any;
129: }
130:
131: public void check(ErrorListener context) {
132: int n = childs();
133: for (int i = 0; i < n; i++) {
134: Node child = getChild(i);
135: if (child.typeOf() == EXPR_LINK) {
136: child = ((LinkNode) child).link(context);
137: child.check(context);
138: setChild(i, child);
139: }
140: child.check(context);
141: }
142: }
143:
144: public String toString() {
145: StringBuffer buf = new StringBuffer();
146: buf.append(super .toString());
147: buf.append('(');
148: int n = childs();
149: for (int i = 0; i < n; i++) {
150: buf.append(getChild(i).toString());
151: if (i < n - 1) {
152: buf.append(',');
153: buf.append(' ');
154: }
155: }
156: buf.append(')');
157: return buf.toString();
158: }
159:
160: public void compile(ByteCompiler context, int operation) {
161: int n = childs();
162: for (int i = 0; i < n; i++) {
163: getChild(i).compile(context, operation);
164: }
165: }
166:
167: }
|