예제 #1
0
파일: os.c 프로젝트: amjith/chimp
ChimpRef *
chimp_init_os_module (void)
{
    ChimpRef *os;

    os = chimp_module_new_str ("os", NULL);
    if (os == NULL) {
        return NULL;
    }

    if (!chimp_module_add_method_str (os, "getenv", _chimp_os_getenv)) {
        return NULL;
    }

    if (!chimp_module_add_method_str (os, "sleep", _chimp_os_sleep)) {
        return NULL;
    }

    if (!chimp_module_add_method_str (os, "basename", _chimp_os_basename)) {
        return NULL;
    }

    if (!chimp_module_add_method_str (os, "dirname", _chimp_os_dirname)) {
        return NULL;
    }

    return os;
}
예제 #2
0
파일: chimpunit.c 프로젝트: amjith/chimp
ChimpRef *
chimp_init_unit_module (void)
{
    ChimpRef *mod;

    mod = chimp_module_new_str ("chimpunit", NULL);
    if (mod == NULL) {
        return NULL;
    }

    if (!chimp_module_add_method_str (mod, "test", _chimp_unit_test)) {
        return NULL;
    }

    return mod;
}
예제 #3
0
파일: iomod.c 프로젝트: jasonrclark/chimp
ChimpRef *
chimp_init_io_module (void)
{
    ChimpRef *io;

    io = chimp_module_new_str ("io", NULL);
    if (io == NULL) {
        return NULL;
    }

    if (!chimp_module_add_method_str (io, "print", _chimp_io_print)) {
        return NULL;
    }

    if (!chimp_module_add_method_str (io, "write", _chimp_io_write)) {
        return NULL;
    }

    if (!chimp_module_add_method_str (io, "readline", _chimp_io_readline)) {
        return NULL;
    }

    return io;
}
예제 #4
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;
}