rev 675 - in trunk: include/prothon pr/test src

SVN User <[email protected]> Tue, 29 Jun 2004 05:46:03 -0400
Newsgroups gmane.comp.lang.prothon.cvs
Message-ID <[email protected]>
Author: mark
Date: 2004-06-29 05:46:00 -0400 (Tue, 29 Jun 2004)
New Revision: 675

Modified:
   trunk/include/prothon/prothon.h
   trunk/pr/test/test.pr
   trunk/src/builtins-core.c
   trunk/src/bytecodes.h
   trunk/src/interp.c
   trunk/src/object.c
   trunk/src/parser.h
   trunk/src/parser_routines.c
   trunk/src/prothon.y
Log:
added prop keyword and Prop object

Modified: trunk/include/prothon/prothon.h
===================================================================
--- trunk/include/prothon/prothon.h	2004-06-29 04:52:08 UTC (rev 674)
+++ trunk/include/prothon/prothon.h	2004-06-29 09:46:00 UTC (rev 675)
@@ -333,6 +333,7 @@
 	THREAD_PROTO,		//   Thread
 	MUTEX_PROTO,		//   Mutex
 	PARMPTR_PROTO,		//
+	PROP_PROTO,			//
   
 // Exceptions
 	EXCEPTION,			//    Exception

Modified: trunk/pr/test/test.pr
===================================================================
--- trunk/pr/test/test.pr	2004-06-29 04:52:08 UTC (rev 674)
+++ trunk/pr/test/test.pr	2004-06-29 09:46:00 UTC (rev 675)
@@ -1,8 +1,7 @@
 #!/usr/bin/env prothon
 
-gen f(x):
-	for i in x:
-		yield i
-		
-list = List(f(10))
-print list
+prop x:
+	def get_():
+		return self
+
+print x

Modified: trunk/src/builtins-core.c
===================================================================
--- trunk/src/builtins-core.c	2004-06-29 04:52:08 UTC (rev 674)
+++ trunk/src/builtins-core.c	2004-06-29 09:46:00 UTC (rev 675)
@@ -78,6 +78,7 @@
 MODULE_DECLARE(Method);
 MODULE_DECLARE(RangeGen);
 MODULE_DECLARE(Slice);
+MODULE_DECLARE(Prop);
 MODULE_DECLARE(WeakRef);
 MODULE_DECLARE(UniqueObjects);
 MODULE_DECLARE(NoKey);
@@ -108,7 +109,7 @@
 }
 
 DEF(Object, call_, FORM_STAR3_PARAM) {
-	obj_p new_obj, init_obj;
+	obj_p new_obj;
 	parms[0] = OBJ(PARAM_STAR);  parms[2] = OBJ(PARAM_STAR_STAR);
 	new_obj = new_object(ist, self);
 	call_func(ist, new_obj, SYM(INIT_), 4, parms, NULL);
@@ -1192,6 +1193,20 @@
 	return parms[1];
 }
 
