dwayne / whitespace-python

A Whitespace interpreter written in Python.
https://web.archive.org/web/20150717190859/http://compsoc.dur.ac.uk:80/whitespace/
MIT License
10 stars 0 forks source link

Integrate WS library in python's code. #9

Open Astramast opened 1 week ago

Astramast commented 1 week ago

This could be very interesting if we could call the interpreter within python's code, like

import whitespace
whitespace.run(some_code)

Right now we can do this with subprocess, but it's kind of externalizing the thing.

Thank you for the interpreter tho.

rzuckerm commented 1 week ago

I think this can be done like this:

import whitespace.interpreter
whitespace.interpreter.eval(some_code)
rzuckerm commented 1 week ago

Confirmed:

>>> import whitespace.interpreter
>>> s='S S S T\tS S T\tS S S L\nT\tL\nS S S S S T\tT\tS S T\tS T\tL\nT\tL\nS S S S S T\tT\tS T\tT\tS S L\nT\tL\nS S S S S T\tT\tS T\tT\tS S L\nT\tL\nS S S S S T\tT\tS T\tT\tT\tT\tL\nT\tL\nS S S S S T\tS T\tT\tS S L\nT\tL\nS S S S S T\tS S S S S L\nT\tL\nS S S S S T\tS T\tS T\tT\tT\tL\nT\tL\nS S S S S T\tT\tS T\tT\tT\tT\tL\nT\tL\nS S S S S T\tT\tT\tS S T\tS L\nT\tL\nS S S S S T\tT\tS T\tT\tS S L\nT\tL\nS S S S S T\tT\tS S T\tS S L\nT\tL\nS S S S S T\tS S S S T\tL\nT\tL\nS S L\nL\nL\n'
>>> whitespace.interpreter.eval(s)
Hello, World!>>> 
rzuckerm commented 4 days ago

This has been implemented in my fork of this repo called whitespace2:

>>> import whitespace
>>> s='S S S T\tS S T\tS S S L\nT\tL\nS S S S S T\tT\tS S T\tS T\tL\nT\tL\nS S S S S T\tT\tS T\tT\tS S L\nT\tL\nS S S S S T\tT\tS T\tT\tS S L\nT\tL\nS S S S S T\tT\tS T\tT\tT\tT\tL\nT\tL\nS S S S S T\tS T\tT\tS S L\nT\tL\nS S S S S T\tS S S S S L\nT\tL\nS S S S S T\tS T\tS T\tT\tT\tL\nT\tL\nS S S S S T\tT\tS T\tT\tT\tT\tL\nT\tL\nS S S S S T\tT\tT\tS S T\tS L\nT\tL\nS S S S S T\tT\tS T\tT\tS S L\nT\tL\nS S S S S T\tT\tS S T\tS S L\nT\tL\nS S S S S T\tS S S S T\tL\nT\tL\nS S L\nL\nL\n'
>>> whitespace.run(s)
Hello, World!>>>