Exemple #1
0
def p_formula_subtract(t):
    """formula_subtract : NUMBER '-' NUMBER
                        | formula '-' NUMBER
                        | formula '-' formula
    """
    if is_number(t[1]) and is_number(t[3]):
        t[0] = t[1] - t[3]
    elif is_number(t[1]):
        t[0] = formula(
            ast.nodemaker(stream.get_operator('add', ([t[1]], {})), t[3].ast),
            t[3].restrict)
    elif is_number(t[3]):
        t[0] = formula(
            ast.nodemaker(stream.get_operator('add', ([-t[3]], {})), t[1].ast),
            t[1].restrict)
    else:
        t[0] = formula(
            ast.nodemaker(
                operators.make_composition_operator([
                    stream.get_operator('paste', ([], {
                        'sort': None
                    })),
                    stream.get_operator('diff', ([], {
                        'axis': 1
                    }))
                ]), t[3].ast, t[1].ast), t[3].restrict + t[1].restrict)
Exemple #2
0
def p_formula_multiply(t):
    """formula_multiply : NUMBER '*' formula
                        | formula '*' NUMBER
                        | NUMBER '*' NUMBER
                        | formula '*' formula
    """
    if is_number(t[1]) and is_number(t[3]):
        t[0] = t[1] * t[3]
    elif is_number(t[1]):
        t[0] = formula(ast.nodemaker(stream.get_operator("multiply", ([t[1]], {})), t[3].ast), t[3].restrict)
    elif is_number(t[3]):
        t[0] = formula(ast.nodemaker(stream.get_operator("multiply", ([t[3]], {})), t[1].ast), t[1].restrict)
    else:
        t[0] = formula(
            ast.nodemaker(
                operators.make_composition_operator(
                    [
                        stream.get_operator("paste", ([], {"sort": None})),
                        stream.get_operator("product", ([], {"axis": 1})),
                    ]
                ),
                t[1].ast,
                t[3].ast,
            ),
            t[1].restrict + t[3].restrict,
        )
Exemple #3
0
def p_formula_multiply(t):
    """formula_multiply : NUMBER '*' formula
                        | formula '*' NUMBER
                        | NUMBER '*' NUMBER
                        | formula '*' formula
    """
    if is_number(t[1]) and is_number(t[3]):
        t[0] = t[1] * t[3]
    elif is_number(t[1]):
        t[0] = formula(
            ast.nodemaker(stream.get_operator('multiply', ([t[1]], {})),
                          t[3].ast), t[3].restrict)
    elif is_number(t[3]):
        t[0] = formula(
            ast.nodemaker(stream.get_operator('multiply', ([t[3]], {})),
                          t[1].ast), t[1].restrict)
    else:
        t[0] = formula(
            ast.nodemaker(
                operators.make_composition_operator([
                    stream.get_operator('paste', ([], {
                        'sort': None
                    })),
                    stream.get_operator('product', ([], {
                        'axis': 1
                    }))
                ]), t[1].ast, t[3].ast), t[1].restrict + t[3].restrict)
Exemple #4
0
def p_formula_comparator(t):
    """formula_comparator : formula comparator NUMBER
                          | NUMBER comparator formula
    """
    if is_number(t[1]):
        pass
    else:
        # have to initialize the comparison operator with an
        # appropriate inner AST node.  usually this is done by the
        # parser...
        cmp = ast.nodemaker(stream.get_operator(cmp_names[t[2]], ([t[3]], {})),
                            ast.leafmaker(stream.get_operator('null', ([], {}))))
        t[0] = formula(ast.nodemaker(stream.get_operator('nonzero', ([cmp], {})), t[1].ast),
                       t[1].restrict)
Exemple #5
0
def p_formula_comparator(t):
    """formula_comparator : formula comparator NUMBER
                          | NUMBER comparator formula
    """
    if is_number(t[1]):
        pass
    else:
        # have to initialize the comparison operator with an
        # appropriate inner AST node.  usually this is done by the
        # parser...
        cmp = ast.nodemaker(stream.get_operator(cmp_names[t[2]], ([t[3]], {})),
                            ast.leafmaker(stream.get_operator('null', ([], {}))))
        t[0] = formula(ast.nodemaker(stream.get_operator('nonzero', ([cmp], {})), t[1].ast),
                       t[1].restrict)
Exemple #6
0
def p_formula_subtract(t):
    """formula_subtract : NUMBER '-' NUMBER
                        | formula '-' NUMBER
                        | formula '-' formula
    """
    if is_number(t[1]) and is_number(t[3]):
        t[0] = t[1] - t[3]
    elif is_number(t[1]):
        t[0] = formula(ast.nodemaker(stream.get_operator('add', ([t[1]], {})), t[3].ast),
                       t[3].restrict)
    elif is_number(t[3]):
        t[0] = formula(ast.nodemaker(stream.get_operator('add', ([- t[3]], {})), t[1].ast),
                       t[1].restrict)
    else:
        t[0] = formula(ast.nodemaker(operators.make_composition_operator(
                    [stream.get_operator('paste', ([], {'sort': None})),
                     stream.get_operator('diff', ([], {'axis': 1}))]),
                                     t[3].ast, t[1].ast),
                       t[3].restrict + t[1].restrict)
