rev 726 - trunk/src

SVN User <[email protected]> Mon, 12 Jul 2004 21:52:32 -0400
Newsgroups gmane.comp.lang.prothon.cvs
Message-ID <[email protected]>
Author: mark
Date: 2004-07-12 21:52:29 -0400 (Mon, 12 Jul 2004)
New Revision: 726

Modified:
   trunk/src/bytecodes.h
   trunk/src/interp.c
   trunk/src/parser.h
   trunk/src/parser_routines.c
   trunk/src/prothon.y
Log:
at-sign debug print finished

Modified: trunk/src/bytecodes.h
===================================================================
--- trunk/src/bytecodes.h	2004-07-12 19:37:45 UTC (rev 725)
+++ trunk/src/bytecodes.h	2004-07-13 01:52:29 UTC (rev 726)
@@ -248,6 +248,13 @@
 // |opcode|symbol|
 OP_CMDCALL,
 
+// HDR_PRT (hdr string)
+// print hdr string and value on top of stack, leave value on stack
+// stack:  value -> value
+// param: unused
+// |opcode|hdr string object|
+OP_HDR_PRT,
+
 /********/ OP_TWO_WORDS_BOUNDARY, /********/ 
 
 // CLRSP

Modified: trunk/src/interp.c
===================================================================
--- trunk/src/interp.c	2004-07-12 19:37:45 UTC (rev 725)
+++ trunk/src/interp.c	2004-07-13 01:52:29 UTC (rev 726)
@@ -1709,6 +1709,9 @@
 				set_attr(ist, ist->module, fr_data(param-1), obj);
 				un_su(i);
 			}	break;
+			case OP_HDR_PRT:
+				printf("%s%s\n", as_str(ist, fr_data(1)), as_str(ist, fr_tos));
+				break;
 			default: {
 				raise_exception(ist, OBJ(INTERNAL_EXC), "Bad opcode: %d", op);
 			}
@@ -2256,6 +2259,8 @@
 		pr_assert(param);
 		fprintf(fout,"%4d %20s(%4d) %s", pc, "OP_NOP", param, codedata(str, code, op, param, pc));
 		break;
+	case OP_HDR_PRT: fprintf(fout,"%4d %20s(%4d) %s", pc, "OP_HDR_PRT", param, codedata(str, code, op, param, pc));
+		break;
 	case OP_CLRSP: fprintf(fout,"%4d %20s(%4d) %s", pc, "OP_CLRSP", param, codedata(str, code, op, param, pc));
 		break;
 	case OP_IMPORT: fprintf(fout,"%4d %20s(%4d) %s", pc, "OP_IMPORT", param, codedata(str, code, op, param, pc));

Modified: trunk/src/parser.h
===================================================================
--- trunk/src/parser.h	2004-07-12 19:37:45 UTC (rev 725)
+++ trunk/src/parser.h	2004-07-13 01:52:29 UTC (rev 726)
@@ -84,11 +84,8 @@
 	int			cmp_expr;
 	char*		file_path;
 	clist_p		srcmap;
+	clist_p		src_objs;
 	int			doc_flg;
-	int			min_line;
-	int			min_col;
-	int			max_line;
-	int			max_col;
 	code_t		code_data[];
 } code;
 
@@ -107,6 +104,8 @@
 	int			indents_needed;
 	int			last_line_colon;
 	clist_p     indent_stack;
+	char		token_buf[120];
+	int			token_buf_ptr;
 	FILE*		stream;
 	int			char_ptr;
 	char		old_chars[10];
@@ -177,7 +176,7 @@
 code_p expr_to_param(void* param, code_p expr);
 code_p expr_to_slice(void* param, code_p expr);
 code_p expr_to_sparm(void* param, code_p p1);
-code_p float_to_obj(void* param, double num, obj_p int_obj, int imag_flag);
+code_p float_to_obj(void* param, obj_p num, obj_p int_obj, int imag_flag);
 code_p expr_for_lcc_to_listcomp(void* param, code_p expr, clist_p forlst, clist_p lcc);
 code_p for_lcc_body_else(void* param, clist_p locals, code_p body, code_p els);
 code_p from_path_import_params(void* param, clist_p path, clist_p iparms);

