Skip to content
Draft

Syntax

Draft’s syntax is designed to be readable and consistent. This page covers all syntax elements.

@// This is a comment
@x = 5 @// Inline comment
@/*
This is a
multi-line comment
*/

Doc comments generate documentation:

@##
Calculates the factorial of a number.
@@param @n The number to calculate
@@return The factorial result
##
action factorial(@n: Int) -> Int
if @n <= 1
return 1
fin if.
return @n * factorial(@n - 1)
fin action.

Identifiers must start with a letter or underscore, followed by letters, digits, or underscores.

@valid_name
@_private
@count123
@CamelCase
@snake_case

Reserved keywords cannot be used as identifiers.

action and async await break const continue
else enum error false fin for fun
if in let match mut module
not or pub record return
struct super then true type var
while with yield

Statements end with a period (.) or are terminated by block keywords.

@x = 5. @// Statement with period
@greet(). @// Action call
return 42. @// Return statement

Blocks begin with a keyword and end with a matching fin keyword followed by the block type and a period.

action example()
@// Action block
fin if.
if @condition
@// If block
fin if.
for @i in @range
@// For loop block
fin for.
while @condition
@// While loop block
fin while.
@integer = 42
@float = 3.14
@string = "Hello, Draft!"
@char = 'a'
@boolean = true
@nothing = nil
@sum = @a + @b
@diff = @a - @b
@product = @a * @b
@quotient = @a / @b
@remainder = @a % @b
@equal = @a == @b
@not_equal = @a != @b
@greater = @a > @b
@less = @a < @b
@greater_equal = @a >= @b
@less_equal = @a <= @b
@and = @a and @b
@or = @a or @b
@not = not @a
@name = "World"
@greeting = "Hello, {@name}!"
@// Result: "Hello, World!"
@range = 1..10 @// Inclusive range
@exclusive = 1..<10 @// Exclusive upper bound
@length = @string#length
@first = @list#first
@value = @map#"key"
@first = @array[0]
@value = @map["key"]

Draft is not whitespace-sensitive like Python. Statements are terminated explicitly.

@// Valid
action greet() write("Hi") fin action.
@// Preferred (formatted)
action greet()
write("Hi")
fin action.

The formatter (draft fmt) handles consistent style.

From lowest to highest:

  1. or
  2. and
  3. ==, !=, <, >, <=, >=
  4. +, -
  5. *, /, %
  6. not, unary -
  7. #, [], ()

Semicolons are not used in Draft. Statements end with . or block terminators.

The last statement in a file may omit the trailing period if it’s a block terminator.

action main()
write("Hello")
fin action
@// No period needed at EOF

Learn about Variables and Constants in the next sections.