Ternary C-like programming language

Balanced Ternary Numeral System - forum was moved from http://ternary.info

Moderator: haqreu

User avatar
Shaos
Admin
Posts: 24080
Joined: 08 Jan 2003 23:22
Location: Silicon Valley

Ternary C-like programming language

Post by Shaos »

Right now I think about how should look syntax of ternary C-like programming language. Problem is "binary" operations like << >> & | ^ ~ and logical && || !. Should it have "ternary" meaning or it has to be still "binary" with some set of special "ternary" variants like <<< >>> &&& ||| etc?
eudoxie
Maniac
Posts: 277
Joined: 17 Sep 2012 13:36
Location: 81.170.128.52

Re: Ternary C-like programming language

Post by eudoxie »

Ternary logic can be made to be boolean-compatible (if the only interaction is between true and false-states, it behaves like binary logic), so I don't see a pressing need for special operators for ternary logic.

But there would also need to be special operators (both tritwise and trytewise). Like, in my assembler I've implemented several operators (roll, but and permute) that have no symbol in C.

I've been thinking about how controlling program flow would change in ternary-space. Example: In C you have

if(expression) statement;
else statement;

But it would be possible to implement, in ternary, a construction like this (almost fringing on a switch statement):

test(expression) statement; // expression is true
but statement; // expression is unknown
else statement; // expression is false

Though I wonder if it wouldn't be easier to work out the finesses of ternary high level computing in a more readable and less complex language, like BASIC (where you wouldn't need to concern yourself with introducing special operators.) At least to start with?
Mac Buster
Retired
Posts: 1474
Joined: 03 Aug 2003 22:37
Location: Moscow

Re: Ternary C-like programming language

Post by Mac Buster »

Ternary logic can be made to be boolean-compatible (if the only interaction is between true and false-states, it behaves like binary logic), so I don't see a pressing need for special operators for ternary logic.
So, you would like to perform up to three binary logic operations instead of one ternary operation ?
But there would also need to be special operators (both tritwise and trytewise). Like, in my assembler I've implemented several operators (roll, but and permute) that have no symbol in C.
There are enough true math and science symbols in modern unicode fonts, that can be mapped to any keys combination in most OS and graphic environment. I myself prefer to use graphic buttons of IDE instead of typing obsolete characters sequence that were made to be used as replacements for proper symbols on ASCII terminats (since it can display only limited set of characters).
I've been thinking about how controlling program flow would change in ternary-space. Example: In C you have

if(expression) statement;
else statement;

But it would be possible to implement, in ternary, a construction like this (almost fringing on a switch statement):

test(expression) statement; // expression is true
but statement; // expression is unknown
else statement; // expression is false
Why not use something more clear, like this:

Code: Select all

test(expression)
{
	true:
		{
			break;
		}
	false:
		{
			break;
		}
	unknown:
		{
			break;
		}
}
If a state is not mentioned then control passes to end of test() block;
Though I wonder if it wouldn't be easier to work out the finesses of ternary high level computing in a more readable and less complex language, like BASIC (where you wouldn't need to concern yourself with introducing special operators.) At least to start with?
Well, you can try :-)
User avatar
Shaos
Admin
Posts: 24080
Joined: 08 Jan 2003 23:22
Location: Silicon Valley

Re: Ternary C-like programming language

Post by Shaos »

If we will use ternary logic as binary (just forget third state) it will not work with arithmetics as every C programmer knows that >>1 means /2 and <<1 means *2, in this case it will be wrong...
eudoxie
Maniac
Posts: 277
Joined: 17 Sep 2012 13:36
Location: 81.170.128.52

Re: Ternary C-like programming language

Post by eudoxie »

But that is a base-specific hack. You can't expect a ternary machine to have weird shifting operations that work in binary to maintain backward-compability with binary machines.

Well, that's my opinion anyways.
User avatar
Shaos
Admin
Posts: 24080
Joined: 08 Jan 2003 23:22
Location: Silicon Valley

Re: Ternary C-like programming language

Post by Shaos »

We can do >> and << as binary shifting (/2^n and *2^n), but >>> and <<< as ternary shifting - directly supported in hardware
eudoxie
Maniac
Posts: 277
Joined: 17 Sep 2012 13:36
Location: 81.170.128.52

Re: Ternary C-like programming language

Post by eudoxie »

That's true. But I don't see any real gains from this. A ternary language will never be completely compatible with a binary one, and it's fairly likely that "faking" binary shifting on a ternary machine will lead to performance loss (instead of the intended performance gain).

