Skip to content

Commit fcd3215

Browse files
committed
+ for statements
1 parent 657d8d8 commit fcd3215

File tree

4 files changed

+77
-8
lines changed

4 files changed

+77
-8
lines changed

README.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,9 @@ As demonstrated by the third example above, python scripts can step in, to chang
7979
- Write and collect example/test java files with supposed translations.
8080
- do switch statement right
8181
- make install
82+
- do java.typed right for VarArityParam (see LoopStyles)
83+
84+
8285

8386
### Pitfalls
8487

j2py/java/util.py

Lines changed: 32 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,35 @@
1-
class List(list):
1+
class Collection(list):
2+
pass
3+
4+
5+
class List(Collection):
26
pass
37

48

9+
class Iterator(object):
10+
11+
def __init__(self,collection):
12+
self.pos = 0
13+
self.collection = collection
14+
15+
def hasNext(self):
16+
return self.pos < len(self.collection)
17+
18+
def next(self):
19+
self.pos += 1
20+
return self.collection[self.pos-1]
21+
22+
23+
class ArrayList(Collection):
24+
def __init__(self,typ):
25+
self._typ = typ
26+
27+
def add(self,o):
28+
self.append(o)
29+
30+
def iterator(self):
31+
return Iterator(self)
32+
33+
34+
35+

j2py/test/j2py/LoopStyles.java

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
// taken from http://www.javapractices.com/topic/TopicAction.do?Id=125
2+
3+
import java.util.*;
4+
5+
public final class LoopStyles {
6+
7+
public static void main(String... aArguments ) {
8+
List<String> flavours = new ArrayList<String>();
9+
flavours.add("chocolate");
10+
flavours.add("strawberry");
11+
flavours.add("vanilla");
12+
13+
useWhileLoop( flavours );
14+
15+
useForLoop( flavours );
16+
}
17+
18+
private static void useWhileLoop( Collection<String> aFlavours ) {
19+
Iterator<String> flavoursIter = aFlavours.iterator();
20+
while ( flavoursIter.hasNext() ){
21+
System.out.println( flavoursIter.next() );
22+
}
23+
}
24+
25+
/**
26+
* Note that this for-loop does not use an integer index.
27+
*/
28+
private static void useForLoop( Collection<String> aFlavours ) {
29+
for ( Iterator<String> flavoursIter = aFlavours.iterator(); flavoursIter.hasNext(); ) {
30+
System.out.println( flavoursIter.next() );
31+
}
32+
}
33+
}
34+
35+

tools/pp2py/statement.str

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -163,14 +163,14 @@ rules
163163
for-head :
164164
For(init, cond, update, body)
165165
->
166-
H hs=0 [
167-
"::###NOTIMPL for"
168-
" "
169-
~binit
170-
H hs=1 [";" ~*bcond]
171-
H hs=1 [";" ~bupdate]
172-
":"
166+
V [
167+
~binit
168+
H hs=0 [
169+
"while " ~*bcond ":"
170+
]
171+
H hs=1 [" " ~bupdate]
173172
]
173+
174174
where
175175
<separate-by-comma> update => bupdate
176176

0 commit comments

Comments
 (0)