Skip to main content
ThunderLang™
Draft documentation. Syntax and behavior are illustrative and will change before v1.

The Thunder 2.0 language specification

Status: frozen for 2.x. This is Thunder™ language version 2. Every program that is valid under this specification stays valid, and means the same, in every 2.x release of ThunderLang™ (section 7). The compiler says which language it speaks: thunder --version prints language 2.

The specification has four parts:

Part Where
The grammar: how a program is written sections 1 to 4 of this page
The meaning: what a program does when it runs What Thunder means, with the tests that check each rule on every target
The diagnostics: the problems the compiler reports, by code section 6
The compatibility promise section 7

It covers Thunder Core programs (code that runs) and the mission files the compiler checks. Thunder Text, what thunder read prints for code in other languages, is an explanation, not a program, and is not frozen: it gets better from release to release (Specification V1 describes it). The reference with more examples is Thunder Core.

1. Files and projects#

  • A Thunder source file ends in .thunder. Its kind is decided by a fixed rule: inside a project (a folder with thunder.toml, or below one) or with --core, it is a program. Otherwise its first line that is not blank or a comment decides: a mission keyword in lowercase, with at most a name after it (mission Checkout), makes a mission; any other line makes a program; a file that could be either (an empty file) is a mission.

  • A project is a folder with thunder.toml. Programs are in src/, tests in tests/; a file in tests/ sees every name its project's src/ defines.

  • The language version is written in thunder.toml:

    thunder.toml
    [project]
    name = "shop"
    language = "2"

    thunder new writes it. A project without it is read as the compiler's own language. A project that names a newer major version is refused with a message that says to install a newer ThunderLang; it is never read as something else. A file outside a project is read as the compiler's own language.

2. Lines, words, and names#

  • A program is a list of lines. A line that ends with : opens a block: the lines under it are indented further, with spaces (tabs are TC101). A line can continue a condition on the next line when that line is indented further and starts with and or or.
  • # starts a comment that runs to the end of the line, outside text.
  • Tokens: words (letters, digits, _; yes/no is one word), the possessive 's, whole numbers (42, 1_000), decimals (3.5), money ($1.50), percents (7%), text in double quotes, and the symbols + - * / ( ) [ ] , : . = == != <> < <= > >= ?.
  • Text is written in double quotes (or “curly quotes”). {…} inside text puts a value in; {{ and }} are a brace. \n, \t, \r, \\, and \" are escapes.
  • Keywords are not case-sensitive (Set and set are the same). Names are not case-sensitive either, and a name can be several words: tax rate, decide refund. In an expression, a run of words is matched against the names the program knows, longest first.
  • a, an, and the at the start of a name are skipped (the total is total). These words belong to Thunder and cannot be part of a name: is are and or not to at of with from where by in as than the a an plus minus times divided mod true false yes no nothing otherwise sorted descending ascending equals contains between joined split replacing starts ends.

3. Grammar#

The notation: "Set" is a keyword (any case), name is a name (one or more words), expr an expression, kind a kind of value, [ … ] is optional, { … } repeats zero or more times, | separates choices, and INDENT … DEDENT is a block of lines indented under the line above.

3.1 A program#

Output
program      = { top-line }
top-line     = function | record | data-type | rule | use | keep | store | test | screen | endpoint | statement

Functions, records, data types, rules, Use, Keep, Store, tests, screens, and endpoints are written at the left edge (TC402 otherwise). Everything else is a statement and may be at the left edge (the main program) or in a block.

3.2 Definitions#

Output
function     = "To" name ":" INDENT { receive } block-lines DEDENT
receive      = "Receive" input { ( "," | "and" ) input }
input        = name [ "as" kind ] [ "(" expr "if not given" ")" ]

record       = ( "A" | "An" ) name "has:" INDENT { field | rule } DEDENT
field        = name ( "is" | "are" ) kind

data-type    = ( "A" | "An" ) name "is one of:" INDENT { alternative } DEDENT
alternative  = name [ "with" name "as" kind { ( "," | "and" ) name "as" kind } ]

rule         = "Never allow" condition
use          = "Use" module-name [ "as" name ]
             | "Use" text "from" ( "npm" | ".NET" | "NuGet" ) [ "as" name ]
