Installing Pallene Compiler

· klm's blog


Original post is here: eklausmeier.goip.de

Pallene is a Lua based language. In contrast to Lua, which is untyped, Pallene is typed. A good paper on Pallene is "Pallene: A companion language for Lua", by Hugo Musso Gualandi, and Roberto Ierusalimschy.

From above paper:

The compiler itself is quite conventional. After a standard parsing step, it converts the program to a high-level intermediate form and from that it emits C code, which is then fed into a C compiler such as gcc.

From "A gradually typed subset of a scripting language can be simple and efficient":

Pallene was designed for performance, and one fundamental part of that is that its compiler generates efficient machine code. To simplify the implementation, and for portability, Pallene generates C source code instead of directly generating assembly language.

So, very generally, this idea is similar to f2c (Fortran to C), cobc (Cobol compiler), or Lush (Lisp Universal SHell).

The whole Pallene compiler is implemented in less than 7 kLines of Lua, and less than 1 kLines of C source code for the runtime.

To install Pallene compiler you need git, gcc, lua, and luarocks. Description is for Linux. MacOS is very similar.

1. Source. Fetch source code via git clone.

1$ git clone https://github.com/pallene-lang/pallene.git
2Cloning into 'pallene'...
3$ cd pallene

2. Rocks. Fetch required Lua rocks via luarocks command.

 1$ luarocks install --local --only-deps pallene-dev-1.rockspec
 2Missing dependencies for pallene dev-1:
 3   lpeglabel >= 1.5.0 (not installed)
 4   inspect >= 3.1.0 (not installed)
 5   argparse >= 0.7.0 (not installed)
 6   luafilesystem >= 1.7.0 (not installed)
 7   chronos >= 0.2 (not installed)
 8
 9pallene dev-1 depends on lua ~> 5.3 (5.3-1 provided by VM)
10pallene dev-1 depends on lpeglabel >= 1.5.0 (not installed)
11Installing https://luarocks.org/lpeglabel-1.6.0-1.src.rock
12
13lpeglabel 1.6.0-1 depends on lua >= 5.1 (5.3-1 provided by VM)
14gcc -O2 -fPIC -I/usr/include -c lpcap.c -o lpcap.o
15gcc -O2 -fPIC -I/usr/include -c lpcode.c -o lpcode.o
16gcc -O2 -fPIC -I/usr/include -c lpprint.c -o lpprint.o
17gcc -O2 -fPIC -I/usr/include -c lptree.c -o lptree.o
18gcc -O2 -fPIC -I/usr/include -c lpvm.c -o lpvm.o
19gcc -shared -o lpeglabel.so lpcap.o lpcode.o lpprint.o lptree.o lpvm.o
20No existing manifest. Attempting to rebuild...
21lpeglabel 1.6.0-1 is now installed in /home/klm/.luarocks (license: MIT/X11)
22
23pallene dev-1 depends on inspect >= 3.1.0 (not installed)
24Installing https://luarocks.org/inspect-3.1.1-0.src.rock
25
26inspect 3.1.1-0 depends on lua >= 5.1 (5.3-1 provided by VM)
27inspect 3.1.1-0 is now installed in /home/klm/.luarocks (license: MIT <http://opensource.org/licenses/MIT>)
28
29pallene dev-1 depends on argparse >= 0.7.0 (not installed)
30Installing https://luarocks.org/argparse-0.7.0-1.all.rock
31
32argparse 0.7.0-1 depends on lua >= 5.1, < 5.4 (5.3-1 provided by VM)
33argparse 0.7.0-1 is now installed in /home/klm/.luarocks (license: MIT)
34
35pallene dev-1 depends on luafilesystem >= 1.7.0 (not installed)
36Installing https://luarocks.org/luafilesystem-1.8.0-1.src.rock
37
38luafilesystem 1.8.0-1 depends on lua >= 5.1 (5.3-1 provided by VM)
39gcc -O2 -fPIC -I/usr/include -c src/lfs.c -o src/lfs.o
40gcc -shared -o lfs.so src/lfs.o
41luafilesystem 1.8.0-1 is now installed in /home/klm/.luarocks (license: MIT/X11)
42
43pallene dev-1 depends on chronos >= 0.2 (not installed)
44Installing https://luarocks.org/chronos-0.2-4.src.rock
45
46chronos 0.2-4 depends on lua >= 5.1 (5.3-1 provided by VM)
47gcc -O2 -fPIC -I/usr/include -c src/chronos.c -o src/chronos.o -I/usr/include
48gcc -shared -o chronos.so src/chronos.o -L/usr/lib -Wl,-rpath,/usr/lib -lrt
49chronos 0.2-4 is now installed in /home/klm/.luarocks (license: MIT/X11)
50
51Stopping after installing dependencies for pallene dev-1

3. Environment variables. Make sure that you source the environment variables given by

1luarocks path

For example:

1export LUA_PATH='/usr/share/lua/5.3/?.lua;/usr/share/lua/5.3/?/init.lua;/usr/lib/lua/5.3/?.lua;/usr/lib/lua/5.3/?/init.lua;./?.lua;./?/init.lua;/home/klm/.luarocks/share/lua/5.3/?.lua;/home/klm/.luarocks/share/lua/5.3/?/init.lua'
2export LUA_CPATH='/usr/lib/lua/5.3/?.so;/usr/lib/lua/5.3/loadall.so;./?.so;/home/klm/.luarocks/lib/lua/5.3/?.so'
3export PATH='/home/klm/.luarocks/bin:/usr/bin:/home/klm/bin:...:.

4. Build Lua and runtime. Build Lua and the Pallene runtime (you are still in the pallene directory):

1make linux-readline

Some warnings will show up for Lua, but they can be ignored for now.

5. Run compiler. Now you can run pallenec, provided you still are in the same directory, where you built pallene.

1$ ./pallenec
2Usage: pallenec [-h] [--emit-c] [--emit-asm] [--compile-c]
3       [--dump {parser,checker,ir,uninitialized,constant_propagation}]
4       <source_file>
5
6Error: missing argument 'source_file'

6. Run example. Now check one of the examples.

1$ pallenec examples/factorial/factorial.pln
2$ ./lua/src/lua -l factorial examples/factorial/main.lua
3The factorial of 5 is 120.

The most common error will be to not use the lua/src/lua command from Pallene, but rather the system-wide.

You can compile all examples and benchmarks:

1for i in examples/*/*.pln; do pallenec $i; done
2for i in benchmark/*/*.pln; do pallenec $i; done

Things to note in Pallene:

  1. Array indexes must start at one
  2. Pallene source code, except type-, record-definition or variable definitions, must be within a function
  3. Pallene offers no goto statement. The goto statement was added in Lua 5.2.