def p_logical_or_expression(p): """ logical_or_expression : logical_and_expression | logical_or_expression OR_OP logical_and_expression """ if len(p) == 2: p[0] = p[1] else: p[0] = expressions.BinaryExpressionNode( "logical and", (lambda x, y: x or y), "(%s or %s)", (True, True), p[1], p[3] )
def p_inclusive_or_expression(p): """ inclusive_or_expression : exclusive_or_expression | inclusive_or_expression OR exclusive_or_expression """ if len(p) == 2: p[0] = p[1] else: p[0] = expressions.BinaryExpressionNode( "bitwise or", (lambda x, y: x | y), "(%s | %s)", (False, False), p[1], p[3] )
def p_logical_and_expression(p): """ logical_and_expression : inclusive_or_expression | logical_and_expression AND_OP inclusive_or_expression """ if len(p) == 2: p[0] = p[1] else: p[0] = expressions.BinaryExpressionNode( "logical and", (lambda x, y: x and y), "(%s and %s)", (True, True), p[1], p[3] )
def p_and_expression(p): """ and_expression : equality_expression | and_expression AND equality_expression """ if len(p) == 2: p[0] = p[1] else: p[0] = expressions.BinaryExpressionNode( "bitwise and", (lambda x, y: x & y), "(%s & %s)", (False, False), p[1], p[3] )
def p_exclusive_or_expression(p): """ exclusive_or_expression : and_expression | exclusive_or_expression XOR and_expression """ if len(p) == 2: p[0] = p[1] else: p[0] = expressions.BinaryExpressionNode( "bitwise xor", (lambda x, y: x ^ y), "(%s ^ %s)", (False, False), p[1], p[3] )
def p_shift_expression(p): """ shift_expression : additive_expression | shift_expression LEFT_OP additive_expression | shift_expression RIGHT_OP additive_expression """ if len(p) == 2: p[0] = p[1] else: name, op, format = shift_ops_dict[p[2]] p[0] = expressions.BinaryExpressionNode(name, op, format, (False, False), p[1], p[3])
def p_equality_expression(p): """ equality_expression : relational_expression | equality_expression EQ_OP relational_expression | equality_expression NE_OP relational_expression """ if len(p) == 2: p[0] = p[1] else: name, op, format = equality_ops_dict[p[2]] p[0] = expressions.BinaryExpressionNode(name, op, format, (False, False), p[1], p[3])
def p_additive_expression(p): """ additive_expression : multiplicative_expression | additive_expression PLUS multiplicative_expression | additive_expression MINUS multiplicative_expression """ if len(p) == 2: p[0] = p[1] else: name, op, format = add_ops_dict[p[2]] p[0] = expressions.BinaryExpressionNode(name, op, format, (False, False), p[1], p[3])
def p_multiplicative_expression(p): """ multiplicative_expression : cast_expression | multiplicative_expression TIMES cast_expression | multiplicative_expression DIVIDE cast_expression | multiplicative_expression MOD cast_expression """ if len(p) == 2: p[0] = p[1] else: name, op, format = mult_ops_dict[p[2]] p[0] = expressions.BinaryExpressionNode(name, op, format, (False, False), p[1], p[3])
def p_relational_expression(p): """ relational_expression : shift_expression | relational_expression LT shift_expression | relational_expression GT shift_expression | relational_expression LE_OP shift_expression | relational_expression GE_OP shift_expression """ if len(p) == 2: p[0] = p[1] else: name, op, format = rel_ops_dict[p[2]] p[0] = expressions.BinaryExpressionNode(name, op, format, (False, False), p[1], p[3])
def p_multi_string_literal(p): """ multi_string_literal : string_literal | macro_param | multi_string_literal string_literal | multi_string_literal macro_param """ if len(p) == 2: p[0] = p[1] else: p[0] = expressions.BinaryExpressionNode( "string concatenation", (lambda x, y: x + y), "(%s + %s)", (False, False), p[1], p[2] )
def p_assignment_expression(p): """ assignment_expression : conditional_expression | unary_expression assignment_operator assignment_expression """ if len(p) == 2: p[0] = p[1] else: # In C, the value of (x*=3) is the same as (x*3). We support that here. # However, we don't support the change in the value of x. if p[2] == "=": p[0] = p[3] else: name, op, format = assign_ops_dict[p[2]] p[0] = expressions.BinaryExpressionNode(name, op, format, (True, True), p[1], p[3])
def p_postfix_expression(p): """ postfix_expression : primary_expression | postfix_expression LBRACKET expression RBRACKET | postfix_expression LPAREN RPAREN | postfix_expression LPAREN argument_expression_list RPAREN | postfix_expression PERIOD IDENTIFIER | postfix_expression PTR_OP IDENTIFIER | postfix_expression INC_OP | postfix_expression DEC_OP """ if len(p) == 2: p[0] = p[1] elif p[2] == "[": p[0] = expressions.BinaryExpressionNode( "array access", (lambda a, b: a[b]), "(%s [%s])", (True, False), p[1], p[3] ) elif p[2] == "(": if p[3] == ")": p[0] = expressions.CallExpressionNode(p[1], []) else: p[0] = expressions.CallExpressionNode(p[1], p[3]) elif p[2] == ".": p[0] = expressions.AttributeExpressionNode( (lambda x, a: getattr(x, a)), "(%s.%s)", p[1], p[3] ) elif p[2] == "->": p[0] = expressions.AttributeExpressionNode( (lambda x, a: getattr(x.contents, a)), "(%s.contents.%s)", p[1], p[3] ) elif p[2] == "++": p[0] = expressions.UnaryExpressionNode( "increment", (lambda x: x + 1), "(%s + 1)", False, p[1] ) elif p[2] == "--": p[0] = expressions.UnaryExpressionNode( "decrement", (lambda x: x - 1), "(%s - 1)", False, p[1] )