def primary(self, cursor: TokenOperator) -> Node:
        """
        primary具象構文木

        primary = num | identifier | "(" expression ")"
        """
        # (expr)
        if cursor.consume("("):
            node = self.expression(cursor)
            if cursor.consume(")") is False:
                raise ValueError("トークンのカッコが左右で対応していません")
            return node

        # identifier
        if cursor.chack_type(TokenKind.IDENTIFIER):
            value = cursor.get_value()
            cursor.proceed_cursor()

            new_node = self.create_new_node(value, NodeKind.LOCAL_VAR, None,
                                            None)
            new_node.offset = (ord(value) - ord("a") + 1) * 8
            return new_node

        if cursor.chack_type(TokenKind.NUMBER) is False:
            raise TypeError("数値型のトークンではありません")

        # num
        value = cursor.get_value()
        cursor.proceed_cursor()

        return self.create_new_node(value, NodeKind.NUMBER, None, None)
    def mul(self, cursor: TokenOperator) -> Node:
        """
        mul具象構文木

        mul = unary("*" unary | "/" unary)*
        """
        node = self.unary(cursor)

        while 1:
            if cursor.consume("*"):
                node = self.create_new_node("*", NodeKind.MUL, node, self.unary(cursor))
            elif cursor.consume("/"):
                node = self.create_new_node("/", NodeKind.DIV, node, self.unary(cursor))
            else:
                return node
    def add(self, cursor: TokenOperator) -> Node:
        """
        add具象構文木

        add = mul("+" mul| "-" mul)*
        """
        node = self.mul(cursor)

        while 1:
            if cursor.consume("+"):
                node = self.create_new_node("+", NodeKind.ADD, node, self.mul(cursor))
            elif cursor.consume("-"):
                node = self.create_new_node("-", NodeKind.SUB, node, self.mul(cursor))
            else:
                return node
    def equality(self, cursor: TokenOperator) -> Node:
        """
        equality具象構文木

        equality = relation ("==" relation | "!=" relation)*
        """
        node = self.relation(cursor)

        while 1:
            if cursor.consume("=="):
                node = self.create_new_node("==", NodeKind.EQUAL, node, self.relation(cursor))
            elif cursor.consume("!="):
                node = self.create_new_node("!=", NodeKind.NOT_EQUAL, node, self.relation(cursor))
            else:
                return node
    def unary(self, cursor: TokenOperator) -> Node:
        """
        unary具象構文木

        unary = ("+" | "-")? primary
        """
        if cursor.consume("+"):
            return self.primary(cursor)
        if cursor.consume("-"):
            return self.create_new_node(
                None,
                NodeKind.SUB,
                self.create_new_node(0, NodeKind.NUMBER, None, None),
                self.primary(cursor),
            )

        return self.primary(cursor)
예제 #6
0
def main(argv: list) -> int:
    """main関数"""
    if len(argv) != 2:
        raise ValueError("引数の個数が正しくありません")

    operator = TokenOperator(tokenize(argv[1]))
    parser = Parser(operator)
    nodes = parser.run()
    generate_code(nodes)
    def relation(self, cursor: TokenOperator) -> Node:
        """
        relation具象構文木

        relation = add ("<" add | "<=" add | ">" add | ">=" add )*
        """
        node = self.add(cursor)

        while 1:
            if cursor.consume(">="):
                node = self.create_new_node(">=", NodeKind.GREATER_EQUAL, node, self.add(cursor))
            elif cursor.consume(">"):
                node = self.create_new_node(">", NodeKind.GREATER, node, self.add(cursor))
            elif cursor.consume("<="):
                node = self.create_new_node("<=", NodeKind.LOWER_EQUAL, node, self.add(cursor))
            elif cursor.consume("<"):
                node = self.create_new_node("<", NodeKind.LOWER, node, self.add(cursor))
            else:
                return node
    def program(self, cursor: TokenOperator) -> List[Node]:
        """
        program具象構文木

        program = statement*
        """
        nodes = []
        while cursor.chack_type(TokenKind.EOF) is False:
            nodes.append(self.statement(cursor))

        return nodes
    def statement(self, cursor: TokenOperator) -> Node:
        """
        statement具象構文木

        statement = expression ";"
        """
        node = self.expression(cursor)

        if cursor.consume(";") is False:
            raise ValueError("プログラムの末尾が;で終わっていません")

        return node
    def assign(self, cursor: TokenOperator) -> Node:
        """
        assign具象構文木

        assign = equality ("=" assign)?
        """
        node = self.equality(cursor)

        if cursor.consume("="):
            node = self.create_new_node("=", NodeKind.ASSIGN, node, self.assign(cursor))

        return node