Your first cobra4 program¶
Save this as hello.c4:
fn main() {
name = "world"
log("hello", to=name)
# `each` is a list comprehension when an expression
squares = each n in [1, 2, 3, 4] { n * n }
log("squares", values=squares)
}
main()
Run it:
You should see:
2026-05-07T17:00 level=info msg=hello to=world
2026-05-07T17:00 level=info msg=squares values=[1, 4, 9, 16]
What c4 run actually did¶
c4 run hello.c4 is a one-shot pipeline:
- Parse
hello.c4with the Lark grammar → AST. - Resolve scopes; warn on undefined names / shadowing.
- Type-check (gradual; advisory).
- Codegen → Python source.
- Execute the generated Python with the cobra4 runtime imported.
To see the Python that gets executed:
Other useful commands¶
c4 fmt hello.c4 # canonical re-format from AST
c4 check hello.c4 # lint + type warnings, no execution
c4 repl # multi-line REPL with completion + history
c4 doc hello.c4 # extract docstrings to markdown
c4 test # discover and run tests/test_*.c4
c4 serve hello.c4 # run as a daemon (every / on event / serve)
c4 lsp # language server (used by IDE extensions)