Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
tree: 43dd82e83e
Fetching contributors…

Cannot retrieve contributors at this time

file 667 lines (617 sloc) 18.175 kb
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667
"""
Perlito__Python__Runtime

DESCRIPTION

Provides runtime routines for the Perlito-in-Python compiled code

AUTHORS

The Pugs Team E<lt>perl6-compiler@perl.orgE<gt>.

COPYRIGHT

Copyright 2010, 2011 by Flavio Soibelmann Glock and others.

This program is free software; you can redistribute it and/or modify it
under the same terms as Perl itself.

See L<http://www.perl.com/perl/misc/Artistic.html>

"""

import sys
import re
import __builtin__

recursion = sys.getrecursionlimit()
if recursion < 2000:
    sys.setrecursionlimit(2000)

__all__ = ['mp6_to_num', 'mp6_to_scalar', 'mp6_to_bool', 'f_isa',
           'mp6_and', 'mp6_or', 'mp6_defined_or',
           'f_join', 'mp6_index', 'f_id', 'f_split',
           'mp6_Mu',
           'mp6_Array', 'mp6_Hash', 'mp6_Scalar',
           'mp6_Return',
           'Perlito__Match',
           'Perlito__Grammar', 'Perlito__Grammar_proto',
           'Main', 'Main_proto',
           'IO', 'IO_proto']

def f_print(*msg):
    for m in msg:
        sys.stdout.write(unicode(m).encode('utf-8'))
    return 1;
__builtin__.f_print = f_print

def f_say(*msg):
    for m in msg:
        sys.stdout.write(unicode(m).encode('utf-8'))
    sys.stdout.write("\n")
    return 1;
__builtin__.f_say = f_say

def f_warn(*msg):
    for m in msg:
        sys.stderr.write(unicode(m).encode('utf-8'))
    sys.stderr.write("\n")
__builtin__.f_warn = f_warn

def f_die(*msg):
    sys.stderr.write("Died: ")
    for m in msg:
        sys.stderr.write(unicode(m).encode('utf-8'))
    sys.stderr.write("\n")
    raise mp6_Die()
__builtin__.f_die = f_die

def f_map(v, f):
    try:
        return v.f_map(f)
    except AttributeError:
        return mp6_Array([])
__builtin__.f_map = f_map

def f_chr(n):
    return unichr(mp6_to_num(n))
__builtin__.f_chr = f_chr

def f_ord(n):
    return ord(unicode(n))
__builtin__.f_ord = f_ord

def mp6_to_scalar(v):
    try:
        return v.f_scalar()
    except AttributeError:
        return v

def mp6_to_bool(o):
    try:
        return o.f_bool()
    except AttributeError:
        if type(o) == type(False):
            return o
        if type(o) == type(1):
            return o != 0
        if type(o) == type(1.1):
            return o != 0
        if type(o) == type("a"):
            return o != "" and o != "0"
        if type(o) == type(u"a"):
            return o != u"" and o != u"0"
        return False

def mp6_and(x, y):
    if mp6_to_bool(x):
        return y()
    return x

def mp6_or(x, y):
    if mp6_to_bool(x):
        return x
    return y()

def mp6_defined_or(x, y):
    if f_isa(x, 'Mu'):
        return y()
    return x

def f_isa(v, name):
    try:
        v = v.f_get()
    except AttributeError:
        None
    try:
        return v.f_isa(name)
    except AttributeError:
        if type(v) == type(1):
            return (name == 'Int') or (name == 'Num')
        if type(v) == type(1.1):
            return name == 'Num'
        if type(v) == type("a"):
            return name == 'Str'
        if type(v) == type(u"a"):
            return name == 'Str'
        f_warn("Warning: Can't calculate .isa() on ", v.__class__.__name__, " ", f_perl(v))
        return False

def f_join(l, s):
    try:
        l = l.f_get()
    except AttributeError:
        None
    if not f_isa(l, 'Array'):
        return unicode(l)
    return s.join(l.f_map( lambda x: unicode(x) ).l)

def f_split(l, s):
    try:
        l = l.f_get()
    except AttributeError:
        None
    return mp6_Array(unicode(l).split(s))

def mp6_index(s, s2):
    try:
        return s.index(s2)
    except ValueError:
        return -1

def mp6_to_num(s):
    try:
        c = coerce(s, 0);
        return c[0]
    except TypeError:
        s = unicode(s)
        try:
            s.index(".")
            return float(s)
        except ValueError:
            try:
                return int(s)
            except ValueError:
                return 0
        except AttributeError:
            try:
                return int(s)
            except ValueError:
                return 0
            except TypeError:
                return 0