Modified: trunk/src/parser_routines.c
===================================================================
--- trunk/src/parser_routines.c	2004-07-12 19:37:45 UTC (rev 725)
+++ trunk/src/parser_routines.c	2004-07-13 01:52:29 UTC (rev 726)
@@ -79,24 +79,25 @@
 #define debug_retrn(IST, line_no,res)  res
 #endif
 
-#define IST (((parse_state*) param)->ist)
+#define state ((parse_state*) param)
+#define IST   (state->ist)
 
 //********************************* new_parse_state ***************************
 parse_state* new_parse_state(isp ist, FILE* stream, char* in_buf) {
-	parse_state* state = pr_malloc(sizeof(parse_state));
-	memset(state, 0, sizeof(parse_state));
-	if (!state){raise_exception(ist, OBJ(OUTOFMEMORY_EXC), NULL); return NULL;}
-	state->ist = ist;
-	state->line = 1;
-	state->column = 1;
-	state->old_column = 1;
-	state->stream = stream;
-	state->in_buf = in_buf;
-	state->num_errors = -1;
-	state->assert_enbld = TRUE;
-	state->indent_stack = new_clist(10);
-	state->const_objs   = new_clist(10);
-	return state;
+	parse_state* statep = pr_malloc(sizeof(parse_state));
+	memset(statep, 0, sizeof(parse_state));
+	if (!statep){raise_exception(ist, OBJ(OUTOFMEMORY_EXC), NULL); return NULL;}
+	statep->ist = ist;
+	statep->line = 1;
+	statep->column = 1;
+	statep->old_column = 1;
+	statep->stream = stream;
+	statep->in_buf = in_buf;
+	statep->num_errors = -1;
+	statep->assert_enbld = TRUE;
+	statep->indent_stack = new_clist(10);
+	statep->const_objs   = new_clist(10);
+	return statep;
 }
 
 //********************************* parse_file_or_string **********************
@@ -177,54 +178,24 @@
 
 //********************************* add_to_const_list *************************
 #define add_to_const_list(param, obj)  \
