|
From: Peep P. <so...@us...> - 2004-07-24 18:02:14
|
Update of /cvsroot/agd/server/src In directory sc8-pr-cvs1.sourceforge.net:/tmp/cvs-serv450 Modified Files: compile.c Log Message: Removed debugging code; compile error handler (apply in master); some fixes to check_operand Index: compile.c =================================================================== RCS file: /cvsroot/agd/server/src/compile.c,v retrieving revision 1.30 retrieving revision 1.31 diff -u -d -r1.30 -r1.31 --- compile.c 23 Jul 2004 17:19:27 -0000 1.30 +++ compile.c 24 Jul 2004 18:02:05 -0000 1.31 @@ -17,8 +17,8 @@ #include "lang.h" extern YYLTYPE yylloc; - extern object_t *this_ob; +extern object_t *master; program_t *this_prog; function_t *curr_fun; @@ -109,6 +109,9 @@ case ID_GVAR: buf2 = "global variable"; break; + case ID_DFUN: + buf2 = "driver function"; + break; default: buf2 = "idontknowwhat!"; break; @@ -120,10 +123,16 @@ int redeclaration_allowed(def_id_t *target, int type) { + if(target->type == ID_DFUN) + if(type == ID_DFUN) + return 1; + else + return 0; + if(target->base_scope >= scope_level) return 0; - if(target->type == ID_FUN_PROT || target->type == ID_DFUN) + if(target->type == ID_FUN_PROT) return 0; return 1; @@ -382,6 +391,31 @@ return op->valid_types & x; } +int valid_check(operator_t *op, int opr1, int opr2) +{ + if(opr1 == T_ANY || opr2 == T_ANY) + return 1; + + if(op->num_operand == 2) { + if(op->both_same_type) { + if(opr1 != opr2) + return 0; + } else { + int oa1 = opr1 & T_ARRAY; + int oa2 = opr2 & T_ARRAY; + /* Can't have you mixing array and non-array operands. */ + if(oa1 != oa2) + return 0; + } + } + + if(!is_operand_valid(opr1, op)) + return 0; + if(op->num_operand == 2 && opr2 && !is_operand_valid(opr2, op)) + return 0; + return 1; +} + /* Checks if opr1 and opr2 are valid operands to operator. * Returns 0 if not. */ /* Operand 0 means don't do any checking. */ @@ -403,31 +437,28 @@ op = &operator_table[operator-F_ADD]; - /* Urgh. */ - if((opr1 != T_ANY && opr2 != T_ANY) && - (!is_operand_valid(opr1, op) || - (op->num_operand == 2 && opr2 - && !is_operand_valid(opr2, op)) - || (op->both_same_type && opr1 != opr2))) { - switch(op->num_operand) { - case 1: - sprintf(buf, "wrong type argument to unary "); - break; - case 2: - sprintf(buf, "invalid operands to binary "); - break; - case 3: - sprintf(buf, "invalid operands to trinary "); - break; - default: - comp_error("invalid num_operand in check_operand()"); - return; - } - sprintf(buf, "%s%s", buf, op->name); - comp_error(buf); - return 0; + if(valid_check(op, opr1, opr2)) + return 1; + + printf("opr1: %d; opr2: %d\n", opr1, opr2); + + switch(op->num_operand) { + case 1: + sprintf(buf, "wrong type argument to unary "); + break; + case 2: + sprintf(buf, "invalid operands to binary "); + break; + case 3: + sprintf(buf, "invalid operands to trinary "); + break; + default: + comp_error("invalid num_operand in check_operand()"); + return; } - return 1; + sprintf(buf, "%s%s", buf, op->name); + comp_error(buf); + return 0; } /* Post-compilation functions. */ @@ -442,7 +473,7 @@ } } -void add_function(def_id_t *idp, int lineno) +void add_function(def_id_t *idp) { if(idp->index < this_prog->numfunc) { /* Adding a definition for a prototype. */ @@ -455,7 +486,6 @@ f->args = xmalloc(sizeof(int) * f->num_arg); memcpy(f->args, idp->args, f->num_arg * sizeof(int)); #endif - f->lineno = lineno; f->line_numbers = NULL; f->codelen = curr_fun->codelen; f->code = xmalloc(sizeof(int) * f->codelen); @@ -464,9 +494,10 @@ curr_fun_check(); curr_fun->type = idp->lpc_type; curr_fun->num_arg = idp->num_arg; - curr_fun->args = xmalloc(sizeof(int) * curr_fun->num_arg); - memcpy(curr_fun->args, idp->args, curr_fun->num_arg); - curr_fun->lineno = lineno; + if(curr_fun->num_arg) { + curr_fun->args = xmalloc(sizeof(int) * curr_fun->num_arg); + memcpy(curr_fun->args, idp->args, curr_fun->num_arg * sizeof(int)); + } this_prog->numfunc++; this_prog->functions = xrealloc(this_prog->functions, this_prog->numfunc * sizeof(function_t *)); @@ -527,27 +558,32 @@ return define_id(name, type, lpc_type, NULL, 0); } -/* This does the actual formatting. */ -void display_error(char *s) +void do_error(char *s) { + char *buf; + buf = xmalloc(strlen(s) + 50); + sprintf(buf, "%d:%d: %s\n", yylloc.first_line, yylloc.first_column, s); + if(master) { + extern variable_t *fp, *sp; + fp = sp; + apply(master, "compile_error", "ss", this_file, buf); + } else { + printf("%s:%s", this_file, buf); + } + free(buf); +#if 0 if(start_with_newline) { putchar('\n'); start_with_newline = 0; } - printf("%s:%d:%d: %s\n", this_file, yylloc.first_line, yylloc.first_column, s); +#endif } -/* This is the function called by bison, probably with the argument "parse error" - * (I haven't seen anything else). We like to use "syntax error", though. */ int yyerror(char *s) { - if(strcmp(s, "parse error") == 0) - display_error("syntax error"); - else - display_error(s); - compile_errors++; + comp_error(s); return 0; } @@ -602,24 +638,31 @@ program_t *prog; int ret; -#ifdef DEBUG - struct timeval tm, tm2; -#endif + path = add_extension(path); + this_file = path; + if(!legal_path(path)) { + char *buf = xmalloc(strlen(path) + 50); + sprintf(buf, "illegal path '%s'", path); + do_error(buf); + free(buf); + return NULL; + } + + real_path = absolute_path(path); - path = add_extension(path); - #ifdef DEBUG - if(conf.debuglevel) - gettimeofday(&tm, NULL); + if(conf.debuglevel) { + printf("compiling %s... ", path); + start_with_newline = 1; + } #endif - - real_path = absolute_path(path); - printf("compiling %s... ", path); - start_with_newline = 1; yyin = fopen(real_path, "r"); if(!yyin) { - printf("failed (can't open %s: %s)\n", real_path, strerror(errno)); +#ifdef DEBUG + if(conf.debuglevel) + printf("failed (can't open %s: %s)\n", real_path, strerror(errno)); +#endif prog = NULL; goto out; } @@ -629,7 +672,6 @@ this_prog = prog; parse_init(); - this_file = path; #if defined(DEBUG) && YYDEBUG if(conf.debuglevel > 4) { @@ -640,16 +682,19 @@ ret = yyparse(); pop_scope(); /* Go to scope level 0 and free all identifiers. */ - if(ret > 0 || compile_errors) { - if(compile_errors) { - printf("%d error%s", compile_errors, - compile_errors == 1 ? "" : "s"); - if(compile_warnings) - printf(" and %d warning%s", compile_warnings, - compile_warnings == 1 ? "": "s"); +#ifdef DEBUG + if(conf.debuglevel) { + if(compile_errors) { + printf("%d error%s", compile_errors, + compile_errors == 1 ? "" : "s"); + if(compile_warnings) + printf(" and %d warning%s", compile_warnings, + compile_warnings == 1 ? "": "s"); + } + printf(" - failed\n"); } - printf(" - failed\n"); +#endif free(prog); prog = NULL; goto out; @@ -663,24 +708,16 @@ ref_prog(prog); - printf("done"); - #ifdef DEBUG - if(conf.debuglevel) { - gettimeofday(&tm2, NULL); - printf(" in %d microseconds\n", abs(tm2.tv_usec - tm.tv_usec)); - } else { - putchar('\n'); - } + if(conf.debuglevel) + printf("done\n"); if(conf.debuglevel > 1) { printf("Printing code for program %s:\n", path); printf("Globals: \n"); -/* print_var_arr(&this_ob->globals);*/ + print_variables(this_ob->globals, this_ob->numglobals); printf("\n"); print_code(prog); } -#else - putchar('\n'); #endif out: @@ -690,4 +727,3 @@ start_with_newline = 0; return prog; } - |