01: /*
02: * $Id: UnaryMinusNode.java,v 1.6 2002/09/16 08:05:05 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.codec.Code;
14: import anvil.script.compiler.ByteCompiler;
15: import anvil.script.Context;
16: import java.io.IOException;
17:
18: /**
19: * class UnaryMinusNode
20: *
21: * @author: Jani Lehtimäki
22: */
23: public class UnaryMinusNode extends UnaryParent {
24:
25: public UnaryMinusNode(Node child) {
26: super (child);
27: }
28:
29: public int typeOf() {
30: return Node.EXPR_UNARY_MINUS;
31: }
32:
33: public Node optimize() {
34: int count = 1;
35: Node child = _child;
36: while (child.typeOf() == EXPR_UNARY_MINUS) {
37: count++;
38: child = ((UnaryMinusNode) child).getChild();
39: }
40: if (count % 2 == 0) {
41: return child;
42: } else {
43: _child = child;
44: return this ;
45: }
46: }
47:
48: public Any eval() {
49: return _child.eval().minus();
50: }
51:
52: public void compile(ByteCompiler context, int operation) {
53: _child.compile(context, GET);
54: Code code = context.getCode();
55: code.invokevirtual(code.getPool().addMethodRef(
56: context.TYPE_ANY, "minus", "()Lanvil/core/Any;"));
57: if (operation == GET_BOOLEAN) {
58: context.any2boolean();
59: }
60: }
61:
62: }
|