Mercurial Hosting > luan
comparison src/luan/lib/BasicLib.java @ 30:8d8f4f5caef4
minor
git-svn-id: https://luan-java.googlecode.com/svn/trunk@31 21e917c8-12df-6dd8-5cb6-c86387c605b9
author | fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9> |
---|---|
date | Fri, 07 Dec 2012 20:22:19 +0000 |
parents | 9bc66d09ea48 |
children | 0cdc1da466ee |
comparison
equal
deleted
inserted
replaced
29:9bc66d09ea48 | 30:8d8f4f5caef4 |
---|---|
5 import java.io.FileReader; | 5 import java.io.FileReader; |
6 import java.io.IOException; | 6 import java.io.IOException; |
7 import java.lang.reflect.Method; | 7 import java.lang.reflect.Method; |
8 import java.util.Iterator; | 8 import java.util.Iterator; |
9 import java.util.Map; | 9 import java.util.Map; |
10 import org.parboiled.Parboiled; | |
11 import org.parboiled.errors.ErrorUtils; | |
12 import org.parboiled.parserunners.ReportingParseRunner; | |
13 import org.parboiled.parserunners.TracingParseRunner; | |
14 import org.parboiled.support.ParsingResult; | |
15 import luan.Lua; | 10 import luan.Lua; |
16 import luan.LuaState; | 11 import luan.LuaState; |
17 import luan.LuaTable; | 12 import luan.LuaTable; |
18 import luan.LuaNumber; | 13 import luan.LuaNumber; |
19 import luan.LuaFunction; | 14 import luan.LuaFunction; |
20 import luan.LuaJavaFunction; | 15 import luan.LuaJavaFunction; |
21 import luan.LuaException; | 16 import luan.LuaException; |
22 import luan.interp.LuaParser; | 17 import luan.interp.LuaCompiler; |
23 import luan.interp.Expressions; | |
24 import luan.interp.Chunk; | |
25 | 18 |
26 | 19 |
27 public class BasicLib { | 20 public class BasicLib { |
28 | 21 |
29 public static void register(LuaState lua) { | 22 public static void register(LuaState lua) { |
56 public static String type(Object obj) { | 49 public static String type(Object obj) { |
57 return Lua.type(obj); | 50 return Lua.type(obj); |
58 } | 51 } |
59 | 52 |
60 public static LuaFunction load(LuaState lua,String ld) throws LuaException { | 53 public static LuaFunction load(LuaState lua,String ld) throws LuaException { |
61 LuaParser parser = Parboiled.createParser(LuaParser.class); | 54 return LuaCompiler.compile(lua,ld); |
62 ParsingResult<?> result = new ReportingParseRunner(parser.Target()).run(ld); | |
63 // ParsingResult<?> result = new TracingParseRunner(parser.Target()).run(ld); | |
64 if( result.hasErrors() ) | |
65 throw new LuaException( ErrorUtils.printParseErrors(result) ); | |
66 Object resultValue = result.resultValue; | |
67 if( resultValue instanceof Expressions ) { | |
68 final Expressions expressions = (Expressions)resultValue; | |
69 return new LuaFunction() { | |
70 public Object[] call(LuaState lua,Object... args) throws LuaException { | |
71 return expressions.eval(lua); | |
72 } | |
73 }; | |
74 } | |
75 Chunk chunk = (Chunk)resultValue; | |
76 return chunk.newClosure(lua); | |
77 } | 55 } |
78 | 56 |
79 public static String readAll(Reader in) | 57 public static String readAll(Reader in) |
80 throws IOException | 58 throws IOException |
81 { | 59 { |