Code Monkey home page Code Monkey logo

lolcode-babel-macro's Introduction

LOLCODE-to-JavaScript compiler babel macro

A fully functioning LOLCODE-to-JavaScript compiler implemented as a Babel macro. You never thought you wanted this and now here it is. You’re welcome. 🐱

Let me start by proving that this crazy contraption works πŸ‘‡

https://codesandbox.io/s/github/Swizec/lolcode.macro-example

Here you have a CodeSandbox with the legendary FizzBuzz implemented in LOLCODE. A Babel macro compiles it to a JavaScript function at build-time and you use it as any ole JavaScript at runtime.

LOLCODE goes in 🐱

HAI
    I HAS A count ITS 1
    IM IN YR fizzbuzz UPPIN YR count TIL BOTH SAEM count AN 30
        I HAS A div ITS MOD OF count AN 3
        IT R BOTH SAEM 0 AN div
        O RLY?
            YA RLY
                VISIBLE "Fizz"
            MEBBE BOTH SAEM 0 AN MOD OF count AN 5
                VISIBLE "Buzz"
            NO WAI
                VISIBLE count
        OIC
    IM OUTTA YR fizzbuzz
KTHXBYE

JavaScript comes out ✌️

var fizzbuzz = function (stdlib) {
  return function () {
    var IT;
    var count = 1;

    for (; !stdlib["BOTH SAEM"](count, 30); count++) {
      var _IT = void 0;

      var div = stdlib["MOD OF"](count, 3);
      _IT = stdlib["BOTH SAEM"](0, div);

      if (_IT) {
        var _IT2 = void 0;

        console.log("Fizz");
      } else if (stdlib["BOTH SAEM"](0, stdlib["MOD OF"](count, 5))) {
        var _IT3 = void 0;

        console.log("Buzz");
      } else {
        var _IT4 = void 0;

        console.log(count);
      }

      ;
    }

    ;
  };
}(lolcode_macro_dist_lolstdlib__WEBPACK_IMPORTED_MODULE_2__["default"]);

Taken from Chrome DevTools source maps. That's after Webpack and Babel do their thing. Intermediate output from lolcode.macro is modern JavaScript with lets and consts.

How to use

Install from NPM

$ npm install lolcode.macro

Then import and use similarly to CSS-in-JS or GraphQL

import { lolcode } from "lolcode.macro";
import lolstdlib from "lolcode.macro/dist/lolstdlib";

const fizzbuzz = lolcode`
HAI
    I HAS A count ITS 1
    IM IN YR fizzbuzz UPPIN YR count TIL BOTH SAEM count AN 30
        I HAS A div ITS MOD OF count AN 3
        IT R BOTH SAEM 0 AN div
        O RLY?
            YA RLY
                VISIBLE "Fizz"
            MEBBE BOTH SAEM 0 AN MOD OF count AN 5
                VISIBLE "Buzz"
            NO WAI
                VISIBLE count
        OIC
    IM OUTTA YR fizzbuzz
KTHXBYE
`;

fizzbuzz();

Details best grokked from this CodeSandbox

Wanna know more?

Here is a full blog post about how it works πŸ‘‰ LOLCODE-to-JavaScript compiler babel macro

lolcode-babel-macro's People

Contributors

swizec avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

lolcode-babel-macro's Issues

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    πŸ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. πŸ“ŠπŸ“ˆπŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❀️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.