def reduce_MINUS_Expr(self, *kids): arg = kids[1].val if isinstance(arg, qlast.BaseRealConstant): # Special case for -<real_const> so that type inference based # on literal size works correctly in the case of INT_MIN and # friends. self.val = type(arg)(value=arg.value, is_negative=True) else: self.val = qlast.UnaryOp(op=kids[0].val, operand=arg)
def visit_ObjectField(self, node): fname = node.name.value # handle boolean ops if fname == 'and': return self._visit_list_of_inputs(node.value, 'AND') elif fname == 'or': return self._visit_list_of_inputs(node.value, 'OR') elif fname == 'not': return qlast.UnaryOp(op='NOT', operand=self.visit(node.value)) # handle various scalar ops op = gt.GQL_TO_OPS_MAP.get(fname) if op: value = self.visit(node.value) return qlast.BinOp(left=self._context.filter, op=op, right=value) # we're at the beginning of a scalar op _, target = self._get_parent_and_current_type() name = self.get_path_prefix() name.append(qlast.Ptr(ptr=qlast.ObjectRef(name=fname))) name = qlast.Path(steps=name) ftype = target.get_field_type(fname) typename = ftype.name if typename not in {'std::str', 'std::uuid'}: gql_type = gt.EDB_TO_GQL_SCALARS_MAP.get(typename) if gql_type == graphql.GraphQLString: # potentially need to cast the 'name' side into a # <str>, so as to be compatible with the 'value' name = qlast.TypeCast( expr=name, type=qlast.TypeName(maintype=qlast.ObjectRef(name='str')), ) self._context.filter = name value = self.visit(node.value) # we need to cast a target string into <uuid> or enum if typename == 'std::uuid' and not isinstance(value.right, qlast.TypeCast): value.right = qlast.TypeCast( expr=value.right, type=qlast.TypeName(maintype=qlast.ObjectRef(name='uuid')), ) elif ftype.is_enum(): value.right = qlast.TypeCast( expr=value.right, type=qlast.TypeName(maintype=qlast.ObjectRef(name=ftype.name)), ) return value
def visit_OperatorCall(self, node): args = node.args if node.operator_kind is ft.OperatorKind.INFIX: result = qlast.BinOp( left=self.visit(args[0].expr), right=self.visit(args[1].expr), op=node.func_shortname.name, ) elif node.operator_kind is ft.OperatorKind.PREFIX: result = qlast.UnaryOp( operand=self.visit(args[0].expr), op=node.func_shortname.name, ) elif node.func_shortname == 'std::IF': result = qlast.IfElse() result.condition = self.visit(args[0].expr) result.if_expr = self.visit(args[1].expr) result.else_expr = self.visit(args[2].expr) else: raise RuntimeError( f'unexpected operator kind: {node.operator_kind}') return result
def reduce_NOT_Expr(self, *kids): self.val = qlast.UnaryOp(op=kids[0].val.upper(), operand=kids[1].val)
def reduce_PLUS_Expr(self, *kids): self.val = qlast.UnaryOp(op=kids[0].val, operand=kids[1].val)
def reduce_DISTINCT_Expr(self, *kids): self.val = qlast.UnaryOp(op='DISTINCT', operand=kids[1].val)
def reduce_EXISTS_Expr(self, *kids): self.val = qlast.UnaryOp(op='EXISTS', operand=kids[1].val)