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 »

Maybe 1000, but I'm relying heavily on metaprogramming (C++ templates) and generated code from flex/bison, so the total code is maybe 5-6000 lines...
Mac Buster
Retired
Posts: 1474
Joined: 03 Aug 2003 22:37
Location: Moscow

Re: Ternary C-like programming language

Post by Mac Buster »

Any news about the language and its release date ? We're waiting for it :-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 had to rework it extensively because of a stupid design mistake I did at the beginning, that combined with a lot of other stuff taking up my time, I probably would set a pessimistic time plan with a working version beginning of the summer or something like that.

Making a programming language that is merely working good enough is one thing. I want one that is working from the core, without weird work-arounds in the code to get it to sort-of behave like C.

I'm also considering making it more different from C than originally planned. It will still use the same programming paradigm (imperative programming), but with bigger distinction between logic and mathematics, as well as some other changes.

It'll probably altogether be more intuitive from a mathematical and logical point of view. But it's still on the drawing table. I'll update later.
Mac Buster
Retired
Posts: 1474
Joined: 03 Aug 2003 22:37
Location: Moscow

Re: Ternary C-like programming language

Post by Mac Buster »

I see. Keep us informed :-)
eudoxie
Maniac
Posts: 277
Joined: 17 Sep 2012 13:36
Location: 81.170.128.52

Re: Ternary C-like programming language

Post by eudoxie »

Progress have been going really slow lately. I'm still very occupied with other stuff :-(
Mac Buster
Retired
Posts: 1474
Joined: 03 Aug 2003 22:37
Location: Moscow

Re: Ternary C-like programming language

Post by Mac Buster »

Same here - no time to complete other programs for Tunguska :-?
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 finally had some time to work on this thing again. I've scrapped maybe half of the old code base, since it had major issues that were very hard to weed out. I managed to salvage the code for most operations, but introduced new functionality for proper support for pointers and plain-old-data types. I still have to write function code and some other stuff, but most stuff is working pretty nicely at this point.

I'll try to slap that together, and then there is going to be some major debugging. I haven't really been able to check whether the 1000-or-so lines of possible assembly code does what it's supposed to yet (since it currently only produces small pieces of code), so ... yeah. Debugging.

I'd also like to point out that it does not produce effective code. (Likely due to change in the future when more outstanding issues are cleared out)

Compare machine generated code:

Code: Select all

; char c;
; c=c*2;
                PSH     c
                PSH     #2
                PLL     A
                PLL     tmp
                MLL     tmp
                PSH     A
                PLL     A
                STA     c
With hand written code doing the same

Code: Select all

                LDA     c
                MLL     #2
eudoxie
Maniac
Posts: 277
Joined: 17 Sep 2012 13:36
Location: 81.170.128.52

Re: Ternary C-like programming language

Post by eudoxie »

It's slowly beginning to take shape. There are still a bunch of issues to resolve before I'll make an initial release, but things are actually beginning to look a bit like C.

