How to compile for the Erlang VM

762 views Asked by At

I'm trying to implement a small description language for user scripting in an application. Basically users describe how objects are created from other objects. (Not really a programming language). I could use JSON as well (but it would be more verbose).

I use leex and yecc and i have a decent parse tree. Say it is something like this :

{def,double,   %% function name double
    [{x}],     %% list of input names
    %% return expression, in this case {Operator, Operand1, Operand2}
    {'*',{var,x},{number,2}} 
}.

With this tree, i would like to build something like this:

double(State) ->
    _Var1 = some_app:get_input(State,x),
    _Var1 * 2.

But i don't know where to start. I can read the tree at runtime and build funs but i really want to learn how to compile (and hope better performance).

Should i transform my parse tree to Erlang AST and then compile erlang module ?

Thanks.

1

There are 1 answers

2
lud On BEST ANSWER

So, the classic workflow is this :

  1. Define tokens and build a lexer with leex
  2. Define a grammar and build a parser with yecc
  3. Tokenize source code with the lexer
  4. Build a parse tree with the parser fed with the tokens
  5. Generate core erlang forms module whith cerl
  6. Compile forms with compile module. It can yield beam binaries to load as a module or create a .beam file

As an alternative to leex/yecc (LALR parser) we can use neotoma which works with PEG grammars. It depends on what you have to parse. There i needed left recursion and didn't want to handle whitespace.

As a target, the EVM (BEAM) seems quite easy. Compiling ends up to simply translate from one language to another and Core Erlang is a very simple language but still expressive.