<commit_before>// Copyright 2017 The Wuffs Authors.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//    https://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package check

import (
	"errors"
	"fmt"

	a "github.com/google/wuffs/lang/ast"
	t "github.com/google/wuffs/lang/token"
)

// otherHandSide returns the operator and other hand side when n is an
// binary-op expression like "thisHS == thatHS" or "thatHS < thisHS" (which is
// equivalent to "thisHS > thatHS"). If not, it returns (0, nil).
func otherHandSide(n *a.Expr, thisHS *a.Expr) (op t.ID, thatHS *a.Expr) {
	op = n.Operator()

	reverseOp := t.ID(0)
	switch op {
	case t.IDXBinaryNotEq:
		reverseOp = t.IDXBinaryNotEq
	case t.IDXBinaryLessThan:
		reverseOp = t.IDXBinaryGreaterThan
	case t.IDXBinaryLessEq:
		reverseOp = t.IDXBinaryGreaterEq
	case t.IDXBinaryEqEq:
		reverseOp = t.IDXBinaryEqEq
	case t.IDXBinaryGreaterEq:
		reverseOp = t.IDXBinaryLessEq
	case t.IDXBinaryGreaterThan:
		reverseOp = t.IDXBinaryLessThan
	}

	if reverseOp != 0 {
		if thisHS.Eq(n.LHS().AsExpr()) {
			return op, n.RHS().AsExpr()
		}
		if thisHS.Eq(n.RHS().AsExpr()) {
			return reverseOp, n.LHS().AsExpr()
		}
	}
	return 0, nil
}

type facts []*a.Expr

func (z *facts) appendFact(fact *a.Expr) {
	// TODO: make this faster than O(N) by keeping facts sorted somehow?
	for _, x := range *z {
		if x.Eq(fact) {
			return
		}
	}

	switch fact.Operator() {
	case 0:
		for _, x := range *z {
			if op, other := otherHandSide(x, fact); op == t.IDXBinaryEqEq {
				z.appendFact(other)
			}
		}
	case t.IDXBinaryAnd:
		z.appendFact(fact.LHS().AsExpr())
		z.appendFact(fact.RHS().AsExpr())
		return
	case t.IDXAssociativeAnd:
		for _, a := range fact.Args() {
			z.appendFact(a.AsExpr())
		}
		return
	}

	*z = append(*z, fact)<commit_msg>Remove unused appendFact code path<commit_after>// Copyright 2017 The Wuffs Authors.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//    https://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package check

import (
	"errors"
	"fmt"

	a "github.com/google/wuffs/lang/ast"
	t "github.com/google/wuffs/lang/token"
)

// otherHandSide returns the operator and other hand side when n is an
// binary-op expression like "thisHS == thatHS" or "thatHS < thisHS" (which is
// equivalent to "thisHS > thatHS"). If not, it returns (0, nil).
func otherHandSide(n *a.Expr, thisHS *a.Expr) (op t.ID, thatHS *a.Expr) {
	op = n.Operator()

	reverseOp := t.ID(0)
	switch op {
	case t.IDXBinaryNotEq:
		reverseOp = t.IDXBinaryNotEq
	case t.IDXBinaryLessThan:
		reverseOp = t.IDXBinaryGreaterThan
	case t.IDXBinaryLessEq:
		reverseOp = t.IDXBinaryGreaterEq
	case t.IDXBinaryEqEq:
		reverseOp = t.IDXBinaryEqEq
	case t.IDXBinaryGreaterEq:
		reverseOp = t.IDXBinaryLessEq
	case t.IDXBinaryGreaterThan:
		reverseOp = t.IDXBinaryLessThan
	}

	if reverseOp != 0 {
		if thisHS.Eq(n.LHS().AsExpr()) {
			return op, n.RHS().AsExpr()
		}
		if thisHS.Eq(n.RHS().AsExpr()) {
			return reverseOp, n.LHS().AsExpr()
		}
	}
	return 0, nil
}

type facts []*a.Expr

func (z *facts) appendFact(fact *a.Expr) {
	// TODO: make this faster than O(N) by keeping facts sorted somehow?
	for _, x := range *z {
		if x.Eq(fact) {
			return
		}
	}

	switch fact.Operator() {
	case t.IDXBinaryAnd:
		z.appendFact(fact.LHS().AsExpr())
		z.appendFact(fact.RHS().AsExpr())
		return
	case t.IDXAssociativeAnd:
		for _, a := range fact.Args() {
			z.appendFact(a.AsExpr())
		}
		return
	}

	*z = append(*z, fact)
{'fraction_non_alphanumeric': 0.12767918875316894, 'fraction_numerical': 0.005761696243374049, 'mean_word_length': 3.44672131147541, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 5, 'https://': 2, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6918789', 'n_tokens_mistral': 1615, 'n_tokens_neox': 1525, 'n_words': 465}