Mercurial Hosting > luan
comparison src/luan/interp/LuanCompiler.java @ 48:64ecb7a3aad7
rename Lua to Luan
git-svn-id: https://luan-java.googlecode.com/svn/trunk@49 21e917c8-12df-6dd8-5cb6-c86387c605b9
author | fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9> |
---|---|
date | Fri, 28 Dec 2012 03:29:12 +0000 |
parents | |
children | 8ede219cd111 |
comparison
equal
deleted
inserted
replaced
47:659c7139e903 | 48:64ecb7a3aad7 |
---|---|
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.LuanFunction; | |
9 import luan.LuanState; | |
10 import luan.LuanException; | |
11 import luan.LuanSource; | |
12 import luan.LuanElement; | |
13 | |
14 | |
15 public final class LuanCompiler { | |
16 private LuanCompiler() {} // never | |
17 | |
18 public static LuanFunction compile(LuanState lua,LuanSource src) throws LuanException { | |
19 LuanParser parser = Parboiled.createParser(LuanParser.class); | |
20 parser.source = src; | |
21 ParsingResult<?> result = new ReportingParseRunner(parser.Target()).run(src.text); | |
22 // ParsingResult<?> result = new TracingParseRunner(parser.Target()).run(src); | |
23 if( result.hasErrors() ) | |
24 throw new LuanException( lua, null, ErrorUtils.printParseErrors(result) ); | |
25 Object resultValue = result.resultValue; | |
26 if( resultValue instanceof Expressions ) { | |
27 final Expressions expressions = (Expressions)resultValue; | |
28 return new LuanFunction() { | |
29 public Object[] call(LuanState lua,Object[] args) throws LuanException { | |
30 return expressions.eval((LuanStateImpl)lua); | |
31 } | |
32 }; | |
33 } | |
34 Chunk chunk = (Chunk)resultValue; | |
35 return chunk.newClosure((LuanStateImpl)lua); | |
36 } | |
37 | |
38 public static LuanState newLuaState() { | |
39 return new LuanStateImpl(); | |
40 } | |
41 } |