package cz.chovanecm.pascal.truffle.nodes.controlflow;

import com.oracle.truffle.api.CompilerDirectives;
import com.oracle.truffle.api.dsl.UnsupportedSpecializationException;
import com.oracle.truffle.api.frame.VirtualFrame;
import com.oracle.truffle.api.nodes.Node;
import com.oracle.truffle.api.nodes.NodeInfo;
import com.oracle.truffle.api.nodes.UnexpectedResultException;
import cz.chovanecm.pascal.truffle.PascalTypesGen;
import cz.chovanecm.pascal.truffle.nodes.ExpressionNode;
import cz.chovanecm.pascal.truffle.nodes.StatementNode;

/**
 * Created by martin on 1/27/17.
 */

/**
 * While node without any optimisation
 * (without Truffle.getRuntime().createLoopNode())
 */
@NodeInfo(shortName = "while")
public class WhileNode extends StatementNode {
    @Child
    private ExpressionNode conditionNode;
    @Child
    private StatementNode loopNode;

    public WhileNode(ExpressionNode conditionNode, StatementNode loopNode) {
        this.conditionNode = conditionNode;
        this.loopNode = loopNode;
    }

    @Override
    public void execute(VirtualFrame frame) {
        try {
            while (PascalTypesGen.expectBoolean(conditionNode.execute(frame))) {
                loopNode.execute(frame);
            }
        } catch (UnexpectedResultException e) {
            CompilerDirectives.transferToInterpreter();
            throw new UnsupportedSpecializationException(this, new Node[]{conditionNode}, e.getResult());
        }
    }
}

@@ -83,96 +83,15 @@
 api.
-CompilerDirectives;%0Aimport com.oracle.truffle.api.dsl.UnsupportedSpecializationException
+Truffle
 ;%0Aim
@@ -138,16 +138,62 @@
 lFrame;%0A
+import com.oracle.truffle.api.nodes.LoopNode;%0A
 import c
@@ -312,33 +312,21 @@
 des.
-UnexpectedResultException
+RepeatingNode
 ;%0Aim
@@ -733,49 +733,473 @@
 ate 
-ExpressionNode conditionNode;%0A    @Child%0A
+LoopNode loop;%0A%0A    public WhileNode(ExpressionNode conditionNode, StatementNode loopNode) %7B%0A%0A        loop = Truffle.getRuntime().createLoopNode(new WhileRepeatingNode(conditionNode, loopNode));%0A    %7D%0A%0A    @Override%0A    public void execute(VirtualFrame frame) %7B%0A        loop.executeLoop(frame);%0A    %7D%0A%0A    public static class WhileRepeatingNode extends Node implements RepeatingNode %7B%0A        @Child%0A        private ExpressionNode conditionNode;%0A        @Child%0A    
     
@@ -1231,16 +1231,20 @@
 pNode;%0A%0A
+    
     publ
@@ -1243,32 +1243,41 @@
     public While
+Repeating
 Node(ExpressionN
@@ -1325,24 +1325,28 @@
 ) %7B%0A        
+    
 this.conditi
@@ -1373,24 +1373,28 @@
 de;%0A        
+   
+ 
 this.loopNod
@@ -1407,27 +1407,35 @@
 opNode;%0A    
-%7D%0A%0A
+    %7D%0A%0A    
     @Overrid
@@ -1440,35 +1440,42 @@
 ide%0A    
+  
+  
 public 
-void
+boolean
  execute
 (Virtual
@@ -1458,32 +1458,41 @@
  boolean execute
+Repeating
 (VirtualFrame fr
@@ -1510,31 +1510,14 @@
     
-try %7B%0A            while
+    if
  (Pa
@@ -1529,22 +1529,18 @@
 ypesGen.
-expect
+as
 Boolean(
@@ -1629,219 +1629,89 @@
     
-%7D%0A        %7D catch (UnexpectedResultException e) %7B%0A            CompilerDirectives.transferToInterpreter();%0A            throw new UnsupportedSpecializationException(this, new Node%5B%5D%7BconditionNode%7D, e.getResult());
+    return true;%0A            %7D else %7B%0A                return false;%0A            %7D
 %0A   

Optimise loop to use Truffle's LoopNode