+// ***************************** PROP MODULE ********************************
+MODULE_START(Prop)
+{
+	Prop_OBJ = OBJ(PROP_PROTO);
+	MODULE_SET_DOC(Prop,  "Prop prototype object");
+	MODULE_ADD_TO_OBJ(Prop, OBJ(OBJECT), "Prop");
+	set_obj_id(Prop_OBJ, *, Object);
+	set_obj_doc(Prop_OBJ, "Prop object prototype");
+}
+
+DEF(Prop, str_, NULL) {
+	return NEW_STRING("<Prop>");
+}
+
 // ***************************** UNIQUEOBJECTS ********************************
 MODULE_START(UniqueObjects)
 {
@@ -1319,6 +1334,9 @@
 	MODULE_ADD_SYM(Slice, call_);
 	MODULE_ADD_SYM(Slice, str_);
 
+	MODULE_SUB_INIT(Prop);
+	MODULE_ADD_SYM(Prop, str_);
+
 	MODULE_SUB_INIT(WeakRef);
 	MODULE_ADD_SYM(WeakRef, init_);
 	MODULE_ADD_SYM(WeakRef, str_);

Modified: trunk/src/bytecodes.h
===================================================================
--- trunk/src/bytecodes.h	2004-06-29 04:52:08 UTC (rev 674)
+++ trunk/src/bytecodes.h	2004-06-29 09:46:00 UTC (rev 675)
@@ -160,6 +160,15 @@
 //***       The combined obj/reference represents attributes or data 
 //***		contained in the object that can be read from or written to.
 
+// PROP()
+// Push new execution frame identical to last except with default-local set to
+// a new object that will hold property methods.
+// New obj also stored at destination reference location on stack.
+// stack: dst-ref -> <empty>
+// param: <unused>
+// |opcode|func-obj|
+OP_PROP,
+
 // COMMAND(label)
 // call function as a command, label is name of function in local scope
 // stack: arg1-key, arg1-value,  arg2-key, arg2-value, ... -> result

Modified: trunk/src/interp.c
===================================================================
--- trunk/src/interp.c	2004-06-29 04:52:08 UTC (rev 674)
+++ trunk/src/interp.c	2004-06-29 09:46:00 UTC (rev 675)
@@ -1291,14 +1291,23 @@
 			}	break;
 no_func_ptr:	raise_exception(ist, OBJ(INTERNAL_EXC), "attempt to call an uninitialized function");
 				break;
+
+			case OP_PROP:
+				goto op_prop;
+
 			case OP_OBJ: {
-				int i, num_protos = (int)(intptr_t) fr_data(2);
+				int i, num_protos;
 				obj_p new_obj, key;
+				goto op_obj;
+
+op_prop:		num_protos = 0; 
+				fr_sp -= 2;
+				new_obj = NEW_OBJ(OBJ(PROP_PROTO));
+				goto op_obj_end;
+
+op_obj:			num_protos = (int)(intptr_t) fr_data(2);
 				fr_sp -= (num_protos+1)*2;
-#ifdef DUMP_MODULE_CODE
-				if (dump_module_code) dump_code_key = fr_stack[fr_sp+1];
-#endif
-				if (num_protos == 0) new_obj = new_object(ist, NULL);
+				if (num_protos == 0) new_obj = NEW_OBJ(NULL);
 				else {
 					obj_p psym = fr_stack[fr_sp+3];
 					obj_p proto = get_item(ist, fr_stack[fr_sp+2], psym); IF_EXC_BREAK;
@@ -1306,7 +1315,7 @@
 						raise_exception(ist, OBJ(NAME_EXC), "prototype %s not found", as_str(ist, psym));
 						goto end_op_obj;
 					}
-					new_obj = new_object(ist, proto);
+					new_obj = NEW_OBJ(proto);
 				}
 				for (i=1; i < num_protos; i++) {
 					obj_p psym = fr_stack[fr_sp+(i+1)*2+1];
@@ -1319,13 +1328,15 @@
 					add_proto(ist, new_obj, proto);
 					if (ist->exception_obj) goto end_op_obj;
 				}
+op_obj_end:
 				key = fr_stack[fr_sp+1];
+#ifdef DUMP_MODULE_CODE
+				if (dump_module_code) dump_code_key = key;
+#endif			
 				if (has_proto(ist, key, OBJ(LOCAL_PROTO))) {
 					key = key->data.ptr;
-					//del_unlock(fr_stack[fr_sp+1]);
 				}
 				set_attr(ist, fr_stack[fr_sp], key, new_obj); IF_EXC_BREAK;
-				//del_unlock(new_obj);
 				fr_push(new_obj);
 				goto op_with;
 			}
@@ -2185,6 +2196,8 @@
 		break;
 	case OP_OBJ: fprintf(fout,"%4d %20s(%4d) %s", pc, "OP_OBJ", param, codedata(str, code, op, param, pc));
 		break;
+	case OP_PROP: fprintf(fout,"%4d %20s(%4d) %s", pc, "OP_PROP", param, codedata(str, code, op, param, pc));
+		break;
 	case OP_WITH: fprintf(fout,"%4d %20s(%4d) %s", pc, "OP_WITH", param, codedata(str, code, op, param, pc));
 		break;
 	case OP_OBJCALL: fprintf(fout,"%4d %20s(%4d) %s", pc, "OP_OBJCALL", param, codedata(str, code, op, param, pc));