-	clist_append(((parse_state*)param)->const_objs, obj);
+	clist_append(state->const_objs, obj);
 
 
 //********************************* loc routines ******************************
 void get_loc(isp ist, code_p res, obj_p obj) {
-	obj_p list_loc;
-	if (!obj) return;
-	if (list_loc = get_attr(ist, obj, SYM(SRCLOC_))) {
-		res->min_line = int2i32t(ist, list_item(ist, list_loc, 0));
-		res->min_col  = int2i32t(ist, list_item(ist, list_loc, 1));
-		res->max_line = int2i32t(ist, list_item(ist, list_loc, 2));
-		res->max_col  = int2i32t(ist, list_item(ist, list_loc, 3));
-	}
+	obj_p str_obj;
+	if (res && obj && (str_obj = get_attr(ist, obj, SYM(SRCLOC_))))
+		clist_append(res->src_objs, str_obj);
 }
 
 void merge_loc(code_p dst, code_p src) {
-	if (!src) return;
-	if ( src->max_line  > dst->max_line ||
-		 src->max_line == dst->max_line && src->max_col > dst->max_col ) {
-		dst->max_line  = src->max_line;
-		dst->max_col   = src->max_col;
+	if (src && dst) {
+		int i, llen = clist_len(src->src_objs);
+		for (i=0; i < llen; i++)
+			clist_append(dst->src_objs, clist_item(src->src_objs, i));
 	}
-	if ( src->min_line  < dst->min_line ||
-		 src->min_line == dst->min_line && src->min_col < dst->min_col ) {
-		dst->min_line  = src->min_line;
-		dst->min_col   = src->min_col;
-	}
 }
 
-void get_merge_loc(isp ist, code_p dst, obj_p obj) {
-	obj_p list_loc;
-	if (!obj) return;
-	if (list_loc = get_attr(ist, obj, SYM(SRCLOC_))) {
-		if ( int2i32t(ist, list_item(ist, list_loc, 2))  > dst->max_line ||
-			 int2i32t(ist, list_item(ist, list_loc, 2)) == dst->max_line && 
-			 int2i32t(ist, list_item(ist, list_loc, 3))  > dst->max_col     ) {
-			dst->max_line  = int2i32t(ist, list_item(ist, list_loc, 2));
-			dst->max_col   = int2i32t(ist, list_item(ist, list_loc, 3));
-		}
-		if ( int2i32t(ist, list_item(ist, list_loc, 0))  < dst->min_line ||
-			 int2i32t(ist, list_item(ist, list_loc, 0)) == dst->min_line && 
-			 int2i32t(ist, list_item(ist, list_loc, 1))  < dst->min_col     ) {
-			dst->min_line  = int2i32t(ist, list_item(ist, list_loc, 0));
-			dst->min_col   = int2i32t(ist, list_item(ist, list_loc, 1));
-		}
-	}
-}
-
 //********************************* new_code **********************************
 code_p new_code(void* param, int size, int stk, int max_stk, opcode_t opcode) {
 	data_size_t data_size = sizeof(code)+size*sizeof(code_t);
@@ -237,14 +208,11 @@
 	p->stack_depth     = stk;
 	p->max_stack_depth = max_stk;
 	if(size) p->code_data[0].bytecode.opcode = opcode;
-	p->srcmap = new_clist(16);
+	p->srcmap   = new_clist(16);
+	p->src_objs = new_clist(4);
 	clist_append_num(p->srcmap, 0);
 	clist_append_num(p->srcmap, ((parse_state*) param)->line);
 	clist_append_num(p->srcmap, ((parse_state*) param)->column);
-	p->min_line = ((parse_state*) param)->line;
-	p->min_col  = ((parse_state*) param)->column;
-	p->max_line = ((parse_state*) param)->line;
-	p->max_col  = ((parse_state*) param)->column;
 	return debug_retrn(IST, __LINE__, p);
 }
 
@@ -321,8 +289,34 @@
 }
 
 code_p atdebug(void* param, code_p parm) {
-	printf ("@max_line:%d, max_col:%d, min_line:%d, min_col:%d\n",
-		     parm->max_line, parm->max_col, parm->min_line, parm->min_col);
+	data_size_t data_size;
+	int i, llen = clist_len(parm->src_objs);
+	obj_p str_obj;
+	char buf[256], *dst, *end = buf+255;
+	apr_snprintf(buf, sizeof(buf), "@debug file %s, line ", parm->file_path);
+	dst = buf + strlen(buf);
+	for (i=0; i < llen; i++) {
+		char *str = pr2c_strptr(IST, clist_item(parm->src_objs, i)), *src=str;
+		if (i == 0) {
+			while ((*(dst++) = *(src++)) != ' ');
+			while (*src && *src != '@') src++;
+		} else
+			while(*(src++) != ' ');
+		for (; *src && dst < end; src++)
+			if (*src != '@') *(dst++) = *src;
+		pr_free(str);
+	}
+	*dst = 0;
+	if (buf + 4 < end)  strcat(buf, " => ");
+	str_obj = new_string_obj(IST, buf);
+	//printf("%s\n",buf);
+	data_size = sizeof(code) + (parm->len+2)*sizeof(code_t);
+	parm = pr_realloc(parm, data_size);
+	parm->size = data_size;
+	parm->len += 2;
+	parm->stack_depth = 1;
+	parm->code_data[parm->len-2].bytecode.opcode = OP_HDR_PRT;
+	parm->code_data[parm->len-1].data            = str_obj;
 	return debug_retrn(IST, __LINE__, parm);
 }
 
@@ -513,7 +507,7 @@
 	obj->code_data[obj->len-1].data = sym(IST, pr2c_strptr(IST, label));
 	obj->stack_depth += 1;
 	obj->max_stack_depth = max(obj->max_stack_depth, obj->stack_depth);
