Пример #1
0
chimp_bool_t
chimp_symtable_class_bootstrap (void)
{
    chimp_symtable_class = chimp_class_new (
        CHIMP_STR_NEW("symtable"), NULL, sizeof(ChimpSymtable));
    if (chimp_symtable_class == NULL) {
        return CHIMP_FALSE;
    }
    CHIMP_CLASS(chimp_symtable_class)->init = _chimp_symtable_init;
    CHIMP_CLASS(chimp_symtable_class)->mark = _chimp_symtable_mark;
    chimp_gc_make_root (NULL, chimp_symtable_class);
    return CHIMP_TRUE;
}
Пример #2
0
Файл: os.c Проект: amjith/chimp
static ChimpRef *
_chimp_os_dirname (ChimpRef *self, ChimpRef *args)
{
    ChimpRef *path = CHIMP_ARRAY_ITEM(args, 0);
    size_t i;
    const char *begin = CHIMP_STR_DATA(path);
    const char *end = begin + CHIMP_STR_SIZE(path);

    for (i = 0; i < CHIMP_STR_SIZE(path); i++) {
        end--;
        if (*end == '/') {
            return chimp_str_new (begin, (size_t) (end - begin));
        }
    }
    return CHIMP_STR_NEW ("");
}
Пример #3
0
Файл: task.c Проект: eax/chimp
chimp_bool_t
chimp_task_class_bootstrap (void)
{
    chimp_task_class =
        chimp_class_new (CHIMP_STR_NEW("task"), NULL, sizeof(ChimpTask));
    if (chimp_task_class == NULL) {
        return CHIMP_FALSE;
    }
    CHIMP_CLASS(chimp_task_class)->init = _chimp_task_init;
    CHIMP_CLASS(chimp_task_class)->dtor = _chimp_task_dtor;
    CHIMP_CLASS(chimp_task_class)->str  = _chimp_task_str;
    CHIMP_CLASS(chimp_task_class)->cmp  = _chimp_task_cmp;
    CHIMP_CLASS(chimp_task_class)->mark = _chimp_task_mark;
    chimp_gc_make_root (NULL, chimp_task_class);
    chimp_class_add_native_method (chimp_task_class, "send", _chimp_task_send);
    chimp_class_add_native_method (chimp_task_class, "join", _chimp_task_join);
    return CHIMP_TRUE;
}
Пример #4
0
Файл: hash.c Проект: eax/chimp
chimp_bool_t
chimp_hash_class_bootstrap (void)
{
    chimp_hash_class =
        chimp_class_new (CHIMP_STR_NEW("hash"), chimp_object_class, sizeof(ChimpHash));
    if (chimp_hash_class == NULL) {
        return CHIMP_FALSE;
    }
    CHIMP_CLASS(chimp_hash_class)->str = chimp_hash_str;
    CHIMP_CLASS(chimp_hash_class)->dtor = _chimp_hash_dtor;
    CHIMP_CLASS(chimp_hash_class)->getitem = _chimp_hash_getitem;
    CHIMP_CLASS(chimp_hash_class)->mark = _chimp_hash_mark;
    chimp_gc_make_root (NULL, chimp_hash_class);
    chimp_class_add_native_method (chimp_hash_class, "put", _chimp_hash_put);
    chimp_class_add_native_method (chimp_hash_class, "get", _chimp_hash_get);
    chimp_class_add_native_method (chimp_hash_class, "size", _chimp_hash_size);
    chimp_class_add_native_method (chimp_hash_class, "items", _chimp_hash_items);
    return CHIMP_TRUE;
}
Пример #5
0
Файл: chasm.c Проект: eax/chimp
static ChimpRef *
assemble_module (const char *filename)
{
    char buf[8192];
    ChimpRef *code;
    ChimpRef *mod;
    ChimpRef *method;
    FILE *stream = fopen (filename, "r");
    if (stream == NULL) {
        fprintf (stderr, "error: failed to open %s\n", filename);
        return NULL;
    }
    code = chimp_code_new ();
    if (code == NULL) {
        fprintf (stderr, "error: failed to allocate code object\n");
        return NULL;
    }
    while (!feof (stream)) {
        const char *ptr;
        ChimpRef *opname;
        ChimpOpcode opcode;
        if (fgets (buf, sizeof(buf)-1, stream) == NULL) {
            break;
        }
        ptr = buf;
        _skip_whitespace (&ptr);
        if (!*ptr) continue;
        if (*ptr == '#') continue;
        if (!_parse_name (&ptr, &opname)) {
            fprintf (stderr, "error: expected name\n");
            goto error;
        }
        opcode = _str_to_opcode (CHIMP_STR_DATA (opname));
        switch (opcode) {
            case CHIMP_OPCODE_PUSHNIL:
                {
                    if (!chimp_code_pushnil (code)) {
                        goto error;
                    }
                    break;
                }
            case CHIMP_OPCODE_DUP:
                {
                    if (!chimp_code_dup (code)) {
                        goto error;
                    }
                    break;
                }
            case CHIMP_OPCODE_NOT:
                {
                    if (!chimp_code_not (code)) {
                        goto error;
                    }
                    break;
                }
            case CHIMP_OPCODE_RET:
                {
                    if (!chimp_code_ret (code)) {
                        goto error;
                    }
                    break;
                }
            case CHIMP_OPCODE_SPAWN:
                {
                    if (!chimp_code_spawn (code)) {
                        goto error;
                    }
                    break;
                }
            case CHIMP_OPCODE_MAKEARRAY:
                {
                    ChimpRef *nargs;

                    if (!_parse_const_int (&ptr, &nargs)) {
                        goto error;
                    }

                    if (!chimp_code_makearray (
                            code, CHIMP_INT(nargs)->value)) {
                        goto error;
                    }

                    break;
                }
            case CHIMP_OPCODE_MAKECLOSURE:
                {
                    if (!chimp_code_makeclosure (code)) {
                        goto error;
                    }
                    break;
                }
            case CHIMP_OPCODE_POP:
                {
                    if (!chimp_code_pop (code)) {
                        goto error;
                    }
                    break;
                }
            case CHIMP_OPCODE_PUSHNAME:
                {
                    ChimpRef *name;

                    if (!_parse_name (&ptr, &name)) {
                        goto error;
                    }

                    if (!chimp_code_pushname (code, name)) {
                        goto error;
                    }

                    break;
                }
            case CHIMP_OPCODE_STORENAME:
                {
                    ChimpRef *name;

                    if (!_parse_name (&ptr, &name)) {
                        goto error;
                    }

                    if (!chimp_code_storename (code, name)) {
                        goto error;
                    }

                    break;
                }
            case CHIMP_OPCODE_GETATTR:
                {
                    ChimpRef *name;

                    if (!_parse_name (&ptr, &name)) {
                        goto error;
                    }

                    if (!chimp_code_getattr (code, name)) {
                        goto error;
                    }

                    break;
                }
            case CHIMP_OPCODE_GETITEM:
                {
                    if (!chimp_code_getitem (code)) {
                        goto error;
                    }

                    break;
                }
            case CHIMP_OPCODE_PUSHCONST:
                {
                    ChimpRef *value;

                    if (!_parse_const (&ptr, &value)) {
                        goto error;
                    }

                    if (!chimp_code_pushconst (code, value)) {
                        goto error;
                    }
                    break;
                }
            case CHIMP_OPCODE_ADD:
                {
                    if (!chimp_code_add (code)) {
                        goto error;
                    }
                    break;
                }
            case CHIMP_OPCODE_SUB:
                {
                    if (!chimp_code_sub (code)) {
                        goto error;
                    }
                    break;
                }
            case CHIMP_OPCODE_MUL:
                {
                    if (!chimp_code_mul (code)) {
                        goto error;
                    }
                    break;
                }
            case CHIMP_OPCODE_DIV:
                {
                    if (!chimp_code_div (code)) {
                        goto error;
                    }
                    break;
                }
            case CHIMP_OPCODE_GETCLASS:
                {
                    if (!chimp_code_getclass (code)) {
                        goto error;
                    }
                    break;
                }
            case CHIMP_OPCODE_CALL:
                {
                    ChimpRef *nargs;

                    if (!_parse_const_int (&ptr, &nargs)) {
                        goto error;
                    }

                    if (!chimp_code_call (
                            code, (uint8_t) CHIMP_INT(nargs)->value)) {
                        goto error;
                    }
                    break;
                }
            default:
                fprintf (stderr, "error: unknown or unsupported opname: %s\n", CHIMP_STR_DATA (opname));
                goto error;
        };
        _skip_whitespace (&ptr);
        if (*ptr) {
            fprintf (stderr,
                "error: too many arguments for op: %s\n", CHIMP_STR_DATA(opname));
            goto error;
        }
    }
    fclose (stream);
    /* printf ("%s\n", CHIMP_STR_DATA (chimp_code_dump (code))); */
    mod = chimp_module_new_str ("main", NULL);
    if (mod == NULL) {
        return NULL;
    }
    method = chimp_method_new_bytecode (mod, code);
    if (method == NULL) {
        return NULL;
    }
    if (!chimp_module_add_local (mod, CHIMP_STR_NEW("main"), method)) {
        return NULL;
    }
    return mod;

error:
    fclose (stream);
    return NULL;
}