rev 431 - in trunk: . include/prothon pr src

SVN User <[email protected]> Thu, 29 Apr 2004 18:26:17 -0400
Newsgroups gmane.comp.lang.prothon.cvs
Message-ID <[email protected]>
Author: mark
Date: 2004-04-29 18:26:14 -0400 (Thu, 29 Apr 2004)
New Revision: 431

Modified:
   trunk/STATUS.txt
   trunk/include/prothon/prothon.h
   trunk/pr/closure.pr
   trunk/src/interp.c
   trunk/src/interp.h
   trunk/src/src.vcproj
   trunk/src/symbol.c
Log:
closure now works on unlimited nesting (see closure.pr)

Modified: trunk/STATUS.txt
===================================================================
--- trunk/STATUS.txt	2004-04-29 14:42:01 UTC (rev 430)
+++ trunk/STATUS.txt	2004-04-29 22:26:14 UTC (rev 431)
@@ -1,12 +1,9 @@
 
 ----------------------- TO-DO (highest priority first) ------------------------
 
---- Int(x) String("dsfga") etc
 
 --- binary space registering in object
 
---- object keyword
-
 --- Extend closures to all enclosing functions
 
 --- Bound methods - binding - calling

Modified: trunk/include/prothon/prothon.h
===================================================================
--- trunk/include/prothon/prothon.h	2004-04-29 14:42:01 UTC (rev 430)
+++ trunk/include/prothon/prothon.h	2004-04-29 22:26:14 UTC (rev 431)
@@ -369,9 +369,10 @@
 	__HASH__,
 	__FPARAMS__,
 	__GLOBALS__,
+	__SYN_FUNC__,
 	__SYN_LOCALS__,
 	__LOCALS__,
-	__SELF__,
+	__SYN_SELF__,
 	RPARAM,
 	PARAM1,
 	PARAM2,

Modified: trunk/pr/closure.pr
===================================================================
--- trunk/pr/closure.pr	2004-04-29 14:42:01 UTC (rev 430)
+++ trunk/pr/closure.pr	2004-04-29 22:26:14 UTC (rev 431)
@@ -1,17 +1,39 @@
 #!/usr/bin/env prothon
 
+print """
+The following should be:
+
+1 2 3 4 5 6 7 8 100
+1 2 3 4 7 6 13 23 200
+"""
 print
-print 'this should print 1 2 3'
 # tested example
-def getFunc():
-    counter = 0
-    def count():
-            &counter += 1
-            print &counter,
-    return count
-
-c = getFunc()
-c()     # prints 1
-c()     # prints 2
-c()     # prints 3
-print    
+def f1():
+	counter = 0
+	a=1
+	def f2():
+		c=3
+		def f3():
+			e=5
+			def f4():
+				g=7
+				def count():
+					print &a,&b,&c,&d,&e,&f,&g,&h,
+					&c = &a+&b
+					&e = &c+&d
+					&g = &e+&f
+					&h = &c+&e+&g
+					&counter += 100
+					print &counter
+				h=8
+				return count
+			f=6
+			return f4()
+		d=4
+		return f3()
+	b=2
+	return f2()
+	
+c = f1()
+c()    
+c()    

Modified: trunk/src/interp.c
===================================================================
--- trunk/src/interp.c	2004-04-29 14:42:01 UTC (rev 430)
+++ trunk/src/interp.c	2004-04-29 22:26:14 UTC (rev 431)
@@ -106,6 +106,8 @@
 	flen = sizeof(frame_t) + ( code ? (code->max_stack_depth+STACK_PADDING) * sizeof(obj_p) : 0 );
 	frame = pr_malloc(flen);
 	memset(frame, 0, flen);
+
+	if (func_obj) frame->func = func_obj;
 	frame->syn_self			= syn_self;
 	frame->self				= self;
 	frame->globals			= (func_obj ? get_attr(ist, func_obj, SYM(__GLOBALS__))    : globals);
