Mercurial Hosting > luan
comparison src/luan/lib/BasicLib.java @ 19:a7c13c6017f7
add GenericForStmt
git-svn-id: https://luan-java.googlecode.com/svn/trunk@20 21e917c8-12df-6dd8-5cb6-c86387c605b9
author | fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9> |
---|---|
date | Sun, 02 Dec 2012 10:08:24 +0000 |
parents | 2ddf85634d20 |
children | c93d8c781853 |
comparison
equal
deleted
inserted
replaced
18:3971113699b8 | 19:a7c13c6017f7 |
---|---|
1 package luan.lib; | 1 package luan.lib; |
2 | 2 |
3 import java.io.FileReader; | 3 import java.io.FileReader; |
4 import java.io.IOException; | 4 import java.io.IOException; |
5 import java.lang.reflect.Method; | |
6 import java.util.Iterator; | |
7 import java.util.Map; | |
5 import org.parboiled.Parboiled; | 8 import org.parboiled.Parboiled; |
6 import org.parboiled.errors.ErrorUtils; | 9 import org.parboiled.errors.ErrorUtils; |
7 import org.parboiled.parserunners.ReportingParseRunner; | 10 import org.parboiled.parserunners.ReportingParseRunner; |
8 import org.parboiled.parserunners.TracingParseRunner; | 11 import org.parboiled.parserunners.TracingParseRunner; |
9 import org.parboiled.support.ParsingResult; | 12 import org.parboiled.support.ParsingResult; |
10 import luan.Lua; | 13 import luan.Lua; |
11 import luan.LuaState; | 14 import luan.LuaState; |
12 import luan.LuaTable; | 15 import luan.LuaTable; |
16 import luan.LuaNumber; | |
13 import luan.LuaFunction; | 17 import luan.LuaFunction; |
14 import luan.LuaJavaFunction; | 18 import luan.LuaJavaFunction; |
15 import luan.LuaException; | 19 import luan.LuaException; |
16 import luan.interp.LuaParser; | 20 import luan.interp.LuaParser; |
17 import luan.interp.Expressions; | 21 import luan.interp.Expressions; |
22 | 26 |
23 public static void register(LuaState lua) { | 27 public static void register(LuaState lua) { |
24 LuaTable t = lua.env(); | 28 LuaTable t = lua.env(); |
25 add( t, "print", new Object[0].getClass() ); | 29 add( t, "print", new Object[0].getClass() ); |
26 add( t, "type", Object.class ); | 30 add( t, "type", Object.class ); |
31 add( t, "load", String.class ); | |
32 add( t, "loadFile", String.class ); | |
33 add( t, "pairs", LuaTable.class ); | |
34 add( t, "ipairs", LuaTable.class ); | |
27 } | 35 } |
28 | 36 |
29 private static void add(LuaTable t,String method,Class<?>... parameterTypes) { | 37 private static void add(LuaTable t,String method,Class<?>... parameterTypes) { |
30 try { | 38 try { |
31 t.set( method, new LuaJavaFunction(BasicLib.class.getMethod(method,parameterTypes),null) ); | 39 t.set( method, new LuaJavaFunction(BasicLib.class.getMethod(method,parameterTypes),null) ); |
84 | 92 |
85 public static LuaFunction loadFile(String fileName) throws LuaException,IOException { | 93 public static LuaFunction loadFile(String fileName) throws LuaException,IOException { |
86 return load(readFile(fileName)); | 94 return load(readFile(fileName)); |
87 } | 95 } |
88 | 96 |
97 private static class TableIter { | |
98 private final Iterator<Map.Entry<Object,Object>> iter; | |
99 | |
100 TableIter(LuaTable t) { | |
101 this.iter = t.map.entrySet().iterator(); | |
102 } | |
103 | |
104 public Object[] next() { | |
105 if( !iter.hasNext() ) | |
106 return LuaFunction.EMPTY_RTN; | |
107 Map.Entry<Object,Object> entry = iter.next(); | |
108 return new Object[]{entry.getKey(),entry.getValue()}; | |
109 } | |
110 } | |
111 | |
112 public static LuaFunction pairs(LuaTable t) { | |
113 try { | |
114 TableIter ti = new TableIter(t); | |
115 Method m = TableIter.class.getMethod("next"); | |
116 m.setAccessible(true); | |
117 return new LuaJavaFunction(m,ti); | |
118 } catch(NoSuchMethodException e) { | |
119 throw new RuntimeException(e); | |
120 } | |
121 } | |
122 | |
123 private static class ArrayIter { | |
124 private final LuaTable t; | |
125 private double i = 0.0; | |
126 | |
127 ArrayIter(LuaTable t) { | |
128 this.t = t; | |
129 } | |
130 | |
131 public Object[] next() { | |
132 LuaNumber n = new LuaNumber(++i); | |
133 Object val = t.get(n); | |
134 return val==null ? LuaFunction.EMPTY_RTN : new Object[]{n,val}; | |
135 } | |
136 } | |
137 | |
138 public static LuaFunction ipairs(LuaTable t) { | |
139 try { | |
140 ArrayIter ai = new ArrayIter(t); | |
141 Method m = ArrayIter.class.getMethod("next"); | |
142 m.setAccessible(true); | |
143 return new LuaJavaFunction(m,ai); | |
144 } catch(NoSuchMethodException e) { | |
145 throw new RuntimeException(e); | |
146 } | |
147 } | |
89 } | 148 } |