5

100 Languages Speedrun: Episode 02: Emojicode

 2 years ago
source link: https://dev.to/taw/100-languages-speedrun-episode-02-emojicode-1lbh
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
neoserver,ios ssh client

Most programming languages are created to address some genuine need, but some are just an art statement. One such art statement is the Emojicode language - self-described as a "full-blown programming language consisting of emojis."

Hello World

Let's start by writing hello.๐Ÿ‡:

๐Ÿ’ญ๐”€๐“ธ๐”€
๐Ÿ ๐Ÿ‡
  ๐Ÿ˜€ ๐Ÿ”ค๐Ÿ„ท๐Ÿ„ด๐Ÿ„ป๐Ÿ„ป๐Ÿ„พ, ๐Ÿ…†๐Ÿ„พ๐Ÿ…๐Ÿ„ป๐Ÿ„ณ๐Ÿ”คโ—๏ธ
๐Ÿ‰
Enter fullscreen modeExit fullscreen mode

As you can see, usual keywords have been replaced by emoji:

  • ๐Ÿ’ญ are comment lines
  • ๐Ÿ ๐Ÿ‡ ... ๐Ÿ‰ is the main function
  • ๐Ÿ”ค is like quotes
  • ๐Ÿ˜€...โ—๏ธ is like print
  • in general, โ—๏ธ ends argument list

The contents of the strings and comments don't have to be emoji, but I used my app for fancy text to spice them up as well.

Variables

We can put thing in variables by using โžก๏ธ. Variables go on the right, which is fairly unconventional choice, but not completely unheard of. ๐Ÿงฒ does string interpolation:

๐Ÿ ๐Ÿ‡
  ๐Ÿ”คโ“”โ“ฅโ“”โ“กโ“จโ“žโ“โ“”๐Ÿ”คโžก๏ธwho
  ๐Ÿ˜€ ๐Ÿ”ค๐Ÿ„ท๐Ÿ„ด๐Ÿ„ป๐Ÿ„ป๐Ÿ„พ, ๐Ÿงฒwho๐Ÿงฒ๐Ÿ”คโ—๏ธ
๐Ÿ‰
Enter fullscreen modeExit fullscreen mode

Lists and for loops

Lists are created by ๐Ÿฟ...๐Ÿ†. I get the first emoji, not so sure about the second one.

You can loop with ๐Ÿ”‚ iterator list ๐Ÿ‡ body ๐Ÿ‰, which is a decent loop emoji:

๐Ÿ ๐Ÿ‡
  ๐Ÿฟ
    ๐Ÿ”คPython๐Ÿ”ค
    ๐Ÿ”คJavaScript๐Ÿ”ค
    ๐Ÿ”คRuby๐Ÿ”ค
    ๐Ÿ”คEmojicode๐Ÿ”ค
    ๐Ÿ”คPHP๐Ÿ”ค
  ๐Ÿ†โžก๏ธlanguages
  ๐Ÿ˜€ ๐Ÿ”คMost important languages to learn๐Ÿ”คโ—๏ธ
  ๐Ÿ”‚ language languages ๐Ÿ‡
    ๐Ÿ˜€ languageโ—๏ธ
  ๐Ÿ‰
๐Ÿ‰
Enter fullscreen modeExit fullscreen mode

FizzBuzz

We need a few more things here.

  • ๐Ÿ”‚ variable ๐Ÿ†•โฉ start afterend step is a for i in range(start, afterend, step) loop.
  • i ๐Ÿšฎ 15 ๐Ÿ™Œ 0 is i % 15 == 0
  • โ†ช๏ธ, ๐Ÿ™…โ€โ†ช๏ธ, and ๐Ÿ™… are if, else if, and else

And with that we can write a FizzBuzz, using fancy fonts for output as well:

๐Ÿ ๐Ÿ‡
  ๐Ÿ”‚ i ๐Ÿ†•โฉ 1 101 1 โ— ๐Ÿ‡
    โ†ช๏ธ i ๐Ÿšฎ 15 ๐Ÿ™Œ 0 ๐Ÿ‡
      ๐Ÿ˜€ ๐Ÿ”ค๐”ฝ๐•š๐•ซ๐•ซ๐”น๐•ฆ๐•ซ๐•ซ๐Ÿ”ค โ—
    ๐Ÿ‰
    ๐Ÿ™…โ†ช๏ธ i ๐Ÿšฎ 3 ๐Ÿ™Œ 0 ๐Ÿ‡
      ๐Ÿ˜€ ๐Ÿ”ค๐”ฝ๐•š๐•ซ๐•ซ๐Ÿ”ค โ—
    ๐Ÿ‰
    ๐Ÿ™…โ†ช๏ธ i ๐Ÿšฎ 5 ๐Ÿ™Œ 0 ๐Ÿ‡
      ๐Ÿ˜€ ๐Ÿ”ค๐”น๐•ฆ๐•ซ๐•ซ๐Ÿ”ค โ—
    ๐Ÿ‰๐Ÿ™…๐Ÿ‡
      ๐Ÿ˜€ ๐Ÿ”ค๐Ÿงฒi๐Ÿงฒ๐Ÿ”ค โ—
    ๐Ÿ‰
  ๐Ÿ‰
๐Ÿ‰
Enter fullscreen modeExit fullscreen mode

Doubling things

We need one more thing - functions.

Well, we're a bit lost. Documentation doesn't say anything about functions. It has closures so we'd think that this might work:

