示例#1
0
def parse(input):
    program = []
    lines   = [rstring.strip_spaces(i) for i in rstring.split(input, '\n')]
    labels  = get_labels(lines)
    for line in lines:
        line = [i for i in rstring.split(line, ' ') if i]
        if not line:
            continue
        ins, args = line[0].upper(), [convert_arg(i, labels) for i in line[1:]]
        if ins[-1] == ':':
            continue
        elif ins == "COPY":
            val = Copy(args[0])
        elif ins == "CONST":
            val = Const(args[0])
        elif ins == "CALL":
            val = Call(args[0], args[1:])
        elif ins == "JUMP":
            val = Jump(args[0])
        elif ins == "RETURN":
            val = Return(args)
        elif ins == "PICK":
            val = Pick(args[0], args[1], args[2])
        elif ins in Binop.classes:
            val = Binop.get_cls(ins)(args[0], args[1])
        else:
            raise Exception("Unparsable instruction %s" % ins)
        program.append(val)
    return program[:]
示例#2
0
	def splitter(statement):
		values = [];
		_values = [];
		p = 0;
		m = False;
		if("," in statement):
			values = rstring.split(statement, ",")
			#values = statement.split(","); #strip the ( and )
			m = True;
		#for value in values:
		#	print value
		if(m == True):
			for value in values:
				p+=1;
				if value.find("(") != -1:
					if value == values[0]:
						f1 = value
						f0, f1 = rstring.split(f1, "(")
						_values.append(f1)
				elif value.startswith("("):
					value = value[1:]
					_values.append(value);
				
				elif value.endswith(")"):
					value = value[:-1]
					_values.append(value);
				else:
					_values.append(value)
		else:
			_values.append(statement);
		return _values
示例#3
0
    def check_split(value, sub, *args, **kwargs):
        result = kwargs['res']
        assert split(value, sub, *args) == result

        list_result = [list(i) for i in result]
        assert split(list(value), sub, *args) == list_result

        assert split(buffer(value), sub, *args) == result
示例#4
0
    def check_split(value, sub, *args, **kwargs):
        result = kwargs['res']
        assert split(value, sub, *args) == result

        list_result = [list(i) for i in result]
        assert split(list(value), sub, *args) == list_result

        assert split(buffer(value), sub, *args) == result
示例#5
0
def remove_const(name):
    tmplt_start = name.find("<")
    tmplt_stop  = name.rfind(">")
    if 0 <= tmplt_start and 0 <= tmplt_stop:
        # only replace const qualifying the class name, not in the template parameters
        return "".join([x.strip(" ") for x in rstring.split(name[:tmplt_start], "const")])+\
                     name[tmplt_start:tmplt_stop]+\
                     "".join([x.strip(" ") for x in rstring.split(name[tmplt_stop:], "const")])
    else:
        return "".join([x.strip(" ") for x in rstring.split(name, "const")])
示例#6
0
 def fn():
     res = True
     res = res and split('a//b//c//d', '//') == ['a', 'b', 'c', 'd']
     res = res and split('a//b//c//d', '//', 2) == ['a', 'b', 'c//d']
     res = res and split(u'a//b//c//d', u'//') == [u'a', u'b', u'c', u'd']
     res = res and split(u'endcase test', u'test') == [u'endcase ', u'']
     res = res and rsplit('a|b|c|d', '|', 2) == ['a|b', 'c', 'd']
     res = res and rsplit('a//b//c//d', '//') == ['a', 'b', 'c', 'd']
     res = res and rsplit(u'a|b|c|d', u'|') == [u'a', u'b', u'c', u'd']
     res = res and rsplit(u'a|b|c|d', u'|', 2) == [u'a|b', u'c', u'd']
     res = res and rsplit(u'a//b//c//d', u'//') == [u'a', u'b', u'c', u'd']
     return res
示例#7
0
    def descr_split(self, space, w_sep=None, maxsplit=-1):
        res = []
        value = self._val(space)
        if space.is_none(w_sep):
            res = split(value, maxsplit=maxsplit)
            return self._newlist_unwrapped(space, res)

        by = self._op_val(space, w_sep)
        if len(by) == 0:
            raise oefmt(space.w_ValueError, "empty separator")
        res = split(value, by, maxsplit)

        return self._newlist_unwrapped(space, res)
