Truly independent programming language
Truly independent programming language
Is there a programming language that has been invented that is truly independent? Maybe that isn't the right word, but for example in C# you have:
Int i = 0;
Int is a data type, is there a language where the data type's are actually written and defined in the language itself? So like Int would be defined and all of the functions like Int.ToString() are created within the language, that way the compiler may not need to know what Int is but it's a number that is four bytes.
I was just curious if that has ever been done before.
Thanks,
Matt
Int i = 0;
Int is a data type, is there a language where the data type's are actually written and defined in the language itself? So like Int would be defined and all of the functions like Int.ToString() are created within the language, that way the compiler may not need to know what Int is but it's a number that is four bytes.
I was just curious if that has ever been done before.
Thanks,
Matt
Re: Truly independent programming language
I don't really understand your question. All languages define their data types (they may be similar to, or even identical to, the definitions used in another language, but there's only so many ways of defining essentially the same thing). There are some languages (Eiffel is an example) where all data types are defined as classes; that's different to C#.
Re: Truly independent programming language
If you mean by "independent" architecture independence and the lack of primitives, then the language I've designed and use for my OS matches your criteria. It has only two built-in operator-less "data types", void and byte, the latter merely allocates one byte of memory and nothing more. All the others, like char, int, float etc. implemented in the standard library, and are out of the scope of the language itself. There's a cheat though, my language basically is a dialect of C, but allows assembly mode. It is necessary to implement primitive types, because no operator exists by default to work with (not to mention performance issues).
Example:
Unlike C, my language uses the keyword "typedef" for instanceable classes. The "inline" keyword tells the compiler to use the method as a macro instead of compiling a "call" for it. The codeblock directive turns on assembly mode (and specifies assembler, address space width and page size in bits). The compiler outputs asm source that can be assembled with a crafted macro assembler. Each assembler has it's own include file, the clbaseof mnemonic is a macro defined in this file (fasm.inc in this case).
Non assembly sources (without codeblock directive) will be compiled to an intermediate bytecode object file.
If you want to port it to a new architecture, you'll need a
a) macro assembler for that architecture,
b) an include file for it with specific, well-defined macros,
c) and to port it's stdlib (strictly speaking this is not necessary from the language point of view, but you'll need the types in it anyway).
Once these are ready, you can compile other sources without any modifications. The binary bytecode object files also can be used as is, they will be translated to an assembly source (containing macro invocations only, no direct mnemonics), and assembled to native code with the help of the fore-mentioned include file.
This may seem strange for the first glance, but makes the language, the produced binaries as well as the assembly outputs truly architecture and class independent, which is what I think you were asking.
Example:
Code: Select all
#codeblock "fasm", "x86_64", 48, 12
typedef int {
byte[8] val; //allocate 8 bytes for instance
public inline int +(int val2) {
clbaseof rax, this.val //get offset of instance
add qword [rax], r8 //first argument passed in r8
}
//...
}
Unlike C, my language uses the keyword "typedef" for instanceable classes. The "inline" keyword tells the compiler to use the method as a macro instead of compiling a "call" for it. The codeblock directive turns on assembly mode (and specifies assembler, address space width and page size in bits). The compiler outputs asm source that can be assembled with a crafted macro assembler. Each assembler has it's own include file, the clbaseof mnemonic is a macro defined in this file (fasm.inc in this case).
Non assembly sources (without codeblock directive) will be compiled to an intermediate bytecode object file.
Code: Select all
class app.helloworld {
string hello = "Hello "; //class variable (global among threads)
public int main(string args[]){
string world = "World and "; //local variable (thread private)
lib.ui.printf(hello + world + args[0] + "!\n");
return(true);
}
}
a) macro assembler for that architecture,
b) an include file for it with specific, well-defined macros,
c) and to port it's stdlib (strictly speaking this is not necessary from the language point of view, but you'll need the types in it anyway).
Once these are ready, you can compile other sources without any modifications. The binary bytecode object files also can be used as is, they will be translated to an assembly source (containing macro invocations only, no direct mnemonics), and assembled to native code with the help of the fore-mentioned include file.
This may seem strange for the first glance, but makes the language, the produced binaries as well as the assembly outputs truly architecture and class independent, which is what I think you were asking.
Re: Truly independent programming language
Ah yes! This is what I was looking for. See let me explain further. I am developing a language for my operating system and kernel to be written in. See I want it to be powerful and very expandable. For example I want to be able to define an Integer, make the functions for it and everything. This way I don't have to do any compiler hacks or anything, like I don't want to per say pre program the data types into the compiler, I think the language should be versatile enough to where I specify the data types.turdus wrote:If you mean by "independent" architecture independence and the lack of primitives, then the language I've designed and use for my OS matches your criteria. It has only two built-in operator-less "data types", void and byte, the latter merely allocates one byte of memory and nothing more. All the others, like char, int, float etc. implemented in the standard library, and are out of the scope of the language itself. There's a cheat though, my language basically is a dialect of C, but allows assembly mode. It is necessary to implement primitive types, because no operator exists by default to work with (not to mention performance issues).
Example:Code: Select all
#codeblock "fasm", "x86_64", 48, 12 typedef int { byte[8] val; //allocate 8 bytes for instance public inline int +(int val2) { clbaseof rax, this.val //get offset of instance add qword [rax], r8 //first argument passed in r8 } //... }
Unlike C, my language uses the keyword "typedef" for instanceable classes. The "inline" keyword tells the compiler to use the method as a macro instead of compiling a "call" for it. The codeblock directive turns on assembly mode (and specifies assembler, address space width and page size in bits). The compiler outputs asm source that can be assembled with a crafted macro assembler. Each assembler has it's own include file, the clbaseof mnemonic is a macro defined in this file (fasm.inc in this case).
Non assembly sources (without codeblock directive) will be compiled to an intermediate bytecode object file.If you want to port it to a new architecture, you'll need aCode: Select all
class app.helloworld { string hello = "Hello "; //class variable (global among threads) public int main(string args[]){ string world = "World and "; //local variable (thread private) lib.ui.printf(hello + world + args[0] + "!\n"); return(true); } }
a) macro assembler for that architecture,
b) an include file for it with specific, well-defined macros,
c) and to port it's stdlib (strictly speaking this is not necessary from the language point of view, but you'll need the types in it anyway).
Once these are ready, you can compile other sources without any modifications. The binary bytecode object files also can be used as is, they will be translated to an assembly source (containing macro invocations only, no direct mnemonics), and assembled to native code with the help of the fore-mentioned include file.
This may seem strange for the first glance, but makes the language, the produced binaries as well as the assembly outputs truly architecture and class independent, which is what I think you were asking.
Might sound slightly crazy, but I think it would allow me to write a better operating system.
Thanks, Matt
Re: Truly independent programming language
Maybe independent isn't the word but flexible. Using pascal you can define a type name and its size in bytes by a very fashion way. That's the single language I know you can do that with compiler support. C-like languages usually doesn't allow that but you can get close using C... maybe creating a struct with #pragma pack(1) so that you can create a "type" with the exact size you're looking for just adding native data types until the sum of all them is the size you're looking for. Below an example to how create a type name called myType of 3-bytes size on 32-bit machine. We use #pragma pack(1) which is not standard but most C compilers doesn't support it, including MSC(IIFC) and gcc. I used clang. What differ from each one other is basically the name of extension. Just look at how they call it on documentation.PearOs wrote:Is there a programming language that has been invented that is truly independent? Maybe that isn't the right word, but for example in C# you have:
Int i = 0;
Int is a data type, is there a language where the data type's are actually written and defined in the language itself? So like Int would be defined and all of the functions like Int.ToString() are created within the language, that way the compiler may not need to know what Int is but it's a number that is four bytes.
I was just curious if that has ever been done before.
Thanks,
Matt
Code: Select all
#pragma pack(1) // ensure 1-byte align and no extra padding added by compiler.
// this is our type definition
struct myTypeTag
{
short a;
char b;
// unnammed members are allowed too, so you can add needed padding.
// int; // it works.
};
typedef struct myTypeTag myType;
#pragma pack()
int main()
{
myType a; // declare our type
char s[] = "FO\0BAA"; // copy source
memcpy(&a, &s, sizeof(myType)); // copy 3 bytes: 'F', 'O' and '\0' (need for printf())
printf("%s\n", (char*) &a); // print it
return 0;
}
Code: Select all
mytype<destionation_type> a = "FO\0BAA";
Code: Select all
#pragma pack(1)
template <typename T>
struct myType
{
short a;
char b;
myType() { }
myType(T v)
{
memcpy(this, v, sizeof(myType));
}
myType& operator=(T v)
{
memcpy(this, v, sizeof(myType));
return *this;
}
};
#pragma pack()
int main()
{
char s[] = "FO\0BAA";
myType<char[sizeof(s)]> a = s;
printf("%s\n", (char*) &a); // output: FO
}
BTW, before you say no to C++ in your operating system, remember you don't need to use all C++ features also struct with methods and operator overloading. All the other stuff you do in C you can do with C++ too.
Re: Truly independent programming language
Thank you for the information. That is very interesting. I would like to not use C++ or C if I can for a simple reason. My whole operating system and kernel, every version before this one used C# as the programming language with a custom compiler. Which worked and was fine, but Microsoft owns C# and I don't want to write a full blown C# compiler so that I can compile the project while its running and make it platform independent. C and C++ have its advantages but everyone uses it. I think it would be neat to make a language like it, that maybe had some new optimization tricks or methods that allowed me a little more control over the flow of my system.ASMMan wrote:Maybe independent isn't the word but flexible. Using pascal you can define a type name and its size in bytes by a very fashion way. That's the single language I know you can do that with compiler support. C-like languages usually doesn't allow that but you can get close using C... maybe creating a struct with #pragma pack(1) so that you can create a "type" with the exact size you're looking for just adding native data types until the sum of all them is the size you're looking for. Below an example to how create a type name called myType of 3-bytes size on 32-bit machine. We use #pragma pack(1) which is not standard but most C compilers doesn't support it, including MSC(IIFC) and gcc. I used clang. What differ from each one other is basically the name of extension. Just look at how they call it on documentation.PearOs wrote:Is there a programming language that has been invented that is truly independent? Maybe that isn't the right word, but for example in C# you have:
Int i = 0;
Int is a data type, is there a language where the data type's are actually written and defined in the language itself? So like Int would be defined and all of the functions like Int.ToString() are created within the language, that way the compiler may not need to know what Int is but it's a number that is four bytes.
I was just curious if that has ever been done before.
Thanks,
MattI think you will find this lot of memcpy() calls tedious. You aren't probably using C++ to your kernel, isn't? but in the case you're, you could just make the following work with C++:Code: Select all
#pragma pack(1) // ensure 1-byte align and no extra padding added by compiler. // this is our type definition struct myTypeTag { short a; char b; // unnammed members are allowed too, so you can add needed padding. // int; // it works. }; typedef struct myTypeTag myType; #pragma pack() int main() { myType a; // declare our type char s[] = "FO\0BAA"; // copy source memcpy(&a, &s, sizeof(myType)); // copy 3 bytes: 'F', 'O' and '\0' (need for printf()) printf("%s\n", (char*) &a); // print it return 0; }
And copy 3 bytes from this string literal in put in a location. Here's the full code:Code: Select all
mytype<destionation_type> a = "FO\0BAA";
In fact, I still think that type <dest_type> is less tedius and error-prone than a lot of memcpy() calls. You still can use all operators to work with his new data type =,+,-,++,-- etc. Also, I'm new to C+ but most probably there's a way to even you omit dest_type. I hope it helps.Code: Select all
#pragma pack(1) template <typename T> struct myType { short a; char b; myType() { } myType(T v) { memcpy(this, v, sizeof(myType)); } myType& operator=(T v) { memcpy(this, v, sizeof(myType)); return *this; } }; #pragma pack() int main() { char s[] = "FO\0BAA"; myType<char[sizeof(s)]> a = s; printf("%s\n", (char*) &a); // output: FO }
BTW, before you say no to C++ in your operating system, remember you don't need to use all C++ features also struct with methods and operator overloading. All the other stuff you do in C you can do with C++ too.
That's kind of why I am wondering if I should go this route, plus if I do it right. I will have to write a compiler for the language anyways, which means porting it is going to be fairly easy.
Thanks,
Matt
Last edited by PearOs on Tue Apr 15, 2014 11:18 pm, edited 1 time in total.
Re: Truly independent programming language
VHDL and Verilog come to my mind. Ultimately, a CPU is a set of interconnected logic gates. It's up to you how many bits (bytes) to have in the machine word, registers, buses, etc when you implement a CPU.
Re: Truly independent programming language
Using C# is amazing. It's one of my favorite languages. I thought you was using assembly or something very close to low-level and didn't want hear about C++ on our kernel. Don't worry about Microsoft owns C#. Things changed a lot at Microsoft. They recently open source a lot of .NET compilers. Including C#, VB.NET and F#. The open ECMA specification on C# 5 language is on the way. Microsoft works together with Mono guys. Founded the .NET foundation. Also, are building a universal platform. You write in C# and run everywhere. I've hear also will have native code generation to .NET languages. So, you will even can use C# as a system language. So, if I were you I would switch back to C# language for the kernel development. equal-or-better than this I think D language only. C and C++ are a world's pain, seriously.PearOs wrote:
Thank you for the information. That is very interesting. I would like to not use C++ or C if I can for a simple reason. My whole operating system and kernel, every version before this one used C# as the programming language with a custom compiler. Which worked and was fine, but Microsoft owns C# and I don't want to write a full blown C# compiler so that I can compile the project while its running and make it platform independent.
I know what you feel. I was wondering a while ago to build a new one from scratch. Actually, I even did something. But It's a big deal. And too many time and energy consuming. Requires a lot of baggage to create something useful. A language only to meet my needs wasn't going to be much useful to someone else. Also that, there's a lot of year on research of optimization compilers (which I care a lot and why I was writing my own language also try to don't have UB(Undefined behavior) like does have C/C++). So, I left language designer to people are already working in that. It was when I found D language (I did used C# too but I forget it because I didn't have a native compiler, back then I was trying to get off C and C++ world's pain). Also, don't forget to check out D language (https://dlang.org/) very promissing. If you have a suggest to change something in the language, you can go to http://forum.dlang.org/ and ask it in there in discuss the designer with a lot of active users(including language creator Walter Bright) and if it's considered a real feature, create a DIP and it will be in the next releases of the language. Also, I'm not saying you can't write a successfully language.PearOs wrote: C and C++ have its advantages but everyone uses it.I think it would be neat to make a language like it, that maybe had some new optimization tricks or methods that allowed me a little more control over the flow of my system.
That's kind of why I am wondering if I should go this route, plus if I do it right. I will have to write a compiler for the language anyways, which means porting it is going to be fairly easy.
Just saying how was my experience on it. There are already good alternatives to C and C++ I could use it and give the free time to do another good stuff.
Re: Truly independent programming language
Yes, first order predicate logic. The Horn clause fragment of it can be given a computational interpretation. Here's simple integer arithmetic in Horn clauses (a '%' starts a comment which continues to the end of the line):PearOs wrote:Is there a programming language that has been invented that is truly independent?
Code: Select all
%%%
%%% plus(A, B, C).
%%%
%%% Succeeds if A+B=C
%%%
plus(A, 0, A). % A+0=A
plus(A, s(B), s(C)) :- % A+(B+1)=(A+B)+1
plus(A, B, C).
%%%
%%% multiply(A, B, C).
%%%
%%% Succeeds if A*B=C
%%%
multiply(_, 0, 0). % A*0=0
multiply(A, s(0), A). % A*1=A
multiply(A, s(B), C) :- % A*(B+1)=(A*B)+A
multiply(A, B, Temp),
plus(A, Temp, C).
Some computations with the above clauses:
First, 1+2=?:
Code: Select all
| ?- plus(s(0), s(s(0)), Result).
Code: Select all
Result = s(s(s(0))) ?
Code: Select all
?- multiply(s(s(0)), s(s(s(0))), Result).
Code: Select all
Result = s(s(s(s(s(s(0)))))) ?
Code: Select all
| ?- multiply(X, s(s(0)), s(s(s(s(0))))).
Code: Select all
X = s(s(0)) ?
Code: Select all
| ?- plus(X,0,Result).
Code: Select all
X = _699552
Result = _699552 ?
The source code for an interpreter which can execute this type of language is to be found here.
Every universe of discourse has its logical structure --- S. K. Langer.
Re: Truly independent programming language
Thank you so much for the reply. I did not know C# was becoming Open Source. Alright now that I know this I will just continue using C# since its already there and working for my needs. I appreciate the reply.ASMMan wrote:Using C# is amazing. It's one of my favorite languages. I thought you was using assembly or something very close to low-level and didn't want hear about C++ on our kernel. Don't worry about Microsoft owns C#. Things changed a lot at Microsoft. They recently open source a lot of .NET compilers. Including C#, VB.NET and F#. The open ECMA specification on C# 5 language is on the way. Microsoft works together with Mono guys. Founded the .NET foundation. Also, are building a universal platform. You write in C# and run everywhere. I've hear also will have native code generation to .NET languages. So, you will even can use C# as a system language. So, if I were you I would switch back to C# language for the kernel development. equal-or-better than this I think D language only. C and C++ are a world's pain, seriously.PearOs wrote:
Thank you for the information. That is very interesting. I would like to not use C++ or C if I can for a simple reason. My whole operating system and kernel, every version before this one used C# as the programming language with a custom compiler. Which worked and was fine, but Microsoft owns C# and I don't want to write a full blown C# compiler so that I can compile the project while its running and make it platform independent.
I know what you feel. I was wondering a while ago to build a new one from scratch. Actually, I even did something. But It's a big deal. And too many time and energy consuming. Requires a lot of baggage to create something useful. A language only to meet my needs wasn't going to be much useful to someone else. Also that, there's a lot of year on research of optimization compilers (which I care a lot and why I was writing my own language also try to don't have UB(Undefined behavior) like does have C/C++). So, I left language designer to people are already working in that. It was when I found D language (I did used C# too but I forget it because I didn't have a native compiler, back then I was trying to get off C and C++ world's pain). Also, don't forget to check out D language (https://dlang.org/) very promissing. If you have a suggest to change something in the language, you can go to http://forum.dlang.org/ and ask it in there in discuss the designer with a lot of active users(including language creator Walter Bright) and if it's considered a real feature, create a DIP and it will be in the next releases of the language. Also, I'm not saying you can't write a successfully language.PearOs wrote: C and C++ have its advantages but everyone uses it.I think it would be neat to make a language like it, that maybe had some new optimization tricks or methods that allowed me a little more control over the flow of my system.
That's kind of why I am wondering if I should go this route, plus if I do it right. I will have to write a compiler for the language anyways, which means porting it is going to be fairly easy.
Just saying how was my experience on it. There are already good alternatives to C and C++ I could use it and give the free time to do another good stuff.
Thanks,
Matt
Re: Truly independent programming language
You're welcome.PearOs wrote: Thank you so much for the reply.
I forget the links:PearOs wrote: I did not know C# was becoming Open Source.
https://roslyn.codeplex.com/SourceControl/latest
http://www.dotnetfoundation.org/
Also, just out curiosity, what native compiler are you using? Mono?