From Go to ooc

Getting to know ooc, a small programming language that compiles to C99.

Similarities with Go

Let’s start off by listing some of the characteristics shared between Go and ooc:

  • Assignment using the declare-assign operator :=
  • Multiple return values from functions (via Tuples in ooc)
  • Support for writing platform-specific code (Build Constraints in Go, Version blocks in ooc)
  • Garbage collection (Disable with GOGC=off in Go and --gc=off in rock)

How is it different from Go?

Getting started

I initially installed rock using Homebrew, but then had some issues when compiling sam.

To install the rock compiler, simply clone the git repo and run make rescue:

mkdir -p ~/Work/ooc
cd ~/Work/ooc
git clone https://github.com/fasterthanlime/rock.git
cd rock
make rescue

Now you need to set some environment variables in your .bashrc

# ooc programming language
export OOC_LIBS=$HOME/Work/ooc
export PATH=$PATH:$OOC_LIBS/sam:$OOC_LIBS/rock/bin

If everything is set up properly, then you should be able to run rock like this:

rock -V
rock 0.9.10-head codename sapphire, built on Fri Jan 16 04:12:40 2015

We also want to install the sam package manager:

cd ~/Work/ooc
git clone https://github.com/fasterthanlime/sam.git
cd sam
rock -v

You should now be able to update sam by just by typing:

sam update
Pulling repository /Users/peter/Work/ooc/sam
> Current branch master is up to date.
Recompiling sam
> Cleaning up outpath and .libs
> [ OK ]

To get syntax highlighting in Vim using Vundle just add Plugin 'fasterthanlime/ooc.vim'

Example

A simple wget like tool written in ooc

# First you need to install curl
brew install curl
brew link curl --force

# Then clone the ooc bindings using sam
sam clone curl

cd $OOC_LIBS
mkdir wget
cd wget
vim wget.ooc

wget.ooc

use curl

import curl/Curl
import io/FileWriter, structs/ArrayList

writecb: func(buffer: Pointer, size: SizeT, nmemb: SizeT, fw: FileWriter) {
    fw write(buffer as CString, nmemb)
}

main: func(args: ArrayList<String>) {
    if(args size <= 1) {
        "Usage: %s URL output.html\n" printfln(args[0])
        exit(0)
    }

    url := args get(1)

    fw := FileWriter new(args[2])

    handle := Curl new()
    handle setOpt(CurlOpt url, url toCString())
    handle setOpt(CurlOpt writeData, fw)
    handle setOpt(CurlOpt writeFunction, writecb)
    handle perform()
    handle cleanup()

    fw close()
}

Compile it

rock -v -pr -O3 --gc=off wget.ooc

And run it

./wget http://c7.se/ test.html

Learn more about ooc