Modified: trunk/src/object.c
===================================================================
--- trunk/src/object.c	2004-06-29 04:52:08 UTC (rev 674)
+++ trunk/src/object.c	2004-06-29 09:46:00 UTC (rev 675)
@@ -233,6 +233,7 @@
 	OBJ(THREAD_PROTO)       = NEW_OBJ(NULL);
 	OBJ(MUTEX_PROTO)        = NEW_OBJ(NULL);
 	OBJ(PARMPTR_PROTO)      = NEW_OBJ(NULL);
+	OBJ(PROP_PROTO)         = NEW_OBJ(NULL);
 	OBJ(ROOT_GLOBALS)       = NEW_OBJ(NULL);
 	OBJ(MODULES)            = NEW_OBJ(NULL);
 	OBJ(EXCEPTION)          = NEW_OBJ(NULL);

Modified: trunk/src/parser.h
===================================================================
--- trunk/src/parser.h	2004-06-29 04:52:08 UTC (rev 674)
+++ trunk/src/parser.h	2004-06-29 09:46:00 UTC (rev 675)
@@ -184,7 +184,7 @@
 code_p label_eq_expr_to_param(void* param, obj_p label, code_p expr);
 code_p label_to_attrref(void* param, obj_p label);
 code_p label_to_formparm(void* param, obj_p label);
-code_p lbl_proto_body_to_obj(void* param, code_p label, clist_p protos, code_p body);
+code_p lbl_proto_body_to_obj(void* param, obj_p label, code_p ref, clist_p protos, code_p body);
 code_p new_dict(void* param, clist_p lst);
 code_p new_tuple_list(void* param, clist_p lst, int tuple_flag);
 code_p none(void* param);

Modified: trunk/src/parser_routines.c
===================================================================
--- trunk/src/parser_routines.c	2004-06-29 04:52:08 UTC (rev 674)
+++ trunk/src/parser_routines.c	2004-06-29 09:46:00 UTC (rev 675)
@@ -613,30 +613,42 @@
 	expr->code_data[expr->len-1].bytecode.opcode = OP_RETURN;
 	return debug_retrn(__LINE__, expr);
 }
