Developing a natively compiled language

Programming, for all ages and all languages.
Post Reply
thre3dee
Member
Member
Posts: 34
Joined: Mon Nov 03, 2008 7:42 pm

Developing a natively compiled language

Post by thre3dee »

Hi,

I've been designing a new programming language (mostly as a hobby, nothing to take the world by storm :P) based mostly on C++ and C# (with some other language features) called N. The language will be designed as to be compilable to native machine code however I have one question about such languages.

How would one provide system interaction to the language? Would it require a platform invocation feature to call system DLL functions that perform useful hardware interaction such as file I/O?

What I mean is that, essentially, C allows access to memory addresses etc but other than that there is no real 'hardware' interfacing features in C other than using inline assembly.

For example something like this? (no conversion from char [] ref needed as when compiled they are the same as char* or char[])
A ref is a mutable C++ like reference.

Code: Select all

alias int ref HWND;
alias const char [] ref LPCSTR;
platform ("user32.dll") HWND FindWindowA (LPCSTR lpszClassName, LPCSTR lpszWindowName);

public static int Main (int argc, char [][] ref argv)
{
	for (int i = 0; i < argc; i++)
	{
		PrintFormat("Argument %d: %s\n", i, argv[i]);
	}
	HWND hWnd = FindWindowA(null, "My Computer");
	if (hWnd != null) {
		// stuff
	}
	return 0;
}
Does D simply invoke the C APIs such as <stdio.h>?

I was also considering first developing it as a virtual machine language like Lua etc to be used as a scripting language.
CodeCat
Member
Member
Posts: 158
Joined: Tue Sep 23, 2008 1:45 pm
Location: Eindhoven, Netherlands

Re: Developing a natively compiled language

Post by CodeCat »

Languages such as C have additional library support. When a call to a library function is made, it acts like any other function call. Eventually however, the library will need to actually make a request to the operating system. This occurs through a system call, which is different per OS. I believe Linux libraries trigger system calls directly through SYSCALL or INT. I think Windows NT uses a bunch of standardised userspace DLLs which contain a basic system call interface. The actual system call (presumably also using SYSCALL or INT) is then made inside the DLL, and the DLL is supplied with Windows.
thre3dee
Member
Member
Posts: 34
Joined: Mon Nov 03, 2008 7:42 pm

Re: Developing a natively compiled language

Post by thre3dee »

So if I went down the path of a natively compiling language, I should look into developing a runtime for each supported operating system?
User avatar
Troy Martin
Member
Member
Posts: 1686
Joined: Fri Apr 18, 2008 4:40 pm
Location: Langley, Vancouver, BC, Canada
Contact:

Re: Developing a natively compiled language

Post by Troy Martin »

Yep. That works best.
Image
Image
Solar wrote:It keeps stunning me how friendly we - as a community - are towards people who start programming "their first OS" who don't even have a solid understanding of pointers, their compiler, or how a OS is structured.
I wish I could add more tex
thre3dee
Member
Member
Posts: 34
Joined: Mon Nov 03, 2008 7:42 pm

Re: Developing a natively compiled language

Post by thre3dee »

I hope its easier than it sounds :P

First I have to write a language specification as its just loose examples at the moment.

What I intend out of this language (maybe I can call it C³ or C-cubed since its primarily a C family based language and every other postfix is taken haha) is to bring higher-than-C++ level features and a bit more safety to C++ while maintaining the high-performance machine code nature of C++.

Below are the main features compared to C++ I've designed:
  • No pointers persay. The reference and pointer mechanisms in C++ are now replaced with a mutable reference mechanism. They are merely pointers that can only be assigned null or the address of a valid object. To reassign the reference use the := operator. A reference to a type is specified by the 'ref' keyword. For example:

    Code: Select all

    char [] ref str = new char[]{ "Hello" }; // six characters including null terminator
    char ref ch = new char('A');
    reclaim str;
    reclaim ch;
  • A non-array reference cannot be accessed as an array. A char ref can only reference a single character. To reference an array one must use the square brackets: char [] ref. Memory is reclaimed using a single delete operator called reclaim - there is no array delete.
  • There is no requirement for declaration and definitions to be in separate files.
  • C³ will support generic/template classes via a slightly different declaration syntax:

    Code: Select all

    generic (K, T) class map
    {
    }
    
    map(string, int) ref myStringMap = new map(string, int)(); 
  • Interfaces are supported but there is only single class inheritance. Class/interface inheritance uses a slightly different syntax:

    Code: Select all

    // .NET like example
    class String (System.Object, IComparable, IClonable)
    {
    }
  • Functions that have no return type and/or no parameters do not require placeholders. As well, constructors that have only initialisers and no function body do not require a body. Constructors and destructors also use slightly different syntax:

    Code: Select all

    class String
    {
    	private char[] ref Buffer;
    	private ulong Length;
    	
    	// no empty parameter list required
    	public constructor
    		: Buffer = new char[1] { '\0' }
    		: Length = 0;	// no function body due to semi-colon after last initialiser
    		
    	public destructor
    	{
    		reclaim Buffer;
    	}
    		
    	// no return type of parameter list required
    	public Trim
    	{
    		// ...
    	}
    }
  • All arrays have an embedded count mechanism. This removes the requirement of accompanying 'capacity' variables, allows any code to find the length of an array (in elements not bytes) and also gives rise to the foreach keyword for native arrays. For example:

    Code: Select all

    int[] ref numbers = new int[10] { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 }; // in memory this is [10][1,2,3,4,5,6,7,8,9,10]
    
    // countof accesses this embedded array count
    printf("array length: %d\n", countof(numbers));
    
    // foreach uses this embedded array count (can be overridden in classes to provide foreach support)
    foreach(int i in numbers)
    {
    	printf("%d,", i);
    }
    Output:
    array length: 10
    1,2,3,4,5,6,7,8,9,10

    Code: Select all

    // foreach also allows conditional continuation
    foreach(int i in numbers while i <= 5)
    {
    	printf("%d <= 5\n", i);
    }
    
    Output:
    1 <= 5
    2 <= 5
    3 <= 5
    4 <= 5
    5 <= 5
    To implement a class overload of foreach, one implements the following two methods:

    Code: Select all

    class array
    {
    	private T[] ref buffer;
    	
    	// class foreach specifies the count
    	// const tells the compiler the count won't change during the foreach { }
    	public foreach const
    	{
    		return countof(buffer);
    	}
    
    	// foreach uses the [ulong index] operator on its array operand
    	public T ref operator [] (ulong idx)
    	{
    		return buffer[idx];
    	}
    }
  • Classes now have access to properties as used in languages like C#. There are six keywords that relate to properties: property, readonly, writeonly, get, set, value. For example:

    Code: Select all

    // only get() is public, set() is private
    readonly property ulong myNumber {
    	get { return myNumber; }
    }
    
    // only set() is public, get() is private
    writeonly property ulong myNumber {
    	set { myNumber = value; }
    }
    
    // example of property use in a string class
    readonly property ulong length {
    	get {
    		// get length of C-style string
    		ulong len = 0;
    		foreach(char c in buffer while c != '\0')
    			++len;
    		return len;
    	}
    }
    
    // length.get() implicitly called
    ulong strlen = myString.length;
That's about all the features I can think of at the moment. Be good to hear your thoughts about such improvements/changes.
DeletedAccount
Member
Member
Posts: 566
Joined: Tue Jun 20, 2006 9:17 am

Re: Developing a natively compiled language

Post by DeletedAccount »

Hi,
I aplogize for my rudeness.
Regards
Sandeep
Last edited by DeletedAccount on Thu Nov 13, 2008 11:09 pm, edited 1 time in total.
User avatar
JamesM
Member
Member
Posts: 2935
Joined: Tue Jul 10, 2007 5:27 am
Location: York, United Kingdom
Contact:

Re: Developing a natively compiled language

Post by JamesM »

SandeepMathew wrote:Hi,
I am sorry if sound a bit harsh .But my gut feeling is that you have not done enough research from your side.Writing "language features" is not the right starting point for actually writing a complier. The effort is not likely to lead anywhere .The resource compilers is the Dragon Book.

Regards
Sandeep
You are being too harsh. Design is the important part - any monkey can write a compiler. Yes they're complex things, but so are operating systems, and we have sub-15 year olds on this forum making just that.

Before you counterargue, I've worked for a company that do dynamic binary translation - i.e. the backend of compilers. I've also worked with parsers and done university courses on both, so I'm not 100% ignorant.

