Exemple #1
0
def string_append(args):
    if not args:
        return W_String.fromascii("")
    builder = StringBuilder()
    unibuilder = None
    ascii_idx = 0
    try:
        for ascii_idx in range(len(args)):
            arg = args[ascii_idx]
            if not isinstance(arg, W_String):
                raise SchemeException("string-append: expected a string")
            builder.append(arg.as_str_ascii())
    except ValueError:
        unibuilder = UnicodeBuilder()
        unibuilder.append(unicode(builder.build()))
        builder = None
        for i in range(ascii_idx, len(args)):
            arg = args[i]
            if not isinstance(arg, W_String):
                raise SchemeException("string-append: expected a string")
            unibuilder.append(arg.as_unicode())
    if unibuilder is None:
        assert builder is not None
        return W_String.fromascii(builder.build())
    else:
        assert unibuilder is not None
        return W_String.fromunicode(unibuilder.build())
Exemple #2
0
def string_append(args):
    if jit.isconstant(len(args)):
        return string_append_fastpath(args)
    if not args:
        return W_String.fromascii("")
    builder = StringBuilder(len(args))
    unibuilder = None
    ascii_idx = 0
    try:
        for ascii_idx in range(len(args)):
            arg = args[ascii_idx]
            if not isinstance(arg, W_String):
                raise SchemeException("string-append: expected a string")
            builder.append(arg.as_str_ascii())
    except ValueError:
        unibuilder = UnicodeBuilder(len(args))
        unibuilder.append(unicode(builder.build()))
        builder = None
        for i in range(ascii_idx, len(args)):
            arg = args[i]
            if not isinstance(arg, W_String):
                raise SchemeException("string-append: expected a string")
            unibuilder.append(arg.as_unicode())
    if unibuilder is None:
        assert builder is not None
        return W_String.fromascii(builder.build())
    else:
        assert unibuilder is not None
        return W_String.fromunicode(unibuilder.build())
Exemple #3
0
def num2str(a, radix):
    from rpython.rlib.rbigint import BASE8, BASE16
    if radix.value == 10:
        return W_String.fromascii(a.tostring())
    else:
        if isinstance(a, values.W_Fixnum):
            if radix.value == 16:
                res = hex(a.value)
                if a.value >= 0:
                    res = res[2:]
                else:
                    res = "-" + res[3:]
                return W_String.fromascii(res)
            #elif radix.value == 8:
            #    return W_String.fromascii(oct(a.value))
            # elif radix.value == 2:
            #     return W_String.fromascii(bin(a.value))
            else:
                raise SchemeException("number->string: radix unsupported")
        elif isinstance(a, values.W_Bignum):
            if radix.value == 16:
                return W_String.fromascii(a.value.format(BASE16))
            elif radix.value == 8:
                return W_String.fromascii(a.value.format(BASE8))
            elif radix.value == 2:
                return W_String.fromascii(a.value.format("01"))
            else:
                raise SchemeException("number->string: radix unsupported")
        elif isinstance(a, values.W_Flonum):
            raise SchemeException("number->string: flonum only supports radix 10")
        else:
            assert 0 # not reached
Exemple #4
0
def num2str(a, radix):
    from rpython.rlib.rbigint import BASE8, BASE16
    if radix.value == 10:
        return W_String.fromascii(a.tostring())
    else:
        if isinstance(a, values.W_Fixnum):
            if radix.value == 16:
                res = hex(a.value)
                if a.value >= 0:
                    res = res[2:]
                else:
                    res = "-" + res[3:]
                return W_String.fromascii(res)
            #elif radix.value == 8:
            #    return W_String.fromascii(oct(a.value))
            # elif radix.value == 2:
            #     return W_String.fromascii(bin(a.value))
            else:
                raise SchemeException("number->string: radix unsupported")
        elif isinstance(a, values.W_Bignum):
            if radix.value == 16:
                return W_String.fromascii(a.value.format(BASE16))
            elif radix.value == 8:
                return W_String.fromascii(a.value.format(BASE8))
            elif radix.value == 2:
                return W_String.fromascii(a.value.format("01"))
            else:
                raise SchemeException("number->string: radix unsupported")
        elif isinstance(a, values.W_Flonum):
            raise SchemeException(
                "number->string: flonum only supports radix 10")
        else:
            assert 0  # not reached
Exemple #5
0
def string_append_fastpath(args):
    try:
        joined = "".join([a.as_str_ascii() for a in args])
        result = W_String.fromascii(joined)
    except ValueError:
        joined = u"".join([a.as_unicode() for a in args])
        result = W_String.fromunicode(joined)
    return result
Exemple #6
0
def string_append_fastpath(args):
    try:
        joined = "".join([a.as_str_ascii() for a in args])
        result = W_String.fromascii(joined)
    except ValueError:
        joined = u"".join([a.as_unicode() for a in args])
        result = W_String.fromunicode(joined)
    return result
Exemple #7
0
def string(args):
    if len(args) == 0:
        return W_String.fromascii("")
    assert len(args) > 0
    builder = UnicodeBuilder()
    # XXX could do one less copy in the ascii case
    for char in args:
        if not isinstance(char, values.W_Character):
            raise SchemeException("string: expected a character")
        builder.append(char.value)
    return W_String.fromunicode(builder.build())
Exemple #8
0
def string(args):
    if len(args) == 0:
        return W_String.fromascii("")
    assert len(args) > 0
    builder = UnicodeBuilder()
    # XXX could do one less copy in the ascii case
    for char in args:
        if not isinstance(char, values.W_Character):
            raise SchemeException("string: expected a character")
        builder.append(char.value)
    return W_String.fromunicode(builder.build())
Exemple #9
0
def list_to_string(w_list):
    if not w_list.is_proper_list():
        raise SchemeException("list->string: expected proper list")
    if not isinstance(w_list, values.W_Cons):
        return W_String.fromascii("")
    builder = UnicodeBuilder()
    while isinstance(w_list, values.W_Cons):
        char, w_list = w_list.car(), w_list.cdr()
        if not isinstance(char, values.W_Character):
            raise SchemeException("list->string: expected list of characters")
        builder.append(char.value)
    return W_String.fromunicode(builder.build())
Exemple #10
0
def list_to_string(w_list):
    if not w_list.is_proper_list():
        raise SchemeException("list->string: expected proper list")
    if not isinstance(w_list, values.W_Cons):
        return W_String.fromascii("")
    builder = UnicodeBuilder()
    while isinstance(w_list, values.W_Cons):
        char, w_list = w_list.car(), w_list.cdr()
        if not isinstance(char, values.W_Character):
            raise SchemeException("list->string: expected list of characters")
        builder.append(char.value)
    return W_String.fromunicode(builder.build())
Exemple #11
0
def make_string(k, char):
    if char is None:
        char = u'\0'
    else:
        char = char.value
    c = ord(char)
    if k.value < 0:
        raise SchemeException("make-string: around negative")
    if c < 128:
        char = chr(c)
        return W_String.fromascii(char * k.value)
    else:
        char = unichr(c)
        return W_String.fromunicode(char * k.value)
Exemple #12
0
def make_string(k, char):
    if char is None:
        char = u'\0'
    else:
        char = char.value
    c = ord(char)
    if k.value < 0:
        raise SchemeException("make-string: around negative")
    if c < 128:
        char = chr(c)
        return W_String.fromascii(char * k.value)
    else:
        char = unichr(c)
        return W_String.fromunicode(char * k.value)
Exemple #13
0
    def actual_entry(argv):
        jit.set_param(None, "trace_limit", 1000000)
        jit.set_param(None, "threshold", 131)
        jit.set_param(None, "trace_eagerness", 50)
        jit.set_param(None, "max_unroll_loops", 15)

        config, names, args, retval = parse_args(argv)
        if retval != 0 or config is None:
            return retval
        args_w = [W_String.fromstr_utf8(arg) for arg in args]
        module_name, json_ast = ensure_json_ast(config, names)

        entry_flag = 'byte-expand' in names
        reader = JsonLoader(bytecode_expand=entry_flag)
        if json_ast is None:
            ast = reader.expand_to_ast(module_name)
        else:
            ast = reader.load_json_ast_rpython(module_name, json_ast)

        env = ToplevelEnv(pycketconfig)
        env.globalconfig.load(ast)
        env.commandline_arguments = args_w
        env.module_env.add_module(module_name, ast)
        try:
            val = interpret_module(ast, env)
        finally:
            from pycket.prims.input_output import shutdown
            for callback in POST_RUN_CALLBACKS:
                callback(config, env)
            shutdown(env)
        return 0
