Пример #1
0
def simulate(source, filename="<string>"):
    global options, snk
    getopts(["--simul", filename])
    node = parse(source, filename=filename)
    snk = snakes.plugins.load(options.plugins, "snakes.nets", "snk")
    build = Builder(snk)
    net = build.build(node)
    net.label(srcfile=filename, snakes=snk)
    return ABCDSimulator(node, net, draw(net, None))
Пример #2
0
def simulate (source, filename="<string>") :
    global options, snk
    getopts(["--simul", filename])
    node = parse(source, filename=filename)
    snk = snakes.plugins.load(options.plugins, "snakes.nets", "snk")
    build = Builder(snk)
    net = build.build(node)
    net.label(srcfile=filename, snakes=snk)
    return ABCDSimulator(node, net, draw(net, None))
Пример #3
0
        name = node.name
        if name in self and self[name].kind == Decl.TYPE :
            return self[name].type
        elif name in self.globals :
            obj = self.globals[name]
            if inspect.isclass(obj) :
                return self.snk.Instance(obj)
            elif inspect.isroutine(obj) :
                return self.snk.TypeCheck(obj)
        elif hasattr(sys.modules["__builtin__"], name) :
            obj = getattr(sys.modules["__builtin__"], name)
            if inspect.isclass(obj) :
                return self.snk.Instance(obj)
            elif inspect.isroutine(obj) :
                return self.snk.TypeCheck(obj)
        self._raise(CompilationError,
                    "invalid type %r" % name)

if __name__ == "__main__" :
    import doctest
    doctest.testmod(optionflags=doctest.NORMALIZE_WHITESPACE
                    | doctest.REPORT_ONLY_FIRST_FAILURE
                    | doctest.ELLIPSIS)
    from snakes.lang.abcd.parser import parse
    node = parse(open(sys.argv[1]).read())
    import snakes.plugins
    snk = snakes.plugins.load(["ops", "gv", "labels"], "snakes.nets", "snk")
    build = Builder(snk)
    net = build.build(node)
    net.draw(sys.argv[1] + ".png")
Пример #4
0
def main(args=sys.argv[1:], src=None):
    global options, snk
    # get options
    try:
        if src is None:
            getopts(args)
        else:
            getopts(list(args) + ["<string>"])
    except SystemExit:
        raise
    except:
        die(ERR_OPT, str(sys.exc_info()[1]))
    # read source
    if src is not None:
        source = src
    else:
        try:
            source = open(abcd).read()
        except:
            die(ERR_IO, "could not read input file %r" % abcd)
    # parse
    try:
        node = parse(source, filename=abcd)
    except ParseError:
        die(ERR_PARSE, str(sys.exc_info()[1]))
    except:
        bug()
    # compile
    dirname = os.path.dirname(abcd)
    if dirname and dirname not in sys.path:
        sys.path.append(dirname)
    elif "." not in sys.path:
        sys.path.append(".")
    try:
        snk = snakes.plugins.load(options.plugins, "snakes.nets", "snk")
    except:
        die(ERR_PLUGIN, str(sys.exc_info()[1]))
    build = Builder(snk)
    try:
        net = build.build(node)
        net.label(srcfile=abcd, snakes=snk)
    except (CompilationError, DeclarationError):
        die(ERR_COMPILE, str(sys.exc_info()[1]))
    except:
        bug()
    # output
    if options.pnml:
        save_pnml(net, options.pnml)
    for engine in gv_engines:
        target = getattr(options, "gv%s" % engine)
        if target:
            draw(net, target, engine)
    if options.html:
        try:
            html(abcd, node, net, draw(net, None), options.html)
        except:
            bug()
    trace, lineno = [], None
    if options.check:
        states, lineno, trace = Checker(net, options.progress).run()
        if options.progress:
            print("%s states explored" % states)
    if options.simul:
        engine = "dot"
        for eng in gv_engines:
            if getattr(options, "gv%s" % eng):
                engine = eng
                break
        try:
            simul = Simulator(node, net, draw(net, None, engine), options.port)
        except:
            bug()
        simul.start()
        if options.headless:
            with open(options.headless, "w") as out:
                out.write(
                    json({
                        "res": "%sr" % simul.url,
                        "url": simul.url,
                        "key": simul.server.httpd.key,
                        "host": "127.0.0.1",
                        "port": simul.port
                    }))
        else:
            webbrowser.open(simul.url)
        simul.wait()
    elif trace:
        if lineno is None:
            print("unsafe execution:")
        else:
            asserts = dict((a.lineno, a) for a in net.label("asserts"))
            print("line %s, %r failed:" %
                  (lineno, asserts[lineno].st.source()))
        for trans, mode in trace:
            print("  %s %s" % (trans, mode))
    return net
