switch to using shell builtins for thought rendering

This commit is contained in:
2025-06-07 23:30:56 -07:00
parent bc40b225f0
commit 15405bf8a3
9 changed files with 9144 additions and 14 deletions

View File

@ -5,6 +5,7 @@ rm -rf site/html
mkdir -p site/{html,assets,scripts}
cp -r src/output/* site/html
echo 'html generated'
(cd ./src/scripts/builtins; ./build.sh)
cp -r src/scripts site/
echo 'scripts copied'
cp -r src/assets site/

View File

@ -0,0 +1,62 @@
#include <stdio.h>
#include <bash/builtins.h>
#include <bash/builtins/bashgetopt.h>
#include <bash/shell.h>
#include <dlfcn.h>
extern char **make_builtin_argv(WORD_LIST*, int*);
#define DEFINE_BUILTIN(name) \
struct builtin name##_struct = { \
#name, \
name##_builtin_func, \
BUILTIN_ENABLED, \
NULL, \
NULL, \
0 \
}
#define WRAP_FUNC_WITH_BUILTIN(name)\
int name##_builtin_func(WORD_LIST *list) { \
int argc, ret; \
char **argv; \
argv = make_builtin_argv(list, &argc); \
ret = name(argc, argv); \
free(argv); \
return ret;\
}
#define PY_FUNC(bash_name, modname, function) \
int bash_name(int argc, char **argv) { \
if (!Py_IsInitialized()) { \
dlopen("libpython3.13.so", RTLD_NOW | RTLD_GLOBAL);\
PyImport_AppendInittab(#modname, PyInit_##modname); \
Py_Initialize(); \
} \
int ret = 1; \
PyObject *mod = NULL, *func = NULL, *result; \
\
mod = PyImport_ImportModule(#modname); \
func = PyObject_GetAttrString(mod, #function); \
\
PyObject *py_argv = PyTuple_New(argc - 1); \
\
for (int i = 1; i < argc; i++) { \
PyObject *arg_as_str = PyUnicode_FromString(argv[i]); \
PyTuple_SetItem(py_argv, i-1, arg_as_str); \
} \
\
result = PyObject_CallObject(func, py_argv); \
if (!result) { PyErr_Print(); goto finally; } \
ret = (int) PyFloat_AsDouble(result); \
\
finally: \
Py_XDECREF(result); \
Py_XDECREF(py_argv); \
Py_XDECREF(func); \
Py_XDECREF(mod); \
return ret; \
} \
WRAP_FUNC_WITH_BUILTIN(bash_name); \
DEFINE_BUILTIN(bash_name);

View File

@ -0,0 +1,39 @@
#!/bin/bash
# maybe uses non-posix regex and thus may be non-portable. oops.
# the bash regex implementation is actually platform dependent, lol.
shopt -s extglob
PY_VERSION=3.13
PYFLAGS="$(/usr/bin/python${PY_VERSION}-config --cflags --ldflags)"
BASH_INCLUDE_DIR="/usr/include/bash"
function translate_function_names() {
# cython generates garbled names like __pyx_pf_7py_test_2foo for a python function named foo
# this function generates a list of #defines that translate these names to what they are in python
file="${1}"
grep 'static PyObject \*__pyx_pf_' "${file}" | grep -v 'proto' | while read -r line; do
desired_name=$"${line#*$"${file/.c/}"_}"
desired_name="${desired_name/#+([0-9])/}"
real_name="${line#*\*}"
echo "#define ${desired_name%(*} ${real_name%(*}"
done
}
py2c() {
rm "${1/.py/.c}"
cython3 -3 "${1}" -o "${1/.py/.c}"
translate_function_names $(basename ${1/.py/.c}) >> "${1/.py/.c}"
}
compile() {
gcc -I$BASH_INCLUDE_DIR{/include,/builtins,}\
$PYFLAGS $1 -o ${1/.c/} -Wall\
-lpython3.13 \
-fPIC -shared -O3
}
py2c execute-bash/execute_bash.py
compile execute-bash/builtin.c

Binary file not shown.

View File

@ -0,0 +1,4 @@
#include "../bash.h"
#include "execute_bash.c"
PY_FUNC(execute_bash, execute_bash, _execute_bash)

File diff suppressed because it is too large Load Diff

View File

@ -0,0 +1,11 @@
def _execute_bash(*args):
from re import sub
from os import environ as hy_env
from subprocess import check_output
env = hy_env
env['PATH'] = hy_env['PATH'] + ':./scripts'
path = args[0]
with open(path, 'r') as fp:
_hy_anon_var_1 = fp.read()
print(sub('\\$\\[(.*?)\\]', lambda sequence: check_output(sequence.group(1), shell=True, executable='/bin/bash', env=env).decode().strip(), _hy_anon_var_1))
return 0

View File

@ -1,11 +0,0 @@
#!/usr/bin/env hy
;; vim: filetype=hy
(import sys [argv])
(import re [sub])
(import os [environ :as hy-env])
(import subprocess [check-output])
(setv env hy-env)
(setv (get env "PATH") (+ (get hy-env "PATH") ":./scripts"))
(print (sub r"\$\[(.*?)\]" (fn [sequence] (. (check-output (.group sequence 1) :shell True :executable "/bin/bash" :env env) (decode) (strip))) (with [fp (open (get argv 1) "r")] (.read fp))))

View File

@ -1,6 +1,6 @@
#!/bin/sh
output="$(execute-bash $1)"
#!/bin/bash
enable -f ./www/site/scripts/builtins/execute-bash/builtin execute_bash
output="$(execute_bash $1)"
if [ -z "${2}" ]; then
echo "${output}" | sed "${2}"
else