Created
October 6, 2025 16:01
-
-
Save philzook58/c408d7a7cc5020dca14b3eb392588944 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| from kdrag.parsers.sexp import parse | |
| import operator | |
| import functools | |
| import numpy as np | |
| parse("(+ 1 2 3 4)") | |
| def expr(env, sexp): | |
| match sexp: | |
| case int() | float(): | |
| return sexp | |
| case str(): | |
| return env[sexp] | |
| case ["if", cond, then, otherwise]: | |
| return expr(env, then) if expr(env, cond) else expr(env, otherwise) | |
| case ["set!", name, value]: | |
| env[name] = expr(env, value) | |
| case ["lambda", [*args], body]: | |
| return lambda *argvals: expr({**env, **dict(zip(args, argvals))}, body) | |
| case ["quote", body]: | |
| return body | |
| case ["define-macro", name, [*args], body]: | |
| env["__macros"][name] = lambda *argvals: expr({**env, **dict(zip(args, argvals))}, body) | |
| case ["eval", body]: | |
| return expr(env, expr(env, body)) | |
| case [".", obj, attr]: | |
| return getattr(expr(env, obj), attr) # attr is intrinsically quoted? | |
| case ["array", shape, *vals]: | |
| return np.array(vals).reshape(shape) | |
| case ["frame", shape, *vals]: | |
| raise NotImplementedError() | |
| case [op, *args]: | |
| if isinstance(op, str) and op in env["__macros"]: | |
| return expr(env, env["__macros"][op](*args)) | |
| else: | |
| return expr(env, op)(*(expr(env, arg) for arg in args)) | |
| def stmt(env, sexp): | |
| match sexp: | |
| case ["define", name, value]: | |
| env[name] = expr(env, value) | |
| case _: | |
| print(expr(env, sexp)) | |
| def stmts(sexps): | |
| def myassert(x): | |
| assert x | |
| env = {"+": lambda *args: functools.reduce(operator.add, args), | |
| "print": print, | |
| "assert": myassert, | |
| "nil" : [], | |
| "true": True, | |
| "false": False, | |
| "-": operator.sub, | |
| "*": lambda *args: functools.reduce(operator.mul, args), | |
| "=": operator.eq, | |
| "<": operator.lt, | |
| ">": operator.gt, | |
| "<=": operator.le, | |
| ">=": operator.ge, | |
| "/": operator.truediv, | |
| "car": lambda x: x[0], | |
| "cdr": lambda x: x[1:], | |
| "cons": lambda x, xs: [x] + xs, | |
| "list": lambda *args: list(args), | |
| "map": lambda f, lst: list(map(f, lst)), | |
| "array": lambda shape, *vals: np.array(vals).reshape(shape), | |
| "reduce": lambda f, arr: f.reduce(arr), | |
| "np": np, | |
| "__macros" : {} | |
| } | |
| for sexp in sexps: | |
| stmt(env, sexp) | |
| return env | |
| stmts(parse(""" | |
| (define x 10) | |
| (define y 20) | |
| (+ x y 30) | |
| (print 17) | |
| (assert true) | |
| ;(assert false) | |
| ((lambda (x) (+ x 1)) 10) | |
| (+ (quote (1 2)) (quote (3 4))) | |
| (cdr (quote (1 2 3 4))) | |
| (car (quote (1 2 3 4))) | |
| (map (lambda (x) (* x 2)) (quote (1 2 3 4 5 6 7 8 9 10))) | |
| (eval (quote (+ 1 2))) | |
| (array (2 5) 1 2 3 4 5 6 7 8 9 10) | |
| (array () 17) | |
| ((. np sum) (array (2 5) 1 2 3 4 5 6 7 8 9 10)) | |
| (define-macro when (cond body) (list (quote if) cond body (quote nil))) | |
| (when false (print (quote it_worked))) | |
| """)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment