Exemple #1
0
def main():
    import gonelex
    import goneparse
    import gonecheck
    import gonecode
    import goneblock
    import sys
    import ctypes
    from errors import subscribe_errors, errors_reported
    from llvm.ee import ExecutionEngine

    # Load the Gone runtime library (see Makefile)
    ctypes._dlopen('./gonert.so', ctypes.RTLD_GLOBAL)

    lexer = gonelex.make_lexer()
    parser = goneparse.make_parser()
    with subscribe_errors(lambda msg: sys.stdout.write(msg+"\n")):
        program = parser.parse(open(sys.argv[1]).read())
        # Check the program
        gonecheck.check_program(program)
        # If no errors occurred, generate code
        if not errors_reported():
            functions = gonecode.generate_code(program)
            # Emit the code sequence
            bv = LLVMBlockVisitor()
            bv.generate_llvm(functions)
            print(bv.llvm.module)

            # Verify and run function that was created during code generation
            print(":::: RUNNING ::::")
            bv.llvm.function.verify()
            llvm_executor = ExecutionEngine.new(bv.llvm.module)
            llvm_executor.run_function(bv.llvm.vars['__start'], [])
def main():
    import gonelex
    import goneparse
    import sys
    from errors import subscribe_errors
    lexer = gonelex.make_lexer()
    parser = goneparse.make_parser()
    with subscribe_errors(lambda msg: sys.stdout.write(msg + "\n")):
        program = parser.parse(open(sys.argv[1]).read())
        # Check the program
        check_program(program)
def main():
    import gonelex
    import sys
    from errors import subscribe_errors
    lexer = gonelex.make_lexer()
    parser = make_parser()
    with subscribe_errors(lambda msg: sys.stdout.write(msg + "\n")):
        program = parser.parse(open(sys.argv[1]).read())

    # Output the resulting parse tree structure
    for depth, node in flatten(program):
        print("%s%s" % (" " * (4 * depth), node))
Exemple #4
0
def main():
    import gonelex
    import goneparse
    import gonecheck
    import gonecode
    import sys
    import ctypes
    import time
    import argparse
    from errors import subscribe_errors, errors_reported
    from llvm.ee import ExecutionEngine

    global args
    parser = argparse.ArgumentParser("Compile and run a Gone program from a .g file")
    parser.add_argument('file', type=str, default='', nargs=1,
                        help="the file containing Gone source")
    parser.add_argument('--verbose', '-v', action="store_true",
                        help="print verbose output")
    parser.add_argument('--validate', '-c', action="store_true",
                        help="perform llvm bitcode validation prior to program execution")
    args = parser.parse_args()

    # Load the Gone runtime library (see Makefile)
    ctypes._dlopen('./gonert.so', ctypes.RTLD_GLOBAL)

    lexer = gonelex.make_lexer()
    parser = goneparse.make_parser()
    with subscribe_errors(lambda msg: sys.stdout.write(msg + "\n")):
        program = parser.parse(open(args.file[0]).read())
        gonecheck.check_program(program)
        if not errors_reported():
            code = gonecode.generate_code(program)
            g = GenerateLLVMBlockVisitor()
            g.visit_functions(code.functions)

            if args.verbose:
                print("---- NATIVE ASSEMBLY ----")
                print(g.generator.module.to_native_assembly())
                print("---- END NATIVE ASSEMBLY ----")

            if args.verbose:
                print(":::: RUNNING ::::")
            start = time.time()

            llvm_executor = ExecutionEngine.new(g.generator.module)
            llvm_executor.run_function(g.generator.main_func, [])

            if args.verbose:
                print(":::: FINISHED ::::")
                print("execution time: {0:.15f}s".format(time.time() - start))
Exemple #5
0
def main():
    import gonelex
    import goneparse
    import gonecheck
    import sys
    from errors import subscribe_errors, errors_reported
    lexer = gonelex.make_lexer()
    parser = goneparse.make_parser()
    with subscribe_errors(lambda msg: sys.stdout.write(msg + "\n")):
        program = parser.parse(open(sys.argv[1]).read())
        # Check the program
        gonecheck.check_program(program)
        # If no errors occurred, generate code
        if not errors_reported():
            code = generate_code(program)
            code = EmitBlocksVisitor().loop(code.functions)
Exemple #6
0
def main():
    import gonelex
    import goneparse
    import gonecheck
    import sys
    from errors import subscribe_errors, errors_reported
    from pprint import pformat
    lexer = gonelex.make_lexer()
    parser = goneparse.make_parser()
    with subscribe_errors(lambda msg: sys.stdout.write(msg+"\n")):
        program = parser.parse(open(sys.argv[1]).read())
        # Check the program
        gonecheck.check_program(program)
        # If no errors occurred, generate code
        if not errors_reported():
            functions = generate_code(program)
            for fn in functions:
                print(fn, '\n')
Exemple #7
0
def main():
    import gonelex
    import goneparse
    import gonecheck
    import gonecode
    import sys
    from errors import subscribe_errors, errors_reported

    lexer = gonelex.make_lexer()
    parser = goneparse.make_parser()
    with subscribe_errors(lambda msg: sys.stdout.write(msg+"\n")):
        program = parser.parse(open(sys.argv[1]).read())
        # Check the program
        gonecheck.check_program(program)
        # If no errors occurred, generate code
        if not errors_reported():
            functions = gonecode.generate_code(program)
            functions = list(functions) # just be sure it doesn't exhause
            interpreter = Interpreter(functions)
            interpreter.run(functions[0]) # first func is the place to start
def main():
    import gonelex
    import goneparse
    import gonecheck
    import gonecode
    import sys
    import time
    from errors import subscribe_errors, errors_reported

    lexer = gonelex.make_lexer()
    parser = goneparse.make_parser()
    with subscribe_errors(lambda msg: sys.stdout.write(msg + "\n")):
        program = parser.parse(open(sys.argv[1]).read())
        # Check the program
        gonecheck.check_program(program)
        # If no errors occurred, generate code
        if not errors_reported():
            code = gonecode.generate_code(program)
            interpreter = Interpreter()
            start = time.clock()
            interpreter.run(code.code)
            print(":::: FINISHED ::::")
            print("execution time: {0:.15f}s".format(time.clock() - start))
Exemple #9
0
# testlex.py

import unittest
import gonelex

# Make the lexer object
lexer = gonelex.make_lexer()


class TestLexer(unittest.TestCase):
    def test_symbols(self):
        lexer.input('+ - * / ( ) = , ;')
        toks = list(iter(lexer.token, None))
        self.assertEqual(
            [t.type for t in toks],
            ['PLUS', 'MINUS', 'TIMES', 'DIVIDE', 'LPAREN', 'RPAREN',
             'ASSIGN', 'COMMA', 'SEMI'])

    def test_keywords(self):
        lexer.input('const var print func extern')
        toks = list(iter(lexer.token, None))
        self.assertEqual(
            [t.type for t in toks],
            ['CONST', 'VAR', 'PRINT', 'FUNC', 'EXTERN'])

    def test_identifiers(self):
        lexer.input('a z  A Z _a _z _A _Z a123 A123 a123z A123Z')
        toks = list(iter(lexer.token, None))
        self.assertTrue(all(t.type == 'ID' for t in toks))
        self.assertEqual(
            [t.value for t in toks],