项目作者: Swizec

项目描述 :
LOLCODE implementation with Babel macros
高级语言: JavaScript
项目地址: git://github.com/Swizec/lolcode-babel-macro.git
创建时间: 2019-05-01T04:27:47Z
项目社区:https://github.com/Swizec/lolcode-babel-macro

开源协议:MIT License

下载


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 🐱

  1. HAI
  2. I HAS A count ITS 1
  3. IM IN YR fizzbuzz UPPIN YR count TIL BOTH SAEM count AN 30
  4. I HAS A div ITS MOD OF count AN 3
  5. IT R BOTH SAEM 0 AN div
  6. O RLY?
  7. YA RLY
  8. VISIBLE "Fizz"
  9. MEBBE BOTH SAEM 0 AN MOD OF count AN 5
  10. VISIBLE "Buzz"
  11. NO WAI
  12. VISIBLE count
  13. OIC
  14. IM OUTTA YR fizzbuzz
  15. KTHXBYE

JavaScript comes out ✌️

  1. var fizzbuzz = function (stdlib) {
  2. return function () {
  3. var IT;
  4. var count = 1;
  5. for (; !stdlib["BOTH SAEM"](count, 30); count++) {
  6. var _IT = void 0;
  7. var div = stdlib["MOD OF"](count, 3);
  8. _IT = stdlib["BOTH SAEM"](0, div);
  9. if (_IT) {
  10. var _IT2 = void 0;
  11. console.log("Fizz");
  12. } else if (stdlib["BOTH SAEM"](0, stdlib["MOD OF"](count, 5))) {
  13. var _IT3 = void 0;
  14. console.log("Buzz");
  15. } else {
  16. var _IT4 = void 0;
  17. console.log(count);
  18. }
  19. ;
  20. }
  21. ;
  22. };
  23. }(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

  1. $ npm install lolcode.macro

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

  1. import { lolcode } from "lolcode.macro";
  2. import lolstdlib from "lolcode.macro/dist/lolstdlib";
  3. const fizzbuzz = lolcode`
  4. HAI
  5. I HAS A count ITS 1
  6. IM IN YR fizzbuzz UPPIN YR count TIL BOTH SAEM count AN 30
  7. I HAS A div ITS MOD OF count AN 3
  8. IT R BOTH SAEM 0 AN div
  9. O RLY?
  10. YA RLY
  11. VISIBLE "Fizz"
  12. MEBBE BOTH SAEM 0 AN MOD OF count AN 5
  13. VISIBLE "Buzz"
  14. NO WAI
  15. VISIBLE count
  16. OIC
  17. IM OUTTA YR fizzbuzz
  18. KTHXBYE
  19. `;
  20. 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