Ecosyste.ms: Awesome

An open API service indexing awesome lists of open source software.

Awesome Lists | Featured Topics | Projects

https://github.com/bakpakin/ats-lua

An ATS to Lua compiler
https://github.com/bakpakin/ats-lua

Last synced: 2 months ago
JSON representation

An ATS to Lua compiler

Awesome Lists containing this project

README

        

# Atscc2lua

Atscc2lua is for transpiling into Lua the C output generated from
compiling ATS source. With atscc2lua, one can make direct use of a
rich set of programming features supported by ATS in the construction
of Lua code. In particular, one can readily practice a style of
co-programming with ATS and Lua by taking advantage of both languages.

Suppose that foo.dats is a file containing a program written in
ATS. We can use patscc to compile foo.dats into foo_dats.c (which
contains code written in a small subset of C) and then use atscc2lua
to transpile foo_dats.c into foo_dats.lua that contains Lua code
corresponding to the ATS program in foo.dats.

## Getting Setup

First, clone this repository recursively, such that you get the required git submodule libatscc2lua.

```sh
git clone --recurse-submodules https://github.com/bakpakin/ats-lua.git
cd ats-lua
```

The libatscc2lua submodule is the runtime library that implements many basic functions in ATS
in Lua. Lua programs generated by atscc2lua will require this library to run.

Next make sure you have a working [ATS2 - ATS/Postiats](http://www.ats-lang.org/)
compiler installed on your system. Installation and compilation
instructions for ATS can be found on the website.

Building atscc2lua also requires npm for using the catsparsemit package. This is an ATS
package that parses the C subset generated by patsopt for use by other compilers that use the
ATS-Postiats programs. Once you have npm installed, you can install the catsparsemit package.
Npm can be installed with [node](https://nodejs.org/), or with a system package manager.

```sh
npm update
npm install

# Or, alternatively

make npm-update
make npm-install
```

Lastly, make sure you have a recent Lua install. Any version of Lua >= 5.1 should work, just be sure
to update the makefiles for the test programs if the lua binary is not accessible with the command
`lua`. In particular, LuaJIT should also work, but since LuaJIT is accessed with the command `luajit`, some Makefiles may need to be changed. Lua can be installed with most package managers,
or downloaded from the [Lua website](https://www.lua.org/).

Once all dependencies are installed, use make to build the atscc2lua compiler. The output program
will be in `bin/atscc2lua`.

## Compiling programs with atscc2lua

The atscc2lua program at the moment supports only a handful of options.
Run `bin/atscc2lua` with no arguments to get a usage description. The atscc2lua
program takes an input file, which is a c source file generate by patsopt, and an output
pathname which is the destination of the Lua source file. The `-r` flag will prepend the
code necessary to load the libatscc2lua library at runtime. Generally, only the entry point
of the final lua program needs to require the runtime library, but it does not hurt if every compiled
file requires it. In some cases where you do not want to include the runtime
library (say you are compiling the runtime library itself) this flag can be omitted.

Another caveat is that the output file should be listed before the input file.

Example:
```sh
bin/atscc2lua -r -o myfile_dats.lua -i myfile_dats.c
```

One useful pattern for using the atscc2lua compiler is directly piping the output from patsopt to
atscc2lua. This can simplify build rules. Below is an example rule from the Makefile
in `tests/triangle` that creates corresponding lua files
in an output directory for every .dats file in the current directory:
```make
./output/%_dats.lua: ./%.dats
$(PATSOPT) -d $< | $(ATSCC2LUA) -r -o $@ -i
```

## Example Programs

Several example programs and Makefiles are located in the `tests/` directory.
Each test program contains a Makefile and can be compiled with `make`, and then run with `make run`.
Try the triangle test program for the simplest test programs.

Be sure to compile the main ats-lua program before building the example programs.

## Code overview of atscc2lua

The code for atscc2lua is in the DATS directory.

* atscc2lua\_main.dats - For command-line support. Comtains main.
* atscc2lua\_emit.dats - For stuff common to atsccomp.
* atscc2lua\_emit2.dats - For stuff specific to atscc2lua. Majority of code here.

## Making changes to libatscc2lua

The submodule libatscc2lua contains the runtime library for atscc2lua.
To add code to the libatscc2lua library, make changes in the directory and then run 'make' inside
the libatscc2lua directory. This will rebuild the file `libatscc2lua.lua`, which is the full
concatenated source for the runtime library.