This is a read-only snapshot of the ComputerCraft forums, taken in April 2020.
ecmaxp's profile picture

Lua In Python, With Many Question.

Started by ecmaxp, 17 November 2013 - 12:57 PM
ecmaxp #1
Posted 17 November 2013 - 01:57 PM
Unlike all other's new language request, my thing is i coding it!

https://github.com/EcmaXp/PyCraft

Problem is very hard to rewrite with lua. (eg. support metaclass, parse the arguments)

So i have some choice.
* Run python code in real interpreter
- with some resist (memory limit are possible, ASTVisitor + weakref can track all value)
- and use socket (or queueEvent? slow) for call lua function (If we can do that, really happy)
- and can't call python function. only python -> lua are allowed;
- problem is i don't have good skill with Java, and Modding.
- Calling lua function is slow, but run python is fast.
- It will spawn one python (or pypy) process.

* Run translated-python code in Computer
- (that is lua code, and my code do that, and limited)
- Calling lua function is fast, but run python code is slow.
- We can mix lua object + python object (not suggest, but lua code can call python code.)
- It don't need mod but http or other thing need for translate. (for run python compiler)

* write PyPy backport for lua
- Never choice me?

* Run Jython
- http://www.computerc...aft-for-mc-16x/
- It use Jython, but 2.5; i love Python 3k, not 2.x
- I can rewrite python-part inital code.

First method are almost look's like CC's Language API.
Second method are some people say that. but i think run translated-python code are slow than normal lua code.
4th method, i don't like it. (but that is look's like First method)

Q1. What is best?

Q2. First and Second Method, What is more fast? (Case: GUI programs, and CUI programs with no user input, etc.)

Q3. ?

Q4. 4's Method, That are add the new block for run python code. I don't like that, Can I add new api directly to Lua? (like builtins os api (lua part))

Q5. If i choice first method, It is possible calling python function in lua? (And in python function, call the lua function)

Q6. How i download the dependency libary (like msgpack-java, jython, or ForgeMultiparts) in mod (java-code)?
Edited on 17 November 2013 - 01:21 PM
theoriginalbit #2
Posted 17 November 2013 - 07:09 PM
  1. second method, Instead of running a runtime interpreter each time on the python code, why not just make a cross compiler and translate the code they made in python into Lua and save it to a new file for them to run?
  2. first method would probably be slightly slower when making the calls, second method would be slower at first to translate, but definitely once the translation has been made it would be a lot quicker
  3. ?
  4. Nope it is not possible, and API can only be mounted with a block attached to the computer.
  5. If you make a mod, your mod tells ComputerCraft what you can call from Lua, meaning only the Python methods you specify would be available. You cannot however call Lua functions from within a mod except for a few that have been exposed such as queuing and event and such. I asked the other week about this to add some better support into OpenPeripheral but the debs said that its just not possible
  6. The normal way you download any file in Java, just download/store it in a location near your mod, and then load it when your mod initialises (the new IC2 does this if you do want any references)
Do note, this is not going to be an easy task for you in any case, Python has particular syntaxes that are more efficient to use that just don't work the same or even have an easy implementation in Lua, the first example that comes to mind is this in Python definitely has no equivalent in Lua and would actually require the creation and insertion of a function by you into each program you generate.

someArray[1:6]
ecmaxp #3
Posted 17 November 2013 - 10:04 PM
OK, i continue the develop with second method.

Well the second method are do.

Normal python code.

print(3 + 2 / 1)

Translated-python code

