Mercurial Hosting > nabble
view src/nabble/naml/namespaces/ListSequence.java @ 0:7ecd1a4ef557
add content
author | Franklin Schmidt <fschmidt@gmail.com> |
---|---|
date | Thu, 21 Mar 2019 19:15:52 -0600 |
parents | |
children |
line wrap: on
line source
package nabble.naml.namespaces; import nabble.naml.compiler.Command; import nabble.naml.compiler.CommandSpec; import nabble.naml.compiler.IPrintWriter; import nabble.naml.compiler.Interpreter; import nabble.naml.compiler.Namespace; import java.util.List; @Namespace ( name = "list", global = true ) public class ListSequence<T> extends Sequence { protected final List<T> elements; protected int index = -1; public ListSequence(List<T> elements) { this.elements = elements; } protected final T get() { return elements.get(index); } protected final T getPrevious() { return index == 0? null : elements.get(index-1); } public static final CommandSpec has_more_elements = new CommandSpec.Builder() .dotParameter("n") .optionalParameters("n") .build() ; @Command public final void has_more_elements(IPrintWriter out,Interpreter interp) { int n = interp.getArgAsInt("n", 1); out.print( index+n < elements.size() ); } public static final CommandSpec next_element = new CommandSpec.Builder() .optionalParameters("inc") .build() ; @Command public void next_element(IPrintWriter out,Interpreter interp) { int inc = interp.getArgAsInt("inc", 1); index += inc; out.print( index < elements.size() ); } @Command public void current_element(IPrintWriter out,Interpreter interp) { out.print( get() ); } @Command public void element_count(IPrintWriter out,Interpreter interp) { out.print( elements.size() ); } @Command public void is_last_element(IPrintWriter out,Interpreter interp) { out.print( index == elements.size()-1); } @Command public void is_first_element(IPrintWriter out,Interpreter interp) { out.print( index == 0); } @Command public void list_is_empty(IPrintWriter out,Interpreter interp) { out.print( elements.isEmpty() ); } public static final CommandSpec remove_current_element = CommandSpec.NO_OUTPUT; @Command public void remove_current_element(IPrintWriter out,Interpreter interp) { elements.remove(index--); } public static final CommandSpec reset_list_index = CommandSpec.NO_OUTPUT; @Command public void reset_list_index(IPrintWriter out,Interpreter interp) { index = -1; } @Command public void current_index(IPrintWriter out,Interpreter interp) { out.print(index); } public static final CommandSpec has_element_at = new CommandSpec.Builder() .parameters("index") .build() ; @Command public void has_element_at(IPrintWriter out,Interpreter interp) { int index = interp.getArgAsInt("index"); out.print( index < elements.size() ); } public static final CommandSpec join = new CommandSpec.Builder() .dotParameter("do") .parameters("separator") .build() ; @Command public void join(IPrintWriter out,Interpreter interp) { Object todo = interp.getArg("do"); String separator = interp.getArgString("separator"); StringBuilder buf = new StringBuilder(); for( index = 0; index < elements.size(); index++ ) { if( index > 0 ) buf.append( separator ); buf.append( todo ); } out.print( buf ); } }