Author |
Message |
Shaos
Admin
Joined: 08 Jan 2003 23:22 Posts: 22829 Location: Silicon Valley
|
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 |
|
|
eudoxie
Maniac
Joined: 17 Sep 2012 13:36 Posts: 277 Location: 81.170.128.52
|
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 |
|
|
Mac Buster
Retired
Joined: 03 Aug 2003 22:37 Posts: 1474 Location: Moscow
|
So, you would like to perform up to three binary logic operations instead of one ternary operation ? 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). 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;
Well, you can try
|
15 Feb 2008 14:49 |
|
|
Shaos
Admin
Joined: 08 Jan 2003 23:22 Posts: 22829 Location: Silicon Valley
|
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 |
|
|
eudoxie
Maniac
Joined: 17 Sep 2012 13:36 Posts: 277 Location: 81.170.128.52
|
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 |
|
|
Shaos
Admin
Joined: 08 Jan 2003 23:22 Posts: 22829 Location: Silicon Valley
|
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 |
|
|
eudoxie
Maniac
Joined: 17 Sep 2012 13:36 Posts: 277 Location: 81.170.128.52
|
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 |
|
|
Shaos
Admin
Joined: 08 Jan 2003 23:22 Posts: 22829 Location: Silicon Valley
|
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 |
|
|
eudoxie
Maniac
Joined: 17 Sep 2012 13:36 Posts: 277 Location: 81.170.128.52
|
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 |
|
|
Shaos
Admin
Joined: 08 Jan 2003 23:22 Posts: 22829 Location: Silicon Valley
|
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 |
|
|
eudoxie
Maniac
Joined: 17 Sep 2012 13:36 Posts: 277 Location: 81.170.128.52
|
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 |
|
|
eudoxie
Maniac
Joined: 17 Sep 2012 13:36 Posts: 277 Location: 81.170.128.52
|
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 |
|
|
Mac Buster
Retired
Joined: 03 Aug 2003 22:37 Posts: 1474 Location: Moscow
|
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
|
17 Mar 2008 14:04 |
|
|
eudoxie
Maniac
Joined: 17 Sep 2012 13:36 Posts: 277 Location: 81.170.128.52
|
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 |
|
|
Shaos
Admin
Joined: 08 Jan 2003 23:22 Posts: 22829 Location: Silicon Valley
|
You did this so fast! How many lines of code do you have to have 40% of C functionality?
|
19 Mar 2008 03:42 |
|
|