Mercurial Hosting > luan
comparison src/luan/interp/LuaCompiler.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 | |
children | 5cf15507d77e |
comparison
equal
deleted
inserted
replaced
29:9bc66d09ea48 | 30:8d8f4f5caef4 |
---|---|
1 package luan.interp; | |
2 | |
3 import org.parboiled.Parboiled; | |
4 import org.parboiled.errors.ErrorUtils; | |
5 import org.parboiled.parserunners.ReportingParseRunner; | |
6 import org.parboiled.parserunners.TracingParseRunner; | |
7 import org.parboiled.support.ParsingResult; | |
8 import luan.LuaFunction; | |
9 import luan.LuaState; | |
10 import luan.LuaException; | |
11 | |
12 | |
13 public final class LuaCompiler { | |
14 private LuaCompiler() {} // never | |
15 | |
16 public static LuaFunction compile(LuaState lua,String src) throws LuaException { | |
17 LuaParser parser = Parboiled.createParser(LuaParser.class); | |
18 ParsingResult<?> result = new ReportingParseRunner(parser.Target()).run(src); | |
19 // ParsingResult<?> result = new TracingParseRunner(parser.Target()).run(src); | |
20 if( result.hasErrors() ) | |
21 throw new LuaException( ErrorUtils.printParseErrors(result) ); | |
22 Object resultValue = result.resultValue; | |
23 if( resultValue instanceof Expressions ) { | |
24 final Expressions expressions = (Expressions)resultValue; | |
25 return new LuaFunction() { | |
26 public Object[] call(LuaState lua,Object... args) throws LuaException { | |
27 return expressions.eval(lua); | |
28 } | |
29 }; | |
30 } | |
31 Chunk chunk = (Chunk)resultValue; | |
32 return chunk.newClosure(lua); | |
33 } | |
34 } |