rev 660 - in trunk: . include/prothon pr/test src

SVN User <[email protected]> Thu, 24 Jun 2004 03:26:19 -0400
Newsgroups gmane.comp.lang.prothon.cvs
Message-ID <[email protected]>
Author: mark
Date: 2004-06-24 03:26:16 -0400 (Thu, 24 Jun 2004)
New Revision: 660

Modified:
   trunk/STATUS.txt
   trunk/include/prothon/prothon.h
   trunk/pr/test/test.pr
   trunk/src/builtins-core.c
   trunk/src/bytecodes.h
   trunk/src/console.c
   trunk/src/interp.c
   trunk/src/parser.h
   trunk/src/parser_routines.c
   trunk/src/prothon.y
Log:
removed print keyword, added Object.print method

Modified: trunk/STATUS.txt
===================================================================
--- trunk/STATUS.txt	2004-06-24 05:19:09 UTC (rev 659)
+++ trunk/STATUS.txt	2004-06-24 07:26:16 UTC (rev 660)
@@ -1,9 +1,6 @@
 
 ----------------------- TO-DO (highest priority first) ------------------------
 
---- print proposal
-	http://www.prothon.org/pipermail/prothon-user/2004-June/001970.html
-	http://www.prothon.org/pipermail/prothon-user/2004-June/001991.html
 --- pretty print
 	http://www.prothon.org/pipermail/prothon-user/2004-June/002112.html
 

Modified: trunk/include/prothon/prothon.h
===================================================================
--- trunk/include/prothon/prothon.h	2004-06-24 05:19:09 UTC (rev 659)
+++ trunk/include/prothon/prothon.h	2004-06-24 07:26:16 UTC (rev 660)
@@ -513,6 +513,7 @@
 	access_t	access;
 	void*		lock_stack;
 	frame_p		frame;
+	int			in_console;
 	obj_p		exception_obj;
 } interp_state_t;
  

Modified: trunk/pr/test/test.pr
===================================================================
--- trunk/pr/test/test.pr	2004-06-24 05:19:09 UTC (rev 659)
+++ trunk/pr/test/test.pr	2004-06-24 07:26:16 UTC (rev 660)
@@ -1,7 +1,4 @@
 #!/usr/bin/env prothon
 
-print Slice(99)
-print Slice(99,1)
-print Slice(99,1,2)
-
-print Object.attrs_.getItem_($cmp)
+x = print(Object, Modules, sep='\n', eol='\n\n')
+print (x)

Modified: trunk/src/builtins-core.c
===================================================================
--- trunk/src/builtins-core.c	2004-06-24 05:19:09 UTC (rev 659)
+++ trunk/src/builtins-core.c	2004-06-24 07:26:16 UTC (rev 660)
@@ -381,7 +381,43 @@
 	return self;
 }
 
+DEF(Object, print, FORM_STAR3_PARAM) {
+	obj_p res;
+	obj_p arg_list = parms[1];
+	obj_p arg_dict = parms[3];
+	int i, llen = (int) list_len(ist, arg_list);
+	char *sep, *eol, *str;
+	obj_p sep_obj = dict_item(ist, arg_dict, sym(ist, "sep"));
+	obj_p eol_obj = dict_item(ist, arg_dict, sym(ist, "eol"));
+	if (sep_obj) sep = pr2c_strptr(ist, sep_obj); else sep = " ";
+	if (eol_obj) eol = pr2c_strptr(ist, eol_obj); else eol = "\n";
+	str = pr_malloc(strlen(eol)+1);
+	str[0] = 0;
+	for (i=0; i < llen; i++)  {
+		obj_p str_obj, item = list_item(ist, arg_list, i);
+		char* item_str;
+		if (!item) item = OBJ(NONE);
+		str_obj = call_func0(ist, item, SYM(STR_));
+		if_exc_return NULL;
+		item_str = pr2c_strptr(ist, str_obj);
+		str = pr_realloc(str, strlen(str)+strlen(item_str)+strlen(sep)+strlen(eol)+1);
+		strcat(str, item_str);
+		if (i < llen-1) strcat(str, sep);
+		else            strcat(str, eol);
+	}
+	if (llen == 0) strcpy(str, eol);
+	printf("%s", str);
+	if (ist->in_console) {
+		pr_free(str);
+		return OBJ(NONE);
+	} else {
+		res = NEW_STRING(str);
+		pr_free(str);
+		return res;
+	}
+}
 
+
 // ***************************** SYMBOL *****************************************
 
 MODULE_START(Symbol)
@@ -1037,6 +1073,7 @@
 	MODULE_ADD_SYM(Object, range);
 	MODULE_ADD_SYM(Object, toStorProxy_);
 	MODULE_ADD_SYM(Object, fromStorProxy_);
+	MODULE_ADD_SYM(Object, print);
 
 	MODULE_SUB_INIT(Symbol);
 	MODULE_ADD_SYM(Symbol, init_);

Modified: trunk/src/bytecodes.h
===================================================================
--- trunk/src/bytecodes.h	2004-06-24 05:19:09 UTC (rev 659)
+++ trunk/src/bytecodes.h	2004-06-24 07:26:16 UTC (rev 660)
@@ -379,14 +379,6 @@
 // |opcode|
 OP_APPEND,
 