print(_OP__Add__(int(3), _OP__Div__(int(2) / int(1))) # in late, i will make the cache for int -64 ~ 256

and the func environ are filled by {}, metaclass={__index=py} # py is my py_API.lua
I think about hooking the os.run and os.loadAPI for new language. (can directly run program from shell, and i need release lang api for other mods.)

I forgot the CPython is super slow than lua, and lua are slow than luaj, so now i can choice second method.
Also i need cache for translated-python code, if source code are changed, i will recompile the code. (that is looks like pyc, but CC are not support for mtime, need check the CRC)

The Q4, I think about use http for compile. but problem is if ComputerCraft's settings is disable the http option. (I don't have any exp with add new block!)
Oh, well. i know the python syntex someArray[1:6], and this is equal with someArray[slice(1, 6, None)], thanks.
AgentE382 #4
Posted 17 November 2013 - 10:06 PM
Just putting it out there, but you could write a Python compiler that compiles to LASM, or you could just look at what he did and compile directly to LuaJ bytecode.
theoriginalbit #5
Posted 17 November 2013 - 10:28 PM
Just putting it out there, but you could write a Python compiler that compiles to LASM
That would be extremely pointless, compiling from one language to another, so that it can then compile to another.
ecmaxp #6
Posted 17 November 2013 - 10:42 PM
Just putting it out there, but you could write a Python compiler that compiles to LASM
That would be extremely pointless, compiling from one language to another, so that it can then compile to another.

Well. i think about that…

But, now i have think about number-indexed functions (for don't need compare string, just number), thanks.

Add = 1
Sub = 2


print(OP[1](int(21), int(21))) # equal a + b


int = (function()
  __op = {
    1 = function(self, other) return self.value + other.value
    2 = function(self, other) return self.value - other.value
  }
  return getfenv()
end)()

like this.
AgentE382 #7
Posted 18 November 2013 - 08:53 AM
Just putting it out there, but you could write a Python compiler that compiles to LASM
That would be extremely pointless, compiling from one language to another, so that it can then compile to another.
Possibly, but that's how some real compilers still work. High Level Language to Assembly, then to machine code, or High(er) Level Language to C, then to machine code. That's what I was suggesting there. Python to LASM (Assembly), then to LuaJ bytecode (machine code).

However, if possible, it would be better to compile directly from Python to LuaJ bytecode. ElvishJerricco's work on LuaLua and LASM could be used to learn how that could be done for Python.

Seeing as CraftOS doesn't run bytecode by default, I think it would be best to generate a "native executable" that consists of a Lua file like so:
assert(loadstring([[Insert real bytecode here]]),"Invalid Bytecode!")()

But, that's just my 2 cents.
Edited on 18 November 2013 - 07:59 AM
ecmaxp #8
Posted 18 November 2013 - 06:27 PM
Just putting it out there, but you could write a Python compiler that compiles to LASM
That would be extremely pointless, compiling from one language to another, so that it can then compile to another.
Possibly, but that's how some real compilers still work. High Level Language to Assembly, then to machine code, or High(er) Level Language to C, then to machine code. That's what I was suggesting there. Python to LASM (Assembly), then to LuaJ bytecode (machine code).

However, if possible, it would be better to compile directly from Python to LuaJ bytecode. ElvishJerricco's work on LuaLua and LASM could be used to learn how that could be done for Python.

Seeing as CraftOS doesn't run bytecode by default, I think it would be best to generate a "native executable" that consists of a Lua file like so:
assert(loadstring([[Insert real bytecode here]]),"Invalid Bytecode!")()

But, that's just my 2 cents.

Why don't use Default Lua Bytecode Generator?
Use assambly is very hard. and Lua Bytecode Generator are very good.

I can compile the lua code (that is translated-python code) to byte code by string.dump(loadstring(x))

Well. thanks the 2 cents. now i have 1 $ :D/>

# Lua bytecode, or Lua code are not important, that is equal, for run my code.


PYC FORMAT
[*]A four-byte magic number, (i will use custom magic number)
[*]A four-byte modification timestamp, and (i will change timestamp to crc!)
[*]A marshalled [url="http://docs.python.org/ref/types.html#l2h-143"]code object[/url]. (that is will Lua bytecode!)


While i develop the PyCraft, i used AST Viewer for generate AST.

Can you provide the compiled lua code to lasm??
Edited on 18 November 2013 - 05:36 PM