示例#8
0
    def descr_split(self, space, w_sep=None, maxsplit=-1):
        res = []
        value = self._val(space)
        if space.is_none(w_sep):
            res = split(value, maxsplit=maxsplit)
            return self._newlist_unwrapped(space, res)

        by = self._op_val(space, w_sep)
        if len(by) == 0:
            raise oefmt(space.w_ValueError, "empty separator")
        res = split(value, by, maxsplit)

        return self._newlist_unwrapped(space, res)
示例#9
0
def test_split():
    assert split("", 'x') == ['']
    assert split("a", "a", 1) == ['', '']
    assert split(" ", " ", 1) == ['', '']
    assert split("aa", "a", 2) == ['', '', '']
    assert split('a|b|c|d', '|') == ['a', 'b', 'c', 'd']
    assert split('a|b|c|d', '|', 2) == ['a', 'b', 'c|d']
    assert split('a//b//c//d', '//') == ['a', 'b', 'c', 'd']
    assert split('a//b//c//d', '//', 2) == ['a', 'b', 'c//d']
    assert split('endcase test', 'test') == ['endcase ', '']
    py.test.raises(ValueError, split, 'abc', '')
示例#10
0
文件: parser.py 项目: snim2/rcsp
def parse_bytecode_file(bytecode_file):
    """Parse a file of bytecode.

    The argument should be the text of the original file, with
    mnemonic bytecodes. The result will be a rcsp.box.ProgramBox
    object.
    """
    from rcsp.box import CodeBox, ProgramBox
    bytecode = []
    # Split bytecode into individual strings.
    # Throw away comments and whitespace.
    for line_ in bytecode_file.split('\n'):
        if DEBUG:
            line = line_.strip()
        else:
            line = rstring.strip_spaces(line_)
        if line.startswith('#'):
            continue
        else:
            if DEBUG:
                for code in line.split():
                    bytecode.append(code)
            else:
                bytecode.extend(rstring.split(line))
    # Parse bytecodes into numeric opcodes.
    functions = {}  # Dict of str names -> CodeBox objects
    counter = 0
    while counter < len(bytecode):
        opcodes, strings, integers, bools = [], [], [], []
        name = ''  # Name of each function which is parsed.
        code = bytecode[counter]
        # Parse a list of functions.
        if code == 'DEF':
            counter += 1  # Skip DEF.
            name = bytecode[counter]
            counter += 1  # Skip name of function.
            code = bytecode[counter]
            # TODO: Enable nested functions.
            while code != 'ENDDEF':
                # Handle general instructions.
                if code in opcode_mnemonics():
                    opcodes.append(opcode(code))
                # Handle literals.
                else:
                    if code.isdigit():
                        integers.append(int(code))
                        opcodes.append(len(integers) - 1)
                    elif code == 'True' or code == 'False':
                        bools.append(bool(code))
                        opcodes.append(len(bools) - 1)
                    else:
                        strings.append(code)
                        opcodes.append(len(strings) - 1)
                counter += 1
                code = bytecode[counter]
            functions[name] = CodeBox(opcodes, strings, integers, bools)
            # Ignore ENDDEF
            counter += 1
    return ProgramBox(functions)
示例#11
0
def test_split_unicode():
    assert split(u"", u"x") == [u""]
    assert split(u"a", u"a", 1) == [u"", u""]
    assert split(u" ", u" ", 1) == [u"", u""]
    assert split(u"aa", u"a", 2) == [u"", u"", u""]
    assert split(u"a|b|c|d", u"|") == [u"a", u"b", u"c", u"d"]
    assert split(u"a|b|c|d", u"|", 2) == [u"a", u"b", u"c|d"]
    assert split(u"a//b//c//d", u"//") == [u"a", u"b", u"c", u"d"]
    assert split(u"endcase test", u"test") == [u"endcase ", u""]
    py.test.raises(ValueError, split, u"abc", u"")
示例#12
0
def test_split_unicode():
    assert split(u"", u'x') == [u'']
    assert split(u"a", u"a", 1) == [u'', u'']
    assert split(u" ", u" ", 1) == [u'', u'']
    assert split(u"aa", u"a", 2) == [u'', u'', u'']
    assert split(u'a|b|c|d', u'|') == [u'a', u'b', u'c', u'd']
    assert split(u'a|b|c|d', u'|', 2) == [u'a', u'b', u'c|d']
    assert split(u'a//b//c//d', u'//') == [u'a', u'b', u'c', u'd']
    assert split(u'endcase test', u'test') == [u'endcase ', u'']
    py.test.raises(ValueError, split, u'abc', u'')
