154 lines
2.7 KiB
Markdown
154 lines
2.7 KiB
Markdown
---
|
|
title: "Getting Started"
|
|
description: "Install ƿit and write your first program"
|
|
---
|
|
|
|
## Prerequisites
|
|
|
|
- A C compiler (gcc or clang)
|
|
- [Meson](https://mesonbuild.com/) build system
|
|
- Git
|
|
|
|
## Installation
|
|
|
|
```bash
|
|
# Clone the repository
|
|
git clone https://gitea.pockle.world/john/cell
|
|
cd cell
|
|
|
|
# Bootstrap the build
|
|
make bootstrap
|
|
```
|
|
|
|
This compiles the ƿit runtime and installs the `pit` binary. The ƿit shop is created at `~/.pit/`.
|
|
|
|
Verify your installation:
|
|
|
|
```bash
|
|
pit version
|
|
```
|
|
|
|
## Hello World
|
|
|
|
Create a file called `hello.ce`:
|
|
|
|
```javascript
|
|
// hello.ce
|
|
print("Hello, ƿit!")
|
|
$stop()
|
|
```
|
|
|
|
Run it:
|
|
|
|
```bash
|
|
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`:
|
|
|
|
```javascript
|
|
// counter.ce
|
|
var count = 0
|
|
|
|
$clock(function(dt) {
|
|
count = count + 1
|
|
print(`tick ${count}`)
|
|
if (count >= 5) {
|
|
$stop()
|
|
}
|
|
})
|
|
```
|
|
|
|
```bash
|
|
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:
|
|
|
|
```javascript
|
|
// greeter.ce
|
|
$receiver(function(msg, reply) {
|
|
reply({greeting: `Hello, ${msg.name}!`})
|
|
})
|
|
```
|
|
|
|
```javascript
|
|
// main.ce
|
|
$start(function(greeter) {
|
|
$send(greeter, {name: "world"}, function(response) {
|
|
print(response.greeting)
|
|
$stop()
|
|
})
|
|
}, "greeter")
|
|
```
|
|
|
|
```bash
|
|
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:
|
|
|
|
```javascript
|
|
// math_helpers.cm
|
|
var square = function(x) {
|
|
return x * x
|
|
}
|
|
|
|
var distance = function(x1, y1, x2, y2) {
|
|
var math = use('math/radians')
|
|
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:
|
|
|
|
```javascript
|
|
// calc.ce
|
|
var helpers = use('math_helpers')
|
|
|
|
print(helpers.square(5)) // 25
|
|
print(helpers.distance(0, 0, 3, 4)) // 5
|
|
|
|
$stop()
|
|
```
|
|
|
|
## Creating a Package
|
|
|
|
To share code or manage dependencies, create a `pit.toml`:
|
|
|
|
```toml
|
|
package = "myproject"
|
|
version = "0.1.0"
|
|
|
|
[dependencies]
|
|
```
|
|
|
|
Your package can now use `pit build`, `pit test`, and install dependencies.
|
|
|
|
## What's Next
|
|
|
|
- [**Language Manual**](/manual/) — full syntax reference, actors, packages, standard library
|
|
- [**CLI Reference**](/cli/) — all `pit` commands
|
|
- [**Language Spec**](/spec/) — internals for implementers
|