Flipper: Your First Coco Program
Build, compile, and run a simple logic using the Coco compiler and Cocolab test environment.
Prerequisites: Install Coco as described in installation instructions.
Create a Module
A Coco module compiles to a manifest that can be deployed to MOI. Create one with:
mkdir flipper && cd flipper
coco nut init Flipper
This creates a coco.nut configuration file. Your folder structure:
flipper/
├── coco.nut
└── flipper.coco (create next)
Write the Code
Create flipper.coco:
flipper.coco
coco Flipper
state logic:
values Map[Identifier]Bool
endpoint deploy Init():
mutate values <- Flipper.Logic.values:
values[Sender] = true
endpoint dynamic Flip():
mutate values <- Flipper.Logic.values:
values[Sender] = !values[Sender]
endpoint static Mode() -> (value Bool):
observe values <- Flipper.Logic.values:
value = values[Sender]
endpoint dynamic Set(value Bool):
mutate values <- Flipper.Logic.values:
values[Sender] = value
Compile:
coco compile
This produces flipper.yaml — the manifest ready for deployment.
Run in Cocolab
Start Cocolab with automatic setup:
coco lab init
This registers a default_user, sets it as default.sender, and compiles the logic.
Deploy and Test
# Deploy the logic
deploy Flipper.Init()
# Check initial value
observe Flipper.Logic.values[default_user]
# → true
# Flip the value
invoke Flipper.Flip()
# Read via endpoint
invoke Flipper.Mode()
# → value: false
# Set directly
invoke Flipper.Set(value: true)
observe Flipper.Logic.values[default_user]
# → true
Test with Another User
# Register and switch to new user
register Robert
set default.sender Robert
# Test as Robert
invoke Flipper.Set(value: false)
invoke Flipper.Flip()
observe Flipper.Logic.values[Robert]
# → true
Exit Cocolab:
exit
Your first Coco program works.