Ebonicode++: Hackin C++ Gangsta Style

On December 3, 2011, in Code Monkey, by Tom

A little part of me died doing this to Bjarne, but I couldn't resist.

“Foshizzle” is a modified version of the Clang compiler which supports Ebonicode++ (an alternate C++ syntax). Foshizzle supports all the existing features of Clang, but with extensions to more fully express your gansta style. All the regular C++ syntax still works, but you can now substitute Ebonicode++ keywords and operators (see below).

Here’s a sample method that calculates factorials (Note: the semicolons can be replaced with a certain expletive):

int factorialz(int n)
{
    int rezult be 1;
    slongas (n bepimpin 0)
    {
        rezult be rezult dimes n;
        dissin n;
    }
    putou rezult;
}

 

I can’t take all the credit. I was inspired by the Iowa State students that first created Ebonicode (webpage no longer available).

Here are my Clang modifications: ebpp-clang30rc4.patch

Build instructions are near the end of this post.

How It Happened

I periodically venture off into the h4x0r wilderness and pour myself into some random project. It always starts out innocently enough, but the project soon takes on a life of it’s own. This time was no different… except it happened over Thanksgiving (much to my wife’s chagrin).

Being the charismatic socialite that I am (sarcasm), I was digging through the C++11 spec on Thanksgiving. What struck me was how much of my language design course I had forgotten, and it really bugged me. It’s one of those required courses in the CompSci graduate program that I loathed having to take, but turned out to be invaluable (don’t get me started on the database and UI design courses… blah).

Long story less long, I started refreshing on compilers (read this for inspiration). I’d heard about Clang and LLVM awhile ago, and I remembered their focus on modularity, maintainability, and all sorts of other *ability’s. I figured it’d be a good place to start, so I download the source code and dug through it (watch this video for background on Clang).

Stepping through Clang in a debugger sounded like a great way to start, but I quickly had mental stack overflow through the recursive parsing code. Okay, time for plan B. Common wisdom says that a good way to learn a piece of code is to try and fix a bug. So I went to the Clang bug list looking for a starter task, but all the “interesting” tasks were way too large in scope for a Clang newb like me.

Time to invent plan C: make a small tweak to the language and see if I can get it working. I remembered a few years ago that some students from Iowa State built an Ebonicode compiler for a language class they had. It got quite a few laughs around the office and was always wishing for a working compiler. So I decided take their idea and replace ‘while’ with ‘slongas’… and an hour later I had it working (which is testament to the readability of the Clang code… it’s very well commented).

Not satisfied to stop there, I moved on to other keywords… simple, once you get comfortable with how the code’s laid out. “What about operators and punctuators?”, I wondered. After a few failed attempts, it dawned on me that C++ has aliases for operators (‘and_eq’ can be used in place of ‘&=’, etc). Instead of heavily modifying the parser/lexer, I started looking for how Clang handled these aliases… maybe I could just add some more. Turns out, it fairly simple to add new aliases, so ‘==’ got overloaded with ‘sameas’, ‘!=’ got overloaded with ‘aint’, etc… and it worked! Sweet!

After all that, I dug a little into the preprocessor and figured out how to support ‘#mahomie’ in place of ‘#include’. Simple enough. I dug a little into the build stuff and changed the name from ‘clang’ to ‘foshizzle’… cool. I little bit of frustration later, and I had it recognizing ‘*.opp’ and ‘*.ho’ as source and header files, respectively.

Three days after I started, I had a fully functional Ebonicode++ compiler and a lot deeper understanding of how a production C++ compiler works. I can’t stress enough that this project would never have happened if the Clang code wasn’t written so well. Kudos to the Clang team.

I really enjoyed this project, and I hope you get half the fun out of it I did. Next time you have a coding task, consider adding a litta gansta sizzle ;)

C++ To Ebonicode++ Mapping

 

C++ Ebonicode++ | C++ Ebonicode++
#include #mahomie | switch whatchugondo
break bustanut | this dis
case ifa | throw throz
catch wrekdit | true tru
char alphabizzle | try checkit
class claz | typedef reprezentin
const foodonchange | using uzin
continue mo | virtual frontin
delete cap | void shiznit
else elz | volatile trippin
export bounce | while slongas
extern othahood | ; bitch or biatch
false falz | = be
float flo | == sameas
for fo | != aint
friend homie | + an
long shlong | - widout
namespace namespaze | * dimes
new makea | / videdby
private nonyobiz | > bepimpin
protected indafamily | < fearin
public tweetdat | ++ propsta
return putou | -- dissin
  •  *.cpp -> *.opp
  • *.h -> *.ho

