Mercurial Hosting > luan
comparison src/luan/lib/BasicLib.java @ 115:eacf6ce1b47d
add IoLib
git-svn-id: https://luan-java.googlecode.com/svn/trunk@116 21e917c8-12df-6dd8-5cb6-c86387c605b9
author | fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9> |
---|---|
date | Thu, 29 May 2014 09:26:44 +0000 |
parents | f5af13062b10 |
children | 1ff1c32417eb |
comparison
equal
deleted
inserted
replaced
114:c599206448b9 | 115:eacf6ce1b47d |
---|---|
4 import java.io.InputStreamReader; | 4 import java.io.InputStreamReader; |
5 import java.io.IOException; | 5 import java.io.IOException; |
6 import java.lang.reflect.Method; | 6 import java.lang.reflect.Method; |
7 import java.util.Iterator; | 7 import java.util.Iterator; |
8 import java.util.Map; | 8 import java.util.Map; |
9 import java.util.List; | |
10 import java.util.ArrayList; | |
9 import luan.Luan; | 11 import luan.Luan; |
10 import luan.LuanState; | 12 import luan.LuanState; |
11 import luan.LuanTable; | 13 import luan.LuanTable; |
12 import luan.LuanFunction; | 14 import luan.LuanFunction; |
13 import luan.LuanJavaFunction; | 15 import luan.LuanJavaFunction; |
61 private static void add(LuanTable t,String method,Class<?>... parameterTypes) throws NoSuchMethodException { | 63 private static void add(LuanTable t,String method,Class<?>... parameterTypes) throws NoSuchMethodException { |
62 t.put( method, new LuanJavaFunction(BasicLib.class.getMethod(method,parameterTypes),null) ); | 64 t.put( method, new LuanJavaFunction(BasicLib.class.getMethod(method,parameterTypes),null) ); |
63 } | 65 } |
64 | 66 |
65 public static void print(LuanState luan,Object... args) throws LuanException { | 67 public static void print(LuanState luan,Object... args) throws LuanException { |
68 LuanFunction write = (LuanFunction)luan.get("Io.stdout.write"); | |
69 List list = new ArrayList(); | |
66 for( int i=0; i<args.length; i++ ) { | 70 for( int i=0; i<args.length; i++ ) { |
67 if( i > 0 ) | 71 if( i > 0 ) |
68 luan.out.print('\t'); | 72 list.add("\t"); |
69 luan.out.print( luan.JAVA.toString(args[i]) ); | 73 list.add(args[i]); |
70 } | 74 } |
71 luan.out.println(); | 75 list.add("\n"); |
76 write.call(luan,list.toArray()); | |
72 } | 77 } |
73 | 78 |
74 public static String type(Object obj) { | 79 public static String type(Object obj) { |
75 return Luan.type(obj); | 80 return Luan.type(obj); |
76 } | 81 } |
81 else | 86 else |
82 return LuanCompiler.compileModule(luan,new LuanSource(sourceName,text)); | 87 return LuanCompiler.compileModule(luan,new LuanSource(sourceName,text)); |
83 } | 88 } |
84 | 89 |
85 | 90 |
86 public static LuanFunction load_file(LuanState luan,String fileName) throws LuanException { | 91 public static LuanFunction load_file(LuanState luan,String fileName) throws LuanException, IOException { |
87 try { | 92 String src = fileName==null ? Utils.readAll(new InputStreamReader(System.in)) : Utils.read(new File(fileName)); |
88 String src = fileName==null ? Utils.readAll(new InputStreamReader(System.in)) : Utils.read(new File(fileName)); | 93 return load(luan,src,fileName,false); |
89 return load(luan,src,fileName,false); | 94 } |
90 } catch(IOException e) { | 95 |
91 throw luan.JAVA.exception(e); | 96 public static Object do_file(LuanState luan,String fileName) throws LuanException, IOException { |
92 } | |
93 } | |
94 | |
95 public static Object do_file(LuanState luan,String fileName) throws LuanException { | |
96 LuanFunction fn = load_file(luan,fileName); | 97 LuanFunction fn = load_file(luan,fileName); |
97 return luan.JAVA.call(fn,null); | 98 return luan.JAVA.call(fn,null); |
98 } | 99 } |
99 | 100 |
100 private static LuanFunction pairs(final Iterator<Map.Entry<Object,Object>> iter) { | 101 private static LuanFunction pairs(final Iterator<Map.Entry<Object,Object>> iter) { |