package cz.chovanecm.pascal.truffle.nodes;

import com.oracle.truffle.api.CompilerDirectives;
import com.oracle.truffle.api.frame.VirtualFrame;
import com.oracle.truffle.api.nodes.ExplodeLoop;
import com.oracle.truffle.api.nodes.NodeInfo;

/**
 * Created by martin on 1/17/17.
 */
@NodeInfo(shortName = "write")
public class WriteNode extends BuiltinProcedureNode {

    public WriteNode(ExpressionNode[] parameters) {
        super(parameters);
    }

    public WriteNode() {
        super(new ExpressionNode[]{});
    }

    @Override
    @ExplodeLoop
    public void executeStatement(VirtualFrame frame) {
        boolean firstPass = true;
        for (ExpressionNode parameter : parameters) {
            // NOTE: Should we use ConditionProfile?
            if (!firstPass) {
                // Pritn separating space between argumetns.
                print(" ");
            } else {
                firstPass = false;
            }
            print(parameter.executeGeneric(frame));
        }
    }

    @CompilerDirectives.TruffleBoundary
    public void print(Object value) {
        System.out.print(value);
    }
}

@@ -607,42 +607,8 @@
 ) %7B%0A
-        boolean firstPass = true;%0A
     
@@ -661,250 +661,8 @@
 ) %7B%0A
-            // NOTE: Should we use ConditionProfile?%0A            if (!firstPass) %7B%0A                // Pritn separating space between argumetns.%0A                print(%22 %22);%0A            %7D else %7B%0A                firstPass = false;%0A            %7D%0A
     

write behaves as in normal pascal again (without spaces between parameters)