Reference Guide

Syntax Catalog

Complete syntax reference for Clean Language

Basics

Variables

integer x = 42
number pi = 3.14
string name = "Clean"
boolean active = true

Declare typed variables with initial values.

Print

print "Hello, World!"
print("With parentheses")

Output text to the console. Parentheses are optional.

Comments

// This is a comment

Single-line comments start with //.

Input

string name = input("Enter your name: ")
integer age = input.integer("Enter your age: ")
boolean confirm = input.yesNo("Continue?")

Read input from the user with type-safe methods.

Functions

Function Declaration

functions:
	integer add(integer a, integer b)
		return a + b

Declare functions in a functions: block with return type first.

Function Calls

integer result = add(3, 4)
print result.toString()

Call functions by name with arguments.

Control Flow

If/Else

if x > 0
	print "positive"
else
	print "not positive"

Conditional branching without parentheses.

While Loop

while i < 10
	print i.toString()
	i = i + 1

Repeat while condition is true, no parentheses.

Iterate Range

iterate i in 0 to 10
	print i.toString()

Loop over a numeric range using 'in' keyword.

Iterate Array

iterate item in myArray
	print item.toString()

Loop over array elements using 'in' keyword.

Types & Classes

Class

class Person
	string name
	integer age
	constructor(string n, integer a)
		name = n
		age = a

Define classes with fields directly under the class declaration. No 'this' keyword - use different parameter names.

Inheritance

class Dog is Animal
	string breed
	constructor(string n, string b)
		base(n)
		breed = b

Extend classes with 'is' keyword and base() constructor calls.

Arrays

array<integer> nums = [1, 2, 3, 4, 5]
nums.push(6)
integer len = nums.length()

Typed arrays with push, length, and indexing. Use lowercase array<T>.

Operators

Arithmetic

integer sum = a + b
integer diff = a - b
integer prod = a * b
integer quot = a / b
integer rem = a % b
number power = x ^ y

Basic arithmetic operators: +, -, *, /, %, ^

Comparison

boolean eq = a == b
boolean neq = a != b
boolean lt = a < b
boolean gt = a > b
boolean lte = a <= b
boolean gte = a >= b

Comparison operators: ==, !=, <, >, <=, >=

Logical

boolean both = a and b
boolean either = a or b
boolean opposite = not a
boolean check = x is SomeType

Logical operators: and, or, not, is

String Operations

String Interpolation

string greeting = "Hello, {name}!"
string result = "Sum: {a + b}"

Embed expressions in strings using curly braces.

Methods

string s = "hello world"
s.length()
s.toUpperCase()
s.substring(0, 5)
s.contains("world")
s.replace("world", "Clean")
s.split(" ")
s.trim()

Built-in string manipulation methods.

Error Handling

Require

functions:
	integer divide(integer a, integer b)
		require b != 0
		return a / b

Precondition validation with require.

Rules

state:
	integer balance = 0
	integer limit = 100

	rules:
		balance >= 0
		balance <= limit

Multiple validation rules.

Async Programming

Later (Await)

later data = start fetchData()
print data

Wait for async function result with 'later' and 'start'.

Background (Fire-and-forget)

background logAction()

Execute async functions without waiting for results.

Modules & Imports

Import Block

import:
	math
	utils

Import modules at the start of a file.

Private Functions

functions:
	void helperFunction()
		// private: only this file can call it
		print("internal work")

	public:
		void run()
			helperFunction()

Functions are private by default. Only those under public: are visible to other files.

Testing

Tests Block

tests:
	"add returns sum": add(2, 3) = 5
	"divide by zero falls back": divide(10, 0) onError -1 = -1

Define inline tests with expected values.

Apply Blocks

Batch Operations

integer:
	x = 10
	y = 20

print:
	"Hello"
	"World"

constant:
	number PI = 3.14159
	integer MAX_SIZE = 100

Apply type or function to multiple statements in a block.

AI Integration

Intent

functions:
	void processPayment(number amount, string method)
		intent "Process a payment transaction"
		// function body

Describe function purpose in natural language for AI tools.

Spec

functions:
	number calculateDiscount(number price, number pct)
		spec "specs/pricing/discount.spec.cln"
		return price * (pct / 100)

Link functions to specification documents for traceability.

Source Block

source:
	spec: "specs/payment.spec.cln"
	version: "a3f2c1d"

Mark files as generated from specifications with version tracking.

Copied!