Exemple #7
0
def p_formula_multiply(t):
    """formula_multiply : NUMBER '*' formula
                        | formula '*' NUMBER
                        | NUMBER '*' NUMBER
                        | formula '*' formula
    """
    if is_number(t[1]) and is_number(t[3]):
        t[0] = t[1] * t[3]
    elif is_number(t[1]):
        t[0] = formula(ast.nodemaker(stream.get_operator('multiply', ([t[1]], {})), t[3].ast),
                       t[3].restrict)
    elif is_number(t[3]):
        t[0] = formula(ast.nodemaker(stream.get_operator('multiply', ([t[3]], {})), t[1].ast),
                       t[1].restrict)
    else:
        t[0] = formula(ast.nodemaker(operators.make_composition_operator(
                    [stream.get_operator('paste', ([], {'sort': None})),
                     stream.get_operator('product', ([], {'axis': 1}))]),
                                     t[1].ast, t[3].ast),
                       t[1].restrict + t[3].restrict)
Exemple #8
0
def p_formula_pipe(t):
    """formula_pipe   : formula
                      | formula '<' formula_pipe"""
    if len(t) == 2:
        tree = t[1].ast
        restrict = t[1].restrict
        mapping = {}
    else:
        tree = ast.nodemaker(t[1].ast, t[3].ast)
        restrict = t[1].restrict + t[3].restrict
        mapping = t[3].mapping
    restrict, mapping = rename_restrictions(restrict, mapping)
    t[0] = formula_pipe(tree, restrict, mapping)
Exemple #9
0
def p_formula_pipe(t):
    """formula_pipe   : formula
                      | formula '<' formula_pipe"""
    if len(t) == 2:
        tree = t[1].ast
        restrict = t[1].restrict
        mapping = {}
    else:
        tree = ast.nodemaker(t[1].ast, t[3].ast)
        restrict = t[1].restrict + t[3].restrict
        mapping = t[3].mapping
    restrict, mapping = rename_restrictions(restrict, mapping)
    t[0] = formula_pipe(tree, restrict, mapping)
Exemple #10
0
def p_formula_subtract(t):
    """formula_subtract : NUMBER '-' NUMBER
                        | formula '-' NUMBER
                        | formula '-' formula
    """
    if is_number(t[1]) and is_number(t[3]):
        t[0] = t[1] - t[3]
    elif is_number(t[1]):
        t[0] = formula(ast.nodemaker(stream.get_operator("add", ([t[1]], {})), t[3].ast), t[3].restrict)
    elif is_number(t[3]):
        t[0] = formula(ast.nodemaker(stream.get_operator("add", ([-t[3]], {})), t[1].ast), t[1].restrict)
    else:
        t[0] = formula(
            ast.nodemaker(
                operators.make_composition_operator(
                    [stream.get_operator("paste", ([], {"sort": None})), stream.get_operator("diff", ([], {"axis": 1}))]
                ),
                t[3].ast,
                t[1].ast,
            ),
            t[3].restrict + t[1].restrict,
        )
Exemple #11
0
def p_formula(t):
    """formula : formula_where_clause
               | LVALUE arg_clause formula_where_clause
               | formula_multiply
               | formula_add
               | formula_subtract
               | formula_divide
               | formula_power
               | formula_comparator
    """
    if len(t) == 2:
        t[0] = t[1]
    else:
        if t[1] == "rename":
            restrict = [("rename", t[2][0])]
        else:
            restrict = []
        t[0] = formula(ast.nodemaker(make_operator(t[1], t[2]), t[3].ast), restrict + t[3].restrict)
Exemple #12
0
def p_formula(t):
    """formula : formula_where_clause
               | LVALUE arg_clause formula_where_clause
               | formula_multiply
               | formula_add
               | formula_subtract
               | formula_divide
               | formula_power
               | formula_comparator
    """
    if len(t) == 2:
        t[0] = t[1]
    else:
        if t[1] == 'rename':
            restrict = [('rename', t[2][0])]
        else:
            restrict = []
        t[0] = formula(ast.nodemaker(make_operator(t[1], t[2]), t[3].ast),
                       restrict + t[3].restrict)
Exemple #13
0
def p_formula_power(t):
    """formula_power : formula '^' NUMBER"""
    t[0] = formula(ast.nodemaker(stream.get_operator('power', ([t[3]], {})), t[1].ast),
                   t[1].restrict)
Exemple #14
0
def p_formula_divide(t):
    """formula_divide : formula '/' NUMBER"""
    t[0] = formula(ast.nodemaker(stream.get_operator('multiply', ([1. / t[3]], {})), t[1].ast),
                   t[1].restrict)
Exemple #15
0
def p_formula_power(t):
    """formula_power : formula '^' NUMBER"""
    t[0] = formula(
        ast.nodemaker(stream.get_operator('power', ([t[3]], {})), t[1].ast),
        t[1].restrict)
Exemple #16
0
def p_formula_divide(t):
    """formula_divide : formula '/' NUMBER"""
    t[0] = formula(
        ast.nodemaker(stream.get_operator('multiply', ([1. / t[3]], {})),
                      t[1].ast), t[1].restrict)