Java Swing - LuaApi
package jlib5.java; import javax.script.ScriptEngine; import javax.script.ScriptEngineFactory; import javax.script.ScriptEngineManager; import javax.script.ScriptException; public class LuaApi { ScriptEngineManager sem ; ScriptEngine e; ScriptEngineFactory f; public LuaApi() { sem = new ScriptEngineManager(); e = sem.getEngineByName("luaj"); f = e.getFactory(); } public void showVersion() { System.out.println( "Engine name: " +f.getEngineName() ); System.out.println( "Engine Version: " +f.getEngineVersion() ); System.out.println( "LanguageName: " +f.getLanguageName() ); System.out.println( "Language Version: " +f.getLanguageVersion() ); } public void set(String name, int value ) { e.put( name, value ); } public Object get(String name) { return e.get( name ); } public Object eval(String statement ) { try { return e.eval(statement); } catch (ScriptException ex) { ex.printStackTrace(); } return null; } public boolean checkCondition(String statement) { String lua_statement = "if " + statement + " then rv=1 else rv=0 end"; eval( lua_statement ); int rv = (int)get("rv"); return rv != 0; } public boolean checkJavaCondition(String statement) { String lua_statement = statement.replace("&&", " and " ) .replace( "~=", "!=") .replace( "||", " or "); return checkCondition(lua_statement); } public static void main(String[] args) { LuaApi lua = new LuaApi(); lua.showVersion(); lua.set("a",1); lua.set("b",2); System.out.println( lua.checkCondition("a == 1 and b == 2") ); System.out.println( lua.checkCondition("a == 1 and b == 1") ); } }
댓글 없음:
댓글 쓰기