001: /*
002: * $Id: Expression.java,v 1.26 2002/09/16 08:05:04 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.Location;
013: import anvil.core.Any;
014: import anvil.codec.Code;
015: import anvil.script.compiler.ByteCompiler;
016: import anvil.script.Context;
017: import anvil.script.statements.FunctionStatement;
018: import anvil.ErrorListener;
019: import java.io.IOException;
020:
021: /**
022: * class Expression
023: *
024: * @author: Jani Lehtimäki
025: */
026: public class Expression extends UnaryParent {
027:
028: public static final int IS_TRUE = 0;
029: public static final int IS_FALSE = 1;
030: public static final int IS_DYNAMIC = 2;
031:
032: public static Expression NULL = new Expression(ConstantNode.NULL,
033: null);
034: public static Expression UNDEFINED = new Expression(
035: ConstantNode.UNDEFINED, null);
036:
037: private Location _location;
038:
039: public Expression(Node child, Location location) {
040: super (child);
041: _location = location;
042: }
043:
044: public Location getLocation() {
045: return _location;
046: }
047:
048: public boolean needLineNumbers() {
049: return _child.needLineNumbers();
050: }
051:
052: public boolean isStandalone() {
053: return _child.isStandalone();
054: }
055:
056: public boolean isAssignable() {
057: return _child.isAssignable();
058: }
059:
060: public boolean isUpdatable() {
061: return _child.isUpdatable();
062: }
063:
064: public boolean isConstant() {
065: return _child.isConstant();
066: }
067:
068: public Any constant() {
069: return _child.eval();
070: }
071:
072: public int conditionOf() {
073: if (_child.isConstant()) {
074: return _child.eval().toBoolean() ? IS_TRUE : IS_FALSE;
075: } else {
076: return IS_DYNAMIC;
077: }
078: }
079:
080: public void check(ErrorListener context) {
081: super .check(context);
082: }
083:
084: public Node optimize() {
085: return this ;
086: }
087:
088: public Any eval() {
089: return _child.eval();
090: }
091:
092: public void createNegation() {
093: if (_child.typeOf() == EXPR_NOT) {
094: _child = ((Parent) _child).getChild(0);
095: } else {
096: _child = new UnaryNegateNode(_child);
097: }
098: }
099:
100: public void compile(ByteCompiler context, final int operation) {
101: _child.optimize().compile(context, operation);
102: }
103:
104: public void compile(ByteCompiler context, final Node child) {
105: _child.optimize().compile(context, child);
106: }
107:
108: }
|