예제 #1
0
파일: symtable.c 프로젝트: amjith/chimp
static chimp_bool_t
chimp_symtable_enter_scope (ChimpRef *self, ChimpRef *scope, int flags)
{
    ChimpRef *ste = CHIMP_SYMTABLE_GET_CURRENT_ENTRY(self);
    ChimpRef *new_ste = chimp_symtable_entry_new (self, ste, scope, flags);
    if (new_ste == NULL) {
        return CHIMP_FALSE;
    }
    if (!chimp_hash_put (CHIMP_SYMTABLE(self)->lookup, scope, new_ste)) {
        return CHIMP_FALSE;
    }
    if (ste != chimp_nil) {
        if (!chimp_array_push (CHIMP_SYMTABLE(self)->stack, ste)) {
            return CHIMP_FALSE;
        }
        if (!chimp_array_push (CHIMP_SYMTABLE_ENTRY_CHILDREN(ste), new_ste)) {
            return CHIMP_FALSE;
        }
    }
    CHIMP_SYMTABLE(self)->ste = new_ste;
    return CHIMP_TRUE;
}
예제 #2
0
파일: chasm.c 프로젝트: eax/chimp
static ChimpRef *
parse_args (int argc, char **argv)
{
    int i;
    ChimpRef *args;
    ChimpRef *argv_ = chimp_array_new ();
    for (i = 1; i < argc; i++) {
        ChimpRef *arg = chimp_str_new (argv[i], strlen(argv[i]));
        if (arg == NULL) {
            return NULL;
        }
        if (!chimp_array_push (argv_, arg)) {
            return NULL;
        }
    }
    args = chimp_array_new ();
    if (args == NULL) {
        return NULL;
    }
    if (!chimp_array_push (args, argv_)) {
        return NULL;
    }
    return args;
}
예제 #3
0
파일: hash.c 프로젝트: eax/chimp
static ChimpRef *
_chimp_hash_items (ChimpRef *self, ChimpRef *args)
{
    size_t i;
    ChimpRef *items = chimp_array_new ();
    for (i = 0; i < CHIMP_HASH(self)->size; i++) {
        ChimpRef *item =
            chimp_array_new_var (
                    CHIMP_HASH(self)->keys[i],
                    CHIMP_HASH(self)->values[i], NULL);
        if (item == NULL) {
            return NULL;
        }
        if (!chimp_array_push (items, item)) {
            return NULL;
        }
    }
    return items;
}
예제 #4
0
파일: chimpunit.c 프로젝트: amjith/chimp
static ChimpRef *
_chimp_unit_test(ChimpRef *self, ChimpRef *args)
{
    fprintf (stdout, ".");

    // TODO: Size/argument validation on the incoming args
    ChimpRef *name = chimp_object_str (CHIMP_ARRAY_ITEM(args, 0));
    ChimpRef *fn = CHIMP_ARRAY_ITEM(args, 1);

    if (test_runner == NULL)
    {
        test_runner = chimp_test_new();
    }
    CHIMP_TEST(test_runner)->name = name;

    ChimpRef *fn_args = chimp_array_new();
    chimp_array_push(fn_args, test_runner);

    chimp_object_call (fn, fn_args);
    CHIMP_TEST(test_runner)->passed++;

    return chimp_nil;
}
예제 #5
0
파일: hash.c 프로젝트: eax/chimp
static ChimpRef *
chimp_hash_str (ChimpRef *self)
{
    size_t size = CHIMP_HASH_SIZE(self);
    /* '{' + '}' + (': ' x size) + (', ' x (size-1)) + '\0' */
    size_t total_len = 2 + (size * 2) + (size > 0 ? ((size-1) * 2) : 0) + 1;
    ChimpRef *ref;
    ChimpRef *strs;
    char *data;
    size_t i, k;


    strs = chimp_array_new_with_capacity (size);
    if (strs == NULL) {
        return NULL;
    }

    for (i = 0; i < size; i++) {
        size_t j;
        ChimpRef *item[2];
        item[0] = CHIMP_HASH(self)->keys[i];
        item[1] = CHIMP_HASH(self)->values[i];
        for (j = 0; j < 2; j++) {
            ref = item[j];
            /* XXX what we really want is something like Python's repr() */
            if (CHIMP_ANY_CLASS(ref) == chimp_str_class) {
                /* for surrounding quotes */
                total_len += 2;
            }
            ref = chimp_object_str (ref);
            if (ref == NULL) {
                return NULL;
            }
            chimp_array_push (strs, ref);
            total_len += CHIMP_STR_SIZE(ref);
        }
    }

    data = CHIMP_MALLOC(char, total_len);
    if (data == NULL) {
        return NULL;
    }
    k = 0;
    data[k++] = '{';

    for (i = 0; i < size; i++) {
        size_t j;
        ChimpRef *item[2];
        item[0] = CHIMP_HASH(self)->keys[i];
        item[1] = CHIMP_HASH(self)->values[i];
        for (j = 0; j < 2; j++) {
            ref = CHIMP_ARRAY_ITEM(strs, (i * 2) + j);
            /* XXX what we really want is something like Python's repr() */
            /* TODO instanceof */
            if (CHIMP_ANY_CLASS(item[j]) == chimp_str_class) {
                data[k++] = '"';
            }
            memcpy (data + k, CHIMP_STR_DATA(ref), CHIMP_STR_SIZE(ref));
            k += CHIMP_STR_SIZE(ref);
            if (CHIMP_ANY_CLASS(item[j]) == chimp_str_class) {
                data[k++] = '"';
            }
            if (j == 0) {
                data[k++] = ':';
                data[k++] = ' ';
            }
        }
        if (i < (size-1)) {
            data[k++] = ',';
            data[k++] = ' ';
        }
    }

    data[k++] = '}';
    data[k] = '\0';

    return chimp_str_new_take (data, total_len-1);
}