-1

I am in the process of writing interpreters for a couple of languages, in TeX, which would allow TeX users to insert some code from their favorite language (if supported), and have TeX run it when producing the pdf result.

I started by writing an interpreter for Brainfuck, since it is a very simple language. I thought GolfScript would be a piece of cake, but it is richer than I had expected (mostly because it is based on the rather elaborate Ruby). I'll probably do Whitespace for the sake of it. But none of those is actually used by people, so up to now the whole process is mostly an exercise to see how to best write interpreters in TeX.

My question is: what real-world language should I consider? It should have the following qualities:

  • simple (I'm not ready for Python),
  • typically be used as one-liners (if possible),
  • and have a reasonably large user base.

I'm assuming that every language can have an interpreter (compiling only enhances the speed), please mention if you think of technical hurdles for the proposed language.

EDIT: I am also interested in comments such as "implement Perl 2, then gradually add support for later versions" (no idea if that particular scenario is a good idea, though). I've already coded some support for regular expressions.

Bruno Le Floch
  • 244
  • 4
  • 12
  • 1
    Is this something where it runs the code and replaces it with that codes output, like a preprocessor? I can't imagine that writing your own interpreter is the way to go, you should just extract the code sections and pass them to the real compiler to run and return the result. – Jordan Bentley Nov 21 '11 at 22:05
  • I agree. Unfortunately, old TeX engines do not have the ability to call external programs. And newer TeX engines often disable this feature, since having access to the OS is a potential security risk. --- If that's not enough, just think of it as a wasteful exercise :-). – Bruno Le Floch Nov 21 '11 at 22:16

2 Answers2

2

How about a stack based language like Forth (or even a subset of PostScript)? Stack operations should translate to TeX constructs relatively easily. Finally, if all of this is done for the sake of an exercise, what about implementing a C preprocessor?

  • Thank you for the idea! I've implemented a (Turing complete) [subset of Forth](https://github.com/blefloch/latex-runner) this weekend. I still need to implement quite a few of the Core words, but this should just take me one more weekend, I think. I'll think about extending this to PostScript, which could have practical uses since it would allow people to use some ps-specific code and still go directly (tex => pdf) rather than going (tex => dvi => ps => pdf). – Bruno Le Floch Apr 22 '13 at 08:06
1

You can use some LISP based language like Beginning Student Language or some ML variant like OCaml. For a simple exercise in interpreting ML-like code, just implementing definitions, function applications, arithmetic and conditional expressions gives you a nice start:

let rec factorial n =
    if n = 0 then 1 else n * (factorial (n-1))

let sqr x = x * x

let compose f g x = f (g x)

(compose sqr sqr) (factorial 3)

Also consider Lua which aims at being a good scripting language that can be used to let users customize your applications and is pretty popular.

Adrian Panasiuk
  • 7,249
  • 5
  • 33
  • 54