-	get_merge_loc(IST, obj, label);
+	get_loc(IST, obj, label);
 	return debug_retrn(IST, __LINE__, obj);
 }
 code_p attrref_to_tgtparm(void* param, code_p p1){
@@ -677,7 +671,7 @@
 	expr->code_data[expr->len-2].bytecode.opcode = OP_POP;
 	expr->code_data[expr->len-2].bytecode.param  = 1;
 	expr->code_data[expr->len-1].bytecode.opcode = OP_RETURN;
-	get_merge_loc(IST, expr, label);
+	get_loc(IST, expr, label);
 	return debug_retrn(IST, __LINE__, expr);
 }
 code_p lbl_proto_body_to_obj(void* param, obj_p label, code_p ref, clist_p protos, code_p body) {
@@ -1577,15 +1571,16 @@
 	res->code_data[1].data = num;
 	return debug_retrn(IST, __LINE__, res);
 }
-code_p float_to_obj(void* param, double num, obj_p int_obj, int imag_flag) {
+code_p float_to_obj(void* param, obj_p num, obj_p int_obj, int imag_flag) {
 	code_p res;
 	obj_p proto = (imag_flag == NEW_IMAG? OBJ(IMAG_PROTO) : OBJ(FLOAT_PROTO));
 	obj_p obj = new_object(IST, proto);
 	if (int_obj)
 		*((double*) obj_malloc(IST, proto, obj, sizeof(double))) = (double) int2i64t(IST, int_obj);
 	else
-		*((double*) obj_malloc(IST, proto, obj, sizeof(double))) = num;
+		*((double*) obj_malloc(IST, proto, obj, sizeof(double))) = num->data.f64;
 	res = new_code(param, 2, 1, 1, OP_PUSH);
+	get_loc(IST, res, num);
 	get_loc(IST, res, int_obj);
 	res->code_data[0].bytecode.param = 2;
 	res->code_data[1].data = obj;

Modified: trunk/src/prothon.y
===================================================================
--- trunk/src/prothon.y	2004-07-12 19:37:45 UTC (rev 725)
+++ trunk/src/prothon.y	2004-07-13 01:52:29 UTC (rev 726)
@@ -91,7 +91,7 @@
 
 %union {
 	obj_p		int_type;
-	double		float_type;
+	obj_p		float_type;
 	obj_p		str_type;
 	code_p		code_type;
 	clist_p		list_type;
@@ -546,7 +546,7 @@
 	|	CALLER														{ $$ = caller_to_obj(yylex_param); }
 	|	INT_														{ $$ = int_to_obj(yylex_param, $1); }
 	| 	FLOAT_														{ $$ = float_to_obj(yylex_param,  $1, NULL, NEW_REAL); }
-	| 	INT_IMAG													{ $$ = float_to_obj(yylex_param, 0.0,   $1, NEW_IMAG); }
+	| 	INT_IMAG													{ $$ = float_to_obj(yylex_param, NULL,   $1, NEW_IMAG); }
 	|	FLOAT_IMAG													{ $$ = float_to_obj(yylex_param,  $1, NULL, NEW_IMAG); }
 	| 	STRING														{ $$ = string_to_obj(yylex_param, $1); }
 	|   '$' LABEL							%prec '$'				{ $$ = rquote_lbl_to_obj(yylex_param, $2); }
@@ -671,16 +671,13 @@
 
 #include <ctype.h>
 
-void set_srcloc(isp ist, void* yylex_param, int token_line, int token_col, obj_p obj) {
-	obj_p list = new_list_obj(IST, 4);
+void set_srcloc(isp ist, parse_state* stat, obj_p obj) {
 	int immutable_flg;
-	list_append(IST, list, new_int_obj(IST, token_line));
-	list_append(IST, list, new_int_obj(IST, token_col));
-	list_append(IST, list, new_int_obj(IST, state->line));
-	list_append(IST, list, new_int_obj(IST, state->column));
-	immutable_flg = is_immutable(obj);
-	clr_immutable(obj);
-	set_attr(IST, obj, SYM(SRCLOC_), list);
+    obj_p str_obj = new_string_n_obj(ist, stat->token_buf, stat->token_buf_ptr);
+    stat->token_buf_ptr = 0;
+	if (immutable_flg = is_immutable(obj));
+		clr_immutable(obj);
+	set_attr(ist, obj, SYM(SRCLOC_), str_obj);
 	if (immutable_flg)
 		set_immutable(obj);
 } 
@@ -688,6 +685,7 @@
 // special get character function
 // automatically handle different format line ends (\r, \n, & \r\n -> \n)
 // increment lines and columns for YYLTYPE in parser
+// add to token_buf
 char getch(parse_state* stat){
 	char c;
 	if (stat->char_ptr) {
@@ -696,6 +694,7 @@
 			stat->line++;
 			stat->old_column = stat->column;
 			stat->column = 1;
+			stat->token_buf[stat->token_buf_ptr++] = ' ';
 			return '\n';
 		}
 	} else if (stat->stream) {
@@ -706,6 +705,7 @@
 			stat->line++;
 			stat->old_column = stat->column;
 			stat->column = 1;
+			stat->token_buf[stat->token_buf_ptr++] = ' ';
 			return '\n';
 		}
 	} else if (stat->in_buf && *(stat->in_buf)) {
@@ -716,16 +716,19 @@
 			stat->line++;
 			stat->old_column = stat->column;
 			stat->column = 1;
+			stat->token_buf[stat->token_buf_ptr++] = ' ';
 			return '\n';
 		}
 	} else c = EOF;
 	stat->column++;
+	stat->token_buf[stat->token_buf_ptr++] = c;
 	return c;
 }
 
 // special put-back character function
 // decrement lines and columns for YYLTYPE in parser
 void ungetch(char c, parse_state* stat){
+    stat->token_buf_ptr--;
 	stat->old_chars[stat->char_ptr++] = c;
 	if (c == '\n'){
 		stat->line--;
@@ -748,10 +751,13 @@
 
 int yylex (YYSTYPE *lvalp, void* yylex_param){
 	int c, indent_cnt=0, unknown_indent_flag=0, mixed_indents, level_mismatch;
+	double floatval;
 	
-	int token_line = state->line;
-	int token_col  = state->column;
-
+	if (!state->token_buf_ptr) {
+	    apr_snprintf(state->token_buf, 120, "%d: ", state->line);
+	    state->token_buf_ptr = strlen(state->token_buf);
+	}
+	
 	if (state->indents_needed > 0){
 		state->indents_needed--;
 		return INDENT;
@@ -1005,7 +1011,7 @@
 			lvalp->str_type = new_bytes_obj(IST, str_ptr, str_index-1);
 		else
 			lvalp->str_type = new_string_n_obj(IST, str_ptr, str_index-1);
-		set_srcloc(IST, yylex_param, token_line, token_col, lvalp->str_type);
+		set_srcloc(IST, yylex_param, lvalp->str_type);
 #ifdef TRACE_PARSER
 		if (trace_parser){
 			fprintf(((parse_state*) yylex_param)->debug_stream, "\"%s\"\n",str_ptr);
@@ -1196,7 +1202,7 @@
 
 		str_ptr = pr_realloc(str_ptr, str_index);
 		lvalp->str_type = new_string_n_obj(IST, str_ptr, str_index-1); 
-		set_srcloc(IST, yylex_param, token_line, token_col, lvalp->str_type);
+		set_srcloc(IST, yylex_param, lvalp->str_type);
 #ifdef TRACE_PARSER
 		if (trace_parser){
 			fprintf(((parse_state*) yylex_param)->debug_stream, "\"%s\"\n",str_ptr);
@@ -1288,7 +1294,7 @@
 					printf("Lexical error: invalid integer number format\n");
 					return ERR;
 				}
-				set_srcloc(IST, yylex_param, token_line, token_col, lvalp->int_type);
+				set_srcloc(IST, yylex_param, lvalp->int_type);
 				if (imag_flag)
 					return INT_IMAG;
 				else
@@ -1313,12 +1319,13 @@
 		}
 end_input:
 		str_ptr[str_index++]=0;	
-		lvalp->float_type = atof(str_ptr);
-		if (nonzero_flag && lvalp->float_type == 0.0){
+		floatval = atof(str_ptr);
+		if (nonzero_flag && floatval == 0.0) {
 			printf("Lexical error: invalid floating number format\n");
 			return ERR;
 		}
-		//set_srcloc(IST, yylex_param, token_line, token_col, lvalp->float_type);
+		lvalp->float_type = new_float_obj(IST, floatval);
+		set_srcloc(IST, yylex_param, lvalp->float_type);
 		if (imag_flag)
 			return FLOAT_IMAG;
 		else