Exemple #14
0
    def fasl_integer_inner(self, fasl_string, pos, b):
        from pycket import values as v
        from pycket.prims.numeric import _integer_bytes_to_integer
        from pycket.prims.string import _str2num
        from pycket.values_string import W_String

        if b <= 127:
            return b, pos
        elif b >= 132:
            return b-256, pos
        elif b == 128:
            num_str, pos = self.read_bytes_exactly(fasl_string, pos, 2)
            return _integer_bytes_to_integer(list(num_str), v.w_true, v.w_false).toint(), pos
        elif b == 129:
            num_str, pos = self.read_bytes_exactly(fasl_string, pos, 4)
            return _integer_bytes_to_integer(list(num_str), v.w_true, v.w_false).toint(), pos
        elif b == 130:
            num_str, pos = self.read_bytes_exactly(fasl_string, pos, 8)
            return _integer_bytes_to_integer(list(num_str), v.w_true, v.w_false).toint(), pos
        elif b == 131:
            length, pos = self.read_fasl_integer(fasl_string, pos)
            num_str, pos = self.read_fasl_string(fasl_string, pos, length)
            if len(num_str) != length:
                raise Exception("fasl: truncated stream at number")
            return _str2num(W_String.fromstr_utf8(num_str).as_str_utf8(), 16).toint(), pos
        else:
            raise Exception("fasl: internal error on integer mode")
Exemple #15
0
def cms_context(marks):
    from pycket.values_string import W_String
    # TODO: Pycket does not have a mark to denote context. We need to fix that.

    k = marks.cont
    n = 0
    # find out the length
    while isinstance(k, Cont):
        if is_ast_cont_with_surrounding_lambda(k):
            n += 1
        k = k.get_previous_continuation()

    # second traversal saves us from reversing it later
    ls = [None] * n
    k = marks.cont
    i = n - 1
    while isinstance(k, Cont):
        if is_ast_cont_with_surrounding_lambda(k):
            surrounding_lam = k.get_ast().surrounding_lambda
            lam_str = W_String.make(surrounding_lam.tostring())
            ls[i] = values.W_Cons.make(lam_str, values.w_false)
            i -= 1
        k = k.get_previous_continuation()

    return values.to_list(ls)
Exemple #16
0
def char_utf_8_length(char):
    # same as (bytes-length (string->bytes/utf-8 (string char)))
    builder = UnicodeBuilder()
    builder.append(char.value)
    w_str = W_String.fromunicode(builder.build())
    w_bytes = values.W_Bytes.from_charlist(w_str.as_charlist_utf8())
    return values.W_Fixnum(w_bytes.length())
Exemple #17
0
    def actual_entry(argv):
        jit.set_param(None, "trace_limit", 1000000)
        jit.set_param(None, "threshold", 131)
        jit.set_param(None, "trace_eagerness", 50)

        config, names, args, retval = parse_args(argv)
        if retval != 0 or config is None:
            return retval
        args_w = [W_String.fromstr_utf8(arg) for arg in args]
        module_name, json_ast = ensure_json_ast(config, names)

        entry_flag = 'byte-expand' in names
        reader = JsonLoader(bytecode_expand=entry_flag)
        if json_ast is None:
            ast = reader.expand_to_ast(module_name)
        else:
            ast = reader.load_json_ast_rpython(module_name, json_ast)

        env = ToplevelEnv(pycketconfig)
        env.globalconfig.load(ast)
        env.commandline_arguments = args_w
        env.module_env.add_module(module_name, ast)
        try:
            val = interpret_module(ast, env)
        finally:
            from pycket.prims.input_output import shutdown
            if config.get('save-callgraph', False):
                with open('callgraph.dot', 'w') as outfile:
                    env.callgraph.write_dot_file(outfile)
            shutdown(env)
        return 0
Exemple #18
0
def bytes_to_string_latin(w_bytes, err, start, end):
    str = w_bytes.as_str().decode("latin-1")

    # From Racket Docs: The err-char argument is ignored, but present
    # for consistency with the other operations.

    return get_substring(W_String.fromunicode(str), start, end)
Exemple #19
0
    def actual_entry(argv):
        jit.set_param(None, "trace_limit", 1000000)
        jit.set_param(None, "threshold", 131)
        jit.set_param(None, "trace_eagerness", 50)
        jit.set_param(None, "max_unroll_loops", 10)

        config, names, args, retval = parse_args(argv)
        if retval != 0 or config is None:
            return retval
        args_w = [W_String.fromstr_utf8(arg) for arg in args]
        module_name, json_ast = ensure_json_ast(config, names)

        entry_flag = 'byte-expand' in names
        reader = JsonLoader(bytecode_expand=entry_flag)
        if json_ast is None:
            ast = reader.expand_to_ast(module_name)
        else:
            ast = reader.load_json_ast_rpython(module_name, json_ast)

        env = ToplevelEnv(pycketconfig)
        env.globalconfig.load(ast)
        env.commandline_arguments = args_w
        env.module_env.add_module(module_name, ast)
        try:
            val = interpret_module(ast, env)
        finally:
            from pycket.prims.input_output import shutdown
            for callback in POST_RUN_CALLBACKS:
                callback(config, env)
            shutdown(env)
        return 0
Exemple #20
0
def load_linklet_from_fasl(file_name, set_version=False):
    from pycket.fasl import Fasl
    from pycket.env import w_version
    from pycket.util import console_log
    from pycket.ast_vs_sexp import deserialize_loop

    debug_start("loading-linklet")
    debug_print("Loading linklet from fasl -- %s" % file_name)
    sexp = Fasl().to_sexp_from_file(file_name)
    version_sexp, linklet_sexp = W_String.make(""), None
    if set_version:
        version_sexp = sexp.car()
        linklet_sexp = sexp.cdr()
    else:
        linklet_sexp = sexp
    linklet = None
    if "zo" in file_name:
        linklet = deserialize_loop(linklet_sexp)
    else:
        console_log("Run pycket with --make-linklet-zos to make the compiled zo files for bootstrap linklets", 1)
        compile_linklet = get_primitive("compile-linklet")
        linklet = compile_linklet.call_interpret([linklet_sexp, W_Symbol.make("linkl"), w_false, w_false, w_false])

    if set_version:
        ver = version_sexp.as_str_ascii()
        console_log("Setting the version to %s" % ver)
        w_version.set_version(ver)

    debug_stop("loading-linklet")
    return linklet, version_sexp
Exemple #21
0
def char_utf_8_length(char):
    # same as (bytes-length (string->bytes/utf-8 (string char)))
    builder = UnicodeBuilder()
    builder.append(char.value)
    w_str = W_String.fromunicode(builder.build())
    w_bytes = values.W_Bytes.from_charlist(w_str.as_charlist_utf8())
    return values.W_Fixnum(w_bytes.length())
Exemple #22
0
def load_linklet_from_fasl(file_name, set_version=False):
    from pycket.fasl import Fasl
    from pycket.env import w_version
    from pycket.util import console_log
    from pycket.ast_vs_sexp import deserialize_loop

    debug_start("loading-linklet")
    debug_print("Loading linklet from fasl -- %s" % file_name)
    sexp = Fasl().to_sexp_from_file(file_name)
    version_sexp, linklet_sexp = W_String.make(""), None
    if set_version:
        version_sexp = sexp.car()
        linklet_sexp = sexp.cdr()
    else:
        linklet_sexp = sexp
    linklet = None
    if "zo" in file_name:
        linklet = deserialize_loop(linklet_sexp)
    else:
        console_log("Run pycket with --make-linklet-zos to make the compiled zo files for bootstrap linklets", 1)
        compile_linklet = get_primitive("compile-linklet")
        linklet = compile_linklet.call_interpret([linklet_sexp, W_Symbol.make("linkl"), w_false, w_false, w_false])

    if set_version:
        ver = version_sexp.as_str_ascii()
        console_log("Setting the version to %s" % ver)
        w_version.set_version(ver)

    debug_stop("loading-linklet")
    return linklet, version_sexp
Exemple #23
0
    def read_fasl_integer_stream(self, stream):
        from pycket import values as v
        from pycket.prims.numeric import _integer_bytes_to_integer
        from pycket.prims.string import _str2num
        from pycket.values_string import W_String
        _b = stream.read(1)[0]
        if not _b:
            raise Exception("truncated stream - got eof")

        b = ord(_b)

        if b <= 127:
            return b
        elif b >= 132:
            return b-256
        elif b == 128:
            num_str = self.read_bytes_exactly_stream(stream, 2)
            return _integer_bytes_to_integer(list(num_str), v.w_true, v.w_false).toint()
        elif b == 129:
            num_str = self.read_bytes_exactly_stream(stream, 4)
            return _integer_bytes_to_integer(list(num_str), v.w_true, v.w_false).toint()
        elif b == 130:
            num_str = self.read_bytes_exactly_stream(stream, 8)
            return _integer_bytes_to_integer(list(num_str), v.w_true, v.w_false).toint()
        elif b == 131:
            length = self.read_fasl_integer_stream(stream)
            assert isinstance(length, int)
            num_str = self.read_bytes_exactly_stream(stream, length)
            if len(num_str) != length:
                raise Exception("fasl: truncated stream at number")
            return _str2num(W_String.fromstr_utf8(num_str).as_str_utf8(), 16).toint()
        else:
            raise Exception("fasl: internal error on integer mode")
Exemple #24
0
def bytes_to_string_latin(w_bytes, err, start, end):
    str = w_bytes.as_str().decode("latin-1")

    # From Racket Docs: The err-char argument is ignored, but present
    # for consistency with the other operations.

    return get_substring(W_String.fromunicode(str), start, end)
Exemple #25
0
    def actual_entry(argv):
        jit.set_param(None, "trace_limit", 1000000)
        jit.set_param(None, "threshold", 131)
        jit.set_param(None, "trace_eagerness", 50)

        if NonConstant(False):
            # Hack to give os.open() the correct annotation
            os.open("foo", 1, 1)

        config, names, args, retval = parse_args(argv)
        if retval != 0 or config is None:
            return retval
        args_w = [W_String.fromstr_utf8(arg) for arg in args]
        module_name, json_ast = ensure_json_ast(config, names)

        modtable = ModTable()
        modtable.enter_module(module_name)
        if json_ast is None:
            ast = expand_to_ast(module_name, modtable)
        else:
            ast = load_json_ast_rpython(json_ast, modtable)
        modtable.exit_module(module_name, ast)

        env = ToplevelEnv(pycketconfig)
        env.globalconfig.load(ast)
        env.commandline_arguments = args_w
        env.module_env.add_module(module_name, ast)
        try:
            val = interpret_module(ast, env)
        finally:
            from pycket.prims.input_output import shutdown

            shutdown(env)
        return 0
Exemple #26
0
    def actual_entry(argv):
        jit.set_param(None, "trace_limit", 1000000)
        jit.set_param(None, "threshold", 131)
        jit.set_param(None, "trace_eagerness", 50)

        config, names, args, retval = parse_args(argv)
        if retval != 0 or config is None:
            return retval
        args_w = [W_String.fromstr_utf8(arg) for arg in args]
        module_name, json_ast = ensure_json_ast(config, names)
        modtable = ModTable()
        if json_ast is None:
            ast = expand_to_ast(module_name, modtable)
        else:
            ast = load_json_ast_rpython(json_ast, modtable)
        env = ToplevelEnv(pycketconfig)
        env.globalconfig.load(ast)
        env.commandline_arguments = args_w
        env.module_env.add_module(module_name, ast)
        try:
            val = interpret_module(ast, env)
        finally:
            from pycket.prims.input_output import shutdown
            shutdown(env)
        return 0
Exemple #27
0
def cms_context(marks):
    from pycket.values_string import W_String
    # TODO: Pycket does not have a mark to denote context. We need to fix that.

    k = marks.cont
    n = 0
    # find out the length
    while isinstance(k, Cont):
        if is_ast_cont_with_surrounding_lambda(k):
            n += 1
        k = k.get_previous_continuation()

    # second traversal saves us from reversing it later
    ls = [None]*n
    k = marks.cont
    i = n-1
    while isinstance(k, Cont):
        if is_ast_cont_with_surrounding_lambda(k):
            surrounding_lam = k.get_ast().surrounding_lambda
            lam_str = W_String.make(surrounding_lam.tostring())
            ls[i] = values.W_Cons.make(lam_str, values.w_false)
            i -= 1
        k = k.get_previous_continuation()

    return values.to_list(ls)
Exemple #28
0
    def fasl_integer_inner(self, fasl_string, pos, b):
        from pycket import values as v
        from pycket.prims.numeric import _integer_bytes_to_integer
        from pycket.prims.string import _str2num
        from pycket.values_string import W_String

        if b <= 127:
            return b, pos
        elif b >= 132:
            return b - 256, pos
        elif b == 128:
            num_str, pos = self.read_bytes_exactly(fasl_string, pos, 2)
            return _integer_bytes_to_integer(list(num_str), v.w_true,
                                             v.w_false).toint(), pos
        elif b == 129:
            num_str, pos = self.read_bytes_exactly(fasl_string, pos, 4)
            return _integer_bytes_to_integer(list(num_str), v.w_true,
                                             v.w_false).toint(), pos
        elif b == 130:
            num_str, pos = self.read_bytes_exactly(fasl_string, pos, 8)
            return _integer_bytes_to_integer(list(num_str), v.w_true,
                                             v.w_false).toint(), pos
        elif b == 131:
            length, pos = self.read_fasl_integer(fasl_string, pos)
            num_str, pos = self.read_fasl_string(fasl_string, pos, length)
            if len(num_str) != length:
                raise Exception("fasl: truncated stream at number")
            return _str2num(W_String.fromstr_utf8(num_str).as_str_utf8(),
                            16).toint(), pos
        else:
            raise Exception("fasl: internal error on integer mode")
Exemple #29
0
def configure_runtime(m):
    dynamic_require = get_primitive("dynamic-require")
    module_declared = get_primitive("module-declared?")
    join = get_primitive("module-path-index-join")
    submod = W_WrappedConsProper.make(W_Symbol.make("submod"),
                                      W_WrappedConsProper.make(W_String.make("."),
                                                               W_WrappedConsProper(W_Symbol.make("configure-runtime"), w_null)))

    config_m = join.call_interpret([submod, m])
    if module_declared.call_interpret([config_m, w_true]) is w_true:
        dynamic_require.call_interpret([config_m, w_false])
Exemple #30
0
def string_append(args):
    if not args:
        return W_String.fromascii("")
    builder = StringBuilder()
    unibuilder = None
    for a in args:
        if not isinstance(a, W_String):
            raise SchemeException("string-append: expected a string")
        if unibuilder is None:
            try:
                builder.append(a.as_str_ascii())
                continue
            except ValueError:
                unibuilder = UnicodeBuilder()
                unibuilder.append(unicode(builder.build()))
        unibuilder.append(a.as_unicode())
    if unibuilder is None:
        return W_String.fromascii(builder.build())
    else:
        return W_String.fromunicode(unibuilder.build())
Exemple #31
0
    def actual_entry(argv):
        jit.set_param(None, "trace_limit", 1000000)
        jit.set_param(None, "threshold", 131)
        jit.set_param(None, "trace_eagerness", 50)
        jit.set_param(None, "max_unroll_loops", 15)

        from pycket.env import w_global_config
        w_global_config.set_pycketconfig(pycketconfig)

        config, names, args, retval = parse_args(argv)

        if config['verbose']:
            level = int(names['verbosity_level'][0])
            w_global_config.set_config_val('verbose', level)

            if 'verbosity_keywords' in names:
                w_global_config.set_verbose_keywords(
                    names['verbosity_keywords'])

            if 'not-implemented' in names:
                print("These flags are not implemented yet : %s" %
                      names['not-implemented'])

        if retval == MISSING_ARG:
            print("Bad switch, or missing argument in : %s" % argv[1:])
            return 1

        if retval == JUST_EXIT or config is None:
            return RETURN_OK  # exit normally

        if 'stdout_level' in names:  # -O
            from pycket.prims.logging import w_main_logger
            w_main_logger.set_stdout_level(names['stdout_level'][0])

        if 'stderr_level' in names:  # -W
            from pycket.prims.logging import w_main_logger
            w_main_logger.set_stderr_level(names['stderr_level'][0])

        if 'syslog_level' in names:  # -L
            from pycket.prims.logging import w_main_logger
            w_main_logger.set_syslog_level(names['syslog_level'][0])

        current_cmd_args = [W_String.fromstr_utf8(arg) for arg in args]

        if 'json-linklets' in names:
            for linkl_json in names['json-linklets']:
                vvv = config['verbose']
                load_inst_linklet(linkl_json, debug=vvv)

        try:
            if not config['stop']:
                racket_entry(names, config, current_cmd_args)
        except ExitException, e:
            pass
