Programming C, C++, Java, PHP, Ruby, Turing, VB
Computer Science Canada 
Programming C, C++, Java, PHP, Ruby, Turing, VB  

Username:   Password: 
 RegisterRegister   
 Writing my own programming language
Index -> General Programming
View previous topic Printable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic
Author Message
Prads




PostPosted: Mon Feb 25, 2013 7:24 am   Post subject: Writing my own programming language

Hello, I have been working on this project for some months now and I
want to show it to other people. Basically speaking, it's a programming
language which can be compiled and ran using a virtual machine. Here's a
detailed explanation I wrote in my blog:

Quote:

So, in last blog post I gave small introduction to my current
project that I am working on. In this blog I want to release the first
version of my current project. You can grab it from here: http://www.pradsproj...om/despair.html

So, this piece of software that I wrote is a system to write portable
programs. It has its own programming language and the program that are
compiled can be executed using a Virtual Machine. The system isn?t
mature and lot of things are lacking but I hope to develop it further in
the future.

Let me talk a little about how this system works. You write a program
using its programming language (here?s the documentation of programming
language: http://www.pradsproj....ion/index.html)
then compile it using compiler tool that I have written. The compiler
generates a file with ?.dbin? extension which can be executed using the
Virtual Machine.

I have created a small IDE for developing program for this system
which you can download it from the project site (the one that I gave you
on the top). IDE consist of editor to write and manage source code
(which is fairly basic at the moment, no syntax highlighting for the
moment sorry), a compiler to compile the source code and a virtual
machine to run the compiled file.

Alternately, if you don?t want to create programs for the system but
want to run program created by others, you?re going to have to download
the VM from the project site. There are two types of VM for Windows OS:
interpreter version and JIT version (Dynamic Recompiler). The JIT
version is faster than interpreter version though I am still not very
happy with the JIT performance, I think it can be improved more. I will
port the JIT version to Linux later.

I want to create the Virtual Machine for various different platforms
like android, iOS, MacOS etc so that the program written using
despairLanguage can be run in different platforms. Currently the VM
works in Windows and Linux.

P.S: The software is open source and is on github. Links are on the project site.


So, I just wanted you guys to look at it and play around with it. Some feedbacks (good or bad) is highly appreciated.

Thanks!
Sponsor
Sponsor
Sponsor
sponsor
Zren




PostPosted: Mon Feb 25, 2013 1:00 pm   Post subject: Re: Writing my own programming language

I'm guessing you meant: http://www.pradsprojects.com/despair.html

Since people are lazy, might as well link and copy some example here too: http://www.pradsprojects.com/despairLanguageDocumentation/index.html
There seems to be a tiny tutorial here: http://www.pradsprojects.com/despairLanguageDocumentation/others.html

The syntax is close to C++, so I'll use that for the highlighting.

loadBMP.dsrc from the Breakout example.
c++:

group BMPHeader {
        byte signature[2];
        int size, reserved, imgOffset, sizeOfBIH, width, height;
        byte numOfPlanes[2], bitDepth[2];
        int compression, imageSize, hPixPerM, vPixPerM, numOfColors, numOfImpColors;
        int redMask, greenMask, blueMask, alphaMask;
}

public int loadImageFromBMP(string path, pointer image imgOut) {
        //Open Bitmap File
        file bmpFile;
        if (openFile(bmpFile, path, 1) == 0) {
                return 0;
        }
       
        //Get Bitmap Header
        BMPHeader header;
        readFile(bmpFile, weakPtr header, sizeof(BMPHeader));
       
        //Only BI_RGB ad BI_BITFIELDS compressions are supported
        //Only 16, 24, and 32 bits bitmaps are supported
        if (header.compression != 0 && header.compression != 3 && header.bitDepth != 32 && header.bitDepth != 24 && header.bitDepth != 16) {
                closeFile(bmpFile);
                return 0;
        }
       
        //Get number of bytes per row, including padding bytes
        int numOfBytesInRow = header.width * ((header.bitDepth | (header.bitDepth[1] << 8)) / 8);
        int modResult = numOfBytesInRow % 4;
        if (modResult) {
                numOfBytesInRow = numOfBytesInRow + 4 - modResult;
        }

        //Fix the header image size
        header.imageSize = numOfBytesInRow * header.height;
        if (header.imageSize < 0) {
                 header.imageSize = header.imageSize * -1;
        }
       
        //Get the pixel array
        pointer byte tempPixelArray = new byte[header.imageSize];
        setFileCursor(bmpFile, header.imgOffset);
        readFile(bmpFile, tempPixelArray, header.imageSize);
        closeFile(bmpFile);

        //If height is positive, image is upside down
        if (header.height > 0) {
                //Swap the bytes from top and bottom rows
                int frontRowPointer = 0, backRowPointer = (header.height - 1) * numOfBytesInRow;
                int i = 0;
                while (i < header.height / 2) {
                        int fp = frontRowPointer, bp = backRowPointer, j = 0;
                        while (j < numOfBytesInRow) {
                                tempPixelArray[fp] = tempPixelArray[fp] ^ tempPixelArray[bp];
                                tempPixelArray[bp] = tempPixelArray[fp] ^ tempPixelArray[bp];
                                tempPixelArray[fp] = tempPixelArray[fp] ^ tempPixelArray[bp];
                                fp = fp + 1;
                                bp = bp + 1;
                                j = j + 1;
                        }
                        frontRowPointer = frontRowPointer + numOfBytesInRow;
                        backRowPointer = backRowPointer - numOfBytesInRow;
                        i = i + 1;
                }
        } else {
                header.height = header.height * -1;
        }

        //Create image structure       
        pointer byte img = new byte[header.width * header.height * 4 + 20];
        pointer int imgDimension;
        -imgDimension = img;
        imgDimension = header.width;
        -imgDimension = img[4]
        imgDimension = header.height;
       
        //Put the RGB data in image stucture
        pointer byte pixelArray;
        -pixelArray = img[20]
        if (header.compression == 0) { //BI_RGB
                if (header.bitDepth == 16) {
                        rgb16(pixelArray, header, tempPixelArray, numOfBytesInRow);
                } else if (header.bitDepth == 24) {
                        rgb24(pixelArray, header, tempPixelArray, numOfBytesInRow);
                } else {
                        rgb32(pixelArray, header, tempPixelArray, numOfBytesInRow);
                }
        } else { //BI_BITFILED
                if (header.bitDepth == 16) {
                        bitFields16(pixelArray, header, tempPixelArray, numOfBytesInRow);
                } else if (header.bitDepth == 24) {
                        bitFields24(pixelArray, header, tempPixelArray, numOfBytesInRow);
                } else {
                        bitFields32(pixelArray, header, tempPixelArray, numOfBytesInRow);
                }
        }       

        delete tempPixelArray;
        pointer image imgStructure;
        -imgStructure = img;
        imgOut = imgStructure;
        return 1;
}

// ... Cut by Zren

void rgb32(pointer byte pixelArrayOut, pointer BMPHeader header, pointer byte tempPixelArray, int numOfBytesInRow) {
        //Get number of bytes per row excluding padding
        int exNumOfByteInRow = header.width * ((header.bitDepth | (header.bitDepth[1] << 8)) / 8);

        int srcByteCounter = 0, destByteCounter = 0;

        //Copy the RGB values from tempPixelArray to pixelArrayOut
        int y = 0;
        while (y < header.height) {
                srcByteCounter = y * numOfBytesInRow;
                int x = 0;
                while (x < exNumOfByteInRow) {
                        pixelArrayOut[destByteCounter] = tempPixelArray[srcByteCounter];
                        srcByteCounter = srcByteCounter + 1;
                        destByteCounter = destByteCounter + 1;
                        pixelArrayOut[destByteCounter] = tempPixelArray[srcByteCounter];
                        srcByteCounter = srcByteCounter + 1;
                        destByteCounter = destByteCounter + 1;
                        pixelArrayOut[destByteCounter] = tempPixelArray[srcByteCounter];
                        srcByteCounter = srcByteCounter + 1;
                        destByteCounter = destByteCounter + 1;
                        pixelArrayOut[destByteCounter] = tempPixelArray[srcByteCounter];
                        srcByteCounter = srcByteCounter + 1;
                        destByteCounter = destByteCounter + 1;

                        x = x + 4;     
                }
                y = y + 1;
        }       
}

// ... Cut by Zren

void bitFields32(pointer byte pixelArrayOut, pointer BMPHeader header, pointer byte tempPixelArray, int numOfBytesInRow) {
        //Get number of bytes per row excluding padding
        int exNumOfByteInRow = header.width * ((header.bitDepth | (header.bitDepth[1] << 8)) / 8);

        int srcByteCounter = 0, destByteCounter = 0;

        //Get Mask shift values
        int redShiftValue = findTrailingZeros(header.redMask), greenShiftValue = findTrailingZeros(header.greenMask), blueShiftValue = findTrailingZeros(header.blueMask), alphaShiftValue = findTrailingZeros(header.alphaMask);

        //Copy the RGB values from tempPixelArray to pixelArrayOut
        int y = 0;
        while (y < header.height) {
                srcByteCounter = y * numOfBytesInRow;
                int x = 0;
                while (x < exNumOfByteInRow) {
                        pointer int rgb;
                        -rgb = tempPixelArray[srcByteCounter];
                        srcByteCounter = srcByteCounter + 4;
                       
                       
                        int r = (rgb & header.redMask) >> redShiftValue, g = (rgb & header.greenMask) >> greenShiftValue, b = (rgb & header.blueMask) >> blueShiftValue, a = (rgb & header.alphaMask) >> alphaShiftValue;

                        pixelArrayOut[destByteCounter] = b;
                        destByteCounter = destByteCounter + 1;
                        pixelArrayOut[destByteCounter] = g;
                        destByteCounter = destByteCounter + 1;
                        pixelArrayOut[destByteCounter] = r;
                        destByteCounter = destByteCounter + 1;
                        pixelArrayOut[destByteCounter] = a;
                        destByteCounter = destByteCounter + 1;

                        x = x + 4;     
                }
                y = y + 1;
        }       
}

int findTrailingZeros(int val) {
        if (val == 0) {
                return 0;
        }

        int retVal = 0, bitCounter = 1;

        while ((val & bitCounter) == 0) {
                retVal = retVal + 1;
                bitCounter =  bitCounter << 1;
        }

        return retVal;
}
Tony




PostPosted: Mon Feb 25, 2013 3:28 pm   Post subject: RE:Writing my own programming language

what's not clear from the description is the motivation behind the project. Why not (and there are legitimate reasons) implement something with a well established spec, such as Java?
Latest from compsci.ca/blog: Tony's programming blog. DWITE - a programming contest.
Prads




PostPosted: Tue Feb 26, 2013 2:38 am   Post subject: Re: Writing my own programming language

Thanks Zren for fixing up the links!

Well, the real motivation of this project was to see if I could make my own programming language. I knew how compiler works and how low level stuffs work, and I just wanted to take a challenge to implement it. It started out as hobby, but then it got serious. I started to think if it can be used by other people in some way.

I just want to create something that homebrew game developers can use. Like once they write code and compile it, they will never have to rewrite or recompile it. The binary that was generated will work for most platform.
I want to port my VM to most of the modern devices we use. I just want to see how it evolves.

Also, I am going to enroll for bachelor in computer science next year, and I hope creating all these software will give me a head start.
Tony




PostPosted: Tue Feb 26, 2013 3:03 am   Post subject: Re: Writing my own programming language

Prads @ Tue Feb 26, 2013 2:38 am wrote:
Also, I am going to enroll for bachelor in computer science next year, and I hope creating all these software will give me a head start.

Realistically, you'll be well ahead in many aspects. Implementing a Java compiler is basically 4th year material for those that dare to take up the challenge.
Latest from compsci.ca/blog: Tony's programming blog. DWITE - a programming contest.
Prads




PostPosted: Tue Feb 26, 2013 3:17 am   Post subject: Re: Writing my own programming language

Tony @ Tue Feb 26, 2013 3:03 am wrote:
Prads @ Tue Feb 26, 2013 2:38 am wrote:
Also, I am going to enroll for bachelor in computer science next year, and I hope creating all these software will give me a head start.

Realistically, you'll be well ahead in many aspects. Implementing a Java compiler is basically 4th year material for those that dare to take up the challenge.


Cool! Very Happy That's good to know.
Display posts from previous:   
   Index -> General Programming
View previous topic Tell A FriendPrintable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic

Page 1 of 1  [ 6 Posts ]
Jump to:   


Style:  
Search: