Ejemplo n.º 1
0
def main():
    in_stream = sys.stdin
    if len(sys.argv) > 1:
        try:
            in_stream = open(sys.argv[1], "r")
        except IOError:
            print_error(ErrorType.IOError)
            exit(1)

    tokens = lexer.lex(in_stream)
    parser_.parse(tokens)
Ejemplo n.º 2
0
    def test_parse_object(self):
        for test, expected in [
            ('{}', {}), ('{"name": "brent"}', {
                "name": "brent"
            }), ('{"name": "brent","age":22}', {
                "name": "brent",
                "age": 22
            }),
            ('{"name": "brent","age":22,"interests":["juggling","programming","reading"]}',
             {
                 "name": "brent",
                 "age": 22,
                 "interests": ["juggling", "programming", "reading"]
             }),
            ('''{
                "name": "brent",
                "age":22,
                "interests":[
                    "juggling","programming","reading"
                    ],
                "key1":{"key2":"value"}}
            ''', {
                "name": "brent",
                "age": 22,
                "interests": ["juggling", "programming", "reading"],
                "key1": {
                    "key2": "value"
                }
            }),
            ('''
            {
            "test"

            : {}
            }''', {
                "test": {}
            }),
            ('''
    {
        "first name":"Arthur",
        "last name": "Dent",
        "age":42,
        "interests": [
            "flying",
            "sandwich making"
        ]
    }
    ''', {
                "first name": "Arthur",
                "last name": "Dent",
                "age": 42,
                "interests": ["flying", "sandwich making"]
            })
        ]:
            self.assertEqual(parse(lex(test)), expected)
Ejemplo n.º 3
0
def __main():
    input_str = "(λx.x x)(λy.y)"
    tokens = tokenize(input_str)
    ast = parse(tokens)
    reduced = reduce(ast)

    print(input_str)
    print()
    pprint(tokens)
    print()
    pprint(ast)
    print()
    print(ast)
    print()
    print(reduced)
Ejemplo n.º 4
0
 def test_parse_array(self):
     for test, expected in [('[]', []),
                            ('["simple test"]', ["simple test"]),
                            ('[null, null, null]', [None, None, None]),
                            ('[[],[]]', [[], []]),
                            ('[[null,[]],[]]', [[None, []], []]),
                            ('''[
                 [
                     null,
                     []
                 ],
                 []
             ]''', [[None, []], []]),
                            ('''
         [
             null,
             ["test",123,null],
             [],
             [[]]
         ]
         ''', [None, ["test", 123, None], [], [[]]]),
                            ('''
         [
             null,
             [
                 "test"
             ]
         ]
         ''', [None, ["test"]]),
                            ('''
         [
             null,
             [
                 "test",
                 {"key":"value"}
             ]
         ]
         ''', [None, ["test", {
                                "key": "value"
                            }]])]:
         self.assertEqual(parse(lex(test)), expected)
Ejemplo n.º 5
0
    def handleLine_prompt(self, line):
        # XXX does this belong here?
        if line == TIMESEAL_PONG:
            self.session.pong(self.session.timeseal_last_timestamp)
            return

        if not utf8.check_user_utf8(line):
            print('command from %s ignored: invalid chars: %r' %
                (self.user.name, line))
            self.write(_("Command ignored: invalid characters.\n"))
            return
        line = line.decode('utf-8')

        if self.transport.compatibility:
            # decode special characters in Maciejg format
            line = utf8.decode_maciejg(line)

        global_.langs[self.user.vars_['lang']].install(names=['ngettext'])
        global_.curuser = self.user
        yield parser.parse(line, self)
        if self.state != 'quitting':
            assert(self.user)
            if self.user:
                self.user.write_prompt()
Ejemplo n.º 6
0
def compile_lisp(src):
    tokens = tokenizer.tokenize(src)
    ast = parser_.parse(tokens)
    asm = compiler.compile_(ast)
    bytecode = assembler.assemble(asm)
    return bytecode
Ejemplo n.º 7
0

def thread(tree):
    entry = AST.EntryNode()
    tree.thread(entry)
    return entry


if __name__ == '__main__':
    file = sys.argv[1]
    fileSplited = os.path.splitext(file)
    fileName = fileSplited[0]
    fileExtension = fileSplited[1]

    code = open(file).read()
    programs, errorList = parse(code)

    for i in range(len(programs)):
        program = programs[i]
        print("Program", i)
        print(program)

        graphName = fileName + "-" + str(i) + ".pdf"
        graph = program.makegraphicaltree()
        print("Generating graphical tree...")
        graph.write_pdf(graphName)

        graphNameThreaded = fileName + "-" + str(i) + "-threaded.pdf"
        programThreaded = thread(program)
        programThreaded.threadTree(graph)
Ejemplo n.º 8
0
            node = node.next[0]
        else:
            node = None

    return shapeList


if __name__ == "__main__":
    file = sys.argv[1]
    fileSplited = os.path.splitext(file)
    fileName = fileSplited[0]
    fileExtension = fileSplited[1]

    code = open(file).read()
    print("Parsing file :", file)
    programs, errors = parse(code)

    if len(errors) > 0:
        print("Errors :")
        print("\n".join(errors))
        print("Compilation aborted")
        exit()

    i = 0
    for program in programs:
        # Each code part delimitated with braces will result in an individual .svg output file
        entry = thread(program)
        shapeList = synthese(entry)

        name = fileName
        if len(programs) > 1:
Ejemplo n.º 9
0
 def test_parse_none(self):
     self.assertEqual(parse(lex('')), None)
Ejemplo n.º 10
0
    espaceTablulation = "";
    for a in range(0,compteurIncrementation+1):
        espaceTablulation+="\t";

    bytecode += "%s%s\n" %(espaceTablulation,self.children[3].compile())

    compteurIncrementation -=1

    espaceTablulation = "";
    for a in range(0,compteurIncrementation+1):
        espaceTablulation+="\t";


    return bytecode


if __name__ == "__main__":
    #connot call a file "parser.py" !
    from parser_ import parse
    import sys
    import os
    prog = open(sys.argv[1]).read()
    ast = parse(prog)
    compiled = "import turtle\nt=turtle.Turtle()\nt.speed(1)\nt.shape('turtle')\n"
    compiled += ast.compile()
    compiled += "wn = turtle.Screen()\nwn.exitonclick()"
    name = os.path.splitext(sys.argv[1])[0]+'.py'
    outfile = open(name, 'w')
    outfile.write(compiled)
    outfile.close()
    print ("Wrote output to", name)