keep         = "Keep" name "at" expr
store        = "Store" name "as a list of" name
test         = "Test" text ":" INDENT { expect | statement } DEDENT
expect       = "Expect" expr "to be" [ comparison-words ] expr
             | "Expect" expr "to fail" [ "with" expr ]
             | "Expect" condition

A default ((… if not given)) is a fixed value, and inputs with a default come after the inputs without one (TC404).

3.3 Statements#

Output
statement    = "say" expr | "print" expr
             | "Set" target "to" expr [ ":" INDENT { name ":" expr } DEDENT ]
             | "Increase" target "by" expr | "Decrease" target "by" expr
             | "Add" expr "to" target | "Remove" expr "from" target
             | "Sort" target [ "by" name [ "descending" | "ascending" ] ] | "Reverse" target
             | "Give" [ "back" ] expr [ "with status" expr ]
             | "Stop with" expr [ "and status" expr ]
             | choice | depending | for-each | repeat
             | statement NEWLINE "If this fails:" INDENT block-lines DEDENT
target       = name | expr "'s" name | expr "at" expr

choice       = ( "When" | "If" ) condition ":" INDENT block-lines DEDENT
               { "Otherwise" ( "when" | "if" ) condition ":" INDENT block-lines DEDENT }
               [ "Otherwise:" INDENT block-lines DEDENT ]
depending    = "Depending on" expr ":" INDENT { name ":" INDENT block-lines DEDENT }
               [ "Otherwise:" INDENT block-lines DEDENT ] DEDENT
for-each     = "For each" name [ "and" name ] "in" expr ":" INDENT block-lines DEDENT
             | "For each" name "from" expr "to" expr ":" INDENT block-lines DEDENT
repeat       = "Repeat" ( "while" condition | "until" condition | expr "times" ) ":" INDENT block-lines DEDENT

Give … with status and Stop … and status are only for endpoints (TC218). Depending on must name every alternative of the data type, or have Otherwise: (TC215).

3.4 Expressions#

From the loosest to the tightest binding:

Output
condition    = expr
expr         = or-expr
or-expr      = and-expr { "or" and-expr }
and-expr     = not-expr { "and" not-expr }
not-expr     = [ "not" ] compare
compare      = sum [ comparison ]
comparison   = "is" [ "not" ] sum | "is at least" sum | "is at most" sum | "is more than" sum | "is less than" sum
             | "is between" sum "and" sum | "is in" sum | "contains" sum | "does not contain" sum
             | "is" ( "empty" | "positive" | "negative" | "zero" | "nothing" ) | "is" [ "not" ] alternative-name
             | ( "==" | "!=" | "<>" | "<" | "<=" | ">" | ">=" ) sum
sum          = product { ( "+" | "-" | "plus" | "minus" ) product }
product      = unary { ( "*" | "/" | "times" | "divided by" | "mod" ) unary }
unary        = [ "-" ] postfix
postfix      = primary { "'s" name | "at" expr [ "otherwise" expr ] | "as" kind
             | "where" condition | "sorted" [ "by" name ] [ "descending" | "ascending" ]
             | "joined with" expr | "split at" expr | "replacing" expr "with" expr
             | "starts with" expr | "ends with" expr }
primary      = number | money | percent [ "of" unary ] | text | "true" | "false" | "yes" | "no" | "nothing"
             | "[" [ expr { "," expr } ] "]" | ( "an empty list" | "an empty map" )
             | "date" text | "today"
             | name                                     (a value, or a function called with no inputs)
             | name "with" expr { "and" expr }          (a call)
             | name "(" [ expr { "," expr } ] ")"       (a call)
             | ( "a" | "an" ) [ "new" ] name [ "with" name expr { "and" name expr } ]      (a record)
             | ( "a" | "an" ) alternative-name name [ "with" name expr { "and" name expr } ] (an alternative)
             | "the" op "of" unary
             | name "from" unary                        (a field of each item)
             | "round" unary [ "to" expr "places" ]
             | "read file" unary
             | "the answer from" method text [ "with" expr ] [ "as" kind ]
             | "?" [ kind ]                             (a hole: the program is a draft and does not run)
             | "(" expr ")"
op           = "sum" | "total" | "average" | "count" | "smallest" | "largest" | "length" | "first" | "last" | "words"
             | "lines" | "parts" | "lowercase" | "uppercase" | "trimmed" | "keys" | "values" | "absolute value"
             | "JSON data in" | "JSON text"
