<commit_before>package sexp

import (
	"fmt"
	"io"
)

func (atom *Bool) WriteTo(w io.Writer) (int64, error) {
	if atom.Val {
		return fprint(w, "t")
	}
	return fprint(w, "nil")
}

func (atom *Int) WriteTo(w io.Writer) (int64, error) {
	return fprintf(w, "%d", atom.Val)
}

func (atom *Float) WriteTo(w io.Writer) (int64, error) {
	return fprintf(w, "%f", atom.Val)
}

func (atom *String) WriteTo(w io.Writer) (int64, error) {
	return fprintf(w, "%s", atom.Val)
}

func (atom *Var) WriteTo(w io.Writer) (int64, error) {
	return fprintf(w, "%s", atom.Name)
}

func (lit *ArrayLit) WriteTo(w io.Writer) (int64, error) {
	return writeList(w, "array", lit.Vals)
}

func (lit *QuotedArray) WriteTo(w io.Writer) (int64, error) {
	return writeList(w, "quoted-array", lit.Vals)
}

func (block *Block) WriteTo(w io.Writer) (int64, error) {
	return writeList(w, "block", block.Nodes)
}

func (node *If) WriteTo(w io.Writer) (int64, error) {
	if node.Else == nil {
		return writeList(w, "if", node.Test, node.Then, "nil")
	}
	return writeList(w, "if", node.Test, node.Then, node.Else)
}

func (node *Bind) WriteTo(w io.Writer) (int64, error) {
	return writeList(w, "bind", node.Sym, " ", node.Val)
}

func (node *Return) WriteTo(w io.Writer) (int64, error) {
	return writeList(w, "return", node.Result)
}

var binaryOpHead = []string{
	OpRem: "%",
}

var variadicOpHead = []string{
	OpBitOr:  "bit-or",
	OpBitAnd: "bit-and",
	OpBitXor: "bit-xor",
	OpAdd:    "+",
	OpSub:    "-",
	OpMul:    "*",
	OpDiv:    "/",
	OpEq:     "=",
	OpGt:     ">",
	OpGte:    ">=",
	OpLt:     "<",
	OpLte:    "<=",
}

func (op *VariadicOp) WriteTo(w io.Writer) (int64, error) {
	return writeList(w, variadicOpHead[op.Type], op.Args)
}

func (op *BinaryOp) WriteTo(w io.Writer) (int64, error) {
	return writeList(w, binaryOpHead[op.Type], op.Arg1, op.Arg2)
}

func (call *Call) WriteTo(w io.Writer) (int64, error) {
	return writeList(w, call.Fn, call.Args)
}

type sexpWriter struct {
	written int64
	dst     io.Writer
	err     error
}

func (sw *sexpWriter) writeWith(w io.WriterTo) {
	if sw.err == nil {
		n, err := w.WriteTo(sw.dst)
		sw.written += n
		sw.err = err
	}
}

func (sw *sexpWriter) write(data []byte) {
	if sw.err == nil {
		n, err := sw.dst.Write(data)
		sw.written += int64(n)
		sw.err = err
	}
}

func (sw *sexpWriter) writeString(s string) {
	sw.write([]byte(s))
}

func (sw *sexpWriter) writeByte(b byte) {
	sw.write([]byte{b})
}

func (sw *sexpWriter) writeForm(form interface{}) {
	switch form := form.(type) {
	case []Node:
		for i := 0; i < len(form)-1; i++ {
			sw.writeWith(form[i])
			sw.writeByte(' ')
		}
		sw.writeWith(form[len(form)-1])
	case io.WriterTo:
		sw.writeWith(form)
	case string:<commit_msg>check for len(form) == 0<commit_after>package sexp

import (
	"fmt"
	"io"
)

func (atom *Bool) WriteTo(w io.Writer) (int64, error) {
	if atom.Val {
		return fprint(w, "t")
	}
	return fprint(w, "nil")
}

func (atom *Int) WriteTo(w io.Writer) (int64, error) {
	return fprintf(w, "%d", atom.Val)
}

func (atom *Float) WriteTo(w io.Writer) (int64, error) {
	return fprintf(w, "%f", atom.Val)
}

func (atom *String) WriteTo(w io.Writer) (int64, error) {
	return fprintf(w, "%s", atom.Val)
}

func (atom *Var) WriteTo(w io.Writer) (int64, error) {
	return fprintf(w, "%s", atom.Name)
}

func (lit *ArrayLit) WriteTo(w io.Writer) (int64, error) {
	return writeList(w, "array", lit.Vals)
}

func (lit *QuotedArray) WriteTo(w io.Writer) (int64, error) {
	return writeList(w, "quoted-array", lit.Vals)
}

func (block *Block) WriteTo(w io.Writer) (int64, error) {
	return writeList(w, "block", block.Nodes)
}

func (node *If) WriteTo(w io.Writer) (int64, error) {
	if node.Else == nil {
		return writeList(w, "if", node.Test, node.Then, "nil")
	}
	return writeList(w, "if", node.Test, node.Then, node.Else)
}

func (node *Bind) WriteTo(w io.Writer) (int64, error) {
	return writeList(w, "bind", node.Sym, " ", node.Val)
}

func (node *Return) WriteTo(w io.Writer) (int64, error) {
	return writeList(w, "return", node.Result)
}

var binaryOpHead = []string{
	OpRem: "%",
}

var variadicOpHead = []string{
	OpBitOr:  "bit-or",
	OpBitAnd: "bit-and",
	OpBitXor: "bit-xor",
	OpAdd:    "+",
	OpSub:    "-",
	OpMul:    "*",
	OpDiv:    "/",
	OpEq:     "=",
	OpGt:     ">",
	OpGte:    ">=",
	OpLt:     "<",
	OpLte:    "<=",
}

func (op *VariadicOp) WriteTo(w io.Writer) (int64, error) {
	return writeList(w, variadicOpHead[op.Type], op.Args)
}

func (op *BinaryOp) WriteTo(w io.Writer) (int64, error) {
	return writeList(w, binaryOpHead[op.Type], op.Arg1, op.Arg2)
}

func (call *Call) WriteTo(w io.Writer) (int64, error) {
	return writeList(w, call.Fn, call.Args)
}

type sexpWriter struct {
	written int64
	dst     io.Writer
	err     error
}

func (sw *sexpWriter) writeWith(w io.WriterTo) {
	if sw.err == nil {
		n, err := w.WriteTo(sw.dst)
		sw.written += n
		sw.err = err
	}
}

func (sw *sexpWriter) write(data []byte) {
	if sw.err == nil {
		n, err := sw.dst.Write(data)
		sw.written += int64(n)
		sw.err = err
	}
}

func (sw *sexpWriter) writeString(s string) {
	sw.write([]byte(s))
}

func (sw *sexpWriter) writeByte(b byte) {
	sw.write([]byte{b})
}

func (sw *sexpWriter) writeForm(form interface{}) {
	switch form := form.(type) {
	case []Node:
		if len(form) == 0 {
			return
		}
		for i := 0; i < len(form)-1; i++ {
			sw.writeWith(form[i])
			sw.writeByte(' ')
		}
		sw.writeWith(form[len(form)-1])
	case io.WriterTo:
		sw.writeWith(form)
	case string:
{'fraction_non_alphanumeric': 0.19107110049604997, 'fraction_numerical': 0.013962888113172882, 'mean_word_length': 3.804942630185349, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 7, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, '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': '1179267', 'n_tokens_mistral': 2400, 'n_tokens_neox': 2319, 'n_words': 552}