comparison graal/com.oracle.truffle.sl/src/com/oracle/truffle/sl/nodes/controlflow/SLIfNode.java @ 13821:b16ec83edc73

Documentation and more refactoring of Simple Language
author Christian Wimmer <christian.wimmer@oracle.com>
date Wed, 29 Jan 2014 20:45:43 -0800
parents 7c418666c6c9
children 64c77f0577bb
comparison
equal deleted inserted replaced
13820:20e7727588e8 13821:b16ec83edc73
1 /* 1 /*
2 * Copyright (c) 2012, 2012, Oracle and/or its affiliates. All rights reserved. 2 * Copyright (c) 2012, 2014, Oracle and/or its affiliates. All rights reserved.
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 * 4 *
5 * This code is free software; you can redistribute it and/or modify it 5 * This code is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License version 2 only, as 6 * under the terms of the GNU General Public License version 2 only, as
7 * published by the Free Software Foundation. 7 * published by the Free Software Foundation.
20 * or visit www.oracle.com if you need additional information or have any 20 * or visit www.oracle.com if you need additional information or have any
21 * questions. 21 * questions.
22 */ 22 */
23 package com.oracle.truffle.sl.nodes.controlflow; 23 package com.oracle.truffle.sl.nodes.controlflow;
24 24
25 import com.oracle.truffle.api.dsl.*;
25 import com.oracle.truffle.api.frame.*; 26 import com.oracle.truffle.api.frame.*;
27 import com.oracle.truffle.api.nodes.*;
26 import com.oracle.truffle.api.utilities.*; 28 import com.oracle.truffle.api.utilities.*;
27 import com.oracle.truffle.sl.nodes.*; 29 import com.oracle.truffle.sl.nodes.*;
28 30
31 @NodeInfo(shortName = "if")
29 public class SLIfNode extends SLStatementNode { 32 public class SLIfNode extends SLStatementNode {
30 @Child private SLExpressionNode conditionNode; 33 @Child private SLExpressionNode conditionNode;
31 @Child private SLStatementNode thenPartNode; 34 @Child private SLStatementNode thenPartNode;
32 @Child private SLStatementNode elsePartNode; 35 @Child private SLStatementNode elsePartNode;
33 36
40 this.elsePartNode = adoptChild(elsePartNode); 43 this.elsePartNode = adoptChild(elsePartNode);
41 } 44 }
42 45
43 @Override 46 @Override
44 public void executeVoid(VirtualFrame frame) { 47 public void executeVoid(VirtualFrame frame) {
45 if (conditionNode.executeCondition(frame)) { 48 if (evaluateCondition(frame)) {
46 thenTaken.enter(); 49 thenTaken.enter();
47 thenPartNode.executeVoid(frame); 50 thenPartNode.executeVoid(frame);
48 } else { 51 } else {
49 if (elsePartNode != null) { 52 if (elsePartNode != null) {
50 elseTaken.enter(); 53 elseTaken.enter();
51 elsePartNode.executeVoid(frame); 54 elsePartNode.executeVoid(frame);
52 } 55 }
53 } 56 }
54 } 57 }
58
59 private boolean evaluateCondition(VirtualFrame frame) {
60 try {
61 /*
62 * The condition must evaluate to a boolean value, so we call boolean-specialized
63 * method.
64 */
65 return conditionNode.executeBoolean(frame);
66 } catch (UnexpectedResultException ex) {
67 /*
68 * The condition evaluated to a non-boolean result. This is a type error in the SL
69 * program. We report it with the same exception that Truffle DSL generated nodes use to
70 * report type errors.
71 */
72 throw new UnsupportedSpecializationException(this, ex.getResult());
73 }
74 }
55 } 75 }