DEV Community

loading...

The Super Tiny Compiler!

chenge profile image chenge ・1 min read

Code is more clear than words. I like short code.

A 100 lines file to explain compiler.

From the code below, We can see compile is the whole process. Parser is a step. Metaprogramming is at transform stage I think. AST is kernel data structure.

Know more compile will help you understand all kinds of languages.

link-->

/**
 * FINALLY! We'll create our `compiler` function. Here we will link together
 * every part of the pipeline.
 *
 *   1. input  => tokenizer   => tokens
 *   2. tokens => parser      => ast
 *   3. ast    => transformer => newAst
 *   4. newAst => generator   => output
 */

function compiler(input) {
  let tokens = tokenizer(input);
  let ast    = parser(tokens);
  let newAst = transformer(ast);
  let output = codeGenerator(newAst);

  // and simply return the output!
  return output;
}

Discussion

pic
Editor guide