I use this for my Python-based Lisp: https://github.com/shawwn/pymen/blob/ml/importer.py
import foo
checks for "foo.l", and if found, compiles it to foo.py on the fly and imports that instead.It's so cursed. I love it.
I use this for my Python-based Lisp: https://github.com/shawwn/pymen/blob/ml/importer.py
import foo
checks for "foo.l", and if found, compiles it to foo.py on the fly and imports that instead.It's so cursed. I love it.
"from stackoverflow import quick_sort will go through the search results of [python] quick sort looking for the largest code block that doesn’t syntax error in the highest voted answer from the highest voted question and return it as a module. If that answer doesn’t have any valid python code, it checks the next highest voted answer for code blocks."
I once implemented a custom importer as part of a system where the Python interpreter never touched the filesystem.
I keep trying to get into Lisp (and JavaScript, and TypeScript, etc etc) but I've been a sysadmin my whole professional life and also a chronic pain sufferer. That translates into mostly having energy only for work and that's it, not much motivation to learn after work or on the weekend.
In my DevOps job, I write Terraform, plus read javascript and cloudformation yaml. I do wish I could convert my current stuff to AWS CDK, but I don't want to fragment the multiple projects that are using Terraform. (I haven't looked into tf-cdk much at all yet)
But, I did make an interactive tutorial here: https://docs.ycombinator.lol/
If you have any questions about it, I'd be happy to answer. This stuff is pure fun mixed with a shot of professionalism.
For what it's worth, as someone with narcolepsy, I relate quite a lot to your chronic pain. (https://twitter.com/theshawwn/status/1392213804684038150) For me, it mostly translated into wandering aimlessly from job to job, since I thought no one would have me. I hope that you find your way -- there's nothing wrong at all with taking it slow and spending years on something that takes others a few months. Everyone is different, and it's all about the fun.
It evaluates to -500000, as you’d expect.
(Just kidding, it’s -50000. Amusingly, the https://docs.ycombinator.lol version gets it right, since it has to; every expression is actually evaluated in the browser.)
This sounds very much like the good kind of magic, though.
> that you create
As in existing language supplied eg. Perl, Java, etc., or literally anything? Like bootstrapping your own home made language from scratch?
It starts with reader.l: https://github.com/shawwn/pymen/blob/ml/reader.l where the raw character stream is turned into a bunch of nested arrays. E.g. (+ 1 2) becomes ["+", 1, 2]
Then it's punted over to compiler.l https://github.com/shawwn/pymen/blob/ml/compiler.l where it's passed through `expand`, which does a `macroexpand` followed by a `lower`. E.g. (do (do (do (print 'hi)))) becomes ["print", "\"hi\""]
Then the final thing is thrown to the compile function, which spits out print("hi") -- the final valid Python code that gets passed into the standard python `exec` function.
Works with all the standard python things, like async functions and `with` contexts. Been screwing around with it for a few years.
Should work with anything as long as you can ultimately generate Python bytecode (and provide a module object). The import system is not simple, but it's really open.
Edit: A must have for any prolific dynamic language. But now I’m not sure that’s true, because even though it apparently works in Python, it’s certainly not widely used. In NodeJS this feature is used quite heavily for typescript, coffeescript (etcetera) interop.
I mean, it is used—even in the standard library—but often for alternative packaging (e.g., loading python modules in zip files) rather than alternative languages. It may be used less prominently than in Node, but it definitely is used for a variety of things.
That doesn't sound cursed to me, just flexible.
In the first .com wave we used a similar mechanism, just with TCL instead of Python, to ship our scripts encrypted.
Only our modified loader, written in C, could handle them.
$ touch foo.l
$ python3
>>> import foo
ModuleNotFoundError: No module named 'foo' git clone https://github.com/shawwn/pymen -b ml
cd pymen
touch foo.l
bin/pymen
(import foo)
It's a bit of a WIP (notice this is on the `ml` branch, not mainline), but it does work. >:)You need nodejs to be installed too, ha.