<commit_before>package anylisp

import (
	"fmt"
	"math/big"
	"os"
)

type Lister interface {
	Car() interface{}
	SetCar(v interface{}) interface{}
	Cdr() Lister
	SetCdr(v Lister) Lister
	Last() Lister
}

type List struct {
	car interface{}
	cdr Lister
}

type Inter interface {
	Cmp(y *big.Int) (r int)
	Int64() int64
}

var (
	Ps_      Lister
	C_       Lister
	TempRoot Lister
)

func Parse(code string) {
	TempRoot = &List{"sx'", nil}
	Ps_ = &List{TempRoot, nil}
	C_ = &List{&List{TempRoot, nil}, nil}
	tok := ""
	for i := 0; i < len(code); i++ {
		if code[i] == ' ' || code[i] == '\t' || code[i] == '\n' {
			if tok == ")" {
				Assert(Ps_.Cdr() != nil, "Parse WTF! Too many )s")
				Ps_ = Ps_.Cdr()
			} else if len(tok) > 0 {
				var ls Lister
				if tok == "(" { // list
					ls = &List{nil, nil}
				} else if tok[0] == '[' && tok[len(tok)-1] == ']' { // number
					for j := 1; j < len(tok)-1; j++ {
						Assert(tok[j] == '-' || (tok[j] >= '0' && tok[j] <= '9') || (tok[j] >= 'a' && tok[j] <= 'f'),
							"Parse WTF! Bad character in number")
					}
					bi := new(big.Int)
					_, err := fmt.Sscanf(tok[1:len(tok)-1], "%x", bi)
					Assert(err == nil, "Parse WTF! Bad number")
					ls = &List{bi, nil}
				} else { // symbol
					ls = &List{tok, nil}
				}
				if Ps_.Car() == nil {
					Ps_.Cdr().Car().(Lister).SetCar(ls) // 1st token in list
				} else {
					Ps_.Car().(Lister).SetCdr(ls)
				}
				Ps_.SetCar(ls)
				if tok == "(" {
					Ps_ = &List{nil, Ps_}
				}
			}
			tok = ""
		} else {
			tok += string(code[i])
		}
	}
	Assert(Ps_.Cdr() == nil, "Parse WTF! Too few )s")
}

func Run() {
	for C_ != nil {
		frm, ok := C_.Car().(Lister)
		Assert(ok, "WTF! Bad stack frame")
		HasArg := func(n int) bool {
			return Nth(frm, n-1).Cdr() != nil
		}
		Arg := func(n int) interface{} {
			return Nth(frm, n).Car()
		}
		ArgL := func(n int) Lister {
			arg, ok := Arg(n).(Lister)
			Assert(ok, fmt.Sprintf("WTF! Stack frame argument %d isn't a list", n))
			return arg
		}
		exp, ok := frm.Car().(Lister)
		if !ok {
			fmt.Print("0 ")
			Ret(frm.Car())
		} else {
			switch t := exp.Car().(type) {
			case nil:
				Assert(false, "WTF! Can't call the empty list")
			case Inter:
				Assert(false, "WTF! Can't call an int")
			case Lister:
				Assert(false, "WTF! Can't call a list")
				// I kind of like the behavior below, but it causes strange error messages if there's a bug
				/*if frm.Cdr() == nil {
					fmt.Println("a")
					C_ = &List{&List{t, nil}, C_}
				} else {
					fmt.Println("b")
					frm.SetCar(frm.Cdr().Car())
				}*/
			case string:
				switch t {
				case "sx'": // sx', arg, ret
					if exp.Cdr() == nil {
						fmt.Print("{0 ")
						Ret(nil)
					} else if !HasArg(1) {
						fmt.Print("{1 ")
						frm.SetCdr(&List{exp.Cdr(), nil})
					} else if Arg(1) == nil {
						fmt.Print("{2 ")
						Ret(Arg(2))
					} else {
						fmt.Print("{3 ")
						C_ = &List{&List{ArgL(1).Car(), nil}, C_}
						frm.SetCdr(&List{ArgL(1).Cdr(), nil})
					}
				case "q'":
					if exp.Cdr() == nil {
						fmt.Print("'0 ")
						Ret(nil)
					} else {
						fmt.Print("'1 ")
						Ret(exp.Cdr().Car())
					}
				case "?'":
					// ?', if part, then part, ret
					// ?', then part, nil, ret
					if exp.Cdr() == nil {
						fmt.Print("?0 ")
						Ret(nil)
					} else if !HasArg(1) {
						fmt.Print("?1 ")
						frm.SetCdr(&List{exp.Cdr(), &List{exp.Cdr().Cdr(), nil}})
					} else if HasArg(3) {
						if Arg(2) == nil {
							fmt.Print("?2 ")
							Ret(Arg(3))
						} else if Arg(3) != nil {
							fmt.Print("?3 ")
							frm.SetCdr(&List{ArgL(1).Cdr(), &List{nil, nil}})
						} else if ArgL(2).Cdr() == nil {
							fmt.Print("?4 ")
							Ret(nil)
						} else {
							fmt.Print("?5 ")
							frm.SetCdr(&List{ArgL(2).Cdr(), &List{ArgL(2).Cdr().Cdr(), nil}})
						}
					} else {
						fmt.Print("?6 ")
						C_ = &List{&List{ArgL(1).Car(), nil}, C_}
					}
				case "pr'":
					// pr', ret
					// TODO: print all arguments
					if exp.Cdr() == nil {
						fmt.Print("pr0 ")
						Ret(nil)
					} else if !HasArg(1) {
						fmt.Print("pr1 ")
						C_ = &List{&List{exp.Cdr().Car(), nil}, C_}
					} else {
						fmt.Print("pr2 ")
						s := make([]uint8, Len(ArgL(1)))
						for i, arg := 0, ArgL(1); arg != nil; i, arg = i+1, arg.Cdr() {
							c, ok := arg.Car().(Inter)
							Assert(ok && c.Cmp(big.NewInt(-1)) == 1 && c.Cmp(big.NewInt(256)) == -1,<commit_msg>first, rest, last<commit_after>package anylisp

import (
	"fmt"
	"math/big"
	"os"
)

type Lister interface {
	Car() interface{}
	SetCar(v interface{}) interface{}
	Cdr() Lister
	SetCdr(v Lister) Lister
	Last() Lister
}

type List struct {
	car interface{}
	cdr Lister
}

type Inter interface {
	Cmp(y *big.Int) (r int)
	Int64() int64
}

var (
	Ps_      Lister
	C_       Lister
	TempRoot Lister
)

func Parse(code string) {
	TempRoot = &List{"sx'", nil}
	Ps_ = &List{TempRoot, nil}
	C_ = &List{&List{TempRoot, nil}, nil}
	tok := ""
	for i := 0; i < len(code); i++ {
		if code[i] == ' ' || code[i] == '\t' || code[i] == '\n' {
			if tok == ")" {
				Assert(Ps_.Cdr() != nil, "Parse WTF! Too many )s")
				Ps_ = Ps_.Cdr()
			} else if len(tok) > 0 {
				var ls Lister
				if tok == "(" { // list
					ls = &List{nil, nil}
				} else if tok[0] == '[' && tok[len(tok)-1] == ']' { // number
					for j := 1; j < len(tok)-1; j++ {
						Assert(tok[j] == '-' || (tok[j] >= '0' && tok[j] <= '9') || (tok[j] >= 'a' && tok[j] <= 'f'),
							"Parse WTF! Bad character in number")
					}
					bi := new(big.Int)
					_, err := fmt.Sscanf(tok[1:len(tok)-1], "%x", bi)
					Assert(err == nil, "Parse WTF! Bad number")
					ls = &List{bi, nil}
				} else { // symbol
					ls = &List{tok, nil}
				}
				if Ps_.Car() == nil {
					Ps_.Cdr().Car().(Lister).SetCar(ls) // 1st token in list
				} else {
					Ps_.Car().(Lister).SetCdr(ls)
				}
				Ps_.SetCar(ls)
				if tok == "(" {
					Ps_ = &List{nil, Ps_}
				}
			}
			tok = ""
		} else {
			tok += string(code[i])
		}
	}
	Assert(Ps_.Cdr() == nil, "Parse WTF! Too few )s")
}

func Run() {
	for C_ != nil {
		frm, ok := C_.Car().(Lister)
		Assert(ok, "WTF! Bad stack frame")
		HasArg := func(n int) bool {
			return Nth(frm, n-1).Cdr() != nil
		}
		Arg := func(n int) interface{} {
			return Nth(frm, n).Car()
		}
		ArgL := func(n int) Lister {
			arg, ok := Arg(n).(Lister)
			Assert(ok, fmt.Sprintf("WTF! Stack frame argument %d isn't a list", n))
			return arg
		}
		exp, ok := frm.Car().(Lister)
		if !ok {
			fmt.Print("0 ")
			Ret(frm.Car())
		} else {
			switch t := exp.Car().(type) {
			case nil:
				Assert(false, "WTF! Can't call the empty list")
			case Inter:
				Assert(false, "WTF! Can't call an int")
			case Lister:
				Assert(false, "WTF! Can't call a list")
				// I kind of like the behavior below, but it causes strange error messages if there's a bug
				/*if frm.Cdr() == nil {
					fmt.Println("a")
					C_ = &List{&List{t, nil}, C_}
				} else {
					fmt.Println("b")
					frm.SetCar(frm.Cdr().Car())
				}*/
			case string:
				switch t {
				case "sx'": // sx', arg, ret
					if exp.Cdr() == nil {
						fmt.Print("{0 ")
						Ret(nil)
					} else if !HasArg(1) {
						fmt.Print("{1 ")
						frm.SetCdr(&List{exp.Cdr(), nil})
					} else if Arg(1) == nil {
						fmt.Print("{2 ")
						Ret(Arg(2))
					} else {
						fmt.Print("{3 ")
						C_ = &List{&List{ArgL(1).Car(), nil}, C_}
						frm.SetCdr(&List{ArgL(1).Cdr(), nil})
					}
				case "q'":
					if exp.Cdr() == nil {
						fmt.Print("'0 ")
						Ret(nil)
					} else {
						fmt.Print("'1 ")
						Ret(exp.Cdr().Car())
					}
				case ":^'", ":>'", ":|'":
					if exp.Cdr() == nil {
						fmt.Print(":0 ")
						Ret(nil)
					} else if !HasArg(1) {
						fmt.Print(":1 ")
						C_ = &List{&List{exp.Cdr().Car(), nil}, C_}
					} else if Arg(1) == nil {
						fmt.Print(":2 ")
						Ret(nil)
					} else { // TODO: better error message if not a list
						fmt.Print(":3 ")
						switch t {
						case ":^'":
							Ret(ArgL(1).Car())
						case ":>'":
							Ret(ArgL(1).Cdr())
						case ":|'":
							Ret(ArgL(1).Last())
						}
					}
				case "?'":
					// ?', if part, then part, ret
					// ?', then part, nil, ret
					if exp.Cdr() == nil {
						fmt.Print("?0 ")
						Ret(nil)
					} else if !HasArg(1) {
						fmt.Print("?1 ")
						frm.SetCdr(&List{exp.Cdr(), &List{exp.Cdr().Cdr(), nil}})
					} else if HasArg(3) {
						if Arg(2) == nil {
							fmt.Print("?2 ")
							Ret(Arg(3))
						} else if Arg(3) != nil {
							fmt.Print("?3 ")
							frm.SetCdr(&List{ArgL(1).Cdr(), &List{nil, nil}})
						} else if ArgL(2).Cdr() == nil {
							fmt.Print("?4 ")
							Ret(nil)
						} else {
							fmt.Print("?5 ")
							frm.SetCdr(&List{ArgL(2).Cdr(), &List{ArgL(2).Cdr().Cdr(), nil}})
						}
					} else {
						fmt.Print("?6 ")
						C_ = &List{&List{ArgL(1).Car(), nil}, C_}
					}
				case "pr'":
					// pr', ret
					// TODO: print all arguments
					if exp.Cdr() == nil {
						fmt.Print("pr0 ")
						Ret(nil)
					} else if !HasArg(1) {
						fmt.Print("pr1 ")
						C_ = &List{&List{exp.Cdr().Car(), nil}, C_}
					} else if Arg(1) == nil {
						fmt.Print("pr2 ")
						Ret(nil)
					} else {
						fmt.Print("pr3 ")
						s := make([]uint8, Len(ArgL(1))) // TODO: better error message if not a list
						for i, arg := 0, ArgL(1); arg != nil; i, arg = i+1, arg.Cdr() {
							c, ok := arg.Car().(Inter)
							Assert(ok && c.Cmp(big.NewInt(-1)) == 1 && c.Cmp(big.NewInt(256)) == -1,
{'fraction_non_alphanumeric': 0.2195483939361815, 'fraction_numerical': 0.013887416516484681, 'mean_word_length': 2.1073781291172597, 'pattern_counts': {'":': 22, '<': 11, '<?xml version=': 0, '>': 11, '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': '1241270', 'n_tokens_mistral': 5006, 'n_tokens_neox': 3871, 'n_words': 897}