class mp6_Array:
    def __init__(self, l):
        self.l = l
    def __unicode__(self):
        return " ".join(map(lambda x: unicode(x), self.l))
    def __int__(self):
        return len(self.l)
    def f_bool(self):
        return len(self.l) > 0
    def __iter__(self):
        return self.l.__iter__()
    def f_perl(self, last_seen={}):
        seen = last_seen.copy()
        o = self.l
        try:
            if seen[id(o)]:
                return '*recursive*'
        except KeyError:
            None
        seen[id(o)] = True
        return "[" + ", ".join(map(lambda x: f_perl(x, seen), o)) + "]"
    def f_elems(self):
        return len(self.l)
    def f_push(self, s):
        try:
            self.l.append(s.f_get())
        except AttributeError:
            self.l.append(s)
    def f_unshift(self, s):
        try:
            self.l.insert(0, s.f_get())
        except AttributeError:
            self.l.insert(0, s)
    def f_pop(self):
        try:
            return self.l.pop()
        except IndexError:
            return mp6_Mu()
    def f_set(self, v):
        self.l = []
        v = v.l
        for x in range(0, len(v)):
            try:
                self.l.append(v[x].f_get())
            except AttributeError:
                self.l.append(v[x])
        return self
    def f_index_set(self, i, s):
        try:
            s = s.f_get()
        except AttributeError:
            None
        while True:
            try:
                self.l[i] = s
                return s
            except IndexError:
                self.l.append( mp6_Mu() )
    def f_shift(self):
        try:
            return self.l.pop(0)
        except IndexError:
            return mp6_Mu()
    def f_index(self, i):
        try:
            return mp6_Mu_get_proxy(self, i, self.l[i])
        except IndexError:
            return mp6_Mu_get_proxy(self, i, mp6_Mu())
    def f_isa(self, name):
        return name == 'Array'
    def str(self):
        return unicode(self.l)
    def f_map(self, f):
        result = []
        v = map(f, self.l)
        for x in range(0, len(v)):
            try:
                result.append(v[x].f_get())
            except AttributeError:
                result.append(v[x])
        return mp6_Array(result)
    def f_grep(self, f):
        result = []
        v = self.l
        for x in range(0, len(v)):
            item = v[x]
            if mp6_to_bool(f(item)):
                try:
                    result.append(v[x].f_get())
                except AttributeError:
                    result.append(v[x])
        return mp6_Array(result)
    def f_sort(self, f=lambda x: x):
        # TODO: add "key="
        result = sorted(self.l)
        return mp6_Array(result)
    def f_say(self):
        f_say(self)
 
class mp6_Hash:
    def __init__(self, l):
        self.l = l
    def __unicode__(self):
        return unicode(self.l)
    def __int__(self):
        return len(self.l)
    def f_bool(self):
        return len(self.l) > 0
    def __iter__(self):
        return self.l.__iter__()
    def __getitem__(self, k):
        return self.l.__getitem__(k)
    def f_perl(self, last_seen={}):
        seen = last_seen.copy()
        o = self.l
        try:
            if seen[id(o)]:
                return '*recursive*'
        except KeyError:
            None
        seen[id(o)] = True
        out = [];
        for i in o.keys():
            out.append(f_perl(i, seen) + " => " + f_perl(o[i], seen))
        return "{" + ", ".join(out) + "}";
    def values(self):
        return mp6_Array(self.l.values())
    def keys(self):
        return mp6_Array(self.l.keys())
    def f_pairs(self):
        out = [];
        for i in self.l.keys():
            out.append( Pair(v_key=i, v_value=self.__getitem__(i)) )
        return mp6_Array(out)
    def has_key(self, k):
        return self.l.has_key(k)
    def f_set(self, v):
        self.l = {}
        v = v.l
        for x in v.keys():
            try:
                self.l[x] = v[x].f_get()
            except AttributeError:
                self.l[x] = v[x]
        return self
    def f_index_set(self, i, s):
        try:
            i = i.f_get()
        except AttributeError:
            None
        try:
            self.l[i] = s.f_get()
        except AttributeError:
            self.l[i] = s
        return s
    def f_lookup(self, i):
        try:
            i = i.f_get()
        except AttributeError:
            None
        try:
            return mp6_Mu_get_proxy(self, i, self.l[i])
        except KeyError:
            return mp6_Mu_get_proxy(self, i, mp6_Mu())
    def f_isa(self, name):
        return name == 'Hash'
    def str(self):
        return unicode(self.l)

class mp6_Mu:
    def __unicode__(self):
        return ""
    def __int__(self):
        return 0
    def __float__(self):
        return 0.0
    def f_bool(self):
        return False
    def __add__(self, x):
        return x
    def __index__(self):
        return 0
    def __getitem__(self, k):
        return self
    def __iter__(self):
        return [].__iter__()
    def f_perl(self, seen={}):
        return "Mu"
    def f_isa(self, name):
        return name == 'Mu'

