Advertising (This ad goes away for registered users. You can Login or Register)

Language Design Discussion

Discuss about your favorite (gaming...or not) devices here. The most popular ones will end up getting their own categories
Programming discussions for your favorite Device
Forum rules
Forum rule Nº 15 is strictly enforced in this subforum.
Acid_Snake
Retired Mod
Posts: 3100
Joined: Tue May 01, 2012 11:32 am
Location: Behind you!

Re: Language Design Discussion

Post by Acid_Snake »

Infinite Chalupas wrote: Just another cool thing to bring to the table, a lot of people don't know this but you can declare functions like this in C:

Code: Select all

    int main(argc, argv)
       int argc;
       char *argv[];
    {
       return 0;
    }
a few things I gotta say about this:
- it's ugly and highly unreadable
- normally, having more than one way to do standard stuff (like declaring a function) is something you want to avoid in your language, it's best to keep the one that better suits your language's vision
- just because C (or any other language) can do something, doesn't mean you have to do it too, unless it's a feature your language lacks and you truly want to have. This brings me back to the other two points: if it doesn't fit into your language's vision or it looks ugly, don't do it. Otherwise in the end you'll have an overly convoluted language that would mess developers up.
Advertising
Infinite Chalupas
Posts: 92
Joined: Thu Feb 20, 2014 9:00 pm

Re: Language Design Discussion

Post by Infinite Chalupas »

Acid_Snake wrote:a few things I gotta say about this:
- it's ugly and highly unreadable
- normally, having more than one way to do standard stuff (like declaring a function) is something you want to avoid in your language, it's best to keep the one that better suits your language's vision
- just because C (or any other language) can do something, doesn't mean you have to do it too, unless it's a feature your language lacks and you truly want to have. This brings me back to the other two points: if it doesn't fit into your language's vision or it looks ugly, don't do it. Otherwise in the end you'll have an overly convoluted language that would mess developers up.

I never planned on using it myself, I just wanted to point out that there was a solution to the "too many arguments on one line" issue that stems from how C was originally designed. It happens to be one of those obscure facts about the language, like how you can declare typedefs before structs, use function pointers to create vector tables, use goto to navigate complex loops resulting in faster code, use trigraphs like ??= or <: and :> instead of #, [, and ], or use colons when declaring the fields of structs to specify bitfields.


EDIT:
Just to further illustrate some of the things I was talking about, the following is in fact, a 100% legitimate C program. You'll need to add the -trigraphs option to compile though because they're disabled by default (despite being in the specification). It'll print the names of each argument passed to it that begins with the letter 'A'

Code: Select all

%:include <stdio.h>



typedef struct MyBitfields_s MyBitfields_t;


struct MyBitfields_s
{
	unsigned char thisStruct : 3;
	unsigned char usesBitfields : 1;
	unsigned char andIs1 : 2;
	unsigned char byteIn : 1;
	unsigned char size : 1;
};



int main(argc, argv)
	int argc;
	char *argv<::>;
<%
	// This is legit C code ??/
	printf(" and this line will not be executed.??/n");

	MyBitfields_t mbf;
	printf("Proof MyBitfields is 1 byte in size: %i??/n--??/n", sizeof(MyBitfields_t));

	int i;
	for (i = 1; i < argc; ++i)
	<%
		if (argv<:i:><:0:> == 'a' ??!??! argv<:i:><:0:> == 'A')
			printf("%s??/n", argv<:i:>);
	%>
	return 0;
%>
Advertising
Infinite Chalupas
Posts: 92
Joined: Thu Feb 20, 2014 9:00 pm

Re: Language Design Discussion

Post by Infinite Chalupas »

So revisiting the problem I brought up earlier about using non-object-oriented functions and variables from object-oriented code, I think I came up with a much better solution.

Basically, you have 3 main keywords that affect the way objects are linked.
  • internal - this states that the symbol for the object is linked internally, and is thus not visible outside of current binary
  • external - this states that the symbol for the object exists in an external binary.
  • floating - this states that the symbol for the object is 'floating', in other words it doesn't inherit the prefix of the current namespace and class.

Think of it this way, for the method MyNamespace.MyClass.MyMethod, the symbol created for it would be:
  • normal - _MyNamespace_MyClass_MyMethod
  • internal - <none>
  • external - _MyNamespace_MyClass_MyMethod
  • floating - _MyMethod
  • external floating - _MyMethod
Normal floating methods could be called directly by their name from functional code like C, and the user can map C functions to the current class by declaring them like:

Code: Select all

namespace System
{
	class Math is abstract
	{
		method sinf(var f as float)
			is public, external, and floating,
			returns float;

		method MySinf(var f as float)
			is public and floating,
			returns float
		{
			return sinf(f);
		}
	}
}
In the above code, System.Math.sinf is mapped to the sinf function provided by libm; and any C code linked with this binary can call System.Math.MySinf like:

Code: Select all

#include <stdio.h>

extern float MySinf(float f);

int main(int argc, char *argv[])
{
	printf("MySinf(.5) returned %f\n", MySinf(.5f));
	return 0;
}
Locked

Return to “Programming and Security”