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 gnu.expr;
12:
13: import gnu.bytecode.*;
14:
15: /**
16: Check that a precondition holds, and that the body
17: leads to the postcondition to hold.
18:
19: @version $Date: 2004/07/22 16:14:54 $
20: @author Daniel Bonniot (bonniot@users.sourceforge.net)
21: */
22:
23: public class CheckContract extends Expression {
24: public CheckContract(Expression[] pre, Expression[] post,
25: Expression body) {
26: this .pre = pre;
27: this .post = post;
28: this .body = body;
29: }
30:
31: public static final Expression result = new ResultExp();
32:
33: private Expression[] pre, post;
34: private Expression body;
35:
36: public void compile(Compilation comp, Target target) {
37: CodeAttr code = comp.getCode();
38: ClassExp currentClass = comp.curLambda.outerClass();
39: code.preparePostcondition(currentClass
40: .getAssertionEnabledField(), post.length > 0);
41: if (pre.length > 0) {
42: code.startPrecondition();
43: for (int i = 0; i < pre.length; i++)
44: pre[i].compileWithPosition(comp, Target.Ignore);
45:
46: code.endPrecondition();
47: }
48:
49: body.compileWithPosition(comp, target);
50:
51: if (post.length > 0) {
52: code.startPostcondition();
53: for (int i = 0; i < post.length; i++)
54: post[i].compileWithPosition(comp, Target.Ignore);
55:
56: code.endPostcondition();
57: } else
58: code.pushRetType();
59:
60: }
61:
62: protected void walkChildren(ExpWalker walker) {
63: walker.walkExps(pre);
64: if (walker.exitValue != null)
65: return;
66:
67: body = body.walk(walker);
68: if (walker.exitValue != null)
69: return;
70:
71: walker.walkExps(post);
72: }
73:
74: public void print(gnu.mapping.OutPort ps) {
75: ps.print("(Check ...)");
76: }
77:
78: private static class ResultExp extends Expression {
79: public void compile(Compilation comp, Target target) {
80: CodeAttr code = comp.getCode();
81: code.loadResult();
82: }
83:
84: public void print(gnu.mapping.OutPort ps) {
85: ps.print("(Result)");
86: }
87: }
88: }
|