Skip to content

Syntax Reference

rfi-irfos edited this page Apr 14, 2026 · 2 revisions

Syntax Reference

This page defines the formal syntax structure of Ternlang.

For the formal grammar specification, see:

https://github.com/eriirfos-eng/ternary-intelligence-stack/blob/main/ternlang-root/spec/grammar.ebnf


Lexical Structure

Ternlang source files use the .tern extension.

Example:

program.tern

Source code is parsed into:

  • tokens
  • AST nodes
  • BET bytecode instructions

Comments

Single-line comments:

// this is a comment

Use comments for:

  • logic documentation
  • model assumptions
  • safety gates
  • experimental notes

Identifiers

Identifiers are used for:

  • variables
  • functions
  • modules
  • namespaces

Example:

let signal: trit = affirm;

Valid examples:

my_var
safety_gate
expert13
consensus_score

Primitive Types

Trit

Primary scalar type:

trit

Possible values:

reject
tend
affirm

Variable Declaration

Syntax:

let name: type = value;

Example:

let decision: trit = affirm;

Multiple declarations:

let a: trit = reject;
let b: trit = tend;
let c: trit = affirm;

Function Definition

Syntax:

fn name(param: type) -> return_type {
    ...
}

Example:

fn classify(signal: trit) -> trit {
    return signal;
}

Multiple parameters:

fn combine(a: trit, b: trit) -> trit {
    return consensus(a, b);
}

Return Statement

Syntax:

return value;

Example:

return affirm;

Match Expression

Primary control structure for ternary logic.

Syntax:

match variable {
    reject => { ... }
    tend   => { ... }
    affirm => { ... }
}

Example:

match signal {
    reject => { return reject; }
    tend   => { return tend;   }
    affirm => { return affirm; }
}

All three arms must be present.

Non-exhaustive matches are compiler errors. :contentReference[oaicite:1]{index=1}


Conditional Flow

Binary compatibility syntax is supported.

if score > 0 {
    return affirm;
}

Used for:

  • translated Python logic
  • SQL rule migration
  • threshold classifiers

Block Syntax

Blocks use braces:

{
    ...
}

Example:

fn gate(x: trit) -> trit {
    return x;
}

Operators

Assignment

=

Example:

let x: trit = affirm;

Comparison

==
!=
<
>
<=
>=

Example:

if score >= 0 {
    return affirm;
}

Logical Operators

&&
||

Example:

if valid && safe {
    return affirm;
}

Function Calls

Syntax:

name(arg1, arg2)

Example:

consensus(affirm, tend)

Module Imports

Syntax:

use std::module;

Example:

use std::math;
use nn::layers;

The resolver is built into the compiler frontend.

No runtime import I/O is required.


Array / Tensor Indexing

Syntax:

tensor[row, col]

Example:

weights[2, 4]

Used for:

  • tensors
  • matrices
  • sparse weights
  • feature maps

Sparse Directive

The inference runtime supports sparsity directives.

Syntax:

@sparseskip

Example:

@sparseskip
fn sparse_layer(x: trit) -> trit {
    return x;
}

This is used for sparse ternary inference optimization.


Minimal Valid Program

fn main(signal: trit) -> trit {
    match signal {
        reject => { return reject; }
        tend   => { return tend;   }
        affirm => { return affirm; }
    }
}

Formal Grammar

Full EBNF grammar:

https://github.com/eriirfos-eng/ternary-intelligence-stack/blob/main/ternlang-root/spec/grammar.ebnf

Specification directory:

https://github.com/eriirfos-eng/ternary-intelligence-stack/tree/main/ternlang-root/spec

:contentReference[oaicite:2]{index=2}