13514
|
1 /*
|
|
2 * Copyright (c) 2013 Oracle and/or its affiliates. All rights reserved. This
|
|
3 * code is released under a tri EPL/GPL/LGPL license. You can use it,
|
|
4 * redistribute it and/or modify it under the terms of the:
|
|
5 *
|
|
6 * Eclipse Public License version 1.0
|
|
7 * GNU General Public License version 2
|
|
8 * GNU Lesser General Public License version 2.1
|
|
9 */
|
|
10 package com.oracle.truffle.ruby.nodes.control;
|
|
11
|
|
12 import com.oracle.truffle.api.*;
|
|
13 import com.oracle.truffle.api.frame.*;
|
|
14 import com.oracle.truffle.api.nodes.*;
|
|
15 import com.oracle.truffle.api.utilities.*;
|
|
16 import com.oracle.truffle.ruby.nodes.*;
|
|
17 import com.oracle.truffle.ruby.nodes.cast.*;
|
|
18 import com.oracle.truffle.ruby.runtime.*;
|
|
19 import com.oracle.truffle.ruby.runtime.control.*;
|
|
20
|
|
21 /**
|
|
22 * Represents a Ruby {@code while} statement.
|
|
23 */
|
|
24 @NodeInfo(shortName = "while")
|
|
25 public class WhileNode extends RubyNode {
|
|
26
|
|
27 @Child protected BooleanCastNode condition;
|
|
28 @Child protected RubyNode body;
|
|
29
|
|
30 private final BranchProfile breakProfile = new BranchProfile();
|
|
31 private final BranchProfile nextProfile = new BranchProfile();
|
|
32 private final BranchProfile redoProfile = new BranchProfile();
|
|
33
|
|
34 public WhileNode(RubyContext context, SourceSection sourceSection, BooleanCastNode condition, RubyNode body) {
|
|
35 super(context, sourceSection);
|
|
36 this.condition = adoptChild(condition);
|
|
37 this.body = adoptChild(body);
|
|
38 }
|
|
39
|
|
40 @Override
|
|
41 public Object execute(VirtualFrame frame) {
|
|
42 outer: while (condition.executeBoolean(frame)) {
|
|
43 while (true) {
|
|
44 try {
|
|
45 body.execute(frame);
|
|
46 continue outer;
|
|
47 } catch (BreakException e) {
|
|
48 breakProfile.enter();
|
|
49 return e.getResult();
|
|
50 } catch (NextException e) {
|
|
51 nextProfile.enter();
|
|
52 continue outer;
|
|
53 } catch (RedoException e) {
|
|
54 redoProfile.enter();
|
|
55 }
|
|
56 }
|
|
57 }
|
|
58
|
|
59 return NilPlaceholder.INSTANCE;
|
|
60 }
|
|
61
|
|
62 }
|