All Downloads are FREE. Search and download functionalities are using the official Maven repository.

prompto.param.ParameterList Maven / Gradle / Ivy

The newest version!
package prompto.param;

import java.util.List;
import java.util.stream.Collectors;

import prompto.grammar.Identifier;
import prompto.runtime.Context;
import prompto.transpiler.Transpiler;
import prompto.utils.CodeWriter;
import prompto.utils.ObjectList;

public class ParameterList extends ObjectList {

	private static final long serialVersionUID = 1L;

	public ParameterList() {
	}

	public ParameterList(IParameter param) {
		this.add(param);
	}
	
	public ParameterList(IParameter ... params) {
		for(IParameter param : params)
			this.add(param);
	}

	public void register(Context context) {
		for(IParameter param : this) 
			param.register(context);
	}

	public void check(Context context) {
		for(IParameter param : this) 
			param.check(context);
	}

	public IParameter find(Identifier name) {
		for(IParameter param : this) {
			if(name.equals(param.getId()))
					return param;
		}
		return null;
	}
	
	public List stripOutTemplateParameters() {
		return this.stream()
			.filter((a)->
				!(a instanceof CodeParameter))
			.collect(Collectors.toList());
	}


	public void toDialect(CodeWriter writer) {
		if(this.size()==0)
			return;
		switch(writer.getDialect()) {
		case E:
			toEDialect(writer);
			break;
		case O:
			toODialect(writer);
			break;
		case M:
			toMDialect(writer);
			break;
		}
	}
	
	private void toEDialect(CodeWriter writer) {
		IParameter last = this.getLast();
		writer.append("receiving ");
		for(IParameter param : this) {
			if(param==last)
				break;
			param.toDialect(writer);
			writer.append(", ");
		}
		if(this.size()>1) {
			writer.trimLast(2);
			writer.append(" and ");
		}
		last.toDialect(writer);
		writer.append(" ");
	}

	private void toODialect(CodeWriter writer) {
		for(IParameter param : this) {
			param.toDialect(writer);
			writer.append(", ");
		}
		writer.trimLast(2);
	}

	private void toMDialect(CodeWriter writer) {
		for(IParameter param : this) {
			param.toDialect(writer);
			writer.append(", ");
		}
		writer.trimLast(2);
	}

	public void declare(Transpiler transpiler) {
		this.forEach(param -> param.declare(transpiler));
	}

	public void transpile(Transpiler transpiler) {
		List params = this.stream()
			.filter(param->!(param instanceof CodeParameter))
			.collect(Collectors.toList());
		if(!params.isEmpty()) {
	        params.forEach(param->{
	            param.transpile(transpiler);
	            transpiler.append(", ");
	        });
	        transpiler.trimLast(2);
	    }
	}

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy