nedoPC.org

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



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

Joined: 17 Sep 2012 13:36
Posts: 277
Location: 81.170.128.52
Reply with quote
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:
#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:
X = X |P> 3;


TSH on Y and 20 would be written

Code:
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.


29 Jul 2008 15:15
Profile
Maniac

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


12 Aug 2008 07:48
Profile
Retired

Joined: 03 Aug 2003 22:37
Posts: 1474
Location: Moscow
Reply with quote
Thanks for news! Will try it on weekend :)


12 Aug 2008 12:20
Profile
Maniac

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


20 Aug 2008 14:27
Profile
Maniac

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


21 Aug 2008 05:36
Profile
Maniac

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


23 Aug 2008 11:53
Profile
Maniac

Joined: 17 Sep 2012 13:36
Posts: 277
Location: 81.170.128.52
Reply with quote
Array indexing now works like in C. Say you have a pointer p to some type t,
Code:
p[t]

is now equivalent to
Code:
*(p+sizeof(*p)*t)


24 Aug 2008 11:21
Profile
Maniac

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


24 Aug 2008 13:56
Profile
Maniac

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


27 Aug 2008 04:29
Profile
Maniac

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


19 Sep 2008 14:47
Profile
Retired

Joined: 03 Aug 2003 22:37
Posts: 1474
Location: Moscow
Reply with quote
Did you updated public sources ?


20 Sep 2008 12:47
Profile
Maniac

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


21 Sep 2008 07:54
Profile
Maniac

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


24 Sep 2008 04:48
Profile
Admin
User avatar

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


24 Sep 2008 19:00
Profile WWW
Maniac

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


30 Sep 2008 10:33
Profile
Display posts from previous:  Sort by  
Reply to topic   [ 64 posts ]  Go to page Previous  1, 2, 3, 4, 5  Next

Who is online

Users browsing this forum: No registered users and 4 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.