-
Notifications
You must be signed in to change notification settings - Fork 6
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
Ternlang source files use the .tern extension.
Example:
program.tern
Source code is parsed into:
- tokens
- AST nodes
- BET bytecode instructions
Single-line comments:
// this is a comment
Use comments for:
- logic documentation
- model assumptions
- safety gates
- experimental notes
Identifiers are used for:
- variables
- functions
- modules
- namespaces
Example:
let signal: trit = affirm;
Valid examples:
my_var
safety_gate
expert13
consensus_score
Primary scalar type:
trit
Possible values:
reject
tend
affirm
Syntax:
let name: type = value;
Example:
let decision: trit = affirm;
Multiple declarations:
let a: trit = reject;
let b: trit = tend;
let c: trit = affirm;
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);
}
Syntax:
return value;
Example:
return affirm;
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}
Binary compatibility syntax is supported.
if score > 0 {
return affirm;
}
Used for:
- translated Python logic
- SQL rule migration
- threshold classifiers
Blocks use braces:
{
...
}
Example:
fn gate(x: trit) -> trit {
return x;
}
=
Example:
let x: trit = affirm;
==
!=
<
>
<=
>=
Example:
if score >= 0 {
return affirm;
}
&&
||
Example:
if valid && safe {
return affirm;
}
Syntax:
name(arg1, arg2)
Example:
consensus(affirm, tend)
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.
Syntax:
tensor[row, col]
Example:
weights[2, 4]
Used for:
- tensors
- matrices
- sparse weights
- feature maps
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.
fn main(signal: trit) -> trit {
match signal {
reject => { return reject; }
tend => { return tend; }
affirm => { return affirm; }
}
}
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}