-// PRINT(objects)
-// print objects on top of stack
-// 0 at top-of-stack means no return at end of line
-// stack: object-id,object-id,...,optional zero -> <empty>
-// param: number of objects on stack
-// |opcode|
-OP_PRINT,
-
 // TRYEXCEPT
 // push try frame on exception stack
 // param: distance to first matching except opcode

Modified: trunk/src/console.c
===================================================================
--- trunk/src/console.c	2004-06-24 05:19:09 UTC (rev 659)
+++ trunk/src/console.c	2004-06-24 07:26:16 UTC (rev 660)
@@ -187,7 +187,8 @@
     char buf[16];
 	int ind_level = 0, colon_flg = FALSE;
 	char histfile[512], *home;
-
+	
+	ist->in_console = TRUE;
 	new_self = new_object(ist, NULL);
 	if (!ist->frame) {
 		new_locals  = NEW_OBJ(NULL);

Modified: trunk/src/interp.c
===================================================================
--- trunk/src/interp.c	2004-06-24 05:19:09 UTC (rev 659)
+++ trunk/src/interp.c	2004-06-24 07:26:16 UTC (rev 660)
@@ -83,8 +83,7 @@
 
 isp new_ist(access_t access) {
 	isp ist = pr_malloc(sizeof(interp_state_t));
-	ist->frame = NULL;
-	ist->exception_obj = 0;
+	memset(ist, 0, sizeof(interp_state_t));
 	ist->lock_stack = new_clist(20);
 	ist->access = access;
 	return ist;
@@ -1537,17 +1536,6 @@
 				IF_EXC_BREAK;
 				return_flag = fr_ccall;
 				break;
-			case OP_PRINT: {
-				int i;
-				fr_sp -= param;
-				for (i=0; i < param; i++) 
-					if (fr_stack[fr_sp+i]) {  // NOT STACKLESS XXX
-						obj_p str_obj = call_func0(ist, fr_stack[fr_sp+i], SYM(STR_));
-						IF_EXC_BREAK;
-						printf("%s ", pr2c_strptr(ist, str_obj));
-					} else if (i == param-1) goto break_loc;
-				printf("\n");
-			}   break;
 			case OP_IMPORT:    
 				import_module(ist, frame, param, TRUE, NULL, FALSE);
 				break;
@@ -2196,8 +2184,6 @@
 		break;
 	case OP_APPEND: fprintf(fout,"%4d %20s(%4d) %s", pc, "OP_APPEND", param, codedata(str, code, op, param, pc));
 		break;
-	case OP_PRINT: fprintf(fout,"%4d %20s(%4d) %s", pc, "OP_PRINT", param, codedata(str, code, op, param, pc));
-		break;
 	case OP_TRYEXCEPT: fprintf(fout,"%4d %20s(%4d) %s", pc, "OP_TRYEXCEPT", param, codedata(str, code, op, param, pc));
 		break;
 	case OP_EXCEPT: fprintf(fout,"%4d %20s(%4d) %s", pc, "OP_EXCEPT", param, codedata(str, code, op, param, pc));

Modified: trunk/src/parser.h
===================================================================
--- trunk/src/parser.h	2004-06-24 05:19:09 UTC (rev 659)
+++ trunk/src/parser.h	2004-06-24 07:26:16 UTC (rev 660)
@@ -189,7 +189,6 @@
 code_p obj_func_params(void* param, code_p obj, clist_p parms);
 code_p obj_label_to_attrref(void* param, code_p obj, obj_p label);
 code_p op_expr(int op, code_p expr, int size);
-code_p print_arglist(void* param, clist_p list);
 code_p push_null(void* param);
 code_p raise_expr(void* param, code_p expr1,  code_p expr2);
 code_p ref_expr_to_ass_op(void* param, code_p ref, code_p rparm, int iop, int op);

Modified: trunk/src/parser_routines.c
===================================================================
--- trunk/src/parser_routines.c	2004-06-24 05:19:09 UTC (rev 659)
+++ trunk/src/parser_routines.c	2004-06-24 07:26:16 UTC (rev 660)
@@ -1388,36 +1388,6 @@
 		set_attr(IST, func, SYM(DOC_), (obj_p)(body->code_data[1].data));
 	return debug_retrn(__LINE__, res);
 }
-code_p print_arglist(void* param, clist_p list){
-	code *res;
-	int i, k=0, len=0, stack_depth=0, max_stack_depth = 0;
-	for(i=0; i < clist_len(list); i++){
-		code_p p = (code_p)clist_item(list,i);
-		if (p)
-			calc_code(p, &len, &stack_depth, &max_stack_depth); 
-		else {
-			len += 2;
-			max_stack_depth = max(max_stack_depth, stack_depth + 1);
-			stack_depth++;
-		}
-	}
-	res = new_code(param, len+1, stack_depth, max_stack_depth, 0);
-	for(i=0; i < clist_len(list); i++){
-		code_p p = (code_p)clist_item(list,i);
-		if(p)
-			add_code(p, res, &k);
-		else {
-			res->code_data[k  ].bytecode.opcode = OP_PUSH;
-			res->code_data[k++].bytecode.param  = 2;
-			res->code_data[k++].data = NULL;
-		}
-	}
-	res->code_data[k  ].bytecode.opcode = OP_PRINT;
-	res->code_data[k++].bytecode.param  = clist_len(list);
-	res->stack_depth -= clist_len(list);
-	pr_assert(res->len == k);
-	return debug_retrn(__LINE__, res);
-}
 code_p new_tuple_list(void* param, clist_p lst, int tuple_flag){
 	int i, k=0, len=1, stack_depth=1, max_stack_depth=1;
 	code_p res;

Modified: trunk/src/prothon.y
===================================================================
--- trunk/src/prothon.y	2004-06-24 05:19:09 UTC (rev 659)
+++ trunk/src/prothon.y	2004-06-24 07:26:16 UTC (rev 660)
@@ -121,7 +121,6 @@
 %nonassoc OBJ_      
 %nonassoc OUTER      
 %nonassoc PASS      
-%nonassoc PRINT     
 %nonassoc RAISE     
 %nonassoc RETURN    
 %nonassoc SELF    
@@ -223,8 +222,8 @@
 			target_params func_params brace_params
 			brace_params2 brace_param mul_except ass_expr_list
 			formal_params elif mul_elif list_params
-			list_params2 arg_list for_refs for_refs1 for_refs2
-			arg_list2 tuple_params tuple_params2 for_lc_clause
+			list_params2 for_refs for_refs1 for_refs2
+			tuple_params tuple_params2 for_lc_clause
 			lc_clauses if_lc_clause protos proto
 
 %type <code_type>	statement_block statement mul_statement
@@ -233,7 +232,7 @@
 			def_statement import_statement small_statement
 			if_statement while_statement for_statement
 			try_except_statement except else_except
-			try_finally_statement print_statement
+			try_finally_statement
 			assert_statement target_param unbound_ref bound_ref
 			ass_add_statement ass_sub_statement
 			ass_mul_statement ass_div_statement
@@ -274,15 +273,15 @@
 		try_except_statement | try_finally_statement |  for_statement	 | obj_statement
 	;
 small_statement: 
-		expr		   	   | assignment_statement |
-		print_statement    | assert_statement  | import_statement   |	
-		ass_add_statement  | ass_sub_statement | ass_mul_statement  |
-		ass_div_statement  | ass_tdv_statement | ass_rem_statement  |
-		ass_band_statement | ass_bor_statement | ass_bxor_statement |
-		ass_shr_statement  | ass_shl_statement | ass_exp_statement  |
-		break_statement    | continue_statement| del_statement      |
-   		exec_statement	   | raise_statement   | return_statement   |
-   	    yield_statement    |
+		expr		   	     |
+		assignment_statement | assert_statement  | import_statement   |	
+		ass_add_statement    | ass_sub_statement | ass_mul_statement  |
+		ass_div_statement    | ass_tdv_statement | ass_rem_statement  |
+		ass_band_statement   | ass_bor_statement | ass_bxor_statement |
+		ass_shr_statement    | ass_shl_statement | ass_exp_statement  |
+		break_statement      | continue_statement| del_statement      |
+   		exec_statement	     | raise_statement   | return_statement   |
+   	    yield_statement      |
    	    PASS														{ $$ = none(yylex_param); }
 	;
 if_statement: 
@@ -489,9 +488,6 @@
 ass_exp_statement:
 		target_param EXP_EQ expr									{ $$ = ref_expr_to_ass_op(yylex_param, $1, $3, IPOW_, POW_); }
 	;
-print_statement:
-		PRINT arg_list												{ $$ = print_arglist(yylex_param, $2); }
-	;
 is_not_check:
 		/* empty string */											{ $$ = (void *)(unsigned long)IS__QUES; }
 	|	NOT															{ $$ = (void *)(unsigned long)ISNOT__QUES; }
@@ -603,15 +599,6 @@
 		/* empty string */											{ $$ = none(yylex_param); }
 	|	expr														{ $$ = expr_to_sparm(yylex_param, $1); }
 	;
-arg_list:
-		/* empty string */											{ $$ = empty_list(yylex_param); }
-	|   arg_list2													
-	|   arg_list2 ','												{ $$ = append_list(yylex_param, $1, NULL); }
-	;
-arg_list2:
-	 	expr														{ $$ = new_list(yylex_param, $1); }
-	|	arg_list2 ',' expr											{ $$ = append_list(yylex_param, $1, $3); }
-	;
 func_params:
 		/* empty string */											{ $$ = empty_list(yylex_param); }
 	|	function_param												{ $$ = new_list(yylex_param, $1); }
@@ -1169,7 +1156,6 @@
 		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,"print")) 	return PRINT;
 		if (!strcmp(str_ptr,"raise")) 	return RAISE;
 		if (!strcmp(str_ptr,"self")) 	return SELF;
 		if (!strcmp(str_ptr,"super")) 	return SUPER;