@@ -782,15 +784,21 @@
 				}
 				fr_push(frame->dyn_locals);  
 				fr_push(fr_data(1)); 
-				break;
-			case OP_PUSH_SYN_REF:
-				if (!frame->syn_locals) {
-					raise_exception(ist, OBJ(INTERPRETER_EXC), "Syntactic access (&x) not available here");
-					break;
+sloc_break:		break;
+			case OP_PUSH_SYN_REF: {
+				obj_p func   = frame->func;
+				obj_p synloc = frame->syn_locals;
+				obj_p sym    = fr_data(1);
+				while (!synloc || !get_attr(ist, synloc, sym)) {
+					if (!func || !(synloc = get_attr(ist, func, SYM(__SYN_LOCALS__)))) {
+						raise_exception(ist, OBJ(INTERPRETER_EXC), "closure variable &%s not found", as_str(ist, sym));
+						goto sloc_break;
+					}
+					func = get_attr(ist, func, SYM(__SYN_FUNC__));
 				}
-				fr_push(frame->syn_locals);  
-				fr_push(fr_data(1)); 
-				break;
+				fr_push(synloc);  
+				fr_push(sym); 
+			}	break;
 			case OP_DEREF: {
 				obj_p ret;
 				fr_sp -= 2;
@@ -871,10 +879,11 @@
 					listlen(fparams_obj) = fparams_len;
 					set_attr(ist, func,    SYM(__FPARAMS__),  fparams_obj);
 				}
-				set_attr(ist, func,       SYM(__SELF__),  frame->self);
+				set_attr(ist, func,   SYM(__SYN_SELF__),  frame->self);
 				set_attr(ist, func,    SYM(__GLOBALS__),  frame->globals);
 				set_attr(ist, func, SYM(__SYN_LOCALS__),  frame->locals);
-				//set_attr(ist, func,   SYM(__SYN_FUNC__),  frame->func);
+				if (frame->func)
+					set_attr(ist, func,   SYM(__SYN_FUNC__),  frame->func);
 #ifdef DUMP_MODULE_CODE
 				{
 					char dump_name[256];
@@ -998,7 +1007,7 @@
 					frame_p new_frame;
 					IF_EXC_BREAK;
 					new_locals = NEW_OBJ(NULL);
-					syn_self = get_attr(ist, func_obj, SYM(__SELF__)); IF_EXC_BREAK;
+					syn_self = get_attr(ist, func_obj, SYM(__SYN_SELF__)); IF_EXC_BREAK;
 					new_frame = create_frame( ist, syn_self, frame->self, NULL, NULL,	
 											  frame->locals, new_locals, func_obj, NULL );
 					fr_next = new_frame;
@@ -1119,7 +1128,7 @@
 					frame_p new_frame;
 					IF_EXC_BREAK;
 					new_locals = NEW_OBJ(NULL);
-					syn_self = get_attr(ist, func_obj, SYM(__SELF__)); IF_EXC_BREAK;
+					syn_self = get_attr(ist, func_obj, SYM(__SYN_SELF__)); IF_EXC_BREAK;
 					new_frame = create_frame(	ist, syn_self,  super_flag? frame->self : fr_stack[fr_sp], NULL, NULL, 
 												frame->locals, new_locals, func_obj, NULL );
 					fr_next = new_frame;
@@ -1369,7 +1378,7 @@
 		}
 		frame = ist->frame;
 		new_locals = NEW_OBJ(NULL);
-		syn_self = get_attr(ist, func_obj, SYM(__SELF__));
+		syn_self = get_attr(ist, func_obj, SYM(__SYN_SELF__));
 		if (intrp_exobj) {
 			if (!func_sym) del_unlock(self);
 			return 0;
@@ -1638,7 +1647,7 @@
 	}
 	new_locals = NEW_OBJ(NULL);
 	set_attr(ist, thread_obj, SYM(__LOCALS__), new_locals);
-	syn_self = get_attr(ist, func_obj, SYM(__SELF__));
+	syn_self = get_attr(ist, func_obj, SYM(__SYN_SELF__));
 	new_frame = create_frame( ist, syn_self, NULL, thread_obj, NULL, NULL, new_locals, func_obj, NULL );
 	new_frame->called_from_c = TRUE;
 	new_frame->prev_frame = NULL;

Modified: trunk/src/interp.h
===================================================================
--- trunk/src/interp.h	2004-04-29 14:42:01 UTC (rev 430)
+++ trunk/src/interp.h	2004-04-29 22:26:14 UTC (rev 431)
@@ -75,6 +75,7 @@
 	int			called_from_c;
 	int			do_not_free;
 	int			gen_marker;
+	obj_p		func;
 	obj_p		syn_self;
 	obj_p		self;
 	obj_p		syn_locals;

Modified: trunk/src/src.vcproj
===================================================================
--- trunk/src/src.vcproj	2004-04-29 14:42:01 UTC (rev 430)
+++ trunk/src/src.vcproj	2004-04-29 22:26:14 UTC (rev 431)
@@ -247,77 +247,20 @@
 			Name="Resource Files"
 			Filter="rc;ico;cur;bmp;dlg;rc2;rct;bin;rgs;gif;jpg;jpeg;jpe">
 			<File
-				RelativePath="..\pr\chained.pr">
+				RelativePath="..\pr\closure.pr">
 			</File>
 			<File
-				RelativePath="..\pr\dbm.pr">
-			</File>
-			<File
-				RelativePath="..\pr\defs.pr">
-			</File>
-			<File
-				RelativePath="..\pr\dict.pr">
-			</File>
-			<File
-				RelativePath="..\pr\file_test.pr">
-			</File>
-			<File
-				RelativePath="..\pr\hex.pr">
-			</File>
-			<File
-				RelativePath="..\pr\init.pr">
-			</File>
-			<File
 				RelativePath="init.pth">
 			</File>
 			<File
-				RelativePath="..\pr\listcomp.pr">
-			</File>
-			<File
-				RelativePath="..\pr\meow.pr">
-			</File>
-			<File
-				RelativePath="..\pr\modint.pr">
-			</File>
-			<File
-				RelativePath="..\pr\object.pr">
-			</File>
-			<File
-				RelativePath="..\pr\oops.pr">
-			</File>
-			<File
-				RelativePath="..\pr\prosist.pr">
-			</File>
-			<File
 				RelativePath="prothon.y">
 			</File>
 			<File
-				RelativePath="..\pr\re_test.pr">
-			</File>
-			<File
-				RelativePath="..\pr\speed.pr">
-			</File>
-			<File
-				RelativePath="..\pr\sqlite.pr">
-			</File>
-			<File
 				RelativePath="..\status.txt">
 			</File>
 			<File
-				RelativePath="..\pr\stdio_test.pr">
-			</File>
-			<File
-				RelativePath="..\pr\strmod.pr">
-			</File>
-			<File
-				RelativePath="..\pr\system-test.pr">
-			</File>
-			<File
 				RelativePath="..\pr\test.pr">
 			</File>
-			<File
-				RelativePath="..\pr\threads.pr">
-			</File>
 		</Filter>
 	</Files>
 	<Globals>
Modified: trunk/src/symbol.c
===================================================================
--- trunk/src/symbol.c	2004-04-29 14:42:01 UTC (rev 430)
+++ trunk/src/symbol.c	2004-04-29 22:26:14 UTC (rev 431)
@@ -125,9 +125,10 @@
 	{ "__hash__",		0 },
 	{ "__fparams__",	0 },
 	{ "__globals__",    0 },
+	{ "__syn_func__",   0 },
 	{ "__syn_locals__",	0 },
 	{ "__locals__",		0 },
-	{ "__self__",		0 },
+	{ "__syn_self__",	0 },
 	{ "rparam",			0 },
 	{ "param1",			0 },
 	{ "param2",			0 },