Пример #1
0
def test_parse():
    import pprint

    l = Lex('tests/test.ail')
    ts = l.lex()

    p = Parser('tests/test.ail')
    t = p.test(ts)
    pt = test_utils.make_ast_tree(t)
    pprint.pprint(pt)
Пример #2
0
def test_compiler():
    from core.aparser import Parser
    from core.alex import Lex

    l = Lex('../tests/test.ail')
    ts = l.lex()

    p = Parser('../tests/test.ail')
    t = p.parse(ts)

    c = Compiler(t)
    bf = c.compile(t)

    test_utils.show_bytecode(bf)
Пример #3
0
def launch_main(argv: list):
    init_paths()

    if len(argv) == 0:
        from core import ashell
        ashell.Shell().run_shell()
        return

    if len(argv) > 1:
        print('Usage : ./ail_launcher.py file')
        sys.exit(1)

    fpath = argv[0]

    try:
        from core.alex import Lex
        from core.aparser import Parser
        from core.acompiler import Compiler
        from core.avm import Interpreter

        ast = Parser(fpath).parse(Lex(fpath).lex())
        Interpreter().exec(
            Compiler(ast, filename=fpath).compile(ast).code_object)

    except FileExistsError as e:
        print('AIL : can\'t open file \'%s\' : %s' % (fpath, str(e)))
        sys.exit(1)
Пример #4
0
    def load_namespace(self, module_name: str) -> dict:
        from core.avm import Interpreter, Frame

        module_name = module_name.replace('.', '/')

        if module_name in self.__loaded.keys():
            return self.__loaded[module_name]

        p = self.__search_module(module_name)

        if p is None:
            return None

        if self.__get_type(p) == 'py':
            return self.__add_to_loaded(module_name,
                                        self.__load_py_namespace(p))

        elif self.__get_type(p) == 'ail':
            ast = Parser(p).parse(Lex(p).lex())
            cobj = Compiler(filename=p).compile(ast).code_object

            frame = Frame(cobj, cobj.varnames, cobj.consts)

            Interpreter().exec(cobj, frame)

            v = frame.variable

            return self.__add_to_loaded(module_name, v)

        return None
Пример #5
0
Файл: avm.py Проект: laomobk/AIL
def test_vm():
    import pickle
    from core.alex import Lex
    from core.aparser import Parser
    from core.acompiler import Compiler

    l = Lex('tests/test.ail')
    ts = l.lex()

    t = Parser('tests/test.ail').parse(ts)

    cbf = Compiler(filename='<TEST>').compile(t)

    co = cbf.code_object

    inter = Interpreter()
    inter.exec(co)
Пример #6
0
    def __init__(self):
        self.__buffer = []

        self.ps1 = '>> '
        self.ps2 = '.. '

        self.__more_level = 0

        self.__temp_name = '.temp.tmp'
        self.__fbuffer = open(self.__temp_name, 'w')

        self.__program = 'begin\n%s\nend\n'

        self.__main_frame = Frame()
        self.__main_frame.variable = _SHELL_NAMESPACE

        self.__lexer = Lex(self.__temp_name)
        self.__parser = Parser(self.__temp_name)
        self.__compiler = Compiler(filename='<shell>')
        self.__inter = Interpreter()
Пример #7
0
    def __get_more_line_state(self, line: str) -> int:
        '''
        :return : -1 end more | 0 normal | 1 start more
        '''
        self.__write(line)

        ts = Lex(self.__temp_name).lex()

        for tok in ts.token_list[::-1]:
            if tok.ttype == tokent.LAP_IDENTIFIER:
                if tok.value in _MORE_KEYWORD:
                    return 1
                if tok.value in _END_KEYWORD:
                    return -1

        return 0
Пример #8
0
class Shell:
    def __init__(self):
        self.__buffer = []

        self.ps1 = '>> '
        self.ps2 = '.. '

        self.__more_level = 0

        self.__temp_name = '.temp.tmp'
        self.__fbuffer = open(self.__temp_name, 'w')

        self.__program = 'begin\n%s\nend\n'

        self.__main_frame = Frame()
        self.__main_frame.variable = _SHELL_NAMESPACE

        self.__lexer = Lex(self.__temp_name)
        self.__parser = Parser(self.__temp_name)
        self.__compiler = Compiler(filename='<shell>')
        self.__inter = Interpreter()

    def __write(self, line: str):
        if self.__fbuffer.closed:
            self.__fbuffer = open(self.__temp_name, 'w')

        self.__fbuffer.write(line + '\n')

        self.__fbuffer.close()

    def __read(self) -> str:
        if self.__fbuffer.closed:
            self.__fbuffer = open(self.__temp_name, 'r', encoding='UTF-8')

        s = self.__fbuffer.read()

        self.__fbuffer.close()

        return s

    def __get_more_line_state(self, line: str) -> int:
        '''
        :return : -1 end more | 0 normal | 1 start more
        '''
        self.__write(line)

        ts = Lex(self.__temp_name).lex()

        for tok in ts.token_list[::-1]:
            if tok.ttype == tokent.LAP_IDENTIFIER:
                if tok.value in _MORE_KEYWORD:
                    return 1
                if tok.value in _END_KEYWORD:
                    return -1

        return 0

    def __print_welcome_text(self):
        v = ''

        try:
            v = AIL_VERSION
        except KeyError:
            pass

        print('AIL shell %s' % ('(AIL version = %s)' % v) if v else '')
        print('Type \'exit()\' to exit.\n')

    def __run_single_line(self, line: str, block=False):
        single_line = not block

        self.__write(self.__program % line)

        t = self.__lexer.lex(self.__temp_name)
        t = self.__parser.parse(t)
        cobj = self.__compiler.compile(t, single_line=single_line).code_object

        self.__main_frame.code = cobj
        self.__main_frame.varnames = cobj.varnames
        self.__main_frame.consts = cobj.consts

        self.__inter.exec(cobj, self.__main_frame)

        if self.__main_frame.stack:
            tof = self.__main_frame.stack.pop()
            if tof is not null.null:
                print(repr(tof))

    def __run_block(self):
        self.__run_single_line('\n'.join(self.__buffer), True)

        self.__buffer = []

    def __read_temp_file(self):
        return open(self.__temp_name).read()

    def run_shell(self):
        try:
            self.__run_shell()
        finally:
            os.remove(self.__temp_name)

    def __run_shell(self):
        self.__print_welcome_text()

        ps = self.ps1

        in_more = False

        while True:
            try:
                line = input(ps)

                more = self.__get_more_line_state(line)

                if more == 1:
                    self.__more_level += 1
                    in_more = True
                    ps = self.ps2
                    self.__buffer.append(line)

                elif more == -1:
                    self.__more_level -= 1 if self.__more_level > 0 else 0
                    self.__buffer.append(line)

                    if self.__more_level == 0:
                        in_more = False
                        ps = self.ps1
                        self.__run_block()

                elif self.__more_level >= 0:
                    if in_more:
                        self.__buffer.append(line)
                    else:
                        self.__run_single_line(line)

            except error._AILRuntimeError as e:
                in_more = False
                print(str(e))
                self.__main_frame.stack = []
                self.__buffer = []

            except EOFError as e:
                print()
                break

            except KeyboardInterrupt as e:
                in_more = False
                print('\n%s' % str(type(e).__name__))
                self.__buffer = []

            self.__main_frame.variable['__temp__'] = \
                    string.convert_to_string(self.__read_temp_file())

            os.remove(self.__temp_name)