Page 1 of 1

Parsing non Python code in the scripting?

Posted: Wed Dec 08, 2021 7:18 am
by Arq
To begin with, I have minimal experience in working with Python. As a matter of fact, I could probably not do most basic operations, because, as I attempt to learn anything, it makes me wish I was using my native programming tongue instead, Forth. It is probably a confusing and backwards language to anyone who comes to it after learning any other language, but it has become my personal preference in using it now. By random chance, I had located a Forth language parser written in Python, which would allow any Python based environment to read Forth code. It's called forth-py and can be found here, open sourced on Github.

It is a little rough around the edges but it is basically what I wanted in bridging the gap in between languages. I tweaked it slightly to fit my needs, although not completely functional in the way I want yet, it is in some state usable. Great, right? Well, not quite, because I haven't quite been able to figure out how to make RenPy itself parse this parser. I attempted importing the relevant code and declaring the import as python code beforehand, but it gives me this error:

Code: Select all

Traceback (most recent call last):
  File "renpy/common/00console.rpy", line 689, in run
    renpy.python.py_exec(code)
  File "renpy/python.py", line 2258, in py_exec
    exec(py_compile(source, 'exec'), store, locals)
  File "<none>", line 1, in <module>
  File "renpy/loader.py", line 985, in load_module
    exec(code, mod.__dict__)
  File "forthmain.py", line 1, in <module>
  File "renpy/loader.py", line 985, in load_module
    exec(code, mod.__dict__)
  File "lexer.py", line 2, in <module>
ImportError: cannot import name ForthException
The definition of ForthException being:

Code: Select all

class ForthException(Exception):
    def forth__init__(self, msg):
        self.msg = msg

    def forth__str__(self):
        return self.msg
Why is it unable to import this?

Yes, I'm aware it would be faster and easier to learn Python and the scripting of RenPy, and write my code in that. I suspect however if I'm not doing this in a way I enjoy, then it won't be enjoyed. And having to learn a new language entirely would curbstomp my enjoyment.

Re: Parsing non Python code in the scripting?

Posted: Wed Dec 08, 2021 7:41 am
by enaielei
Ren'Py still uses Python 2.7, your library is based on 3.5.

Re: Parsing non Python code in the scripting?

Posted: Wed Dec 08, 2021 7:53 am
by Arq
OK, so what does that mean for me? Is there a way to backport the code to the older version?

Re: Parsing non Python code in the scripting?

Posted: Wed Dec 08, 2021 8:01 am
by Ocelot
Arq wrote: Wed Dec 08, 2021 7:53 am OK, so what does that mean for me? Is there a way to backport the code to the older version?
Short of rewriting it from scratch — no.

There are pretty radical changes between Python 2 and Python 3. This is why process of porting RenPy to Python 3 is still not finished: it is a massive undertaking.

Re: Parsing non Python code in the scripting?

Posted: Thu Dec 09, 2021 12:22 am
by Arq
Ocelot wrote: Wed Dec 08, 2021 8:01 am
Arq wrote: Wed Dec 08, 2021 7:53 am OK, so what does that mean for me? Is there a way to backport the code to the older version?
Short of rewriting it from scratch — no.

There are pretty radical changes between Python 2 and Python 3. This is why process of porting RenPy to Python 3 is still not finished: it is a massive undertaking.
OK. It is a small repository, so I suspect doing so would not be terribly any difficult for anyone intermediately skilled. Maybe I should start looking into it myself.

Re: Parsing non Python code in the scripting?

Posted: Thu Dec 09, 2021 1:44 am
by PyTom
Note that you're going to run into a lot of problems. Ren'Py does a lot of things to support load, save, and rollback, and those will likely not work here.