{"path": "/posts/transpiling-code-from-other-languages/", "title": "Transpiling code from other languages · Bruno Dias", "html": "<!doctype html>\n<html lang=\"en\">\n<head>\n  <meta charset=\"utf-8\">\n  <meta name=\"viewport\" content=\"width=device-width, initial-scale=1\">\n  <meta name=\"description\" content=\"Transpiling code from other languages\">\n  <title>Transpiling code from other languages · Bruno Dias</title>\n  <link rel=\"canonical\" href=\"https://diasbruno.github.io/posts/transpiling-code-from-other-languages/\" data-post-metadata>\n  <meta name=\"author\" content=\"Bruno Dias\" data-post-metadata>\n  <meta property=\"og:type\" content=\"article\" data-post-metadata>\n  <meta property=\"og:title\" content=\"Transpiling code from other languages\" data-post-metadata>\n  <meta property=\"og:url\" content=\"https://diasbruno.github.io/posts/transpiling-code-from-other-languages/\" data-post-metadata>\n  <meta property=\"article:published_time\" content=\"2021-03-05T15:00:00Z\" data-post-metadata>\n  <link rel=\"icon\" href=\"/assets/site/signature.svg\" type=\"image/svg+xml\">\n  <link rel=\"alternate\" type=\"application/atom+xml\" title=\"diasbruno\" href=\"/atom.xml\">\n  <link rel=\"alternate\" type=\"application/json\" href=\"/posts/transpiling-code-from-other-languages/content.json\">\n  <link rel=\"stylesheet\" href=\"/assets/site/tokens.css\">\n  <link rel=\"stylesheet\" href=\"/assets/site/site.css\">\n  <link rel=\"stylesheet\" href=\"/assets/site/footer.css\">\n\n</head>\n<body class=\"light\">\n<div id=\"site-page\">\n<a class=\"skip-link\" href=\"#content\">Skip to content</a>\n<header class=\"site-header\">\n  <a class=\"brand\" href=\"/\" data-site-navigation aria-label=\"Bruno Dias, home\">\n    <img src=\"/assets/site/signature.svg\" width=\"24\" height=\"34\" alt=\"\">\n  </a>\n  <nav aria-label=\"Main navigation\">\n    <ul>\n<li><a class=\"nav-vrn\" href=\"/vrn/\" data-site-navigation><span class=\"nav-label\">vrn</span> <span class=\"nav-badge\">research</span></a></li>\n<li><a class=\"nav-posts\" href=\"/posts/\" data-site-navigation aria-current=\"location\">Posts</a></li>\n<li><a class=\"nav-projects\" href=\"/projects/\" data-site-navigation>Projects</a></li>\n<li><a class=\"nav-about\" href=\"/about/\" data-site-navigation>About</a></li>\n</ul>\n  </nav>\n</header>\n\n<main class=\"section\" id=\"content\" tabindex=\"-1\">\n  <article class=\"post-detail\">\n    <header class=\"section-header\">\n      <p class=\"post-category\">Post</p>\n      <h1>Transpiling code from other languages</h1>\n\n      <p><time datetime=\"2021-03-05T15:00:00Z\">2021-03-05</time></p>\n    </header>\n    <div class=\"post-columns\">\n      <section class=\"post-body\" aria-label=\"Article\">\n\n<p>Every time I realize I'm going to duplicate code that is already written somewhere else, I feel sad.</p>\n<p>Why not start transpiling code from other languages?</p>\n<p>This is not a new idea, but sometimes we need to remember of what we can do. Code is also data.</p>\n<p>The recipe is actually quite simple. Find out a good tool to generate/manipulate the AST (abstract syntax tree) of the language and just walk the tree collecting information and generate some code, string or maybe the AST, in the language you are going to use.</p>\n<p>One good example to use this approach: enums.</p>\n<p>Input:</p>\n<pre><code class=\"language-python\">from enum import Enum\n\nclass Color(Enum):\n    RED = 1\n    GREEN = 2\n    BLUE = 3\n</code></pre>\n<p>Output:</p>\n<pre><code class=\"language-typescript\">export default class Color {\n    static RED = 1;\n    static GREEN = 2;\n    static BLUE = 3;\n}\n</code></pre>\n<p>I want to &quot;avoid the problem&quot; of having to change this file to update the enums by hand. So, all I have to do is run the script that (re)generate the entire file.</p>\n<pre><code class=\"language-python\"># yeah...it doesn't need to be pretty :)\nimport ast\n\nenums = ast.parse(open('./enums.py', 'r').read())\n\n# filter only class definitions\nclasses = [kl for kl in enums.body if type(kl) == ast.ClassDef]\n\n# make a simple object of name and all available attributes\n# NOTE: if there are other declarations in the class,\n# you might need to filter the `options`.\ndef decode_enum_class(name, options):\n    return { &quot;name&quot;: name,\n             &quot;options&quot;: [(opt.targets[0].id, opt.value.s) for opt in options]}\n\n# render a single option, `name = value`, in javascript.\ndef render_option(option):\n    return &quot;  static {} = \\&quot;{}\\&quot;;&quot;.format(option[0], option[1])\n\n# render an entire python enum class.\ndef to_js(enum):\n    options = &quot;&quot;.join([render_option(opt) + &quot;\\n&quot;\n                       for opt in enum.get(&quot;options&quot;)])\n    return &quot;&quot;&quot;\nexport class {} {{\n{}}}\n    &quot;&quot;&quot;.format(enum.get(&quot;name&quot;), options)\n\n# composition to make the transformation\n# (name, body) |&gt; decode_enum_class |&gt; to_js\ntransform = lambda name, body: to_js(decode_enum_class(name, body))\n\n# dump to the terminal\nprint(&quot;&quot;.join([transform(x.name, x.body) for x in classes]))\n</code></pre>\n<p>This simple technique is helpful to avoid typos and keep everything in sync without doing much.</p>\n\n      </section>\n      <aside class=\"post-links\" aria-labelledby=\"post-links-heading\">\n        <h2 id=\"post-links-heading\">Links</h2>\n        <ul>\n<li><a href=\"/posts/\" data-site-navigation>All posts</a></li>\n        </ul>\n      </aside>\n    </div>\n  </article>\n</main>\n\n<footer class=\"site-footer\">\n  <div class=\"site-footer-inner\">\n    <a class=\"site-footer-home\" href=\"/\" data-site-navigation>Bruno Dias</a>\n    <ul aria-label=\"Footer links\">\n      <li><a href=\"https://github.com/diasbruno\">GitHub</a></li>\n      <li><a href=\"/atom.xml\">Atom feed</a></li>\n    </ul>\n  </div>\n</footer>\n\n</div>\n<script type=\"module\" src=\"/assets/site/navigation.js\"></script>\n</body>\n</html>\n"}
