Hey guys,
I'm developing a python-to-nim transpiler similar to c2nim and I have an initial design choice that needs to be made at this point. I'd like to get some input from you prior and current Python developers if I may.
Converting Python Lists to Nim Type:
Current choices I'm thinking of...
sequence One idea is to have a sequence of JSON objects (or object type of similar functionality) for storing each item in the Python list, so as to keep a list an actual list and allow items of different types.
tuple. One idea is to use a tuple for storing each item in the Python list, so as to just simply allow items of various types.
Both have pros and cons, and I'm open to entirely different ideas.
Because Python lists will eat whatever type is thrown in them and the dynamic nature of them in general, the choice is not going to catch everything. I just want to make the better choice
So far I've got basic types, for/while loops, if/elif/else statements, variable assignments, function calls, and parameter-less function declarations. There are a couple of Python->Nim conversions for certain built-in calls like print.
I'm at the point where it's time to handle the hard stuff and make hard choices lol
I'm working on a similar "pointlessly super secret" project that wrestles with the same issues. (Though it is reading from a different dynamic language, not python.)
The fundamental problem with any dynamic-to-compiled language project is that the "intent" of the programmer is largely lost. But it is quite possible for a var or list (dynamically typed) to really be of a single type..
No solution is perfect, but this is mine:
Personally, I refrained from using a generic dynamic type (such as JSON) simply because they are pretty rare in practice. Both in Python and this other language, most variables are of one static type.
Oh, and as this is Python you are working on, I suspect decorators will be a living nightmare.
It's a cool project and you'll have fun doing it... it's just going to be different to what you are expecting. Here's a little piece of python code that expresses quite a common idiom, maybe you can think about how you would transpile it:
class Foo:
def add_two(self, x):
return x + 2
foo = Foo()
assert foo.add_two(2) == 4
bar = Foo()
bar.add_two = lambda x: x + 1
assert bar.add_two(2) == 3
print("done")
This can be done in Nim as well, but you'd just have to translate the default for add_two:
type
Foo = object
addTwo: proc(x: int): int
var foo = Foo()
foo.addTwo = proc(x: int): int = x + 2
assert foo.addTwo(2) == 4
var bar = Foo()
bar.addTwo = proc(x: int): int = x + 1
assert bar.add_two(2) == 3
You could do something like this
type
Foo = object
addTwoImpl : proc(a : Foo, x : int): int
proc addTwo(a : Foo, x : int): int =
a.addTwoImpl(a,x)
proc `addTwo=`(a : var Foo, b : proc (a : Foo, x : int) : int) =
a.addTwoImpl = b
var
foo = Foo()
foo.addTwo = proc(a : Foo, x: int): int = x + 2
assert foo.addTwo(2) == 4
var bar = Foo()
bar.addTwo = proc(a : Foo, x: int): int = x + 1
assert bar.add_two(2) == 3
Mine is "pointlessly super secret". :) (It is reading from a different dynamic language, not Python.)
I know you will want to neither confirm nor deny anything but I think it must be PHP. I for my part always want to keep my PHP usage secret.