Пример #5
0
        name = node.name
        if name in self and self[name].kind == Decl.TYPE:
            return self[name].type
        elif name in self.globals:
            obj = self.globals[name]
            if inspect.isclass(obj):
                return self.snk.Instance(obj)
            elif inspect.isroutine(obj):
                return self.snk.TypeCheck(obj)
        elif hasattr(sys.modules["__builtin__"], name):
            obj = getattr(sys.modules["__builtin__"], name)
            if inspect.isclass(obj):
                return self.snk.Instance(obj)
            elif inspect.isroutine(obj):
                return self.snk.TypeCheck(obj)
        self._raise(CompilationError, "invalid type %r" % name)


if __name__ == "__main__":
    import doctest
    doctest.testmod(optionflags=doctest.NORMALIZE_WHITESPACE
                    | doctest.REPORT_ONLY_FIRST_FAILURE
                    | doctest.ELLIPSIS)
    from snakes.lang.abcd.parser import parse
    node = parse(open(sys.argv[1]).read())
    import snakes.plugins
    snk = snakes.plugins.load(["ops", "gv", "labels"], "snakes.nets", "snk")
    build = Builder(snk)
    net = build.build(node)
    net.draw(sys.argv[1] + ".png")
Пример #6
0
def main (args=sys.argv[1:], src=None) :
    global options, snk
    # get options
    try:
        if src is None :
            getopts(args)
        else :
            getopts(list(args) + ["<string>"])
    except SystemExit :
        raise
    except :
        die(ERR_OPT, str(sys.exc_info()[1]))
    # read source
    if src is not None :
        source = src
    else :
        try :
            source = open(abcd).read()
        except :
            die(ERR_IO, "could not read input file %r" % abcd)
    # parse
    try :
        node = parse(source, filename=abcd)
    except ParseError :
        die(ERR_PARSE, str(sys.exc_info()[1]))
    except :
        bug()
    # compile
    dirname = os.path.dirname(abcd)
    if dirname and dirname not in sys.path :
        sys.path.append(dirname)
    elif "." not in sys.path :
        sys.path.append(".")
    try :
        snk = snakes.plugins.load(options.plugins, "snakes.nets", "snk")
    except :
        die(ERR_PLUGIN, str(sys.exc_info()[1]))
    build = Builder(snk)
    try :
        net = build.build(node)
        net.label(srcfile=abcd, snakes=snk)
    except (CompilationError, DeclarationError) :
        die(ERR_COMPILE, str(sys.exc_info()[1]))
    except :
        bug()
    # output
    if options.pnml :
        save_pnml(net, options.pnml)
    for engine in gv_engines :
        target = getattr(options, "gv%s" % engine)
        if target :
            draw(net, target, engine)
    if options.html :
        try :
            html(abcd, node, net, draw(net, None), options.html)
        except :
            bug()
    trace, lineno = [], None
    if options.check :
        lineno, trace = Checker(net).run()
    if options.simul :
        try :
            simul = Simulator(node, net, draw(net, None), options.port)
        except :
            bug()
        simul.start()
        if options.headless :
            with open(options.headless, "w") as out :
                out.write(json({"res" : "%sr" % simul.url,
                                "url" : simul.url,
                                "key" : simul.server.httpd.key,
                                "host" : "127.0.0.1",
                                "port" : simul.port}))
        else :
            webbrowser.open(simul.url)
        simul.wait()
    elif trace :
        if lineno is None :
            print("unsafe execution:")
        else :
            asserts = dict((a.lineno, a) for a in net.label("asserts"))
            print("line %s, %r failed:"
                  % (lineno, asserts[lineno].st.source()))
        for trans, mode in trace :
            print("  %s %s" % (trans, mode))
    return net