Unless you intend to add backward compatibility for other binary logic. Where you have A & B is binary and, A&&B is ternary "sign logic", and A&&&B is ternary and?
User avatar
Shaos
Admin
Posts: 24080
Joined: 08 Jan 2003 23:22
Location: Silicon Valley

Re: Ternary C-like programming language

Post by Shaos »

eudoxie wrote: That's true. But I don't see any real gains from this. A ternary language will never be completely compatible with a binary one, and it's fairly likely that "faking" binary shifting on a ternary machine will lead to performance loss (instead of the intended performance gain).

Unless you intend to add backward compatibility for other binary logic. Where you have A & B is binary and, A&&B is ternary "sign logic", and A&&&B is ternary and?
Main idea is to make most of existing C code working. So && || ! may be binary compatible in case of operating in only 2 logical values from 3. << and >> may be binary shiftings (bad performance). <<< and >>> - ternary shiftings (direct hardware support). &,|,^,~ may be binary compatible (bad performance) and &&& ||| may be "tritwise" MIN and MAX (we do not need ~~~ because of simply unary minus '-' is the same thing, also we do not need ^^^ because I personally do not see much sense in ternary exclusive OR).
eudoxie
Maniac
Posts: 277
Joined: 17 Sep 2012 13:36
Location: 81.170.128.52

Re: Ternary C-like programming language

Post by eudoxie »

But do you intend to have these operators work on the bytes or the trytes of the memory value?

If the former -- Why is there a need for binary operators when most ternary operators are identical to binary if all trits are either true or false?

Of the latter -- How are you going to handle the mismatching byte/tryte sizes?
User avatar
Shaos
Admin
Posts: 24080
Joined: 08 Jan 2003 23:22
Location: Silicon Valley

Re: Ternary C-like programming language

Post by Shaos »

Sizes of integers should not be a big problem because of existing macros MAX_INT and MIN_INT, but algorithm that uses overloaded unsigned ints will not work :(

Also if we will handle trits as bits we will lose "arithmetic" meaning of set of that bits...

Anyway it's just ideas and first version of C-like syntax that I will use will be fully ternary
eudoxie
Maniac
Posts: 277
Joined: 17 Sep 2012 13:36
Location: 81.170.128.52

Re: Ternary C-like programming language

Post by eudoxie »

But the problem is that there is no byte/tryte widths that give you a matching number of states. You will have more states in ternary than in binary, or vice versa.

In ternary, you have 3^width states, and in binary, you have 2^width states. Number theory (the unique prime factorization theorem specifically) tells us that there can never be a 3^a = 2^b, except for the case a=b=0.
eudoxie
Maniac
Posts: 277
Joined: 17 Sep 2012 13:36
Location: 81.170.128.52

Re: Ternary C-like programming language

Post by eudoxie »

I'm currently tinkering with a C-like language that translates into tunguska assembly code. I haven't put that many hours into it, so it doesn't do very much at the moment, but the little it does, it does pretty robustly. It has typing support (very loose, even by C standards, will probably only support char and "short"/pointer types) with implicit and explicit casting.
Mac Buster
Retired
Posts: 1474
Joined: 03 Aug 2003 22:37
Location: Moscow

Re: Ternary C-like programming language

Post by Mac Buster »

Even a minimalistic C-like language will be very handy, since coding 6502 asm is not easy thing for me. I do not have any experience and no doubd did some silly mistakes. But I do not see them :-D
eudoxie
Maniac
Posts: 277
Joined: 17 Sep 2012 13:36
Location: 81.170.128.52

Re: Ternary C-like programming language

Post by eudoxie »

I've actually done a lot of progress on the compiler. It supports maybe 40% of all the functionality of C. A couple of flow controllers (for, if, while), functions with arguments and return types.

What I've got left is a lot of the basic, yet tedious functionality. There are so many cases to deal with. I never realized how many ways of adding two numbers there are in C. You've got a=a+b, a+=b, a++, ++a. And you've got similar cases for all basic arithmetic. Then there's the comparative functions (but at least there I get to reap the benefits of ternary logic).
User avatar
Shaos
Admin
Posts: 24080
Joined: 08 Jan 2003 23:22
Location: Silicon Valley

Re: Ternary C-like programming language

Post by Shaos »

You did this so fast! How many lines of code do you have to have 40% of C functionality?