Getting Started

Prerequisites

  • A C compiler (gcc or clang)
  • Git

Installation

# Clone the repository
git clone https://gitea.pockle.world/john/pit
cd pit

# Build the runtime
make

This compiles the ƿit runtime and installs the pit binary. The ƿit shop is created at ~/.pit/.

Verify your installation:

pit version

Hello World

Create a file called hello.ce:

// hello.ce
log.console("Hello, ƿit!")
$stop()

Run it:

pit hello

You should see Hello, ƿit! printed to the console.

Every .ce file is an actor — an independent unit of execution. The $stop() call tells the actor to shut down when it’s done.

A Counting Actor

Actors can schedule work over time. Create counter.ce:

// counter.ce
var count = 0

$clock(function(dt) {
  count = count + 1
  log.console(`tick ${count}`)
  if (count >= 5) {
    $stop()
  }
})
pit counter

The $clock intrinsic calls your function every tick. The actor runs until you stop it.

Two Actors Talking

The power of ƿit is in actors communicating through messages. Create two files:

// greeter.ce
$receiver(function(msg) {
  send(msg, {greeting: `Hello, ${msg.name}!`})
})
// main.ce
$start(function(reply, reason) {
  if (!reply) return log.error("could not start greeter:", reason)
  send(reply.actor, {name: "world"}, function(response) {
    log.console(response.greeting)
    $stop()
  })
}, "greeter")
pit main

$start launches a new actor. $send sends a message and provides a callback for the reply. Messages are automatically serialized — actors never share memory.

Using Modules

Modules (.cm files) return a value that is cached and frozen. Create a module:

// math_helpers.cm
def math = use('math/radians')

var square = function(x) {
  return x * x
}

var distance = function(x1, y1, x2, y2) {
  var dx = x2 - x1
  var dy = y2 - y1
  return math.sqrt(dx * dx + dy * dy)
}

return {
  square: square,
  distance: distance
}

Use it from an actor:

// calc.ce
def helpers = use('math_helpers')

log.console(helpers.square(5))             // 25
log.console(helpers.distance(0, 0, 3, 4))  // 5

$stop()

Creating a Package

To share code or manage dependencies, create a package.json beside your source:

{
  "name": "my-package",
  "dependencies": {}
}

Your package can now use pit build and pit test, and can be named as a dependency by other packages.

What’s Next