As to the OP - the language looks very nice. I was at one point thinking of doing just what you have, albeit without some of the .NET-isms - the main thing for me would be to allow compile time and runtime pre and post conditions to functions, as well as specifying invariants, making debugging easier (and also sticking to specification easier too).

Very nice, I'll keep an eye on this thread :)

James

PS:
Writing "language features" is not the right starting point for actually writing a complier.
Sounds like you advocate aggressive programming sans design, there. Did you really intend to say that?
DeletedAccount
Member
Member
Posts: 566
Joined: Tue Jun 20, 2006 9:17 am

Re: Developing a natively compiled language

Post by DeletedAccount »

Hi,
My intention was not to drive them away . I felt that they are picking up features randomly from existing languages and said that this will be my language , This I felt is not the way . I do not mean to discorage anyone . Writing a compiler , at least the front end is more or less mechanical these days and not at all complex .Writing a good backend is doctoral thesis stuff :)

I do not have any experience with professional compiler development or os development , Mostly sustained development and minor features are only done here in India , I have hardly 3 1/2 months of industry exposure. May be I have sounded a bit rude, I am sorry :(,that was not my intention .

Btw age is not an barrier for anything , there are 15 yrs and sub fifteen years who are more competent than I am , I have lots of respect for the inflaters , prianha's , alex'es . lukem's and other's in the forumn. I am nearly 21 and I could not have done such things at their age [ :oops: ].

James ,thanks for pointing out , I will be careful on subsequent posts.

Regards
Sandeep
thre3dee
Member
Member
Posts: 34
Joined: Mon Nov 03, 2008 7:42 pm

Re: Developing a natively compiled language

Post by thre3dee »

Thanks for your comments. My goal is not to redesign C++ or C# as my original post already explained. It was to bring together some of the most useful constructs and semantics of the entire C family of languages as well reduce some areas of syntax to increase coding efficiency. But the main goal in bringing these together was to maintain the lower level, natively compiled high-performance nature of C and C++.

What I mean is that when I program C code, I know fairly well what's going on underneath. I don't have to worry about magic invisible garbage collectors or features that require the language to be run in a virtual machine like VB and a lot .NET programs (excluding JIT).
DeletedAccount
Member
Member
Posts: 566
Joined: Tue Jun 20, 2006 9:17 am

Re: Developing a natively compiled language

Post by DeletedAccount »

Hi ,
hmmm .... gcj The gnu java compiler does the same i think , isnt it ?? . It compiles into native code ,so does the Bartok C# compiler from Microsoft . I am not supposed to leak out so much , But take a look at Pheonix , you might be able to develop your compiler faster than you think . Please check out: [url]research.microsoft.com/Phoenix/[/url].Please let me know if you did find Phoenix useful in your project. I was tinkering around with the rotor source code recently , you might also find rotor very interesting,

Regards
Sandeep
thre3dee
Member
Member
Posts: 34
Joined: Mon Nov 03, 2008 7:42 pm

Re: Developing a natively compiled language

Post by thre3dee »

I think I might start this as a embeddable programming language as a C or C++ library. Its enough work to make a scripting language API (which I've attempted a few times), but to be able to generate native machine code executables is probably a bit more work than I'd prefer at the moment. For the moment, I'll call it exC (embeddable, extended-C).

To produce a virtual machine running the language would be a big step in developing the language.
thre3dee
Member
Member
Posts: 34
Joined: Mon Nov 03, 2008 7:42 pm

Re: Developing a natively compiled language

Post by thre3dee »

I'm making some progress on the API. I'm developing it in MSVC Express 2008 however I've turned off language extensions and will only use ANSI C++ system/template code for portability. Oh and I've renamed it back to N (the original name).

My current progress dictates this kind of use in an application:

Code: Select all

N::SetMemory(myAlloc, myFree);
N::VirtualMachine *vm = 0;
N::Result result = N::VirtualMachine::Create(vm); // N::VersionNum is a default parameter and is checked

if (N::Failed(result) == false) {
	N::Assembly *assembly = 0;
	result = vm->CompileFile("example.n", assembly); // uses *& for safety
	if (N::Failed(result) == false) {
		// call a script function or something from the assembly
		assembly->Release();
	}
	vm->Release();
}
Crazed123
Member
Member
Posts: 248
Joined: Thu Oct 21, 2004 11:00 pm

Re: Developing a natively compiled language

Post by Crazed123 »

Post Reply