From: Keith J. <bu...@us...> - 2003-08-07 14:46:54
|
Update of /cvsroot/cup-language/cup/src In directory sc8-pr-cvs1:/tmp/cvs-serv18096 Modified Files: cup.c cup.h.in cupvm.c Log Message: Builtin functions are now mostly working. Implemented is enough to make the stdio.cup 'print' function work. 'println' will also work, as far as builtin functions are concerned, but the ADD VM OP code is not implemented to add the '\n' on the end, and to add 1 to the length() return value. Index: cup.c =================================================================== RCS file: /cvsroot/cup-language/cup/src/cup.c,v retrieving revision 1.27 retrieving revision 1.28 diff -C2 -r1.27 -r1.28 *** cup.c 6 Aug 2003 19:06:47 -0000 1.27 --- cup.c 7 Aug 2003 14:09:40 -0000 1.28 *************** *** 272,280 **** vm->vm_debug = opt_idebug; - /* cup_set_globalvar(vm, cup_new_value(CUPTYPE_FILE, stdin), "stdin"); cup_set_globalvar(vm, cup_new_value(CUPTYPE_FILE, stdout), "stdout"); cup_set_globalvar(vm, cup_new_value(CUPTYPE_FILE, stderr), "stderr"); - */ cup_execute(vm); --- 272,278 ---- *************** *** 441,448 **** void cup_callback_builtin(CUPVM *vm, const char *fname) { if (strcmp(fname, "fwrite") == 0) { ! cupvm_value_pop(vm)->references--; ! cupvm_value_pop(vm)->references--; } } --- 439,553 ---- void cup_callback_builtin(CUPVM *vm, const char *fname) { + CUPVALUE *v1, *v2, *v3; + + + if (strcmp(fname, "length") == 0) + { + CUPVALUE *ret; + + v1 = cupvm_value_pop(vm); + v1->references--; + + if (v1->type == CUPTYPE_STRING) + { + long int len = strlen(v1->data.val_string); + ret = cup_new_value(CUPTYPE_INTEGER, &len); + } + else + { + cupvm_tb(vm, "length() builtin", "length on this type not implemented"); + if (!v1->references) + cupvm_value_free(v1); + return; + } + + if (!v1->references) + cupvm_value_free(v1); + cupvm_value_push(vm, ret); + return; + } + if (strcmp(fname, "tostring") == 0) + { + CUPVALUE *ret; + char buf[32]; + + v1 = cupvm_value_pop(vm); + v1->references--; + + if (v1->type == CUPTYPE_INTEGER) + { + sprintf(buf, "%ld", v1->data.val_integer); + ret = cup_new_value(CUPTYPE_STRING, buf); + } + else if (v1->type == CUPTYPE_STRING) + { + ret = v1; + v1->references++; + } + else + { + sprintf(buf, "Not Implemented"); + ret = cup_new_value(CUPTYPE_STRING, buf); + } + + if (!v1->references) + cupvm_value_free(v1); + cupvm_value_push(vm, ret); + return; + } if (strcmp(fname, "fwrite") == 0) { ! long ret; ! ! v3 = cupvm_value_pop(vm); ! v3->references--; ! v2 = cupvm_value_pop(vm); ! v2->references--; ! v1 = cupvm_value_pop(vm); ! v1->references--; ! ! if (v1->type != CUPTYPE_FILE) ! { ! if (!v1->references) ! cupvm_value_free(v1); ! if (!v2->references) ! cupvm_value_free(v2); ! if (!v3->references) ! cupvm_value_free(v3); ! cupvm_tb(vm, "fwrite() builtin", "arg1 is not a file"); ! return; ! } ! if (v2->type != CUPTYPE_STRING) ! { ! if (!v1->references) ! cupvm_value_free(v1); ! if (!v2->references) ! cupvm_value_free(v2); ! if (!v3->references) ! cupvm_value_free(v3); ! cupvm_tb(vm, "fwrite() builtin", "arg2 is not a string"); ! return; ! } ! if (v3->type != CUPTYPE_INTEGER) ! { ! if (!v1->references) ! cupvm_value_free(v1); ! if (!v2->references) ! cupvm_value_free(v2); ! if (!v3->references) ! cupvm_value_free(v3); ! cupvm_tb(vm, "fwrite() builtin", "arg3 is not an integer"); ! return; ! } ! ! ret = fwrite(v2->data.val_string, 1, v3->data.val_integer, v1->data.val_file); ! if (!v1->references) ! cupvm_value_free(v1); ! if (!v2->references) ! cupvm_value_free(v2); ! if (!v3->references) ! cupvm_value_free(v3); ! cupvm_value_push(vm, cup_new_value(CUPTYPE_INTEGER, &ret)); ! return; } } Index: cup.h.in =================================================================== RCS file: /cvsroot/cup-language/cup/src/cup.h.in,v retrieving revision 1.15 retrieving revision 1.16 diff -C2 -r1.15 -r1.16 *** cup.h.in 6 Aug 2003 19:06:47 -0000 1.15 --- cup.h.in 7 Aug 2003 14:09:40 -0000 1.16 *************** *** 144,147 **** --- 144,148 ---- long double val_float; char *val_string; + FILE *val_file; } data; *************** *** 150,153 **** --- 151,163 ---- enum { + CUPTYPE_NONE = 0, + CUPTYPE_FLOAT, + CUPTYPE_INTEGER, + CUPTYPE_STRING, + CUPTYPE_FILE + }; + + enum + { CUPDATA_DONE = 0, CUPDATA_FLOAT, *************** *** 205,211 **** void cup_free_vm(CUPVM *); void cup_callback_builtin(CUPVM *, const char *); - /* CUPVALUE *cup_new_value(int, void *); ! void cup_set_globalvar(CUPVM *, CUPVALUE *, const char *); CUPVALUE *cup_get_globalvar(CUPVM *, const char *); */ --- 215,221 ---- void cup_free_vm(CUPVM *); void cup_callback_builtin(CUPVM *, const char *); CUPVALUE *cup_new_value(int, void *); ! bool cup_set_globalvar(CUPVM *, CUPVALUE *, const char *); ! /* CUPVALUE *cup_get_globalvar(CUPVM *, const char *); */ Index: cupvm.c =================================================================== RCS file: /cvsroot/cup-language/cup/src/cupvm.c,v retrieving revision 1.6 retrieving revision 1.7 diff -C2 -r1.6 -r1.7 *** cupvm.c 6 Aug 2003 19:06:47 -0000 1.6 --- cupvm.c 7 Aug 2003 14:09:40 -0000 1.7 *************** *** 159,167 **** vm->vm_debug = opt_idebug; - /* cup_set_globalvar(vm, cup_new_value(CUPTYPE_FILE, stdin), "stdin"); cup_set_globalvar(vm, cup_new_value(CUPTYPE_FILE, stdout), "stdout"); cup_set_globalvar(vm, cup_new_value(CUPTYPE_FILE, stderr), "stderr"); - */ cup_execute(vm); --- 159,165 ---- *************** *** 230,237 **** void cup_callback_builtin(CUPVM *vm, const char *fname) { if (strcmp(fname, "fwrite") == 0) { ! cupvm_value_pop(vm)->references--; ! cupvm_value_pop(vm)->references--; } } --- 228,342 ---- void cup_callback_builtin(CUPVM *vm, const char *fname) { + CUPVALUE *v1, *v2, *v3; + + + if (strcmp(fname, "length") == 0) + { + CUPVALUE *ret; + + v1 = cupvm_value_pop(vm); + v1->references--; + + if (v1->type == CUPTYPE_STRING) + { + long int len = strlen(v1->data.val_string); + ret = cup_new_value(CUPTYPE_INTEGER, &len); + } + else + { + cupvm_tb(vm, "length() builtin", "length on this type not implemented"); + if (!v1->references) + cupvm_value_free(v1); + return; + } + + if (!v1->references) + cupvm_value_free(v1); + cupvm_value_push(vm, ret); + return; + } + if (strcmp(fname, "tostring") == 0) + { + CUPVALUE *ret; + char buf[32]; + + v1 = cupvm_value_pop(vm); + v1->references--; + + if (v1->type == CUPTYPE_INTEGER) + { + sprintf(buf, "%ld", v1->data.val_integer); + ret = cup_new_value(CUPTYPE_STRING, buf); + } + else if (v1->type == CUPTYPE_STRING) + { + ret = v1; + v1->references++; + } + else + { + sprintf(buf, "Not Implemented"); + ret = cup_new_value(CUPTYPE_STRING, buf); + } + + if (!v1->references) + cupvm_value_free(v1); + cupvm_value_push(vm, ret); + return; + } if (strcmp(fname, "fwrite") == 0) { ! long ret; ! ! v3 = cupvm_value_pop(vm); ! v3->references--; ! v2 = cupvm_value_pop(vm); ! v2->references--; ! v1 = cupvm_value_pop(vm); ! v1->references--; ! ! if (v1->type != CUPTYPE_FILE) ! { ! if (!v1->references) ! cupvm_value_free(v1); ! if (!v2->references) ! cupvm_value_free(v2); ! if (!v3->references) ! cupvm_value_free(v3); ! cupvm_tb(vm, "fwrite() builtin", "arg1 is not a file"); ! return; ! } ! if (v2->type != CUPTYPE_STRING) ! { ! if (!v1->references) ! cupvm_value_free(v1); ! if (!v2->references) ! cupvm_value_free(v2); ! if (!v3->references) ! cupvm_value_free(v3); ! cupvm_tb(vm, "fwrite() builtin", "arg2 is not a string"); ! return; ! } ! if (v3->type != CUPTYPE_INTEGER) ! { ! if (!v1->references) ! cupvm_value_free(v1); ! if (!v2->references) ! cupvm_value_free(v2); ! if (!v3->references) ! cupvm_value_free(v3); ! cupvm_tb(vm, "fwrite() builtin", "arg3 is not an integer"); ! return; ! } ! ! ret = fwrite(v2->data.val_string, 1, v3->data.val_integer, v1->data.val_file); ! if (!v1->references) ! cupvm_value_free(v1); ! if (!v2->references) ! cupvm_value_free(v2); ! if (!v3->references) ! cupvm_value_free(v3); ! cupvm_value_push(vm, cup_new_value(CUPTYPE_INTEGER, &ret)); ! return; } } |