Skip to content

tans/coco

Repository files navigation

Coco

Coco is a small, indentation-based language for Bun. It is designed to feel lightweight like a script, structured like Python, and ready for modern JavaScript tooling.

const version = "0.1"

fn greet name
  print "Hello {name}"

if version == "0.1"
  greet "Coco"
else
  print "Unknown version"

Coco is early, but it now ships a working MVP compiler: lexer, parser, AST, JavaScript emitter, CLI commands, examples, docs, tests, and an optional runtime planning layer.

Try It

bun install
bun run install:global
coco

In the REPL:

coco> const name = "Coco"
KEYWORD("const") IDENTIFIER("name") ASSIGN("=") STRING("\"Coco\"") NEWLINE("\n")

Multi-line input:

coco> .paste
... if ok
...   print "yes"
...
KEYWORD("if") IDENTIFIER("ok") NEWLINE("\n") INDENT IDENTIFIER("print") STRING("\"yes\"") NEWLINE("\n") DEDENT

Useful REPL commands:

.paste    multi-line input
.tokens   toggle JSON token output
.clear    clear input buffer
.help     show help
.exit     exit

Lex a File

coco lex examples/hello.coco

Or without installing globally:

bun run src/cli.ts lex examples/hello.coco

Parse, Compile, and Run

coco parse examples/hello.coco
coco compile examples/hello.coco -o hello.js
coco run examples/hello.coco

Without -o, compile prints JavaScript to stdout.

The Sweet Part

Readable blocks:

if user.active and not user.banned
  print "Welcome {user.name}"

Functions without ceremony:

fn add a b
  return a + b

Arrays and object-like layout:

users = [
  "Tom"
  "Jerry"
  "Lucy"
]

profile =
  name: "Tom"
  age: 18

Optional chaining and modern operators:

avatar = user?.profile?.avatar
double = (x) => x * 2
for i in 1..3
  print i

value |> double |> print

Runtime Preview

Coco Runtime is optional. The language core stays separate, and runtime support is loaded only when you ask for it.

coco runtime plan examples/runtime/agent-web.json
coco runtime dev examples/runtime/agent-web.json --dry-run

The current runtime layer plans apps across engines such as:

web api agent workflow queue db ui desktop game

It does not bundle heavy adapters yet. OpenAI, Redis, PixiJS, Tauri, HTTP servers, and database drivers are intended to become optional plugins.

Use as a Library

Language core:

import { compile, parse, tokenize } from "coco-lang";

const tokens = tokenize('const name = "Coco"\n');
const ast = parse('print "hi"\n');
const js = compile('print "hi"\n');

Runtime layer:

import { CocoRuntime, parseRuntimeManifest } from "coco-lang/runtime";

Install Notes

Global install from this checkout:

bun run install:global

If coco is not found:

echo 'export PATH="$HOME/.bun/bin:$PATH"' >> ~/.zshrc
source ~/.zshrc

Commands

bun test
bun run check
bun run build
bun run test:page

Docs

Status

Implemented:

  • Lexer 1.0
  • Parser and typed AST for MVP syntax
  • JavaScript emitter/compiler
  • REPL
  • coco lex
  • coco parse
  • coco compile
  • coco run
  • coco runtime inspect/plan/dev --dry-run
  • Runtime manifest planning, event bus, scheduler, and plugin registry
  • Tests, docs, examples, and a static docs page

Supported compiler syntax:

  • Variables, constants, assignment, expressions, calls, arrays, indentation object literals, optional property access, functions, async/await, returns, if/elif/else, for in, while, break, continue, inclusive ranges with .., pipeline calls with |>, match statements with _ fallback, imports with default plus named bindings, named exports, classes, extends, new, methods, and string interpolation for simple expressions

Partial or planned:

  • Exceptions, type annotations, generics, decorators, source maps, and a type checker
  • match currently supports statement form only and requires a wildcard _ case
  • Pipeline currently supports value |> fnName and value |> fnName arg
  • Range syntax is currently inclusive: 1..3 emits values 1, 2, 3
  • Real runtime adapters

License

MIT

About

coco - coffee with chocolate, sweet for human read, caffine for AI write.

Topics

Resources

License

Contributing

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors