<commit_before>package main

import (
	"go/ast"
	goToken "go/token"
)

func generateAST(parsed []Any) *ast.File {
	return &ast.File{Name: makeIdent("main"), Decls: generateDeclarations(parsed)}
}

func generateDeclarations(parsed []Any) []ast.Decl {
	decls := make([]ast.Decl, len(parsed))

	for i, sexp := range parsed {
		switch sexp := sexp.(type) {
		case []Any:
			decls[i] = generateDeclaration(sexp)
		default:
			panic("unexpected behaviour!")
		}
	}

	return decls
}

func generateDeclaration(sexp []Any) ast.Decl {
	if sexp[0] == "def" {
		return evalDef(sexp)
	}

	return nil
}

func evalDef(sexp []Any) ast.Decl {
	ident := sexp[1].(string)
	val := evalExpr(sexp[2])

	return &ast.GenDecl{
		Tok: goToken.VAR,
		Specs: []ast.Spec{
			&ast.ValueSpec{
				Names:  []*ast.Ident{makeIdent(ident)},
				Values: []ast.Expr{val},
			},
		},
	}
}

func evalExprs(sexp []Any) []ast.Expr {
	out := make([]ast.Expr, len(sexp))

	for i, v := range sexp {
		out[i] = evalExpr(v)
	}

	return out
}

func evalExpr(sexp Any) ast.Expr {
	switch sexp := sexp.(type) {
	case []Any:
		return evalFuncCall(sexp)
	case Any:
		return makeIdent(sexp.(string))
	default:
		panic("oops!")
	}
}

func evalFuncCall(sexp []Any) ast.Expr {
	return makeLitFunCall(sexp)
}

func makeLitFunCall(body []Any) ast.Expr {
	return &ast.CallExpr{
		Fun:  makeFuncLit([]Any{}, body),
		Args: []ast.Expr{},
	}
}

func wrapExprsWithStmt(exps []ast.Expr) []ast.Stmt {
	out := make([]ast.Stmt, len(exps))
	for i, v := range exps {
		out[i] = &ast.ExprStmt{X: v}
	}
	return out
}

func makeFunCall(name string, args []Any) ast.Expr {
	return &ast.CallExpr{
		Fun:  makeIdent(name),
		Args: evalExprs(args),
	}
}

func makeFuncLit(args, body []Any) *ast.FuncLit {
	return &ast.FuncLit{
		Type: &ast.FuncType{
			Params: &ast.FieldList{

				List: []*ast.Field{
					&ast.Field{
						Type:  makeIdent("Any"),
						Names: makeIdentSlice(args),
					},
				},
			},
			Results: &ast.FieldList{
				List: []*ast.Field{
					&ast.Field{
						Type: makeIdent("Any"),
					},
				},
			},
		},
		Body: &ast.BlockStmt{
			List: wrapExprsWithStmt(evalExprs(body)),
		},
	}
}
<commit_msg>Function calling seems to work now, though there are no builtins so anything is a function<commit_after>package main

import (
	"go/ast"
	goToken "go/token"
)

func generateAST(parsed []Any) *ast.File {
	return &ast.File{Name: makeIdent("main"), Decls: generateDeclarations(parsed)}
}

func generateDeclarations(parsed []Any) []ast.Decl {
	decls := make([]ast.Decl, len(parsed))

	for i, sexp := range parsed {
		switch sexp := sexp.(type) {
		case []Any:
			decls[i] = generateDeclaration(sexp)
		default:
			panic("unexpected behaviour!")
		}
	}

	return decls
}

func generateDeclaration(sexp []Any) ast.Decl {
	if sexp[0] == "def" {
		return evalDef(sexp)
	}

	return nil
}

func evalDef(sexp []Any) ast.Decl {
	ident := sexp[1].(string)
	val := evalExpr(sexp[2])

	return &ast.GenDecl{
		Tok: goToken.VAR,
		Specs: []ast.Spec{
			&ast.ValueSpec{
				Names:  []*ast.Ident{makeIdent(ident)},
				Values: []ast.Expr{val},
			},
		},
	}
}

func evalExprs(sexp []Any) []ast.Expr {
	out := make([]ast.Expr, len(sexp))

	for i, v := range sexp {
		out[i] = evalExpr(v)
	}

	return out
}

func evalExpr(sexp Any) ast.Expr {
	switch sexp := sexp.(type) {
	case []Any:
		return evalFuncCall(sexp)
	case Any:
		return makeIdent(sexp.(string))
	default:
		panic("oops!")
	}
}

func evalFuncCall(sexp []Any) ast.Expr {
	// return makeLitFunCall(sexp)
    return makeFunCall(evalExpr(sexp[0]), sexp[1:])
}

func makeLitFunCall(body []Any) ast.Expr {
	return &ast.CallExpr{
		Fun:  makeFuncLit([]Any{}, body),
		Args: []ast.Expr{},
	}
}

func wrapExprsWithStmt(exps []ast.Expr) []ast.Stmt {
	out := make([]ast.Stmt, len(exps))
	for i, v := range exps {
		out[i] = &ast.ExprStmt{X: v}
	}
	return out
}

func makeFunCall(name ast.Expr, args []Any) ast.Expr {
	return &ast.CallExpr{
		Fun:  name,
		Args: evalExprs(args),
	}
}

func makeFuncLit(args, body []Any) *ast.FuncLit {
	return &ast.FuncLit{
		Type: &ast.FuncType{
			Params: &ast.FieldList{

				List: []*ast.Field{
					&ast.Field{
						Type:  makeIdent("Any"),
						Names: makeIdentSlice(args),
					},
				},
			},
			Results: &ast.FieldList{
				List: []*ast.Field{
					&ast.Field{
						Type: makeIdent("Any"),
					},
				},
			},
		},
		Body: &ast.BlockStmt{
			List: wrapExprsWithStmt(evalExprs(body)),
		},
	}
}

{'fraction_non_alphanumeric': 0.17568485397328504, 'fraction_numerical': 0.001811184061580258, 'mean_word_length': 3.5687693898655635, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, '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': '7168443', 'n_tokens_mistral': 1949, 'n_tokens_neox': 1770, 'n_words': 372}