Here's what I've implemented so far:
Basic arithmetic, nested arithmetics, function calls, function definitions, code surrounding conditionals (I've still got to actually implement while and for, but if works), typing and a not-quite-C-compatible pointer arithmetic.

The stuff I've got to do is:
Clean the code up and add sensible error messages.
Implement conditionals.
Implement strings.
Implement incrementers/decrementers.
Feature cleanups. Right now grammar is very rigid, you have to pre-declare each function before you define them and stuff like that, but I'll remove that in the future.

The stuff I probably won't do until a later release:
Structs
Arrays
Goto:s
Optimization (the produced code is very unoptimized right now)
eudoxie
Maniac
Posts: 277
Joined: 17 Sep 2012 13:36
Location: 81.170.128.52

Re: Ternary C-like programming language

Post by eudoxie »

Okay, so right now it's gotten so far that I'm able to debug the instructions it's outputting by writing programs and running them in Tunguska. This is a lot of work, making sure that every possible aspect of the language works as it should, but I suspect the best way of doing it is simply to write programs, see if they behave as they should, and if not fix the problem.

On the list of stuff I've yet to do I posted earlier, these have been at least partially completed:

*Clean the code up and add sensible error messages.
*Implement conditionals.
*Implement strings.
User avatar
Shaos
Admin
Posts: 23989
Joined: 08 Jan 2003 23:22
Location: Silicon Valley

Re: Ternary C-like programming language

Post by Shaos »

Great news! Is it available somewhere?
eudoxie
Maniac
Posts: 277
Joined: 17 Sep 2012 13:36
Location: 81.170.128.52

Re: Ternary C-like programming language

Post by eudoxie »

Not yet. It's still so buggy I'm not comfortable with releasing even alpha or CVS versions of it. It is at least a couple of weeks away.

I will put it in the tunguska CVS when it is though, and make a note of it in this thread.
Mac Buster
Retired
Posts: 1474
Joined: 03 Aug 2003 22:37
Location: Moscow

Re: Ternary C-like programming language

Post by Mac Buster »

Great news indeed! I'm waiting for release to start use it :)
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 really wonderful that it's finally taking shape. I compiled this program successfully earlier (it prints "Hello world" diagonally across the screen):

Code: Select all

#define LOOP asm(".loop:", "JMP .loop")
#define DEBUGVAL(X) asm("LAD (" #X ")", "DEBUG")

void putcxy(char c, char x, char y);

void main() {
   char* string = "Hello world";
   char c = 0;
   while(*string) {
     putcxy(*string, c, c);
     string = string + 1;
     c = c + 1;
   }

   LOOP;

}
void putcxy(char c, char x, char y) {
   char* redraw = -265718;
   char* memory = -264262;
   memory = memory + 54 * y + x;
   *memory = c;
   *redraw = 1;
}
I'm using GCC's macro preprocessor for function macros.

Unfortunately, it also illustrates the problem at this stage: Inefficiency. It generated a whopping 226 lines or roughly a half memory page of assembly code to complete this simple task.
Mac Buster
Retired
Posts: 1474
Joined: 03 Aug 2003 22:37
Location: Moscow

Re: Ternary C-like programming language

Post by Mac Buster »

Well, seems like it generate much better code than I can do :)
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 don't know about better, but it's systematic. The problem with assembly is that the main workload isn't writing the program, but trivial things like making sure to add the carry to the most significant tryte. Which is a horrible waste of time.

I've gotten a lot of work done today. These things are off the todo list:
* Variable scopes
* Post/pre incrementers/decrementers
* For loops
* Escape characters in strings

Never have I been as happy to see an isometric cube rendered on a screen as when this:

Code: Select all

#define RED 0n4DD
#define GREEN 0nD4D
#define BLUE 0nDD4

void rastermode();
void putpixel(char x, char y, char c);
void repaint();

void main() {
   char x = 10;
   char y = 10;

   rastermode();

   for(x = 10; x < 110; x++)  {
     putpixel(x, 10, RED);
     putpixel(x, 110, RED);
     putpixel(x+50, 60, RED);
     putpixel(x+50, 160, RED);
   }

   for(y = 10; y < 110; y++) {
     putpixel(10, y, GREEN);
     putpixel(110, y, GREEN);
     putpixel(60, y+50, GREEN);
     putpixel(160, y+50, GREEN);
   }

   y = 10;
   for(x = 10; x < 60; x++) {
     putpixel(x, y+100, BLUE);
     putpixel(x+100, y, BLUE);
     putpixel(x+100, y+100, BLUE);
     putpixel(x, y, BLUE);
     
     y++;
   }

   repaint();
   for(;;);
}

void rastermode() {
   *(char*)(0nDDDDDB) = *(char*)(0nDDDDDB) & 0t0000N0;
}
void repaint() {
    *(char*)(0nDDDDDB) = 0t0000NP; 
}
void putpixel(char x, char y, char c) {
   *(char*)(0nDDBDDD + x + 324*y) = c;
}
Compiled into this:

Image