Many thanks to Sean Tegtmeyer for helping with the syntax definition.

Building Foshizzle

I highly suggest building Clang without my changes first and verify that you have it working correctly. Then it should be simple to apply my patch and rebuild. I did all my work on Fedora 16, so your mileage may vary on other distros or OSes.

Note: If you have any problems building LLVM or Clang, please refer to their build page: Getting Started

  1. Create a new directory (e.g. “foshizzle”).
  2. Within the “foshizzle” directory, create the following directories: “build”, “src”, “install”
  3. Checkout the LLVM source code into the “src” directory:
    • svn co http://llvm.org/svn/llvm-project/llvm/tags/RELEASE_30/rc4 ./src
  4. Create a new directory for the Clang source within the LLVM “tools” directory:
    • mkdir src/tools/clang
  5. Checkout the Clang source code into the “src/tools/clang” directoy:
    • svn co http://llvm.org/svn/llvm-project/cfe/tags/RELEASE_30/rc4 ./src/tools/clang
  6. Change directories into the “build” directory.
  7. Configure the Clang/LLVM build and point it to the “install” directory you created earlier:
    • ../src/configure --prefix=<path>/<to>/<foshizzle>/install
  8. Build (takes quite a while): make
  9. Install: make install
  10. Compile some test code to make sure it works: install/bin/clang++ yourtestcode.cpp

Only continue on once the unmodified version of Clang is working.

  1. Download my patch file: ebpp-clang30rc4.patch
  2. Change directories to where the Clang source is (src/tools/clang).
  3. Apply the patch: patch -p0 -i ebpp-clang30rc4.patch
  4. Change back to the “build” directory.
  5. Build: make
  6. Install: make install
  7. Test: install/bin/foshizzle++ yordopecodez.opp

You can also build the sample code below.

Sample Code

baseclaz.hofriendclaz.homain.opp

Building

install/bin/foshizzle++ main.opp

 

Program Output

baseclaz: protected = 1, private = 1
derivedclaz: protected = 0, private = 1
friendclaz: friend private = 0

Factorial(7) = 5040

 

Tagged with:  

4 Responses to Ebonicode++: Hackin C++ Gangsta Style

  1. MShizzle says:

    Ahhh… Iowa State… that figgers. Just for your edification – I think you mean “Ebonics”, not “Eubonics”. There’s no such thing as “Eubonics”, but perhaps Iowa State University students don’t have the ethnic exposures to realize that.

    Reply

  2. z80kid says:

    Thanks for the “edification”, Mshizzle. It’s a mystery how some of us get to be programmers and engineers without knowing the “proper” spelling for the crude ramblings of the illiterate underclass.

    I suppose we’re just lucky that we never hear “sup yo” or “fo shizzle” in a job interview. Wouldn’t we feel foolish.

    Reply

  3. solodon says:

    Why did you have to modify the Clang for that instead of just defining a bunch of macros in a dedicated header:

    // ——- ebonicode.hpp ——-
    #define whatchugondo switch
    #define bustanut break
    #define dis this
    #define ifa case
    #define throz throw
    #define wrekdit catch
    #define tru true
    #define alphabizzle char
    #define checkit try
    #define claz class
    #define reprezentin typedef
    #define foodonchange const
    #define uzin using
    #define mo continue
    #define frontin virtual
    #define cap delete
    #define shiznit void
    #define elz else
    #define trippin volatile
    #define bounce export
    #define slongas while
    #define othahood extern
    #define bitch or biatch ;
    #define falz false
    #define be =
    #define flo float
    #define sameas ==
    #define fo for
    #define aint !=
    #define homie friend
    #define an +
    #define shlong long
    #define widout -
    #define namespaze namespace
    #define dimes *
    #define makea new
    #define videdby /
    #define nonyobiz private
    #define bepimpin >
    #define indafamily protected
    #define fearin <
    #define tweetdat public
    #define propsta ++
    #define putou return
    #define dissin –

    Reply

    Tom reply on April 27th, 2012 4:37 pm:

    @solodon Good question. It’s true that I didn’t *have* to modify the compiler, but I chose to for 2 reasons:

    1. The purpose (excuse) of the project was to dig into Clang and see how it worked.
    2. You actually get correct compiler errors when the keywords are built into the system.

    Reply

Leave a Reply

Your email address will not be published. Required fields are marked *

*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>