class mp6_Scalar:
    def __init__(self, v=mp6_Mu()):
        self.v = v
    def __getattr__(self,name):
        return getattr(self.v, name)
    def __unicode__(self):
        return unicode(self.v)
    def f_perl(self, seen={}):
        return f_perl(self.v, seen)
    def f_id(self):
        return f_id(self.v)
    def f_bool(self):
        return mp6_to_bool(self.v)
    def f_set(self, v):
        try:
            self.v = v.f_get()
        except AttributeError:
            self.v = v
    def f_get(self):
        return self.v
    def f_lookup(self, i):
        try:
            return self.v.f_lookup(i)
        except AttributeError:
            if self.v.f_isa('Mu'):
                return mp6_Mu_lookup_proxy(self, i)
            return self.v.f_lookup(i)
    def f_index(self, i):
        try:
            return self.v.f_index(i)
        except AttributeError:
            if self.v.f_isa('Mu'):
                return mp6_Mu_index_proxy(self, i)
            return self.v.f_index(i)

class mp6_Mu_get_proxy(mp6_Mu):
    def __init__(self, parent, i, v):
        self.parent = parent
        self.i = i
        self.v = v
    def __getattr__(self,name):
        return getattr(self.v, name)
    def __unicode__(self):
        return unicode(self.v)
    def f_perl(self, seen={}):
        return f_perl(self.v, seen)
    def f_id(self):
        return f_id(self.v)
    def f_bool(self):
        return mp6_to_bool(self.v)
    def f_get(self):
        return self.v
    def f_set(self, v):
        self.v = v
        self.parent.f_index_set(self.i, v)
    def f_lookup(self, i):
        try:
            return self.v.f_lookup(i)
        except AttributeError:
            return mp6_Mu_lookup_proxy(self, i)
    def f_index(self, i):
        try:
            return self.v.f_index(i)
        except AttributeError:
            return mp6_Mu_index_proxy(self, i)
    def f_isa(self, name):
        return self.v.f_isa(name)

class mp6_Mu_index_proxy(mp6_Mu):
    def __init__(self, parent, i):
        self.parent = parent
        self.i = i
    def f_set(self, v):
        Array_a = mp6_Array([])
        Array_a.f_index_set(self.i, v)
        self.parent.f_set(Array_a)
    def f_lookup(self, i):
        return mp6_Mu_lookup_proxy(self, i)
    def f_index(self, i):
        return mp6_Mu_index_proxy(self, i)

class mp6_Mu_lookup_proxy(mp6_Mu):
    def __init__(self, parent, i):
        self.parent = parent
        self.i = i
    def f_set(self, v):
        Hash_a = mp6_Hash({})
        Hash_a.f_index_set(self.i, v)
        self.parent.f_set(Hash_a)
    def f_lookup(self, i):
        return mp6_Mu_lookup_proxy(self, i)
    def f_index(self, i):
        return mp6_Mu_index_proxy(self, i)

class mp6_Die(Exception):
    def __init__(self):
        None
    def f_isa(self, name):
        return name == 'Exception::Die'

class mp6_Return(Exception):
    def __init__(self, value=mp6_Mu()):
        self.value = value
    def f_isa(self, name):
        return name == 'Exception::Return'

class Perlito__Match:
    def __init__(self, **arg):
        self.v_m = mp6_Hash({})
        self.v_to = mp6_Scalar()
        self.v_capture = mp6_Scalar()
        self.v_to.f_set(0)
        for k in arg:
            self.__dict__[k] = mp6_Scalar()
            self.__dict__[k].f_set(arg[k])
    def f__setattr__(self, k, v):
        try:
            self.__dict__[k].f_set(v)
        except KeyError:
            self.__dict__[k] = mp6_Scalar()
            self.__dict__[k].f_set(v)
        return self.__dict__[k]
    def __unicode__(self):
        if mp6_to_bool(self.v_bool):
            if not(f_isa(self.v_capture,'Mu')):
                return unicode(self.v_capture)
            return self.v_str[self.v_from:self.v_to]
        return u''
    def f_bool(self):
        return mp6_to_bool(self.v_bool)
    def f_set(self, m):
        self.v_m.f_set(m.v_m)
        self.v_to.f_set(m.v_to)
        self.v_str.f_set(m.v_str)
        self.v_from.f_set(m.v_from)
        self.v_bool.f_set(m.v_bool)
        self.v_capture.f_set(m.v_capture)
    def f_capture(self):
        return self.v_capture
    def f_index_set(self, k, v):
        return self.v_m.f_index_set(k, v)
    def f_lookup(self, k):
        return self.v_m.f_lookup(k)
    def f_scalar(self):
        if mp6_to_bool(self.v_bool):
            if not(f_isa(self.v_capture,'Mu')):
                return self.v_capture
            return self.v_str[self.v_from:self.v_to]
        return self.v_capture
    def has_key(self, k):
        return self.v_m.has_key(k)
    def f_from(self):
        return self.v_from
    def f_to(self):
        return self.v_to
    def f_isa(self, name):
        return name == 'Match'