method       = "GET" | "POST" | "PUT" | "PATCH" | "DELETE"

3.5 Kinds of value#

Output
kind         = "number" | "whole number" | "text" | "yes/no" | "yes or no" | "money" | "date"
             | "list" [ "of" kind ] | "map" [ "of" kind "to" kind ] | "optional" kind | name

A name kind is a record or data type the program defines. A plural (list of Orders) names the same kind.

3.6 Screens and endpoints#

Output
screen       = "Screen" name ":" INDENT { screen-input | remember } { show | choice } DEDENT
screen-input = "Receive" input { ( "," | "and" ) input }
remember     = "Remember" name [ "as" kind ] ( "," | "" ) "starting at" expr
show         = "Show" expr
             | "Show a heading" ( text | ":" expr )
             | "Show a list with each" name "in" expr ":" INDENT { show | choice } DEDENT
             | "Show a row:" INDENT { show | choice } DEDENT
             | "Show a" ( "text box" | "number box" ) text "for" name
             | "Show a button" text [ "(off when" condition ")" ] ":" INDENT "When pressed:" INDENT block-lines DEDENT DEDENT
             | "Show a button" text "that opens" name
             | "Show" name ":" INDENT { name ":" expr } DEDENT

endpoint     = "When someone calls" method path ":" INDENT { receive } block-lines DEDENT
path         = "/" { path-part | "{" name "}" }

A program with screens is built for React (TC406). Endpoints and calls to other services run on Python, the Thunder engine, and TypeScript (TC411 elsewhere); stored lists on those and Go (TC410 elsewhere).

3.7 Missions#

Mission files keep the syntax in the syntax overview and spec v0.2: a mission line, then blocks (goal, input, output, guarantee, never, decision, test, lifecycle, and the others listed there). Every mission that 2.0 accepts is accepted by every 2.x compiler.

4. Examples#

Each example on this page is a complete program; a test compiles every one with the 2.0 compiler.

Thunder
A Payment is one of:
  Successful with amount as money
  Declined with reason as text
  Pending

Keep review limit at $100

To describe:
  Receive payment as Payment
  Depending on payment:
    Successful:
      When payment's amount is at least review limit:
        Give "paid {payment's amount}, to review"
      Give "paid {payment's amount}"
    Declined:
      Give "declined: {payment's reason}"
    Pending:
      Give "waiting"

say describe with a successful Payment with amount $120

Test "describe says what happened":
  Expect describe with a pending Payment to be "waiting"
  Expect describe with a declined Payment with reason "no funds" to be "declined: no funds"
Thunder
An Account has:
  Owner is text
  Balance is money
  Never allow a negative balance

Set ada to an Account with owner "Ada" and balance $10
Set ada's balance to ada's balance - $50
If this fails:
  say "Not allowed: {error}"

Set totals to an empty map
For each word in the words of "the cat and the hat":
  Set totals at word to (totals at word otherwise 0) + 1
say totals at "the"

For each n from 1 to 3:
  When n is 2:
    say "two"
  Otherwise:
    say n

5. Meaning#

What a valid program does is specified in What Thunder means, topic by topic: decimals, division, rounding, text, nothing, changing values and Never rules, errors, large numbers and money, and effects. Each rule names the tests that run it on Python, the Thunder engine, TypeScript, C#, Rust, Go, and Java and require the same result from all of them. A rule written there is part of this specification; the list of what no cross-target test covers yet is part of it too.

6. Diagnostics#

Every problem the compiler reports has a code. The codes and what each one means are frozen: in 2.x a code is never removed, renamed, or reused for something else. New codes may be added. The wording of a message, its hint, and the suggestions in it are not frozen: they get clearer from release to release. A test checks that this table lists exactly the codes the compiler has (CODES in compiler/src/core/diagnostics.mjs).