๐Ÿ ๐Ÿ‡
  ๐Ÿ‡ i ๐Ÿ”ขโžก๏ธ๐Ÿ”ข
    โ†ฉ๏ธ iโž•i
  ๐Ÿ‰ โžก๏ธ double

  ๐Ÿ˜€ ๐Ÿ”ค๐Ÿงฒโ‰๏ธ double 21โ—๐Ÿงฒ๐Ÿ”คโ—
๐Ÿ‰
Enter fullscreen modeExit fullscreen mode

We need to declare types. ๐Ÿ”ขโžก๏ธ๐Ÿ”ข means takes an integer, and returns an integer. โ†ฉ๏ธ is return. โ‰๏ธ...โ— is function call. Not too bad.

Y Combinator fail

Unfortunately these cannot be recursive, so this doesn't work at all!

๐Ÿ ๐Ÿ‡
  ๐Ÿ‡ n ๐Ÿ”ขโžก๏ธ๐Ÿ”ข
    โ†ช๏ธ n โ—€๏ธ 3 ๐Ÿ‡
      โ†ฉ๏ธ 1
    ๐Ÿ‰๐Ÿ™…๐Ÿ‡
      ๐Ÿ’ญ Does not work
      โ†ฉ๏ธ โ‰๏ธ fib nโž–1โ—โž•โ‰๏ธ fib nโž–2โ—
    ๐Ÿ‰
  ๐Ÿ‰ โžก๏ธ fib

  ๐Ÿ˜€ ๐Ÿ”ค๐Ÿงฒโ‰๏ธ fib 5โ—๐Ÿงฒ๐Ÿ”คโ—
๐Ÿ‰
Enter fullscreen modeExit fullscreen mode

Well, I thought about doing the old Lisp trick, and pass fib closure as argument to fib, so it can call itself, but that only works if there are no stupid types getting in the way. It's possible for type system to support Y combinator if it supports recursive types, but I don't think Emojicode does.

Classes

The documentation doesn't say anything about functions, but it has classes. So let's define class ๐Ÿค– with method ๐Ÿš€ that does fib:

๐Ÿ‡ ๐Ÿค– ๐Ÿ‡
  ๐Ÿ†•๐Ÿ‡
  ๐Ÿ‰

  โ—๏ธ ๐Ÿš€ n ๐Ÿ”ขโžก๏ธ๐Ÿ”ข ๐Ÿ‡
    โ†ช๏ธ n โ—€๏ธ 3 ๐Ÿ‡
      โ†ฉ๏ธ 1
    ๐Ÿ‰๐Ÿ™…๐Ÿ‡
      โ†ฉ๏ธ ๐Ÿš€๐Ÿ‘‡ nโž–1โ—โž•๐Ÿš€๐Ÿ‘‡ nโž–2โ—
    ๐Ÿ‰
  ๐Ÿ‰
๐Ÿ‰

๐Ÿ ๐Ÿ‡
  ๐Ÿ†•๐Ÿค–โ—๏ธ โžก๏ธ fib

  ๐Ÿ”‚ i ๐Ÿ†•โฉ 1 31 1 โ— ๐Ÿ‡
    ๐Ÿš€fib iโ—๏ธ โžก๏ธ x
    ๐Ÿ˜€ ๐Ÿ”ค๐Ÿงฒx๐Ÿงฒ๐Ÿ”คโ—
  ๐Ÿ‰
๐Ÿ‰
Enter fullscreen modeExit fullscreen mode

If you've been following along, this code shouldn't be too difficult to understand.

  • ๐Ÿ†•๐Ÿค–โ—๏ธ โžก๏ธ fib - creates a new ๐Ÿค– and assigns it to fib variable
  • ๐Ÿš€fib iโ—๏ธ โžก๏ธ x - calls ๐Ÿš€ on fib instance, calling our only method with argument i
  • ๐Ÿ‡ ๐Ÿค– ๐Ÿ‡ ... ๐Ÿ‰ - definition of a class with name ๐Ÿค– (class names must be emoji)
  • ๐Ÿ†•๐Ÿ‡๐Ÿ‰ - empty constructor, we need some constructor for every class, even if it doesn't do anything
  • โ—๏ธ ๐Ÿš€ n ๐Ÿ”ขโžก๏ธ๐Ÿ”ข ๐Ÿ‡...๐Ÿ‰ - defining method ๐Ÿš€...โ—๏ธ that taken one integer argument n and returns an integer
  • โ†ฉ๏ธ ๐Ÿš€๐Ÿ‘‡ nโž–1โ—โž•๐Ÿš€๐Ÿ‘‡ nโž–2โ— - recursive call, ๐Ÿš€๐Ÿ‘‡ nโž–1โ— is this.๐Ÿš€(n-1) in more usual language

Unicode

This language was of course created as an art statement (or as a joke if you prefer to put it that way), but it asks an important question. Throughout the history of programming, nearly every language (APL and Raku being the most notable exception, and don't worry - we'll surely cover both in this series) limited itself to just plain text with ASCII characters.

Even though ASCII symbols were clearly not enough, so symbols get reused to mean ten different things (try to list all syntactic meanings of {} characters in JavaScript or actually in just about any language), and long strings of characters like !==, **= and such get used as fake extra characters.

So the question is - should programming languages keep limiting themselves to just ASCII, or is it finally time to embrace the whole Unicode? An interesting middle ground are ligature fonts like Fira Code - the source code would still say !==, but in the editor you'd see โ‰ข.

All code examples for the series will be in this repository.

Code for the Emojicode episode is available here.


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK