Recoil logo

Recoil

Recoil by Example

  • Home
  • Getting Started
  • Guide
  • Reference
  • PARSE
  • State Machines
  • FFI & Systems
  • Ports
  • Packages
  • f00 & Embedded
  • Examples
  • Internals

On this page

Loading…

A tour of the language

Each snippet below uses syntax that matches the current compiler and passing tests. Bindings are implicitly typed; make appears only where a specific type or container is required.

1 · Basic values

a: 10
b: 20
print a + b

dbl: 3.14159
print dbl

flag: true
ch: #"A"
print ch

2 · Functions and implicit return

The last expression in a body is the result unless an earlier return exits.

add: func [a [i32!] b [i32!] return: [i32!]] [
    a + b
]

print add 7 8

3 · if, either, block-valued branches

x: make i32! if true [1]
print x

choose: func [flag [logic!] return: [i32!]] [
    either flag [
        a: 10
        a + 2
    ] [
        b: 20
        b + 3
    ]
]

print choose true
print choose false

4 · Short-circuit boolean expressions

mark: func [return: [logic!]] [
    print 9
    return 1 = 1
]

first: make logic! all [0 = 1  mark]
second: make logic! any [0 = 1  mark]
print first
print second

5 · Structs, vectors & indexed paths

Field access uses /field; variable indexing uses /:idx.

voice!: make struct! [phase: f64! active: logic!]

voices: make [#mutable vector! [voice! 2]] []
idx: 1

voices/:idx/phase: 0.5
voices/:idx/active: true

print voices/:idx/phase
print voices/:idx/active

6 · Strings, blocks, dicts & slices

buf: make [#flexible [string! 16]] none
append buf "hello"
append buf ", world"
print freeze buf

nums: make [#flexible [array! [i64!] 8]] none
append nums 10
append nums 20
print length? freeze nums

counts: make [#mutable dict! [string! i32!]] [string! i32!]
get-or-put counts "ok" 1
print pick counts "ok"

7 · Borrowing

greet: func [:name [string!] return: [none!]] [
    print name
]

msg: make string! "hello"
greet :msg
print msg

8 · fn-ptr! & closures

add: func [x [i32!] y [i32!] return: [i32!]] [
    return x + y
]

callback: make fn-ptr! add
print callback 3 4

base: 100
bump: make fn-ptr! func [#capture [base] a [i32!] return: [i32!]] [
    a + base
]
print bump 7

9 · parse

print parse "abcd" ["ab" "cd"]
print parse "cd" ["ab" | "cd"]

chs: make bitset! charset ":/#?"
print parse "abc?" [thru chs]
print parse "a" [ahead "a" "a" end]

More on the PARSE page.

10 · go

print "main"
go [
    print "task"
]

11 · fsm! and send

TrafficLight!: make fsm! [
    events: [go ["Go"] slow ["Slow"] stop ["Stop"]]
    red    [go   => green]
    green  [slow => yellow]
    yellow [stop => red]
]

light: make [#mutable TrafficLight!] TrafficLight!/red
send light go []
send light slow []

More on the State Machines page.

11b · fst!, transduce/lazy & take

Token!: make sum! [
    ident
    num [value: i32!]
]

Lexer!: make fst! [
    emits: Token!
    fields: [count: i32!]
    scanning [
        ["A"] => scanning [count: count + 1  emit Token!/num count]
    ]
    finished []
]

drive: func [] [
    lex: transduce/lazy Lexer! "AAA"
    result: take lex
    match result [
        ok [v]       [print 1]
        need-more [] [print 8]
        done []      [print 9]
        fail [err]   [print 7]
    ]
]

12 · Ports & file I/O

fp: make port! file/open %core.reb
either none? fp [print 0] [print 1]

Borrowed and consuming operations follow the port! model: file/read :fp, file/write :fp "text", file/close fp.

13 · FFI

math: foreign <stdlib.h> [
    abs: "abs" [i32! return: i32!]
]

x: math/abs -7
print x

More on the FFI & Systems page.

14 · Modules

Recoil [
    Type: module
    Module: tests/math-module-demo
    Imports: [
        std/math/core [abs-value clamp-value]
    ]
    Exports: []
]

print abs-value [i32!] -7
print clamp-value [i32!] 10 0 5

15 · Parse + FSM integration

parse "GET /path" [
    "GET" (send conn got-method [])
    " "
    thru end
]

either state? conn Conn!/reading-body [
    parse "payload" [thru end (send conn got-body [])]
] [
    print "still waiting for method"
]

More from the test suite

Browse a selection of complete programs in the repository:

↑

Recoil — an ownership-first, statically-typed language with a static borrow checker.

codeberg.org/rebolek/recoil · boleslav@brezovsky.eu · Apache-2.0