Re: Self hosting OS/Compiler from scratch
Posted: Mon Jan 27, 2014 5:47 am
Hi,
Kevin decided to throw an "alien" language at me, to see how easy/hard it is for someone that's never seen the language before to try to read/understand. I was thinking of showing an "alien" language of my own to see how easily people can read that, but got lazy and didn't. Now Combuster thinks I only ever use assembly and I'm uncomfortable with anything else.
So, here's an example of an "alien" language; that does (what I think) Kevin's sorting example does. Please note that there are no generics and no pre-processing. It's 3 separate pieces of code - you can think of them as 3 separate files if you like.
Integers:
Strings:
Structures:
The nice thing here is that each piece is easy to understand (not splattered everywhere, or buried under excess boilerplate, or complicated by the need to handle many different data types). More importantly, it's easy to modify. For example, you could look at the integer sorting code and decide to do something like this to it:
If you think you can do something like this with generics, you're wrong. If you think assembly isn't that important, then perhaps I should point out that (even for my "simple" language) I'm expecting it to take 20 years before my native compiler is able to match the code quality produced by LLVM and GCC (partly because I need to write a disposable/initial compiler before I can write an OS, before I can write the native compiler, before I can write the IDE that the language is designed for, before I can begin writing code optimisers for the native compiler).
So, generics. They suck, I don't want them, I've never wanted them, and I don't want anyone using my language or writing software for my OS to use them either; and if I really loved generics I probably couldn't afford the development time they'd cost to support anyway.
As an alternative, people can use my language and compiler to write utilities that generate source code. In this case; beginners don't need to learn anything extra; people can easy check the "software generated source code" for debugging, etc; I won't need to spend a few extra years trying to support complex puke; and it's far more powerful than generics in any language ever will be.
Cheers,
Brendan
Kevin decided to throw an "alien" language at me, to see how easy/hard it is for someone that's never seen the language before to try to read/understand. I was thinking of showing an "alien" language of my own to see how easily people can read that, but got lazy and didn't. Now Combuster thinks I only ever use assembly and I'm uncomfortable with anything else.
So, here's an example of an "alien" language; that does (what I think) Kevin's sorting example does. Please note that there are no generics and no pre-processing. It's 3 separate pieces of code - you can think of them as 3 separate files if you like.
Integers:
Code: Select all
function sortIntegers (u32 @array, range 1 to (ums.max/u32.size) items) (void) {
auto src
auto temp
while(items > 0) {
src = 1;
while(src < items) {
if( @array > @(array + src) ) {
temp = @array
@array = @(array + src)
@(array + src) = temp
}
src = src + 1
}
array = array + 1
item = item - 1
}
}
Code: Select all
functiondef stringComparator = (u32 @first, u32 @second) (range -1 to 1)
function sortStrings (u32 @@array, range 1 to (ums.max/(u32 @).size) items, stringComparator comparator) (void) {
auto src
auto temp
while(items > 0) {
src = 1;
while(src < items) {
if( comparator(@array, @(array + src)) < 0 ) {
temp = @array
@array = @(array + src)
@(array + src) = temp
}
src = src + 1
}
array = array + 1
item = item - 1
}
}
Code: Select all
typedef TestStruct = {
u8 a
u8 b
}
function sortStructures (TestStruct @array, range 1 to (ums.max/TestStruct.size) items) (void) {
auto src
TestStruct temp
while(items > 0) {
src = 1;
while(src < items) {
if( @array.a + @array.b > @(array + src).a + @(array + src).b ) {
temp.a = @array.a
temp.b = @array.b
@array.a = @(array + src).a
@array.b = @(array + src).b
@(array + src).a = temp.a
@(array + src).b = temp.b
}
src = src + 1
}
array = array + 1
item = item - 1
}
}
Code: Select all
multitarget sortIntegers (u32 @array, range 1 to (ums.max/u32.size) items) (void) {
function sortIntegers (u32 @array, range 1 to (ums.max/u32.size) items) (void) {
auto src
auto temp
while(items > 0) {
src = 1;
while(src < items) {
if( @array > @(array + src) ) {
temp = @array
@array = @(array + src)
@(array + src) = temp
}
src = src + 1
}
array = array + 1
item = item - 1
}
}
asmfunction x86_32 sortIntegers (u32 @array in edi, range 1 to (ums.max/u32.size) items in edx) (void) {
l1:
mov ecx,edx-1
mov esi,edi+4
l2:
mov eax,[esi]
cmp eax,[edi]
jae l3
mov ebx,[edi]
mov [edi],eax
mov [esi],ebx
l3:
add esi,4
loop .l2
add edi,4
sub edx,1
jne l1
}
}
So, generics. They suck, I don't want them, I've never wanted them, and I don't want anyone using my language or writing software for my OS to use them either; and if I really loved generics I probably couldn't afford the development time they'd cost to support anyway.
As an alternative, people can use my language and compiler to write utilities that generate source code. In this case; beginners don't need to learn anything extra; people can easy check the "software generated source code" for debugging, etc; I won't need to spend a few extra years trying to support complex puke; and it's far more powerful than generics in any language ever will be.
Cheers,
Brendan