Here is an interesting paper that discussed combining Lua with ML in
writing an optimizing compiler for C–:
They went as far as writing the Lua interpreter themselves in ML in order
to avoid the GC clash problems. I think luajit and ATS would pair together
a lot better for the task.
Following is a quote of the part on advantages of having embedded Lua for
the compiler. In case the significance is lost on anyone they are changing
internal variables/parameters of the compiler using Lua assignments at the
command line.
*There are significant advantages to having this code in Lua.• It is easy
to change the configuration of the optimizer, the compiler, or even the
targetmachine by putting a simple phrase on the command line. For example,
simplywriting qc-- backend=Backend.mips allows us to cross-compile for our
MIPSmachine. Cross-compilation is better than compiling natively because
our MIPS machineis very slow.• The Lua code is invaluable for debugging.
For example, we can turn off a pass ofthe compiler by a command-line
assignment such as backend.expand=nil, and wecan make the compiler emit
“diagnostic assembly language” by using the assignmentbackend.asm=Asm.cmm.•
The “driver” code in Lua runs not only the C-- compiler but also
preprocessors,front ends, the assembler, and the linker. It can therefore
be used to compile a testcase from start to finish, then do another test
without restarting the compiler. Theadvantage is speed. For example, to
test calling conventions, we compile a suite ofseveral hundred test cases.
Using the Lua driver, these cases compile three to fivetimes faster than
using a Perl script in which the compiler is started separately foreach
case.*And later on:
*As shown by highlighting, almost every stage in the Lua record is actually
implemented inthe host language, Objective Caml—but the stages are composed
using Lua. The advantageis that by putting an appropriate Lua assignment on
the compiler’s command line, we canomit or change any stage, including the
assembly-language emitter.*Enjoy,
HaitaoOn Tuesday, March 18, 2014 10:04:37 PM UTC-7, H Zhang wrote:
Then you definitely want to try ZeroBrane if you haven’t used it before.
Very small IDE yet very versatile and powerful. Shows how embedding a good
language makes a powerful editor/IDE a breeze. It has very nice pedagogical
demos (written in Lua) that could be used to demo ATS as well. Luajit is
now ZeroBrane’s default interpreter so you don’t even have to separately
build luajit (which is straightforward to do anyway).
I would be very interested in combining this with the ATS compiler to make
exploring the ATS compiler easier. If you want to start a project on this I
would be happy to contribute.
Haitao
On Tuesday, March 18, 2014 7:17:44 PM UTC-7, gmhwxi wrote:
This sounds like a pretty cool idea 
I will teach a class based on ATS this summer. I will see if I can make
it work.
On Tuesday, March 18, 2014 10:05:57 PM UTC-4, H Zhang wrote:
I use luajit (through the ZeroBrane IDE) to call the generated C
functions from ATS and it feels pretty close to a REPL. The process is as
follows:
- Generate C files using patsopt -o prog.c -d prog.dats
- Compile to object files with gcc -fPIC. Make sure that ATS include
files are in the path and MALLOCFLAG is defined.
- Create DLL with gcc -shared
- In luajit console or script, write the appropriate C definitions,
load the DLL and use the functions dynamically. The luajit C definitions
can probably be automatically extracted from extern fun … = “mac#”
statements.
I hope this is helpful to others who find similar needs of an REPL.
Luajit has the easiest CFFI I have seen of all dynamic languages. Since it
has JIT it does not depend on the C compiler to generate calling code.
Remember it is the luajit C FFI extension, not the regular Lua C API.
Haitao
On Saturday, January 11, 2014 9:26:30 PM UTC-8, H Zhang wrote:
I think it would be nice to have a way to do REPL in ATS, at least for
the ML style subset of ATS. This may even be a good way to show how to do a
project in ATS and I will be happy to contribute to such a project.
Now the first question is since ATS has a ML style subset would it make
sense to actually support a true subset of ML? So one can write code in
this intersection of ATS and ML and use the ML interpreter for REPL
development and debugging and use ATS compiler to compile to native code
for performance. Is there a reason why this can not be done? If doable this
seems the low hanging fruit to get.
If we do want/need to do an interpreter for ATS (ML style) here are
some thoughts:
As I understand it ATS depends on the C compilers to do a lot of the
work so there may be some limitations on what can be done for the
interpreter without herculean effort. ATS can already parse code into AST
so that part should be reused to parse the input for the interpreter as
well (although it may be important to generate better error messages for
the interpreter). Parsed ATS expressions can be evaluated if we can
correctly dispatch function calls so we need a loader to find/load/register
compiled functions and a runtime to manage associated symbol table and
global data. The trick here is to ensure type safety even while type
information may have been lost in the compiled native code, so the
interpreter will need to access ATS’s type analysis module to establish
type correctness (so we will need access to the source/header files as well
even for pre-compiled code). Alternatively we may use the compiler working
in the background to establish type-correctness? On the other hand things
like defining new data structures and functions are dependent on the C
compilers and are not easy to support directly in the interpreter. Here I
think the typical Prolog style of separating compilation (consult) and
evaluation (query) may be a useful model. We can organize functions into
different modules that get compiled into DLLs. If a source file is modified
the associated DLL will be recompiled (maybe externally calling make but
runtime may need to know the dependency so it can update type information)
and reloaded.
I know these are rather immature thoughts but I hope this is somehow
doable and I think with a good (enough) REPL ATS will be more accessible to
programmers in general.
Thanks,
Haitao