$ cat posts/fake-it-till-you-make-it.md

Fake it till you make it

(Or how to build a language in 60 hours)

es Leer este post en español →

Although the phrase “fake it 'til you make it” is used as a term associated with self-help, I am going to take it more literally to explain what I just did.

Some 5 years ago I declared on this blog my intention to build a new programming language. In fact its GitHub repository goes back even further.

I named the language Ogú, after the character created by Themo Lobos, the Chilean cartoonist: the likable caveman who is Mampato’s friend. I also told you how I met the illustrator’s daughter to get permission to use an image of Ogú as the project’s mascot.

Months went by, then years, some friends teased me about it, and the project didn’t seem to move. Designing a programming language is complex, but this one in particular is also quite ambitious and, to be frank with you, I had no idea what I was doing.

The design went through several changes. Along the way I learned a lot more about functional programming and grew steadily more critical of object-oriented programming. For years I tried out various tools for building the language, I simplified the design, and it gradually lost its object-oriented flavor and became more of a functional language, because that is the paradigm I find most powerful for these times.

Late last year I decided that from November through March I would mount a new assault to get a first version of Ogú out.

But I couldn’t make progress. January arrived and all I had was a compiler that could timidly generate code for a few simple expressions and write a plain “hello world” to the console.

There are several reasons why this project cannot go past March: after that I simply won’t have enough time or dedication to push it through, and I don’t want another year to go by without progress. Besides, I promised Themo that Ogú, the programming language, would be a teaching tool, and that is a promise I want to keep. So for me it is important to have a first version of Ogú that is usable, and I hope that with help from more people the language can evolve into a useful tool for teaching and programming.

So in January I was starting to despair, until I had an epiphany.

One day I was trying to debug the grammar and the code generation, and I decided to write code to visualize the AST generated by the compiler, so I could understand how expressions worked in Ogú and resolve some ambiguities. The most common way to represent the AST is as lists. For example, the expression:

 funcion a + 5

Is represented like this in the AST:

 (funcion (+ a 5))

This looks a lot like LISP. The reason is that LISP has the syntax it has because the developers of that language were so lazy they realized they could ask programmers to write the AST for them, and that way they didn’t have to develop a front end for their compiler; they simply built the AST backend (so nobody would suspect the trick, they renamed the AST and called it an S-Expression).

Then I thought, “if I can convert my grammar to S-Expressions, then I’ll do the same thing the creators of LISP did, but backwards!”. I’ll build a front end that converts an Ogú program into a LISP program and let the LISP backend run the programs.

On the other hand, an initial requirement for Ogú is that it run on the JVM, because I plan to use its ecosystem, and besides, that way I can go on to dominate the world of enterprise development, displacing Java and Scala.

Once I had drawn up my master plan, which I called “fake it 'til you make it”, I used the best LISP interpreter there is for the JVM (and the only one I know), Clojure, to build my compiler and interpreter plus an initial runtime library (Clojure’s, of course).

In less than 60 hours of work spread over about 2 months I was able to build the first version of Ogú: Plunke, in honor of the mad scientist who created Ferrilo the Automaton, another Themo Lobos character.

Professor Plunke and Ferrilo (creations of Themo Lobos

Professor Plunke and Ferrilo (creations of Themo Lobos

So there you have it: in this new world of fake news, I have my own fake programming language, but a pretty fun one that lets me experiment with several ideas I have.

This is version 0.1 of Ogú. There is a long road ahead, and this version sacrifices a few things. For example, it is a dynamic language; the initial proposal was to have static type checking, but I think that is something that needs validating. Maybe the answer is something in between, and that may be where Ogú has something to contribute to programming languages. There are concepts that have been in my head for several years that I also hope to capture in Ogú.

But building a programming language with Ogú’s ambition is not a task to take on alone. This “fake” stage of the language has to be left behind. The compiler has to stand on its own, function libraries need to be built, compilers created for other runtimes (.Net for example, or Javascript), maybe a compiler for native code. Frameworks have to be built so the language is useful and used by many programmers.

For that I need help. For now I need you to download the compiler, try it, write programs in Ogú and report bugs to me on GitHub. And help me create documentation in Spanish and English. I am ambitious about this project; I would like it to show up on Hacker News, and for GitHub to index programs written in Ogú. But for that, I repeat, I need help. If this excites you, get in touch with me, clone the project and send Pull Requests on GitHub.

Ogú is on GitHub at this address: https://github.com/lnds/Ogu. There is a website, which also needs work, at: http://www.ogu-lang.org/

And what is it like to program in Ogú?

Here are some examples:

Hello world:

 println! "hola mundo"

Solving problem 1 of Project Euler:

 union [3, 6..999] [5, 10..<1000] |> sum |> println!

Computing the factorial:

 fact 0 = 1
 fact 1 = 1
 fact n = n * fact (n - 1)

Quicksort

 quicksort [x & xs] = smallerSorted ++ [x] ++ biggerSorted
  where
    smallerSorted = quicksort [a | a <- xs & a <= x]
    biggerSorted = quicksort [a | a <- xs & a > x]

The game of Toque y Fama (Bulls and Cows)

def ingresar tam = prompt! #"Ingresa una secuencia de ${tam} dígitos distintos (o escribe salir):"def famas num xs = zip num xs |> filter \x -> first x == second x |> countdef toques num xs = num |> filter \x -> (elem x xs)  |> countdef validar n xs = if (length num) == n then num else []
    where num = xs |> filter is-digit?  |> map to-digit  |> uniqlet tam = 5let sec = shuffle [0..9] |> take tamprintln! $ fmt "Bienvenido a Toque y Fama.
==========================\n\n
En este juego debes tratar de adivinar una secuencia de ${tam} dígitos generadas por el programa.\n
Para esto ingresas ${tam} dígitos distintos con el fin de adivinar la secuencia.\n
Si has adivinado correctamente la posición de un dígito se produce una Fama.\n
Si has adivinado uno de los dígitos de la secuencia, pero en una posición distinta se trata de un Toque.\n\nEjemplo: Si la secuencia es : [8, 0, 6, 1, 3] e ingresas  40863, entonces en pantalla aparecerá:\n
tu ingresaste [4, 0, 8, 6, 3]\n
resultado: 2 Toques 2 Famas\n\n\n"loop intentos = 1, accion = ingresar tam in
     if accion == "salir" || empty? accion then
        println! "\ngracias por jugar, adios."
     else
        let num = validar tam accion in 
            if empty? num then 
            begin
               println! "error!\n"
               repeat inc intentos, ingresar tam
            end
            else 
            begin
               println! "tu ingresaste " num
               let (toques, famas) = (toques num sec, famas num sec) in 
               begin         
                  println! "resultado: "  (toques - famas)  " Toques "  famas  "Famas"
                  if famas == tam then
                     println! "Ganaste! Acertaste al intento "   intentos  "! La secuencia era "  sec
                  else
                     repeat inc intentos, ingresar tam
               end
            end

Ogú is heavily influenced by Haskell, Clojure of course, F#, Elixir and Pascal (because of the begin end ;).

I will be writing about Ogú over the coming weeks, and of course I will solve the challenges in Ogú too. So stay tuned; I look forward to your comments and contributions.

Comments

comments powered by Disqus