Ternary C-like programming language

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

Moderator: haqreu

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 made a lot of progress on the language, even begun working on optimization. There is just a few bugs I would like to work out, and a few instructions I'd like to add, and then I'll put it on the CVS. If everything goes to plan, it's maybe a week away, possibly even less :-D.

In debugging, I've begun work on a standard library, and I tell you: It feels really strange to write lines like

Code: Select all

#define NULL 0

I've also used ket notation for various trinary operations that lack a symbol in standard C. It's just a temporary placeholder grammar until better symbols can be found. Right now, to use the PRM instruction on X and 3, one would write

Code: Select all

X = X |P> 3;
TSH on Y and 20 would be written

Code: Select all

Y = Y |S> 20;
It is dangerously ambiguous grammar (X|P>3 has a different meaning from X | P > 3), so I'm 100% certain this will be changed in the near future.
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 just imported it to the tunguska CVS (under the name tunguska_3cc).

It's still got a lot of work to be done (some of the code design is dubious to say the least), but it should work reasonably well. I'll get around to uploading some working c code for it later.

Also, as usual with flex and bison (for some reason, they really don't like my makefiles), you have to compile with the following

"make clean all"

Otherwise, they will complain about missing autogenerated files.
Mac Buster
Retired
Posts: 1474
Joined: 03 Aug 2003 22:37
Location: Moscow

Re: Ternary C-like programming language

Post by Mac Buster »

Thanks for news! Will try it on weekend :)
eudoxie
Maniac
Posts: 277
Joined: 17 Sep 2012 13:36
Location: 81.170.128.52

Re: Ternary C-like programming language

Post by eudoxie »

The C compiler is actually sort of working. I've made a prompt like in the assembler image. It really doesn't actually do anything, but it does take commands :-)

Image
eudoxie
Maniac
Posts: 277
Joined: 17 Sep 2012 13:36
Location: 81.170.128.52

Re: Ternary C-like programming language

Post by eudoxie »

Actually, this is really cool, I got it to draw a mandelbrot fractal (in a very slow manner, takes at least a minute to render):

Image

Code: Select all

#pragma ORIGIN 0

char mandelbrot(char x, char y);
void main() {
	int x;
	int y;
	for(x = 0; x < 324; x++) {
		for(y = 0; y < 243; y++) {
			*(char*) (0nDDBDDD + x + 324*y) = mandelbrot(x,y);
		}
		*(char*) (0nDDDDDB) = -2;
	}
	for(;;) ;
}

/* Use integers as fixed point numbers. That is, 
 * [11 10 9 8] [7 6 5 4] [3 2 1 0]
 *  Spillover   Integer   Decimal 
 * Store the integer part in the four middle trits,
 * Store the fractional part in the four least significant trits,
 * And the four most significant trits store multiplication results
 *  (divide by 81 after each multiplication to get back in format)
 */
char mandelbrot(char x, char y) {
	int CX; 
	int CY;
	int DX = 0;
	int DY = 0;
	int DX2 = 0;
	int DY2 = 0;
	char i = 0;

	/* Translate screen coordinates to complex space */
	CX = x - 160;
	CY = (324* y) / 243 - 160;

	
	while(DX2 + DY2 < 324) {
		// D' = D*D + C, where C and D are complex numbers
		DY = 2*DY*DX / 81 + CY;
		DX = DX2 - DY2 + CX;
		DX2 = DX*DX / 81;
		DY2 = DY*DY / 81;

		// Iterate 10 times at most
		if(++i > 10) return -364;
	}

	return 72*(i-5);
}
eudoxie
Maniac
Posts: 277
Joined: 17 Sep 2012 13:36
Location: 81.170.128.52

Re: Ternary C-like programming language

Post by eudoxie »

This is pretty cool. I added support for arbitrary commutative operators in 3CC. The syntax is as follows:

x = a << OP >> b, where OP is any given number.

It's easiest to enter the instruction in ternary, instead of decimal or nonary, since that makes it possible to put up a truth table and directly see what the operator does without having to do a lot of calculations.

Like, if I wanted to create an AND operator, I would put up a table like this, and then simply enter the correct values:

Code: Select all

/*      A                 NNOOPP
 *      B                 NOOPPN */
#define COP_AND         0tNN00PN //
Now a << COP_AND >> b is the same as a & b.


I may change the syntax to a <[ OP ]> b in the future, if and when I implement trit shifting.
eudoxie
Maniac
Posts: 277
Joined: 17 Sep 2012 13:36
Location: 81.170.128.52

Re: Ternary C-like programming language

Post by eudoxie »

Array indexing now works like in C. Say you have a pointer p to some type t,

Code: Select all

p[t]
is now equivalent to

Code: Select all

*(p+sizeof(*p)*t)
eudoxie
Maniac
Posts: 277
Joined: 17 Sep 2012 13:36
Location: 81.170.128.52

Re: Ternary C-like programming language

Post by eudoxie »

Holy smoke, it's raining features over 3CC today. I just finished implementing structs!

Thus far, the following features are either buggy or lacking:

* Variable initialization.
* Global variables.
* Unions.
* Arrays (they aren't quite treated as pointers, as they should)
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 been without internet access for the last few days, so there's a whole host of changes to 3cc this time around:

* Late symbol evaluation, this means that variable initialization for primitive types should work now.
* At the very least 20 times faster 12 trit arithmetic!!! (Through new opcodes in tunguska). Clearing the screen with a loop in raster mode now takes a second, instead of ten+.
* Arrays work better (still sort of quirky).
* Static variables work better.
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 haven't had much time to spend programming as of late, but I have made some progress with 3CC. It now handles the interaction between {array,pointer,reference} better.
Mac Buster
Retired
Posts: 1474
Joined: 03 Aug 2003 22:37
Location: Moscow

Re: Ternary C-like programming language

Post by Mac Buster »

Did you updated public sources ?
eudoxie
Maniac
Posts: 277
Joined: 17 Sep 2012 13:36
Location: 81.170.128.52

Re: Ternary C-like programming language

Post by eudoxie »

Yeah, it's in the CVS.
eudoxie
Maniac
Posts: 277
Joined: 17 Sep 2012 13:36
Location: 81.170.128.52

Re: Ternary C-like programming language

Post by eudoxie »

Even though 3CC isn't quite finished, I'm probably going to include it in a release of tunguska pretty soon (matter of weeks?). I'll just have to type up a new chapter in the manual on how to use 3CC.
User avatar
Shaos
Admin
Posts: 24008
Joined: 08 Jan 2003 23:22
Location: Silicon Valley

Re: Ternary C-like programming language

Post by Shaos »

eudoxie wrote: Even though 3CC isn't quite finished, I'm probably going to include it in a release of tunguska pretty soon (matter of weeks?). I'll just have to type up a new chapter in the manual on how to use 3CC.
OK, so I will wait until your CVS will be "frozen" for that new version and then I will prepare Windows build and may be MacOS X build for PowerPC...
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 have a few things I want to fix before releasing, but hopefully it won't take that long.

The problems I'm addressing are the following:
* Lack of initializers for global and or non plain-old-data variables. I've gotten most of the architectural changes to support such code done.
* Strange variable management in functions. In regular C, if you recurse back into a function, existing non-static variable values are preserved. But in 3CC, it simply rewrites the same memory location with new values, causing unpredictable behavior. This is because regular x86 C can afford to put variables like this on the stack, but since tunguska has a claustrophobia-inducingly small stack, so it's really hard to motivate squandering that much stack space. I may add something like a 'reentrant' keyword to functions, if more standard-C like behavior is desired.

Also, the SF CVS is down at the moment, they're apparently doing maintenance.