changeset 118:735708619119

add Debug.debug() git-svn-id: https://luan-java.googlecode.com/svn/trunk@119 21e917c8-12df-6dd8-5cb6-c86387c605b9
author fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
date Sun, 01 Jun 2014 07:07:31 +0000
parents e935581cf9fb
children f1bf2890d80f
files src/luan/LuanTable.java src/luan/init.luan src/luan/interp/LuanParser.java src/luan/lib/BasicLib.java src/luan/lib/IoLib.java src/luan/lib/TableLib.java src/luan/parser/ParseException.java src/luan/parser/Parser.java
diffstat 8 files changed, 90 insertions(+), 24 deletions(-) [+]
line wrap: on
line diff
--- a/src/luan/LuanTable.java	Fri May 30 08:57:24 2014 +0000
+++ b/src/luan/LuanTable.java	Sun Jun 01 07:07:31 2014 +0000
@@ -23,16 +23,30 @@
 
 	public LuanTable(List<Object> list) {
 		this.list = list;
+		this.map = new HashMap<Object,Object>();
+		map.put("n",list.size());
+		for( int i=0; i<list.size(); i++ ) {
+			if( list.get(i) == null ) {
+				listToMap(i);
+				break;
+			}
+		}
 	}
 
 	public LuanTable(Map<Object,Object> map) {
+		map.remove(null);
+		for( Iterator<Object> i=map.values().iterator(); i.hasNext(); ) {
+			if( i.next() == null )
+				i.remove();
+		}
 		this.map = map;
 	}
 
 	public LuanTable(Set<Object> set) {
 		map = new HashMap<Object,Object>();
 		for( Object obj : set ) {
-			map.put(obj,Boolean.TRUE);
+			if( obj != null )
+				map.put(obj,Boolean.TRUE);
 		}
 	}
 
@@ -200,10 +214,8 @@
 				} else if( i>=0 && i<list.size() ) {
 					Object old = list.get(i);
 					list.set(i,val);
-					if( val == null && i == list.size()-1 ) {
-						while( i>=0 && list.get(i)==null ) {
-							list.remove(i--);
-						}
+					if( val == null ) {
+						listToMap(i);
 					}
 					return old;
 				}
@@ -234,6 +246,20 @@
 		}
 	}
 
+	private void listToMap(int from) {
+		if( list != null ) {
+			while( list.size() > from ) {
+				int i = list.size() - 1;
+				Object v = list.remove(i);
+				if( v != null ) {
+					if( map==null )
+						map = new HashMap<Object,Object>();
+					map.put(i+1,v);
+				}
+			}
+		}
+	}
+
 	private List<Object> list() {
 		if( list == null ) {
 			list = new ArrayList<Object>();
@@ -314,11 +340,11 @@
 			}
 		};
 	}
-
+/*
 	public Object[] listToArray() {
 		return list==null ? new Object[0] : list.toArray();
 	}
-
+*/
 	public LuanTable subList(int from,int to) {
 		return new LuanTable(new ArrayList<Object>(list().subList(from-1,to-1)));
 	}
--- a/src/luan/init.luan	Fri May 30 08:57:24 2014 +0000
+++ b/src/luan/init.luan	Sun Jun 01 07:07:31 2014 +0000
@@ -1,6 +1,6 @@
 --Io.stdout.write "this is init.luan\n"
 
-function _G.print(...)
+local function print(...)
 	local list = {}
 	for v in Basic.values(...) do
 		list[#list+1] = to_string(v)
@@ -13,3 +13,24 @@
 		Io.stdout.write( Table.unpack(list) )
 	end
 end
+
+_G.print = print
+
+
+local Debug = {}
+Package.loaded.Debug = Debug
+_G.Debug = Debug
+
+function Debug.debug(prompt)
+	prompt = prompt or "luan_debug> "
+	local function console()
+		return Io.read_console_line(prompt)
+	end
+	for line in console do
+		local fn = load(line,"stdin",true)
+		local result = Table.pack( fn() )
+		if result.n > 0 then
+			print( Table.unpack(result,1,result.n) )
+		end
+	end
+end
--- a/src/luan/interp/LuanParser.java	Fri May 30 08:57:24 2014 +0000
+++ b/src/luan/interp/LuanParser.java	Sun Jun 01 07:07:31 2014 +0000
@@ -84,7 +84,7 @@
 	LuanParser(LuanSource source,UpValue.Getter envGetter) {
 		this.source = source;
 		this.frame = new Frame(envGetter);
-		this.parser = new Parser(source.text);
+		this.parser = new Parser(source);
 		this.interactive = envGetter==UpValue.globalGetter;
 	}
 
--- a/src/luan/lib/BasicLib.java	Fri May 30 08:57:24 2014 +0000
+++ b/src/luan/lib/BasicLib.java	Sun Jun 01 07:07:31 2014 +0000
@@ -88,7 +88,6 @@
 			return LuanCompiler.compileModule(luan,new LuanSource(sourceName,text));
 	}
 
-
 	public static LuanFunction load_file(LuanState luan,String fileName) throws LuanException {
 		try {
 			String src = fileName==null ? Utils.readAll(new InputStreamReader(System.in)) : Utils.read(new File(fileName));
--- a/src/luan/lib/IoLib.java	Fri May 30 08:57:24 2014 +0000
+++ b/src/luan/lib/IoLib.java	Sun Jun 01 07:07:31 2014 +0000
@@ -34,6 +34,7 @@
 				add( module, "java_resource_to_url", String.class );
 				add( module, "url", String.class );
 				add( module, "java_resource", String.class );
+				add( module, "read_console_line", String.class );
 
 				LuanTable stdin = new LuanTable();
 				stdin.put( "read_text", new LuanJavaFunction(
@@ -222,6 +223,12 @@
 		return url(java_resource_to_url(path));
 	}
 
+	public static String read_console_line(String prompt) throws IOException {
+		if( prompt==null )
+			prompt = "> ";
+		return System.console().readLine(prompt);
+	}
+
 
 	public interface LuanWriter {
 		public void write(LuanState luan,Object... args) throws LuanException, IOException;
--- a/src/luan/lib/TableLib.java	Fri May 30 08:57:24 2014 +0000
+++ b/src/luan/lib/TableLib.java	Sun Jun 01 07:07:31 2014 +0000
@@ -1,6 +1,7 @@
 package luan.lib;
 
 import java.util.Comparator;
+import java.util.List;
 import java.util.ArrayList;
 import java.util.Arrays;
 import luan.Luan;
@@ -27,7 +28,7 @@
 				add( module, "remove", LuanState.class, LuanTable.class, Integer.TYPE );
 				add( module, "sort", LuanState.class, LuanTable.class, LuanFunction.class );
 				add( module, "sub_list", LuanTable.class, Integer.TYPE, Integer.TYPE );
-				add( module, "unpack", LuanTable.class );
+				add( module, "unpack", LuanTable.class, Integer.class, Integer.class );
 			} catch(NoSuchMethodException e) {
 				throw new RuntimeException(e);
 			}
@@ -111,12 +112,18 @@
 		}
 	}
 
-	public static LuanTable pack(Object[] args) {
+	public static LuanTable pack(Object... args) {
 		return new LuanTable(new ArrayList<Object>(Arrays.asList(args)));
 	}
 
-	public static Object[] unpack(LuanTable list) {
-		return list.listToArray();
+	public static Object[] unpack(LuanTable tbl,Integer iFrom,Integer iTo) {
+		int from = iFrom!=null ? iFrom : 1;
+		int to = iTo!=null ? iTo : tbl.length();
+		List<Object> list = new ArrayList<Object>();
+		for( int i=from; i<=to; i++ ) {
+			list.add( tbl.get(i) );
+		}
+		return list.toArray();
 	}
 
 	public static LuanTable sub_list(LuanTable list,int from,int to) {
--- a/src/luan/parser/ParseException.java	Fri May 30 08:57:24 2014 +0000
+++ b/src/luan/parser/ParseException.java	Sun Jun 01 07:07:31 2014 +0000
@@ -1,14 +1,16 @@
 package luan.parser;
 
+import luan.LuanSource;
+
 
 public class ParseException extends Exception {
-	public final String text;
+	public final LuanSource src;
 	public final int iCurrent;
 	public final int iHigh;
 
-	ParseException(String msg,String text,int iCurrent,int iHigh) {
+	ParseException(String msg,LuanSource src,int iCurrent,int iHigh) {
 		super(msg);
-		this.text = text;
+		this.src = src;
 		this.iCurrent = iCurrent;
 		this.iHigh = iHigh;
 //System.out.println("iCurrent = "+iCurrent);
@@ -23,8 +25,8 @@
 			int line = 0;
 			int i = -1;
 			while(true) {
-				int j = text.indexOf('\n',i+1);
-				if( j == -1 || j > index )
+				int j = src.text.indexOf('\n',i+1);
+				if( j == -1 || j >= index )
 					break;
 				i = j;
 				line++;
@@ -35,13 +37,13 @@
 	}
 
 	private String[] lines() {
-		return text.split("\n",-1);
+		return src.text.split("\n",-1);
 	}
 
 	public String getFancyMessage() {
 		Location loc = new Location(iCurrent);
 		String line = lines()[loc.line];
-		String msg = getMessage() +  " (line " + (loc.line+1) + ", pos " + (loc.pos+1) + ")\n";
+		String msg = getMessage() +  " (line " + (loc.line+1) + ", pos " + (loc.pos+1) + ") in " + src.name + "\n";
 		StringBuilder sb = new StringBuilder(msg);
 		sb.append( line + "\n" );
 		for( int i=0; i<loc.pos; i++ ) {
--- a/src/luan/parser/Parser.java	Fri May 30 08:57:24 2014 +0000
+++ b/src/luan/parser/Parser.java	Sun Jun 01 07:07:31 2014 +0000
@@ -1,15 +1,19 @@
 package luan.parser;
 
+import luan.LuanSource;
+
 
 public final class Parser {
+	private final LuanSource src;
 	public final String text;
 	private final int len;
 	private int[] stack = new int[256];
 	private int frame = 0;
 	private int iHigh;
 
-	public Parser(String text) {
-		this.text = text;
+	public Parser(LuanSource src) {
+		this.src = src;
+		this.text = src.text;
 		this.len = text.length();
 	}
 
@@ -60,7 +64,7 @@
 	}
 
 	public ParseException exception(String msg) {
-		return new ParseException(msg,text,i(),iHigh);
+		return new ParseException(msg,src,i(),iHigh);
 	}
 
 	public ParseException exception() {