pyslc.py 文件源码

python
阅读 24 收藏 0 点赞 0 评论 0

项目:PYSL 作者: sparkon 项目源码 文件源码
def parse_assignment(node: ast.AST) -> pysl.Assignment:
    """Block level assignment, can either be a declaration or not"""
    if not isinstance(node, ast.Assign) and not isinstance(node, ast.AnnAssign):
        error(loc(node), "Expected Assignment")
        return

    ret = pysl.Assignment()
    ret.set_location(node)
    ret.qualifiers = []
    if isinstance(node, ast.AnnAssign):
        # Declaration
        ret.name = node.target.id
        if isinstance(node.annotation, ast.Name):
            ret.type = node.annotation.id
        elif (isinstance(node.annotation, ast.Attribute) and
              isinstance(node.annotation.value, ast.Name)):
            if node.annotation.value.id == pysl.Language.Qualifier.CONST:
                ret.qualifiers.append(pysl.Language.Qualifier.CONST)
            else:
                error(loc(node.annotation), "Unsupported qualifier, only const is supported in a block assignment/declaration")
            ret.type = node.annotation.attr

        else:
            error(loc(node.annotation), "Unsupported annotation, supported are <type> or const.<type>")
    else:
        # Assignment
        if isinstance(node.targets[0], ast.Name):
            ret.name = node.targets[0].id
        elif isinstance(node.targets[0], ast.Attribute):
            ret.name = parse_attribute_no_eval(node.targets[0])
        else:
            error(loc(node), "Unsupported assignment target")
    if node.value and isinstance(node.value, ast.Name):
        ret.value = node.value.id
    elif node.value and isinstance(node.value, ast.Num):
        ret.value = str(node.value.n)
    else:
        ret.value = None
    return ret
评论列表
文章目录


问题


面经


文章

微信
公众号

扫码关注公众号