package codeline;

import processor.ControlFlow;
import processor.Memory;
import processor.PseudoCodeNode;
import processor.Memory.Visibility;

public class FunctionCall extends CodeLine {

	private PseudoCodeNode target;
	private String[] params;
	
	public FunctionCall( PseudoCodeNode function, String[] params )
	{
		this.target = function;
		this.params = params;
	}
	
	@Override
	public ControlFlow runForward(Memory m) {
		int index = 1;
		for( String p : params )
		{
			m.declare( "param" + index, m.read( p, Visibility.LOCAL ), Visibility.GLOBAL );
			index++;
		}
		actions.push( (Memory mem) -> {
			int ind = 1;
			for( @SuppressWarnings("unused") String p : params )
			{
				m.undeclare( "param" + ind, Visibility.GLOBAL );
				ind++;
			}
		} );
		return new ControlFlow( target );
	}

}
