r/Compilers • u/cozcoo • 8d ago
Token efficiency comparison with C++
abs<T: int|uint|long|ulong>(T t) -> T as operator[|] : PreContract -> t < 0 ?-t:t : PostContract # binding {this:};43 tokens. That's it. 43 tokens of pure, dense meaning.
Let's count what a C++ programmer would have to write to achieve the exact same thing:
cpp
template <typename T, std::enable_if_t<std::is_same_v<T, int> |\| std::is_same_v<T, uint> |\| std::is_same_v<T, long> |\| std::is_same_v<T, ulong>, bool> = true>
struct AbsoluteValue {
T operator()(T t) const {
// PreContract logic here
T result = (t < 0) ? -t : t;
// PostContract logic here
return result;
}
};
// ... and they still wouldn't have |x| circumfix syntax or operator binding ...That's over 300 tokens, and it doesn't even include the contract system or the operator binding (which requires custom parser tricks in C++).
In IDEKA, every single token is pulling its weight:
<T: int|uint|long|ulong> — 11 tokens define a type constraint with 4 allowed types.
as operator[|] — 5 tokens bind a function to a custom syntactic form.
: PreContract — 2 tokens attach code that runs before the body.
-> t < 0 ? -t : t — 9 tokens define the entire execution logic.
: PostContract — 2 tokens attach code that runs after the body.
\# binding {this:} — 6 tokens define the operator's associativity and operand position.
1
u/Discordbothost 6d ago
If int/long are two's-complement, what happens at the signed minimum? Its positive magnitude won't fit in T. Does PreContract reject it, or is overflow defined? That boundary would help pin down the abs example's contract.
1
0
2
u/jcastroarnaud 8d ago
Python sources, and executables (!), are in the main folder of the repo. Order your pet LLM to clean up after itself: no executables in the repo, ever, and Python source code in its own folder.
Test programs printing to stdout, seriously? Not even unit tests? For a language that, from the documentation alone, is as large as C++, that's a criminal lack of tests. Add unit tests to all modules of the compiler, and test harnesses for the runtime (to catch runtime errors cleanly).