Ejemplo n.º 1
0
def build(s_exp):
    '''
    Build Domain AST from the given S-expression
    '''
    assert_node(s_exp, s_expression.List)
    assert_child(s_exp, 0, s_expression.Symbol, lex_token.DOMAIN)
    name_expr = assert_child(s_exp, 1, s_expression.Symbol)

    domain = node_type.Domain()
    domain.name = lex_token.fixup_name(name_expr.text)

    for child_expr in s_exp.children[2:]:
        assert_node(child_expr, s_expression.List)
        symbol = assert_child(child_expr, 0, s_expression.Symbol)

        if symbol.text == lex_token.METHOD:
            method = build_method(child_expr)
            domain.children.append(method)
            domain.method_map[method.name] = method

        if symbol.text == lex_token.MODULE:
            module_name = assert_child(child_expr, 1, s_expression.Symbol).text

            if len(child_expr.children) == 3:
                symbol_name = assert_child(child_expr, 2, s_expression.Symbol).text
            else:
                symbol_name = None

            domain.modules.append((module_name, symbol_name))

        if lex_token.is_constant(symbol.text):
            rhs_expr = assert_child(child_expr, 1, [s_expression.List, s_expression.Symbol, s_expression.Number])
            domain.constants[symbol.text] = term.build(rhs_expr)

    return domain
Ejemplo n.º 2
0
def _build_recursive(s_expr):
    assert_node(s_expr, s_expression.List)
    symbol = assert_child(s_expr, 0, s_expression.Symbol)

    if token_to_op.has_key(symbol.text):
        return _build_logical_op(s_expr)

    if symbol.text == lex_token.CALL:
        return term.build(s_expr)

    return _build_atom(s_expr)
Ejemplo n.º 3
0
def _build_recursive(s_expr):
    assert_node(s_expr, s_expression.List)
    symbol = assert_child(s_expr, 0, s_expression.Symbol)

    if token_to_op.has_key(symbol.text):
        return _build_logical_op(s_expr)

    if symbol.text == lex_token.CALL:
        return term.build(s_expr)

    return _build_atom(s_expr)
Ejemplo n.º 4
0
def build_task_list(s_expr):
    task_list = []

    for child_expr in s_expr.children:
        assert_node(child_expr, s_expression.List)
        task_node = node_type.Atom()
        symbol = assert_child(child_expr, 0, s_expression.Symbol)
        task_node.name = lex_token.fixup_name(symbol.text)

        for arg_expr in child_expr.children[1:]:
            term_node = term.build(arg_expr)
            task_node.children.append(term_node)

        task_list.append(task_node)

    return task_list
Ejemplo n.º 5
0
def build_task_list(s_expr):
    task_list = []

    for child_expr in s_expr.children:
        assert_node(child_expr, s_expression.List)
        task_node = node_type.Atom()
        symbol = assert_child(child_expr, 0, s_expression.Symbol)
        task_node.name = lex_token.fixup_name(symbol.text)

        for arg_expr in child_expr.children[1:]:
            term_node = term.build(arg_expr)
            task_node.children.append(term_node)

        task_list.append(task_node)

    return task_list
Ejemplo n.º 6
0
def build_branches(s_exprs, method_node):
    index = 0
    branch_count = 0

    while index < len(s_exprs):
        s_expr = assert_node(s_exprs[index],
                             [s_expression.List, s_expression.Symbol])

        branch_name = 'branch_' + str(branch_count)

        if match_node(s_expr, s_expression.Symbol):
            branch_name = s_expr.text

            if index + 1 < len(s_exprs):
                index = index + 1
                s_expr = s_exprs[index]
            else:
                raise s_expression.SyntaxException(s_expr.pos)

        precondition = None
        task_list = None

        if match_node(s_expr, s_expression.List):
            if match_child(s_expr, 0, s_expression.Symbol, lex_token.FOREACH):
                precondition = assert_child(s_expr, 1, s_expression.List)
                task_list = assert_child(s_expr, 2, s_expression.List)
                index = index + 1
            else:
                if index + 1 < len(s_exprs):
                    precondition = s_expr
                    task_list = assert_node(s_exprs[index + 1],
                                            s_expression.List)
                    index = index + 2
                else:
                    raise s_expression.SyntaxException(s_expr.pos)

        if precondition != None and task_list != None:
            branch = build_branch(branch_name, precondition, task_list,
                                  method_node)
            method_node.children.append(branch)
            branch_count = branch_count + 1

            if match_child(s_expr, 0, s_expression.Symbol, lex_token.FOREACH):
                branch.foreach = True
        else:
            raise s_expression.SyntaxException(s_expr.pos)
