/* * return a set that is the union of lhs and rhs */ wycc_obj* wyil_set_union(wycc_obj* lhs, wycc_obj* rhs){ WY_OBJ_SANE(lhs, "wyil_set_union lhs"); WY_OBJ_SANE(rhs, "wyil_set_union rhs"); wycc_obj* ans; struct chunk_ptr lhs_chunk_ptr; struct chunk_ptr *lptr = & lhs_chunk_ptr; struct chunk_ptr rhs_chunk_ptr; struct chunk_ptr *rptr = & rhs_chunk_ptr; wycc_obj *litm; wycc_obj *ritm; int end; /* **** Tuple? */ if ((lhs->typ == Wy_List) || (rhs->typ == Wy_List)) { return wyil_set_union_list(lhs, rhs); } if (lhs->typ != Wy_Set) { WY_PANIC("Help needed in wyil_set_union for type %d\n", lhs->typ) }; if (rhs->typ != Wy_Set) { WY_PANIC("Help needed in wyil_set_union for type %d\n", rhs->typ) }; ans = wycc_set_new(-1); wycc_chunk_ptr_fill(lptr, lhs, 0); /* 0 == this is a set */ wycc_chunk_ptr_fill(rptr, rhs, 0); wycc_chunk_ptr_inc(lptr); wycc_chunk_ptr_inc(rptr); while (1) { litm = lptr->key; ritm = rptr->key; if ((litm == NULL) && (ritm == NULL)) { return ans; }; if (litm == NULL) { end = 1; } else if (ritm == NULL) { end = -1; } else { end = wycc_comp_gen(litm, ritm); }; if (end == 0) { wycc_set_add(ans, litm); wycc_chunk_ptr_inc(lptr); wycc_chunk_ptr_inc(rptr); continue; }; if (end < 0) { wycc_set_add(ans, litm); wycc_chunk_ptr_inc(lptr); } else { wycc_set_add(ans, ritm); wycc_chunk_ptr_inc(rptr); }; }; }
/* * given an object that addresses a FILE, close the file. */ void wycc__close(wycc_obj *itm) { WY_OBJ_SANE(itm, "wycc__close"); FILE *fil; int sts; if (itm->typ != Wy_Addr) { WY_PANIC("Help needed in wycc__close for type %d\n", itm->typ) }; fil = (FILE *) itm->ptr; sts = fclose(fil); if (sts != 0) { WY_PANIC("Failed to close file\n") }; }
/* * given a string open a file of that name, to use for reading */ wycc_obj *wycc__Reader(wycc_obj *fnam) { WY_OBJ_SANE(fnam, "wycc__Reader"); wycc_obj* ans = NULL; FILE *fil; if ((fnam->typ != Wy_String) && (fnam->typ != Wy_CString)) { WY_PANIC("Help needed in wycc__Reader for type %d\n", fnam->typ) }; fil = fopen(fnam->ptr, "r"); if (fil == NULL) { WY_PANIC("Failed to open '%s' for reading\n", fnam->ptr) }; ans = wycc_box_new(Wy_Addr, fil); return ans; }
/* * given a char, return a bool * set to true iff the char is between '0' & '9' inclusive */ wycc_obj* wycc__isDigit(wycc_obj* itm) { WY_OBJ_SANE(itm, "wycc__isDigit"); long val; int ans; val = -1; if (itm->typ == Wy_Int) { val = (long) itm->ptr; } else if (itm->typ == Wy_Char) { val = (long) itm->ptr; } else if (itm->typ == Wy_Byte) { val = (long) itm->ptr; } else { WY_PANIC("Help needed in wycc__isLetter for type %d\n" , itm->typ); exit(-3); }; if (val < 0) { return wycc_box_bool(0); }; ans = 1; if (val < '0') { ans = 0; } else if (val > '9') { ans = 0; }; return wycc_box_bool(ans); }
/* * given a char, return a bool * set to true iff the char is space, tab, newline, return * fails to check for formfeed (\f), vertical tab (\v) */ wycc_obj* wycc__isWhiteSpace(wycc_obj* itm) { WY_OBJ_SANE(itm, "wycc__isWhiteSpace"); int ans; long val; val = -1; if (itm->typ == Wy_Int) { val = (long) itm->ptr; } else if (itm->typ == Wy_Char) { val = (long) itm->ptr; } else if (itm->typ == Wy_Byte) { val = (long) itm->ptr; } else { WY_PANIC("Help needed in wycc__isWhiteSpace for type %d\n" , itm->typ); exit(-3); }; ans = 0; if (val == ' ') { ans = 1; } else if (val == '\t') { ans = 1; } else if (val == '\n') { ans = 1; } else if (val == '\r') { ans = 1; }; return wycc_box_bool(ans); }
/* * given an object that addresses a FILE, and a max count, * get a list of characters */ void wycc__write(wycc_obj *itm, wycc_obj *lst) { WY_OBJ_SANE(itm, "wycc__write itm"); WY_OBJ_SANE(itm, "wycc__write lst"); FILE *fil; size_t siz; char chr; long tmp; long cnt = 0; wycc_obj *alt; wycc_obj *mbr; wycc_obj *iter; if (itm->typ != Wy_Addr) { WY_PANIC("Help needed in wycc__write for type %d\n", itm->typ) }; fil = (FILE *) itm->ptr; iter = wycc_iter_new(lst); while (mbr = wycc_iter_next(iter)) { if (mbr->typ == Wy_Byte) { siz = fwrite((void *) &(mbr->ptr), 1, 1, fil); } else { alt = wycc__toString(mbr); fprintf(fil, "%s", (const char *) alt->ptr); wycc_deref_box(alt, 0); }; wycc_deref_box(mbr, 0); } wycc_deref_box(iter, 0); }
/* * create a list that combines two lists */ wycc_obj* wyil_list_sub(wycc_obj* lst, wycc_obj* lhs, wycc_obj* rhs){ WY_OBJ_SANE(lst, "wyil_list_sub lst"); WY_OBJ_SANE(lhs, "wyil_list_sub lhs"); WY_OBJ_SANE(rhs, "wyil_list_sub rhs"); if (lst->typ != Wy_List) { WY_PANIC("ERROR: list in wyil_list_sub is type %d\n", lst->typ) }; if (lhs->typ != Wy_Int) { WY_PANIC("ERROR: low in wyil_list_sub is type %d\n", lhs->typ) }; if (rhs->typ != Wy_Int) { WY_PANIC("ERROR: hi in wyil_list_sub is type %d\n", rhs->typ) }; return wycc_list_slice(lst, (int) lhs->ptr, (int) rhs->ptr); }
/* * given an object, return one with absolute value. */ wycc_obj* wycc__isqrt(wycc_obj* itm) { WY_OBJ_SANE(itm, "wycc__isqrt"); if (itm->typ == Wy_Int) { return wycc__isqrt_int(itm); }; WY_PANIC("Help needed in wycc__isqrt for type %d\n", itm->typ); exit(-3); }
/* * given a pair of object, return the smaller value. */ wycc_obj* wycc__min(wycc_obj* lhs, wycc_obj* rhs) { WY_OBJ_SANE(lhs, "wycc__min lhs"); WY_OBJ_SANE(rhs, "wycc__min rhs"); if ((lhs->typ == Wy_Int) && (rhs->typ == Wy_Int)) { return wycc__min_int(lhs, rhs); }; WY_PANIC("Help needed in wycc__min for types %d:%d\n" , lhs->typ, rhs->typ); exit(-3); }
/* * given an object that addresses a FILE, and a max count, * get a list of characters */ static wycc_obj* my_read(wycc_obj *itm, int max) { WY_OBJ_SANE(itm, "my_read"); FILE *fil; size_t siz; char chr; long tmp; long cnt = 0; wycc_obj *mbr; wycc_obj *lst; if (itm->typ != Wy_Addr) { WY_PANIC("Help needed in my_read for type %d\n", itm->typ) }; fil = (FILE *) itm->ptr; lst = wycc_list_new(10); while (feof(fil) == 0) { /* could be skipped */ siz = fread(&chr, 1, 1, fil); if (siz != 1) { if (feof(fil)) { break; }; WY_PANIC("Failed to read byte from file\n") }; tmp = (long) chr; mbr = wycc_box_new(Wy_Char, (void *) tmp); wycc_list_add(lst, mbr); cnt++; if (cnt == max) { break; }; } return lst; } /* * given an object that addresses a FILE, get a list of characters */ wycc_obj* wycc__read_max(wycc_obj *itm, wycc_obj *max) { WY_OBJ_SANE(itm, "wycc__read_max itm"); WY_OBJ_SANE(max, "wycc__read_max max"); if (max->typ == Wy_Int) { return my_read(itm, (int) max->ptr); }; if (max->typ == Wy_WInt) { return my_read(itm, 0); // too big to matter }; WY_PANIC("Help needed in wycc__read_max for type %d\n", max->typ) }
/* * given a int, byte, or char, return an int with the same value. */ wycc_obj* wycc__toUnsignedInt(wycc_obj* itm) { WY_OBJ_SANE(itm, "wycc__toUnsignedInt"); long val; if (itm->typ == Wy_Int) { val = (long) itm->ptr; } else if (itm->typ == Wy_Char) { val = (long) itm->ptr; } else if (itm->typ == Wy_Byte) { val = (long) itm->ptr; } else { WY_PANIC("Help needed in wycc__toUnsignedByte for type %d\n" , itm->typ); exit(-3); }; return wycc_box_long(val); }
/* * given a int, byte, or char, return a bool. * true if the code is for lower case ASCII letter. */ wycc_obj* wycc__isLowerCase(wycc_obj* itm) { WY_OBJ_SANE(itm, "wycc__isLowerCase"); long val; int ans; val = -1; if (itm->typ == Wy_Int) { val = (long) itm->ptr; } else if (itm->typ == Wy_Char) { val = (long) itm->ptr; } else if (itm->typ == Wy_Byte) { val = (long) itm->ptr; } else { WY_PANIC("Help needed in wycc__isLowerCase for type %d\n" , itm->typ); exit(-3); }; ans = 0; if ((val >= 'a') && (val <= 'z')) { ans = 1; }; return wycc_box_bool(ans); }
/* * return a string extracted from the given string */ wycc_obj* wyil_substring(wycc_obj* str, wycc_obj* loo, wycc_obj* hio){ WY_OBJ_SANE(str, "wyil_substrin str"); WY_OBJ_SANE(loo, "wyil_substrin loo"); WY_OBJ_SANE(hio, "wyil_substrin hio"); wycc_obj* ans; size_t siz; long lo, hi, max; char *buf; char *sptr; if ((str->typ != Wy_String) && (str->typ != Wy_CString)) { WY_PANIC("Help needed in wyil_substring for type %d\n", str->typ) }; if (loo->typ != Wy_Int) { WY_PANIC("Help needed in wyil_substring for type %d\n", loo->typ) }; if (hio->typ != Wy_Int) { WY_PANIC("Help needed in wyil_substring for type %d\n", hio->typ) }; lo = (long) loo->ptr; hi = (long) hio->ptr; if (lo > hi) { WY_PANIC("Help needed in wyil_substring for limits %d:%d\n", lo, hi) }; siz = hi - lo; sptr = (char *) str->ptr; max = strlen(sptr); if (hi > max) { WY_PANIC("Help needed in wyil_substring for max %d:%d\n", hi, max) }; if (lo < 0) { WY_PANIC("Help needed in wyil_substring for min %d\n", lo) }; buf = (char *) malloc(siz + 3); if (siz > 0) { strncpy(buf, (sptr+lo), siz); } buf[siz] = 0; return wycc_box_str(buf); }