Simple arithmetic
Use the Fift interpreter as a calculator with reverse Polish notation:17 * 17 = 289
6 * 289 = 1734
1734 + 289 = 2023
Standard output
emit
prints the Unicode character corresponding to the number on top of the stack."..."
outputs a constant string
Defining functions (Fift words)
To define a word, follow these steps:- Enclose the word’s effects in curly braces
{}
. - Add a colon
:
after the closing brace. - Specify the word’s name after the colon.
increment
that increases x
by 1
.
Examples:
:
. They differ in behavior:
- Active words – Operate inside curly braces
{}
. - Prefix words – Do not require a trailing space .
Conditional execution
Execute code blocks conditionally usingcond
:
Loops
Use loop primitives for repetitive operations:times
takes two arguments - let’s call them cont
and n
- and executes cont
n
times.
Here list-delete-first
takes continuation of safe-cdr
(command deleting head from Lisp-style list), places it under c
and then c
times removes head from list present on stack.
while
/until
provide conditional looping.
Comments
Comments in Fift are defined inFift.fif
and come in two forms:
- Single-line comments: Start with
//
and continue to the end of the line - Multiline comments: Start with
/*
and end with*/
How comments work
Fift programs are sequences of words that transform the stack or define new words. Comments must work even during word definitions, requiring them to be active words (defined with::
).
Breaking down the //
definition:
0
- Pushes zero onto the stackword
- Reads characters until reaching one matching the top stack value (zero is special - skips leading spaces then reads to end of line)drop
- Removes the comment text from the stack0
- Pushes zero again (number of results for::
definition)'nop
- Pushes an execution token that does nothing (equivalent to{ nop }
)
Using Fift for defining TVM assembly codes
How @Defop works
@Defop
checks available space for the opcode using @havebitrefs
. If space is insufficient, it writes to another builder via @|
(implicit jump).
Important: Always use x{A988} @addop
instead of x{A988} s,
to avoid compilation failures when space is limited.
Including cells in contracts
You can embed large bag-of-cells into contracts:- Writes
x{88}
(PUSHREF
) when included in the program - Adds a reference to the specified bag-of-cells
- Pushes the cell to TVM stack when executing
LDBLOB
Special features
Ed25519 cryptography
Fift provides built-in support for Ed25519 cryptographic operations:newkeypair
- Generates a private-public key pairpriv>pub
- Derives a public key from a private keyed25519_sign[_uint]
- Creates a signature for given data using a private keyed25519_chksign
- Verifies an Ed25519 signature
TVM interaction
runvmcode
and similar commands - Executes TVM with a code slice taken from the stack
File operations
-
Save BoC to file:
Continue learning
- Fift: A Brief Introduction - Nikolai Durov