nedoPC.org

Electronics hobbyists community established in 2002
Atom Feed | View unanswered posts | View active topics It is currently 28 Mar 2024 12:15



Reply to topic  [ 64 posts ]  Go to page 1, 2, 3, 4, 5  Next
Ternary C-like programming language 
Author Message
Admin
User avatar

Joined: 08 Jan 2003 23:22
Posts: 22412
Location: Silicon Valley
Reply with quote
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?


14 Feb 2008 17:45
Profile WWW
Maniac

Joined: 17 Sep 2012 13:36
Posts: 277
Location: 81.170.128.52
Reply with quote
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?


15 Feb 2008 05:31
Profile
Retired

Joined: 03 Aug 2003 22:37
Posts: 1474
Location: Moscow
Reply with quote
Quote:
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 ?

Quote:
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).

Quote:
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:

test(expression)
{
true:
{
break;
}
false:
{
break;
}
unknown:
{
break;
}
}

If a state is not mentioned then control passes to end of test() block;

Quote:
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 :-)


15 Feb 2008 14:49
Profile
Admin
User avatar

Joined: 08 Jan 2003 23:22
Posts: 22412
Location: Silicon Valley
Reply with quote
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...


17 Feb 2008 16:33
Profile WWW
Maniac

Joined: 17 Sep 2012 13:36
Posts: 277
Location: 81.170.128.52
Reply with quote
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.


18 Feb 2008 11:09
Profile
Admin
User avatar

Joined: 08 Jan 2003 23:22
Posts: 22412
Location: Silicon Valley
Reply with quote
We can do >> and << as binary shifting (/2^n and *2^n), but >>> and <<< as ternary shifting - directly supported in hardware


18 Feb 2008 14:34
Profile WWW
Maniac

Joined: 17 Sep 2012 13:36
Posts: 277
Location: 81.170.128.52
Reply with quote
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?


18 Feb 2008 14:45
Profile
Admin
User avatar

Joined: 08 Jan 2003 23:22
Posts: 22412
Location: Silicon Valley
Reply with quote
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).


18 Feb 2008 21:14
Profile WWW
Maniac

Joined: 17 Sep 2012 13:36
Posts: 277
Location: 81.170.128.52
Reply with quote
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?


19 Feb 2008 02:18
Profile
Admin
User avatar

Joined: 08 Jan 2003 23:22
Posts: 22412
Location: Silicon Valley
Reply with quote
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


19 Feb 2008 06:02
Profile WWW
Maniac

Joined: 17 Sep 2012 13:36
Posts: 277
Location: 81.170.128.52
Reply with quote
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.


19 Feb 2008 10:11
Profile
Maniac

Joined: 17 Sep 2012 13:36
Posts: 277
Location: 81.170.128.52
Reply with quote
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.


15 Mar 2008 13:57
Profile
Retired

Joined: 03 Aug 2003 22:37
Posts: 1474
Location: Moscow
Reply with quote
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


17 Mar 2008 14:04
Profile
Maniac

Joined: 17 Sep 2012 13:36
Posts: 277
Location: 81.170.128.52
Reply with quote
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).


18 Mar 2008 04:03
Profile
Admin
User avatar

Joined: 08 Jan 2003 23:22
Posts: 22412
Location: Silicon Valley
Reply with quote
You did this so fast! How many lines of code do you have to have 40% of C functionality?


19 Mar 2008 03:42
Profile WWW
Display posts from previous:  Sort by  
Reply to topic   [ 64 posts ]  Go to page 1, 2, 3, 4, 5  Next

Who is online

Users browsing this forum: No registered users and 3 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Jump to:  
Powered by phpBB® Forum Software © phpBB Group
Designed by ST Software.