Ejemplo n.º 7
0
def build_branches(s_exprs, method_node):
    index = 0
    branch_count = 0

    while index < len(s_exprs):
        s_expr = assert_node(s_exprs[index], [s_expression.List, s_expression.Symbol])

        branch_name = 'branch_' + str(branch_count)

        if match_node(s_expr, s_expression.Symbol):
            branch_name = s_expr.text
            
            if index + 1 < len(s_exprs):
                index = index + 1
                s_expr = s_exprs[index]
            else:
                raise s_expression.SyntaxException(s_expr.pos)

        precondition = None
        task_list = None

        if match_node(s_expr, s_expression.List):
            if match_child(s_expr, 0, s_expression.Symbol, lex_token.FOREACH):
                precondition = assert_child(s_expr, 1, s_expression.List)
                task_list = assert_child(s_expr, 2, s_expression.List)
                index = index + 1
            else:
                if index + 1 < len(s_exprs):
                    precondition = s_expr
                    task_list = assert_node(s_exprs[index + 1], s_expression.List)
                    index = index + 2
                else:
                    raise s_expression.SyntaxException(s_expr.pos)

        if precondition != None and task_list != None:
            branch = build_branch(branch_name, precondition, task_list, method_node)
            method_node.children.append(branch)
            branch_count = branch_count+1

            if match_child(s_expr, 0, s_expression.Symbol, lex_token.FOREACH):
                branch.foreach = True
        else:
            raise s_expression.SyntaxException(s_expr.pos)
Ejemplo n.º 8
0
def build(s_expr):
    '''
    Build AST from the given S-expression
    '''
    root_node = node_type.LogicalOp()
    assert_node(s_expr, s_expression.List)

    if len(s_expr.children) == 0:
        root_node.op = node_type.OP_AND
        return root_node

    if len(s_expr.children) > 1:
        root_node.op = node_type.OP_AND

        for child_expr in s_expr.children:
            child_node = _build_recursive(child_expr)
            root_node.children.append(child_node)
    else:
        root_node = _build_recursive(s_expr.children[0])

    return root_node
Ejemplo n.º 9
0
def build(s_expr):
    """
    Build AST from the given S-expression
    """
    root_node = node_type.LogicalOp()
    assert_node(s_expr, s_expression.List)

    if len(s_expr.children) == 0:
        root_node.op = node_type.OP_AND
        return root_node

    if len(s_expr.children) > 1:
        root_node.op = node_type.OP_AND

        for child_expr in s_expr.children:
            child_node = _build_recursive(child_expr)
            root_node.children.append(child_node)
    else:
        root_node = _build_recursive(s_expr.children[0])

    return root_node
Ejemplo n.º 10
0
def build_method(s_exp):
    signature_expr = assert_child(s_exp, 1, s_expression.List)

    method = node_type.Method()
    method.name = lex_token.fixup_name(assert_child(signature_expr, 0, s_expression.Symbol).text)

    for child_expr in signature_expr.children[1:]:
        method.variables.append(assert_node(child_expr, s_expression.Symbol).text)

    build_branches(s_exp.children[2:], method)

    return method
Ejemplo n.º 11
0
def build(s_exp):
    '''
    Build Domain AST from the given S-expression
    '''
    assert_node(s_exp, s_expression.List)
    assert_child(s_exp, 0, s_expression.Symbol, lex_token.DOMAIN)
    name_expr = assert_child(s_exp, 1, s_expression.Symbol)

    domain = node_type.Domain()
    domain.name = lex_token.fixup_name(name_expr.text)

    for child_expr in s_exp.children[2:]:
        assert_node(child_expr, s_expression.List)
        symbol = assert_child(child_expr, 0, s_expression.Symbol)

        if symbol.text == lex_token.METHOD:
            method = build_method(child_expr)
            domain.children.append(method)
            domain.method_map[method.name] = method

        if symbol.text == lex_token.MODULE:
            module_name = assert_child(child_expr, 1, s_expression.Symbol).text

            if len(child_expr.children) == 3:
                symbol_name = assert_child(child_expr, 2,
                                           s_expression.Symbol).text
            else:
                symbol_name = None

            domain.modules.append((module_name, symbol_name))

        if lex_token.is_constant(symbol.text):
            rhs_expr = assert_child(
                child_expr, 1,
                [s_expression.List, s_expression.Symbol, s_expression.Number])
            domain.constants[symbol.text] = term.build(rhs_expr)

    return domain
Ejemplo n.º 12
0
def build_method(s_exp):
    signature_expr = assert_child(s_exp, 1, s_expression.List)

    method = node_type.Method()
    method.name = lex_token.fixup_name(
        assert_child(signature_expr, 0, s_expression.Symbol).text)

    for child_expr in signature_expr.children[1:]:
        method.variables.append(
            assert_node(child_expr, s_expression.Symbol).text)

    build_branches(s_exp.children[2:], method)

    return method