Kry Source Language
Language Reference
Kry is C-close application code for Kryon. This page mirrors the canonical source contract in docs/KRY_LANGUAGE_SPEC.md: source lowers into KIR first, then the same KIR can produce readable C, Go bindings, browser-loadable JS, or portable KRB cartridges.
.kry -> KIR -> C
.kry -> KIR -> Go
.kry -> KIR -> JS
.kry -> KIR -> KRB
Contract
What Kry Guarantees
| Rule | Meaning |
|---|---|
| Single frontend | .kry parses into KIR before every backend. Backend-specific behavior must be visible in KIR or reported as a diagnostic. |
| C-close expressions | Types, pointer expressions, calls, field access, and raw C escape lines stay familiar to C programmers. |
| Declarative UI shape | #ui functions declare named node hierarchies; the runtime owns reconciliation, layout, input routing, focus, and update order. |
| Inspectable lowering | k2ir prints imports, state, functions, statements, assertions, and simple structured expression trees for tooling. |
| Honest subsets | C is the full path. Go, JS, and KRB accept supported KIR and reject unsupported compile-time assertions instead of silently changing semantics. |
Declarations
Top-Level Forms
#import "kryon.h"
ui :: #import "src/ui/panel"
WEB :: #defined(PLATFORM_WEB)
ANSWER :: #run 6 * 7
#assert ANSWER == 42, "compile-time math failed"
state {
click_count: int = 0
}
Counter :: (viewport: Rectangle, app: App*) #ui {
Screen root: {
bounds = viewport
Column body: {
gap = 8
Text("Count", 0, 0, Text16, GetThemeText())
if Button((ButtonProps){.label = "Increment"}) {
app->click_count += 1
}
}
}
}
| Form | Purpose |
|---|---|
#import "header.h" | Include a C header in generated native code. |
name :: #import "module" | Import another Kry module. |
name :: (...) -> Ret { ... } | Define a function. Module functions are source-prefixed in generated C unless exported. |
name :: (...) #ui { ... } | Define a UI hierarchy function. Named nodes inside it get stable hierarchy paths. |
#export, #private, #extern | Control ABI visibility and declarations that are implemented outside Kry. |
#global | Create a file-level variable; combine with #export for public extern storage. |
Type :: struct { ... } | Define a C-compatible struct type. |
state { ... } | Declare persistent app state fields owned by the generated module. |
Statements
Function Body Forms
- Locals:
name := expr,name: Type = expr, orname: Type. - Assignments:
name = exprand compound operators such as+=,%=, and&=. - UI nodes:
Screen root: { ... },Column body: { ... },Row actions: { ... }, andStack layer: { ... }declare hierarchy nodes. - Calls: plain call statements remain available for leaf widgets and utility calls inside UI or normal functions.
- Control flow:
if,else if,else,while,for,switch,case,default, labels,goto,break,continue,guard,defer, andreturn. - Interop:
c lineemits raw C glue, andunused exprsilences unused-value warnings.
Compile Time
Constants, Guards, And Assertions
| Feature | Status | Backend Behavior |
|---|---|---|
#defined(SYMBOL) | Available | Expands to a C preprocessor-style condition for guards and generated C. |
NAME :: #run INTEGER_EXPR | Available | Evaluated by the Kry frontend as a small integer constant. It is not a general compile-time function system. |
#assert CONDITION, "message" | Available | Ungarded constant-false assertions fail during Kry parsing. C emits #if/#error. Go, JS, and KRB require known true assertions. |
#if, #else, #endif | Available | Retained in KIR as expanded guards so backends know the active platform conditions. |
KIR
Intermediate Representation
KIR keeps raw source statement text for backend compatibility and adds structured metadata where the frontend can parse safely. Today that metadata covers simple identifiers, integer literals, string literals, calls, and binary expressions. More expression kinds can be added without changing the source pipeline.
k2ir --root . -o build/ir src/app.kry
k2c --root . -o build/c src/app.kry
k2g --root . -o build/go src/app.kry
k2js --root . -o build/js src/app.kry
k2b --root . -o build/krb src/app.kry
Limits
Known Language Boundaries
- No hidden macro language: Kry keeps compile-time behavior small and explicit.
#runcurrently folds integer expressions only. - Backend subsets are real: C is broadest; Go, JS, and KRB intentionally support smaller surfaces and must diagnose unsupported KIR.
- Text system limits remain runtime limits: font fallback exists, but shaping, bidi, RTL mirroring, and IME preedit are not complete.
- KRB is portable by design: it carries a smaller widget and capability surface than native C.
Backend Contract
Conformance Summary
| Feature | KIR | C | Go | JS | KRB |
|---|---|---|---|---|---|
#run integer constants | Yes | Yes | Yes | Yes | Yes |
#assert | Yes | #if/#error | Known true only | Known true only | Known true only |
| State block | Yes | Yes | Yes | Yes | Yes |
| Raw C lines | Preserved | Yes | No | No | No |
| Structured expression metadata | Partial | Metadata | Metadata | Metadata | Metadata |