Mercurial Hosting > nabble
comparison 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 |
comparison
equal
deleted
inserted
replaced
-1:000000000000 | 0:7ecd1a4ef557 |
---|---|
1 package nabble.naml.namespaces; | |
2 | |
3 import nabble.naml.compiler.Command; | |
4 import nabble.naml.compiler.CommandSpec; | |
5 import nabble.naml.compiler.IPrintWriter; | |
6 import nabble.naml.compiler.Interpreter; | |
7 import nabble.naml.compiler.Namespace; | |
8 | |
9 import java.util.List; | |
10 | |
11 | |
12 @Namespace ( | |
13 name = "list", | |
14 global = true | |
15 ) | |
16 public class ListSequence<T> extends Sequence { | |
17 protected final List<T> elements; | |
18 protected int index = -1; | |
19 | |
20 public ListSequence(List<T> elements) { | |
21 this.elements = elements; | |
22 } | |
23 | |
24 protected final T get() { | |
25 return elements.get(index); | |
26 } | |
27 | |
28 protected final T getPrevious() { | |
29 return index == 0? null : elements.get(index-1); | |
30 } | |
31 | |
32 public static final CommandSpec has_more_elements = new CommandSpec.Builder() | |
33 .dotParameter("n") | |
34 .optionalParameters("n") | |
35 .build() | |
36 ; | |
37 | |
38 @Command public final void has_more_elements(IPrintWriter out,Interpreter interp) { | |
39 int n = interp.getArgAsInt("n", 1); | |
40 out.print( index+n < elements.size() ); | |
41 } | |
42 | |
43 public static final CommandSpec next_element = new CommandSpec.Builder() | |
44 .optionalParameters("inc") | |
45 .build() | |
46 ; | |
47 | |
48 @Command public void next_element(IPrintWriter out,Interpreter interp) { | |
49 int inc = interp.getArgAsInt("inc", 1); | |
50 index += inc; | |
51 out.print( index < elements.size() ); | |
52 } | |
53 | |
54 @Command public void current_element(IPrintWriter out,Interpreter interp) { | |
55 out.print( get() ); | |
56 } | |
57 | |
58 @Command public void element_count(IPrintWriter out,Interpreter interp) { | |
59 out.print( elements.size() ); | |
60 } | |
61 | |
62 @Command public void is_last_element(IPrintWriter out,Interpreter interp) { | |
63 out.print( index == elements.size()-1); | |
64 } | |
65 | |
66 @Command public void is_first_element(IPrintWriter out,Interpreter interp) { | |
67 out.print( index == 0); | |
68 } | |
69 | |
70 @Command public void list_is_empty(IPrintWriter out,Interpreter interp) { | |
71 out.print( elements.isEmpty() ); | |
72 } | |
73 | |
74 public static final CommandSpec remove_current_element = CommandSpec.NO_OUTPUT; | |
75 | |
76 @Command public void remove_current_element(IPrintWriter out,Interpreter interp) { | |
77 elements.remove(index--); | |
78 } | |
79 | |
80 public static final CommandSpec reset_list_index = CommandSpec.NO_OUTPUT; | |
81 | |
82 @Command public void reset_list_index(IPrintWriter out,Interpreter interp) { | |
83 index = -1; | |
84 } | |
85 | |
86 @Command public void current_index(IPrintWriter out,Interpreter interp) { | |
87 out.print(index); | |
88 } | |
89 | |
90 public static final CommandSpec has_element_at = new CommandSpec.Builder() | |
91 .parameters("index") | |
92 .build() | |
93 ; | |
94 | |
95 @Command public void has_element_at(IPrintWriter out,Interpreter interp) { | |
96 int index = interp.getArgAsInt("index"); | |
97 out.print( index < elements.size() ); | |
98 } | |
99 | |
100 public static final CommandSpec join = new CommandSpec.Builder() | |
101 .dotParameter("do") | |
102 .parameters("separator") | |
103 .build() | |
104 ; | |
105 | |
106 @Command public void join(IPrintWriter out,Interpreter interp) { | |
107 Object todo = interp.getArg("do"); | |
108 String separator = interp.getArgString("separator"); | |
109 StringBuilder buf = new StringBuilder(); | |
110 for( index = 0; index < elements.size(); index++ ) { | |
111 if( index > 0 ) | |
112 buf.append( separator ); | |
113 buf.append( todo ); | |
114 } | |
115 out.print( buf ); | |
116 } | |
117 } |