Re:No Compiler – On LLVM, and writing software without a com...
by kungfooman on Thursday March 03, 2016 @11:53PM
(#11222108)
Terra is on my radar for a long time, but the Lua syntax prevents me every time to actually use it.
Basically making a simple foreach loop in Lua escalates everytime into a hour trial-and-error session.
Re:No Compiler – On LLVM, and writing software without a com...
by klibertp on Friday March 04, 2016 @01:24PM
(#11225743)
Here:
for var_1, ···, var_n in explist do block end
use `pairs(a_table)` to get `key, val` variables bound respectively. Use `ipairs` to get a loop over indices and values. This is very similar to Python, only with "do/end" instead of indent/dedent. What you can't do in Python, but can in Lua is to provide less variable names than the iterator function returns. For example, to get a list of keys when iterating over `(key, val)` pairs in Python you'd have to do this: for key, _ in some_dict.items()
While in Lua it's less explicit: for key in pairs(some_table)
...and that's it.
The strength of the two is that Terra can be manipulated using Lua as a metalanguage: for example, you could write a Lua function which produces Terra code, which is then executed. In some sense, this is like C, which has a lower-level language (C) and a corresponding high-level language which can manipulate that lower-level language (the C preprocessor). In this case, though, Lua is much more powerful and flexible than CPP, and Terra is also a smaller, simpler language than C.
It gets more powerful, though, because the Lua parts don't just have to exist at compile-time. You could use Lua at run-time to produce Terra code, which in turn can get executed immediately, producing object code specialized to a given situation or machine: for example, you might write a language, use Lua to parse it, and then convert it to Terra to produce object code which is then invoked immediately: this effectively just-in-time compiles your code. Or, you could write an algorithm in Terra with specific constants that aren't known until run-time, and then use Lua to stitch those constants in before running the algorithm: for example, to take advantage of specific machine features or parameters.
These examples also assume that you have only "compile" and "run" stages; you could also write programs with more stages: for example "compile source", "specialize for machine", and "run" could be three distinct stages. Terra/Lua would afford you the flexibility to stage your computations in this way.