package org.shedlang.compiler.patio; import org.pcollections.PVector; import org.shedlang.compiler.patio.instructions.PatioInstruction; import org.shedlang.compiler.patio.types.PatioType; import org.shedlang.compiler.sources.Source; import org.shedlang.compiler.sources.Sources; import org.shedlang.compiler.util.P; import java.util.List; public record PatioFunctionDefinition( String name, PatioVisibility visibility, List locals, List returnTypes, List body, Source source ) implements PatioCompilationUnitElement { public Iterable params() { return locals.stream() .filter(local -> local.localType() == PatioLocalType.PARAM) .toList(); } public static Builder builder() { return new Builder( "f", PatioVisibility.PRIVATE, P.vector(), P.vector(), P.vector() ); } public record Builder( String name, PatioVisibility visibility, PVector locals, PVector returnTypes, PVector body ) { public Builder withName(String name) { return new Builder( name, this.visibility, this.locals, this.returnTypes, this.body ); } public Builder withVisibility(PatioVisibility visibility) { return new Builder( this.name, visibility, this.locals, this.returnTypes, this.body ); } public Builder addLocal(PatioLocal local) { return new Builder( this.name, this.visibility, this.locals.plus(local), this.returnTypes, this.body ); } public Builder addBodyInstruction(PatioInstruction instruction) { return new Builder( this.name, this.visibility, this.locals, this.returnTypes, this.body.plus(instruction) ); } public Builder addReturnType(PatioType returnType) { return new Builder( this.name, this.visibility, this.locals, this.returnTypes.plus(returnType), this.body ); } public PatioFunctionDefinition build() { return new PatioFunctionDefinition( this.name, this.visibility, this.locals, this.returnTypes, this.body, Sources.NULL ); } } }