-code_p lbl_proto_body_to_obj(void* param, code_p ref, clist_p protos, code_p body) {
+code_p lbl_proto_body_to_obj(void* param, obj_p label, code_p ref, clist_p protos, code_p body) {
 	obj_p func;
 	code_p res;
 	int code_len, llen = 0;
 	int i, k=0, len=0, stack_depth=0, max_stack_depth=0;
+	if (label)
+		{ len += 2; stack_depth += 2; max_stack_depth += 2; }
 	if (protos)
 		llen = clist_len(protos);
-	calc_code(ref, &len, &stack_depth, &max_stack_depth);
+	if (ref)
+		calc_code(ref, &len, &stack_depth, &max_stack_depth);
 	for (i=0; i < llen; i++)
 		calc_code(clist_item(protos, i),&len, &stack_depth, &max_stack_depth);
-	len += 3;
+	len += (label ? 2 : 3);
 	res = new_code(param, len, stack_depth, max_stack_depth, 0);
-	add_code(ref, res, &k);
+	if (label)
+		res->code_data[k++].bytecode.opcode = OP_PUSH_LOCAL_REF;
+		res->code_data[k++].data = sym(IST, pr2c_strptr(IST, label));
+	if (ref)
+		add_code(ref, res, &k);
 	for (i=0; i < llen; i++)
 		add_code(clist_item(protos, i), res, &k);
-	res->code_data[k  ].bytecode.opcode = OP_OBJ;
-	res->code_data[k++].bytecode.param = 3;
+	if (label)
+		res->code_data[k++].bytecode.opcode = OP_PROP;
+	else {
+		res->code_data[k  ].bytecode.opcode = OP_OBJ;
+		res->code_data[k++].bytecode.param = 3;
+	}
 	func = new_object(IST, OBJ(FUNC_PROTO));
 	code_len = sizeof(code) + body->len * sizeof(code_t);
 	memmove(obj_malloc(IST, OBJ(FUNC_PROTO), func, code_len), body, code_len);
 	res->code_data[k++].data = func;
-	res->code_data[k++].num  = llen;
+	if (!label)
+		res->code_data[k++].num  = llen;
 	pr_assert(k = len);
-	res->stack_depth -= (llen+1)*2-1;
+	res->stack_depth = 0;
 	return res;
 }
 code_p star_ref_to_formparm(void* param, obj_p label){

Modified: trunk/src/prothon.y
===================================================================
--- trunk/src/prothon.y	2004-06-29 04:52:08 UTC (rev 674)
+++ trunk/src/prothon.y	2004-06-29 09:46:00 UTC (rev 675)
@@ -121,6 +121,7 @@
 %nonassoc OBJ_      
 %nonassoc OUTER      
 %nonassoc PASS      
+%nonassoc PROP      
 %nonassoc RAISE     
 %nonassoc RETURN    
 %nonassoc SELF    
@@ -232,7 +233,7 @@
 			def_statement import_statement small_statement
 			if_statement while_statement for_statement
 			try_except_statement except else_except
-			try_finally_statement
+			try_finally_statement list_comp	 attr_ref def_ref gen_ref obj_ref
 			assert_statement target_param unbound_ref bound_ref
 			ass_add_statement ass_sub_statement
 			ass_mul_statement ass_div_statement
@@ -243,8 +244,8 @@
 			else slice slice_param formal_param is_not_check
 			function_param break_statement return_statement
 			yield_statement with_statement continue_statement
-			del_statement exec_statement attr_ref def_ref gen_ref obj_ref
-			raise_statement obj_statement command_statement list_comp					
+			del_statement exec_statement command_statement 
+			raise_statement prop_statement obj_statement				
 					
 /* Grammar */
 %%
@@ -270,7 +271,8 @@
 	;
 compound_statement: 
 		if_statement 		 | while_statement       |  def_statement    | with_statement |
-		try_except_statement | try_finally_statement |  for_statement	 | obj_statement
+		try_except_statement | try_finally_statement |  for_statement	 | obj_statement  |
+		prop_statement
 	;
 small_statement: 
 		assignment_statement | assert_statement  | import_statement   |	
@@ -383,9 +385,12 @@
 with_statement:
 		WITH obj compound_body										{ $$ = ref_parm_body_to_def(yylex_param, NULL, NULL, $2, $3, WTH_FLG); }						
 	;
+prop_statement:
+		PROP LABEL compound_body									{ $$ = lbl_proto_body_to_obj(yylex_param, $2, NULL, NULL, $3); }
+	;
 obj_statement:	
-		obj_ref  compound_body										{ $$ = lbl_proto_body_to_obj(yylex_param, $1, NULL, $2); }
-	|	obj_ref '('  protos ')' compound_body						{ $$ = lbl_proto_body_to_obj(yylex_param, $1, $3, $5); }
+		obj_ref  compound_body										{ $$ = lbl_proto_body_to_obj(yylex_param, NULL, $1, NULL, $2); }
+	|	obj_ref '('  protos ')' compound_body						{ $$ = lbl_proto_body_to_obj(yylex_param, NULL, $1,   $3, $5); }
 	;
 obj_ref:
 		OBJ_ LABEL													{ $$ = label_to_attrref(yylex_param, $2);		state->defdoc_flag=1; }
@@ -1158,6 +1163,7 @@
 		if (!strcmp(str_ptr,"object")) 	return OBJ_;
 		if (!strcmp(str_ptr,"outer")) 	return OUTER;
 		if (!strcmp(str_ptr,"pass")) 	return PASS;
+		if (!strcmp(str_ptr,"prop")) 	return PROP;
 		if (!strcmp(str_ptr,"raise")) 	return RAISE;
 		if (!strcmp(str_ptr,"self")) 	return SELF;
 		if (!strcmp(str_ptr,"super")) 	return SUPER;