01: /**************************************************************************/
02: /* N I C E */
03: /* A high-level object-oriented research language */
04: /* (c) Daniel Bonniot 2002 */
05: /* */
06: /* This program is free software; you can redistribute it and/or modify */
07: /* it under the terms of the GNU General Public License as published by */
08: /* the Free Software Foundation; either version 2 of the License, or */
09: /* (at your option) any later version. */
10: /* */
11: /**************************************************************************/package nice.lang.inline;
12:
13: import gnu.mapping.Procedure2;
14: import gnu.expr.*;
15: import gnu.bytecode.*;
16: import nice.tools.code.EnsureTypeProc;
17:
18: /**
19: e1 || e2
20: Evaluate e1, and return it if it is not null (without evaluating e2).
21: Otherwise evaluate e2 and return it.
22:
23: @author Daniel Bonniot
24: */
25: public class OptionOr extends Procedure2 implements bossa.syntax.Macro {
26: public static OptionOr create(String param) {
27: return instance;
28: }
29:
30: private static OptionOr instance = new OptionOr();
31:
32: public void compile(ApplyExp exp, Compilation comp, Target target) {
33: Expression[] args = exp.getArgs();
34: CodeAttr code = comp.getCode();
35:
36: // We cannot use target for the first argument, since its value maybe
37: // be null, while the target may not.
38: args[0].compile(comp, Target.pushObject);
39: code.emitDup();
40: code.emitIfNotNull();
41: target.compileFromStack(comp, code.topType());
42: code.emitElse();
43: code.emitPop(1);
44: args[1].compile(comp, target);
45: code.emitFi();
46: }
47:
48: public Type getReturnType(Expression[] args) {
49: return Type.pointer_type;
50: }
51:
52: public void checkSpecialRequirements(
53: bossa.syntax.Expression[] arguments) {
54: if (nice.tools.typing.Types.isSure(arguments[0].getType()
55: .getMonotype()))
56: bossa.util.User
57: .warning(arguments[0],
58: "First argument is a non-null value thus the second one will not be used.");
59: }
60:
61: // Interpretation
62:
63: public Object apply2(Object arg1, Object arg2) {
64: throw new Error("Not implemented");
65: }
66: }
|