示例#13
0
def test_split_unicode():
    assert split(u"", u'x') == [u'']
    assert split(u"a", u"a", 1) == [u'', u'']
    assert split(u" ", u" ", 1) == [u'', u'']
    assert split(u"aa", u"a", 2) == [u'', u'', u'']
    assert split(u'a|b|c|d', u'|') == [u'a', u'b', u'c', u'd']
    assert split(u'a|b|c|d', u'|', 2) == [u'a', u'b', u'c|d']
    assert split(u'a//b//c//d', u'//') == [u'a', u'b', u'c', u'd']
    assert split(u'endcase test', u'test') == [u'endcase ', u'']
    py.test.raises(ValueError, split, u'abc', u'')
示例#14
0
def str_split__String_String_ANY(space, w_self, w_by, w_maxsplit=-1):
    maxsplit = space.int_w(w_maxsplit)
    value = w_self._value
    by = w_by._value
    bylen = len(by)
    if bylen == 0:
        raise OperationError(space.w_ValueError, space.wrap("empty separator"))
    res = split(value, by, maxsplit)
    return space.newlist_str(res)
示例#15
0
def str_split__String_String_ANY(space, w_self, w_by, w_maxsplit=-1):
    maxsplit = space.int_w(w_maxsplit)
    value = w_self._value
    by = w_by._value
    bylen = len(by)
    if bylen == 0:
        raise OperationError(space.w_ValueError, space.wrap("empty separator"))
    res = split(value, by, maxsplit)
    return space.newlist_str(res)
示例#16
0
def unicode_split__Unicode_Unicode_ANY(space, w_self, w_delim, w_maxsplit):
    self = w_self._value
    delim = w_delim._value
    maxsplit = space.int_w(w_maxsplit)
    delim_len = len(delim)
    if delim_len == 0:
        raise OperationError(space.w_ValueError,
                             space.wrap('empty separator'))
    parts = split(self, delim, maxsplit)
    return space.newlist_unicode(parts)
示例#17
0
 def fn():
     res = True
     res = res and split("a//b//c//d", "//") == ["a", "b", "c", "d"]
     res = res and split(" a\ta\na b") == ["a", "a", "a", "b"]
     res = res and split("a//b//c//d", "//", 2) == ["a", "b", "c//d"]
     res = res and split("abcd,efghi", ",") == ["abcd", "efghi"]
     res = res and split(u"a//b//c//d", u"//") == [u"a", u"b", u"c", u"d"]
     res = res and split(u"endcase test", u"test") == [u"endcase ", u""]
     res = res and rsplit("a|b|c|d", "|", 2) == ["a|b", "c", "d"]
     res = res and rsplit("a//b//c//d", "//") == ["a", "b", "c", "d"]
     res = res and rsplit(u"a|b|c|d", u"|") == [u"a", u"b", u"c", u"d"]
     res = res and rsplit(u"a|b|c|d", u"|", 2) == [u"a|b", u"c", u"d"]
     res = res and rsplit(u"a//b//c//d", u"//") == [u"a", u"b", u"c", u"d"]
     return res
示例#18
0
 def fn():
     res = True
     res = res and split('a//b//c//d', '//') == ['a', 'b', 'c', 'd']
     res = res and split(' a\ta\na b') == ['a', 'a', 'a', 'b']
     res = res and split('a//b//c//d', '//', 2) == ['a', 'b', 'c//d']
     res = res and split('abcd,efghi', ',') == ['abcd', 'efghi']
     res = res and split(u'a//b//c//d', u'//') == [u'a', u'b', u'c', u'd']
     res = res and split(u'endcase test', u'test') == [u'endcase ', u'']
     res = res and rsplit('a|b|c|d', '|', 2) == ['a|b', 'c', 'd']
     res = res and rsplit('a//b//c//d', '//') == ['a', 'b', 'c', 'd']
     res = res and rsplit(u'a|b|c|d', u'|') == [u'a', u'b', u'c', u'd']
     res = res and rsplit(u'a|b|c|d', u'|', 2) == [u'a|b', u'c', u'd']
     res = res and rsplit(u'a//b//c//d', u'//') == [u'a', u'b', u'c', u'd']
     return res
示例#19
0
    def descr_expandtabs(self, space, tabsize=8):
        value = self._val(space)
        if not value:
            return self._empty()

        if self._use_rstr_ops(space, self):
            splitted = value.split(self._chr('\t'))
        else:
            splitted = split(value, self._chr('\t'))

        try:
            ovfcheck(len(splitted) * tabsize)
        except OverflowError:
            raise oefmt(space.w_OverflowError, "new string is too long")
        expanded = oldtoken = splitted.pop(0)

        for token in splitted:
            expanded += self._multi_chr(self._chr(' ')) * self._tabindent(oldtoken,
                                                         tabsize) + token
            oldtoken = token

        return self._new(expanded)
示例#20
0
 def _split(self, splitter, splits=-1):
     return [StrObject(s) for s in split(self._s, splitter, splits)]
示例#21
0
def test_split_None():
    assert split("") == []
    assert split(" a\ta\na b") == ["a", "a", "a", "b"]
    assert split(" a a ", maxsplit=1) == ["a", "a "]
示例#22
0
 def split(self, splitter):
     return [StrObject(s) for s in split(self._s, splitter)]
示例#23
0
def test_split_None():
    assert split("") == []
    assert split(' a\ta\na b') == ['a', 'a', 'a', 'b']
    assert split(" a a ", maxsplit=1) == ['a', 'a ']
示例#24
0
def test_split_utf8():
    assert split('', 'a', isutf8=1) == ['']
    assert split('baba', 'a', isutf8=1) == ['b', 'b', '']
    assert split('b b', isutf8=1) == ['b', 'b']
    assert split('b\xe1\x9a\x80b', isutf8=1) == ['b', 'b']
示例#25
0
文件: data.py 项目: washort/typhon
 def split(self, splitter, splits=-1):
     if splits == -1:
         return [BytesObject(s) for s in split(self._bs, splitter)]
     else:
         return [BytesObject(s) for s in split(self._bs, splitter, splits)]
示例#26
0
    def method_split(self, space, w_sep=None, limit=0):
        if w_sep is None:
            res_w = []
            i = 0
            limit -= 1
            s = space.str_w(self)
            while True:
                while i < len(s):
                    if not s[i].isspace():
                        break
                    i += 1
                else:
                    break
                if limit == 0:
                    j = len(s)
                else:
                    j = i + 1
                    while j < len(s) and not s[j].isspace():
                        j += 1
                    limit -= 1
                res_w.append(space.newstr_fromstr(s[i:j]))
                i = j + 1
            return space.newarray(res_w)
        elif space.is_kind_of(w_sep, space.w_string):
            sep = space.str_w(w_sep)
            return space.newarray([
                space.newstr_fromstr(s) for s in split(space.str_w(self), sep, limit - 1)
            ])
        elif space.is_kind_of(w_sep, space.w_regexp):
            results_w = []
            n = 0
            last = 0
            string = space.str_w(self)
            ctx = w_sep.make_ctx(string)
            w_match = w_sep.get_match_result(space, ctx, found=True)

            while limit <= 0 or n + 1 < limit:
                if not self.search_context(space, ctx):
                    break
                elif ctx.match_start == ctx.match_end:
                    if ctx.match_start == ctx.end:
                        break
                    results_w.append(space.newstr_fromstr(string[last]))
                    last = ctx.match_end + 1
                else:
                    results_w.append(space.newstr_fromstr(string[last:ctx.match_start]))
                    for num in xrange(1, w_match.size(), 1):
                        begin, end = w_match.get_span(num)
                        begin += last
                        end += last
                        assert begin >= 0
                        assert end >= 0
                        results_w.append(space.newstr_fromstr(string[begin:end]))
                    last = ctx.match_end
                n += 1
                ctx.reset(last)

            if len(string) > last:
                results_w.append(space.newstr_fromstr(string[last:]))
            if limit < 0 or len(results_w) < limit:
                results_w.append(space.newstr_fromstr(""))
            return space.newarray(results_w)

        else:
            raise space.error(
                space.w_TypeError,
                "wrong argument type %s (expected Regexp)" %
                    space.obj_to_s(space.getclass(w_sep))
            )
示例#27
0
def split(a, b):
    affirm(rt.count(b) > 0, u"separator can't be empty")
    v = rt.vector()
    for s in rstring.split(rt.name(a), rt.name(b)):
        v = rt.conj(v, rt.wrap(s))
    return v
示例#28
0
文件: data.py 项目: dckc/typhon
 def _split(self, splitter, splits):
     return [BytesObject(s) for s in split(self._bs, splitter, splits)]
示例#29
0
    def method_split(self, space, w_sep=None, limit=0):
        if w_sep is None:
            res_w = []
            i = 0
            limit -= 1
            s = space.str_w(self)
            while True:
                while i < len(s):
                    if not s[i].isspace():
                        break
                    i += 1
                else:
                    break
                if limit == 0:
                    j = len(s)
                else:
                    j = i + 1
                    while j < len(s) and not s[j].isspace():
                        j += 1
                    limit -= 1
                res_w.append(space.newstr_fromstr(s[i:j]))
                i = j + 1
            return space.newarray(res_w)
        elif space.is_kind_of(w_sep, space.w_string):
            sep = space.str_w(w_sep)
            if sep:
                return space.newarray([
                    space.newstr_fromstr(s)
                    for s in split(space.str_w(self), sep, limit - 1)
                ])
            else:
                if limit:
                    raise space.error(
                        space.w_NotImplementedError,
                        "String#split with empty string and limit")
                return space.newarray([
                    space.newstr_fromstr(
                        self.strategy.getitem(self.str_storage, i))
                    for i in xrange(self.length())
                ])
        elif space.is_kind_of(w_sep, space.w_regexp):
            results_w = []
            n = 0
            last = 0
            string = space.str_w(self)
            ctx = w_sep.make_ctx(string)
            w_match = w_sep.get_match_result(space, ctx, string, found=True)

            while limit <= 0 or n + 1 < limit:
                if not self.search_context(space, ctx):
                    break
                elif ctx.match_start == ctx.match_end:
                    if ctx.match_start == ctx.end:
                        break
                    results_w.append(space.newstr_fromstr(string[last]))
                    last = ctx.match_end + 1
                else:
                    results_w.append(
                        space.newstr_fromstr(string[last:ctx.match_start]))
                    for num in xrange(1, w_match.size(), 1):
                        begin, end = w_match.get_span(num)
                        begin += last
                        end += last
                        if begin >= 0 and end >= 0:
                            results_w.append(
                                space.newstr_fromstr(string[begin:end]))
                    last = ctx.match_end
                n += 1
                ctx.reset(last)

            if len(string) > last:
                results_w.append(space.newstr_fromstr(string[last:]))
            if limit < 0 or len(results_w) < limit:
                results_w.append(space.newstr_fromstr(""))
            return space.newarray(results_w)

        else:
            raise space.error(
                space.w_TypeError, "wrong argument type %s (expected Regexp)" %
                (space.obj_to_s(space.getclass(w_sep))))
示例#30
0
 def split(self, splitter):
     return [BytesObject(s) for s in split(self._bs, splitter)]
示例#31
0
文件: data.py 项目: dckc/typhon
 def split(self, splitter):
     return [StrObject(s) for s in split(self._s, splitter)]
示例#32
0
def str_split__String_None_ANY(space, w_self, w_none, w_maxsplit=-1):
    maxsplit = space.int_w(w_maxsplit)
    value = w_self._value
    res = split(value, maxsplit=maxsplit)
    return space.newlist_str(res)
示例#33
0
文件: data.py 项目: dckc/typhon
 def _split(self, splitter, splits=-1):
     return [StrObject(s) for s in split(self._s, splitter, splits)]
示例#34
0
def test_split_None():
    assert split("") == []
    assert split(' a\ta\na b') == ['a', 'a', 'a', 'b']
    assert split(" a a ", maxsplit=1) == ['a', 'a ']
示例#35
0
文件: string.py 项目: zen3d/pixie
def split(a, b):
    affirm(rt.count(b) > 0, u"separator can't be empty")
    v = rt.vector()
    for s in rstring.split(rt.name(a), rt.name(b)):
        v = rt.conj(v, rt.wrap(s))
    return v
示例#36
0
 def _split(self, splitter, splits):
     return [BytesObject(s) for s in split(self._bs, splitter, splits)]
示例#37
0
文件: data.py 项目: washort/typhon
 def split(self, splitter, splits=-1):
     if splits == -1:
         return [StrObject(s) for s in split(self._s, splitter)]
     else:
         return [StrObject(s) for s in split(self._s, splitter, splits)]
示例#38
0
文件: string.py 项目: gordol/lever
def String_split(self, sep, maxsplit):
    out = []
    m = maxsplit.value if maxsplit else -1
    for s in split(self.string, sep.string, m):
        out.append(String(s))
    return space.List(out)
示例#39
0
文件: data.py 项目: dckc/typhon
 def split(self, splitter):
     return [BytesObject(s) for s in split(self._bs, splitter)]