Showing posts with label Lua. Show all posts
Showing posts with label Lua. Show all posts

Friday, March 1, 2013

repl.it, online REPL for many languages, and empythoned

By Vasudev Ram

repl.it is an online REPL (Read-Eval-Print-Loop) for multiple programming languages, including for Python. Some of the other supported languages are QBASIC, FORTH, Lua and Scheme and Ruby (it says "beta" for Ruby, but the other languages also may not have full support, e.g. see the excerpts below).

Here is a small test run of using repl.it with Python, that I just did:
Python 2.7.2 (default, Jul 20 2011, 02:32:18)
[GCC 4.2.1 (LLVM, Emscripten 1.5, Empythoned)] on linux2
   def foo(bar): 
..   print "in foo, bar =", bar 
..   
   foo(1)
in foo, bar = 1
   foo("ab")
in foo, bar = ab
   
   for i in range(4): 
..   foo(i) 
..   
in foo, bar = 0
in foo, bar = 1
in foo, bar = 2
in foo, bar = 3

   class Foo(): 
..   def bar(self): 
..     print "in Foo.bar()" 
..     
   
   foo = Foo()
   foo.bar()
in Foo.bar()
    

About repl.it.

For running Python, repl.it uses empythoned, which in turn uses emscripten.

Excerpts from the related tools' sites:

[ Empythoned is a build script that uses Emscripten to compile CPython for use in a browser. It attempts to compile the main interpreter as a single small executable and the whole standard library as dynamically loaded libraries.

The project is in its infancy. Right now the core interpreter works very well, but many of the libraries either don't work at all or contain various bugs. ]

[ Emscripten is an LLVM-to-JavaScript compiler. It takes LLVM bitcode - which can be generated from C/C++, using llvm-gcc or clang, or any other language that can be converted into LLVM - and compiles that into JavaScript, which can be run on the web (or anywhere else JavaScript can run).

Links to demos, tutorial, FAQ, etc: https://github.com/kripken/emscripten/wiki

Main project page: http://emscripten.org ]

- Vasudev Ram - Dancing Bison Enterprises



Thursday, February 21, 2013

Lua inside Python, Python inside Lua - Lunatic Python


(Fixing the no newlines issues in the post, with this edit. If any reader still notices any issue with formatting, please leave a comment and I'll check it again - thanks.)

Lunatic Python - Labix

As the title of this post says, Lunatic Python (nice name - see below for why :-), is a software bridge, that enables two-way communication between Python and Lua.

From the site: "Being two-way means that it allows Lua inside Python, Python inside Lua, Lua inside Python inside Lua, Python inside Lua inside Python, and so on."

I only saw it today, so have not tried it out much yet, but from reading some of the code examples, it looks pretty interesting. Here are a few examples from the site (pasted from mobile, some characters may get changed, check site for exact code):

[
Lua inside Python

A basic example.

>>> import lua
>>> lg = lua.globals()
>>> lg.string

>>> lg.string.lower

>>> lg.string.lower("Hello world!")
'hello world!'
Now, let's put a local object into Lua space.
>>> d = {}
>>> lg.d = d
>>> lua.execute("d['key'] = 'value'")
>>> d {'key': 'value'}
Can we get the reference back from Lua space?
>>> d2 = lua.eval("d")
>>> d is d2
True
Good! Is the python interface available inside the Lua interpreter?
>>> lua.eval("python") 
Yes, it looks so. Let's nest some evaluations and see a local reference passing through.
>>> class MyClass: pass
...
>>> obj = MyClass()
>>> obj <__main__.MyClass instance at 0x403ccb4c>
>>> lua.eval(r"python.eval('lua.eval(\"python.eval (\'obj\')\")')")
<__main__.MyClass instance at 0x403ccb4c>
Are you still following me? Good. Then you've probably noticed that the Python interpreter state inside the Lua interpreter state is the same as the outside Python we're running. Let's see that in a more comfortable way.
>>> lua.execute("pg = python.globals()")
>>> lua.eval("pg.obj")
<__main__.MyClass instance at 0x403ccb4c>

]

Though it is an experimental project, the author of Lunatic Python, Gustavo Niemeyer, says that it is being used in some real world applications.

Lua.

Lua on Wikipedia.

Lua is a (buzzwords ahead :) cross-platform (*), extensible, multi-paradigm, lightweight scripting language.

It is used a lot in real world applications, including games, an Adobe product, and other areas.

Lua means moon in Portuguese (it was developed at an institute in Brazil), that's why the name Lunatic Python is a good one, since lunatics are supposed to be influenced by the moon ...

I had first come across Lua some years ago when checking out various new or less known programming languages. Tried it out a bit at the basic level, not the extensibility aspects.

(*) It seems to have developed a lot since then, at least in platform support; the Lua home page says it runs on OS's from many mobile ones up to mainframes. When I first saw it, it only worked on Linux and Windows, IIRC.

- Vasudev Ram