Ejemplo n.º 1
0
/*
 * 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);
}
Ejemplo n.º 2
0
/*
 * like wyil_set_union but where one or both operands are lists
 */
static wycc_obj* wyil_set_union_list(wycc_obj* lhs, wycc_obj* rhs){
    WY_OBJ_SANE(lhs, "wyil_set_union_list lhs");
    WY_OBJ_SANE(rhs, "wyil_set_union_list rhs");
    wycc_obj* ans;
    struct chunk_ptr my_chunk_ptr;
    struct chunk_ptr *cptr = & my_chunk_ptr;

    ans = wycc_set_new(-1);
    if (lhs->typ == Wy_Set) {
	wycc_chunk_ptr_fill(cptr, lhs, 0);	/* 0 == this is a set */
	wycc_chunk_ptr_inc(cptr);
    } else if (rhs->typ == Wy_Set) {
	wycc_chunk_ptr_fill(cptr, rhs, 0);
	wycc_chunk_ptr_inc(cptr);
    } else {
	cptr->key = (wycc_obj *) NULL;
    };
    while (cptr->key != (wycc_obj *) NULL) {
	wycc_set_add(ans, cptr->key);
	wycc_chunk_ptr_inc(cptr);
    };
    /* **** Tuple? */
    if (lhs->typ == Wy_List) {
	wyil_set_add_list(ans, lhs);
    };
    if (rhs->typ == Wy_List) {
	wyil_set_add_list(ans, rhs);
    };
    return ans;
}
Ejemplo n.º 3
0
/*
 * 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);
	};

    };
}
Ejemplo n.º 4
0
/*
 * 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);
}
Ejemplo n.º 5
0
/*
 * given a set and a list add each element of the list to the set
 */
static wycc_obj* wyil_set_add_list(wycc_obj* set, wycc_obj* lst){
    WY_OBJ_SANE(set, "wyil_set_add_list set");
    WY_OBJ_SANE(lst, "wyil_set_add_list lst");
    void** p = lst->ptr;
    wycc_obj *itm;
    long at, tmp;

    for (at= 0; at < (long) p[0]; at++) {
	itm = (wycc_obj *) p[3+at];
	wycc_set_add(set, itm);
    };
    return set;
}
Ejemplo n.º 6
0
/*
 * 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)
}
Ejemplo n.º 7
0
/*
 * 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);
}
Ejemplo n.º 8
0
/*
 * 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);
}
Ejemplo n.º 9
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);
}
Ejemplo n.º 10
0
/*
 * 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);
}
Ejemplo n.º 11
0
/*
 * 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);
}
Ejemplo n.º 12
0
/*
 * 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")
    };
}
Ejemplo n.º 13
0
/*
 * 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;
}
Ejemplo n.º 14
0
/*
 * 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);
}
Ejemplo n.º 15
0
/*
 * 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);
}
Ejemplo n.º 16
0
/*
 * given an object that addresses a FILE, get a list of characters
 */
wycc_obj* wycc__read(wycc_obj *itm) {
    WY_OBJ_SANE(itm, "wycc__read");

    return my_read(itm, 0);
}