Exemple #32
0
def configure_runtime(m):
    dynamic_require = get_primitive("dynamic-require")
    module_declared = get_primitive("module-declared?")
    join = get_primitive("module-path-index-join")
    submod = W_WrappedConsProper.make(W_Symbol.make("submod"),
                                      W_WrappedConsProper.make(W_String.make("."),
                                                               W_WrappedConsProper(W_Symbol.make("configure-runtime"), w_null)))

    config_m = join.call_interpret([submod, m])
    if module_declared.call_interpret([config_m, w_true]) is w_true:
        dynamic_require.call_interpret([config_m, w_false])
Exemple #33
0
def string_append(args):
    if not args:
        return W_String.fromascii("")
    builder = StringBuilder()
    unibuilder = None
    for a in args:
        if not isinstance(a, W_String):
            raise SchemeException("string-append: expected a string")
        if unibuilder is None:
            try:
                builder.append(a.as_str_ascii())
                continue
            except ValueError:
                unibuilder = UnicodeBuilder()
                unibuilder.append(unicode(builder.build()))
        unibuilder.append(a.as_unicode())
    if unibuilder is None:
        return W_String.fromascii(builder.build())
    else:
        return W_String.fromunicode(unibuilder.build())
Exemple #34
0
def namespace_require_plus(spec):
    namespace_require = get_primitive("namespace-require")
    dynamic_require = get_primitive("dynamic-require")
    module_declared = get_primitive("module-declared?")
    join = get_primitive("module-path-index-join")
    m = join.call_interpret([spec, w_false])
    submod = W_WrappedConsProper.make(W_Symbol.make("submod"),
                                      W_WrappedConsProper.make(W_String.make("."),
                                                               W_WrappedConsProper(W_Symbol.make("main"), w_null)))
    # FIXME: configure-runtime
    if need_runtime_configure[0]:
        configure_runtime(m)
        need_runtime_configure[0] = False
    namespace_require.call_interpret([m])
    main = join.call_interpret([submod, m])
    if module_declared.call_interpret([main, w_true]) is w_true:
        dynamic_require.call_interpret([main, w_false])
Exemple #35
0
def namespace_require_plus(spec):
    namespace_require = get_primitive("namespace-require")
    dynamic_require = get_primitive("dynamic-require")
    module_declared = get_primitive("module-declared?")
    join = get_primitive("module-path-index-join")
    m = join.call_interpret([spec, w_false])
    submod = W_WrappedConsProper.make(W_Symbol.make("submod"),
                                      W_WrappedConsProper.make(W_String.make("."),
                                                               W_WrappedConsProper(W_Symbol.make("main"), w_null)))
    # FIXME: configure-runtime
    if need_runtime_configure[0]:
        configure_runtime(m)
        need_runtime_configure[0] = False
    namespace_require.call_interpret([m])
    main = join.call_interpret([submod, m])
    if module_declared.call_interpret([main, w_true]) is w_true:
        dynamic_require.call_interpret([main, w_false])
Exemple #36
0
def string_to_sexp(sexp):
    stack = []
    out = []
    if dbg:
        print("%-6s %-14s %-44s %-s" % tuple("term value out stack".split()))
    for termtypes in re.finditer(term_regex, sexp):
        term, value = [(t, v) for t, v in termtypes.groupdict().items()
                       if v][0]
        if dbg: print("%-7s %-14s %-44r %-r" % (term, value, out, stack))
        if term == 'l_paren' or term == 'll_paren':
            stack.append(out)
            out = []
        elif term == 'r_paren' or term == 'rr_paren':
            assert stack, "Trouble with nesting of brackets"
            tmpout, out = out, stack.pop(-1)
            out.append(tmpout)
        elif term == 'rational':
            v = to_num(value)
            out.append(v)
        elif term == 'compnum':
            real_part, imag_part = split_complex(value)
            v = W_Complex.make(to_num(real_part), to_num(imag_part))
            out.append(v)
        elif term == 'num':
            v = to_num(value)
            out.append(v)
        elif term == 'string':
            s = W_String.make(value[1:-1])
            out.append(s)
        elif term == 'char':
            c = W_Character(value[2:])
            out.append(c)
        elif term == 'bool':
            if value in ['#t', '#T', 'true', '#true']:
                b = w_true
            else:
                b = w_false
            out.append(b)
        elif term == 'sym':
            s = W_Symbol.make(value)
            out.append(s)
        else:
            raise NotImplementedError("Error: %r" % (term, value))
    assert not stack, "Trouble with nesting of brackets"
    return to_w_list(out[0])
Exemple #37
0
    def old_pycket_actual_entry(argv):
        jit.set_param(None, "trace_limit", 1000000)
        jit.set_param(None, "threshold", 131)
        jit.set_param(None, "trace_eagerness", 50)
        jit.set_param(None, "max_unroll_loops", 15)

        config, names, args, retval = old_pycket_parse_args(argv)
        if retval != 0 or config is None:
            return retval
        args_w = [W_String.fromstr_utf8(arg) for arg in args]
        module_name, json_ast = ensure_json_ast(config, names)

        from pycket.env import w_global_config
        from pycket.prims.general import make_stub_predicates_no_linklet
        w_global_config.set_linklet_mode_off()

        entry_flag = 'byte-expand' in names
        multi_mod_flag = 'multiple-modules' in names

        multi_mod_map = ModuleMap(json_ast) if multi_mod_flag else None

        reader = JsonLoader(bytecode_expand=entry_flag,
                            multiple_modules=multi_mod_flag,
                            module_mapper=multi_mod_map)

        if json_ast is None:
            ast = reader.expand_to_ast(module_name)
        else:
            ast = reader.load_json_ast_rpython(module_name, json_ast)

        env = ToplevelEnv(pycketconfig)
        env.globalconfig.load(ast)
        env.commandline_arguments = args_w
        env.module_env.add_module(module_name, ast)
        try:
            val = interpret_module(ast, env)
        finally:
            from pycket.prims.input_output import shutdown
            for callback in POST_RUN_CALLBACKS:
                callback(config, env)
            shutdown(env)
        return 0
Exemple #38
0
    def actual_entry(argv):
        jit.set_param(None, "trace_limit", 1000000)

        config, names, args, retval = parse_args(argv)
        if retval != 0 or config is None:
            return retval
        args_w = [W_String.fromstr_utf8(arg) for arg in args]
        module_name, json_ast = ensure_json_ast(config, names)
        modtable = ModTable()
        if json_ast is None:
            ast = expand_to_ast(module_name, modtable)
        else:
            ast = load_json_ast_rpython(json_ast, modtable)
        env = ToplevelEnv(pycketconfig)
        env.globalconfig.load(ast)
        env.commandline_arguments = args_w
        env.module_env.add_module(module_name, ast)
        try:
            val = interpret_module(ast, env)
        finally:
            from pycket.prims.input_output import shutdown
            shutdown(env)
        return 0
Exemple #39
0
    def read_fasl_integer_stream(self, stream):
        from pycket import values as v
        from pycket.prims.numeric import _integer_bytes_to_integer
        from pycket.prims.string import _str2num
        from pycket.values_string import W_String
        _b = stream.read(1)[0]
        if not _b:
            raise Exception("truncated stream - got eof")

        b = ord(_b)

        if b <= 127:
            return b
        elif b >= 132:
            return b - 256
        elif b == 128:
            num_str = self.read_bytes_exactly_stream(stream, 2)
            return _integer_bytes_to_integer(list(num_str), v.w_true,
                                             v.w_false).toint()
        elif b == 129:
            num_str = self.read_bytes_exactly_stream(stream, 4)
            return _integer_bytes_to_integer(list(num_str), v.w_true,
                                             v.w_false).toint()
        elif b == 130:
            num_str = self.read_bytes_exactly_stream(stream, 8)
            return _integer_bytes_to_integer(list(num_str), v.w_true,
                                             v.w_false).toint()
        elif b == 131:
            length = self.read_fasl_integer_stream(stream)
            assert isinstance(length, int)
            num_str = self.read_bytes_exactly_stream(stream, length)
            if len(num_str) != length:
                raise Exception("fasl: truncated stream at number")
            return _str2num(W_String.fromstr_utf8(num_str).as_str_utf8(),
                            16).toint()
        else:
            raise Exception("fasl: internal error on integer mode")