Perlito__Match_proto = Perlito__Match()
__builtin__.Perlito__Match = Perlito__Match
__builtin__.Perlito__Match_proto = Perlito__Match_proto


try:
    type(Perlito__Grammar)
except NameError:
    class Perlito__Grammar:
        def __init__(self, **arg):
            self.__dict__.update(arg)
        def f_word(self, s, pos):
            m = re.match( r"\w", s[pos:] )
            if m:
                return Perlito__Match(v_str=s, v_from=pos, v_to=m.end() + pos, v_bool=1 )
            return Perlito__Match(v_bool=0)
        def f_digit(self, s, pos):
            m = re.match( r"\d", s[pos:] )
            if m:
                return Perlito__Match(v_str=s, v_from=pos, v_to=m.end() + pos, v_bool=1 )
            return Perlito__Match(v_bool=0)
        def f_space(self, s, pos):
            m = re.match( r"\s", s[pos:] )
            if m:
                return Perlito__Match(v_str=s, v_from=pos, v_to=m.end() + pos, v_bool=1 )
            return Perlito__Match(v_bool=0)
        def f_isa(self, name):
            return name == 'Grammar'
Perlito__Grammar_proto = Perlito__Grammar()
__builtin__.Perlito__Grammar = Perlito__Grammar
__builtin__.Perlito__Grammar_proto = Perlito__Grammar_proto


class Main:
    def __init__(self, **arg):
        self.__dict__.update(arg)
    def f__setattr__(v_self, k, v):
        v_self.__dict__[k] = v
        return v_self.__dict__[k]
    def f_bool(self):
        return 1
    def f_isa(v_self, name):
        return name == 'Main'
    def f_lisp_escape_string(self, s):
        o = s.replace( "\\", "\\\\");
        o = o.replace( '"', "\\\"");
        return o;
    def f_to_javascript_namespace(self, s):
        o = s.replace( "::", "$");
        return o;
    def f_to_lisp_namespace(self, s):
        o = s.replace( "::", "-");
        return "mp-" + o;
    def f_to_go_namespace(self, s):
        o = s.replace( "::", "__");
        return o
Main_proto = Main()
__builtin__.Main = Main
__builtin__.Main_proto = Main_proto


class IO:
    def __init__(v_self, **arg):
        v_self.__dict__.update(arg)
    def f__setattr__(v_self, k, v):
        v_self.__dict__[k] = v
        return v_self.__dict__[k]
    def f_bool(self):
        return 1
    def f_isa(v_self, name):
        return name == 'IO'
    def f_slurp(self, name):
        return file(unicode(name)).read().decode('utf-8')
IO_proto = IO()
__builtin__.IO = IO
__builtin__.IO_proto = IO_proto


def _dump(o, seen):
    try:
        if seen[id(o)]:
            return '*recursive*'
    except KeyError:
        None
    seen[id(o)] = True
    out = [];
    for i in o.__dict__.keys():
        out.append(i[2:] + " => " + f_perl(o.__dict__[i], seen))
    name = o.__class__.__name__.replace( "__", "::")
    return name + ".new(" + ", ".join(out) + ")"

def f_id(o):
    try:
        return o.f_id()
    except AttributeError:
        return id(o)

def f_perl(o, last_seen={}):
    seen = last_seen.copy()
    try:
        return o.f_perl(seen)
    except AttributeError:
        if type(o) == type(False):
            return unicode(o)
        if type(o) == type(1):
            return unicode(o)
        if type(o) == type(1.1):
            return unicode(o)
        if type(o) == type("a"):
            return '"' + o + '"'
        if type(o) == type(u"a"):
            return u'"' + o + u'"'
        if type(o) == type([]):
            try:
                if seen[id(o)]:
                    return '*recursive*'
            except KeyError:
                None
            seen[id(o)] = True
            return "PythonArray([" + ", ".join(map(lambda x: f_perl(x, seen), o)) + "])"
        if type(o) == type({}):
            try:
                if seen[id(o)]:
                    return '*recursive*'
            except KeyError:
                None
            seen[id(o)] = True
            out = [];
            for i in o.keys():
                out.append(i + " => " + f_perl(o[i], seen))
            return "PythonHash({" + ", ".join(out) + "})";
        return _dump(o, seen)
__builtin__.f_perl = f_perl

__builtin__.List_ARGS = mp6_Array([])
List_ARGS.l.extend( map(lambda x: x.decode("utf-8"), sys.argv[1:]) )
Something went wrong with that request. Please try again.