Your first program

Save this as hello.md:

snippet-1.md
---
midad: "0.1"
module: hello
---

# Hello

A function lives in a `fn` block. The prose around it is documentation, and it
travels with the code.

```fn
function greet(name: string): string {
  return "hello, " + name + "!";
}
```

```exec
print(greet("world"));
```
output (checked by the build)
hello, world!

Run it:

$ midad run hello.md
hello, world!

That is the whole idea. The file is a Markdown document you can read, and it is also the program.

The parts of the file

Frontmatter. Every midad program opens with YAML frontmatter that has a midad: key. The value is the language version, and "0.1" is the only version there is. module: names this file, which matters when another file imports it. A Markdown file without that key is never executed: midad check says so with E0001 and stops.

Prose. Headings, paragraphs, tables and code fences that are not one of the block kinds are documentation. The runtime skips them. You can write as much as you like around the code.

Blocks. These fence tags are code:

fence what it is
exec top-level statements, run by midad run
fn functions
state program state, optionally persisted
route:METHOD /path an HTTP route
command:NAME a CLI subcommand
html:NAME an HTML template
types type aliases and interfaces, recorded for tooling
native:NAME a foreign function behind the ffi permission
ai:intent an intent with a declared fallback

Check it without running it

$ midad check hello.md
hello.md: ok

midad check type-checks the file, lints it and audits the permissions it asks for. It runs nothing. When something is wrong it prints the file, the line and column in the Markdown source, the code, and a fix:

check-2.md
---
midad: "0.1"
module: broken
---

```exec
print(1 + "two");
```
midad check
× E0230: operator cannot combine number and string

Every diagnostic has a code, and `midad explain` tells you what one means:

```console
$ midad explain E0230
E0230 (error)
  type mismatch: expected {}, found {}
  fix: write the literal with a decimal point, e.g. `1.0`

The page for every code is in the diagnostics reference.

Format it

midad fmt rewrites the code inside the blocks and leaves the prose alone. --check reports whether anything would change without writing, and --stdout prints the result instead of overwriting the file.

$ midad fmt hello.md --check
$ midad fmt hello.md

Give it a command

A command: block is a subcommand. args is the list of words after the command name:

snippet-3.md
---
midad: "0.1"
module: greet
---

# greet

```command:hello
print("hello " + join(args, " "));
```
output (checked by the build)
hello world

midad run greet.md hello world runs that block and nothing else. The exec block, if there were one, does not run. An unknown command name is an error on stderr and exit code 2.

Build it

midad build has three shapes:

$ midad build hello.md -o hello                 # interpreter + source in one file
$ midad build hello.md --native greet -o hello  # compile one function to machine code
$ midad build hello.md --native-all -o hello    # compile the whole program

The first is a bundle: the interpreter and your source in one executable, so it runs anywhere without the .md file next to it. The other two emit real machine code through a C compiler or through Cranelift, and refuse by name anything they cannot compile rather than guessing. midad build keeps the permission checks: the grants are baked into the binary, and a program that computes a path at run time is refused instead of being handed a wildcard it did not ask for.

Details for each form are in midad build.

Serve it

Add a route: block and the same file serves HTTP:

serve-4.md
---
midad: "0.1"
module: notes
permissions:
  listen: ["127.0.0.1:18787"]
---

# Notes

```route:GET /health
return respond(200, {"ok": true});
```
requests and responses
GET /health -> 200 {"ok":true}

midad run notes.md prints the address it listens on and stays up. Routes, request maps and responses are in Routes and serving.

On this page