Exemple #40
0
    def actual_entry(argv):
        jit.set_param(None, "trace_limit", 1000000)
        jit.set_param(None, "threshold", 131)
        jit.set_param(None, "trace_eagerness", 50)

        if NonConstant(False):
            # Hack to give os.open() the correct annotation
            os.open('foo', 1, 1)

        config, names, args, retval = parse_args(argv)
        if retval != 0 or config is None:
            return retval
        args_w = [W_String.fromstr_utf8(arg) for arg in args]
        module_name, json_ast = ensure_json_ast(config, names)

        modtable = ModTable()
        modtable.enter_module(module_name)
        if json_ast is None:
            ast = expand_to_ast(module_name, modtable)
        else:
            ast = load_json_ast_rpython(json_ast, modtable)
        modtable.exit_module(module_name, ast)

        env = ToplevelEnv(pycketconfig)
        env.globalconfig.load(ast)
        env.commandline_arguments = args_w
        env.module_env.add_module(module_name, ast)
        try:
            val = interpret_module(ast, env)
        finally:
            from pycket.prims.input_output import shutdown
            if config.get('save-callgraph', False):
                with open('callgraph.dot', 'w') as outfile:
                    env.callgraph.write_dot_file(outfile)
            shutdown(env)
        return 0
Exemple #41
0
def symbol_to_string_impl(v):
    asciivalue = v.asciivalue()
    if asciivalue is not None:
        return W_String.fromascii(asciivalue)
    return W_String.fromunicode(v.unicodevalue())
Exemple #42
0
def string_to_bytes_locale(bytes, errbyte, start, end):
    # FIXME: This ignores the locale
    return W_String.fromstr_utf8(bytes.as_str())
Exemple #43
0
 def to_sexp_from_file(self, file_name):
     from pycket.values_string import W_String
     from pycket.prims.input_output import open_infile
     port = open_infile(W_String.make(file_name), "rb")
     return self.to_sexp_from_w_port(port)
Exemple #44
0
    def fasl_to_sexp_recursive(self, fasl_string, pos):
        from pycket import values as v
        from pycket.values_string import W_String
        from pycket.values_regex import W_Regexp, W_PRegexp, W_ByteRegexp, W_BytePRegexp
        from pycket.vector import W_Vector
        from pycket.values_struct import W_Struct
        from pycket.hash import simple as hash_simple
        from pycket.hash.equal import W_EqualHashTable
        from pycket.prims.numeric import float_bytes_to_real
        from pycket.prims.string import _str2num
        from rpython.rlib.rbigint import rbigint
        from pycket.prims.input_output import build_path, bytes_to_path_element
        from pycket.ast_vs_sexp import to_rpython_list

        typ, pos = self.read_byte_no_eof(fasl_string, pos)

        if typ == FASL_GRAPH_DEF_TYPE:
            position, pos = self.read_fasl_integer(fasl_string, pos)
            val, pos = self.fasl_to_sexp_recursive(fasl_string, pos)
            if position >= self.GLOBAL_SHARED_COUNT:
                raise Exception("fasl: bad graph index")
            self.SHARED[position] = val
            return val, pos
        elif typ == FASL_GRAPH_REF_TYPE:
            position, pos = self.read_fasl_integer(fasl_string, pos)
            if position >= self.GLOBAL_SHARED_COUNT:
                raise Exception("fasl: bad graph index")
            return self.SHARED[position], pos
        elif typ == FASL_FALSE_TYPE:
            return v.w_false, pos
        elif typ == FASL_TRUE_TYPE:
            return v.w_true, pos
        elif typ == FASL_NULL_TYPE:
            return v.w_null, pos
        elif typ == FASL_VOID_TYPE:
            return v.w_void, pos
        elif typ == FASL_EOF_TYPE:
            return v.eof_object, pos
        elif typ == FASL_INTEGER_TYPE:
            num, pos = self.read_fasl_integer(fasl_string, pos)
            if isinstance(num, rbigint):
                return v.W_Bignum(num), pos
            return v.W_Fixnum(num), pos
        elif typ == FASL_FLONUM_TYPE:
            num_str, pos = self.read_bytes_exactly(fasl_string, pos, 8)
            return float_bytes_to_real(list(num_str), v.w_false), pos
        elif typ == FASL_SINGLE_FLONUM_TYPE:
            num_str, pos = self.read_bytes_exactly(fasl_string, pos, 4)
            real = float_bytes_to_real(list(num_str), v.w_false)
            return real.arith_exact_inexact(), pos
        elif typ == FASL_EXTFLONUM_TYPE:
            bstr_len, pos = self.read_fasl_integer(fasl_string, pos)
            num_str, pos = self.read_bytes_exactly(fasl_string, pos, bstr_len)
            return _str2num(W_String.fromstr_utf8(num_str).as_str_utf8(), 10), pos
        elif typ == FASL_RATIONAL_TYPE:
            num, pos = self.fasl_to_sexp_recursive(fasl_string, pos)
            den, pos = self.fasl_to_sexp_recursive(fasl_string, pos)
            return v.W_Rational.make(num, den), pos
        elif typ == FASL_COMPLEX_TYPE:
            re, pos = self.fasl_to_sexp_recursive(fasl_string, pos)
            im, pos = self.fasl_to_sexp_recursive(fasl_string, pos)
            return v.W_Complex.from_real_pair(re, im), pos
        elif typ == FASL_CHAR_TYPE:
            _chr, pos = self.read_fasl_integer(fasl_string, pos)
            return v.W_Character(unichr(_chr)), pos
        elif typ == FASL_SYMBOL_TYPE:
            sym_str, pos = self.read_fasl_string(fasl_string, pos)
            return v.W_Symbol.make(sym_str), pos
        elif typ == FASL_UNREADABLE_SYMBOL_TYPE:
            sym_str, pos = self.read_fasl_string(fasl_string, pos)
            return v.W_Symbol.make_unreadable(sym_str), pos
        elif typ == FASL_UNINTERNED_SYMBOL_TYPE:
            sym_str, pos = self.read_fasl_string(fasl_string, pos)
            return v.W_Symbol(sym_str), pos
        elif typ == FASL_KEYWORD_TYPE:
            key_str, pos = self.read_fasl_string(fasl_string, pos)
            return v.W_Keyword.make(key_str), pos
        elif typ == FASL_STRING_TYPE:
            str_str, pos = self.read_fasl_string(fasl_string, pos)
            return W_String.make(str_str), pos
        elif typ == FASL_IMMUTABLE_STRING_TYPE:
            str_str, pos = self.read_fasl_string(fasl_string, pos)
            return W_String.make(str_str).make_immutable(), pos
        elif typ == FASL_BYTES_TYPE:
            byts, pos = self.read_fasl_bytes(fasl_string, pos)
            return v.W_Bytes.from_string(byts, immutable=False), pos
        elif typ == FASL_IMMUTABLE_BYTES_TYPE:
            byts, pos = self.read_fasl_bytes(fasl_string, pos)
            return v.W_Bytes.from_string(byts), pos
        elif typ == FASL_PATH_TYPE:
            byts, pos = self.read_fasl_bytes(fasl_string, pos)
            return v.W_Path(byts), pos
        elif typ == FASL_RELATIVE_PATH_TYPE:
            wrt_dir = self.current_relative_dir
            p_w_lst, pos = self.fasl_to_sexp_recursive(fasl_string, pos)
            p_r_lst, _ = to_rpython_list(p_w_lst)
            rel_elems = [bytes_to_path_element(p) if isinstance(p, v.W_Bytes) else p for p in p_r_lst]
            if wrt_dir:
                return build_path([wrt_dir] + rel_elems), pos
            elif rel_elems == []:
                return build_path([v.W_Symbol.make("same")]), pos
            else:
                return build_path(rel_elems), pos
        elif typ == FASL_PREGEXP_TYPE:
            str_str, pos = self.read_fasl_string(fasl_string, pos)
            return W_PRegexp(str_str), pos
        elif typ == FASL_REGEXP_TYPE:
            str_str, pos = self.read_fasl_string(fasl_string, pos)
            return W_Regexp(str_str), pos
        elif typ == FASL_BYTE_PREGEXP:
            str_str, pos = self.read_fasl_string(fasl_string, pos)
            return W_BytePRegexp(str_str), pos
        elif typ == FASL_BYTE_REGEXP_TYPE:
            str_str, pos = self.read_fasl_string(fasl_string, pos)
            return W_ByteRegexp(str_str), pos
        elif typ == FASL_LIST_TYPE:
            list_len, pos = self.read_fasl_integer(fasl_string, pos)
            lst, pos = self.read_multi_into_rpython_list(fasl_string, pos, list_len)
            return v.to_list(lst), pos
        elif typ == FASL_PAIR_TYPE:
            car, pos = self.fasl_to_sexp_recursive(fasl_string, pos)
            cdr, pos = self.fasl_to_sexp_recursive(fasl_string, pos)
            return v.W_Cons.make(car, cdr), pos
        elif typ == FASL_LIST_STAR_TYPE:
            list_len, pos = self.read_fasl_integer(fasl_string, pos)
            # list_len is the length of the proper part
            lst, pos = self.read_multi_into_rpython_list(fasl_string, pos, list_len)
            # read the last element
            return_list, pos = self.fasl_to_sexp_recursive(fasl_string, pos)
            for i in range(list_len-1, -1, -1):
                return_list = v.W_Cons.make(lst[i], return_list)
            return return_list, pos
        elif typ == FASL_VECTOR_TYPE or typ == FASL_IMMUTABLE_VECTOR_TYPE:
            vec_len, pos = self.read_fasl_integer(fasl_string, pos)
            storage, pos = self.read_multi_into_rpython_list(fasl_string, pos, vec_len)
            if typ == FASL_IMMUTABLE_VECTOR_TYPE:
                return W_Vector.fromelements(storage, immutable=True), pos
            return W_Vector.fromelements(storage), pos
        elif typ == FASL_BOX_TYPE:
            element, pos = self.fasl_to_sexp_recursive(fasl_string, pos)
            return v.W_MBox(element), pos
        elif typ == FASL_IMMUTABLE_BOX_TYPE:
            element, pos = self.fasl_to_sexp_recursive(fasl_string, pos)
            return v.W_IBox(element), pos
        elif typ == FASL_PREFAB_TYPE:
            key, pos = self.fasl_to_sexp_recursive(fasl_string, pos)
            length, pos = self.read_fasl_integer(fasl_string, pos)
            vals, pos = self.read_multi_into_rpython_list(fasl_string, pos, length)
            return W_Struct.make_prefab(key, vals), pos
        elif typ == FASL_HASH_TYPE:
            variant, pos = self.read_byte_no_eof(fasl_string, pos)
            length, pos = self.read_fasl_integer(fasl_string, pos)
            keys, vals, pos = self.read_multi_double_into_rpython_list(fasl_string, pos, length)
            if variant == FASL_HASH_EQ_VARIANT:
                return hash_simple.make_simple_mutable_table(hash_simple.W_EqMutableHashTable, keys, vals), pos
            elif variant == FASL_HASH_EQV_VARIANT:
                return hash_simple.make_simple_mutable_table(hash_simple.W_EqvMutableHashTable, keys, vals), pos
            else: # variant == FASL_HASH_EQUAL_VARIANT:
                return W_EqualHashTable(keys, vals, immutable=False), pos
        elif typ == FASL_IMMUTABLE_HASH_TYPE:
            variant, pos = self.read_byte_no_eof(fasl_string, pos)
            length, pos = self.read_fasl_integer(fasl_string, pos)
            keys, vals, pos = self.read_multi_double_into_rpython_list(fasl_string, pos, length)
            if variant == FASL_HASH_EQ_VARIANT:
                return hash_simple.make_simple_immutable_table(hash_simple.W_EqImmutableHashTable, keys, vals), pos
            elif variant == FASL_HASH_EQV_VARIANT:
                return hash_simple.make_simple_immutable_table(hash_simple.W_EqvImmutableHashTable, keys, vals), pos
            else: # variant == FASL_HASH_EQUAL_VARIANT:
                return W_EqualHashTable(keys, vals, immutable=True), pos
        elif typ == FASL_SRCLOC:
            # difficult to create an instance of srcloc struct so defer that to the runtime
            source, pos = self.fasl_to_sexp_recursive(fasl_string, pos)
            line, pos = self.fasl_to_sexp_recursive(fasl_string, pos)
            column, pos = self.fasl_to_sexp_recursive(fasl_string, pos)
            position, pos = self.fasl_to_sexp_recursive(fasl_string, pos)
            span, pos = self.fasl_to_sexp_recursive(fasl_string, pos)
            return v.to_list([v.W_Symbol.make("srcloc"), source, line, column, position, span]), pos
        else:
            if typ >= FASL_SMALL_INTEGER_START:
                return v.W_Fixnum((typ-FASL_SMALL_INTEGER_START)+FASL_LOWEST_SMALL_INTEGER), pos
            else:
                raise Exception("unrecognized fasl tag : %s" % typ)
Exemple #45
0
def racket_entry(names, config, command_line_arguments):
    from pycket.prims.general import executable_yield_handler
    from pycket.values import W_Fixnum

    if config['make-zos']:
        make_bootstrap_zos()
        return 0

    linklet_perf.init()

    loads, startup_options, flags = get_options(names, config)

    init_library     = startup_options['init_library'][0]
    set_run_file     = startup_options['set_run_file'][0]
    set_collects_dir = startup_options['set_collects_dir'][0]
    set_config_dir   = startup_options['set_config_dir'][0]
    set_addon_dir    = startup_options['set_addon_dir'][0]
    eval_sexp        = startup_options['eval_sexp'][0]
    run_as_linklet   = startup_options['run_as_linklet'][0]
    load_linklets    = startup_options['load_linklets']
    load_as_linklets = startup_options['load_as_linklets']

    is_repl          = flags['is_repl']
    no_lib           = flags['no_lib']
    just_kernel      = flags['just_kernel']
    just_init        = flags['just_init']
    use_compiled     = flags['use_compiled']
    debug            = flags['debug']
    version          = flags['version']
    c_a              = flags['compile_any']
    do_load_regexp   = flags['do_load_regexp']
    dev_mode         = flags['dev_mode']

    if load_as_linklets:
        for rkt in load_as_linklets:
            create_linklet_json(rkt)
            load_inst_linklet("%s.linklet" % rkt)
        return 0

    if load_linklets:
        load_linklets_at_startup(load_linklets)
        return 0

    if dev_mode:
        dev_mode_entry(dev_mode, eval_sexp, run_as_linklet)
        return 0

    with PerfRegion("startup"):
        initiate_boot_sequence(command_line_arguments,
                               use_compiled,
                               debug,
                               set_run_file,
                               set_collects_dir,
                               set_config_dir,
                               set_addon_dir,
                               compile_any=c_a,
                               do_load_regexp=do_load_regexp)

    if just_init:
        return 0

    namespace_require = get_primitive("namespace-require")
    load = get_primitive("load")

    if just_kernel:
        console_log("Running on just the #%kernel")
        namespace_require_kernel()

    if not no_lib:
        with PerfRegion("init-lib"):
            init_lib = W_WrappedConsProper.make(W_Symbol.make("lib"),
                                                W_WrappedConsProper.make(W_String.make(init_library), w_null))
            console_log("(namespace-require %s) ..." % init_lib.tostring())

            namespace_require_plus(init_lib)
            console_log("Init lib : %s loaded..." % (init_library))

    put_newline = False
    if loads:
        for rator_str, rand_str in loads:
            if rator_str == "load":
                # -f
                console_log("(load %s)" % (rand_str))
                load.call_interpret([W_String.make(rand_str)])
            elif rator_str == "file" or rator_str == "lib":
                # -t & -l
                require_spec = W_WrappedConsProper.make(W_Symbol.make(rator_str),
                                                        W_WrappedConsProper.make(W_String.make(rand_str), w_null))
                console_log("(namespace-require '(%s %s))" % (rator_str, rand_str))
                namespace_require_plus(require_spec)
            elif rator_str == "eval":
                # -e
                console_log("(eval (read (open-input-string %s)))" % rand_str)
                read_eval_print_string(rand_str, False, debug)

    if version:
        from pycket.env import w_version
        print("Welcome to Pycket v%s" % w_version.get_version())

    if is_repl: # -i
        put_newline = True
        dynamic_require = get_primitive("dynamic-require")
        repl = dynamic_require.call_interpret([W_Symbol.make("racket/repl"),
                                               W_Symbol.make("read-eval-print-loop")])
        from pycket.env import w_global_config
        w_global_config.set_config_val('repl_loaded', 1)
        repl.call_interpret([])

    if put_newline:
        print


    linklet_perf.print_report()

    # we just want the global value anyway
    eyh = executable_yield_handler.call_interpret([])
    eyh.call_interpret([W_Fixnum.ZERO])

    exit = get_primitive("exit")
    exit.call_interpret([])

    return 0
Exemple #46
0
def racket_read_file(file_name):
    oif = get_primitive("open-input-file")

    in_port = oif.call_interpret([W_String.make(file_name)])

    return racket_read(in_port)
Exemple #47
0
def symbol_to_string(v):
    if v.asciivalue is not None:
        return W_String.fromascii(v.asciivalue)
    return W_String.fromunicode(v.unicodevalue)
Exemple #48
0
def symbol_to_string(v):
    if v.asciivalue is not None:
        return W_String.fromascii(v.asciivalue)
    return W_String.fromunicode(v.unicodevalue)
Exemple #49
0
def string_to_bytes_locale(bytes, errbyte, start, end):
    # FIXME: This ignores the locale
    return W_String.fromstr_utf8(bytes.as_str())
Exemple #50
0
def bytes_to_string_latin(str, err, start, end):
    # XXX Not a valid implementation
    return W_String.fromascii("")
Exemple #51
0
    def fasl_to_sexp_recursive(self, fasl_string, pos):
        from pycket import values as v
        from pycket.values_string import W_String
        from pycket.values_regex import W_Regexp, W_PRegexp, W_ByteRegexp, W_BytePRegexp
        from pycket.vector import W_Vector
        from pycket.values_struct import W_Struct
        from pycket.prims.general import srcloc
        from pycket.hash import simple as hash_simple
        from pycket.hash.equal import W_EqualHashTable
        from pycket.prims.numeric import float_bytes_to_real
        from pycket.prims.string import _str2num
        from rpython.rlib.rbigint import rbigint
        from pycket.prims.input_output import build_path, bytes_to_path_element
        from pycket.ast_vs_sexp import to_rpython_list
        from pycket.racket_entry import get_primitive

        typ, pos = self.read_byte_no_eof(fasl_string, pos)

        if typ == FASL_GRAPH_DEF_TYPE:
            position, pos = self.read_fasl_integer(fasl_string, pos)
            val, pos = self.fasl_to_sexp_recursive(fasl_string, pos)
            if position >= self.GLOBAL_SHARED_COUNT:
                raise Exception("fasl: bad graph index")
            self.SHARED[position] = val
            return val, pos
        elif typ == FASL_GRAPH_REF_TYPE:
            position, pos = self.read_fasl_integer(fasl_string, pos)
            if position >= self.GLOBAL_SHARED_COUNT:
                raise Exception("fasl: bad graph index")
            return self.SHARED[position], pos
        elif typ == FASL_FALSE_TYPE:
            return v.w_false, pos
        elif typ == FASL_TRUE_TYPE:
            return v.w_true, pos
        elif typ == FASL_NULL_TYPE:
            return v.w_null, pos
        elif typ == FASL_VOID_TYPE:
            return v.w_void, pos
        elif typ == FASL_EOF_TYPE:
            return v.eof_object, pos
        elif typ == FASL_INTEGER_TYPE:
            num, pos = self.read_fasl_integer(fasl_string, pos)
            if isinstance(num, rbigint):
                return v.W_Bignum(num), pos
            return v.W_Fixnum(num), pos
        elif typ == FASL_FLONUM_TYPE:
            num_str, pos = self.read_bytes_exactly(fasl_string, pos, 8)
            return float_bytes_to_real(list(num_str), v.w_false), pos
        elif typ == FASL_SINGLE_FLONUM_TYPE:
            num_str, pos = self.read_bytes_exactly(fasl_string, pos, 4)
            real = float_bytes_to_real(list(num_str), v.w_false)
            return real.arith_exact_inexact(), pos
        elif typ == FASL_EXTFLONUM_TYPE:
            bstr_len, pos = self.read_fasl_integer(fasl_string, pos)
            num_str, pos = self.read_bytes_exactly(fasl_string, pos, bstr_len)
            return _str2num(W_String.fromstr_utf8(num_str).as_str_utf8(),
                            10), pos
        elif typ == FASL_RATIONAL_TYPE:
            num, pos = self.fasl_to_sexp_recursive(fasl_string, pos)
            den, pos = self.fasl_to_sexp_recursive(fasl_string, pos)
            return v.W_Rational.make(num, den), pos
        elif typ == FASL_COMPLEX_TYPE:
            re, pos = self.fasl_to_sexp_recursive(fasl_string, pos)
            im, pos = self.fasl_to_sexp_recursive(fasl_string, pos)
            return v.W_Complex.from_real_pair(re, im), pos
        elif typ == FASL_CHAR_TYPE:
            _chr, pos = self.read_fasl_integer(fasl_string, pos)
            return v.W_Character(unichr(_chr)), pos
        elif typ == FASL_SYMBOL_TYPE:
            sym_str, pos = self.read_fasl_string(fasl_string, pos)
            return v.W_Symbol.make(sym_str), pos
        elif typ == FASL_UNREADABLE_SYMBOL_TYPE:
            sym_str, pos = self.read_fasl_string(fasl_string, pos)
            return v.W_Symbol.make_unreadable(sym_str), pos
        elif typ == FASL_UNINTERNED_SYMBOL_TYPE:
            sym_str, pos = self.read_fasl_string(fasl_string, pos)
            return v.W_Symbol(sym_str), pos
        elif typ == FASL_KEYWORD_TYPE:
            key_str, pos = self.read_fasl_string(fasl_string, pos)
            return v.W_Keyword.make(key_str), pos
        elif typ == FASL_STRING_TYPE:
            str_str, pos = self.read_fasl_string(fasl_string, pos)
            return W_String.make(str_str), pos
        elif typ == FASL_IMMUTABLE_STRING_TYPE:
            str_str, pos = self.read_fasl_string(fasl_string, pos)
            return W_String.make(str_str).make_immutable(), pos
        elif typ == FASL_BYTES_TYPE:
            byts, pos = self.read_fasl_bytes(fasl_string, pos)
            return v.W_Bytes.from_string(byts, immutable=False), pos
        elif typ == FASL_IMMUTABLE_BYTES_TYPE:
            byts, pos = self.read_fasl_bytes(fasl_string, pos)
            return v.W_Bytes.from_string(byts), pos
        elif typ == FASL_PATH_TYPE:
            byts, pos = self.read_fasl_bytes(fasl_string, pos)
            return v.W_Path(byts), pos
        elif typ == FASL_RELATIVE_PATH_TYPE:
            wrt_dir = self.current_relative_dir
            p_w_lst, pos = self.fasl_to_sexp_recursive(fasl_string, pos)
            p_r_lst, _ = to_rpython_list(p_w_lst)
            rel_elems = [
                bytes_to_path_element(p) if isinstance(p, v.W_Bytes) else p
                for p in p_r_lst
            ]
            if wrt_dir:
                return build_path([wrt_dir] + rel_elems), pos
            elif rel_elems == []:
                return build_path([v.W_Symbol.make("same")]), pos
            else:
                return build_path(rel_elems), pos
        elif typ == FASL_PREGEXP_TYPE:
            str_str, pos = self.read_fasl_string(fasl_string, pos)
            reg_str = W_String.make(str_str)
            pregexp = get_primitive('pregexp')
            pregexp_obj = pregexp.call_interpret([reg_str])
            return pregexp_obj, pos
        elif typ == FASL_REGEXP_TYPE:
            str_str, pos = self.read_fasl_string(fasl_string, pos)
            reg_str = W_String.make(str_str)
            regexp = get_primitive('regexp')
            regexp_obj = regexp.call_interpret([reg_str])
            return regexp_obj, pos
        elif typ == FASL_BYTE_PREGEXP:
            str_str, pos = self.read_fasl_string(fasl_string, pos)
            reg_bytes = v.W_Bytes.from_string(str_str)
            byte_pregexp = get_primitive('byte-pregexp')
            byte_pregexp_obj = byte_pregexp.call_interpret([reg_bytes])
            return byte_pregexp_obj, pos
        elif typ == FASL_BYTE_REGEXP_TYPE:
            str_str, pos = self.read_fasl_string(fasl_string, pos)
            reg_bytes = v.W_Bytes.from_string(str_str)
            byte_regexp = get_primitive('byte-regexp')
            byte_regexp_obj = byte_regexp.call_interpret([reg_bytes])
            return byte_regexp_obj, pos
        elif typ == FASL_LIST_TYPE:
            list_len, pos = self.read_fasl_integer(fasl_string, pos)
            lst, pos = self.read_multi_into_rpython_list(
                fasl_string, pos, list_len)
            return v.to_list(lst), pos
        elif typ == FASL_PAIR_TYPE:
            car, pos = self.fasl_to_sexp_recursive(fasl_string, pos)
            cdr, pos = self.fasl_to_sexp_recursive(fasl_string, pos)
            return v.W_Cons.make(car, cdr), pos
        elif typ == FASL_LIST_STAR_TYPE:
            list_len, pos = self.read_fasl_integer(fasl_string, pos)
            # list_len is the length of the proper part
            lst, pos = self.read_multi_into_rpython_list(
                fasl_string, pos, list_len)
            # read the last element
            return_list, pos = self.fasl_to_sexp_recursive(fasl_string, pos)
            for i in range(list_len - 1, -1, -1):
                return_list = v.W_Cons.make(lst[i], return_list)
            return return_list, pos
        elif typ == FASL_VECTOR_TYPE or typ == FASL_IMMUTABLE_VECTOR_TYPE:
            vec_len, pos = self.read_fasl_integer(fasl_string, pos)
            storage, pos = self.read_multi_into_rpython_list(
                fasl_string, pos, vec_len)
            if typ == FASL_IMMUTABLE_VECTOR_TYPE:
                return W_Vector.fromelements(storage, immutable=True), pos
            return W_Vector.fromelements(storage), pos
        elif typ == FASL_BOX_TYPE:
            element, pos = self.fasl_to_sexp_recursive(fasl_string, pos)
            return v.W_MBox(element), pos
        elif typ == FASL_IMMUTABLE_BOX_TYPE:
            element, pos = self.fasl_to_sexp_recursive(fasl_string, pos)
            return v.W_IBox(element), pos
        elif typ == FASL_PREFAB_TYPE:
            key, pos = self.fasl_to_sexp_recursive(fasl_string, pos)
            length, pos = self.read_fasl_integer(fasl_string, pos)
            vals, pos = self.read_multi_into_rpython_list(
                fasl_string, pos, length)
            return W_Struct.make_prefab(key, vals), pos
        elif typ == FASL_HASH_TYPE:
            variant, pos = self.read_byte_no_eof(fasl_string, pos)
            length, pos = self.read_fasl_integer(fasl_string, pos)
            keys, vals, pos = self.read_multi_double_into_rpython_list(
                fasl_string, pos, length)
            if variant == FASL_HASH_EQ_VARIANT:
                return hash_simple.make_simple_mutable_table(
                    hash_simple.W_EqMutableHashTable, keys, vals), pos
            elif variant == FASL_HASH_EQV_VARIANT:
                return hash_simple.make_simple_mutable_table(
                    hash_simple.W_EqvMutableHashTable, keys, vals), pos
            else:  # variant == FASL_HASH_EQUAL_VARIANT:
                return W_EqualHashTable(keys, vals, immutable=False), pos
        elif typ == FASL_IMMUTABLE_HASH_TYPE:
            variant, pos = self.read_byte_no_eof(fasl_string, pos)
            length, pos = self.read_fasl_integer(fasl_string, pos)
            keys, vals, pos = self.read_multi_double_into_rpython_list(
                fasl_string, pos, length)
            if variant == FASL_HASH_EQ_VARIANT:
                return hash_simple.make_simple_immutable_table(
                    hash_simple.W_EqImmutableHashTable, keys, vals), pos
            elif variant == FASL_HASH_EQV_VARIANT:
                return hash_simple.make_simple_immutable_table(
                    hash_simple.W_EqvImmutableHashTable, keys, vals), pos
            else:  # variant == FASL_HASH_EQUAL_VARIANT:
                return W_EqualHashTable(keys, vals, immutable=True), pos
        elif typ == FASL_SRCLOC:
            # difficult to create an instance of srcloc struct so defer that to the runtime
            source, pos = self.fasl_to_sexp_recursive(fasl_string, pos)
            line, pos = self.fasl_to_sexp_recursive(fasl_string, pos)
            column, pos = self.fasl_to_sexp_recursive(fasl_string, pos)
            position, pos = self.fasl_to_sexp_recursive(fasl_string, pos)
            span, pos = self.fasl_to_sexp_recursive(fasl_string, pos)
            return W_Struct.make([source, line, column, position, span],
                                 srcloc), pos
        else:
            if typ >= FASL_SMALL_INTEGER_START:
                return v.W_Fixnum((typ - FASL_SMALL_INTEGER_START) +
                                  FASL_LOWEST_SMALL_INTEGER), pos
            else:
                raise Exception("unrecognized fasl tag : %s" % typ)
Exemple #52
0
def racket_read_file(file_name):
    oif = get_primitive("open-input-file")

    in_port = oif.call_interpret([W_String.make(file_name)])

    return racket_read(in_port)
Exemple #53
0
def racket_read_str(expr_str):
    ois = get_primitive("open-input-string")

    str_port = check_one_val(ois.call_interpret([W_String.make(expr_str)]))

    return racket_read(str_port)
Exemple #54
0
def string_to_keyword(keyword):
    return W_String.make(keyword.value)
Exemple #55
0
def bytes_to_string_latin(str, err, start, end):
    # XXX Not a valid implementation
    return W_String.fromascii("")
Exemple #56
0
def string_to_keyword(keyword):
    return W_String.make(keyword.value)
Exemple #57
0
def initiate_boot_sequence(command_line_arguments,
                           use_compiled=False,
                           debug=False,
                           set_run_file="",
                           set_collects_dir="",
                           set_config_dir="",
                           set_addon_dir="",
                           compile_any=False,
                           do_load_regexp=False):
    from pycket.env import w_version

    load_bootstrap_linklets(debug, do_load_regexp=do_load_regexp)

    with PerfRegion("set-params"):

        # These need to be set before the boot sequence
        if set_run_file:
            console_log("Setting the 'run-file path to %s" % set_run_file)
            set_path("run-file", set_run_file)

        if set_collects_dir:
            console_log("Setting the 'collects-dir path to %s" % set_collects_dir)
            set_path("collects-dir", set_collects_dir)

        if set_config_dir:
            console_log("Setting the 'config-dir path to %s" % set_config_dir)
            set_path("config-dir", set_config_dir)

        if set_addon_dir:
            console_log("Setting the 'addon-dir path to %s" % set_addon_dir)
            set_path("addon-dir", set_addon_dir)

        # Set the cmd arguments for racket
        ccla = get_primitive("current-command-line-arguments")
        ccla.call_interpret([W_Vector.fromelements(command_line_arguments)])

        # Run "boot" to set things like (current-module-name-resolver) (o/w it's going to stay as the
        # "core-module-name-resolver" which can't recognize modules like 'racket/base (anything other than
        # things like '#%kernel really)

        console_log("Entering Boot Sequence")

        console_log("(boot)")
        boot = get_primitive("boot")
        boot.call_interpret([])

        console_log("(current-library-collection-links (find-library-collection-links))")
        flcl = get_primitive("find-library-collection-links")
        lib_coll_links = flcl.call_interpret([])
        clcl = get_primitive("current-library-collection-links")
        clcl.call_interpret([lib_coll_links])

        console_log("(current-library-collection-paths (find-library-collection-paths))")
        flcp = get_primitive("find-library-collection-paths")
        lib_coll_paths = flcp.call_interpret([])
        clcp = get_primitive("current-library-collection-paths")
        clcp.call_interpret([lib_coll_paths])

        console_log("(read-accept-compiled true)")
        read_accept_compiled = get_primitive("read-accept-compiled")
        read_accept_compiled.call_interpret([w_true])

        compiled_file_path = "compiled/pycket"
        ucfp = get_primitive("use-compiled-file-paths")
        if use_compiled:
            console_log("(use-compiled-file-paths %s)" % compiled_file_path)
            ucfp.call_interpret([W_WrappedConsProper.make(W_String.make(compiled_file_path), w_null)])
        else:
            ucfp.call_interpret([w_null])
            console_log("(use-compiled-file-paths null)")

        cctm = get_primitive("current-compile-target-machine")
        if compile_any:
            cctm.call_interpret([w_false])

        # set the current directory to the current directory
        import os
        c_dir = os.getcwd()
        console_log("(current-directory %s)" % c_dir)
        # We can't call the current-directory like a primitive
        # because it prints a report to stdout
        from pycket.values_parameter import top_level_config
        from pycket.prims.general import current_directory_param
        c = top_level_config.get(current_directory_param)
        assert isinstance(c, W_ThreadCell)
        c.set(W_Path(c_dir))

        console_log("...Boot Sequence Completed")
        from pycket.env import w_global_config as glob
        glob.boot_is_completed()

    return 0
Exemple #58
0
 def to_sexp_from_file(self, file_name):
     from pycket.values_string import W_String
     from pycket.prims.input_output import open_infile
     port = open_infile(W_String.make(file_name), "rb")
     return self.to_sexp_from_w_port(port)
Exemple #59
0
def symbol_to_string_impl(v):
    asciivalue = v.asciivalue()
    if asciivalue is not None:
        return W_String.fromascii(asciivalue)
    return W_String.fromunicode(v.unicodevalue())