mirror of
https://github.com/PostHog/posthog.git
synced 2024-11-28 18:26:15 +01:00
46 lines
1.7 KiB
Python
Executable File
46 lines
1.7 KiB
Python
Executable File
# Run from project root (cd ../..)
|
|
# python3 -m hogvm.stl.compile
|
|
|
|
import glob
|
|
import json
|
|
|
|
from posthog.hogql import ast
|
|
from posthog.hogql.bytecode import create_bytecode, parse_program
|
|
|
|
source = "hogvm/stl/src/*.hog"
|
|
target_ts = "hogvm/typescript/src/stl/bytecode.ts"
|
|
target_py = "hogvm/python/stl/bytecode.py"
|
|
|
|
bytecodes: dict[str, [list[str], list[any]]] = {}
|
|
|
|
for filename in glob.glob(source):
|
|
with open(filename) as file:
|
|
code = file.read()
|
|
basename = filename.split("/")[-1].split(".")[0]
|
|
program = parse_program(code)
|
|
found = False
|
|
for declaration in program.declarations:
|
|
if isinstance(declaration, ast.Function) and declaration.name == basename:
|
|
found = True
|
|
bytecode = create_bytecode(declaration.body, args=declaration.params)
|
|
bytecodes[basename] = [declaration.params, bytecode]
|
|
if not found:
|
|
print(f"Error: no function called {basename} was found in {filename}!") # noqa: T201
|
|
exit(1)
|
|
|
|
with open(target_ts, "w") as output:
|
|
output.write("// This file is generated by hogvm/stl/compile.py\n")
|
|
output.write("export const BYTECODE_STL: Record<string, [string[], any[]]> = {\n")
|
|
for name, (params, bytecode) in sorted(bytecodes.items()):
|
|
output.write(f' "{name}": [{json.dumps(params)}, {json.dumps(bytecode)}],\n')
|
|
output.write("}\n")
|
|
|
|
with open(target_py, "w") as output:
|
|
output.write("# This file is generated by hogvm/stl/compile.py\n")
|
|
output.write("# fmt: off\n")
|
|
output.write("BYTECODE_STL: dict[str, tuple[list[str], list]] = {\n")
|
|
for name, (params, bytecode) in sorted(bytecodes.items()):
|
|
output.write(f' "{name}": ({json.dumps(params)}, {json.dumps(bytecode)}),\n')
|
|
output.write("}\n")
|
|
output.write("# fmt: on\n")
|