Code Meaning
TC101 Tabs are not used for indentation
TC102 A line is indented but nothing opens a block above it
TC103 A line ends with ":" but has nothing indented under it
TC104 Indentation does not line up with an earlier line
TC105 A line could not be read
TC106 Text is missing its closing quote
TC107 Brackets or parentheses do not match
TC108 Something is missing from this statement
TC109 "Otherwise" without a "When" or "If" before it
TC110 "If this fails:" without a statement before it
TC111 "Receive" is not the first line of a "To" block
TC112 Something extra at the end of a line
TC113 A line under a record or call written as a block is not "name: value"
TC114 A field or an input is given twice
TC115 A function called with names has no input of that name (or its inputs are not known)
TC116 A function called with names is missing an input it needs
TC117 Lines like "name: value" with no record or function call to give them to
TC118 A line inside a Screen is not something a screen can show
TC119 "Receive" or "Remember" is not at the top of a Screen
TC120 A hole (?, ?number) in a draft: the program does not run until it is filled in
TC201 Unknown name
TC202 A name uses a word that belongs to Thunder
TC203 A name is defined twice
TC204 A function uses a value from the main program
TC205 A kept value is changed
TC206 Unknown field
TC207 Wrong number of values given to a function
TC208 "Give" outside a "To" block
TC209 Unknown record
TC210 A record is missing a field
TC211 A name is used before it is set
TC212 Unknown screen
TC213 An input of a screen is changed
TC214 A screen is given an input it does not have, or is missing one it needs
TC215 "Depending on" leaves out an alternative of the data type (and has no "Otherwise")
TC216 A field only some alternatives have, read where the alternative is not known
TC217 An alternative a data type does not have, or a value built without saying which alternative it is
TC218 An HTTP status (Give … with status, Stop … and status) outside an endpoint
TC219 Inside where or sorted by, a name that is both a field of each item and a value of yours (the field wins, so your value is hidden)
TC301 Money and a plain number are mixed
TC302 Values of different kinds are compared
TC303 A name changes kind
TC304 A condition is not yes/no
TC305 This kind of value cannot do that
TC306 Unknown kind of value
TC401 Thunder Core does not know this action yet
TC402 Thunder Core does not support this yet
TC403 A "Never" rule names more than one changing value
TC404 A default for an input must be a fixed value, after the inputs without one
TC405 A Python module ("Use") can be used only when compiling to Python
TC406 A Screen is built only for React (--target react)
TC407 A step that cannot run when a button is pressed
TC408 An npm package ("Use … from npm") can be used only when compiling to TypeScript
TC409 A .NET library ("Use … from .NET" or "from NuGet") can be used only when compiling to C#
TC410 A stored list ("Store … as a list of …") runs on Python, TypeScript, Go, and the Thunder engine only, for now
TC411 An endpoint or a call to another service in a program compiled to a target without HTTP yet
TC501 Divided by zero (runtime)
TC502 Nothing used where a value was needed (runtime)
TC503 A field or entry is missing (runtime)
TC504 A Never rule was broken (runtime)
TC505 An Expect check failed (runtime)
TC506 Values of different kinds were mixed (runtime)
TC507 The program stopped with a message (runtime)
TC508 Library code used through "Use" failed (runtime)
TC509 A value could not be converted (runtime)
TC510 A call to another service failed: it could not be reached, or it answered with a problem (runtime)

TC1xx are problems reading a line, TC2xx problems with names, TC3xx problems with kinds, TC4xx things a target or this version does not do, TC5xx problems while running. A draft (holes, TC120, and nothing else) never runs or builds.

7. The compatibility promise#

From 2.0.0 on:

  1. A valid 2.0 program stays valid in every 2.x release, and means the same. It gives the same output and the same errors (by code) on every target that runs it. A release may make a program that was refused valid (a new feature, a new target), never the other way round.
  2. A fix to a bug in a meaning rule is not a new meaning. When an engine or target differs from What Thunder means, the engine is fixed, not the rule; the fix is listed in the release notes.
  3. Diagnostic codes are frozen (section 6); messages may get clearer.
  4. A change that would break programs waits for language 3. Before it ships, the compiler warns about the old form for at least one minor release, and thunder upgrade rewrites programs to the new form, checked by running them before and after on every engine.
  5. What is not covered: what thunder read prints (Thunder Text), the wording of messages, the shape of the generated Python, TypeScript, C#, Go, Rust, Java, and React code, and command output. Data formats other tools read carry their own version (thunder-examples-v1, thunder-template-v1, intent-proof-v1, evidence-event-v1, and the others) and change only by adding a new version.

The policy and the version field are new in 2.0; earlier versions (0.x and 1.x) changed freely, and their programs are read as language 2.